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 | struct tree_entry { |
| 113 | u64 start; |
| 114 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 115 | struct rb_node rb_node; |
| 116 | }; |
| 117 | |
| 118 | struct extent_page_data { |
| 119 | struct bio *bio; |
| 120 | struct extent_io_tree *tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 121 | /* tells writepage not to lock the state bits for this range |
| 122 | * it still does the unlocking |
| 123 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 124 | unsigned int extent_locked:1; |
| 125 | |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 126 | /* tells the submit_bio code to use REQ_SYNC */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 127 | unsigned int sync_io:1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 128 | }; |
| 129 | |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 130 | static int add_extent_changeset(struct extent_state *state, unsigned bits, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 131 | struct extent_changeset *changeset, |
| 132 | int set) |
| 133 | { |
| 134 | int ret; |
| 135 | |
| 136 | if (!changeset) |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 137 | return 0; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 138 | if (set && (state->state & bits) == bits) |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 139 | return 0; |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 140 | if (!set && (state->state & bits) == 0) |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 141 | return 0; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 142 | changeset->bytes_changed += state->end - state->start + 1; |
David Sterba | 53d3235 | 2017-02-13 13:42:29 +0100 | [diff] [blame] | 143 | ret = ulist_add(&changeset->range_changed, state->start, state->end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 144 | GFP_ATOMIC); |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 145 | return ret; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 146 | } |
| 147 | |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 148 | static int __must_check submit_one_bio(struct bio *bio, int mirror_num, |
| 149 | unsigned long bio_flags) |
| 150 | { |
| 151 | blk_status_t ret = 0; |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 152 | struct extent_io_tree *tree = bio->bi_private; |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 153 | |
| 154 | bio->bi_private = NULL; |
| 155 | |
| 156 | if (tree->ops) |
| 157 | ret = tree->ops->submit_bio_hook(tree->private_data, bio, |
Nikolay Borisov | 50489a5 | 2019-04-10 19:46:04 +0300 | [diff] [blame] | 158 | mirror_num, bio_flags); |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 159 | else |
| 160 | btrfsic_submit_bio(bio); |
| 161 | |
| 162 | return blk_status_to_errno(ret); |
| 163 | } |
| 164 | |
Qu Wenruo | 3065976 | 2019-03-20 14:27:42 +0800 | [diff] [blame] | 165 | /* Cleanup unsubmitted bios */ |
| 166 | static void end_write_bio(struct extent_page_data *epd, int ret) |
| 167 | { |
| 168 | if (epd->bio) { |
| 169 | epd->bio->bi_status = errno_to_blk_status(ret); |
| 170 | bio_endio(epd->bio); |
| 171 | epd->bio = NULL; |
| 172 | } |
| 173 | } |
| 174 | |
Qu Wenruo | f434062 | 2019-03-20 14:27:41 +0800 | [diff] [blame] | 175 | /* |
| 176 | * Submit bio from extent page data via submit_one_bio |
| 177 | * |
| 178 | * Return 0 if everything is OK. |
| 179 | * Return <0 for error. |
| 180 | */ |
| 181 | static int __must_check flush_write_bio(struct extent_page_data *epd) |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 182 | { |
Qu Wenruo | f434062 | 2019-03-20 14:27:41 +0800 | [diff] [blame] | 183 | int ret = 0; |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 184 | |
Qu Wenruo | f434062 | 2019-03-20 14:27:41 +0800 | [diff] [blame] | 185 | if (epd->bio) { |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 186 | ret = submit_one_bio(epd->bio, 0, 0); |
Qu Wenruo | f434062 | 2019-03-20 14:27:41 +0800 | [diff] [blame] | 187 | /* |
| 188 | * Clean up of epd->bio is handled by its endio function. |
| 189 | * And endio is either triggered by successful bio execution |
| 190 | * or the error handler of submit bio hook. |
| 191 | * So at this point, no matter what happened, we don't need |
| 192 | * to clean up epd->bio. |
| 193 | */ |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 194 | epd->bio = NULL; |
| 195 | } |
Qu Wenruo | f434062 | 2019-03-20 14:27:41 +0800 | [diff] [blame] | 196 | return ret; |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 197 | } |
David Sterba | e2932ee | 2017-06-23 04:16:17 +0200 | [diff] [blame] | 198 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 199 | int __init extent_io_init(void) |
| 200 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 201 | extent_state_cache = kmem_cache_create("btrfs_extent_state", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 202 | sizeof(struct extent_state), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 203 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 204 | if (!extent_state_cache) |
| 205 | return -ENOMEM; |
| 206 | |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 207 | extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 208 | sizeof(struct extent_buffer), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 209 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 210 | if (!extent_buffer_cache) |
| 211 | goto free_state_cache; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 212 | |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 213 | if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE, |
| 214 | offsetof(struct btrfs_io_bio, bio), |
| 215 | BIOSET_NEED_BVECS)) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 216 | goto free_buffer_cache; |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 217 | |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 218 | if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE)) |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 219 | goto free_bioset; |
| 220 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 221 | return 0; |
| 222 | |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 223 | free_bioset: |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 224 | bioset_exit(&btrfs_bioset); |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 225 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 226 | free_buffer_cache: |
| 227 | kmem_cache_destroy(extent_buffer_cache); |
| 228 | extent_buffer_cache = NULL; |
| 229 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 230 | free_state_cache: |
| 231 | kmem_cache_destroy(extent_state_cache); |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 232 | extent_state_cache = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 233 | return -ENOMEM; |
| 234 | } |
| 235 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 236 | void __cold extent_io_exit(void) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 237 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 238 | btrfs_leak_debug_check(); |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 239 | |
| 240 | /* |
| 241 | * Make sure all delayed rcu free are flushed before we |
| 242 | * destroy caches. |
| 243 | */ |
| 244 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 245 | kmem_cache_destroy(extent_state_cache); |
| 246 | kmem_cache_destroy(extent_buffer_cache); |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 247 | bioset_exit(&btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 248 | } |
| 249 | |
Qu Wenruo | c258d6e | 2019-03-01 10:47:58 +0800 | [diff] [blame] | 250 | void extent_io_tree_init(struct btrfs_fs_info *fs_info, |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 251 | struct extent_io_tree *tree, unsigned int owner, |
| 252 | void *private_data) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 253 | { |
Qu Wenruo | c258d6e | 2019-03-01 10:47:58 +0800 | [diff] [blame] | 254 | tree->fs_info = fs_info; |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 255 | tree->state = RB_ROOT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 256 | tree->ops = NULL; |
| 257 | tree->dirty_bytes = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 258 | spin_lock_init(&tree->lock); |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 259 | tree->private_data = private_data; |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 260 | tree->owner = owner; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 261 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 262 | |
Nikolay Borisov | 41e7acd | 2019-03-25 14:31:24 +0200 | [diff] [blame] | 263 | void extent_io_tree_release(struct extent_io_tree *tree) |
| 264 | { |
| 265 | spin_lock(&tree->lock); |
| 266 | /* |
| 267 | * Do a single barrier for the waitqueue_active check here, the state |
| 268 | * of the waitqueue should not change once extent_io_tree_release is |
| 269 | * called. |
| 270 | */ |
| 271 | smp_mb(); |
| 272 | while (!RB_EMPTY_ROOT(&tree->state)) { |
| 273 | struct rb_node *node; |
| 274 | struct extent_state *state; |
| 275 | |
| 276 | node = rb_first(&tree->state); |
| 277 | state = rb_entry(node, struct extent_state, rb_node); |
| 278 | rb_erase(&state->rb_node, &tree->state); |
| 279 | RB_CLEAR_NODE(&state->rb_node); |
| 280 | /* |
| 281 | * btree io trees aren't supposed to have tasks waiting for |
| 282 | * changes in the flags of extent states ever. |
| 283 | */ |
| 284 | ASSERT(!waitqueue_active(&state->wq)); |
| 285 | free_extent_state(state); |
| 286 | |
| 287 | cond_resched_lock(&tree->lock); |
| 288 | } |
| 289 | spin_unlock(&tree->lock); |
| 290 | } |
| 291 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 292 | static struct extent_state *alloc_extent_state(gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 293 | { |
| 294 | struct extent_state *state; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 295 | |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 296 | /* |
| 297 | * The given mask might be not appropriate for the slab allocator, |
| 298 | * drop the unsupported bits |
| 299 | */ |
| 300 | mask &= ~(__GFP_DMA32|__GFP_HIGHMEM); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 301 | state = kmem_cache_alloc(extent_state_cache, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 302 | if (!state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 303 | return state; |
| 304 | state->state = 0; |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 305 | state->failrec = NULL; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 306 | RB_CLEAR_NODE(&state->rb_node); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 307 | btrfs_leak_debug_add(&state->leak_list, &states); |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 308 | refcount_set(&state->refs, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 309 | init_waitqueue_head(&state->wq); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 310 | trace_alloc_extent_state(state, mask, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 311 | return state; |
| 312 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 313 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 314 | void free_extent_state(struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 315 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 316 | if (!state) |
| 317 | return; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 318 | if (refcount_dec_and_test(&state->refs)) { |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 319 | WARN_ON(extent_state_in_tree(state)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 320 | btrfs_leak_debug_del(&state->leak_list); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 321 | trace_free_extent_state(state, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 322 | kmem_cache_free(extent_state_cache, state); |
| 323 | } |
| 324 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 325 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 326 | static struct rb_node *tree_insert(struct rb_root *root, |
| 327 | struct rb_node *search_start, |
| 328 | u64 offset, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 329 | struct rb_node *node, |
| 330 | struct rb_node ***p_in, |
| 331 | struct rb_node **parent_in) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 332 | { |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 333 | struct rb_node **p; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 334 | struct rb_node *parent = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 335 | struct tree_entry *entry; |
| 336 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 337 | if (p_in && parent_in) { |
| 338 | p = *p_in; |
| 339 | parent = *parent_in; |
| 340 | goto do_insert; |
| 341 | } |
| 342 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 343 | p = search_start ? &search_start : &root->rb_node; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 344 | while (*p) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 345 | parent = *p; |
| 346 | entry = rb_entry(parent, struct tree_entry, rb_node); |
| 347 | |
| 348 | if (offset < entry->start) |
| 349 | p = &(*p)->rb_left; |
| 350 | else if (offset > entry->end) |
| 351 | p = &(*p)->rb_right; |
| 352 | else |
| 353 | return parent; |
| 354 | } |
| 355 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 356 | do_insert: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 357 | rb_link_node(node, parent, p); |
| 358 | rb_insert_color(node, root); |
| 359 | return NULL; |
| 360 | } |
| 361 | |
Nikolay Borisov | 8666e63 | 2019-06-05 14:50:04 +0300 | [diff] [blame] | 362 | /** |
| 363 | * __etree_search - searche @tree for an entry that contains @offset. Such |
| 364 | * entry would have entry->start <= offset && entry->end >= offset. |
| 365 | * |
| 366 | * @tree - the tree to search |
| 367 | * @offset - offset that should fall within an entry in @tree |
| 368 | * @next_ret - pointer to the first entry whose range ends after @offset |
| 369 | * @prev - pointer to the first entry whose range begins before @offset |
| 370 | * @p_ret - pointer where new node should be anchored (used when inserting an |
| 371 | * entry in the tree) |
| 372 | * @parent_ret - points to entry which would have been the parent of the entry, |
| 373 | * containing @offset |
| 374 | * |
| 375 | * This function returns a pointer to the entry that contains @offset byte |
| 376 | * address. If no such entry exists, then NULL is returned and the other |
| 377 | * pointer arguments to the function are filled, otherwise the found entry is |
| 378 | * returned and other pointers are left untouched. |
| 379 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 380 | 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] | 381 | struct rb_node **next_ret, |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 382 | struct rb_node **prev_ret, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 383 | struct rb_node ***p_ret, |
| 384 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 385 | { |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 386 | struct rb_root *root = &tree->state; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 387 | struct rb_node **n = &root->rb_node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 388 | struct rb_node *prev = NULL; |
| 389 | struct rb_node *orig_prev = NULL; |
| 390 | struct tree_entry *entry; |
| 391 | struct tree_entry *prev_entry = NULL; |
| 392 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 393 | while (*n) { |
| 394 | prev = *n; |
| 395 | entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 396 | prev_entry = entry; |
| 397 | |
| 398 | if (offset < entry->start) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 399 | n = &(*n)->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 400 | else if (offset > entry->end) |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 401 | n = &(*n)->rb_right; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 402 | else |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 403 | return *n; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 404 | } |
| 405 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 406 | if (p_ret) |
| 407 | *p_ret = n; |
| 408 | if (parent_ret) |
| 409 | *parent_ret = prev; |
| 410 | |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 411 | if (next_ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 412 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 413 | while (prev && offset > prev_entry->end) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 414 | prev = rb_next(prev); |
| 415 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 416 | } |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 417 | *next_ret = prev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 418 | prev = orig_prev; |
| 419 | } |
| 420 | |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 421 | if (prev_ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 422 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 423 | while (prev && offset < prev_entry->start) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 424 | prev = rb_prev(prev); |
| 425 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); |
| 426 | } |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 427 | *prev_ret = prev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 428 | } |
| 429 | return NULL; |
| 430 | } |
| 431 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 432 | static inline struct rb_node * |
| 433 | tree_search_for_insert(struct extent_io_tree *tree, |
| 434 | u64 offset, |
| 435 | struct rb_node ***p_ret, |
| 436 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 437 | { |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 438 | struct rb_node *next= NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 439 | struct rb_node *ret; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 440 | |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 441 | ret = __etree_search(tree, offset, &next, NULL, p_ret, parent_ret); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 442 | if (!ret) |
Nikolay Borisov | 352646c | 2019-01-30 16:51:00 +0200 | [diff] [blame] | 443 | return next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 444 | return ret; |
| 445 | } |
| 446 | |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 447 | static inline struct rb_node *tree_search(struct extent_io_tree *tree, |
| 448 | u64 offset) |
| 449 | { |
| 450 | return tree_search_for_insert(tree, offset, NULL, NULL); |
| 451 | } |
| 452 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 453 | /* |
| 454 | * utility function to look for merge candidates inside a given range. |
| 455 | * Any extents with matching state are merged together into a single |
| 456 | * extent in the tree. Extents with EXTENT_IO in their state field |
| 457 | * are not merged because the end_io handlers need to be able to do |
| 458 | * operations on them without sleeping (or doing allocations/splits). |
| 459 | * |
| 460 | * This should be called with the tree lock held. |
| 461 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 462 | static void merge_state(struct extent_io_tree *tree, |
| 463 | struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 464 | { |
| 465 | struct extent_state *other; |
| 466 | struct rb_node *other_node; |
| 467 | |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 468 | if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 469 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 470 | |
| 471 | other_node = rb_prev(&state->rb_node); |
| 472 | if (other_node) { |
| 473 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 474 | if (other->end == state->start - 1 && |
| 475 | other->state == state->state) { |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 476 | if (tree->private_data && |
| 477 | is_data_inode(tree->private_data)) |
| 478 | btrfs_merge_delalloc_extent(tree->private_data, |
| 479 | state, other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 480 | state->start = other->start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 481 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 482 | RB_CLEAR_NODE(&other->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 483 | free_extent_state(other); |
| 484 | } |
| 485 | } |
| 486 | other_node = rb_next(&state->rb_node); |
| 487 | if (other_node) { |
| 488 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 489 | if (other->start == state->end + 1 && |
| 490 | other->state == state->state) { |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 491 | if (tree->private_data && |
| 492 | is_data_inode(tree->private_data)) |
| 493 | btrfs_merge_delalloc_extent(tree->private_data, |
| 494 | state, other); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 495 | state->end = other->end; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 496 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 497 | RB_CLEAR_NODE(&other->rb_node); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 498 | free_extent_state(other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 499 | } |
| 500 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 501 | } |
| 502 | |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 503 | static void set_state_bits(struct extent_io_tree *tree, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 504 | struct extent_state *state, unsigned *bits, |
| 505 | struct extent_changeset *changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 506 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 507 | /* |
| 508 | * insert an extent_state struct into the tree. 'bits' are set on the |
| 509 | * struct before it is inserted. |
| 510 | * |
| 511 | * This may return -EEXIST if the extent is already there, in which case the |
| 512 | * state struct is freed. |
| 513 | * |
| 514 | * The tree lock is not taken internally. This is a utility function and |
| 515 | * probably isn't what you want to call (see set/clear_extent_bit). |
| 516 | */ |
| 517 | static int insert_state(struct extent_io_tree *tree, |
| 518 | struct extent_state *state, u64 start, u64 end, |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 519 | struct rb_node ***p, |
| 520 | struct rb_node **parent, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 521 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 522 | { |
| 523 | struct rb_node *node; |
| 524 | |
David Sterba | 2792237 | 2019-06-18 20:00:05 +0200 | [diff] [blame] | 525 | if (end < start) { |
| 526 | btrfs_err(tree->fs_info, |
| 527 | "insert state: end < start %llu %llu", end, start); |
| 528 | WARN_ON(1); |
| 529 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 530 | state->start = start; |
| 531 | state->end = end; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 532 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 533 | set_state_bits(tree, state, bits, changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 534 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 535 | node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 536 | if (node) { |
| 537 | struct extent_state *found; |
| 538 | found = rb_entry(node, struct extent_state, rb_node); |
David Sterba | 2792237 | 2019-06-18 20:00:05 +0200 | [diff] [blame] | 539 | btrfs_err(tree->fs_info, |
| 540 | "found node %llu %llu on insert of %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 541 | found->start, found->end, start, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 542 | return -EEXIST; |
| 543 | } |
| 544 | merge_state(tree, state); |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * split a given extent state struct in two, inserting the preallocated |
| 550 | * struct 'prealloc' as the newly created second half. 'split' indicates an |
| 551 | * offset inside 'orig' where it should be split. |
| 552 | * |
| 553 | * Before calling, |
| 554 | * the tree has 'orig' at [orig->start, orig->end]. After calling, there |
| 555 | * are two extent state structs in the tree: |
| 556 | * prealloc: [orig->start, split - 1] |
| 557 | * orig: [ split, orig->end ] |
| 558 | * |
| 559 | * The tree locks are not taken by this function. They need to be held |
| 560 | * by the caller. |
| 561 | */ |
| 562 | static int split_state(struct extent_io_tree *tree, struct extent_state *orig, |
| 563 | struct extent_state *prealloc, u64 split) |
| 564 | { |
| 565 | struct rb_node *node; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 566 | |
Nikolay Borisov | abbb55f | 2018-11-01 14:09:53 +0200 | [diff] [blame] | 567 | if (tree->private_data && is_data_inode(tree->private_data)) |
| 568 | btrfs_split_delalloc_extent(tree->private_data, orig, split); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 569 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 570 | prealloc->start = orig->start; |
| 571 | prealloc->end = split - 1; |
| 572 | prealloc->state = orig->state; |
| 573 | orig->start = split; |
| 574 | |
Filipe Manana | f2071b2 | 2014-02-12 15:05:53 +0000 | [diff] [blame] | 575 | node = tree_insert(&tree->state, &orig->rb_node, prealloc->end, |
| 576 | &prealloc->rb_node, NULL, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 577 | if (node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 578 | free_extent_state(prealloc); |
| 579 | return -EEXIST; |
| 580 | } |
| 581 | return 0; |
| 582 | } |
| 583 | |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 584 | static struct extent_state *next_state(struct extent_state *state) |
| 585 | { |
| 586 | struct rb_node *next = rb_next(&state->rb_node); |
| 587 | if (next) |
| 588 | return rb_entry(next, struct extent_state, rb_node); |
| 589 | else |
| 590 | return NULL; |
| 591 | } |
| 592 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 593 | /* |
| 594 | * utility function to clear some bits in an extent state struct. |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 595 | * it will optionally wake up anyone waiting on this state (wake == 1). |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 596 | * |
| 597 | * If no bits are set on the state struct after clearing things, the |
| 598 | * struct is freed and removed from the tree |
| 599 | */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 600 | static struct extent_state *clear_state_bit(struct extent_io_tree *tree, |
| 601 | struct extent_state *state, |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 602 | unsigned *bits, int wake, |
| 603 | struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 604 | { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 605 | struct extent_state *next; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 606 | unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS; |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 607 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 608 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 609 | if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 610 | u64 range = state->end - state->start + 1; |
| 611 | WARN_ON(range > tree->dirty_bytes); |
| 612 | tree->dirty_bytes -= range; |
| 613 | } |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 614 | |
| 615 | if (tree->private_data && is_data_inode(tree->private_data)) |
| 616 | btrfs_clear_delalloc_extent(tree->private_data, state, bits); |
| 617 | |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 618 | ret = add_extent_changeset(state, bits_to_clear, changeset, 0); |
| 619 | BUG_ON(ret < 0); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 620 | state->state &= ~bits_to_clear; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 621 | if (wake) |
| 622 | wake_up(&state->wq); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 623 | if (state->state == 0) { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 624 | next = next_state(state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 625 | if (extent_state_in_tree(state)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 626 | rb_erase(&state->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 627 | RB_CLEAR_NODE(&state->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 628 | free_extent_state(state); |
| 629 | } else { |
| 630 | WARN_ON(1); |
| 631 | } |
| 632 | } else { |
| 633 | merge_state(tree, state); |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 634 | next = next_state(state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 635 | } |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 636 | return next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 637 | } |
| 638 | |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 639 | static struct extent_state * |
| 640 | alloc_extent_state_atomic(struct extent_state *prealloc) |
| 641 | { |
| 642 | if (!prealloc) |
| 643 | prealloc = alloc_extent_state(GFP_ATOMIC); |
| 644 | |
| 645 | return prealloc; |
| 646 | } |
| 647 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 648 | 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] | 649 | { |
David Sterba | 05912a3 | 2018-07-18 19:23:45 +0200 | [diff] [blame] | 650 | struct inode *inode = tree->private_data; |
| 651 | |
| 652 | btrfs_panic(btrfs_sb(inode->i_sb), err, |
| 653 | "locking error: extent tree was modified by another thread while locked"); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 654 | } |
| 655 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 656 | /* |
| 657 | * clear some bits on a range in the tree. This may require splitting |
| 658 | * or inserting elements in the tree, so the gfp mask is used to |
| 659 | * indicate which allocations or sleeping are allowed. |
| 660 | * |
| 661 | * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove |
| 662 | * the given range from the tree regardless of state (ie for truncate). |
| 663 | * |
| 664 | * the range [start, end] is inclusive. |
| 665 | * |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 666 | * 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] | 667 | */ |
David Sterba | 66b0c88 | 2017-10-31 16:30:47 +0100 | [diff] [blame] | 668 | 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] | 669 | unsigned bits, int wake, int delete, |
| 670 | struct extent_state **cached_state, |
| 671 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 672 | { |
| 673 | struct extent_state *state; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 674 | struct extent_state *cached; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 675 | struct extent_state *prealloc = NULL; |
| 676 | struct rb_node *node; |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 677 | u64 last_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 678 | int err; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 679 | int clear = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 680 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 681 | btrfs_debug_check_extent_io_range(tree, start, end); |
Qu Wenruo | a1d1984 | 2019-03-01 10:48:00 +0800 | [diff] [blame] | 682 | trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 683 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 684 | if (bits & EXTENT_DELALLOC) |
| 685 | bits |= EXTENT_NORESERVE; |
| 686 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 687 | if (delete) |
| 688 | bits |= ~EXTENT_CTLBITS; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 689 | |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 690 | if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY)) |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 691 | clear = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 692 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 693 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
Filipe Manana | c7bc631 | 2014-11-03 14:12:57 +0000 | [diff] [blame] | 694 | /* |
| 695 | * Don't care for allocation failure here because we might end |
| 696 | * up not needing the pre-allocated extent state at all, which |
| 697 | * is the case if we only have in the tree extent states that |
| 698 | * cover our input range and don't cover too any other range. |
| 699 | * If we end up needing a new extent state we allocate it later. |
| 700 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 701 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 702 | } |
| 703 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 704 | spin_lock(&tree->lock); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 705 | if (cached_state) { |
| 706 | cached = *cached_state; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 707 | |
| 708 | if (clear) { |
| 709 | *cached_state = NULL; |
| 710 | cached_state = NULL; |
| 711 | } |
| 712 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 713 | if (cached && extent_state_in_tree(cached) && |
| 714 | cached->start <= start && cached->end > start) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 715 | if (clear) |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 716 | refcount_dec(&cached->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 717 | state = cached; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 718 | goto hit_next; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 719 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 720 | if (clear) |
| 721 | free_extent_state(cached); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 722 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 723 | /* |
| 724 | * this search will find the extents that end after |
| 725 | * our range starts |
| 726 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 727 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 728 | if (!node) |
| 729 | goto out; |
| 730 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 731 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 732 | if (state->start > end) |
| 733 | goto out; |
| 734 | WARN_ON(state->end < start); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 735 | last_end = state->end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 736 | |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 737 | /* the state doesn't have the wanted bits, go ahead */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 738 | if (!(state->state & bits)) { |
| 739 | state = next_state(state); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 740 | goto next; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 741 | } |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 742 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 743 | /* |
| 744 | * | ---- desired range ---- | |
| 745 | * | state | or |
| 746 | * | ------------- state -------------- | |
| 747 | * |
| 748 | * We need to split the extent we found, and may flip |
| 749 | * bits on second half. |
| 750 | * |
| 751 | * If the extent we found extends past our range, we |
| 752 | * just split and search again. It'll get split again |
| 753 | * the next time though. |
| 754 | * |
| 755 | * If the extent we found is inside our range, we clear |
| 756 | * the desired bit on it. |
| 757 | */ |
| 758 | |
| 759 | if (state->start < start) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 760 | prealloc = alloc_extent_state_atomic(prealloc); |
| 761 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 762 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 763 | if (err) |
| 764 | extent_io_tree_panic(tree, err); |
| 765 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 766 | prealloc = NULL; |
| 767 | if (err) |
| 768 | goto out; |
| 769 | if (state->end <= end) { |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 770 | state = clear_state_bit(tree, state, &bits, wake, |
| 771 | changeset); |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 772 | goto next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 773 | } |
| 774 | goto search_again; |
| 775 | } |
| 776 | /* |
| 777 | * | ---- desired range ---- | |
| 778 | * | state | |
| 779 | * We need to split the extent, and clear the bit |
| 780 | * on the first half |
| 781 | */ |
| 782 | if (state->start <= end && state->end > end) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 783 | prealloc = alloc_extent_state_atomic(prealloc); |
| 784 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 785 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 786 | if (err) |
| 787 | extent_io_tree_panic(tree, err); |
| 788 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 789 | if (wake) |
| 790 | wake_up(&state->wq); |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 791 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 792 | clear_state_bit(tree, prealloc, &bits, wake, changeset); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 793 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 794 | prealloc = NULL; |
| 795 | goto out; |
| 796 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 797 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 798 | state = clear_state_bit(tree, state, &bits, wake, changeset); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 799 | next: |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 800 | if (last_end == (u64)-1) |
| 801 | goto out; |
| 802 | start = last_end + 1; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 803 | if (start <= end && state && !need_resched()) |
Liu Bo | 692e575 | 2012-02-16 18:34:36 +0800 | [diff] [blame] | 804 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 805 | |
| 806 | search_again: |
| 807 | if (start > end) |
| 808 | goto out; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 809 | spin_unlock(&tree->lock); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 810 | if (gfpflags_allow_blocking(mask)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 811 | cond_resched(); |
| 812 | goto again; |
David Sterba | 7ab5cb2 | 2016-04-27 01:02:15 +0200 | [diff] [blame] | 813 | |
| 814 | out: |
| 815 | spin_unlock(&tree->lock); |
| 816 | if (prealloc) |
| 817 | free_extent_state(prealloc); |
| 818 | |
| 819 | return 0; |
| 820 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 821 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 822 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 823 | static void wait_on_state(struct extent_io_tree *tree, |
| 824 | struct extent_state *state) |
Christoph Hellwig | 641f521 | 2008-12-02 06:36:10 -0500 | [diff] [blame] | 825 | __releases(tree->lock) |
| 826 | __acquires(tree->lock) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 827 | { |
| 828 | DEFINE_WAIT(wait); |
| 829 | prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 830 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 831 | schedule(); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 832 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 833 | finish_wait(&state->wq, &wait); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /* |
| 837 | * waits for one or more bits to clear on a range in the state tree. |
| 838 | * The range [start, end] is inclusive. |
| 839 | * The tree lock is taken by this function |
| 840 | */ |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 841 | static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 842 | unsigned long bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 843 | { |
| 844 | struct extent_state *state; |
| 845 | struct rb_node *node; |
| 846 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 847 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 848 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 849 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 850 | again: |
| 851 | while (1) { |
| 852 | /* |
| 853 | * this search will find all the extents that end after |
| 854 | * our range starts |
| 855 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 856 | node = tree_search(tree, start); |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 857 | process_node: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 858 | if (!node) |
| 859 | break; |
| 860 | |
| 861 | state = rb_entry(node, struct extent_state, rb_node); |
| 862 | |
| 863 | if (state->start > end) |
| 864 | goto out; |
| 865 | |
| 866 | if (state->state & bits) { |
| 867 | start = state->start; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 868 | refcount_inc(&state->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 869 | wait_on_state(tree, state); |
| 870 | free_extent_state(state); |
| 871 | goto again; |
| 872 | } |
| 873 | start = state->end + 1; |
| 874 | |
| 875 | if (start > end) |
| 876 | break; |
| 877 | |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 878 | if (!cond_resched_lock(&tree->lock)) { |
| 879 | node = rb_next(node); |
| 880 | goto process_node; |
| 881 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 882 | } |
| 883 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 884 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 885 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 886 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 887 | static void set_state_bits(struct extent_io_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 888 | struct extent_state *state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 889 | unsigned *bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 890 | { |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 891 | unsigned bits_to_set = *bits & ~EXTENT_CTLBITS; |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 892 | int ret; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 893 | |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 894 | if (tree->private_data && is_data_inode(tree->private_data)) |
| 895 | btrfs_set_delalloc_extent(tree->private_data, state, bits); |
| 896 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 897 | if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 898 | u64 range = state->end - state->start + 1; |
| 899 | tree->dirty_bytes += range; |
| 900 | } |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 901 | ret = add_extent_changeset(state, bits_to_set, changeset, 1); |
| 902 | BUG_ON(ret < 0); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 903 | state->state |= bits_to_set; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 904 | } |
| 905 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 906 | static void cache_state_if_flags(struct extent_state *state, |
| 907 | struct extent_state **cached_ptr, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 908 | unsigned flags) |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 909 | { |
| 910 | if (cached_ptr && !(*cached_ptr)) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 911 | if (!flags || (state->state & flags)) { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 912 | *cached_ptr = state; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 913 | refcount_inc(&state->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 918 | static void cache_state(struct extent_state *state, |
| 919 | struct extent_state **cached_ptr) |
| 920 | { |
| 921 | return cache_state_if_flags(state, cached_ptr, |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 922 | EXTENT_LOCKED | EXTENT_BOUNDARY); |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 923 | } |
| 924 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 925 | /* |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 926 | * set some bits on a range in the tree. This may require allocations or |
| 927 | * sleeping, so the gfp mask is used to indicate what is allowed. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 928 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 929 | * If any of the exclusive bits are set, this will fail with -EEXIST if some |
| 930 | * part of the range already has the desired bits set. The start of the |
| 931 | * existing range is returned in failed_start in this case. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 932 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 933 | * [start, end] is inclusive This takes the tree lock. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 934 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 935 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 936 | static int __must_check |
| 937 | __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 938 | unsigned bits, unsigned exclusive_bits, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 939 | u64 *failed_start, struct extent_state **cached_state, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 940 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 941 | { |
| 942 | struct extent_state *state; |
| 943 | struct extent_state *prealloc = NULL; |
| 944 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 945 | struct rb_node **p; |
| 946 | struct rb_node *parent; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 947 | int err = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 948 | u64 last_start; |
| 949 | u64 last_end; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 950 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 951 | btrfs_debug_check_extent_io_range(tree, start, end); |
Qu Wenruo | a1d1984 | 2019-03-01 10:48:00 +0800 | [diff] [blame] | 952 | trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 953 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 954 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 955 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
David Sterba | 059f791 | 2016-04-27 01:03:45 +0200 | [diff] [blame] | 956 | /* |
| 957 | * Don't care for allocation failure here because we might end |
| 958 | * up not needing the pre-allocated extent state at all, which |
| 959 | * is the case if we only have in the tree extent states that |
| 960 | * cover our input range and don't cover too any other range. |
| 961 | * If we end up needing a new extent state we allocate it later. |
| 962 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 963 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 964 | } |
| 965 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 966 | spin_lock(&tree->lock); |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 967 | if (cached_state && *cached_state) { |
| 968 | state = *cached_state; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 969 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 970 | extent_state_in_tree(state)) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 971 | node = &state->rb_node; |
| 972 | goto hit_next; |
| 973 | } |
| 974 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 975 | /* |
| 976 | * this search will find all the extents that end after |
| 977 | * our range starts. |
| 978 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 979 | node = tree_search_for_insert(tree, start, &p, &parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 980 | if (!node) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 981 | prealloc = alloc_extent_state_atomic(prealloc); |
| 982 | BUG_ON(!prealloc); |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 983 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 984 | &p, &parent, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 985 | if (err) |
| 986 | extent_io_tree_panic(tree, err); |
| 987 | |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 988 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 989 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 990 | goto out; |
| 991 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 992 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 40431d6 | 2009-08-05 12:57:59 -0400 | [diff] [blame] | 993 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 994 | last_start = state->start; |
| 995 | last_end = state->end; |
| 996 | |
| 997 | /* |
| 998 | * | ---- desired range ---- | |
| 999 | * | state | |
| 1000 | * |
| 1001 | * Just lock what we found and keep going |
| 1002 | */ |
| 1003 | if (state->start == start && state->end <= end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1004 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1005 | *failed_start = state->start; |
| 1006 | err = -EEXIST; |
| 1007 | goto out; |
| 1008 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 1009 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1010 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1011 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1012 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 1013 | if (last_end == (u64)-1) |
| 1014 | goto out; |
| 1015 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1016 | state = next_state(state); |
| 1017 | if (start < end && state && state->start == start && |
| 1018 | !need_resched()) |
| 1019 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1020 | goto search_again; |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | * | ---- desired range ---- | |
| 1025 | * | state | |
| 1026 | * or |
| 1027 | * | ------------- state -------------- | |
| 1028 | * |
| 1029 | * We need to split the extent we found, and may flip bits on |
| 1030 | * second half. |
| 1031 | * |
| 1032 | * If the extent we found extends past our |
| 1033 | * range, we just split and search again. It'll get split |
| 1034 | * again the next time though. |
| 1035 | * |
| 1036 | * If the extent we found is inside our range, we set the |
| 1037 | * desired bit on it. |
| 1038 | */ |
| 1039 | if (state->start < start) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1040 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1041 | *failed_start = start; |
| 1042 | err = -EEXIST; |
| 1043 | goto out; |
| 1044 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1045 | |
| 1046 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1047 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1048 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1049 | if (err) |
| 1050 | extent_io_tree_panic(tree, err); |
| 1051 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1052 | prealloc = NULL; |
| 1053 | if (err) |
| 1054 | goto out; |
| 1055 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1056 | set_state_bits(tree, state, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1057 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1058 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 1059 | if (last_end == (u64)-1) |
| 1060 | goto out; |
| 1061 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1062 | state = next_state(state); |
| 1063 | if (start < end && state && state->start == start && |
| 1064 | !need_resched()) |
| 1065 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1066 | } |
| 1067 | goto search_again; |
| 1068 | } |
| 1069 | /* |
| 1070 | * | ---- desired range ---- | |
| 1071 | * | state | or | state | |
| 1072 | * |
| 1073 | * There's a hole, we need to insert something in it and |
| 1074 | * ignore the extent we found. |
| 1075 | */ |
| 1076 | if (state->start > start) { |
| 1077 | u64 this_end; |
| 1078 | if (end < last_start) |
| 1079 | this_end = end; |
| 1080 | else |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1081 | this_end = last_start - 1; |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1082 | |
| 1083 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1084 | BUG_ON(!prealloc); |
Xiao Guangrong | c7f895a | 2011-04-20 06:45:49 +0000 | [diff] [blame] | 1085 | |
| 1086 | /* |
| 1087 | * Avoid to free 'prealloc' if it can be merged with |
| 1088 | * the later extent. |
| 1089 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1090 | err = insert_state(tree, prealloc, start, this_end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1091 | NULL, NULL, &bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1092 | if (err) |
| 1093 | extent_io_tree_panic(tree, err); |
| 1094 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1095 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1096 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1097 | start = this_end + 1; |
| 1098 | goto search_again; |
| 1099 | } |
| 1100 | /* |
| 1101 | * | ---- desired range ---- | |
| 1102 | * | state | |
| 1103 | * We need to split the extent, and set the bit |
| 1104 | * on the first half |
| 1105 | */ |
| 1106 | if (state->start <= end && state->end > end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1107 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1108 | *failed_start = start; |
| 1109 | err = -EEXIST; |
| 1110 | goto out; |
| 1111 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1112 | |
| 1113 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1114 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1115 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1116 | if (err) |
| 1117 | extent_io_tree_panic(tree, err); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1118 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1119 | set_state_bits(tree, prealloc, &bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1120 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1121 | merge_state(tree, prealloc); |
| 1122 | prealloc = NULL; |
| 1123 | goto out; |
| 1124 | } |
| 1125 | |
David Sterba | b5a4ba14 | 2016-04-27 01:02:15 +0200 | [diff] [blame] | 1126 | search_again: |
| 1127 | if (start > end) |
| 1128 | goto out; |
| 1129 | spin_unlock(&tree->lock); |
| 1130 | if (gfpflags_allow_blocking(mask)) |
| 1131 | cond_resched(); |
| 1132 | goto again; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1133 | |
| 1134 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1135 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1136 | if (prealloc) |
| 1137 | free_extent_state(prealloc); |
| 1138 | |
| 1139 | return err; |
| 1140 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1141 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1142 | |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1143 | 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] | 1144 | unsigned bits, u64 * failed_start, |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 1145 | struct extent_state **cached_state, gfp_t mask) |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1146 | { |
| 1147 | return __set_extent_bit(tree, start, end, bits, 0, failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1148 | cached_state, mask, NULL); |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1152 | /** |
Liu Bo | 10983f2 | 2012-07-11 15:26:19 +0800 | [diff] [blame] | 1153 | * convert_extent_bit - convert all bits in a given range from one bit to |
| 1154 | * another |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1155 | * @tree: the io tree to search |
| 1156 | * @start: the start offset in bytes |
| 1157 | * @end: the end offset in bytes (inclusive) |
| 1158 | * @bits: the bits to set in this range |
| 1159 | * @clear_bits: the bits to clear in this range |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1160 | * @cached_state: state that we're going to cache |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1161 | * |
| 1162 | * This will go through and set bits for the given range. If any states exist |
| 1163 | * already in this range they are set with the given bit and cleared of the |
| 1164 | * clear_bits. This is only meant to be used by things that are mergeable, ie |
| 1165 | * converting from say DELALLOC to DIRTY. This is not meant to be used with |
| 1166 | * boundary bits like LOCK. |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1167 | * |
| 1168 | * All allocations are done with GFP_NOFS. |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1169 | */ |
| 1170 | 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] | 1171 | unsigned bits, unsigned clear_bits, |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1172 | struct extent_state **cached_state) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1173 | { |
| 1174 | struct extent_state *state; |
| 1175 | struct extent_state *prealloc = NULL; |
| 1176 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1177 | struct rb_node **p; |
| 1178 | struct rb_node *parent; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1179 | int err = 0; |
| 1180 | u64 last_start; |
| 1181 | u64 last_end; |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1182 | bool first_iteration = true; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1183 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 1184 | btrfs_debug_check_extent_io_range(tree, start, end); |
Qu Wenruo | a1d1984 | 2019-03-01 10:48:00 +0800 | [diff] [blame] | 1185 | trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits, |
| 1186 | clear_bits); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 1187 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1188 | again: |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1189 | if (!prealloc) { |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1190 | /* |
| 1191 | * Best effort, don't worry if extent state allocation fails |
| 1192 | * here for the first iteration. We might have a cached state |
| 1193 | * that matches exactly the target range, in which case no |
| 1194 | * extent state allocations are needed. We'll only know this |
| 1195 | * after locking the tree. |
| 1196 | */ |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1197 | prealloc = alloc_extent_state(GFP_NOFS); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1198 | if (!prealloc && !first_iteration) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1199 | return -ENOMEM; |
| 1200 | } |
| 1201 | |
| 1202 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1203 | if (cached_state && *cached_state) { |
| 1204 | state = *cached_state; |
| 1205 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1206 | extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1207 | node = &state->rb_node; |
| 1208 | goto hit_next; |
| 1209 | } |
| 1210 | } |
| 1211 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1212 | /* |
| 1213 | * this search will find all the extents that end after |
| 1214 | * our range starts. |
| 1215 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1216 | node = tree_search_for_insert(tree, start, &p, &parent); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1217 | if (!node) { |
| 1218 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1219 | if (!prealloc) { |
| 1220 | err = -ENOMEM; |
| 1221 | goto out; |
| 1222 | } |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1223 | err = insert_state(tree, prealloc, start, end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1224 | &p, &parent, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1225 | if (err) |
| 1226 | extent_io_tree_panic(tree, err); |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 1227 | cache_state(prealloc, cached_state); |
| 1228 | prealloc = NULL; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1229 | goto out; |
| 1230 | } |
| 1231 | state = rb_entry(node, struct extent_state, rb_node); |
| 1232 | hit_next: |
| 1233 | last_start = state->start; |
| 1234 | last_end = state->end; |
| 1235 | |
| 1236 | /* |
| 1237 | * | ---- desired range ---- | |
| 1238 | * | state | |
| 1239 | * |
| 1240 | * Just lock what we found and keep going |
| 1241 | */ |
| 1242 | if (state->start == start && state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1243 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1244 | cache_state(state, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1245 | state = clear_state_bit(tree, state, &clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1246 | if (last_end == (u64)-1) |
| 1247 | goto out; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1248 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1249 | if (start < end && state && state->start == start && |
| 1250 | !need_resched()) |
| 1251 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1252 | goto search_again; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * | ---- desired range ---- | |
| 1257 | * | state | |
| 1258 | * or |
| 1259 | * | ------------- state -------------- | |
| 1260 | * |
| 1261 | * We need to split the extent we found, and may flip bits on |
| 1262 | * second half. |
| 1263 | * |
| 1264 | * If the extent we found extends past our |
| 1265 | * range, we just split and search again. It'll get split |
| 1266 | * again the next time though. |
| 1267 | * |
| 1268 | * If the extent we found is inside our range, we set the |
| 1269 | * desired bit on it. |
| 1270 | */ |
| 1271 | if (state->start < start) { |
| 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 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1278 | if (err) |
| 1279 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1280 | prealloc = NULL; |
| 1281 | if (err) |
| 1282 | goto out; |
| 1283 | if (state->end <= end) { |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1284 | set_state_bits(tree, state, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1285 | cache_state(state, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1286 | state = clear_state_bit(tree, state, &clear_bits, 0, |
| 1287 | NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1288 | if (last_end == (u64)-1) |
| 1289 | goto out; |
| 1290 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1291 | if (start < end && state && state->start == start && |
| 1292 | !need_resched()) |
| 1293 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1294 | } |
| 1295 | goto search_again; |
| 1296 | } |
| 1297 | /* |
| 1298 | * | ---- desired range ---- | |
| 1299 | * | state | or | state | |
| 1300 | * |
| 1301 | * There's a hole, we need to insert something in it and |
| 1302 | * ignore the extent we found. |
| 1303 | */ |
| 1304 | if (state->start > start) { |
| 1305 | u64 this_end; |
| 1306 | if (end < last_start) |
| 1307 | this_end = end; |
| 1308 | else |
| 1309 | this_end = last_start - 1; |
| 1310 | |
| 1311 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1312 | if (!prealloc) { |
| 1313 | err = -ENOMEM; |
| 1314 | goto out; |
| 1315 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1316 | |
| 1317 | /* |
| 1318 | * Avoid to free 'prealloc' if it can be merged with |
| 1319 | * the later extent. |
| 1320 | */ |
| 1321 | err = insert_state(tree, prealloc, start, this_end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1322 | NULL, NULL, &bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1323 | if (err) |
| 1324 | extent_io_tree_panic(tree, err); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1325 | cache_state(prealloc, cached_state); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1326 | prealloc = NULL; |
| 1327 | start = this_end + 1; |
| 1328 | goto search_again; |
| 1329 | } |
| 1330 | /* |
| 1331 | * | ---- desired range ---- | |
| 1332 | * | state | |
| 1333 | * We need to split the extent, and set the bit |
| 1334 | * on the first half |
| 1335 | */ |
| 1336 | if (state->start <= end && state->end > end) { |
| 1337 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1338 | if (!prealloc) { |
| 1339 | err = -ENOMEM; |
| 1340 | goto out; |
| 1341 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1342 | |
| 1343 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1344 | if (err) |
| 1345 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1346 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1347 | set_state_bits(tree, prealloc, &bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1348 | cache_state(prealloc, cached_state); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1349 | clear_state_bit(tree, prealloc, &clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1350 | prealloc = NULL; |
| 1351 | goto out; |
| 1352 | } |
| 1353 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1354 | search_again: |
| 1355 | if (start > end) |
| 1356 | goto out; |
| 1357 | spin_unlock(&tree->lock); |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1358 | cond_resched(); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1359 | first_iteration = false; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1360 | goto again; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1361 | |
| 1362 | out: |
| 1363 | spin_unlock(&tree->lock); |
| 1364 | if (prealloc) |
| 1365 | free_extent_state(prealloc); |
| 1366 | |
| 1367 | return err; |
| 1368 | } |
| 1369 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1370 | /* wrappers around set/clear extent bit */ |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1371 | 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] | 1372 | unsigned bits, struct extent_changeset *changeset) |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1373 | { |
| 1374 | /* |
| 1375 | * We don't support EXTENT_LOCKED yet, as current changeset will |
| 1376 | * record any bits changed, so for EXTENT_LOCKED case, it will |
| 1377 | * either fail with -EEXIST or changeset will record the whole |
| 1378 | * range. |
| 1379 | */ |
| 1380 | BUG_ON(bits & EXTENT_LOCKED); |
| 1381 | |
David Sterba | 2c53b91 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1382 | 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] | 1383 | changeset); |
| 1384 | } |
| 1385 | |
Nikolay Borisov | 4ca7365 | 2019-03-27 14:24:10 +0200 | [diff] [blame] | 1386 | int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end, |
| 1387 | unsigned bits) |
| 1388 | { |
| 1389 | return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, |
| 1390 | GFP_NOWAIT, NULL); |
| 1391 | } |
| 1392 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1393 | int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
| 1394 | unsigned bits, int wake, int delete, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1395 | struct extent_state **cached) |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1396 | { |
| 1397 | return __clear_extent_bit(tree, start, end, bits, wake, delete, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1398 | cached, GFP_NOFS, NULL); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1399 | } |
| 1400 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1401 | 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] | 1402 | unsigned bits, struct extent_changeset *changeset) |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1403 | { |
| 1404 | /* |
| 1405 | * Don't support EXTENT_LOCKED case, same reason as |
| 1406 | * set_record_extent_bits(). |
| 1407 | */ |
| 1408 | BUG_ON(bits & EXTENT_LOCKED); |
| 1409 | |
David Sterba | f734c44 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1410 | 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] | 1411 | changeset); |
| 1412 | } |
| 1413 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1414 | /* |
| 1415 | * either insert or lock state struct between start and end use mask to tell |
| 1416 | * us if waiting is desired. |
| 1417 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1418 | 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] | 1419 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1420 | { |
| 1421 | int err; |
| 1422 | u64 failed_start; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1423 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1424 | while (1) { |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1425 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1426 | EXTENT_LOCKED, &failed_start, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1427 | cached_state, GFP_NOFS, NULL); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1428 | if (err == -EEXIST) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1429 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
| 1430 | start = failed_start; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1431 | } else |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1432 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1433 | WARN_ON(start > end); |
| 1434 | } |
| 1435 | return err; |
| 1436 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1437 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1438 | 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] | 1439 | { |
| 1440 | int err; |
| 1441 | u64 failed_start; |
| 1442 | |
Jeff Mahoney | 3fbe5c0 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1443 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1444 | &failed_start, NULL, GFP_NOFS, NULL); |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1445 | if (err == -EEXIST) { |
| 1446 | if (failed_start > start) |
| 1447 | clear_extent_bit(tree, start, failed_start - 1, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1448 | EXTENT_LOCKED, 1, 0, NULL); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1449 | return 0; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1450 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1451 | return 1; |
| 1452 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1453 | |
David Sterba | bd1fa4f | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1454 | 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] | 1455 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1456 | unsigned long index = start >> PAGE_SHIFT; |
| 1457 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1458 | struct page *page; |
| 1459 | |
| 1460 | while (index <= end_index) { |
| 1461 | page = find_get_page(inode->i_mapping, index); |
| 1462 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
| 1463 | clear_page_dirty_for_io(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1464 | put_page(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1465 | index++; |
| 1466 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1467 | } |
| 1468 | |
David Sterba | f631157 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1469 | 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] | 1470 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1471 | unsigned long index = start >> PAGE_SHIFT; |
| 1472 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1473 | struct page *page; |
| 1474 | |
| 1475 | while (index <= end_index) { |
| 1476 | page = find_get_page(inode->i_mapping, index); |
| 1477 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1478 | __set_page_dirty_nobuffers(page); |
Konstantin Khebnikov | 8d38633 | 2015-02-11 15:26:55 -0800 | [diff] [blame] | 1479 | account_page_redirty(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1480 | put_page(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1481 | index++; |
| 1482 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1483 | } |
| 1484 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1485 | /* find the first state struct with 'bits' set after 'start', and |
| 1486 | * return it. tree->lock must be held. NULL will returned if |
| 1487 | * nothing was found after 'start' |
| 1488 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1489 | static struct extent_state * |
| 1490 | find_first_extent_bit_state(struct extent_io_tree *tree, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1491 | u64 start, unsigned bits) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1492 | { |
| 1493 | struct rb_node *node; |
| 1494 | struct extent_state *state; |
| 1495 | |
| 1496 | /* |
| 1497 | * this search will find all the extents that end after |
| 1498 | * our range starts. |
| 1499 | */ |
| 1500 | node = tree_search(tree, start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1501 | if (!node) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1502 | goto out; |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1503 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1504 | while (1) { |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1505 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1506 | if (state->end >= start && (state->state & bits)) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1507 | return state; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1508 | |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1509 | node = rb_next(node); |
| 1510 | if (!node) |
| 1511 | break; |
| 1512 | } |
| 1513 | out: |
| 1514 | return NULL; |
| 1515 | } |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1516 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1517 | /* |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1518 | * find the first offset in the io tree with 'bits' set. zero is |
| 1519 | * returned if we find something, and *start_ret and *end_ret are |
| 1520 | * set to reflect the state struct that was found. |
| 1521 | * |
Wang Sheng-Hui | 477d7ea | 2012-04-06 14:35:47 +0800 | [diff] [blame] | 1522 | * If nothing was found, 1 is returned. If found something, return 0. |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1523 | */ |
| 1524 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1525 | u64 *start_ret, u64 *end_ret, unsigned bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1526 | struct extent_state **cached_state) |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1527 | { |
| 1528 | struct extent_state *state; |
| 1529 | int ret = 1; |
| 1530 | |
| 1531 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1532 | if (cached_state && *cached_state) { |
| 1533 | state = *cached_state; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1534 | if (state->end == start - 1 && extent_state_in_tree(state)) { |
Liu Bo | 9688e9a | 2018-08-23 03:14:53 +0800 | [diff] [blame] | 1535 | while ((state = next_state(state)) != NULL) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1536 | if (state->state & bits) |
| 1537 | goto got_it; |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1538 | } |
| 1539 | free_extent_state(*cached_state); |
| 1540 | *cached_state = NULL; |
| 1541 | goto out; |
| 1542 | } |
| 1543 | free_extent_state(*cached_state); |
| 1544 | *cached_state = NULL; |
| 1545 | } |
| 1546 | |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1547 | state = find_first_extent_bit_state(tree, start, bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1548 | got_it: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1549 | if (state) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 1550 | cache_state_if_flags(state, cached_state, 0); |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1551 | *start_ret = state->start; |
| 1552 | *end_ret = state->end; |
| 1553 | ret = 0; |
| 1554 | } |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1555 | out: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1556 | spin_unlock(&tree->lock); |
| 1557 | return ret; |
| 1558 | } |
| 1559 | |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1560 | /** |
Nikolay Borisov | 1eaebb3 | 2019-06-03 13:06:02 +0300 | [diff] [blame] | 1561 | * find_first_clear_extent_bit - find the first range that has @bits not set. |
| 1562 | * This range could start before @start. |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1563 | * |
| 1564 | * @tree - the tree to search |
| 1565 | * @start - the offset at/after which the found extent should start |
| 1566 | * @start_ret - records the beginning of the range |
| 1567 | * @end_ret - records the end of the range (inclusive) |
| 1568 | * @bits - the set of bits which must be unset |
| 1569 | * |
| 1570 | * Since unallocated range is also considered one which doesn't have the bits |
| 1571 | * set it's possible that @end_ret contains -1, this happens in case the range |
| 1572 | * spans (last_range_end, end of device]. In this case it's up to the caller to |
| 1573 | * trim @end_ret to the appropriate size. |
| 1574 | */ |
| 1575 | void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start, |
| 1576 | u64 *start_ret, u64 *end_ret, unsigned bits) |
| 1577 | { |
| 1578 | struct extent_state *state; |
| 1579 | struct rb_node *node, *prev = NULL, *next; |
| 1580 | |
| 1581 | spin_lock(&tree->lock); |
| 1582 | |
| 1583 | /* Find first extent with bits cleared */ |
| 1584 | while (1) { |
| 1585 | node = __etree_search(tree, start, &next, &prev, NULL, NULL); |
| 1586 | if (!node) { |
| 1587 | node = next; |
| 1588 | if (!node) { |
| 1589 | /* |
| 1590 | * We are past the last allocated chunk, |
| 1591 | * set start at the end of the last extent. The |
| 1592 | * device alloc tree should never be empty so |
| 1593 | * prev is always set. |
| 1594 | */ |
| 1595 | ASSERT(prev); |
| 1596 | state = rb_entry(prev, struct extent_state, rb_node); |
| 1597 | *start_ret = state->end + 1; |
| 1598 | *end_ret = -1; |
| 1599 | goto out; |
| 1600 | } |
| 1601 | } |
Nikolay Borisov | 1eaebb3 | 2019-06-03 13:06:02 +0300 | [diff] [blame] | 1602 | /* |
| 1603 | * At this point 'node' either contains 'start' or start is |
| 1604 | * before 'node' |
| 1605 | */ |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1606 | state = rb_entry(node, struct extent_state, rb_node); |
Nikolay Borisov | 1eaebb3 | 2019-06-03 13:06:02 +0300 | [diff] [blame] | 1607 | |
| 1608 | if (in_range(start, state->start, state->end - state->start + 1)) { |
| 1609 | if (state->state & bits) { |
| 1610 | /* |
| 1611 | * |--range with bits sets--| |
| 1612 | * | |
| 1613 | * start |
| 1614 | */ |
| 1615 | start = state->end + 1; |
| 1616 | } else { |
| 1617 | /* |
| 1618 | * 'start' falls within a range that doesn't |
| 1619 | * have the bits set, so take its start as |
| 1620 | * the beginning of the desired range |
| 1621 | * |
| 1622 | * |--range with bits cleared----| |
| 1623 | * | |
| 1624 | * start |
| 1625 | */ |
| 1626 | *start_ret = state->start; |
| 1627 | break; |
| 1628 | } |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1629 | } else { |
Nikolay Borisov | 1eaebb3 | 2019-06-03 13:06:02 +0300 | [diff] [blame] | 1630 | /* |
| 1631 | * |---prev range---|---hole/unset---|---node range---| |
| 1632 | * | |
| 1633 | * start |
| 1634 | * |
| 1635 | * or |
| 1636 | * |
| 1637 | * |---hole/unset--||--first node--| |
| 1638 | * 0 | |
| 1639 | * start |
| 1640 | */ |
| 1641 | if (prev) { |
| 1642 | state = rb_entry(prev, struct extent_state, |
| 1643 | rb_node); |
| 1644 | *start_ret = state->end + 1; |
| 1645 | } else { |
| 1646 | *start_ret = 0; |
| 1647 | } |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1648 | break; |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | /* |
| 1653 | * Find the longest stretch from start until an entry which has the |
| 1654 | * bits set |
| 1655 | */ |
| 1656 | while (1) { |
| 1657 | state = rb_entry(node, struct extent_state, rb_node); |
| 1658 | if (state->end >= start && !(state->state & bits)) { |
| 1659 | *end_ret = state->end; |
| 1660 | } else { |
| 1661 | *end_ret = state->start - 1; |
| 1662 | break; |
| 1663 | } |
| 1664 | |
| 1665 | node = rb_next(node); |
| 1666 | if (!node) |
| 1667 | break; |
| 1668 | } |
| 1669 | out: |
| 1670 | spin_unlock(&tree->lock); |
| 1671 | } |
| 1672 | |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1673 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1674 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1675 | * more than 'max_bytes'. start and end are used to return the range, |
| 1676 | * |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1677 | * 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] | 1678 | */ |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1679 | static noinline bool find_delalloc_range(struct extent_io_tree *tree, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1680 | u64 *start, u64 *end, u64 max_bytes, |
| 1681 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1682 | { |
| 1683 | struct rb_node *node; |
| 1684 | struct extent_state *state; |
| 1685 | u64 cur_start = *start; |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1686 | bool found = false; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1687 | u64 total_bytes = 0; |
| 1688 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1689 | spin_lock(&tree->lock); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1690 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1691 | /* |
| 1692 | * this search will find all the extents that end after |
| 1693 | * our range starts. |
| 1694 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1695 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1696 | if (!node) { |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1697 | *end = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1698 | goto out; |
| 1699 | } |
| 1700 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1701 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1702 | state = rb_entry(node, struct extent_state, rb_node); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1703 | if (found && (state->start != cur_start || |
| 1704 | (state->state & EXTENT_BOUNDARY))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1705 | goto out; |
| 1706 | } |
| 1707 | if (!(state->state & EXTENT_DELALLOC)) { |
| 1708 | if (!found) |
| 1709 | *end = state->end; |
| 1710 | goto out; |
| 1711 | } |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1712 | if (!found) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1713 | *start = state->start; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1714 | *cached_state = state; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 1715 | refcount_inc(&state->refs); |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1716 | } |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1717 | found = true; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1718 | *end = state->end; |
| 1719 | cur_start = state->end + 1; |
| 1720 | node = rb_next(node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1721 | total_bytes += state->end - state->start + 1; |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1722 | if (total_bytes >= max_bytes) |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1723 | break; |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1724 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1725 | break; |
| 1726 | } |
| 1727 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1728 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1729 | return found; |
| 1730 | } |
| 1731 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1732 | static int __process_pages_contig(struct address_space *mapping, |
| 1733 | struct page *locked_page, |
| 1734 | pgoff_t start_index, pgoff_t end_index, |
| 1735 | unsigned long page_ops, pgoff_t *index_ret); |
| 1736 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1737 | static noinline void __unlock_for_delalloc(struct inode *inode, |
| 1738 | struct page *locked_page, |
| 1739 | u64 start, u64 end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1740 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1741 | unsigned long index = start >> PAGE_SHIFT; |
| 1742 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1743 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1744 | ASSERT(locked_page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1745 | if (index == locked_page->index && end_index == index) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1746 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1747 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1748 | __process_pages_contig(inode->i_mapping, locked_page, index, end_index, |
| 1749 | PAGE_UNLOCK, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | static noinline int lock_delalloc_pages(struct inode *inode, |
| 1753 | struct page *locked_page, |
| 1754 | u64 delalloc_start, |
| 1755 | u64 delalloc_end) |
| 1756 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1757 | unsigned long index = delalloc_start >> PAGE_SHIFT; |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1758 | unsigned long index_ret = index; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1759 | unsigned long end_index = delalloc_end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1760 | int ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1761 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1762 | ASSERT(locked_page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1763 | if (index == locked_page->index && index == end_index) |
| 1764 | return 0; |
| 1765 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1766 | ret = __process_pages_contig(inode->i_mapping, locked_page, index, |
| 1767 | end_index, PAGE_LOCK, &index_ret); |
| 1768 | if (ret == -EAGAIN) |
| 1769 | __unlock_for_delalloc(inode, locked_page, delalloc_start, |
| 1770 | (u64)index_ret << PAGE_SHIFT); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1771 | return ret; |
| 1772 | } |
| 1773 | |
| 1774 | /* |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1775 | * Find and lock a contiguous range of bytes in the file marked as delalloc, no |
| 1776 | * 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] | 1777 | * |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1778 | * Return: true if we find something |
| 1779 | * false if nothing was in the tree |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1780 | */ |
Johannes Thumshirn | ce9f967 | 2018-11-19 10:38:17 +0100 | [diff] [blame] | 1781 | EXPORT_FOR_TESTS |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1782 | noinline_for_stack bool find_lock_delalloc_range(struct inode *inode, |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 1783 | struct page *locked_page, u64 *start, |
Nikolay Borisov | 917aace | 2018-10-26 14:43:20 +0300 | [diff] [blame] | 1784 | u64 *end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1785 | { |
Goldwyn Rodrigues | 9978059 | 2019-06-21 10:02:54 -0500 | [diff] [blame] | 1786 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Nikolay Borisov | 917aace | 2018-10-26 14:43:20 +0300 | [diff] [blame] | 1787 | u64 max_bytes = BTRFS_MAX_EXTENT_SIZE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1788 | u64 delalloc_start; |
| 1789 | u64 delalloc_end; |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1790 | bool found; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1791 | struct extent_state *cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1792 | int ret; |
| 1793 | int loops = 0; |
| 1794 | |
| 1795 | again: |
| 1796 | /* step one, find a bunch of delalloc bytes starting at start */ |
| 1797 | delalloc_start = *start; |
| 1798 | delalloc_end = 0; |
| 1799 | found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1800 | max_bytes, &cached_state); |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1801 | if (!found || delalloc_end <= *start) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1802 | *start = delalloc_start; |
| 1803 | *end = delalloc_end; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1804 | free_extent_state(cached_state); |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1805 | return false; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | /* |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1809 | * start comes from the offset of locked_page. We have to lock |
| 1810 | * pages in order, so we can't process delalloc bytes before |
| 1811 | * locked_page |
| 1812 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1813 | if (delalloc_start < *start) |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1814 | delalloc_start = *start; |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 1815 | |
| 1816 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1817 | * 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] | 1818 | */ |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1819 | if (delalloc_end + 1 - delalloc_start > max_bytes) |
| 1820 | delalloc_end = delalloc_start + max_bytes - 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1821 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1822 | /* step two, lock all the pages after the page that has start */ |
| 1823 | ret = lock_delalloc_pages(inode, locked_page, |
| 1824 | delalloc_start, delalloc_end); |
Nikolay Borisov | 9bfd61d | 2018-10-26 14:43:21 +0300 | [diff] [blame] | 1825 | ASSERT(!ret || ret == -EAGAIN); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1826 | if (ret == -EAGAIN) { |
| 1827 | /* some of the pages are gone, lets avoid looping by |
| 1828 | * shortening the size of the delalloc range we're searching |
| 1829 | */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1830 | free_extent_state(cached_state); |
Chris Mason | 7d78874 | 2014-05-21 05:49:54 -0700 | [diff] [blame] | 1831 | cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1832 | if (!loops) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1833 | max_bytes = PAGE_SIZE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1834 | loops = 1; |
| 1835 | goto again; |
| 1836 | } else { |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1837 | found = false; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1838 | goto out_failed; |
| 1839 | } |
| 1840 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1841 | |
| 1842 | /* step three, lock the state bits for the whole range */ |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 1843 | lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1844 | |
| 1845 | /* then test to make sure it is all still delalloc */ |
| 1846 | ret = test_range_bit(tree, delalloc_start, delalloc_end, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1847 | EXTENT_DELALLOC, 1, cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1848 | if (!ret) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1849 | unlock_extent_cached(tree, delalloc_start, delalloc_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 1850 | &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1851 | __unlock_for_delalloc(inode, locked_page, |
| 1852 | delalloc_start, delalloc_end); |
| 1853 | cond_resched(); |
| 1854 | goto again; |
| 1855 | } |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1856 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1857 | *start = delalloc_start; |
| 1858 | *end = delalloc_end; |
| 1859 | out_failed: |
| 1860 | return found; |
| 1861 | } |
| 1862 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1863 | static int __process_pages_contig(struct address_space *mapping, |
| 1864 | struct page *locked_page, |
| 1865 | pgoff_t start_index, pgoff_t end_index, |
| 1866 | unsigned long page_ops, pgoff_t *index_ret) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1867 | { |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1868 | unsigned long nr_pages = end_index - start_index + 1; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1869 | unsigned long pages_locked = 0; |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1870 | pgoff_t index = start_index; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1871 | struct page *pages[16]; |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1872 | unsigned ret; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1873 | int err = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1874 | int i; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1875 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1876 | if (page_ops & PAGE_LOCK) { |
| 1877 | ASSERT(page_ops == PAGE_LOCK); |
| 1878 | ASSERT(index_ret && *index_ret == start_index); |
| 1879 | } |
| 1880 | |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1881 | if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0) |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1882 | mapping_set_error(mapping, -EIO); |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1883 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1884 | while (nr_pages > 0) { |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1885 | ret = find_get_pages_contig(mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 1886 | min_t(unsigned long, |
| 1887 | nr_pages, ARRAY_SIZE(pages)), pages); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1888 | if (ret == 0) { |
| 1889 | /* |
| 1890 | * Only if we're going to lock these pages, |
| 1891 | * can we find nothing at @index. |
| 1892 | */ |
| 1893 | ASSERT(page_ops & PAGE_LOCK); |
Liu Bo | 49d4a33 | 2017-03-06 18:20:56 -0800 | [diff] [blame] | 1894 | err = -EAGAIN; |
| 1895 | goto out; |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1896 | } |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1897 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1898 | for (i = 0; i < ret; i++) { |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1899 | if (page_ops & PAGE_SET_PRIVATE2) |
Chris Mason | 8b62b72 | 2009-09-02 16:53:46 -0400 | [diff] [blame] | 1900 | SetPagePrivate2(pages[i]); |
| 1901 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1902 | if (pages[i] == locked_page) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1903 | put_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1904 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1905 | continue; |
| 1906 | } |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1907 | if (page_ops & PAGE_CLEAR_DIRTY) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1908 | clear_page_dirty_for_io(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1909 | if (page_ops & PAGE_SET_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1910 | set_page_writeback(pages[i]); |
Filipe Manana | 704de49 | 2014-10-06 22:14:22 +0100 | [diff] [blame] | 1911 | if (page_ops & PAGE_SET_ERROR) |
| 1912 | SetPageError(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1913 | if (page_ops & PAGE_END_WRITEBACK) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1914 | end_page_writeback(pages[i]); |
Josef Bacik | c2790a2 | 2013-07-29 11:20:47 -0400 | [diff] [blame] | 1915 | if (page_ops & PAGE_UNLOCK) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1916 | unlock_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1917 | if (page_ops & PAGE_LOCK) { |
| 1918 | lock_page(pages[i]); |
| 1919 | if (!PageDirty(pages[i]) || |
| 1920 | pages[i]->mapping != mapping) { |
| 1921 | unlock_page(pages[i]); |
| 1922 | put_page(pages[i]); |
| 1923 | err = -EAGAIN; |
| 1924 | goto out; |
| 1925 | } |
| 1926 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1927 | put_page(pages[i]); |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1928 | pages_locked++; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1929 | } |
| 1930 | nr_pages -= ret; |
| 1931 | index += ret; |
| 1932 | cond_resched(); |
| 1933 | } |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1934 | out: |
| 1935 | if (err && index_ret) |
| 1936 | *index_ret = start_index + pages_locked - 1; |
| 1937 | return err; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1938 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1939 | |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1940 | void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 1941 | struct page *locked_page, |
| 1942 | unsigned clear_bits, |
| 1943 | unsigned long page_ops) |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1944 | { |
| 1945 | 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] | 1946 | NULL); |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1947 | |
| 1948 | __process_pages_contig(inode->i_mapping, locked_page, |
| 1949 | start >> PAGE_SHIFT, end >> PAGE_SHIFT, |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1950 | page_ops, NULL); |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 1951 | } |
| 1952 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1953 | /* |
| 1954 | * count the number of bytes in the tree that have a given bit(s) |
| 1955 | * set. This can be fairly slow, except for EXTENT_DIRTY which is |
| 1956 | * cached. The total number found is returned. |
| 1957 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1958 | u64 count_range_bits(struct extent_io_tree *tree, |
| 1959 | u64 *start, u64 search_end, u64 max_bytes, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1960 | unsigned bits, int contig) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1961 | { |
| 1962 | struct rb_node *node; |
| 1963 | struct extent_state *state; |
| 1964 | u64 cur_start = *start; |
| 1965 | u64 total_bytes = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1966 | u64 last = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1967 | int found = 0; |
| 1968 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1969 | if (WARN_ON(search_end <= cur_start)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1970 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1971 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1972 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1973 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| 1974 | total_bytes = tree->dirty_bytes; |
| 1975 | goto out; |
| 1976 | } |
| 1977 | /* |
| 1978 | * this search will find all the extents that end after |
| 1979 | * our range starts. |
| 1980 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1981 | node = tree_search(tree, cur_start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1982 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1983 | goto out; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1984 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1985 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1986 | state = rb_entry(node, struct extent_state, rb_node); |
| 1987 | if (state->start > search_end) |
| 1988 | break; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 1989 | if (contig && found && state->start > last + 1) |
| 1990 | break; |
| 1991 | if (state->end >= cur_start && (state->state & bits) == bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1992 | total_bytes += min(search_end, state->end) + 1 - |
| 1993 | max(cur_start, state->start); |
| 1994 | if (total_bytes >= max_bytes) |
| 1995 | break; |
| 1996 | if (!found) { |
Josef Bacik | af60bed | 2011-05-04 11:11:17 -0400 | [diff] [blame] | 1997 | *start = max(cur_start, state->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1998 | found = 1; |
| 1999 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 2000 | last = state->end; |
| 2001 | } else if (contig && found) { |
| 2002 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2003 | } |
| 2004 | node = rb_next(node); |
| 2005 | if (!node) |
| 2006 | break; |
| 2007 | } |
| 2008 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2009 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2010 | return total_bytes; |
| 2011 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2012 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2013 | /* |
| 2014 | * set the private field for a given byte offset in the tree. If there isn't |
| 2015 | * an extent_state there already, this does nothing. |
| 2016 | */ |
Arnd Bergmann | f827ba9 | 2016-02-22 22:53:20 +0100 | [diff] [blame] | 2017 | 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] | 2018 | struct io_failure_record *failrec) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2019 | { |
| 2020 | struct rb_node *node; |
| 2021 | struct extent_state *state; |
| 2022 | int ret = 0; |
| 2023 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2024 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2025 | /* |
| 2026 | * this search will find all the extents that end after |
| 2027 | * our range starts. |
| 2028 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2029 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 2030 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2031 | ret = -ENOENT; |
| 2032 | goto out; |
| 2033 | } |
| 2034 | state = rb_entry(node, struct extent_state, rb_node); |
| 2035 | if (state->start != start) { |
| 2036 | ret = -ENOENT; |
| 2037 | goto out; |
| 2038 | } |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2039 | state->failrec = failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2040 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2041 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2042 | return ret; |
| 2043 | } |
| 2044 | |
Arnd Bergmann | f827ba9 | 2016-02-22 22:53:20 +0100 | [diff] [blame] | 2045 | 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] | 2046 | struct io_failure_record **failrec) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2047 | { |
| 2048 | struct rb_node *node; |
| 2049 | struct extent_state *state; |
| 2050 | int ret = 0; |
| 2051 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2052 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2053 | /* |
| 2054 | * this search will find all the extents that end after |
| 2055 | * our range starts. |
| 2056 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2057 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 2058 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2059 | ret = -ENOENT; |
| 2060 | goto out; |
| 2061 | } |
| 2062 | state = rb_entry(node, struct extent_state, rb_node); |
| 2063 | if (state->start != start) { |
| 2064 | ret = -ENOENT; |
| 2065 | goto out; |
| 2066 | } |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2067 | *failrec = state->failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2068 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2069 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2070 | return ret; |
| 2071 | } |
| 2072 | |
| 2073 | /* |
| 2074 | * searches a range in the state tree for a given mask. |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2075 | * 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] | 2076 | * has the bits set. Otherwise, 1 is returned if any bit in the |
| 2077 | * range is found set. |
| 2078 | */ |
| 2079 | 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] | 2080 | unsigned bits, int filled, struct extent_state *cached) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2081 | { |
| 2082 | struct extent_state *state = NULL; |
| 2083 | struct rb_node *node; |
| 2084 | int bitset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2085 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2086 | spin_lock(&tree->lock); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 2087 | if (cached && extent_state_in_tree(cached) && cached->start <= start && |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 2088 | cached->end > start) |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2089 | node = &cached->rb_node; |
| 2090 | else |
| 2091 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2092 | while (node && start <= end) { |
| 2093 | state = rb_entry(node, struct extent_state, rb_node); |
| 2094 | |
| 2095 | if (filled && state->start > start) { |
| 2096 | bitset = 0; |
| 2097 | break; |
| 2098 | } |
| 2099 | |
| 2100 | if (state->start > end) |
| 2101 | break; |
| 2102 | |
| 2103 | if (state->state & bits) { |
| 2104 | bitset = 1; |
| 2105 | if (!filled) |
| 2106 | break; |
| 2107 | } else if (filled) { |
| 2108 | bitset = 0; |
| 2109 | break; |
| 2110 | } |
Chris Mason | 46562ce | 2009-09-23 20:23:16 -0400 | [diff] [blame] | 2111 | |
| 2112 | if (state->end == (u64)-1) |
| 2113 | break; |
| 2114 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2115 | start = state->end + 1; |
| 2116 | if (start > end) |
| 2117 | break; |
| 2118 | node = rb_next(node); |
| 2119 | if (!node) { |
| 2120 | if (filled) |
| 2121 | bitset = 0; |
| 2122 | break; |
| 2123 | } |
| 2124 | } |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2125 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2126 | return bitset; |
| 2127 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2128 | |
| 2129 | /* |
| 2130 | * helper function to set a given page up to date if all the |
| 2131 | * extents in the tree for that page are up to date |
| 2132 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 2133 | 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] | 2134 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 2135 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2136 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2137 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2138 | SetPageUptodate(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2139 | } |
| 2140 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2141 | int free_io_failure(struct extent_io_tree *failure_tree, |
| 2142 | struct extent_io_tree *io_tree, |
| 2143 | struct io_failure_record *rec) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2144 | { |
| 2145 | int ret; |
| 2146 | int err = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2147 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2148 | set_state_failrec(failure_tree, rec->start, NULL); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2149 | ret = clear_extent_bits(failure_tree, rec->start, |
| 2150 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2151 | EXTENT_LOCKED | EXTENT_DIRTY); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2152 | if (ret) |
| 2153 | err = ret; |
| 2154 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2155 | ret = clear_extent_bits(io_tree, rec->start, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2156 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2157 | EXTENT_DAMAGED); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2158 | if (ret && !err) |
| 2159 | err = ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2160 | |
| 2161 | kfree(rec); |
| 2162 | return err; |
| 2163 | } |
| 2164 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2165 | /* |
| 2166 | * this bypasses the standard btrfs submit functions deliberately, as |
| 2167 | * the standard behavior is to write all copies in a raid setup. here we only |
| 2168 | * want to write the one bad copy. so we do the mapping for ourselves and issue |
| 2169 | * submit_bio directly. |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2170 | * to avoid any synchronization issues, wait for the data after writing, which |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2171 | * actually prevents the read that triggered the error from finishing. |
| 2172 | * currently, there can be no more than two copies of every data bit. thus, |
| 2173 | * exactly one rewrite is required. |
| 2174 | */ |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 2175 | int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, |
| 2176 | u64 length, u64 logical, struct page *page, |
| 2177 | unsigned int pg_offset, int mirror_num) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2178 | { |
| 2179 | struct bio *bio; |
| 2180 | struct btrfs_device *dev; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2181 | u64 map_length = 0; |
| 2182 | u64 sector; |
| 2183 | struct btrfs_bio *bbio = NULL; |
| 2184 | int ret; |
| 2185 | |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2186 | ASSERT(!(fs_info->sb->s_flags & SB_RDONLY)); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2187 | BUG_ON(!mirror_num); |
| 2188 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 2189 | bio = btrfs_io_bio_alloc(1); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2190 | bio->bi_iter.bi_size = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2191 | map_length = length; |
| 2192 | |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2193 | /* |
| 2194 | * Avoid races with device replace and make sure our bbio has devices |
| 2195 | * associated to its stripes that don't go away while we are doing the |
| 2196 | * read repair operation. |
| 2197 | */ |
| 2198 | btrfs_bio_counter_inc_blocked(fs_info); |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 2199 | if (btrfs_is_parity_mirror(fs_info, logical, length)) { |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2200 | /* |
| 2201 | * Note that we don't use BTRFS_MAP_WRITE because it's supposed |
| 2202 | * to update all raid stripes, but here we just want to correct |
| 2203 | * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad |
| 2204 | * stripe's dev and sector. |
| 2205 | */ |
| 2206 | ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical, |
| 2207 | &map_length, &bbio, 0); |
| 2208 | if (ret) { |
| 2209 | btrfs_bio_counter_dec(fs_info); |
| 2210 | bio_put(bio); |
| 2211 | return -EIO; |
| 2212 | } |
| 2213 | ASSERT(bbio->mirror_num == 1); |
| 2214 | } else { |
| 2215 | ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical, |
| 2216 | &map_length, &bbio, mirror_num); |
| 2217 | if (ret) { |
| 2218 | btrfs_bio_counter_dec(fs_info); |
| 2219 | bio_put(bio); |
| 2220 | return -EIO; |
| 2221 | } |
| 2222 | BUG_ON(mirror_num != bbio->mirror_num); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2223 | } |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2224 | |
| 2225 | sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2226 | bio->bi_iter.bi_sector = sector; |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2227 | dev = bbio->stripes[bbio->mirror_num - 1].dev; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 2228 | btrfs_put_bbio(bbio); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2229 | if (!dev || !dev->bdev || |
| 2230 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) { |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2231 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2232 | bio_put(bio); |
| 2233 | return -EIO; |
| 2234 | } |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2235 | bio_set_dev(bio, dev->bdev); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2236 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC; |
Miao Xie | ffdd201 | 2014-09-12 18:44:00 +0800 | [diff] [blame] | 2237 | bio_add_page(bio, page, length, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2238 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 2239 | if (btrfsic_submit_bio_wait(bio)) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2240 | /* try to remap that extent elsewhere? */ |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2241 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2242 | bio_put(bio); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2243 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2244 | return -EIO; |
| 2245 | } |
| 2246 | |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 2247 | btrfs_info_rl_in_rcu(fs_info, |
| 2248 | "read error corrected: ino %llu off %llu (dev %s sector %llu)", |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 2249 | ino, start, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2250 | rcu_str_deref(dev->name), sector); |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2251 | btrfs_bio_counter_dec(fs_info); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2252 | bio_put(bio); |
| 2253 | return 0; |
| 2254 | } |
| 2255 | |
David Sterba | 20a1fbf9 | 2019-03-20 11:23:44 +0100 | [diff] [blame] | 2256 | int btrfs_repair_eb_io_failure(struct extent_buffer *eb, int mirror_num) |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2257 | { |
David Sterba | 20a1fbf9 | 2019-03-20 11:23:44 +0100 | [diff] [blame] | 2258 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2259 | u64 start = eb->start; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 2260 | int i, num_pages = num_extent_pages(eb); |
Chris Mason | d95603b | 2012-04-12 15:55:15 -0400 | [diff] [blame] | 2261 | int ret = 0; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2262 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 2263 | if (sb_rdonly(fs_info->sb)) |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2264 | return -EROFS; |
| 2265 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2266 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 2267 | struct page *p = eb->pages[i]; |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2268 | |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 2269 | ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2270 | start - page_offset(p), mirror_num); |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2271 | if (ret) |
| 2272 | break; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2273 | start += PAGE_SIZE; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | return ret; |
| 2277 | } |
| 2278 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2279 | /* |
| 2280 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 2281 | * to see if we need to process or clean up an io_failure_record |
| 2282 | */ |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2283 | int clean_io_failure(struct btrfs_fs_info *fs_info, |
| 2284 | struct extent_io_tree *failure_tree, |
| 2285 | struct extent_io_tree *io_tree, u64 start, |
| 2286 | struct page *page, u64 ino, unsigned int pg_offset) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2287 | { |
| 2288 | u64 private; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2289 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2290 | struct extent_state *state; |
| 2291 | int num_copies; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2292 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2293 | |
| 2294 | private = 0; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2295 | ret = count_range_bits(failure_tree, &private, (u64)-1, 1, |
| 2296 | EXTENT_DIRTY, 0); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2297 | if (!ret) |
| 2298 | return 0; |
| 2299 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2300 | ret = get_state_failrec(failure_tree, start, &failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2301 | if (ret) |
| 2302 | return 0; |
| 2303 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2304 | BUG_ON(!failrec->this_mirror); |
| 2305 | |
| 2306 | if (failrec->in_validation) { |
| 2307 | /* there was no real error, just free the record */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2308 | btrfs_debug(fs_info, |
| 2309 | "clean_io_failure: freeing dummy error at %llu", |
| 2310 | failrec->start); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2311 | goto out; |
| 2312 | } |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 2313 | if (sb_rdonly(fs_info->sb)) |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2314 | goto out; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2315 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2316 | spin_lock(&io_tree->lock); |
| 2317 | state = find_first_extent_bit_state(io_tree, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2318 | failrec->start, |
| 2319 | EXTENT_LOCKED); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2320 | spin_unlock(&io_tree->lock); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2321 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2322 | if (state && state->start <= failrec->start && |
| 2323 | state->end >= failrec->start + failrec->len - 1) { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2324 | num_copies = btrfs_num_copies(fs_info, failrec->logical, |
| 2325 | failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2326 | if (num_copies > 1) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2327 | repair_io_failure(fs_info, ino, start, failrec->len, |
| 2328 | failrec->logical, page, pg_offset, |
| 2329 | failrec->failed_mirror); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | out: |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2334 | free_io_failure(failure_tree, io_tree, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2335 | |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2336 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2337 | } |
| 2338 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2339 | /* |
| 2340 | * Can be called when |
| 2341 | * - hold extent lock |
| 2342 | * - under ordered extent |
| 2343 | * - the inode is freeing |
| 2344 | */ |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2345 | 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] | 2346 | { |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2347 | struct extent_io_tree *failure_tree = &inode->io_failure_tree; |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2348 | struct io_failure_record *failrec; |
| 2349 | struct extent_state *state, *next; |
| 2350 | |
| 2351 | if (RB_EMPTY_ROOT(&failure_tree->state)) |
| 2352 | return; |
| 2353 | |
| 2354 | spin_lock(&failure_tree->lock); |
| 2355 | state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY); |
| 2356 | while (state) { |
| 2357 | if (state->start > end) |
| 2358 | break; |
| 2359 | |
| 2360 | ASSERT(state->end <= end); |
| 2361 | |
| 2362 | next = next_state(state); |
| 2363 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2364 | failrec = state->failrec; |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2365 | free_extent_state(state); |
| 2366 | kfree(failrec); |
| 2367 | |
| 2368 | state = next; |
| 2369 | } |
| 2370 | spin_unlock(&failure_tree->lock); |
| 2371 | } |
| 2372 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2373 | 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] | 2374 | struct io_failure_record **failrec_ret) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2375 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2376 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2377 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2378 | struct extent_map *em; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2379 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2380 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
| 2381 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2382 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2383 | u64 logical; |
| 2384 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2385 | ret = get_state_failrec(failure_tree, start, &failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2386 | if (ret) { |
| 2387 | failrec = kzalloc(sizeof(*failrec), GFP_NOFS); |
| 2388 | if (!failrec) |
| 2389 | return -ENOMEM; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2390 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2391 | failrec->start = start; |
| 2392 | failrec->len = end - start + 1; |
| 2393 | failrec->this_mirror = 0; |
| 2394 | failrec->bio_flags = 0; |
| 2395 | failrec->in_validation = 0; |
| 2396 | |
| 2397 | read_lock(&em_tree->lock); |
| 2398 | em = lookup_extent_mapping(em_tree, start, failrec->len); |
| 2399 | if (!em) { |
| 2400 | read_unlock(&em_tree->lock); |
| 2401 | kfree(failrec); |
| 2402 | return -EIO; |
| 2403 | } |
| 2404 | |
Filipe David Borba Manana | 68ba990 | 2013-11-25 03:22:07 +0000 | [diff] [blame] | 2405 | if (em->start > start || em->start + em->len <= start) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2406 | free_extent_map(em); |
| 2407 | em = NULL; |
| 2408 | } |
| 2409 | read_unlock(&em_tree->lock); |
Tsutomu Itoh | 7a2d6a6 | 2012-10-01 03:07:15 -0600 | [diff] [blame] | 2410 | if (!em) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2411 | kfree(failrec); |
| 2412 | return -EIO; |
| 2413 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2414 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2415 | logical = start - em->start; |
| 2416 | logical = em->block_start + logical; |
| 2417 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 2418 | logical = em->block_start; |
| 2419 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; |
| 2420 | extent_set_compress_type(&failrec->bio_flags, |
| 2421 | em->compress_type); |
| 2422 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2423 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2424 | btrfs_debug(fs_info, |
| 2425 | "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu", |
| 2426 | logical, start, failrec->len); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2427 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2428 | failrec->logical = logical; |
| 2429 | free_extent_map(em); |
| 2430 | |
| 2431 | /* set the bits in the private failure tree */ |
| 2432 | ret = set_extent_bits(failure_tree, start, end, |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2433 | EXTENT_LOCKED | EXTENT_DIRTY); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2434 | if (ret >= 0) |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2435 | ret = set_state_failrec(failure_tree, start, failrec); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2436 | /* set the bits in the inode's tree */ |
| 2437 | if (ret >= 0) |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2438 | ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2439 | if (ret < 0) { |
| 2440 | kfree(failrec); |
| 2441 | return ret; |
| 2442 | } |
| 2443 | } else { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2444 | btrfs_debug(fs_info, |
| 2445 | "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d", |
| 2446 | failrec->logical, failrec->start, failrec->len, |
| 2447 | failrec->in_validation); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2448 | /* |
| 2449 | * when data can be on disk more than twice, add to failrec here |
| 2450 | * (e.g. with a list for failed_mirror) to make |
| 2451 | * clean_io_failure() clean all those errors at once. |
| 2452 | */ |
| 2453 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2454 | |
| 2455 | *failrec_ret = failrec; |
| 2456 | |
| 2457 | return 0; |
| 2458 | } |
| 2459 | |
Ming Lei | a0b60d7 | 2017-12-18 20:22:11 +0800 | [diff] [blame] | 2460 | bool btrfs_check_repairable(struct inode *inode, unsigned failed_bio_pages, |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2461 | struct io_failure_record *failrec, int failed_mirror) |
| 2462 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2463 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2464 | int num_copies; |
| 2465 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2466 | num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2467 | if (num_copies == 1) { |
| 2468 | /* |
| 2469 | * we only have a single copy of the data, so don't bother with |
| 2470 | * all the retry and error correction code that follows. no |
| 2471 | * matter what the error is, it is very likely to persist. |
| 2472 | */ |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2473 | btrfs_debug(fs_info, |
| 2474 | "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d", |
| 2475 | num_copies, failrec->this_mirror, failed_mirror); |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2476 | return false; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2477 | } |
| 2478 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2479 | /* |
| 2480 | * there are two premises: |
| 2481 | * a) deliver good data to the caller |
| 2482 | * b) correct the bad sectors on disk |
| 2483 | */ |
Ming Lei | a0b60d7 | 2017-12-18 20:22:11 +0800 | [diff] [blame] | 2484 | if (failed_bio_pages > 1) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2485 | /* |
| 2486 | * to fulfill b), we need to know the exact failing sectors, as |
| 2487 | * we don't want to rewrite any more than the failed ones. thus, |
| 2488 | * we need separate read requests for the failed bio |
| 2489 | * |
| 2490 | * if the following BUG_ON triggers, our validation request got |
| 2491 | * merged. we need separate requests for our algorithm to work. |
| 2492 | */ |
| 2493 | BUG_ON(failrec->in_validation); |
| 2494 | failrec->in_validation = 1; |
| 2495 | failrec->this_mirror = failed_mirror; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2496 | } else { |
| 2497 | /* |
| 2498 | * we're ready to fulfill a) and b) alongside. get a good copy |
| 2499 | * of the failed sector and if we succeed, we have setup |
| 2500 | * everything for repair_io_failure to do the rest for us. |
| 2501 | */ |
| 2502 | if (failrec->in_validation) { |
| 2503 | BUG_ON(failrec->this_mirror != failed_mirror); |
| 2504 | failrec->in_validation = 0; |
| 2505 | failrec->this_mirror = 0; |
| 2506 | } |
| 2507 | failrec->failed_mirror = failed_mirror; |
| 2508 | failrec->this_mirror++; |
| 2509 | if (failrec->this_mirror == failed_mirror) |
| 2510 | failrec->this_mirror++; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2511 | } |
| 2512 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2513 | if (failrec->this_mirror > num_copies) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2514 | btrfs_debug(fs_info, |
| 2515 | "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d", |
| 2516 | num_copies, failrec->this_mirror, failed_mirror); |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2517 | return false; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2518 | } |
| 2519 | |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2520 | return true; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | |
| 2524 | struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio, |
| 2525 | struct io_failure_record *failrec, |
| 2526 | struct page *page, int pg_offset, int icsum, |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2527 | bio_end_io_t *endio_func, void *data) |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2528 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2529 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2530 | struct bio *bio; |
| 2531 | struct btrfs_io_bio *btrfs_failed_bio; |
| 2532 | struct btrfs_io_bio *btrfs_bio; |
| 2533 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 2534 | bio = btrfs_io_bio_alloc(1); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2535 | bio->bi_end_io = endio_func; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2536 | bio->bi_iter.bi_sector = failrec->logical >> 9; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2537 | bio_set_dev(bio, fs_info->fs_devices->latest_bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2538 | bio->bi_iter.bi_size = 0; |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2539 | bio->bi_private = data; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2540 | |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2541 | btrfs_failed_bio = btrfs_io_bio(failed_bio); |
| 2542 | if (btrfs_failed_bio->csum) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2543 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 2544 | |
| 2545 | btrfs_bio = btrfs_io_bio(bio); |
| 2546 | btrfs_bio->csum = btrfs_bio->csum_inline; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2547 | icsum *= csum_size; |
| 2548 | memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum, |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2549 | csum_size); |
| 2550 | } |
| 2551 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2552 | bio_add_page(bio, page, failrec->len, pg_offset); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2553 | |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2554 | return bio; |
| 2555 | } |
| 2556 | |
| 2557 | /* |
Nikolay Borisov | 78e62c0 | 2018-11-22 10:17:49 +0200 | [diff] [blame] | 2558 | * This is a generic handler for readpage errors. If other copies exist, read |
| 2559 | * those and write back good data to the failed position. Does not investigate |
| 2560 | * in remapping the failed extent elsewhere, hoping the device will be smart |
| 2561 | * enough to do this as needed |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2562 | */ |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2563 | static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset, |
| 2564 | struct page *page, u64 start, u64 end, |
| 2565 | int failed_mirror) |
| 2566 | { |
| 2567 | struct io_failure_record *failrec; |
| 2568 | struct inode *inode = page->mapping->host; |
| 2569 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2570 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2571 | struct bio *bio; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2572 | int read_mode = 0; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2573 | blk_status_t status; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2574 | int ret; |
Christoph Hellwig | 8a2ee44 | 2019-02-15 19:13:07 +0800 | [diff] [blame] | 2575 | unsigned failed_bio_pages = failed_bio->bi_iter.bi_size >> PAGE_SHIFT; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2576 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2577 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2578 | |
| 2579 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); |
| 2580 | if (ret) |
| 2581 | return ret; |
| 2582 | |
Ming Lei | a0b60d7 | 2017-12-18 20:22:11 +0800 | [diff] [blame] | 2583 | if (!btrfs_check_repairable(inode, failed_bio_pages, failrec, |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2584 | failed_mirror)) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2585 | free_io_failure(failure_tree, tree, failrec); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2586 | return -EIO; |
| 2587 | } |
| 2588 | |
Ming Lei | a0b60d7 | 2017-12-18 20:22:11 +0800 | [diff] [blame] | 2589 | if (failed_bio_pages > 1) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 2590 | read_mode |= REQ_FAILFAST_DEV; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2591 | |
| 2592 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
| 2593 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, |
| 2594 | start - page_offset(page), |
Miao Xie | 8b110e3 | 2014-09-12 18:44:03 +0800 | [diff] [blame] | 2595 | (int)phy_offset, failed_bio->bi_end_io, |
| 2596 | NULL); |
David Sterba | ebcc326 | 2018-06-29 10:56:53 +0200 | [diff] [blame] | 2597 | bio->bi_opf = REQ_OP_READ | read_mode; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2598 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2599 | btrfs_debug(btrfs_sb(inode->i_sb), |
| 2600 | "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d", |
| 2601 | read_mode, failrec->this_mirror, failrec->in_validation); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2602 | |
Linus Torvalds | 8c27cb3 | 2017-07-05 16:41:23 -0700 | [diff] [blame] | 2603 | status = tree->ops->submit_bio_hook(tree->private_data, bio, failrec->this_mirror, |
Nikolay Borisov | 50489a5 | 2019-04-10 19:46:04 +0300 | [diff] [blame] | 2604 | failrec->bio_flags); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2605 | if (status) { |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2606 | free_io_failure(failure_tree, tree, failrec); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2607 | bio_put(bio); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2608 | ret = blk_status_to_errno(status); |
Miao Xie | 6c387ab | 2014-09-12 18:43:57 +0800 | [diff] [blame] | 2609 | } |
| 2610 | |
Tsutomu Itoh | 013bd4c | 2012-02-16 10:11:40 +0900 | [diff] [blame] | 2611 | return ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2612 | } |
| 2613 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2614 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
| 2615 | |
David Sterba | b5227c0 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 2616 | 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] | 2617 | { |
| 2618 | int uptodate = (err == 0); |
Eric Sandeen | 3e2426b | 2014-06-12 00:39:58 -0500 | [diff] [blame] | 2619 | int ret = 0; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2620 | |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 2621 | btrfs_writepage_endio_finish_ordered(page, start, end, uptodate); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2622 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2623 | if (!uptodate) { |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2624 | ClearPageUptodate(page); |
| 2625 | SetPageError(page); |
Colin Ian King | bff5baf | 2017-05-09 18:14:01 +0100 | [diff] [blame] | 2626 | ret = err < 0 ? err : -EIO; |
Liu Bo | 5dca6ee | 2014-05-12 12:47:36 +0800 | [diff] [blame] | 2627 | mapping_set_error(page->mapping, ret); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2628 | } |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2629 | } |
| 2630 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2631 | /* |
| 2632 | * after a writepage IO is done, we need to: |
| 2633 | * clear the uptodate bits on error |
| 2634 | * clear the writeback bits in the extent tree for this IO |
| 2635 | * end_page_writeback if the page has no more pending IO |
| 2636 | * |
| 2637 | * Scheduling is not allowed, so the extent state tree is expected |
| 2638 | * to have one and only one object corresponding to this IO. |
| 2639 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2640 | static void end_bio_extent_writepage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2641 | { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2642 | int error = blk_status_to_errno(bio->bi_status); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2643 | struct bio_vec *bvec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2644 | u64 start; |
| 2645 | u64 end; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 2646 | struct bvec_iter_all iter_all; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2647 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 2648 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 2649 | bio_for_each_segment_all(bvec, bio, iter_all) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2650 | struct page *page = bvec->bv_page; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2651 | struct inode *inode = page->mapping->host; |
| 2652 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2653 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2654 | /* We always issue full-page reads, but if some block |
| 2655 | * in a page fails to read, blk_update_request() will |
| 2656 | * advance bv_offset and adjust bv_len to compensate. |
| 2657 | * Print a warning for nonzero offsets, and an error |
| 2658 | * if they don't add up to a full page. */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2659 | if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| 2660 | if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2661 | btrfs_err(fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2662 | "partial page write in btrfs with offset %u and length %u", |
| 2663 | bvec->bv_offset, bvec->bv_len); |
| 2664 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2665 | btrfs_info(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2666 | "incomplete page write in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2667 | bvec->bv_offset, bvec->bv_len); |
| 2668 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2669 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2670 | start = page_offset(page); |
| 2671 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2672 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2673 | end_extent_writepage(page, error, start, end); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2674 | end_page_writeback(page); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2675 | } |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 2676 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2677 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2678 | } |
| 2679 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2680 | static void |
| 2681 | endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len, |
| 2682 | int uptodate) |
| 2683 | { |
| 2684 | struct extent_state *cached = NULL; |
| 2685 | u64 end = start + len - 1; |
| 2686 | |
| 2687 | if (uptodate && tree->track_uptodate) |
| 2688 | set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC); |
David Sterba | d810a4b | 2017-12-07 18:52:54 +0100 | [diff] [blame] | 2689 | unlock_extent_cached_atomic(tree, start, end, &cached); |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2690 | } |
| 2691 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2692 | /* |
| 2693 | * after a readpage IO is done, we need to: |
| 2694 | * clear the uptodate bits on error |
| 2695 | * set the uptodate bits if things worked |
| 2696 | * set the page up to date if all extents in the tree are uptodate |
| 2697 | * clear the lock bit in the extent tree |
| 2698 | * unlock the page if there are no other extents locked for it |
| 2699 | * |
| 2700 | * Scheduling is not allowed, so the extent state tree is expected |
| 2701 | * to have one and only one object corresponding to this IO. |
| 2702 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2703 | static void end_bio_extent_readpage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2704 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2705 | struct bio_vec *bvec; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2706 | int uptodate = !bio->bi_status; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2707 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2708 | struct extent_io_tree *tree, *failure_tree; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2709 | u64 offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2710 | u64 start; |
| 2711 | u64 end; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2712 | u64 len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2713 | u64 extent_start = 0; |
| 2714 | u64 extent_len = 0; |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 2715 | int mirror; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2716 | int ret; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 2717 | struct bvec_iter_all iter_all; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2718 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 2719 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 2720 | bio_for_each_segment_all(bvec, bio, iter_all) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2721 | struct page *page = bvec->bv_page; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2722 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2723 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Nikolay Borisov | 78e62c0 | 2018-11-22 10:17:49 +0200 | [diff] [blame] | 2724 | bool data_inode = btrfs_ino(BTRFS_I(inode)) |
| 2725 | != BTRFS_BTREE_INODE_OBJECTID; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 2726 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2727 | btrfs_debug(fs_info, |
| 2728 | "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u", |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2729 | (u64)bio->bi_iter.bi_sector, bio->bi_status, |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2730 | io_bio->mirror_num); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2731 | tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2732 | failure_tree = &BTRFS_I(inode)->io_failure_tree; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2733 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2734 | /* We always issue full-page reads, but if some block |
| 2735 | * in a page fails to read, blk_update_request() will |
| 2736 | * advance bv_offset and adjust bv_len to compensate. |
| 2737 | * Print a warning for nonzero offsets, and an error |
| 2738 | * if they don't add up to a full page. */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2739 | if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { |
| 2740 | if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2741 | btrfs_err(fs_info, |
| 2742 | "partial page read in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2743 | bvec->bv_offset, bvec->bv_len); |
| 2744 | else |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2745 | btrfs_info(fs_info, |
| 2746 | "incomplete page read in btrfs with offset %u and length %u", |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2747 | bvec->bv_offset, bvec->bv_len); |
| 2748 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2749 | |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2750 | start = page_offset(page); |
| 2751 | end = start + bvec->bv_offset + bvec->bv_len - 1; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2752 | len = bvec->bv_len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2753 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2754 | mirror = io_bio->mirror_num; |
Nikolay Borisov | 78e62c0 | 2018-11-22 10:17:49 +0200 | [diff] [blame] | 2755 | if (likely(uptodate)) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2756 | ret = tree->ops->readpage_end_io_hook(io_bio, offset, |
| 2757 | page, start, end, |
| 2758 | mirror); |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2759 | if (ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2760 | uptodate = 0; |
Stefan Behrens | 5ee0844 | 2012-08-27 08:30:03 -0600 | [diff] [blame] | 2761 | else |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2762 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
| 2763 | failure_tree, tree, start, |
| 2764 | page, |
| 2765 | btrfs_ino(BTRFS_I(inode)), 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2766 | } |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2767 | |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2768 | if (likely(uptodate)) |
| 2769 | goto readpage_ok; |
| 2770 | |
Nikolay Borisov | 78e62c0 | 2018-11-22 10:17:49 +0200 | [diff] [blame] | 2771 | if (data_inode) { |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 2772 | |
| 2773 | /* |
Nikolay Borisov | 78e62c0 | 2018-11-22 10:17:49 +0200 | [diff] [blame] | 2774 | * The generic bio_readpage_error handles errors the |
| 2775 | * following way: If possible, new read requests are |
| 2776 | * created and submitted and will end up in |
| 2777 | * end_bio_extent_readpage as well (if we're lucky, |
| 2778 | * not in the !uptodate case). In that case it returns |
| 2779 | * 0 and we just go on with the next page in our bio. |
| 2780 | * If it can't handle the error it will return -EIO and |
| 2781 | * we remain responsible for that page. |
Liu Bo | 9d0d1c8 | 2017-03-24 15:04:50 -0700 | [diff] [blame] | 2782 | */ |
Nikolay Borisov | 78e62c0 | 2018-11-22 10:17:49 +0200 | [diff] [blame] | 2783 | ret = bio_readpage_error(bio, offset, page, start, end, |
| 2784 | mirror); |
| 2785 | if (ret == 0) { |
| 2786 | uptodate = !bio->bi_status; |
| 2787 | offset += len; |
| 2788 | continue; |
| 2789 | } |
| 2790 | } else { |
| 2791 | struct extent_buffer *eb; |
| 2792 | |
| 2793 | eb = (struct extent_buffer *)page->private; |
| 2794 | set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
| 2795 | eb->read_mirror = mirror; |
| 2796 | atomic_dec(&eb->io_pages); |
| 2797 | if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, |
| 2798 | &eb->bflags)) |
| 2799 | btree_readahead_hook(eb, -EIO); |
Chris Mason | 7e38326 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2800 | } |
Miao Xie | f2a09da | 2013-07-25 19:22:33 +0800 | [diff] [blame] | 2801 | readpage_ok: |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2802 | if (likely(uptodate)) { |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2803 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2804 | pgoff_t end_index = i_size >> PAGE_SHIFT; |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2805 | unsigned off; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 2806 | |
| 2807 | /* Zero out the end if this page straddles i_size */ |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 2808 | off = offset_in_page(i_size); |
Liu Bo | a583c02 | 2014-08-19 23:32:22 +0800 | [diff] [blame] | 2809 | if (page->index == end_index && off) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2810 | zero_user_segment(page, off, PAGE_SIZE); |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2811 | SetPageUptodate(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2812 | } else { |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2813 | ClearPageUptodate(page); |
| 2814 | SetPageError(page); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2815 | } |
Alexandre Oliva | 17a5adc | 2013-05-15 11:38:55 -0400 | [diff] [blame] | 2816 | unlock_page(page); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2817 | offset += len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2818 | |
| 2819 | if (unlikely(!uptodate)) { |
| 2820 | if (extent_len) { |
| 2821 | endio_readpage_release_extent(tree, |
| 2822 | extent_start, |
| 2823 | extent_len, 1); |
| 2824 | extent_start = 0; |
| 2825 | extent_len = 0; |
| 2826 | } |
| 2827 | endio_readpage_release_extent(tree, start, |
| 2828 | end - start + 1, 0); |
| 2829 | } else if (!extent_len) { |
| 2830 | extent_start = start; |
| 2831 | extent_len = end + 1 - start; |
| 2832 | } else if (extent_start + extent_len == start) { |
| 2833 | extent_len += end + 1 - start; |
| 2834 | } else { |
| 2835 | endio_readpage_release_extent(tree, extent_start, |
| 2836 | extent_len, uptodate); |
| 2837 | extent_start = start; |
| 2838 | extent_len = end + 1 - start; |
| 2839 | } |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2840 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2841 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2842 | if (extent_len) |
| 2843 | endio_readpage_release_extent(tree, extent_start, extent_len, |
| 2844 | uptodate); |
David Sterba | b3a0dd5 | 2018-11-22 17:16:49 +0100 | [diff] [blame] | 2845 | btrfs_io_bio_free_csum(io_bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2846 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2847 | } |
| 2848 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2849 | /* |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2850 | * Initialize the members up to but not including 'bio'. Use after allocating a |
| 2851 | * new bio by bio_alloc_bioset as it does not initialize the bytes outside of |
| 2852 | * 'bio' because use of __GFP_ZERO is not supported. |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2853 | */ |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2854 | 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] | 2855 | { |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2856 | memset(btrfs_bio, 0, offsetof(struct btrfs_io_bio, bio)); |
| 2857 | } |
| 2858 | |
| 2859 | /* |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2860 | * The following helpers allocate a bio. As it's backed by a bioset, it'll |
| 2861 | * never fail. We're returning a bio right now but you can call btrfs_io_bio |
| 2862 | * for the appropriate container_of magic |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2863 | */ |
David Sterba | e749af44 | 2019-06-18 20:00:16 +0200 | [diff] [blame] | 2864 | struct bio *btrfs_bio_alloc(u64 first_byte) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2865 | { |
| 2866 | struct bio *bio; |
| 2867 | |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 2868 | bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, &btrfs_bioset); |
David Sterba | c821e7f3 | 2017-06-02 18:35:36 +0200 | [diff] [blame] | 2869 | bio->bi_iter.bi_sector = first_byte >> 9; |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2870 | btrfs_io_bio_init(btrfs_io_bio(bio)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2871 | return bio; |
| 2872 | } |
| 2873 | |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 2874 | struct bio *btrfs_bio_clone(struct bio *bio) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2875 | { |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2876 | struct btrfs_io_bio *btrfs_bio; |
| 2877 | struct bio *new; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2878 | |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2879 | /* Bio allocation backed by a bioset does not fail */ |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 2880 | new = bio_clone_fast(bio, GFP_NOFS, &btrfs_bioset); |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2881 | btrfs_bio = btrfs_io_bio(new); |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2882 | btrfs_io_bio_init(btrfs_bio); |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2883 | btrfs_bio->iter = bio->bi_iter; |
Miao Xie | 23ea8e5 | 2014-09-12 18:43:54 +0800 | [diff] [blame] | 2884 | return new; |
| 2885 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2886 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 2887 | struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2888 | { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2889 | struct bio *bio; |
| 2890 | |
David Sterba | 6e707bc | 2017-06-02 17:26:26 +0200 | [diff] [blame] | 2891 | /* Bio allocation backed by a bioset does not fail */ |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 2892 | bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset); |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2893 | btrfs_io_bio_init(btrfs_io_bio(bio)); |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2894 | return bio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2895 | } |
| 2896 | |
Liu Bo | e477094 | 2017-05-16 10:57:14 -0700 | [diff] [blame] | 2897 | 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] | 2898 | { |
| 2899 | struct bio *bio; |
| 2900 | struct btrfs_io_bio *btrfs_bio; |
| 2901 | |
| 2902 | /* this will never fail when it's backed by a bioset */ |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 2903 | bio = bio_clone_fast(orig, GFP_NOFS, &btrfs_bioset); |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2904 | ASSERT(bio); |
| 2905 | |
| 2906 | btrfs_bio = btrfs_io_bio(bio); |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 2907 | btrfs_io_bio_init(btrfs_bio); |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2908 | |
| 2909 | bio_trim(bio, offset >> 9, size >> 9); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 2910 | btrfs_bio->iter = bio->bi_iter; |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 2911 | return bio; |
| 2912 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 2913 | |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 2914 | /* |
| 2915 | * @opf: bio REQ_OP_* and REQ_* flags as one value |
David Sterba | b8b3d62 | 2017-06-12 19:50:41 +0200 | [diff] [blame] | 2916 | * @tree: tree so we can call our merge_bio hook |
| 2917 | * @wbc: optional writeback control for io accounting |
| 2918 | * @page: page to add to the bio |
| 2919 | * @pg_offset: offset of the new bio or to check whether we are adding |
| 2920 | * a contiguous page to the previous one |
| 2921 | * @size: portion of page that we want to write |
| 2922 | * @offset: starting offset in the page |
| 2923 | * @bdev: attach newly created bios to this bdev |
David Sterba | 5c2b1fd | 2017-06-06 19:22:55 +0200 | [diff] [blame] | 2924 | * @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] | 2925 | * @end_io_func: end_io callback for new bio |
| 2926 | * @mirror_num: desired mirror to read/write |
| 2927 | * @prev_bio_flags: flags of previous bio to see if we can merge the current one |
| 2928 | * @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] | 2929 | */ |
| 2930 | 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] | 2931 | struct writeback_control *wbc, |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2932 | struct page *page, u64 offset, |
David Sterba | 6c5a4e2 | 2017-10-04 17:10:34 +0200 | [diff] [blame] | 2933 | size_t size, unsigned long pg_offset, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2934 | struct block_device *bdev, |
| 2935 | struct bio **bio_ret, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 2936 | bio_end_io_t end_io_func, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2937 | int mirror_num, |
| 2938 | unsigned long prev_bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2939 | unsigned long bio_flags, |
| 2940 | bool force_bio_submit) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2941 | { |
| 2942 | int ret = 0; |
| 2943 | struct bio *bio; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2944 | size_t page_size = min_t(size_t, size, PAGE_SIZE); |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 2945 | sector_t sector = offset >> 9; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2946 | |
David Sterba | 5c2b1fd | 2017-06-06 19:22:55 +0200 | [diff] [blame] | 2947 | ASSERT(bio_ret); |
| 2948 | |
| 2949 | if (*bio_ret) { |
David Sterba | 0c8508a | 2017-06-12 20:00:43 +0200 | [diff] [blame] | 2950 | bool contig; |
| 2951 | bool can_merge = true; |
| 2952 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2953 | bio = *bio_ret; |
David Sterba | 0c8508a | 2017-06-12 20:00:43 +0200 | [diff] [blame] | 2954 | if (prev_bio_flags & EXTENT_BIO_COMPRESSED) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2955 | contig = bio->bi_iter.bi_sector == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2956 | else |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 2957 | contig = bio_end_sector(bio) == sector; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2958 | |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 2959 | ASSERT(tree->ops); |
| 2960 | if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags)) |
David Sterba | 0c8508a | 2017-06-12 20:00:43 +0200 | [diff] [blame] | 2961 | can_merge = false; |
| 2962 | |
| 2963 | if (prev_bio_flags != bio_flags || !contig || !can_merge || |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 2964 | force_bio_submit || |
David Sterba | 6c5a4e2 | 2017-10-04 17:10:34 +0200 | [diff] [blame] | 2965 | bio_add_page(bio, page, page_size, pg_offset) < page_size) { |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2966 | ret = submit_one_bio(bio, mirror_num, prev_bio_flags); |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2967 | if (ret < 0) { |
| 2968 | *bio_ret = NULL; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2969 | return ret; |
Naohiro Aota | 289454a | 2015-01-06 01:01:03 +0900 | [diff] [blame] | 2970 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2971 | bio = NULL; |
| 2972 | } else { |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2973 | if (wbc) |
Tejun Heo | 34e51a5 | 2019-06-27 13:39:49 -0700 | [diff] [blame] | 2974 | wbc_account_cgroup_owner(wbc, page, page_size); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2975 | return 0; |
| 2976 | } |
| 2977 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2978 | |
David Sterba | e749af44 | 2019-06-18 20:00:16 +0200 | [diff] [blame] | 2979 | bio = btrfs_bio_alloc(offset); |
| 2980 | bio_set_dev(bio, bdev); |
David Sterba | 6c5a4e2 | 2017-10-04 17:10:34 +0200 | [diff] [blame] | 2981 | bio_add_page(bio, page, page_size, pg_offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2982 | bio->bi_end_io = end_io_func; |
| 2983 | bio->bi_private = tree; |
Jens Axboe | e6959b9 | 2017-06-27 11:51:28 -0600 | [diff] [blame] | 2984 | bio->bi_write_hint = page->mapping->host->i_write_hint; |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 2985 | bio->bi_opf = opf; |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2986 | if (wbc) { |
| 2987 | wbc_init_bio(wbc, bio); |
Tejun Heo | 34e51a5 | 2019-06-27 13:39:49 -0700 | [diff] [blame] | 2988 | wbc_account_cgroup_owner(wbc, page, page_size); |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 2989 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2990 | |
David Sterba | 5c2b1fd | 2017-06-06 19:22:55 +0200 | [diff] [blame] | 2991 | *bio_ret = bio; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2992 | |
| 2993 | return ret; |
| 2994 | } |
| 2995 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2996 | static void attach_extent_buffer_page(struct extent_buffer *eb, |
| 2997 | struct page *page) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 2998 | { |
| 2999 | if (!PagePrivate(page)) { |
| 3000 | SetPagePrivate(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3001 | get_page(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 3002 | set_page_private(page, (unsigned long)eb); |
| 3003 | } else { |
| 3004 | WARN_ON(page->private != (unsigned long)eb); |
| 3005 | } |
| 3006 | } |
| 3007 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3008 | void set_page_extent_mapped(struct page *page) |
| 3009 | { |
| 3010 | if (!PagePrivate(page)) { |
| 3011 | SetPagePrivate(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3012 | get_page(page); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 3013 | set_page_private(page, EXTENT_PAGE_PRIVATE); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3014 | } |
| 3015 | } |
| 3016 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3017 | static struct extent_map * |
| 3018 | __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset, |
| 3019 | u64 start, u64 len, get_extent_t *get_extent, |
| 3020 | struct extent_map **em_cached) |
| 3021 | { |
| 3022 | struct extent_map *em; |
| 3023 | |
| 3024 | if (em_cached && *em_cached) { |
| 3025 | em = *em_cached; |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 3026 | if (extent_map_in_tree(em) && start >= em->start && |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3027 | start < extent_map_end(em)) { |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 3028 | refcount_inc(&em->refs); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3029 | return em; |
| 3030 | } |
| 3031 | |
| 3032 | free_extent_map(em); |
| 3033 | *em_cached = NULL; |
| 3034 | } |
| 3035 | |
Nikolay Borisov | fc4f21b1 | 2017-02-20 13:51:06 +0200 | [diff] [blame] | 3036 | em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3037 | if (em_cached && !IS_ERR_OR_NULL(em)) { |
| 3038 | BUG_ON(*em_cached); |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 3039 | refcount_inc(&em->refs); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3040 | *em_cached = em; |
| 3041 | } |
| 3042 | return em; |
| 3043 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3044 | /* |
| 3045 | * basic readpage implementation. Locked extent state structs are inserted |
| 3046 | * into the tree that are removed when the IO is done (by the end_io |
| 3047 | * handlers) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3048 | * XXX JDM: This needs looking at to ensure proper page locking |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3049 | * return 0 on success, otherwise return error |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3050 | */ |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3051 | static int __do_readpage(struct extent_io_tree *tree, |
| 3052 | struct page *page, |
| 3053 | get_extent_t *get_extent, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3054 | struct extent_map **em_cached, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3055 | struct bio **bio, int mirror_num, |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 3056 | unsigned long *bio_flags, unsigned int read_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3057 | u64 *prev_em_start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3058 | { |
| 3059 | struct inode *inode = page->mapping->host; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3060 | u64 start = page_offset(page); |
David Sterba | 8eec829 | 2017-06-06 19:50:13 +0200 | [diff] [blame] | 3061 | const u64 end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3062 | u64 cur = start; |
| 3063 | u64 extent_offset; |
| 3064 | u64 last_byte = i_size_read(inode); |
| 3065 | u64 block_start; |
| 3066 | u64 cur_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3067 | struct extent_map *em; |
| 3068 | struct block_device *bdev; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3069 | int ret = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3070 | int nr = 0; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3071 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3072 | size_t iosize; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3073 | size_t disk_io_size; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3074 | size_t blocksize = inode->i_sb->s_blocksize; |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3075 | unsigned long this_bio_flag = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3076 | |
| 3077 | set_page_extent_mapped(page); |
| 3078 | |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 3079 | if (!PageUptodate(page)) { |
| 3080 | if (cleancache_get_page(page) == 0) { |
| 3081 | BUG_ON(blocksize != PAGE_SIZE); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3082 | unlock_extent(tree, start, end); |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 3083 | goto out; |
| 3084 | } |
| 3085 | } |
| 3086 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3087 | if (page->index == last_byte >> PAGE_SHIFT) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3088 | char *userpage; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 3089 | size_t zero_offset = offset_in_page(last_byte); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3090 | |
| 3091 | if (zero_offset) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3092 | iosize = PAGE_SIZE - zero_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3093 | userpage = kmap_atomic(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3094 | memset(userpage + zero_offset, 0, iosize); |
| 3095 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3096 | kunmap_atomic(userpage); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3097 | } |
| 3098 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3099 | while (cur <= end) { |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3100 | bool force_bio_submit = false; |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3101 | u64 offset; |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3102 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3103 | if (cur >= last_byte) { |
| 3104 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3105 | struct extent_state *cached = NULL; |
| 3106 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3107 | iosize = PAGE_SIZE - pg_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3108 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3109 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3110 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3111 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3112 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3113 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3114 | unlock_extent_cached(tree, cur, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 3115 | cur + iosize - 1, &cached); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3116 | break; |
| 3117 | } |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3118 | em = __get_extent_map(inode, page, pg_offset, cur, |
| 3119 | end - cur + 1, get_extent, em_cached); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3120 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3121 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3122 | unlock_extent(tree, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3123 | break; |
| 3124 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3125 | extent_offset = cur - em->start; |
| 3126 | BUG_ON(extent_map_end(em) <= cur); |
| 3127 | BUG_ON(end < cur); |
| 3128 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3129 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
Mark Fasheh | 4b38431 | 2013-08-06 11:42:50 -0700 | [diff] [blame] | 3130 | this_bio_flag |= EXTENT_BIO_COMPRESSED; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 3131 | extent_set_compress_type(&this_bio_flag, |
| 3132 | em->compress_type); |
| 3133 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3134 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3135 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 3136 | cur_end = min(extent_map_end(em) - 1, end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3137 | iosize = ALIGN(iosize, blocksize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3138 | if (this_bio_flag & EXTENT_BIO_COMPRESSED) { |
| 3139 | disk_io_size = em->block_len; |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3140 | offset = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3141 | } else { |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3142 | offset = em->block_start + extent_offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3143 | disk_io_size = iosize; |
| 3144 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3145 | bdev = em->bdev; |
| 3146 | block_start = em->block_start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3147 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 3148 | block_start = EXTENT_MAP_HOLE; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3149 | |
| 3150 | /* |
| 3151 | * If we have a file range that points to a compressed extent |
| 3152 | * and it's followed by a consecutive file range that points to |
| 3153 | * to the same compressed extent (possibly with a different |
| 3154 | * offset and/or length, so it either points to the whole extent |
| 3155 | * or only part of it), we must make sure we do not submit a |
| 3156 | * single bio to populate the pages for the 2 ranges because |
| 3157 | * this makes the compressed extent read zero out the pages |
| 3158 | * belonging to the 2nd range. Imagine the following scenario: |
| 3159 | * |
| 3160 | * File layout |
| 3161 | * [0 - 8K] [8K - 24K] |
| 3162 | * | | |
| 3163 | * | | |
| 3164 | * points to extent X, points to extent X, |
| 3165 | * offset 4K, length of 8K offset 0, length 16K |
| 3166 | * |
| 3167 | * [extent X, compressed length = 4K uncompressed length = 16K] |
| 3168 | * |
| 3169 | * If the bio to read the compressed extent covers both ranges, |
| 3170 | * it will decompress extent X into the pages belonging to the |
| 3171 | * first range and then it will stop, zeroing out the remaining |
| 3172 | * pages that belong to the other range that points to extent X. |
| 3173 | * So here we make sure we submit 2 bios, one for the first |
| 3174 | * range and another one for the third range. Both will target |
| 3175 | * the same physical extent from disk, but we can't currently |
| 3176 | * make the compressed bio endio callback populate the pages |
| 3177 | * for both ranges because each compressed bio is tightly |
| 3178 | * coupled with a single extent map, and each range can have |
| 3179 | * an extent map with a different offset value relative to the |
| 3180 | * uncompressed data of our extent and different lengths. This |
| 3181 | * is a corner case so we prioritize correctness over |
| 3182 | * non-optimal behavior (submitting 2 bios for the same extent). |
| 3183 | */ |
| 3184 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) && |
| 3185 | prev_em_start && *prev_em_start != (u64)-1 && |
Filipe Manana | 8e92821 | 2019-02-14 15:17:20 +0000 | [diff] [blame] | 3186 | *prev_em_start != em->start) |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3187 | force_bio_submit = true; |
| 3188 | |
| 3189 | if (prev_em_start) |
Filipe Manana | 8e92821 | 2019-02-14 15:17:20 +0000 | [diff] [blame] | 3190 | *prev_em_start = em->start; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3191 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3192 | free_extent_map(em); |
| 3193 | em = NULL; |
| 3194 | |
| 3195 | /* we've found a hole, just zero and go on */ |
| 3196 | if (block_start == EXTENT_MAP_HOLE) { |
| 3197 | char *userpage; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3198 | struct extent_state *cached = NULL; |
| 3199 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3200 | userpage = kmap_atomic(page); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3201 | memset(userpage + pg_offset, 0, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3202 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 3203 | kunmap_atomic(userpage); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3204 | |
| 3205 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3206 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3207 | unlock_extent_cached(tree, cur, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 3208 | cur + iosize - 1, &cached); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3209 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3210 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3211 | continue; |
| 3212 | } |
| 3213 | /* the get_extent function already copied into the page */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3214 | if (test_range_bit(tree, cur, cur_end, |
| 3215 | EXTENT_UPTODATE, 1, NULL)) { |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 3216 | check_page_uptodate(tree, page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3217 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3218 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3219 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3220 | continue; |
| 3221 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3222 | /* we have an inline extent but it didn't get marked up |
| 3223 | * to date. Error out |
| 3224 | */ |
| 3225 | if (block_start == EXTENT_MAP_INLINE) { |
| 3226 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3227 | unlock_extent(tree, cur, cur + iosize - 1); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3228 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3229 | pg_offset += iosize; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3230 | continue; |
| 3231 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3232 | |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3233 | ret = submit_extent_page(REQ_OP_READ | read_flags, tree, NULL, |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3234 | page, offset, disk_io_size, |
| 3235 | pg_offset, bdev, bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3236 | end_bio_extent_readpage, mirror_num, |
| 3237 | *bio_flags, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3238 | this_bio_flag, |
| 3239 | force_bio_submit); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3240 | if (!ret) { |
| 3241 | nr++; |
| 3242 | *bio_flags = this_bio_flag; |
| 3243 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3244 | SetPageError(page); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3245 | unlock_extent(tree, cur, cur + iosize - 1); |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3246 | goto out; |
Josef Bacik | edd33c9 | 2012-10-05 16:40:32 -0400 | [diff] [blame] | 3247 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3248 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3249 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3250 | } |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 3251 | out: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3252 | if (!nr) { |
| 3253 | if (!PageError(page)) |
| 3254 | SetPageUptodate(page); |
| 3255 | unlock_page(page); |
| 3256 | } |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3257 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3258 | } |
| 3259 | |
Nikolay Borisov | e65ef21 | 2019-03-11 09:55:38 +0200 | [diff] [blame] | 3260 | static inline void contiguous_readpages(struct extent_io_tree *tree, |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3261 | struct page *pages[], int nr_pages, |
| 3262 | u64 start, u64 end, |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3263 | struct extent_map **em_cached, |
Nikolay Borisov | d3fac6b | 2017-10-24 11:50:39 +0300 | [diff] [blame] | 3264 | struct bio **bio, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3265 | unsigned long *bio_flags, |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 3266 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3267 | { |
Nikolay Borisov | 23d31bd | 2019-05-07 10:19:23 +0300 | [diff] [blame] | 3268 | struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3269 | int index; |
| 3270 | |
Nikolay Borisov | 23d31bd | 2019-05-07 10:19:23 +0300 | [diff] [blame] | 3271 | btrfs_lock_and_flush_ordered_range(tree, inode, start, end, NULL); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3272 | |
| 3273 | for (index = 0; index < nr_pages; index++) { |
David Sterba | 4ef7769 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 3274 | __do_readpage(tree, pages[index], btrfs_get_extent, em_cached, |
Jens Axboe | 5e9d398 | 2018-08-17 15:45:39 -0700 | [diff] [blame] | 3275 | bio, 0, bio_flags, REQ_RAHEAD, prev_em_start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3276 | put_page(pages[index]); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3277 | } |
| 3278 | } |
| 3279 | |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3280 | static int __extent_read_full_page(struct extent_io_tree *tree, |
| 3281 | struct page *page, |
| 3282 | get_extent_t *get_extent, |
| 3283 | struct bio **bio, int mirror_num, |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 3284 | unsigned long *bio_flags, |
| 3285 | unsigned int read_flags) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3286 | { |
Nikolay Borisov | 23d31bd | 2019-05-07 10:19:23 +0300 | [diff] [blame] | 3287 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3288 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3289 | u64 end = start + PAGE_SIZE - 1; |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3290 | int ret; |
| 3291 | |
Nikolay Borisov | 23d31bd | 2019-05-07 10:19:23 +0300 | [diff] [blame] | 3292 | btrfs_lock_and_flush_ordered_range(tree, inode, start, end, NULL); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3293 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3294 | ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3295 | bio_flags, read_flags, NULL); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3296 | return ret; |
| 3297 | } |
| 3298 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3299 | 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] | 3300 | get_extent_t *get_extent, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3301 | { |
| 3302 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3303 | unsigned long bio_flags = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3304 | int ret; |
| 3305 | |
Jan Schmidt | 8ddc7d9 | 2011-06-13 20:02:58 +0200 | [diff] [blame] | 3306 | ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3307 | &bio_flags, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3308 | if (bio) |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3309 | ret = submit_one_bio(bio, mirror_num, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3310 | return ret; |
| 3311 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3312 | |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3313 | static void update_nr_written(struct writeback_control *wbc, |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 3314 | unsigned long nr_written) |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3315 | { |
| 3316 | wbc->nr_to_write -= nr_written; |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3317 | } |
| 3318 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3319 | /* |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3320 | * helper for __extent_writepage, doing all of the delayed allocation setup. |
| 3321 | * |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 3322 | * 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] | 3323 | * to write the page (copy into inline extent). In this case the IO has |
| 3324 | * been started and the page is already unlocked. |
| 3325 | * |
| 3326 | * This returns 0 if all went well (page still locked) |
| 3327 | * This returns < 0 if there were errors (page still locked) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3328 | */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3329 | static noinline_for_stack int writepage_delalloc(struct inode *inode, |
Nikolay Borisov | 8cc0237 | 2018-11-08 10:18:07 +0200 | [diff] [blame] | 3330 | struct page *page, struct writeback_control *wbc, |
| 3331 | u64 delalloc_start, unsigned long *nr_written) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3332 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3333 | u64 page_end = delalloc_start + PAGE_SIZE - 1; |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 3334 | bool found; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3335 | u64 delalloc_to_write = 0; |
| 3336 | u64 delalloc_end = 0; |
| 3337 | int ret; |
| 3338 | int page_started = 0; |
| 3339 | |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3340 | |
| 3341 | while (delalloc_end < page_end) { |
Goldwyn Rodrigues | 9978059 | 2019-06-21 10:02:54 -0500 | [diff] [blame] | 3342 | found = find_lock_delalloc_range(inode, page, |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3343 | &delalloc_start, |
Nikolay Borisov | 917aace | 2018-10-26 14:43:20 +0300 | [diff] [blame] | 3344 | &delalloc_end); |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 3345 | if (!found) { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3346 | delalloc_start = delalloc_end + 1; |
| 3347 | continue; |
| 3348 | } |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 3349 | ret = btrfs_run_delalloc_range(inode, page, delalloc_start, |
| 3350 | delalloc_end, &page_started, nr_written, wbc); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3351 | if (ret) { |
| 3352 | SetPageError(page); |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 3353 | /* |
| 3354 | * btrfs_run_delalloc_range should return < 0 for error |
| 3355 | * but just in case, we use > 0 here meaning the IO is |
| 3356 | * started, so we don't want to return > 0 unless |
| 3357 | * things are going well. |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3358 | */ |
| 3359 | ret = ret < 0 ? ret : -EIO; |
| 3360 | goto done; |
| 3361 | } |
| 3362 | /* |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3363 | * delalloc_end is already one less than the total length, so |
| 3364 | * we don't subtract one from PAGE_SIZE |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3365 | */ |
| 3366 | delalloc_to_write += (delalloc_end - delalloc_start + |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3367 | PAGE_SIZE) >> PAGE_SHIFT; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3368 | delalloc_start = delalloc_end + 1; |
| 3369 | } |
| 3370 | if (wbc->nr_to_write < delalloc_to_write) { |
| 3371 | int thresh = 8192; |
| 3372 | |
| 3373 | if (delalloc_to_write < thresh * 2) |
| 3374 | thresh = delalloc_to_write; |
| 3375 | wbc->nr_to_write = min_t(u64, delalloc_to_write, |
| 3376 | thresh); |
| 3377 | } |
| 3378 | |
| 3379 | /* did the fill delalloc function already unlock and start |
| 3380 | * the IO? |
| 3381 | */ |
| 3382 | if (page_started) { |
| 3383 | /* |
| 3384 | * we've unlocked the page, so we can't update |
| 3385 | * the mapping's writeback index, just update |
| 3386 | * nr_to_write. |
| 3387 | */ |
| 3388 | wbc->nr_to_write -= *nr_written; |
| 3389 | return 1; |
| 3390 | } |
| 3391 | |
| 3392 | ret = 0; |
| 3393 | |
| 3394 | done: |
| 3395 | return ret; |
| 3396 | } |
| 3397 | |
| 3398 | /* |
| 3399 | * helper for __extent_writepage. This calls the writepage start hooks, |
| 3400 | * and does the loop to map the page into extents and bios. |
| 3401 | * |
| 3402 | * We return 1 if the IO is started and the page is unlocked, |
| 3403 | * 0 if all went well (page still locked) |
| 3404 | * < 0 if there were errors (page still locked) |
| 3405 | */ |
| 3406 | static noinline_for_stack int __extent_writepage_io(struct inode *inode, |
| 3407 | struct page *page, |
| 3408 | struct writeback_control *wbc, |
| 3409 | struct extent_page_data *epd, |
| 3410 | loff_t i_size, |
| 3411 | unsigned long nr_written, |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 3412 | unsigned int write_flags, int *nr_ret) |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3413 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3414 | struct extent_io_tree *tree = epd->tree; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3415 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3416 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3417 | u64 end; |
| 3418 | u64 cur = start; |
| 3419 | u64 extent_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3420 | u64 block_start; |
| 3421 | u64 iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3422 | struct extent_map *em; |
| 3423 | struct block_device *bdev; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3424 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3425 | size_t blocksize; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3426 | int ret = 0; |
| 3427 | int nr = 0; |
| 3428 | bool compressed; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3429 | |
Nikolay Borisov | d75855b | 2018-11-01 14:09:47 +0200 | [diff] [blame] | 3430 | ret = btrfs_writepage_cow_fixup(page, start, page_end); |
| 3431 | if (ret) { |
| 3432 | /* Fixup worker will requeue */ |
| 3433 | if (ret == -EBUSY) |
| 3434 | wbc->pages_skipped++; |
| 3435 | else |
| 3436 | redirty_page_for_writepage(wbc, page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3437 | |
Nikolay Borisov | d75855b | 2018-11-01 14:09:47 +0200 | [diff] [blame] | 3438 | update_nr_written(wbc, nr_written); |
| 3439 | unlock_page(page); |
| 3440 | return 1; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3441 | } |
| 3442 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3443 | /* |
| 3444 | * we don't want to touch the inode after unlocking the page, |
| 3445 | * so we update the mapping writeback index now |
| 3446 | */ |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3447 | update_nr_written(wbc, nr_written + 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3448 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3449 | end = page_end; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3450 | if (i_size <= start) { |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 3451 | btrfs_writepage_endio_finish_ordered(page, start, page_end, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3452 | goto done; |
| 3453 | } |
| 3454 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3455 | blocksize = inode->i_sb->s_blocksize; |
| 3456 | |
| 3457 | while (cur <= end) { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3458 | u64 em_end; |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3459 | u64 offset; |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3460 | |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3461 | if (cur >= i_size) { |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 3462 | btrfs_writepage_endio_finish_ordered(page, cur, |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 3463 | page_end, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3464 | break; |
| 3465 | } |
David Sterba | 3c98c62 | 2017-06-23 04:01:08 +0200 | [diff] [blame] | 3466 | em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3467 | end - cur + 1, 1); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 3468 | if (IS_ERR_OR_NULL(em)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3469 | SetPageError(page); |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3470 | ret = PTR_ERR_OR_ZERO(em); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3471 | break; |
| 3472 | } |
| 3473 | |
| 3474 | extent_offset = cur - em->start; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3475 | em_end = extent_map_end(em); |
| 3476 | BUG_ON(em_end <= cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3477 | BUG_ON(end < cur); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3478 | iosize = min(em_end - cur, end - cur + 1); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3479 | iosize = ALIGN(iosize, blocksize); |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3480 | offset = em->block_start + extent_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3481 | bdev = em->bdev; |
| 3482 | block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3483 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3484 | free_extent_map(em); |
| 3485 | em = NULL; |
| 3486 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3487 | /* |
| 3488 | * compressed and inline extents are written through other |
| 3489 | * paths in the FS |
| 3490 | */ |
| 3491 | if (compressed || block_start == EXTENT_MAP_HOLE || |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3492 | block_start == EXTENT_MAP_INLINE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3493 | /* |
| 3494 | * end_io notification does not happen here for |
| 3495 | * compressed extents |
| 3496 | */ |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 3497 | if (!compressed) |
| 3498 | btrfs_writepage_endio_finish_ordered(page, cur, |
| 3499 | cur + iosize - 1, |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 3500 | 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3501 | else if (compressed) { |
| 3502 | /* we don't want to end_page_writeback on |
| 3503 | * a compressed extent. this happens |
| 3504 | * elsewhere |
| 3505 | */ |
| 3506 | nr++; |
| 3507 | } |
| 3508 | |
| 3509 | cur += iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3510 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3511 | continue; |
| 3512 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3513 | |
David Sterba | 5cdc84b | 2018-07-18 20:32:52 +0200 | [diff] [blame] | 3514 | btrfs_set_range_writeback(tree, cur, cur + iosize - 1); |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3515 | if (!PageWriteback(page)) { |
| 3516 | btrfs_err(BTRFS_I(inode)->root->fs_info, |
| 3517 | "page %lu not writeback, cur %llu end %llu", |
| 3518 | page->index, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3519 | } |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3520 | |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3521 | ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc, |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3522 | page, offset, iosize, pg_offset, |
David Sterba | c2df8bb | 2017-02-10 19:29:38 +0100 | [diff] [blame] | 3523 | bdev, &epd->bio, |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3524 | end_bio_extent_writepage, |
| 3525 | 0, 0, 0, false); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 3526 | if (ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3527 | SetPageError(page); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 3528 | if (PageWriteback(page)) |
| 3529 | end_page_writeback(page); |
| 3530 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3531 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3532 | cur = cur + iosize; |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 3533 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3534 | nr++; |
| 3535 | } |
| 3536 | done: |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3537 | *nr_ret = nr; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3538 | return ret; |
| 3539 | } |
| 3540 | |
| 3541 | /* |
| 3542 | * the writepage semantics are similar to regular writepage. extent |
| 3543 | * records are inserted to lock ranges in the tree, and as dirty areas |
| 3544 | * are found, they are marked writeback. Then the lock bits are removed |
| 3545 | * and the end_io handler clears the writeback ranges |
Qu Wenruo | 3065976 | 2019-03-20 14:27:42 +0800 | [diff] [blame] | 3546 | * |
| 3547 | * Return 0 if everything goes well. |
| 3548 | * Return <0 for error. |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3549 | */ |
| 3550 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 3551 | struct extent_page_data *epd) |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3552 | { |
| 3553 | struct inode *inode = page->mapping->host; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3554 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3555 | u64 page_end = start + PAGE_SIZE - 1; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3556 | int ret; |
| 3557 | int nr = 0; |
| 3558 | size_t pg_offset = 0; |
| 3559 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3560 | unsigned long end_index = i_size >> PAGE_SHIFT; |
David Sterba | f1c77c5 | 2017-06-06 19:03:49 +0200 | [diff] [blame] | 3561 | unsigned int write_flags = 0; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3562 | unsigned long nr_written = 0; |
| 3563 | |
Liu Bo | ff40adf | 2017-08-24 18:19:48 -0600 | [diff] [blame] | 3564 | write_flags = wbc_to_write_flags(wbc); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3565 | |
| 3566 | trace___extent_writepage(page, inode, wbc); |
| 3567 | |
| 3568 | WARN_ON(!PageLocked(page)); |
| 3569 | |
| 3570 | ClearPageError(page); |
| 3571 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 3572 | pg_offset = offset_in_page(i_size); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3573 | if (page->index > end_index || |
| 3574 | (page->index == end_index && !pg_offset)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3575 | page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3576 | unlock_page(page); |
| 3577 | return 0; |
| 3578 | } |
| 3579 | |
| 3580 | if (page->index == end_index) { |
| 3581 | char *userpage; |
| 3582 | |
| 3583 | userpage = kmap_atomic(page); |
| 3584 | memset(userpage + pg_offset, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3585 | PAGE_SIZE - pg_offset); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3586 | kunmap_atomic(userpage); |
| 3587 | flush_dcache_page(page); |
| 3588 | } |
| 3589 | |
| 3590 | pg_offset = 0; |
| 3591 | |
| 3592 | set_page_extent_mapped(page); |
| 3593 | |
Nikolay Borisov | 7789a55 | 2018-11-08 10:18:06 +0200 | [diff] [blame] | 3594 | if (!epd->extent_locked) { |
Nikolay Borisov | 8cc0237 | 2018-11-08 10:18:07 +0200 | [diff] [blame] | 3595 | ret = writepage_delalloc(inode, page, wbc, start, &nr_written); |
Nikolay Borisov | 7789a55 | 2018-11-08 10:18:06 +0200 | [diff] [blame] | 3596 | if (ret == 1) |
| 3597 | goto done_unlocked; |
| 3598 | if (ret) |
| 3599 | goto done; |
| 3600 | } |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3601 | |
| 3602 | ret = __extent_writepage_io(inode, page, wbc, epd, |
| 3603 | i_size, nr_written, write_flags, &nr); |
| 3604 | if (ret == 1) |
| 3605 | goto done_unlocked; |
| 3606 | |
| 3607 | done: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3608 | if (nr == 0) { |
| 3609 | /* make sure the mapping tag for page dirty gets cleared */ |
| 3610 | set_page_writeback(page); |
| 3611 | end_page_writeback(page); |
| 3612 | } |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3613 | if (PageError(page)) { |
| 3614 | ret = ret < 0 ? ret : -EIO; |
| 3615 | end_extent_writepage(page, ret, start, page_end); |
| 3616 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3617 | unlock_page(page); |
Qu Wenruo | 3065976 | 2019-03-20 14:27:42 +0800 | [diff] [blame] | 3618 | ASSERT(ret <= 0); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3619 | return ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3620 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3621 | done_unlocked: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3622 | return 0; |
| 3623 | } |
| 3624 | |
Josef Bacik | fd8b2b6 | 2013-04-24 16:41:19 -0400 | [diff] [blame] | 3625 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3626 | { |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 3627 | wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, |
| 3628 | TASK_UNINTERRUPTIBLE); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3629 | } |
| 3630 | |
Qu Wenruo | 2e3c251 | 2019-03-20 14:27:46 +0800 | [diff] [blame] | 3631 | /* |
| 3632 | * Lock eb pages and flush the bio if we can't the locks |
| 3633 | * |
| 3634 | * Return 0 if nothing went wrong |
| 3635 | * Return >0 is same as 0, except bio is not submitted |
| 3636 | * Return <0 if something went wrong, no page is locked |
| 3637 | */ |
David Sterba | 9df76fb | 2019-03-20 11:21:41 +0100 | [diff] [blame] | 3638 | static noinline_for_stack int lock_extent_buffer_for_io(struct extent_buffer *eb, |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3639 | struct extent_page_data *epd) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3640 | { |
David Sterba | 9df76fb | 2019-03-20 11:21:41 +0100 | [diff] [blame] | 3641 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Qu Wenruo | 2e3c251 | 2019-03-20 14:27:46 +0800 | [diff] [blame] | 3642 | int i, num_pages, failed_page_nr; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3643 | int flush = 0; |
| 3644 | int ret = 0; |
| 3645 | |
| 3646 | if (!btrfs_try_tree_write_lock(eb)) { |
Qu Wenruo | f434062 | 2019-03-20 14:27:41 +0800 | [diff] [blame] | 3647 | ret = flush_write_bio(epd); |
Qu Wenruo | 2e3c251 | 2019-03-20 14:27:46 +0800 | [diff] [blame] | 3648 | if (ret < 0) |
| 3649 | return ret; |
| 3650 | flush = 1; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3651 | btrfs_tree_lock(eb); |
| 3652 | } |
| 3653 | |
| 3654 | if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { |
| 3655 | btrfs_tree_unlock(eb); |
| 3656 | if (!epd->sync_io) |
| 3657 | return 0; |
| 3658 | if (!flush) { |
Qu Wenruo | f434062 | 2019-03-20 14:27:41 +0800 | [diff] [blame] | 3659 | ret = flush_write_bio(epd); |
Qu Wenruo | 2e3c251 | 2019-03-20 14:27:46 +0800 | [diff] [blame] | 3660 | if (ret < 0) |
| 3661 | return ret; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3662 | flush = 1; |
| 3663 | } |
Chris Mason | a098d8e8 | 2012-03-21 12:09:56 -0400 | [diff] [blame] | 3664 | while (1) { |
| 3665 | wait_on_extent_buffer_writeback(eb); |
| 3666 | btrfs_tree_lock(eb); |
| 3667 | if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) |
| 3668 | break; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3669 | btrfs_tree_unlock(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3670 | } |
| 3671 | } |
| 3672 | |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3673 | /* |
| 3674 | * We need to do this to prevent races in people who check if the eb is |
| 3675 | * under IO since we can end up having no IO bits set for a short period |
| 3676 | * of time. |
| 3677 | */ |
| 3678 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3679 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
| 3680 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3681 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3682 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 3683 | percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, |
| 3684 | -eb->len, |
| 3685 | fs_info->dirty_metadata_batch); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3686 | ret = 1; |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 3687 | } else { |
| 3688 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | btrfs_tree_unlock(eb); |
| 3692 | |
| 3693 | if (!ret) |
| 3694 | return ret; |
| 3695 | |
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 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3698 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3699 | |
| 3700 | if (!trylock_page(p)) { |
| 3701 | if (!flush) { |
Qu Wenruo | f434062 | 2019-03-20 14:27:41 +0800 | [diff] [blame] | 3702 | ret = flush_write_bio(epd); |
Qu Wenruo | 2e3c251 | 2019-03-20 14:27:46 +0800 | [diff] [blame] | 3703 | if (ret < 0) { |
| 3704 | failed_page_nr = i; |
| 3705 | goto err_unlock; |
| 3706 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3707 | flush = 1; |
| 3708 | } |
| 3709 | lock_page(p); |
| 3710 | } |
| 3711 | } |
| 3712 | |
| 3713 | return ret; |
Qu Wenruo | 2e3c251 | 2019-03-20 14:27:46 +0800 | [diff] [blame] | 3714 | err_unlock: |
| 3715 | /* Unlock already locked pages */ |
| 3716 | for (i = 0; i < failed_page_nr; i++) |
| 3717 | unlock_page(eb->pages[i]); |
| 3718 | return ret; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | static void end_extent_buffer_writeback(struct extent_buffer *eb) |
| 3722 | { |
| 3723 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 3724 | smp_mb__after_atomic(); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3725 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); |
| 3726 | } |
| 3727 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3728 | static void set_btree_ioerr(struct page *page) |
| 3729 | { |
| 3730 | struct extent_buffer *eb = (struct extent_buffer *)page->private; |
Dennis Zhou | eb5b64f | 2019-09-13 14:54:07 +0100 | [diff] [blame^] | 3731 | struct btrfs_fs_info *fs_info; |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3732 | |
| 3733 | SetPageError(page); |
| 3734 | if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) |
| 3735 | return; |
| 3736 | |
| 3737 | /* |
Dennis Zhou | eb5b64f | 2019-09-13 14:54:07 +0100 | [diff] [blame^] | 3738 | * If we error out, we should add back the dirty_metadata_bytes |
| 3739 | * to make it consistent. |
| 3740 | */ |
| 3741 | fs_info = eb->fs_info; |
| 3742 | percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, |
| 3743 | eb->len, fs_info->dirty_metadata_batch); |
| 3744 | |
| 3745 | /* |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3746 | * If writeback for a btree extent that doesn't belong to a log tree |
| 3747 | * failed, increment the counter transaction->eb_write_errors. |
| 3748 | * We do this because while the transaction is running and before it's |
| 3749 | * committing (when we call filemap_fdata[write|wait]_range against |
| 3750 | * the btree inode), we might have |
| 3751 | * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it |
| 3752 | * returns an error or an error happens during writeback, when we're |
| 3753 | * committing the transaction we wouldn't know about it, since the pages |
| 3754 | * can be no longer dirty nor marked anymore for writeback (if a |
| 3755 | * subsequent modification to the extent buffer didn't happen before the |
| 3756 | * transaction commit), which makes filemap_fdata[write|wait]_range not |
| 3757 | * able to find the pages tagged with SetPageError at transaction |
| 3758 | * commit time. So if this happens we must abort the transaction, |
| 3759 | * otherwise we commit a super block with btree roots that point to |
| 3760 | * btree nodes/leafs whose content on disk is invalid - either garbage |
| 3761 | * or the content of some node/leaf from a past generation that got |
| 3762 | * cowed or deleted and is no longer valid. |
| 3763 | * |
| 3764 | * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would |
| 3765 | * not be enough - we need to distinguish between log tree extents vs |
| 3766 | * non-log tree extents, and the next filemap_fdatawait_range() call |
| 3767 | * will catch and clear such errors in the mapping - and that call might |
| 3768 | * be from a log sync and not from a transaction commit. Also, checking |
| 3769 | * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is |
| 3770 | * not done and would not be reliable - the eb might have been released |
| 3771 | * from memory and reading it back again means that flag would not be |
| 3772 | * set (since it's a runtime flag, not persisted on disk). |
| 3773 | * |
| 3774 | * Using the flags below in the btree inode also makes us achieve the |
| 3775 | * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started |
| 3776 | * writeback for all dirty pages and before filemap_fdatawait_range() |
| 3777 | * is called, the writeback for all dirty pages had already finished |
| 3778 | * with errors - because we were not using AS_EIO/AS_ENOSPC, |
| 3779 | * filemap_fdatawait_range() would return success, as it could not know |
| 3780 | * that writeback errors happened (the pages were no longer tagged for |
| 3781 | * writeback). |
| 3782 | */ |
| 3783 | switch (eb->log_index) { |
| 3784 | case -1: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3785 | set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3786 | break; |
| 3787 | case 0: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3788 | set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3789 | break; |
| 3790 | case 1: |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3791 | set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3792 | break; |
| 3793 | default: |
| 3794 | BUG(); /* unexpected, logic error */ |
| 3795 | } |
| 3796 | } |
| 3797 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3798 | static void end_bio_extent_buffer_writepage(struct bio *bio) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3799 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3800 | struct bio_vec *bvec; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3801 | struct extent_buffer *eb; |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 3802 | int done; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 3803 | struct bvec_iter_all iter_all; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3804 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 3805 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 3806 | bio_for_each_segment_all(bvec, bio, iter_all) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3807 | struct page *page = bvec->bv_page; |
| 3808 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3809 | eb = (struct extent_buffer *)page->private; |
| 3810 | BUG_ON(!eb); |
| 3811 | done = atomic_dec_and_test(&eb->io_pages); |
| 3812 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 3813 | if (bio->bi_status || |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3814 | test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3815 | ClearPageUptodate(page); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3816 | set_btree_ioerr(page); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | end_page_writeback(page); |
| 3820 | |
| 3821 | if (!done) |
| 3822 | continue; |
| 3823 | |
| 3824 | end_extent_buffer_writeback(eb); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3825 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3826 | |
| 3827 | bio_put(bio); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3828 | } |
| 3829 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 3830 | static noinline_for_stack int write_one_eb(struct extent_buffer *eb, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3831 | struct writeback_control *wbc, |
| 3832 | struct extent_page_data *epd) |
| 3833 | { |
David Sterba | 0ab0206 | 2019-03-20 11:27:57 +0100 | [diff] [blame] | 3834 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3835 | struct block_device *bdev = fs_info->fs_devices->latest_bdev; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 3836 | 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] | 3837 | u64 offset = eb->start; |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3838 | u32 nritems; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 3839 | int i, num_pages; |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3840 | unsigned long start, end; |
Liu Bo | ff40adf | 2017-08-24 18:19:48 -0600 | [diff] [blame] | 3841 | unsigned int write_flags = wbc_to_write_flags(wbc) | REQ_META; |
Josef Bacik | d7dbe9e | 2012-04-23 14:00:51 -0400 | [diff] [blame] | 3842 | int ret = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3843 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3844 | clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 3845 | num_pages = num_extent_pages(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3846 | atomic_set(&eb->io_pages, num_pages); |
Josef Bacik | de0022b | 2012-09-25 14:25:58 -0400 | [diff] [blame] | 3847 | |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3848 | /* set btree blocks beyond nritems with 0 to avoid stale content. */ |
| 3849 | nritems = btrfs_header_nritems(eb); |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3850 | if (btrfs_header_level(eb) > 0) { |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3851 | end = btrfs_node_key_ptr_offset(nritems); |
| 3852 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3853 | memzero_extent_buffer(eb, end, eb->len - end); |
Liu Bo | 851cd17 | 2016-09-23 13:44:44 -0700 | [diff] [blame] | 3854 | } else { |
| 3855 | /* |
| 3856 | * leaf: |
| 3857 | * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0 |
| 3858 | */ |
| 3859 | start = btrfs_item_nr_offset(nritems); |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 3860 | end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3861 | memzero_extent_buffer(eb, start, end - start); |
Liu Bo | 3eb548e | 2016-09-14 17:22:57 -0700 | [diff] [blame] | 3862 | } |
| 3863 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3864 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 3865 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3866 | |
| 3867 | clear_page_dirty_for_io(p); |
| 3868 | set_page_writeback(p); |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3869 | ret = submit_extent_page(REQ_OP_WRITE | write_flags, tree, wbc, |
David Sterba | 6273b7f | 2017-10-04 17:30:11 +0200 | [diff] [blame] | 3870 | p, offset, PAGE_SIZE, 0, bdev, |
David Sterba | c2df8bb | 2017-02-10 19:29:38 +0100 | [diff] [blame] | 3871 | &epd->bio, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 3872 | end_bio_extent_buffer_writepage, |
Liu Bo | 18fdc67 | 2017-09-13 12:18:22 -0600 | [diff] [blame] | 3873 | 0, 0, 0, false); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3874 | if (ret) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 3875 | set_btree_ioerr(p); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 3876 | if (PageWriteback(p)) |
| 3877 | end_page_writeback(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3878 | if (atomic_sub_and_test(num_pages - i, &eb->io_pages)) |
| 3879 | end_extent_buffer_writeback(eb); |
| 3880 | ret = -EIO; |
| 3881 | break; |
| 3882 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3883 | offset += PAGE_SIZE; |
David Sterba | 3d4b949 | 2017-02-10 19:33:41 +0100 | [diff] [blame] | 3884 | update_nr_written(wbc, 1); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3885 | unlock_page(p); |
| 3886 | } |
| 3887 | |
| 3888 | if (unlikely(ret)) { |
| 3889 | for (; i < num_pages; i++) { |
Chris Mason | bbf65cf | 2014-10-04 09:56:45 -0700 | [diff] [blame] | 3890 | struct page *p = eb->pages[i]; |
Liu Bo | 8146502 | 2014-09-23 22:22:33 +0800 | [diff] [blame] | 3891 | clear_page_dirty_for_io(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3892 | unlock_page(p); |
| 3893 | } |
| 3894 | } |
| 3895 | |
| 3896 | return ret; |
| 3897 | } |
| 3898 | |
| 3899 | int btree_write_cache_pages(struct address_space *mapping, |
| 3900 | struct writeback_control *wbc) |
| 3901 | { |
| 3902 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3903 | struct extent_buffer *eb, *prev_eb = NULL; |
| 3904 | struct extent_page_data epd = { |
| 3905 | .bio = NULL, |
| 3906 | .tree = tree, |
| 3907 | .extent_locked = 0, |
| 3908 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
| 3909 | }; |
| 3910 | int ret = 0; |
| 3911 | int done = 0; |
| 3912 | int nr_to_write_done = 0; |
| 3913 | struct pagevec pvec; |
| 3914 | int nr_pages; |
| 3915 | pgoff_t index; |
| 3916 | pgoff_t end; /* Inclusive */ |
| 3917 | int scanned = 0; |
Matthew Wilcox | 10bbd23 | 2017-12-05 17:30:38 -0500 | [diff] [blame] | 3918 | xa_mark_t tag; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3919 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 3920 | pagevec_init(&pvec); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3921 | if (wbc->range_cyclic) { |
| 3922 | index = mapping->writeback_index; /* Start from prev offset */ |
| 3923 | end = -1; |
| 3924 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3925 | index = wbc->range_start >> PAGE_SHIFT; |
| 3926 | end = wbc->range_end >> PAGE_SHIFT; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3927 | scanned = 1; |
| 3928 | } |
| 3929 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3930 | tag = PAGECACHE_TAG_TOWRITE; |
| 3931 | else |
| 3932 | tag = PAGECACHE_TAG_DIRTY; |
| 3933 | retry: |
| 3934 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 3935 | tag_pages_for_writeback(mapping, index, end); |
| 3936 | while (!done && !nr_to_write_done && (index <= end) && |
Jan Kara | 4006f43 | 2017-11-15 17:34:37 -0800 | [diff] [blame] | 3937 | (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, |
Jan Kara | 67fd707 | 2017-11-15 17:35:19 -0800 | [diff] [blame] | 3938 | tag))) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3939 | unsigned i; |
| 3940 | |
| 3941 | scanned = 1; |
| 3942 | for (i = 0; i < nr_pages; i++) { |
| 3943 | struct page *page = pvec.pages[i]; |
| 3944 | |
| 3945 | if (!PagePrivate(page)) |
| 3946 | continue; |
| 3947 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3948 | spin_lock(&mapping->private_lock); |
| 3949 | if (!PagePrivate(page)) { |
| 3950 | spin_unlock(&mapping->private_lock); |
| 3951 | continue; |
| 3952 | } |
| 3953 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3954 | eb = (struct extent_buffer *)page->private; |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3955 | |
| 3956 | /* |
| 3957 | * Shouldn't happen and normally this would be a BUG_ON |
| 3958 | * but no sense in crashing the users box for something |
| 3959 | * we can survive anyway. |
| 3960 | */ |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3961 | if (WARN_ON(!eb)) { |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3962 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3963 | continue; |
| 3964 | } |
| 3965 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3966 | if (eb == prev_eb) { |
| 3967 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3968 | continue; |
| 3969 | } |
| 3970 | |
Josef Bacik | b5bae26 | 2012-09-14 13:43:01 -0400 | [diff] [blame] | 3971 | ret = atomic_inc_not_zero(&eb->refs); |
| 3972 | spin_unlock(&mapping->private_lock); |
| 3973 | if (!ret) |
| 3974 | continue; |
| 3975 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3976 | prev_eb = eb; |
David Sterba | 9df76fb | 2019-03-20 11:21:41 +0100 | [diff] [blame] | 3977 | ret = lock_extent_buffer_for_io(eb, &epd); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3978 | if (!ret) { |
| 3979 | free_extent_buffer(eb); |
| 3980 | continue; |
| 3981 | } |
| 3982 | |
David Sterba | 0ab0206 | 2019-03-20 11:27:57 +0100 | [diff] [blame] | 3983 | ret = write_one_eb(eb, wbc, &epd); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 3984 | if (ret) { |
| 3985 | done = 1; |
| 3986 | free_extent_buffer(eb); |
| 3987 | break; |
| 3988 | } |
| 3989 | free_extent_buffer(eb); |
| 3990 | |
| 3991 | /* |
| 3992 | * the filesystem may choose to bump up nr_to_write. |
| 3993 | * We have to make sure to honor the new nr_to_write |
| 3994 | * at any time |
| 3995 | */ |
| 3996 | nr_to_write_done = wbc->nr_to_write <= 0; |
| 3997 | } |
| 3998 | pagevec_release(&pvec); |
| 3999 | cond_resched(); |
| 4000 | } |
| 4001 | if (!scanned && !done) { |
| 4002 | /* |
| 4003 | * We hit the last page and there is more work to be done: wrap |
| 4004 | * back to the start of the file |
| 4005 | */ |
| 4006 | scanned = 1; |
| 4007 | index = 0; |
| 4008 | goto retry; |
| 4009 | } |
Qu Wenruo | 2b952ee | 2019-03-20 14:27:43 +0800 | [diff] [blame] | 4010 | ASSERT(ret <= 0); |
| 4011 | if (ret < 0) { |
| 4012 | end_write_bio(&epd, ret); |
| 4013 | return ret; |
| 4014 | } |
| 4015 | ret = flush_write_bio(&epd); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4016 | return ret; |
| 4017 | } |
| 4018 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4019 | /** |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 4020 | * 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] | 4021 | * @mapping: address space structure to write |
| 4022 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
David Sterba | 935db85 | 2017-06-23 04:30:28 +0200 | [diff] [blame] | 4023 | * @data: data passed to __extent_writepage function |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4024 | * |
| 4025 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 4026 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 4027 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 4028 | * and msync() need to guarantee that all the data which was dirty at the time |
| 4029 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 4030 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 4031 | * existing IO to complete. |
| 4032 | */ |
David Sterba | 4242b64 | 2017-02-10 19:38:24 +0100 | [diff] [blame] | 4033 | static int extent_write_cache_pages(struct address_space *mapping, |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 4034 | struct writeback_control *wbc, |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 4035 | struct extent_page_data *epd) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4036 | { |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4037 | struct inode *inode = mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4038 | int ret = 0; |
| 4039 | int done = 0; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 4040 | int nr_to_write_done = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4041 | struct pagevec pvec; |
| 4042 | int nr_pages; |
| 4043 | pgoff_t index; |
| 4044 | pgoff_t end; /* Inclusive */ |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4045 | pgoff_t done_index; |
| 4046 | int range_whole = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4047 | int scanned = 0; |
Matthew Wilcox | 10bbd23 | 2017-12-05 17:30:38 -0500 | [diff] [blame] | 4048 | xa_mark_t tag; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4049 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4050 | /* |
| 4051 | * We have to hold onto the inode so that ordered extents can do their |
| 4052 | * work when the IO finishes. The alternative to this is failing to add |
| 4053 | * an ordered extent if the igrab() fails there and that is a huge pain |
| 4054 | * to deal with, so instead just hold onto the inode throughout the |
| 4055 | * writepages operation. If it fails here we are freeing up the inode |
| 4056 | * anyway and we'd rather not waste our time writing out stuff that is |
| 4057 | * going to be truncated anyway. |
| 4058 | */ |
| 4059 | if (!igrab(inode)) |
| 4060 | return 0; |
| 4061 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 4062 | pagevec_init(&pvec); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4063 | if (wbc->range_cyclic) { |
| 4064 | index = mapping->writeback_index; /* Start from prev offset */ |
| 4065 | end = -1; |
| 4066 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4067 | index = wbc->range_start >> PAGE_SHIFT; |
| 4068 | end = wbc->range_end >> PAGE_SHIFT; |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4069 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 4070 | range_whole = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4071 | scanned = 1; |
| 4072 | } |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 4073 | |
| 4074 | /* |
| 4075 | * We do the tagged writepage as long as the snapshot flush bit is set |
| 4076 | * and we are the first one who do the filemap_flush() on this inode. |
| 4077 | * |
| 4078 | * The nr_to_write == LONG_MAX is needed to make sure other flushers do |
| 4079 | * not race in and drop the bit. |
| 4080 | */ |
| 4081 | if (range_whole && wbc->nr_to_write == LONG_MAX && |
| 4082 | test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH, |
| 4083 | &BTRFS_I(inode)->runtime_flags)) |
| 4084 | wbc->tagged_writepages = 1; |
| 4085 | |
| 4086 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 4087 | tag = PAGECACHE_TAG_TOWRITE; |
| 4088 | else |
| 4089 | tag = PAGECACHE_TAG_DIRTY; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4090 | retry: |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 4091 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 4092 | tag_pages_for_writeback(mapping, index, end); |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4093 | done_index = index; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 4094 | while (!done && !nr_to_write_done && (index <= end) && |
Jan Kara | 67fd707 | 2017-11-15 17:35:19 -0800 | [diff] [blame] | 4095 | (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, |
| 4096 | &index, end, tag))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4097 | unsigned i; |
| 4098 | |
| 4099 | scanned = 1; |
| 4100 | for (i = 0; i < nr_pages; i++) { |
| 4101 | struct page *page = pvec.pages[i]; |
| 4102 | |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4103 | done_index = page->index; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4104 | /* |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 4105 | * At this point we hold neither the i_pages lock nor |
| 4106 | * the page lock: the page may be truncated or |
| 4107 | * invalidated (changing page->mapping to NULL), |
| 4108 | * or even swizzled back from swapper_space to |
| 4109 | * tmpfs file mapping |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4110 | */ |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 4111 | if (!trylock_page(page)) { |
Qu Wenruo | f434062 | 2019-03-20 14:27:41 +0800 | [diff] [blame] | 4112 | ret = flush_write_bio(epd); |
| 4113 | BUG_ON(ret < 0); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 4114 | lock_page(page); |
Chris Mason | 01d658f | 2011-11-01 10:08:06 -0400 | [diff] [blame] | 4115 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4116 | |
| 4117 | if (unlikely(page->mapping != mapping)) { |
| 4118 | unlock_page(page); |
| 4119 | continue; |
| 4120 | } |
| 4121 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4122 | if (wbc->sync_mode != WB_SYNC_NONE) { |
Qu Wenruo | f434062 | 2019-03-20 14:27:41 +0800 | [diff] [blame] | 4123 | if (PageWriteback(page)) { |
| 4124 | ret = flush_write_bio(epd); |
| 4125 | BUG_ON(ret < 0); |
| 4126 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4127 | wait_on_page_writeback(page); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 4128 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4129 | |
| 4130 | if (PageWriteback(page) || |
| 4131 | !clear_page_dirty_for_io(page)) { |
| 4132 | unlock_page(page); |
| 4133 | continue; |
| 4134 | } |
| 4135 | |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 4136 | ret = __extent_writepage(page, wbc, epd); |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4137 | if (ret < 0) { |
| 4138 | /* |
| 4139 | * done_index is set past this page, |
| 4140 | * so media errors will not choke |
| 4141 | * background writeout for the entire |
| 4142 | * file. This has consequences for |
| 4143 | * range_cyclic semantics (ie. it may |
| 4144 | * not be suitable for data integrity |
| 4145 | * writeout). |
| 4146 | */ |
| 4147 | done_index = page->index + 1; |
| 4148 | done = 1; |
| 4149 | break; |
| 4150 | } |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 4151 | |
| 4152 | /* |
| 4153 | * the filesystem may choose to bump up nr_to_write. |
| 4154 | * We have to make sure to honor the new nr_to_write |
| 4155 | * at any time |
| 4156 | */ |
| 4157 | nr_to_write_done = wbc->nr_to_write <= 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4158 | } |
| 4159 | pagevec_release(&pvec); |
| 4160 | cond_resched(); |
| 4161 | } |
Liu Bo | 894b36e | 2016-03-07 16:56:22 -0800 | [diff] [blame] | 4162 | if (!scanned && !done) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4163 | /* |
| 4164 | * We hit the last page and there is more work to be done: wrap |
| 4165 | * back to the start of the file |
| 4166 | */ |
| 4167 | scanned = 1; |
| 4168 | index = 0; |
| 4169 | goto retry; |
| 4170 | } |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4171 | |
| 4172 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole)) |
| 4173 | mapping->writeback_index = done_index; |
| 4174 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4175 | btrfs_add_delayed_iput(inode); |
Liu Bo | 894b36e | 2016-03-07 16:56:22 -0800 | [diff] [blame] | 4176 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4177 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4178 | |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 4179 | int extent_write_full_page(struct page *page, struct writeback_control *wbc) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4180 | { |
| 4181 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4182 | struct extent_page_data epd = { |
| 4183 | .bio = NULL, |
Nikolay Borisov | 0a9b0e5 | 2017-12-08 15:55:59 +0200 | [diff] [blame] | 4184 | .tree = &BTRFS_I(page->mapping->host)->io_tree, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4185 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4186 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4187 | }; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4188 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4189 | ret = __extent_writepage(page, wbc, &epd); |
Qu Wenruo | 3065976 | 2019-03-20 14:27:42 +0800 | [diff] [blame] | 4190 | ASSERT(ret <= 0); |
| 4191 | if (ret < 0) { |
| 4192 | end_write_bio(&epd, ret); |
| 4193 | return ret; |
| 4194 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4195 | |
Qu Wenruo | 3065976 | 2019-03-20 14:27:42 +0800 | [diff] [blame] | 4196 | ret = flush_write_bio(&epd); |
| 4197 | ASSERT(ret <= 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4198 | return ret; |
| 4199 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4200 | |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 4201 | int extent_write_locked_range(struct inode *inode, u64 start, u64 end, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4202 | int mode) |
| 4203 | { |
| 4204 | int ret = 0; |
| 4205 | struct address_space *mapping = inode->i_mapping; |
Nikolay Borisov | 5e3ee23 | 2017-12-08 15:55:58 +0200 | [diff] [blame] | 4206 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4207 | struct page *page; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4208 | unsigned long nr_pages = (end - start + PAGE_SIZE) >> |
| 4209 | PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4210 | |
| 4211 | struct extent_page_data epd = { |
| 4212 | .bio = NULL, |
| 4213 | .tree = tree, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4214 | .extent_locked = 1, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4215 | .sync_io = mode == WB_SYNC_ALL, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4216 | }; |
| 4217 | struct writeback_control wbc_writepages = { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4218 | .sync_mode = mode, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4219 | .nr_to_write = nr_pages * 2, |
| 4220 | .range_start = start, |
| 4221 | .range_end = end + 1, |
| 4222 | }; |
| 4223 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4224 | while (start <= end) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4225 | page = find_get_page(mapping, start >> PAGE_SHIFT); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4226 | if (clear_page_dirty_for_io(page)) |
| 4227 | ret = __extent_writepage(page, &wbc_writepages, &epd); |
| 4228 | else { |
Nikolay Borisov | 7087a9d | 2018-11-01 14:09:48 +0200 | [diff] [blame] | 4229 | btrfs_writepage_endio_finish_ordered(page, start, |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 4230 | start + PAGE_SIZE - 1, 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4231 | unlock_page(page); |
| 4232 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4233 | put_page(page); |
| 4234 | start += PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4235 | } |
| 4236 | |
Qu Wenruo | 02c6db4 | 2019-03-20 14:27:45 +0800 | [diff] [blame] | 4237 | ASSERT(ret <= 0); |
| 4238 | if (ret < 0) { |
| 4239 | end_write_bio(&epd, ret); |
| 4240 | return ret; |
| 4241 | } |
| 4242 | ret = flush_write_bio(&epd); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4243 | return ret; |
| 4244 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4245 | |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 4246 | int extent_writepages(struct address_space *mapping, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4247 | struct writeback_control *wbc) |
| 4248 | { |
| 4249 | int ret = 0; |
| 4250 | struct extent_page_data epd = { |
| 4251 | .bio = NULL, |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 4252 | .tree = &BTRFS_I(mapping->host)->io_tree, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 4253 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 4254 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4255 | }; |
| 4256 | |
David Sterba | 935db85 | 2017-06-23 04:30:28 +0200 | [diff] [blame] | 4257 | ret = extent_write_cache_pages(mapping, wbc, &epd); |
Qu Wenruo | a2a72fb | 2019-03-20 14:27:48 +0800 | [diff] [blame] | 4258 | ASSERT(ret <= 0); |
| 4259 | if (ret < 0) { |
| 4260 | end_write_bio(&epd, ret); |
| 4261 | return ret; |
| 4262 | } |
| 4263 | ret = flush_write_bio(&epd); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4264 | return ret; |
| 4265 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4266 | |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 4267 | int extent_readpages(struct address_space *mapping, struct list_head *pages, |
| 4268 | unsigned nr_pages) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4269 | { |
| 4270 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4271 | unsigned long bio_flags = 0; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4272 | struct page *pagepool[16]; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4273 | struct extent_map *em_cached = NULL; |
Nikolay Borisov | 2a3ff0a | 2018-04-19 10:46:36 +0300 | [diff] [blame] | 4274 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4275 | int nr = 0; |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 4276 | u64 prev_em_start = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4277 | |
Nikolay Borisov | 61ed3a1 | 2018-11-29 18:41:31 +0200 | [diff] [blame] | 4278 | while (!list_empty(pages)) { |
Nikolay Borisov | e65ef21 | 2019-03-11 09:55:38 +0200 | [diff] [blame] | 4279 | u64 contig_end = 0; |
| 4280 | |
Nikolay Borisov | 61ed3a1 | 2018-11-29 18:41:31 +0200 | [diff] [blame] | 4281 | for (nr = 0; nr < ARRAY_SIZE(pagepool) && !list_empty(pages);) { |
Nikolay Borisov | f86196e | 2019-01-03 15:29:02 -0800 | [diff] [blame] | 4282 | struct page *page = lru_to_page(pages); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4283 | |
Nikolay Borisov | 61ed3a1 | 2018-11-29 18:41:31 +0200 | [diff] [blame] | 4284 | prefetchw(&page->flags); |
| 4285 | list_del(&page->lru); |
| 4286 | if (add_to_page_cache_lru(page, mapping, page->index, |
| 4287 | readahead_gfp_mask(mapping))) { |
| 4288 | put_page(page); |
Nikolay Borisov | e65ef21 | 2019-03-11 09:55:38 +0200 | [diff] [blame] | 4289 | break; |
Nikolay Borisov | 61ed3a1 | 2018-11-29 18:41:31 +0200 | [diff] [blame] | 4290 | } |
| 4291 | |
| 4292 | pagepool[nr++] = page; |
Nikolay Borisov | e65ef21 | 2019-03-11 09:55:38 +0200 | [diff] [blame] | 4293 | contig_end = page_offset(page) + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4294 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4295 | |
Nikolay Borisov | e65ef21 | 2019-03-11 09:55:38 +0200 | [diff] [blame] | 4296 | if (nr) { |
| 4297 | u64 contig_start = page_offset(pagepool[0]); |
| 4298 | |
| 4299 | ASSERT(contig_start + nr * PAGE_SIZE - 1 == contig_end); |
| 4300 | |
| 4301 | contiguous_readpages(tree, pagepool, nr, contig_start, |
| 4302 | contig_end, &em_cached, &bio, &bio_flags, |
| 4303 | &prev_em_start); |
| 4304 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4305 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 4306 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 4307 | if (em_cached) |
| 4308 | free_extent_map(em_cached); |
| 4309 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4310 | if (bio) |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 4311 | return submit_one_bio(bio, 0, bio_flags); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4312 | return 0; |
| 4313 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4314 | |
| 4315 | /* |
| 4316 | * basic invalidatepage code, this waits on any locked or writeback |
| 4317 | * ranges corresponding to the page, and then deletes any extent state |
| 4318 | * records from the tree |
| 4319 | */ |
| 4320 | int extent_invalidatepage(struct extent_io_tree *tree, |
| 4321 | struct page *page, unsigned long offset) |
| 4322 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4323 | struct extent_state *cached_state = NULL; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4324 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4325 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4326 | size_t blocksize = page->mapping->host->i_sb->s_blocksize; |
| 4327 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4328 | start += ALIGN(offset, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4329 | if (start > end) |
| 4330 | return 0; |
| 4331 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4332 | lock_extent_bits(tree, start, end, &cached_state); |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 4333 | wait_on_page_writeback(page); |
Omar Sandoval | e182163 | 2019-08-15 14:04:04 -0700 | [diff] [blame] | 4334 | clear_extent_bit(tree, start, end, EXTENT_LOCKED | EXTENT_DELALLOC | |
| 4335 | EXTENT_DO_ACCOUNTING, 1, 1, &cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4336 | return 0; |
| 4337 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4338 | |
| 4339 | /* |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4340 | * a helper for releasepage, this tests for areas of the page that |
| 4341 | * are locked or under IO and drops the related state bits if it is safe |
| 4342 | * to drop the page. |
| 4343 | */ |
Nikolay Borisov | 29c68b2d | 2018-04-19 10:46:35 +0300 | [diff] [blame] | 4344 | static int try_release_extent_state(struct extent_io_tree *tree, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4345 | struct page *page, gfp_t mask) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4346 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4347 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4348 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4349 | int ret = 1; |
| 4350 | |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 4351 | if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) { |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4352 | ret = 0; |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 4353 | } else { |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4354 | /* |
| 4355 | * at this point we can safely clear everything except the |
| 4356 | * locked bit and the nodatasum bit |
| 4357 | */ |
David Sterba | 66b0c88 | 2017-10-31 16:30:47 +0100 | [diff] [blame] | 4358 | ret = __clear_extent_bit(tree, start, end, |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 4359 | ~(EXTENT_LOCKED | EXTENT_NODATASUM), |
David Sterba | 66b0c88 | 2017-10-31 16:30:47 +0100 | [diff] [blame] | 4360 | 0, 0, NULL, mask, NULL); |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 4361 | |
| 4362 | /* if clear_extent_bit failed for enomem reasons, |
| 4363 | * we can't allow the release to continue. |
| 4364 | */ |
| 4365 | if (ret < 0) |
| 4366 | ret = 0; |
| 4367 | else |
| 4368 | ret = 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4369 | } |
| 4370 | return ret; |
| 4371 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4372 | |
| 4373 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4374 | * a helper for releasepage. As long as there are no locked extents |
| 4375 | * in the range corresponding to the page, both state records and extent |
| 4376 | * map records are removed |
| 4377 | */ |
Nikolay Borisov | 477a30b | 2018-04-19 10:46:34 +0300 | [diff] [blame] | 4378 | int try_release_extent_mapping(struct page *page, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4379 | { |
| 4380 | struct extent_map *em; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 4381 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4382 | u64 end = start + PAGE_SIZE - 1; |
Filipe Manana | bd3599a | 2018-07-12 01:36:43 +0100 | [diff] [blame] | 4383 | struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host); |
| 4384 | struct extent_io_tree *tree = &btrfs_inode->io_tree; |
| 4385 | struct extent_map_tree *map = &btrfs_inode->extent_tree; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 4386 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 4387 | if (gfpflags_allow_blocking(mask) && |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4388 | page->mapping->host->i_size > SZ_16M) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4389 | u64 len; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4390 | while (start <= end) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4391 | len = end - start + 1; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4392 | write_lock(&map->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 4393 | em = lookup_extent_mapping(map, start, len); |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 4394 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4395 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4396 | break; |
| 4397 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 4398 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
| 4399 | em->start != start) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4400 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4401 | free_extent_map(em); |
| 4402 | break; |
| 4403 | } |
| 4404 | if (!test_range_bit(tree, em->start, |
| 4405 | extent_map_end(em) - 1, |
Nikolay Borisov | 4e586ca | 2019-03-14 15:28:30 +0200 | [diff] [blame] | 4406 | EXTENT_LOCKED, 0, NULL)) { |
Filipe Manana | bd3599a | 2018-07-12 01:36:43 +0100 | [diff] [blame] | 4407 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, |
| 4408 | &btrfs_inode->runtime_flags); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4409 | remove_extent_mapping(map, em); |
| 4410 | /* once for the rb tree */ |
| 4411 | free_extent_map(em); |
| 4412 | } |
| 4413 | start = extent_map_end(em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4414 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 4415 | |
| 4416 | /* once for us */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4417 | free_extent_map(em); |
| 4418 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4419 | } |
Nikolay Borisov | 29c68b2d | 2018-04-19 10:46:35 +0300 | [diff] [blame] | 4420 | return try_release_extent_state(tree, page, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4421 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4422 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4423 | /* |
| 4424 | * helper function for fiemap, which doesn't want to see any holes. |
| 4425 | * This maps until we find something past 'last' |
| 4426 | */ |
| 4427 | static struct extent_map *get_extent_skip_holes(struct inode *inode, |
David Sterba | e3350e1 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4428 | u64 offset, u64 last) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4429 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4430 | u64 sectorsize = btrfs_inode_sectorsize(inode); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4431 | struct extent_map *em; |
| 4432 | u64 len; |
| 4433 | |
| 4434 | if (offset >= last) |
| 4435 | return NULL; |
| 4436 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 4437 | while (1) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4438 | len = last - offset; |
| 4439 | if (len == 0) |
| 4440 | break; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4441 | len = ALIGN(len, sectorsize); |
Nikolay Borisov | 4ab47a8 | 2018-12-12 09:42:32 +0200 | [diff] [blame] | 4442 | em = btrfs_get_extent_fiemap(BTRFS_I(inode), offset, len); |
David Sterba | c704005 | 2011-04-19 18:00:01 +0200 | [diff] [blame] | 4443 | if (IS_ERR_OR_NULL(em)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4444 | return em; |
| 4445 | |
| 4446 | /* if this isn't a hole return it */ |
Nikolay Borisov | 4a2d25c | 2017-11-23 10:51:43 +0200 | [diff] [blame] | 4447 | if (em->block_start != EXTENT_MAP_HOLE) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4448 | return em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4449 | |
| 4450 | /* this is a hole, advance to the next extent */ |
| 4451 | offset = extent_map_end(em); |
| 4452 | free_extent_map(em); |
| 4453 | if (offset >= last) |
| 4454 | break; |
| 4455 | } |
| 4456 | return NULL; |
| 4457 | } |
| 4458 | |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4459 | /* |
| 4460 | * To cache previous fiemap extent |
| 4461 | * |
| 4462 | * Will be used for merging fiemap extent |
| 4463 | */ |
| 4464 | struct fiemap_cache { |
| 4465 | u64 offset; |
| 4466 | u64 phys; |
| 4467 | u64 len; |
| 4468 | u32 flags; |
| 4469 | bool cached; |
| 4470 | }; |
| 4471 | |
| 4472 | /* |
| 4473 | * Helper to submit fiemap extent. |
| 4474 | * |
| 4475 | * Will try to merge current fiemap extent specified by @offset, @phys, |
| 4476 | * @len and @flags with cached one. |
| 4477 | * And only when we fails to merge, cached one will be submitted as |
| 4478 | * fiemap extent. |
| 4479 | * |
| 4480 | * Return value is the same as fiemap_fill_next_extent(). |
| 4481 | */ |
| 4482 | static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo, |
| 4483 | struct fiemap_cache *cache, |
| 4484 | u64 offset, u64 phys, u64 len, u32 flags) |
| 4485 | { |
| 4486 | int ret = 0; |
| 4487 | |
| 4488 | if (!cache->cached) |
| 4489 | goto assign; |
| 4490 | |
| 4491 | /* |
| 4492 | * Sanity check, extent_fiemap() should have ensured that new |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 4493 | * fiemap extent won't overlap with cached one. |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4494 | * Not recoverable. |
| 4495 | * |
| 4496 | * NOTE: Physical address can overlap, due to compression |
| 4497 | */ |
| 4498 | if (cache->offset + cache->len > offset) { |
| 4499 | WARN_ON(1); |
| 4500 | return -EINVAL; |
| 4501 | } |
| 4502 | |
| 4503 | /* |
| 4504 | * Only merges fiemap extents if |
| 4505 | * 1) Their logical addresses are continuous |
| 4506 | * |
| 4507 | * 2) Their physical addresses are continuous |
| 4508 | * So truly compressed (physical size smaller than logical size) |
| 4509 | * extents won't get merged with each other |
| 4510 | * |
| 4511 | * 3) Share same flags except FIEMAP_EXTENT_LAST |
| 4512 | * So regular extent won't get merged with prealloc extent |
| 4513 | */ |
| 4514 | if (cache->offset + cache->len == offset && |
| 4515 | cache->phys + cache->len == phys && |
| 4516 | (cache->flags & ~FIEMAP_EXTENT_LAST) == |
| 4517 | (flags & ~FIEMAP_EXTENT_LAST)) { |
| 4518 | cache->len += len; |
| 4519 | cache->flags |= flags; |
| 4520 | goto try_submit_last; |
| 4521 | } |
| 4522 | |
| 4523 | /* Not mergeable, need to submit cached one */ |
| 4524 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, |
| 4525 | cache->len, cache->flags); |
| 4526 | cache->cached = false; |
| 4527 | if (ret) |
| 4528 | return ret; |
| 4529 | assign: |
| 4530 | cache->cached = true; |
| 4531 | cache->offset = offset; |
| 4532 | cache->phys = phys; |
| 4533 | cache->len = len; |
| 4534 | cache->flags = flags; |
| 4535 | try_submit_last: |
| 4536 | if (cache->flags & FIEMAP_EXTENT_LAST) { |
| 4537 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, |
| 4538 | cache->phys, cache->len, cache->flags); |
| 4539 | cache->cached = false; |
| 4540 | } |
| 4541 | return ret; |
| 4542 | } |
| 4543 | |
| 4544 | /* |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4545 | * Emit last fiemap cache |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4546 | * |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4547 | * The last fiemap cache may still be cached in the following case: |
| 4548 | * 0 4k 8k |
| 4549 | * |<- Fiemap range ->| |
| 4550 | * |<------------ First extent ----------->| |
| 4551 | * |
| 4552 | * In this case, the first extent range will be cached but not emitted. |
| 4553 | * So we must emit it before ending extent_fiemap(). |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4554 | */ |
David Sterba | 5c5aff9 | 2019-03-20 11:29:46 +0100 | [diff] [blame] | 4555 | static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo, |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 4556 | struct fiemap_cache *cache) |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4557 | { |
| 4558 | int ret; |
| 4559 | |
| 4560 | if (!cache->cached) |
| 4561 | return 0; |
| 4562 | |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4563 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, |
| 4564 | cache->len, cache->flags); |
| 4565 | cache->cached = false; |
| 4566 | if (ret > 0) |
| 4567 | ret = 0; |
| 4568 | return ret; |
| 4569 | } |
| 4570 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4571 | int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
David Sterba | 2135fb9 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4572 | __u64 start, __u64 len) |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4573 | { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4574 | int ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4575 | u64 off = start; |
| 4576 | u64 max = start + len; |
| 4577 | u32 flags = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4578 | u32 found_type; |
| 4579 | u64 last; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4580 | u64 last_for_get_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4581 | u64 disko = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4582 | u64 isize = i_size_read(inode); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4583 | struct btrfs_key found_key; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4584 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 4585 | struct extent_state *cached_state = NULL; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4586 | struct btrfs_path *path; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4587 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4588 | struct fiemap_cache cache = { 0 }; |
David Sterba | 5911c8f | 2019-05-15 15:31:04 +0200 | [diff] [blame] | 4589 | struct ulist *roots; |
| 4590 | struct ulist *tmp_ulist; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4591 | int end = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4592 | u64 em_start = 0; |
| 4593 | u64 em_len = 0; |
| 4594 | u64 em_end = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4595 | |
| 4596 | if (len == 0) |
| 4597 | return -EINVAL; |
| 4598 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4599 | path = btrfs_alloc_path(); |
| 4600 | if (!path) |
| 4601 | return -ENOMEM; |
| 4602 | path->leave_spinning = 1; |
| 4603 | |
David Sterba | 5911c8f | 2019-05-15 15:31:04 +0200 | [diff] [blame] | 4604 | roots = ulist_alloc(GFP_KERNEL); |
| 4605 | tmp_ulist = ulist_alloc(GFP_KERNEL); |
| 4606 | if (!roots || !tmp_ulist) { |
| 4607 | ret = -ENOMEM; |
| 4608 | goto out_free_ulist; |
| 4609 | } |
| 4610 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4611 | start = round_down(start, btrfs_inode_sectorsize(inode)); |
| 4612 | len = round_up(max, btrfs_inode_sectorsize(inode)) - start; |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 4613 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4614 | /* |
| 4615 | * lookup the last file extent. We're not using i_size here |
| 4616 | * because there might be preallocation past i_size |
| 4617 | */ |
David Sterba | f85b737 | 2017-01-20 14:54:07 +0100 | [diff] [blame] | 4618 | ret = btrfs_lookup_file_extent(NULL, root, path, |
| 4619 | btrfs_ino(BTRFS_I(inode)), -1, 0); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4620 | if (ret < 0) { |
David Sterba | 5911c8f | 2019-05-15 15:31:04 +0200 | [diff] [blame] | 4621 | goto out_free_ulist; |
Liu Bo | 2d324f5 | 2016-05-17 17:21:48 -0700 | [diff] [blame] | 4622 | } else { |
| 4623 | WARN_ON(!ret); |
| 4624 | if (ret == 1) |
| 4625 | ret = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4626 | } |
Liu Bo | 2d324f5 | 2016-05-17 17:21:48 -0700 | [diff] [blame] | 4627 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4628 | path->slots[0]--; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4629 | 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] | 4630 | found_type = found_key.type; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4631 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4632 | /* No extents, but there might be delalloc bits */ |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4633 | if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) || |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4634 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4635 | /* have to trust i_size as the end */ |
| 4636 | last = (u64)-1; |
| 4637 | last_for_get_extent = isize; |
| 4638 | } else { |
| 4639 | /* |
| 4640 | * remember the start of the last extent. There are a |
| 4641 | * bunch of different factors that go into the length of the |
| 4642 | * extent, so its much less complex to remember where it started |
| 4643 | */ |
| 4644 | last = found_key.offset; |
| 4645 | last_for_get_extent = last + 1; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4646 | } |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4647 | btrfs_release_path(path); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4648 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4649 | /* |
| 4650 | * we might have some extents allocated but more delalloc past those |
| 4651 | * extents. so, we trust isize unless the start of the last extent is |
| 4652 | * beyond isize |
| 4653 | */ |
| 4654 | if (last < isize) { |
| 4655 | last = (u64)-1; |
| 4656 | last_for_get_extent = isize; |
| 4657 | } |
| 4658 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 4659 | lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 4660 | &cached_state); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4661 | |
David Sterba | e3350e1 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4662 | em = get_extent_skip_holes(inode, start, last_for_get_extent); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4663 | if (!em) |
| 4664 | goto out; |
| 4665 | if (IS_ERR(em)) { |
| 4666 | ret = PTR_ERR(em); |
| 4667 | goto out; |
| 4668 | } |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4669 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4670 | while (!end) { |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4671 | u64 offset_in_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4672 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4673 | /* break if the extent we found is outside the range */ |
| 4674 | if (em->start >= max || extent_map_end(em) < off) |
| 4675 | break; |
| 4676 | |
| 4677 | /* |
| 4678 | * get_extent may return an extent that starts before our |
| 4679 | * requested range. We have to make sure the ranges |
| 4680 | * we return to fiemap always move forward and don't |
| 4681 | * overlap, so adjust the offsets here |
| 4682 | */ |
| 4683 | em_start = max(em->start, off); |
| 4684 | |
| 4685 | /* |
| 4686 | * record the offset from the start of the extent |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4687 | * for adjusting the disk offset below. Only do this if the |
| 4688 | * extent isn't compressed since our in ram offset may be past |
| 4689 | * what we have actually allocated on disk. |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4690 | */ |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 4691 | if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4692 | offset_in_extent = em_start - em->start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4693 | em_end = extent_map_end(em); |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4694 | em_len = em_end - em_start; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4695 | flags = 0; |
Filipe Manana | f098631 | 2018-06-20 10:02:30 +0100 | [diff] [blame] | 4696 | if (em->block_start < EXTENT_MAP_LAST_BYTE) |
| 4697 | disko = em->block_start + offset_in_extent; |
| 4698 | else |
| 4699 | disko = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4700 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 4701 | /* |
| 4702 | * bump off for our next call to get_extent |
| 4703 | */ |
| 4704 | off = extent_map_end(em); |
| 4705 | if (off >= max) |
| 4706 | end = 1; |
| 4707 | |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4708 | if (em->block_start == EXTENT_MAP_LAST_BYTE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4709 | end = 1; |
| 4710 | flags |= FIEMAP_EXTENT_LAST; |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4711 | } else if (em->block_start == EXTENT_MAP_INLINE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4712 | flags |= (FIEMAP_EXTENT_DATA_INLINE | |
| 4713 | FIEMAP_EXTENT_NOT_ALIGNED); |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 4714 | } else if (em->block_start == EXTENT_MAP_DELALLOC) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4715 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 4716 | FIEMAP_EXTENT_UNKNOWN); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4717 | } else if (fieinfo->fi_extents_max) { |
| 4718 | u64 bytenr = em->block_start - |
| 4719 | (em->start - em->orig_start); |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4720 | |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4721 | /* |
| 4722 | * As btrfs supports shared space, this information |
| 4723 | * can be exported to userspace tools via |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4724 | * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0 |
| 4725 | * then we're just getting a count and we can skip the |
| 4726 | * lookup stuff. |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4727 | */ |
Edmund Nadolski | bb739cf | 2017-06-28 21:56:58 -0600 | [diff] [blame] | 4728 | ret = btrfs_check_shared(root, |
| 4729 | btrfs_ino(BTRFS_I(inode)), |
David Sterba | 5911c8f | 2019-05-15 15:31:04 +0200 | [diff] [blame] | 4730 | bytenr, roots, tmp_ulist); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4731 | if (ret < 0) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4732 | goto out_free; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4733 | if (ret) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 4734 | flags |= FIEMAP_EXTENT_SHARED; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 4735 | ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4736 | } |
| 4737 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 4738 | flags |= FIEMAP_EXTENT_ENCODED; |
Josef Bacik | 0d2b237 | 2015-05-19 10:44:04 -0400 | [diff] [blame] | 4739 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 4740 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4741 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4742 | free_extent_map(em); |
| 4743 | em = NULL; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4744 | if ((em_start >= last) || em_len == (u64)-1 || |
| 4745 | (last == (u64)-1 && isize <= em_end)) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4746 | flags |= FIEMAP_EXTENT_LAST; |
| 4747 | end = 1; |
| 4748 | } |
| 4749 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4750 | /* now scan forward to see if this is really the last extent. */ |
David Sterba | e3350e1 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 4751 | em = get_extent_skip_holes(inode, off, last_for_get_extent); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4752 | if (IS_ERR(em)) { |
| 4753 | ret = PTR_ERR(em); |
| 4754 | goto out; |
| 4755 | } |
| 4756 | if (!em) { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 4757 | flags |= FIEMAP_EXTENT_LAST; |
| 4758 | end = 1; |
| 4759 | } |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4760 | ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko, |
| 4761 | em_len, flags); |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4762 | if (ret) { |
| 4763 | if (ret == 1) |
| 4764 | ret = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 4765 | goto out_free; |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 4766 | } |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4767 | } |
| 4768 | out_free: |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 4769 | if (!ret) |
David Sterba | 5c5aff9 | 2019-03-20 11:29:46 +0100 | [diff] [blame] | 4770 | ret = emit_last_fiemap_cache(fieinfo, &cache); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4771 | free_extent_map(em); |
| 4772 | out: |
Liu Bo | a52f4cd | 2013-05-01 16:23:41 +0000 | [diff] [blame] | 4773 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 4774 | &cached_state); |
David Sterba | 5911c8f | 2019-05-15 15:31:04 +0200 | [diff] [blame] | 4775 | |
| 4776 | out_free_ulist: |
Colin Ian King | e02d48e | 2019-07-05 08:26:24 +0100 | [diff] [blame] | 4777 | btrfs_free_path(path); |
David Sterba | 5911c8f | 2019-05-15 15:31:04 +0200 | [diff] [blame] | 4778 | ulist_free(roots); |
| 4779 | ulist_free(tmp_ulist); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 4780 | return ret; |
| 4781 | } |
| 4782 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4783 | static void __free_extent_buffer(struct extent_buffer *eb) |
| 4784 | { |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 4785 | btrfs_leak_debug_del(&eb->leak_list); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 4786 | kmem_cache_free(extent_buffer_cache, eb); |
| 4787 | } |
| 4788 | |
Josef Bacik | a26e8c9 | 2014-03-28 17:07:27 -0400 | [diff] [blame] | 4789 | int extent_buffer_under_io(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4790 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4791 | return (atomic_read(&eb->io_pages) || |
| 4792 | test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || |
| 4793 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4794 | } |
| 4795 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4796 | /* |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 4797 | * Release all pages attached to the extent buffer. |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4798 | */ |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 4799 | static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb) |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4800 | { |
Nikolay Borisov | d64766f | 2018-06-27 16:38:22 +0300 | [diff] [blame] | 4801 | int i; |
| 4802 | int num_pages; |
Nikolay Borisov | b0132a3 | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 4803 | int mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4804 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4805 | BUG_ON(extent_buffer_under_io(eb)); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4806 | |
Nikolay Borisov | d64766f | 2018-06-27 16:38:22 +0300 | [diff] [blame] | 4807 | num_pages = num_extent_pages(eb); |
| 4808 | for (i = 0; i < num_pages; i++) { |
| 4809 | struct page *page = eb->pages[i]; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4810 | |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4811 | if (!page) |
| 4812 | continue; |
| 4813 | if (mapped) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4814 | spin_lock(&page->mapping->private_lock); |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4815 | /* |
| 4816 | * We do this since we'll remove the pages after we've |
| 4817 | * removed the eb from the radix tree, so we could race |
| 4818 | * and have this page now attached to the new eb. So |
| 4819 | * only clear page_private if it's still connected to |
| 4820 | * this eb. |
| 4821 | */ |
| 4822 | if (PagePrivate(page) && |
| 4823 | page->private == (unsigned long)eb) { |
| 4824 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
| 4825 | BUG_ON(PageDirty(page)); |
| 4826 | BUG_ON(PageWriteback(page)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4827 | /* |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4828 | * We need to make sure we haven't be attached |
| 4829 | * to a new eb. |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4830 | */ |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4831 | ClearPagePrivate(page); |
| 4832 | set_page_private(page, 0); |
| 4833 | /* One for the page private */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4834 | put_page(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 4835 | } |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 4836 | |
| 4837 | if (mapped) |
| 4838 | spin_unlock(&page->mapping->private_lock); |
| 4839 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4840 | /* One for when we allocated the page */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4841 | put_page(page); |
Nikolay Borisov | d64766f | 2018-06-27 16:38:22 +0300 | [diff] [blame] | 4842 | } |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4843 | } |
| 4844 | |
| 4845 | /* |
| 4846 | * Helper for releasing the extent buffer. |
| 4847 | */ |
| 4848 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) |
| 4849 | { |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 4850 | btrfs_release_extent_buffer_pages(eb); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 4851 | __free_extent_buffer(eb); |
| 4852 | } |
| 4853 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4854 | static struct extent_buffer * |
| 4855 | __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 4856 | unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4857 | { |
| 4858 | struct extent_buffer *eb = NULL; |
| 4859 | |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 4860 | eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4861 | eb->start = start; |
| 4862 | eb->len = len; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 4863 | eb->fs_info = fs_info; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4864 | eb->bflags = 0; |
| 4865 | rwlock_init(&eb->lock); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4866 | atomic_set(&eb->blocking_readers, 0); |
David Sterba | 06297d8 | 2019-05-02 16:47:23 +0200 | [diff] [blame] | 4867 | eb->blocking_writers = 0; |
David Sterba | ed1b4ed | 2018-08-24 16:31:17 +0200 | [diff] [blame] | 4868 | eb->lock_nested = false; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4869 | init_waitqueue_head(&eb->write_lock_wq); |
| 4870 | init_waitqueue_head(&eb->read_lock_wq); |
| 4871 | |
| 4872 | btrfs_leak_debug_add(&eb->leak_list, &buffers); |
| 4873 | |
| 4874 | spin_lock_init(&eb->refs_lock); |
| 4875 | atomic_set(&eb->refs, 1); |
| 4876 | atomic_set(&eb->io_pages, 0); |
| 4877 | |
| 4878 | /* |
| 4879 | * Sanity checks, currently the maximum is 64k covered by 16x 4k pages |
| 4880 | */ |
| 4881 | BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE |
| 4882 | > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4883 | BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE); |
| 4884 | |
David Sterba | 843ccf9 | 2018-08-24 14:56:28 +0200 | [diff] [blame] | 4885 | #ifdef CONFIG_BTRFS_DEBUG |
David Sterba | f3dc24c | 2019-05-02 16:51:53 +0200 | [diff] [blame] | 4886 | eb->spinning_writers = 0; |
David Sterba | afd495a | 2018-08-24 15:57:38 +0200 | [diff] [blame] | 4887 | atomic_set(&eb->spinning_readers, 0); |
David Sterba | 5c9c799 | 2018-08-24 16:15:51 +0200 | [diff] [blame] | 4888 | atomic_set(&eb->read_locks, 0); |
David Sterba | 00801ae | 2019-05-02 16:53:47 +0200 | [diff] [blame] | 4889 | eb->write_locks = 0; |
David Sterba | 843ccf9 | 2018-08-24 14:56:28 +0200 | [diff] [blame] | 4890 | #endif |
| 4891 | |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4892 | return eb; |
| 4893 | } |
| 4894 | |
| 4895 | struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src) |
| 4896 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4897 | int i; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4898 | struct page *p; |
| 4899 | struct extent_buffer *new; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4900 | int num_pages = num_extent_pages(src); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4901 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4902 | new = __alloc_extent_buffer(src->fs_info, src->start, src->len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4903 | if (new == NULL) |
| 4904 | return NULL; |
| 4905 | |
| 4906 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4907 | p = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4908 | if (!p) { |
| 4909 | btrfs_release_extent_buffer(new); |
| 4910 | return NULL; |
| 4911 | } |
| 4912 | attach_extent_buffer_page(new, p); |
| 4913 | WARN_ON(PageDirty(p)); |
| 4914 | SetPageUptodate(p); |
| 4915 | new->pages[i] = p; |
David Sterba | fba1acf | 2016-11-08 17:56:24 +0100 | [diff] [blame] | 4916 | copy_page(page_address(p), page_address(src->pages[i])); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4917 | } |
| 4918 | |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4919 | set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags); |
Nikolay Borisov | b0132a3 | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 4920 | set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4921 | |
| 4922 | return new; |
| 4923 | } |
| 4924 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4925 | struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
| 4926 | u64 start, unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4927 | { |
| 4928 | struct extent_buffer *eb; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4929 | int num_pages; |
| 4930 | int i; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4931 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 4932 | eb = __alloc_extent_buffer(fs_info, start, len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4933 | if (!eb) |
| 4934 | return NULL; |
| 4935 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 4936 | num_pages = num_extent_pages(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4937 | for (i = 0; i < num_pages; i++) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 4938 | eb->pages[i] = alloc_page(GFP_NOFS); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4939 | if (!eb->pages[i]) |
| 4940 | goto err; |
| 4941 | } |
| 4942 | set_extent_buffer_uptodate(eb); |
| 4943 | btrfs_set_header_nritems(eb, 0); |
Nikolay Borisov | b0132a3 | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 4944 | set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 4945 | |
| 4946 | return eb; |
| 4947 | err: |
| 4948 | for (; i > 0; i--) |
| 4949 | __free_page(eb->pages[i - 1]); |
| 4950 | __free_extent_buffer(eb); |
| 4951 | return NULL; |
| 4952 | } |
| 4953 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4954 | 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] | 4955 | u64 start) |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4956 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4957 | return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize); |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 4958 | } |
| 4959 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4960 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
| 4961 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4962 | int refs; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4963 | /* the ref bit is tricky. We have to make sure it is set |
| 4964 | * if we have the buffer dirty. Otherwise the |
| 4965 | * code to free a buffer can end up dropping a dirty |
| 4966 | * page |
| 4967 | * |
| 4968 | * Once the ref bit is set, it won't go away while the |
| 4969 | * buffer is dirty or in writeback, and it also won't |
| 4970 | * go away while we have the reference count on the |
| 4971 | * eb bumped. |
| 4972 | * |
| 4973 | * We can't just set the ref bit without bumping the |
| 4974 | * ref on the eb because free_extent_buffer might |
| 4975 | * see the ref bit and try to clear it. If this happens |
| 4976 | * free_extent_buffer might end up dropping our original |
| 4977 | * ref by mistake and freeing the page before we are able |
| 4978 | * to add one more ref. |
| 4979 | * |
| 4980 | * So bump the ref count first, then set the bit. If someone |
| 4981 | * beat us to it, drop the ref we added. |
| 4982 | */ |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 4983 | refs = atomic_read(&eb->refs); |
| 4984 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 4985 | return; |
| 4986 | |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4987 | spin_lock(&eb->refs_lock); |
| 4988 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4989 | atomic_inc(&eb->refs); |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 4990 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4991 | } |
| 4992 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 4993 | static void mark_extent_buffer_accessed(struct extent_buffer *eb, |
| 4994 | struct page *accessed) |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4995 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4996 | int num_pages, i; |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 4997 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4998 | check_buffer_tree_ref(eb); |
| 4999 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5000 | num_pages = num_extent_pages(eb); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 5001 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5002 | struct page *p = eb->pages[i]; |
| 5003 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 5004 | if (p != accessed) |
| 5005 | mark_page_accessed(p); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 5006 | } |
| 5007 | } |
| 5008 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5009 | struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, |
| 5010 | u64 start) |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5011 | { |
| 5012 | struct extent_buffer *eb; |
| 5013 | |
| 5014 | rcu_read_lock(); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5015 | eb = radix_tree_lookup(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5016 | start >> PAGE_SHIFT); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5017 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
| 5018 | rcu_read_unlock(); |
Filipe Manana | 062c19e | 2015-04-23 11:28:48 +0100 | [diff] [blame] | 5019 | /* |
| 5020 | * Lock our eb's refs_lock to avoid races with |
| 5021 | * free_extent_buffer. When we get our eb it might be flagged |
| 5022 | * with EXTENT_BUFFER_STALE and another task running |
| 5023 | * free_extent_buffer might have seen that flag set, |
| 5024 | * eb->refs == 2, that the buffer isn't under IO (dirty and |
| 5025 | * writeback flags not set) and it's still in the tree (flag |
| 5026 | * EXTENT_BUFFER_TREE_REF set), therefore being in the process |
| 5027 | * of decrementing the extent buffer's reference count twice. |
| 5028 | * So here we could race and increment the eb's reference count, |
| 5029 | * clear its stale flag, mark it as dirty and drop our reference |
| 5030 | * before the other task finishes executing free_extent_buffer, |
| 5031 | * which would later result in an attempt to free an extent |
| 5032 | * buffer that is dirty. |
| 5033 | */ |
| 5034 | if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) { |
| 5035 | spin_lock(&eb->refs_lock); |
| 5036 | spin_unlock(&eb->refs_lock); |
| 5037 | } |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 5038 | mark_extent_buffer_accessed(eb, NULL); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5039 | return eb; |
| 5040 | } |
| 5041 | rcu_read_unlock(); |
| 5042 | |
| 5043 | return NULL; |
| 5044 | } |
| 5045 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 5046 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 5047 | 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] | 5048 | u64 start) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 5049 | { |
| 5050 | struct extent_buffer *eb, *exists = NULL; |
| 5051 | int ret; |
| 5052 | |
| 5053 | eb = find_extent_buffer(fs_info, start); |
| 5054 | if (eb) |
| 5055 | return eb; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 5056 | eb = alloc_dummy_extent_buffer(fs_info, start); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 5057 | if (!eb) |
| 5058 | return NULL; |
| 5059 | eb->fs_info = fs_info; |
| 5060 | again: |
David Sterba | e1860a7 | 2016-05-09 14:11:38 +0200 | [diff] [blame] | 5061 | ret = radix_tree_preload(GFP_NOFS); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 5062 | if (ret) |
| 5063 | goto free_eb; |
| 5064 | spin_lock(&fs_info->buffer_lock); |
| 5065 | ret = radix_tree_insert(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5066 | start >> PAGE_SHIFT, eb); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 5067 | spin_unlock(&fs_info->buffer_lock); |
| 5068 | radix_tree_preload_end(); |
| 5069 | if (ret == -EEXIST) { |
| 5070 | exists = find_extent_buffer(fs_info, start); |
| 5071 | if (exists) |
| 5072 | goto free_eb; |
| 5073 | else |
| 5074 | goto again; |
| 5075 | } |
| 5076 | check_buffer_tree_ref(eb); |
| 5077 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
| 5078 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 5079 | return eb; |
| 5080 | free_eb: |
| 5081 | btrfs_release_extent_buffer(eb); |
| 5082 | return exists; |
| 5083 | } |
| 5084 | #endif |
| 5085 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5086 | struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, |
David Sterba | ce3e698 | 2014-06-15 03:00:04 +0200 | [diff] [blame] | 5087 | u64 start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5088 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 5089 | unsigned long len = fs_info->nodesize; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5090 | int num_pages; |
| 5091 | int i; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5092 | unsigned long index = start >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5093 | struct extent_buffer *eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5094 | struct extent_buffer *exists = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5095 | struct page *p; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5096 | struct address_space *mapping = fs_info->btree_inode->i_mapping; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5097 | int uptodate = 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5098 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5099 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 5100 | if (!IS_ALIGNED(start, fs_info->sectorsize)) { |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5101 | btrfs_err(fs_info, "bad tree block start %llu", start); |
| 5102 | return ERR_PTR(-EINVAL); |
| 5103 | } |
| 5104 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5105 | eb = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5106 | if (eb) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5107 | return eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5108 | |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 5109 | eb = __alloc_extent_buffer(fs_info, start, len); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 5110 | if (!eb) |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5111 | return ERR_PTR(-ENOMEM); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5112 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5113 | num_pages = num_extent_pages(eb); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5114 | for (i = 0; i < num_pages; i++, index++) { |
Michal Hocko | d1b5c56 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 5115 | p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5116 | if (!p) { |
| 5117 | exists = ERR_PTR(-ENOMEM); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5118 | goto free_eb; |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5119 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5120 | |
| 5121 | spin_lock(&mapping->private_lock); |
| 5122 | if (PagePrivate(p)) { |
| 5123 | /* |
| 5124 | * We could have already allocated an eb for this page |
| 5125 | * and attached one so lets see if we can get a ref on |
| 5126 | * the existing eb, and if we can we know it's good and |
| 5127 | * we can just return that one, else we know we can just |
| 5128 | * overwrite page->private. |
| 5129 | */ |
| 5130 | exists = (struct extent_buffer *)p->private; |
| 5131 | if (atomic_inc_not_zero(&exists->refs)) { |
| 5132 | spin_unlock(&mapping->private_lock); |
| 5133 | unlock_page(p); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5134 | put_page(p); |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 5135 | mark_extent_buffer_accessed(exists, p); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5136 | goto free_eb; |
| 5137 | } |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 5138 | exists = NULL; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5139 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5140 | /* |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5141 | * Do this so attach doesn't complain and we need to |
| 5142 | * drop the ref the old guy had. |
| 5143 | */ |
| 5144 | ClearPagePrivate(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5145 | WARN_ON(PageDirty(p)); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5146 | put_page(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5147 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5148 | attach_extent_buffer_page(eb, p); |
| 5149 | spin_unlock(&mapping->private_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5150 | WARN_ON(PageDirty(p)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5151 | eb->pages[i] = p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5152 | if (!PageUptodate(p)) |
| 5153 | uptodate = 0; |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5154 | |
| 5155 | /* |
Nikolay Borisov | b16d011 | 2018-07-04 10:24:52 +0300 | [diff] [blame] | 5156 | * We can't unlock the pages just yet since the extent buffer |
| 5157 | * hasn't been properly inserted in the radix tree, this |
| 5158 | * opens a race with btree_releasepage which can free a page |
| 5159 | * while we are still filling in all pages for the buffer and |
| 5160 | * we could crash. |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5161 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5162 | } |
| 5163 | if (uptodate) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5164 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 5165 | again: |
David Sterba | e1860a7 | 2016-05-09 14:11:38 +0200 | [diff] [blame] | 5166 | ret = radix_tree_preload(GFP_NOFS); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5167 | if (ret) { |
| 5168 | exists = ERR_PTR(ret); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5169 | goto free_eb; |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5170 | } |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5171 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5172 | spin_lock(&fs_info->buffer_lock); |
| 5173 | ret = radix_tree_insert(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5174 | start >> PAGE_SHIFT, eb); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5175 | spin_unlock(&fs_info->buffer_lock); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5176 | radix_tree_preload_end(); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5177 | if (ret == -EEXIST) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5178 | exists = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 5179 | if (exists) |
| 5180 | goto free_eb; |
| 5181 | else |
Josef Bacik | 115391d | 2012-03-09 09:51:43 -0500 | [diff] [blame] | 5182 | goto again; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5183 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5184 | /* add one reference for the tree */ |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5185 | check_buffer_tree_ref(eb); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5186 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5187 | |
| 5188 | /* |
Nikolay Borisov | b16d011 | 2018-07-04 10:24:52 +0300 | [diff] [blame] | 5189 | * Now it's safe to unlock the pages because any calls to |
| 5190 | * btree_releasepage will correctly detect that a page belongs to a |
| 5191 | * live buffer and won't free them prematurely. |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5192 | */ |
Nikolay Borisov | 28187ae | 2018-07-04 10:24:51 +0300 | [diff] [blame] | 5193 | for (i = 0; i < num_pages; i++) |
| 5194 | unlock_page(eb->pages[i]); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5195 | return eb; |
| 5196 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5197 | free_eb: |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 5198 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5199 | for (i = 0; i < num_pages; i++) { |
| 5200 | if (eb->pages[i]) |
| 5201 | unlock_page(eb->pages[i]); |
| 5202 | } |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5203 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5204 | btrfs_release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 5205 | return exists; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5206 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5207 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5208 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
| 5209 | { |
| 5210 | struct extent_buffer *eb = |
| 5211 | container_of(head, struct extent_buffer, rcu_head); |
| 5212 | |
| 5213 | __free_extent_buffer(eb); |
| 5214 | } |
| 5215 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5216 | static int release_extent_buffer(struct extent_buffer *eb) |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5217 | { |
Nikolay Borisov | 07e21c4 | 2018-06-27 16:38:23 +0300 | [diff] [blame] | 5218 | lockdep_assert_held(&eb->refs_lock); |
| 5219 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5220 | WARN_ON(atomic_read(&eb->refs) == 0); |
| 5221 | if (atomic_dec_and_test(&eb->refs)) { |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5222 | if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5223 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5224 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5225 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5226 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5227 | spin_lock(&fs_info->buffer_lock); |
| 5228 | radix_tree_delete(&fs_info->buffer_radix, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5229 | eb->start >> PAGE_SHIFT); |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5230 | spin_unlock(&fs_info->buffer_lock); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 5231 | } else { |
| 5232 | spin_unlock(&eb->refs_lock); |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 5233 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5234 | |
| 5235 | /* Should be safe to release our pages at this point */ |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 5236 | btrfs_release_extent_buffer_pages(eb); |
Josef Bacik | bcb7e44 | 2015-03-16 17:38:02 -0400 | [diff] [blame] | 5237 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Nikolay Borisov | b0132a3 | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 5238 | if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) { |
Josef Bacik | bcb7e44 | 2015-03-16 17:38:02 -0400 | [diff] [blame] | 5239 | __free_extent_buffer(eb); |
| 5240 | return 1; |
| 5241 | } |
| 5242 | #endif |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5243 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5244 | return 1; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5245 | } |
| 5246 | spin_unlock(&eb->refs_lock); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 5247 | |
| 5248 | return 0; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5249 | } |
| 5250 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5251 | void free_extent_buffer(struct extent_buffer *eb) |
| 5252 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5253 | int refs; |
| 5254 | int old; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5255 | if (!eb) |
| 5256 | return; |
| 5257 | |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5258 | while (1) { |
| 5259 | refs = atomic_read(&eb->refs); |
Nikolay Borisov | 46cc775 | 2018-10-15 17:04:01 +0300 | [diff] [blame] | 5260 | if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3) |
| 5261 | || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && |
| 5262 | refs == 1)) |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5263 | break; |
| 5264 | old = atomic_cmpxchg(&eb->refs, refs, refs - 1); |
| 5265 | if (old == refs) |
| 5266 | return; |
| 5267 | } |
| 5268 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5269 | spin_lock(&eb->refs_lock); |
| 5270 | if (atomic_read(&eb->refs) == 2 && |
| 5271 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5272 | !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5273 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5274 | atomic_dec(&eb->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5275 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5276 | /* |
| 5277 | * I know this is terrible, but it's temporary until we stop tracking |
| 5278 | * the uptodate bits and such for the extent buffers. |
| 5279 | */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5280 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5281 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5282 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5283 | void free_extent_buffer_stale(struct extent_buffer *eb) |
| 5284 | { |
| 5285 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5286 | return; |
| 5287 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5288 | spin_lock(&eb->refs_lock); |
| 5289 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); |
| 5290 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5291 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5292 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5293 | atomic_dec(&eb->refs); |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 5294 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5295 | } |
| 5296 | |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 5297 | void clear_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5298 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5299 | int i; |
| 5300 | int num_pages; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5301 | struct page *page; |
| 5302 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5303 | num_pages = num_extent_pages(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5304 | |
| 5305 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5306 | page = eb->pages[i]; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5307 | if (!PageDirty(page)) |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 5308 | continue; |
| 5309 | |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5310 | lock_page(page); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 5311 | WARN_ON(!PagePrivate(page)); |
| 5312 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5313 | clear_page_dirty_for_io(page); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 5314 | xa_lock_irq(&page->mapping->i_pages); |
Matthew Wilcox | 0a943c6 | 2017-12-04 10:37:22 -0500 | [diff] [blame] | 5315 | if (!PageDirty(page)) |
| 5316 | __xa_clear_mark(&page->mapping->i_pages, |
| 5317 | page_index(page), PAGECACHE_TAG_DIRTY); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 5318 | xa_unlock_irq(&page->mapping->i_pages); |
Chris Mason | bf0da8c | 2011-11-04 12:29:37 -0400 | [diff] [blame] | 5319 | ClearPageError(page); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 5320 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5321 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5322 | WARN_ON(atomic_read(&eb->refs) == 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5323 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5324 | |
Liu Bo | abb57ef | 2018-09-14 01:44:42 +0800 | [diff] [blame] | 5325 | bool set_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5326 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5327 | int i; |
| 5328 | int num_pages; |
Liu Bo | abb57ef | 2018-09-14 01:44:42 +0800 | [diff] [blame] | 5329 | bool was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5330 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5331 | check_buffer_tree_ref(eb); |
| 5332 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5333 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5334 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5335 | num_pages = num_extent_pages(eb); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5336 | WARN_ON(atomic_read(&eb->refs) == 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5337 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
| 5338 | |
Liu Bo | abb57ef | 2018-09-14 01:44:42 +0800 | [diff] [blame] | 5339 | if (!was_dirty) |
| 5340 | for (i = 0; i < num_pages; i++) |
| 5341 | set_page_dirty(eb->pages[i]); |
Liu Bo | 51995c3 | 2018-09-14 01:46:08 +0800 | [diff] [blame] | 5342 | |
| 5343 | #ifdef CONFIG_BTRFS_DEBUG |
| 5344 | for (i = 0; i < num_pages; i++) |
| 5345 | ASSERT(PageDirty(eb->pages[i])); |
| 5346 | #endif |
| 5347 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5348 | return was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5349 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5350 | |
David Sterba | 69ba392 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 5351 | void clear_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5352 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5353 | int i; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5354 | struct page *page; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5355 | int num_pages; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5356 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5357 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5358 | num_pages = num_extent_pages(eb); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5359 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5360 | page = eb->pages[i]; |
Chris Mason | 33958dc | 2008-07-30 10:29:12 -0400 | [diff] [blame] | 5361 | if (page) |
| 5362 | ClearPageUptodate(page); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5363 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5364 | } |
| 5365 | |
David Sterba | 09c25a8 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 5366 | void set_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5367 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5368 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5369 | struct page *page; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5370 | int num_pages; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5371 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5372 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5373 | num_pages = num_extent_pages(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5374 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5375 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5376 | SetPageUptodate(page); |
| 5377 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5378 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5379 | |
Nikolay Borisov | c2ccfbc | 2019-04-10 17:24:40 +0300 | [diff] [blame] | 5380 | int read_extent_buffer_pages(struct extent_buffer *eb, int wait, int mirror_num) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5381 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5382 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5383 | struct page *page; |
| 5384 | int err; |
| 5385 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5386 | int locked_pages = 0; |
| 5387 | int all_uptodate = 1; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5388 | int num_pages; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5389 | unsigned long num_reads = 0; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5390 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5391 | unsigned long bio_flags = 0; |
Nikolay Borisov | c2ccfbc | 2019-04-10 17:24:40 +0300 | [diff] [blame] | 5392 | struct extent_io_tree *tree = &BTRFS_I(eb->fs_info->btree_inode)->io_tree; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5393 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5394 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5395 | return 0; |
| 5396 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5397 | num_pages = num_extent_pages(eb); |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5398 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5399 | page = eb->pages[i]; |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5400 | if (wait == WAIT_NONE) { |
David Woodhouse | 2db0496 | 2008-08-07 11:19:43 -0400 | [diff] [blame] | 5401 | if (!trylock_page(page)) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5402 | goto unlock_exit; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5403 | } else { |
| 5404 | lock_page(page); |
| 5405 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5406 | locked_pages++; |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5407 | } |
| 5408 | /* |
| 5409 | * We need to firstly lock all pages to make sure that |
| 5410 | * the uptodate bit of our pages won't be affected by |
| 5411 | * clear_extent_buffer_uptodate(). |
| 5412 | */ |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5413 | for (i = 0; i < num_pages; i++) { |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5414 | page = eb->pages[i]; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5415 | if (!PageUptodate(page)) { |
| 5416 | num_reads++; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5417 | all_uptodate = 0; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5418 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5419 | } |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 5420 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5421 | if (all_uptodate) { |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5422 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5423 | goto unlock_exit; |
| 5424 | } |
| 5425 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 5426 | clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 5427 | eb->read_mirror = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5428 | atomic_set(&eb->io_pages, num_reads); |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5429 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5430 | page = eb->pages[i]; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5431 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5432 | if (!PageUptodate(page)) { |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5433 | if (ret) { |
| 5434 | atomic_dec(&eb->io_pages); |
| 5435 | unlock_page(page); |
| 5436 | continue; |
| 5437 | } |
| 5438 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5439 | ClearPageError(page); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5440 | err = __extent_read_full_page(tree, page, |
David Sterba | 6af49db | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 5441 | btree_get_extent, &bio, |
Josef Bacik | d4c7ca8 | 2013-04-19 19:49:09 -0400 | [diff] [blame] | 5442 | mirror_num, &bio_flags, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 5443 | REQ_META); |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5444 | if (err) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5445 | ret = err; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 5446 | /* |
| 5447 | * We use &bio in above __extent_read_full_page, |
| 5448 | * so we ensure that if it returns error, the |
| 5449 | * current page fails to add itself to bio and |
| 5450 | * it's been unlocked. |
| 5451 | * |
| 5452 | * We must dec io_pages by ourselves. |
| 5453 | */ |
| 5454 | atomic_dec(&eb->io_pages); |
| 5455 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5456 | } else { |
| 5457 | unlock_page(page); |
| 5458 | } |
| 5459 | } |
| 5460 | |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5461 | if (bio) { |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 5462 | err = submit_one_bio(bio, mirror_num, bio_flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5463 | if (err) |
| 5464 | return err; |
Jeff Mahoney | 355808c | 2011-10-03 23:23:14 -0400 | [diff] [blame] | 5465 | } |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 5466 | |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 5467 | if (ret || wait != WAIT_COMPLETE) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5468 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5469 | |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5470 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5471 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5472 | wait_on_page_locked(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5473 | if (!PageUptodate(page)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5474 | ret = -EIO; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5475 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5476 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5477 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5478 | |
| 5479 | unlock_exit: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5480 | while (locked_pages > 0) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5481 | locked_pages--; |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 5482 | page = eb->pages[locked_pages]; |
| 5483 | unlock_page(page); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5484 | } |
| 5485 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5486 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5487 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5488 | void read_extent_buffer(const struct extent_buffer *eb, void *dstv, |
| 5489 | unsigned long start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5490 | { |
| 5491 | size_t cur; |
| 5492 | size_t offset; |
| 5493 | struct page *page; |
| 5494 | char *kaddr; |
| 5495 | char *dst = (char *)dstv; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5496 | size_t start_offset = offset_in_page(eb->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5497 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5498 | |
Liu Bo | f716abd | 2017-08-09 11:10:16 -0600 | [diff] [blame] | 5499 | if (start + len > eb->len) { |
| 5500 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n", |
| 5501 | eb->start, eb->len, start, len); |
| 5502 | memset(dst, 0, len); |
| 5503 | return; |
| 5504 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5505 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5506 | offset = offset_in_page(start_offset + start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5507 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5508 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5509 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5510 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5511 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5512 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5513 | memcpy(dst, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5514 | |
| 5515 | dst += cur; |
| 5516 | len -= cur; |
| 5517 | offset = 0; |
| 5518 | i++; |
| 5519 | } |
| 5520 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5521 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5522 | int read_extent_buffer_to_user(const struct extent_buffer *eb, |
| 5523 | void __user *dstv, |
| 5524 | unsigned long start, unsigned long len) |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5525 | { |
| 5526 | size_t cur; |
| 5527 | size_t offset; |
| 5528 | struct page *page; |
| 5529 | char *kaddr; |
| 5530 | char __user *dst = (char __user *)dstv; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5531 | size_t start_offset = offset_in_page(eb->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5532 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5533 | int ret = 0; |
| 5534 | |
| 5535 | WARN_ON(start > eb->len); |
| 5536 | WARN_ON(start + len > eb->start + eb->len); |
| 5537 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5538 | offset = offset_in_page(start_offset + start); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5539 | |
| 5540 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5541 | page = eb->pages[i]; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5542 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5543 | cur = min(len, (PAGE_SIZE - offset)); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 5544 | kaddr = page_address(page); |
| 5545 | if (copy_to_user(dst, kaddr + offset, cur)) { |
| 5546 | ret = -EFAULT; |
| 5547 | break; |
| 5548 | } |
| 5549 | |
| 5550 | dst += cur; |
| 5551 | len -= cur; |
| 5552 | offset = 0; |
| 5553 | i++; |
| 5554 | } |
| 5555 | |
| 5556 | return ret; |
| 5557 | } |
| 5558 | |
Liu Bo | 415b35a | 2016-06-17 19:16:21 -0700 | [diff] [blame] | 5559 | /* |
| 5560 | * return 0 if the item is found within a page. |
| 5561 | * return 1 if the item spans two pages. |
| 5562 | * return -EINVAL otherwise. |
| 5563 | */ |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5564 | int map_private_extent_buffer(const struct extent_buffer *eb, |
| 5565 | unsigned long start, unsigned long min_len, |
| 5566 | char **map, unsigned long *map_start, |
| 5567 | unsigned long *map_len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5568 | { |
Johannes Thumshirn | cc2c39d | 2018-11-28 09:54:54 +0100 | [diff] [blame] | 5569 | size_t offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5570 | char *kaddr; |
| 5571 | struct page *p; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5572 | size_t start_offset = offset_in_page(eb->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5573 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5574 | unsigned long end_i = (start_offset + start + min_len - 1) >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5575 | PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5576 | |
Liu Bo | f716abd | 2017-08-09 11:10:16 -0600 | [diff] [blame] | 5577 | if (start + min_len > eb->len) { |
| 5578 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n", |
| 5579 | eb->start, eb->len, start, min_len); |
| 5580 | return -EINVAL; |
| 5581 | } |
| 5582 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5583 | if (i != end_i) |
Liu Bo | 415b35a | 2016-06-17 19:16:21 -0700 | [diff] [blame] | 5584 | return 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5585 | |
| 5586 | if (i == 0) { |
| 5587 | offset = start_offset; |
| 5588 | *map_start = 0; |
| 5589 | } else { |
| 5590 | offset = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5591 | *map_start = ((u64)i << PAGE_SHIFT) - start_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5592 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5593 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5594 | p = eb->pages[i]; |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5595 | kaddr = page_address(p); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5596 | *map = kaddr + offset; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5597 | *map_len = PAGE_SIZE - offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5598 | return 0; |
| 5599 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5600 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 5601 | int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, |
| 5602 | unsigned long start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5603 | { |
| 5604 | size_t cur; |
| 5605 | size_t offset; |
| 5606 | struct page *page; |
| 5607 | char *kaddr; |
| 5608 | char *ptr = (char *)ptrv; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5609 | size_t start_offset = offset_in_page(eb->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5610 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5611 | int ret = 0; |
| 5612 | |
| 5613 | WARN_ON(start > eb->len); |
| 5614 | WARN_ON(start + len > eb->start + eb->len); |
| 5615 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5616 | offset = offset_in_page(start_offset + start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5617 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5618 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5619 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5620 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5621 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5622 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5623 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5624 | ret = memcmp(ptr, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5625 | if (ret) |
| 5626 | break; |
| 5627 | |
| 5628 | ptr += cur; |
| 5629 | len -= cur; |
| 5630 | offset = 0; |
| 5631 | i++; |
| 5632 | } |
| 5633 | return ret; |
| 5634 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5635 | |
David Sterba | f157bf7 | 2016-11-09 17:43:38 +0100 | [diff] [blame] | 5636 | void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb, |
| 5637 | const void *srcv) |
| 5638 | { |
| 5639 | char *kaddr; |
| 5640 | |
| 5641 | WARN_ON(!PageUptodate(eb->pages[0])); |
| 5642 | kaddr = page_address(eb->pages[0]); |
| 5643 | memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv, |
| 5644 | BTRFS_FSID_SIZE); |
| 5645 | } |
| 5646 | |
| 5647 | void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv) |
| 5648 | { |
| 5649 | char *kaddr; |
| 5650 | |
| 5651 | WARN_ON(!PageUptodate(eb->pages[0])); |
| 5652 | kaddr = page_address(eb->pages[0]); |
| 5653 | memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv, |
| 5654 | BTRFS_FSID_SIZE); |
| 5655 | } |
| 5656 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5657 | void write_extent_buffer(struct extent_buffer *eb, const void *srcv, |
| 5658 | unsigned long start, unsigned long len) |
| 5659 | { |
| 5660 | size_t cur; |
| 5661 | size_t offset; |
| 5662 | struct page *page; |
| 5663 | char *kaddr; |
| 5664 | char *src = (char *)srcv; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5665 | size_t start_offset = offset_in_page(eb->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5666 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5667 | |
| 5668 | WARN_ON(start > eb->len); |
| 5669 | WARN_ON(start + len > eb->start + eb->len); |
| 5670 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5671 | offset = offset_in_page(start_offset + start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5672 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5673 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5674 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5675 | WARN_ON(!PageUptodate(page)); |
| 5676 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5677 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5678 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5679 | memcpy(kaddr + offset, src, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5680 | |
| 5681 | src += cur; |
| 5682 | len -= cur; |
| 5683 | offset = 0; |
| 5684 | i++; |
| 5685 | } |
| 5686 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5687 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 5688 | void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start, |
| 5689 | unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5690 | { |
| 5691 | size_t cur; |
| 5692 | size_t offset; |
| 5693 | struct page *page; |
| 5694 | char *kaddr; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5695 | size_t start_offset = offset_in_page(eb->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5696 | unsigned long i = (start_offset + start) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5697 | |
| 5698 | WARN_ON(start > eb->len); |
| 5699 | WARN_ON(start + len > eb->start + eb->len); |
| 5700 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5701 | offset = offset_in_page(start_offset + start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5702 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5703 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5704 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5705 | WARN_ON(!PageUptodate(page)); |
| 5706 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5707 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5708 | kaddr = page_address(page); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 5709 | memset(kaddr + offset, 0, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5710 | |
| 5711 | len -= cur; |
| 5712 | offset = 0; |
| 5713 | i++; |
| 5714 | } |
| 5715 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5716 | |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 5717 | void copy_extent_buffer_full(struct extent_buffer *dst, |
| 5718 | struct extent_buffer *src) |
| 5719 | { |
| 5720 | int i; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5721 | int num_pages; |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 5722 | |
| 5723 | ASSERT(dst->len == src->len); |
| 5724 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5725 | num_pages = num_extent_pages(dst); |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 5726 | for (i = 0; i < num_pages; i++) |
| 5727 | copy_page(page_address(dst->pages[i]), |
| 5728 | page_address(src->pages[i])); |
| 5729 | } |
| 5730 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5731 | void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, |
| 5732 | unsigned long dst_offset, unsigned long src_offset, |
| 5733 | unsigned long len) |
| 5734 | { |
| 5735 | u64 dst_len = dst->len; |
| 5736 | size_t cur; |
| 5737 | size_t offset; |
| 5738 | struct page *page; |
| 5739 | char *kaddr; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5740 | size_t start_offset = offset_in_page(dst->start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5741 | unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5742 | |
| 5743 | WARN_ON(src->len != dst_len); |
| 5744 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5745 | offset = offset_in_page(start_offset + dst_offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5746 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5747 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5748 | page = dst->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5749 | WARN_ON(!PageUptodate(page)); |
| 5750 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5751 | cur = min(len, (unsigned long)(PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5752 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5753 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5754 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5755 | |
| 5756 | src_offset += cur; |
| 5757 | len -= cur; |
| 5758 | offset = 0; |
| 5759 | i++; |
| 5760 | } |
| 5761 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5762 | |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5763 | /* |
| 5764 | * eb_bitmap_offset() - calculate the page and offset of the byte containing the |
| 5765 | * given bit number |
| 5766 | * @eb: the extent buffer |
| 5767 | * @start: offset of the bitmap item in the extent buffer |
| 5768 | * @nr: bit number |
| 5769 | * @page_index: return index of the page in the extent buffer that contains the |
| 5770 | * given bit number |
| 5771 | * @page_offset: return offset into the page given by page_index |
| 5772 | * |
| 5773 | * This helper hides the ugliness of finding the byte in an extent buffer which |
| 5774 | * contains a given bit. |
| 5775 | */ |
| 5776 | static inline void eb_bitmap_offset(struct extent_buffer *eb, |
| 5777 | unsigned long start, unsigned long nr, |
| 5778 | unsigned long *page_index, |
| 5779 | size_t *page_offset) |
| 5780 | { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5781 | size_t start_offset = offset_in_page(eb->start); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5782 | size_t byte_offset = BIT_BYTE(nr); |
| 5783 | size_t offset; |
| 5784 | |
| 5785 | /* |
| 5786 | * The byte we want is the offset of the extent buffer + the offset of |
| 5787 | * the bitmap item in the extent buffer + the offset of the byte in the |
| 5788 | * bitmap item. |
| 5789 | */ |
| 5790 | offset = start_offset + start + byte_offset; |
| 5791 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5792 | *page_index = offset >> PAGE_SHIFT; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5793 | *page_offset = offset_in_page(offset); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5794 | } |
| 5795 | |
| 5796 | /** |
| 5797 | * extent_buffer_test_bit - determine whether a bit in a bitmap item is set |
| 5798 | * @eb: the extent buffer |
| 5799 | * @start: offset of the bitmap item in the extent buffer |
| 5800 | * @nr: bit number to test |
| 5801 | */ |
| 5802 | int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start, |
| 5803 | unsigned long nr) |
| 5804 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5805 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5806 | struct page *page; |
| 5807 | unsigned long i; |
| 5808 | size_t offset; |
| 5809 | |
| 5810 | eb_bitmap_offset(eb, start, nr, &i, &offset); |
| 5811 | page = eb->pages[i]; |
| 5812 | WARN_ON(!PageUptodate(page)); |
| 5813 | kaddr = page_address(page); |
| 5814 | return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1))); |
| 5815 | } |
| 5816 | |
| 5817 | /** |
| 5818 | * extent_buffer_bitmap_set - set an area of a bitmap |
| 5819 | * @eb: the extent buffer |
| 5820 | * @start: offset of the bitmap item in the extent buffer |
| 5821 | * @pos: bit number of the first bit |
| 5822 | * @len: number of bits to set |
| 5823 | */ |
| 5824 | void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start, |
| 5825 | unsigned long pos, unsigned long len) |
| 5826 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5827 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5828 | struct page *page; |
| 5829 | unsigned long i; |
| 5830 | size_t offset; |
| 5831 | const unsigned int size = pos + len; |
| 5832 | int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5833 | u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5834 | |
| 5835 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 5836 | page = eb->pages[i]; |
| 5837 | WARN_ON(!PageUptodate(page)); |
| 5838 | kaddr = page_address(page); |
| 5839 | |
| 5840 | while (len >= bits_to_set) { |
| 5841 | kaddr[offset] |= mask_to_set; |
| 5842 | len -= bits_to_set; |
| 5843 | bits_to_set = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5844 | mask_to_set = ~0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5845 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5846 | offset = 0; |
| 5847 | page = eb->pages[++i]; |
| 5848 | WARN_ON(!PageUptodate(page)); |
| 5849 | kaddr = page_address(page); |
| 5850 | } |
| 5851 | } |
| 5852 | if (len) { |
| 5853 | mask_to_set &= BITMAP_LAST_BYTE_MASK(size); |
| 5854 | kaddr[offset] |= mask_to_set; |
| 5855 | } |
| 5856 | } |
| 5857 | |
| 5858 | |
| 5859 | /** |
| 5860 | * extent_buffer_bitmap_clear - clear an area of a bitmap |
| 5861 | * @eb: the extent buffer |
| 5862 | * @start: offset of the bitmap item in the extent buffer |
| 5863 | * @pos: bit number of the first bit |
| 5864 | * @len: number of bits to clear |
| 5865 | */ |
| 5866 | void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start, |
| 5867 | unsigned long pos, unsigned long len) |
| 5868 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5869 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5870 | struct page *page; |
| 5871 | unsigned long i; |
| 5872 | size_t offset; |
| 5873 | const unsigned int size = pos + len; |
| 5874 | int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 5875 | u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5876 | |
| 5877 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 5878 | page = eb->pages[i]; |
| 5879 | WARN_ON(!PageUptodate(page)); |
| 5880 | kaddr = page_address(page); |
| 5881 | |
| 5882 | while (len >= bits_to_clear) { |
| 5883 | kaddr[offset] &= ~mask_to_clear; |
| 5884 | len -= bits_to_clear; |
| 5885 | bits_to_clear = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 5886 | mask_to_clear = ~0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5887 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 5888 | offset = 0; |
| 5889 | page = eb->pages[++i]; |
| 5890 | WARN_ON(!PageUptodate(page)); |
| 5891 | kaddr = page_address(page); |
| 5892 | } |
| 5893 | } |
| 5894 | if (len) { |
| 5895 | mask_to_clear &= BITMAP_LAST_BYTE_MASK(size); |
| 5896 | kaddr[offset] &= ~mask_to_clear; |
| 5897 | } |
| 5898 | } |
| 5899 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5900 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
| 5901 | { |
| 5902 | unsigned long distance = (src > dst) ? src - dst : dst - src; |
| 5903 | return distance < len; |
| 5904 | } |
| 5905 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5906 | static void copy_pages(struct page *dst_page, struct page *src_page, |
| 5907 | unsigned long dst_off, unsigned long src_off, |
| 5908 | unsigned long len) |
| 5909 | { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5910 | char *dst_kaddr = page_address(dst_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5911 | char *src_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5912 | int must_memmove = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5913 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5914 | if (dst_page != src_page) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 5915 | src_kaddr = page_address(src_page); |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5916 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5917 | src_kaddr = dst_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5918 | if (areas_overlap(src_off, dst_off, len)) |
| 5919 | must_memmove = 1; |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 5920 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5921 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5922 | if (must_memmove) |
| 5923 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); |
| 5924 | else |
| 5925 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5926 | } |
| 5927 | |
| 5928 | void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5929 | unsigned long src_offset, unsigned long len) |
| 5930 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5931 | struct btrfs_fs_info *fs_info = dst->fs_info; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5932 | size_t cur; |
| 5933 | size_t dst_off_in_page; |
| 5934 | size_t src_off_in_page; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5935 | size_t start_offset = offset_in_page(dst->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5936 | unsigned long dst_i; |
| 5937 | unsigned long src_i; |
| 5938 | |
| 5939 | if (src_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5940 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5941 | "memmove bogus src_offset %lu move len %lu dst len %lu", |
| 5942 | src_offset, len, dst->len); |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 5943 | BUG(); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5944 | } |
| 5945 | if (dst_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5946 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5947 | "memmove bogus dst_offset %lu move len %lu dst len %lu", |
| 5948 | dst_offset, len, dst->len); |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 5949 | BUG(); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5950 | } |
| 5951 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5952 | while (len > 0) { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5953 | dst_off_in_page = offset_in_page(start_offset + dst_offset); |
| 5954 | src_off_in_page = offset_in_page(start_offset + src_offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5955 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5956 | dst_i = (start_offset + dst_offset) >> PAGE_SHIFT; |
| 5957 | src_i = (start_offset + src_offset) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5958 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5959 | cur = min(len, (unsigned long)(PAGE_SIZE - |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5960 | src_off_in_page)); |
| 5961 | cur = min_t(unsigned long, cur, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5962 | (unsigned long)(PAGE_SIZE - dst_off_in_page)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5963 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5964 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5965 | dst_off_in_page, src_off_in_page, cur); |
| 5966 | |
| 5967 | src_offset += cur; |
| 5968 | dst_offset += cur; |
| 5969 | len -= cur; |
| 5970 | } |
| 5971 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5972 | |
| 5973 | void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, |
| 5974 | unsigned long src_offset, unsigned long len) |
| 5975 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5976 | struct btrfs_fs_info *fs_info = dst->fs_info; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5977 | size_t cur; |
| 5978 | size_t dst_off_in_page; |
| 5979 | size_t src_off_in_page; |
| 5980 | unsigned long dst_end = dst_offset + len - 1; |
| 5981 | unsigned long src_end = src_offset + len - 1; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 5982 | size_t start_offset = offset_in_page(dst->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5983 | unsigned long dst_i; |
| 5984 | unsigned long src_i; |
| 5985 | |
| 5986 | if (src_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5987 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5988 | "memmove bogus src_offset %lu move len %lu len %lu", |
| 5989 | src_offset, len, dst->len); |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 5990 | BUG(); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5991 | } |
| 5992 | if (dst_offset + len > dst->len) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5993 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5994 | "memmove bogus dst_offset %lu move len %lu len %lu", |
| 5995 | dst_offset, len, dst->len); |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 5996 | BUG(); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5997 | } |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5998 | if (dst_offset < src_offset) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5999 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
| 6000 | return; |
| 6001 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6002 | while (len > 0) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6003 | dst_i = (start_offset + dst_end) >> PAGE_SHIFT; |
| 6004 | src_i = (start_offset + src_end) >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6005 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 6006 | dst_off_in_page = offset_in_page(start_offset + dst_end); |
| 6007 | src_off_in_page = offset_in_page(start_offset + src_end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6008 | |
| 6009 | cur = min_t(unsigned long, len, src_off_in_page + 1); |
| 6010 | cur = min(cur, dst_off_in_page + 1); |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 6011 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6012 | dst_off_in_page - cur + 1, |
| 6013 | src_off_in_page - cur + 1, cur); |
| 6014 | |
| 6015 | dst_end -= cur; |
| 6016 | src_end -= cur; |
| 6017 | len -= cur; |
| 6018 | } |
| 6019 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 6020 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 6021 | int try_release_extent_buffer(struct page *page) |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 6022 | { |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 6023 | struct extent_buffer *eb; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 6024 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 6025 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6026 | * 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] | 6027 | * now. |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 6028 | */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6029 | spin_lock(&page->mapping->private_lock); |
| 6030 | if (!PagePrivate(page)) { |
| 6031 | spin_unlock(&page->mapping->private_lock); |
| 6032 | return 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 6033 | } |
| 6034 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6035 | eb = (struct extent_buffer *)page->private; |
| 6036 | BUG_ON(!eb); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 6037 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 6038 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6039 | * This is a little awful but should be ok, we need to make sure that |
| 6040 | * the eb doesn't disappear out from under us while we're looking at |
| 6041 | * this page. |
| 6042 | */ |
| 6043 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 6044 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6045 | spin_unlock(&eb->refs_lock); |
| 6046 | spin_unlock(&page->mapping->private_lock); |
| 6047 | return 0; |
| 6048 | } |
| 6049 | spin_unlock(&page->mapping->private_lock); |
| 6050 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6051 | /* |
| 6052 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
| 6053 | * so just return, this page will likely be freed soon anyway. |
| 6054 | */ |
| 6055 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
| 6056 | spin_unlock(&eb->refs_lock); |
| 6057 | return 0; |
| 6058 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6059 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 6060 | return release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 6061 | } |