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> |
Sweet Tea Dorminy | 395cb57 | 2022-04-06 14:24:18 -0400 | [diff] [blame] | 9 | #include <linux/sched/mm.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 10 | #include <linux/spinlock.h> |
| 11 | #include <linux/blkdev.h> |
| 12 | #include <linux/swap.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 13 | #include <linux/writeback.h> |
| 14 | #include <linux/pagevec.h> |
Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 15 | #include <linux/prefetch.h> |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 16 | #include <linux/fsverity.h> |
Johannes Thumshirn | cea6280 | 2021-03-16 19:04:01 +0900 | [diff] [blame] | 17 | #include "misc.h" |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 18 | #include "extent_io.h" |
Josef Bacik | 9c7d3a5 | 2019-09-23 10:05:19 -0400 | [diff] [blame] | 19 | #include "extent-io-tree.h" |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 20 | #include "extent_map.h" |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 21 | #include "ctree.h" |
| 22 | #include "btrfs_inode.h" |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 23 | #include "volumes.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 24 | #include "check-integrity.h" |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 25 | #include "locking.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 26 | #include "rcu-string.h" |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 27 | #include "backref.h" |
David Sterba | 6af49db | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 28 | #include "disk-io.h" |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 29 | #include "subpage.h" |
Naohiro Aota | d3575156 | 2021-02-04 19:21:54 +0900 | [diff] [blame] | 30 | #include "zoned.h" |
Naohiro Aota | 0bc09ca | 2021-02-04 19:22:08 +0900 | [diff] [blame] | 31 | #include "block-group.h" |
David Sterba | 2a5232a | 2021-07-27 14:47:09 +0200 | [diff] [blame] | 32 | #include "compression.h" |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 33 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 34 | static struct kmem_cache *extent_state_cache; |
| 35 | static struct kmem_cache *extent_buffer_cache; |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 36 | static struct bio_set btrfs_bioset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 37 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 38 | static inline bool extent_state_in_tree(const struct extent_state *state) |
| 39 | { |
| 40 | return !RB_EMPTY_NODE(&state->rb_node); |
| 41 | } |
| 42 | |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 43 | #ifdef CONFIG_BTRFS_DEBUG |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 44 | static LIST_HEAD(states); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 45 | static DEFINE_SPINLOCK(leak_lock); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 46 | |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 47 | static inline void btrfs_leak_debug_add(spinlock_t *lock, |
| 48 | struct list_head *new, |
| 49 | struct list_head *head) |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 50 | { |
| 51 | unsigned long flags; |
| 52 | |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 53 | spin_lock_irqsave(lock, flags); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 54 | list_add(new, head); |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 55 | spin_unlock_irqrestore(lock, flags); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 58 | static inline void btrfs_leak_debug_del(spinlock_t *lock, |
| 59 | struct list_head *entry) |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 60 | { |
| 61 | unsigned long flags; |
| 62 | |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 63 | spin_lock_irqsave(lock, flags); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 64 | list_del(entry); |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 65 | spin_unlock_irqrestore(lock, flags); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 68 | void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info) |
Josef Bacik | 33ca832 | 2019-09-23 10:05:17 -0400 | [diff] [blame] | 69 | { |
| 70 | struct extent_buffer *eb; |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 71 | unsigned long flags; |
Josef Bacik | 33ca832 | 2019-09-23 10:05:17 -0400 | [diff] [blame] | 72 | |
Josef Bacik | 8c38938 | 2020-02-14 16:11:42 -0500 | [diff] [blame] | 73 | /* |
| 74 | * If we didn't get into open_ctree our allocated_ebs will not be |
| 75 | * initialized, so just skip this. |
| 76 | */ |
| 77 | if (!fs_info->allocated_ebs.next) |
| 78 | return; |
| 79 | |
Qu Wenruo | b95b78e | 2022-03-15 18:01:33 +0800 | [diff] [blame] | 80 | WARN_ON(!list_empty(&fs_info->allocated_ebs)); |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 81 | spin_lock_irqsave(&fs_info->eb_leak_lock, flags); |
| 82 | while (!list_empty(&fs_info->allocated_ebs)) { |
| 83 | eb = list_first_entry(&fs_info->allocated_ebs, |
| 84 | struct extent_buffer, leak_list); |
Josef Bacik | 8c38938 | 2020-02-14 16:11:42 -0500 | [diff] [blame] | 85 | pr_err( |
| 86 | "BTRFS: buffer leak start %llu len %lu refs %d bflags %lu owner %llu\n", |
| 87 | eb->start, eb->len, atomic_read(&eb->refs), eb->bflags, |
| 88 | btrfs_header_owner(eb)); |
Josef Bacik | 33ca832 | 2019-09-23 10:05:17 -0400 | [diff] [blame] | 89 | list_del(&eb->leak_list); |
| 90 | kmem_cache_free(extent_buffer_cache, eb); |
| 91 | } |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 92 | spin_unlock_irqrestore(&fs_info->eb_leak_lock, flags); |
Josef Bacik | 33ca832 | 2019-09-23 10:05:17 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static inline void btrfs_extent_state_leak_debug_check(void) |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 96 | { |
| 97 | struct extent_state *state; |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 98 | |
| 99 | while (!list_empty(&states)) { |
| 100 | state = list_entry(states.next, struct extent_state, leak_list); |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 101 | 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] | 102 | state->start, state->end, state->state, |
| 103 | extent_state_in_tree(state), |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 104 | refcount_read(&state->refs)); |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 105 | list_del(&state->leak_list); |
| 106 | kmem_cache_free(extent_state_cache, state); |
| 107 | } |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 108 | } |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 109 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 110 | #define btrfs_debug_check_extent_io_range(tree, start, end) \ |
| 111 | __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end)) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 112 | static inline void __btrfs_debug_check_extent_io_range(const char *caller, |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 113 | struct extent_io_tree *tree, u64 start, u64 end) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 114 | { |
Nikolay Borisov | 65a680f | 2018-11-01 14:09:49 +0200 | [diff] [blame] | 115 | struct inode *inode = tree->private_data; |
| 116 | u64 isize; |
| 117 | |
| 118 | if (!inode || !is_data_inode(inode)) |
| 119 | return; |
| 120 | |
| 121 | isize = i_size_read(inode); |
| 122 | if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) { |
| 123 | btrfs_debug_rl(BTRFS_I(inode)->root->fs_info, |
| 124 | "%s: ino %llu isize %llu odd range [%llu,%llu]", |
| 125 | caller, btrfs_ino(BTRFS_I(inode)), isize, start, end); |
| 126 | } |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 127 | } |
Eric Sandeen | 6d49ba1 | 2013-04-22 16:12:31 +0000 | [diff] [blame] | 128 | #else |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 129 | #define btrfs_leak_debug_add(lock, new, head) do {} while (0) |
| 130 | #define btrfs_leak_debug_del(lock, entry) do {} while (0) |
Josef Bacik | 33ca832 | 2019-09-23 10:05:17 -0400 | [diff] [blame] | 131 | #define btrfs_extent_state_leak_debug_check() do {} while (0) |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 132 | #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] | 133 | #endif |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 134 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 135 | struct tree_entry { |
| 136 | u64 start; |
| 137 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 138 | struct rb_node rb_node; |
| 139 | }; |
| 140 | |
Christoph Hellwig | 7aab8b3 | 2022-04-15 16:33:24 +0200 | [diff] [blame] | 141 | /* |
| 142 | * Structure to record info about the bio being assembled, and other info like |
| 143 | * how many bytes are there before stripe/ordered extent boundary. |
| 144 | */ |
| 145 | struct btrfs_bio_ctrl { |
| 146 | struct bio *bio; |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 147 | int mirror_num; |
David Sterba | 0f07003 | 2021-07-27 15:11:53 +0200 | [diff] [blame] | 148 | enum btrfs_compression_type compress_type; |
Christoph Hellwig | 7aab8b3 | 2022-04-15 16:33:24 +0200 | [diff] [blame] | 149 | u32 len_to_stripe_boundary; |
| 150 | u32 len_to_oe_boundary; |
| 151 | }; |
| 152 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 153 | struct extent_page_data { |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 154 | struct btrfs_bio_ctrl bio_ctrl; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 155 | /* tells writepage not to lock the state bits for this range |
| 156 | * it still does the unlocking |
| 157 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 158 | unsigned int extent_locked:1; |
| 159 | |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 160 | /* tells the submit_bio code to use REQ_SYNC */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 161 | unsigned int sync_io:1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 162 | }; |
| 163 | |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 164 | static int add_extent_changeset(struct extent_state *state, u32 bits, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 165 | struct extent_changeset *changeset, |
| 166 | int set) |
| 167 | { |
| 168 | int ret; |
| 169 | |
| 170 | if (!changeset) |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 171 | return 0; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 172 | if (set && (state->state & bits) == bits) |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 173 | return 0; |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 174 | if (!set && (state->state & bits) == 0) |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 175 | return 0; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 176 | changeset->bytes_changed += state->end - state->start + 1; |
David Sterba | 53d3235 | 2017-02-13 13:42:29 +0100 | [diff] [blame] | 177 | ret = ulist_add(&changeset->range_changed, state->start, state->end, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 178 | GFP_ATOMIC); |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 179 | return ret; |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 180 | } |
| 181 | |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 182 | static void submit_one_bio(struct btrfs_bio_ctrl *bio_ctrl) |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 183 | { |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 184 | struct bio *bio; |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 185 | struct bio_vec *bv; |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 186 | struct inode *inode; |
| 187 | int mirror_num; |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 188 | |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 189 | if (!bio_ctrl->bio) |
| 190 | return; |
| 191 | |
| 192 | bio = bio_ctrl->bio; |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 193 | bv = bio_first_bvec_all(bio); |
| 194 | inode = bv->bv_page->mapping->host; |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 195 | mirror_num = bio_ctrl->mirror_num; |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 196 | |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 197 | /* Caller should ensure the bio has at least some range added */ |
| 198 | ASSERT(bio->bi_iter.bi_size); |
Qu Wenruo | c9583ad | 2022-04-12 20:30:13 +0800 | [diff] [blame] | 199 | |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 200 | btrfs_bio(bio)->file_offset = page_offset(bv->bv_page) + bv->bv_offset; |
| 201 | |
Christoph Hellwig | c93104e | 2022-05-26 09:36:35 +0200 | [diff] [blame] | 202 | if (!is_data_inode(inode)) |
| 203 | btrfs_submit_metadata_bio(inode, bio, mirror_num); |
| 204 | else if (btrfs_op(bio) == BTRFS_MAP_WRITE) |
| 205 | btrfs_submit_data_write_bio(inode, bio, mirror_num); |
Nikolay Borisov | 908930f | 2020-09-18 16:34:37 +0300 | [diff] [blame] | 206 | else |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 207 | btrfs_submit_data_read_bio(inode, bio, mirror_num, |
| 208 | bio_ctrl->compress_type); |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 209 | |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 210 | /* The bio is owned by the bi_end_io handler now */ |
| 211 | bio_ctrl->bio = NULL; |
Qu Wenruo | 3065976 | 2019-03-20 14:27:42 +0800 | [diff] [blame] | 212 | } |
| 213 | |
Qu Wenruo | f434062 | 2019-03-20 14:27:41 +0800 | [diff] [blame] | 214 | /* |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 215 | * Submit or fail the current bio in an extent_page_data structure. |
Qu Wenruo | f434062 | 2019-03-20 14:27:41 +0800 | [diff] [blame] | 216 | */ |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 217 | static void submit_write_bio(struct extent_page_data *epd, int ret) |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 218 | { |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 219 | struct bio *bio = epd->bio_ctrl.bio; |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 220 | |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 221 | if (!bio) |
| 222 | return; |
| 223 | |
| 224 | if (ret) { |
| 225 | ASSERT(ret < 0); |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 226 | bio->bi_status = errno_to_blk_status(ret); |
| 227 | bio_endio(bio); |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 228 | /* The bio is owned by the bi_end_io handler now */ |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 229 | epd->bio_ctrl.bio = NULL; |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 230 | } else { |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 231 | submit_one_bio(&epd->bio_ctrl); |
Qu Wenruo | bb58eb9 | 2019-01-25 13:09:15 +0800 | [diff] [blame] | 232 | } |
| 233 | } |
David Sterba | e2932ee | 2017-06-23 04:16:17 +0200 | [diff] [blame] | 234 | |
Josef Bacik | 6f0d04f | 2019-09-23 10:05:18 -0400 | [diff] [blame] | 235 | int __init extent_state_cache_init(void) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 236 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 237 | extent_state_cache = kmem_cache_create("btrfs_extent_state", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 238 | sizeof(struct extent_state), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 239 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 240 | if (!extent_state_cache) |
| 241 | return -ENOMEM; |
Josef Bacik | 6f0d04f | 2019-09-23 10:05:18 -0400 | [diff] [blame] | 242 | return 0; |
| 243 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 244 | |
Josef Bacik | 6f0d04f | 2019-09-23 10:05:18 -0400 | [diff] [blame] | 245 | int __init extent_io_init(void) |
| 246 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 247 | extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 248 | sizeof(struct extent_buffer), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 249 | SLAB_MEM_SPREAD, NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 250 | if (!extent_buffer_cache) |
Josef Bacik | 6f0d04f | 2019-09-23 10:05:18 -0400 | [diff] [blame] | 251 | return -ENOMEM; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 252 | |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 253 | if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE, |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 254 | offsetof(struct btrfs_bio, bio), |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 255 | BIOSET_NEED_BVECS)) |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 256 | goto free_buffer_cache; |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 257 | |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 258 | if (bioset_integrity_create(&btrfs_bioset, BIO_POOL_SIZE)) |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 259 | goto free_bioset; |
| 260 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 261 | return 0; |
| 262 | |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 263 | free_bioset: |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 264 | bioset_exit(&btrfs_bioset); |
Darrick J. Wong | b208c2f | 2013-09-19 20:37:07 -0700 | [diff] [blame] | 265 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 266 | free_buffer_cache: |
| 267 | kmem_cache_destroy(extent_buffer_cache); |
| 268 | extent_buffer_cache = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 269 | return -ENOMEM; |
| 270 | } |
| 271 | |
Josef Bacik | 6f0d04f | 2019-09-23 10:05:18 -0400 | [diff] [blame] | 272 | void __cold extent_state_cache_exit(void) |
| 273 | { |
| 274 | btrfs_extent_state_leak_debug_check(); |
| 275 | kmem_cache_destroy(extent_state_cache); |
| 276 | } |
| 277 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 278 | void __cold extent_io_exit(void) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 279 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 280 | /* |
| 281 | * Make sure all delayed rcu free are flushed before we |
| 282 | * destroy caches. |
| 283 | */ |
| 284 | rcu_barrier(); |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 285 | kmem_cache_destroy(extent_buffer_cache); |
Kent Overstreet | 8ac9f7c | 2018-05-20 18:25:56 -0400 | [diff] [blame] | 286 | bioset_exit(&btrfs_bioset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 287 | } |
| 288 | |
Josef Bacik | 41a2ee7 | 2020-01-17 09:02:21 -0500 | [diff] [blame] | 289 | /* |
| 290 | * For the file_extent_tree, we want to hold the inode lock when we lookup and |
| 291 | * update the disk_i_size, but lockdep will complain because our io_tree we hold |
| 292 | * the tree lock and get the inode lock when setting delalloc. These two things |
| 293 | * are unrelated, so make a class for the file_extent_tree so we don't get the |
| 294 | * two locking patterns mixed up. |
| 295 | */ |
| 296 | static struct lock_class_key file_extent_tree_class; |
| 297 | |
Qu Wenruo | c258d6e | 2019-03-01 10:47:58 +0800 | [diff] [blame] | 298 | void extent_io_tree_init(struct btrfs_fs_info *fs_info, |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 299 | struct extent_io_tree *tree, unsigned int owner, |
| 300 | void *private_data) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 301 | { |
Qu Wenruo | c258d6e | 2019-03-01 10:47:58 +0800 | [diff] [blame] | 302 | tree->fs_info = fs_info; |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 303 | tree->state = RB_ROOT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 304 | tree->dirty_bytes = 0; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 305 | spin_lock_init(&tree->lock); |
Josef Bacik | c6100a4 | 2017-05-05 11:57:13 -0400 | [diff] [blame] | 306 | tree->private_data = private_data; |
Qu Wenruo | 43eb5f2 | 2019-03-01 10:47:59 +0800 | [diff] [blame] | 307 | tree->owner = owner; |
Josef Bacik | 41a2ee7 | 2020-01-17 09:02:21 -0500 | [diff] [blame] | 308 | if (owner == IO_TREE_INODE_FILE_EXTENT) |
| 309 | lockdep_set_class(&tree->lock, &file_extent_tree_class); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 310 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 311 | |
Nikolay Borisov | 41e7acd | 2019-03-25 14:31:24 +0200 | [diff] [blame] | 312 | void extent_io_tree_release(struct extent_io_tree *tree) |
| 313 | { |
| 314 | spin_lock(&tree->lock); |
| 315 | /* |
| 316 | * Do a single barrier for the waitqueue_active check here, the state |
| 317 | * of the waitqueue should not change once extent_io_tree_release is |
| 318 | * called. |
| 319 | */ |
| 320 | smp_mb(); |
| 321 | while (!RB_EMPTY_ROOT(&tree->state)) { |
| 322 | struct rb_node *node; |
| 323 | struct extent_state *state; |
| 324 | |
| 325 | node = rb_first(&tree->state); |
| 326 | state = rb_entry(node, struct extent_state, rb_node); |
| 327 | rb_erase(&state->rb_node, &tree->state); |
| 328 | RB_CLEAR_NODE(&state->rb_node); |
| 329 | /* |
| 330 | * btree io trees aren't supposed to have tasks waiting for |
| 331 | * changes in the flags of extent states ever. |
| 332 | */ |
| 333 | ASSERT(!waitqueue_active(&state->wq)); |
| 334 | free_extent_state(state); |
| 335 | |
| 336 | cond_resched_lock(&tree->lock); |
| 337 | } |
| 338 | spin_unlock(&tree->lock); |
| 339 | } |
| 340 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 341 | static struct extent_state *alloc_extent_state(gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 342 | { |
| 343 | struct extent_state *state; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 344 | |
Michal Hocko | 3ba7ab2 | 2017-01-09 15:39:02 +0100 | [diff] [blame] | 345 | /* |
| 346 | * The given mask might be not appropriate for the slab allocator, |
| 347 | * drop the unsupported bits |
| 348 | */ |
| 349 | mask &= ~(__GFP_DMA32|__GFP_HIGHMEM); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 350 | state = kmem_cache_alloc(extent_state_cache, mask); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 351 | if (!state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 352 | return state; |
| 353 | state->state = 0; |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 354 | state->failrec = NULL; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 355 | RB_CLEAR_NODE(&state->rb_node); |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 356 | btrfs_leak_debug_add(&leak_lock, &state->leak_list, &states); |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 357 | refcount_set(&state->refs, 1); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 358 | init_waitqueue_head(&state->wq); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 359 | trace_alloc_extent_state(state, mask, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 360 | return state; |
| 361 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 362 | |
Chris Mason | 4845e44 | 2010-05-25 20:56:50 -0400 | [diff] [blame] | 363 | void free_extent_state(struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 364 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 365 | if (!state) |
| 366 | return; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 367 | if (refcount_dec_and_test(&state->refs)) { |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 368 | WARN_ON(extent_state_in_tree(state)); |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 369 | btrfs_leak_debug_del(&leak_lock, &state->leak_list); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 370 | trace_free_extent_state(state, _RET_IP_); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 371 | kmem_cache_free(extent_state_cache, state); |
| 372 | } |
| 373 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 374 | |
Nikolay Borisov | 8666e63 | 2019-06-05 14:50:04 +0300 | [diff] [blame] | 375 | /** |
Nikolay Borisov | 3bed2da | 2021-01-22 11:58:03 +0200 | [diff] [blame] | 376 | * Search @tree for an entry that contains @offset. Such entry would have |
| 377 | * entry->start <= offset && entry->end >= offset. |
Nikolay Borisov | 8666e63 | 2019-06-05 14:50:04 +0300 | [diff] [blame] | 378 | * |
Nikolay Borisov | 3bed2da | 2021-01-22 11:58:03 +0200 | [diff] [blame] | 379 | * @tree: the tree to search |
| 380 | * @offset: offset that should fall within an entry in @tree |
David Sterba | 9db3389 | 2020-06-25 19:03:41 +0200 | [diff] [blame] | 381 | * @node_ret: pointer where new node should be anchored (used when inserting an |
Nikolay Borisov | 3bed2da | 2021-01-22 11:58:03 +0200 | [diff] [blame] | 382 | * entry in the tree) |
| 383 | * @parent_ret: points to entry which would have been the parent of the entry, |
Nikolay Borisov | 8666e63 | 2019-06-05 14:50:04 +0300 | [diff] [blame] | 384 | * containing @offset |
| 385 | * |
David Sterba | 9db3389 | 2020-06-25 19:03:41 +0200 | [diff] [blame] | 386 | * Return a pointer to the entry that contains @offset byte address and don't change |
| 387 | * @node_ret and @parent_ret. |
| 388 | * |
| 389 | * If no such entry exists, return pointer to entry that ends before @offset |
| 390 | * and fill parameters @node_ret and @parent_ret, ie. does not return NULL. |
Nikolay Borisov | 8666e63 | 2019-06-05 14:50:04 +0300 | [diff] [blame] | 391 | */ |
David Sterba | 9db3389 | 2020-06-25 19:03:41 +0200 | [diff] [blame] | 392 | static inline struct rb_node *tree_search_for_insert(struct extent_io_tree *tree, |
| 393 | u64 offset, |
| 394 | struct rb_node ***node_ret, |
| 395 | struct rb_node **parent_ret) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 396 | { |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 397 | struct rb_root *root = &tree->state; |
David Sterba | bebb22c | 2020-06-25 18:35:24 +0200 | [diff] [blame] | 398 | struct rb_node **node = &root->rb_node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 399 | struct rb_node *prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 400 | struct tree_entry *entry; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 401 | |
David Sterba | bebb22c | 2020-06-25 18:35:24 +0200 | [diff] [blame] | 402 | while (*node) { |
| 403 | prev = *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 404 | entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 405 | |
| 406 | if (offset < entry->start) |
David Sterba | bebb22c | 2020-06-25 18:35:24 +0200 | [diff] [blame] | 407 | node = &(*node)->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 408 | else if (offset > entry->end) |
David Sterba | bebb22c | 2020-06-25 18:35:24 +0200 | [diff] [blame] | 409 | node = &(*node)->rb_right; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 410 | else |
David Sterba | bebb22c | 2020-06-25 18:35:24 +0200 | [diff] [blame] | 411 | return *node; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 412 | } |
| 413 | |
David Sterba | 9db3389 | 2020-06-25 19:03:41 +0200 | [diff] [blame] | 414 | if (node_ret) |
| 415 | *node_ret = node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 416 | if (parent_ret) |
| 417 | *parent_ret = prev; |
| 418 | |
David Sterba | bebb22c | 2020-06-25 18:35:24 +0200 | [diff] [blame] | 419 | /* Search neighbors until we find the first one past the end */ |
| 420 | while (prev && offset > entry->end) { |
| 421 | prev = rb_next(prev); |
| 422 | entry = rb_entry(prev, struct tree_entry, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 423 | } |
| 424 | |
David Sterba | bebb22c | 2020-06-25 18:35:24 +0200 | [diff] [blame] | 425 | return prev; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 426 | } |
| 427 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 428 | /* |
David Sterba | ec60c76 | 2020-06-25 18:49:39 +0200 | [diff] [blame] | 429 | * Inexact rb-tree search, return the next entry if @offset is not found |
| 430 | */ |
| 431 | static inline struct rb_node *tree_search(struct extent_io_tree *tree, u64 offset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 432 | { |
| 433 | return tree_search_for_insert(tree, offset, NULL, NULL); |
| 434 | } |
| 435 | |
David Sterba | 9db3389 | 2020-06-25 19:03:41 +0200 | [diff] [blame] | 436 | /** |
| 437 | * Search offset in the tree or fill neighbor rbtree node pointers. |
| 438 | * |
| 439 | * @tree: the tree to search |
| 440 | * @offset: offset that should fall within an entry in @tree |
| 441 | * @next_ret: pointer to the first entry whose range ends after @offset |
| 442 | * @prev_ret: pointer to the first entry whose range begins before @offset |
| 443 | * |
| 444 | * Return a pointer to the entry that contains @offset byte address. If no |
| 445 | * such entry exists, then return NULL and fill @prev_ret and @next_ret. |
| 446 | * Otherwise return the found entry and other pointers are left untouched. |
| 447 | */ |
| 448 | static struct rb_node *tree_search_prev_next(struct extent_io_tree *tree, |
| 449 | u64 offset, |
| 450 | struct rb_node **prev_ret, |
| 451 | struct rb_node **next_ret) |
| 452 | { |
| 453 | struct rb_root *root = &tree->state; |
| 454 | struct rb_node **node = &root->rb_node; |
| 455 | struct rb_node *prev = NULL; |
| 456 | struct rb_node *orig_prev = NULL; |
| 457 | struct tree_entry *entry; |
| 458 | |
| 459 | ASSERT(prev_ret); |
| 460 | ASSERT(next_ret); |
| 461 | |
| 462 | while (*node) { |
| 463 | prev = *node; |
| 464 | entry = rb_entry(prev, struct tree_entry, rb_node); |
| 465 | |
| 466 | if (offset < entry->start) |
| 467 | node = &(*node)->rb_left; |
| 468 | else if (offset > entry->end) |
| 469 | node = &(*node)->rb_right; |
| 470 | else |
| 471 | return *node; |
| 472 | } |
| 473 | |
| 474 | orig_prev = prev; |
| 475 | while (prev && offset > entry->end) { |
| 476 | prev = rb_next(prev); |
| 477 | entry = rb_entry(prev, struct tree_entry, rb_node); |
| 478 | } |
| 479 | *next_ret = prev; |
| 480 | prev = orig_prev; |
| 481 | |
| 482 | entry = rb_entry(prev, struct tree_entry, rb_node); |
| 483 | while (prev && offset < entry->start) { |
| 484 | prev = rb_prev(prev); |
| 485 | entry = rb_entry(prev, struct tree_entry, rb_node); |
| 486 | } |
| 487 | *prev_ret = prev; |
| 488 | |
| 489 | return NULL; |
| 490 | } |
| 491 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 492 | /* |
| 493 | * utility function to look for merge candidates inside a given range. |
| 494 | * Any extents with matching state are merged together into a single |
| 495 | * extent in the tree. Extents with EXTENT_IO in their state field |
| 496 | * are not merged because the end_io handlers need to be able to do |
| 497 | * operations on them without sleeping (or doing allocations/splits). |
| 498 | * |
| 499 | * This should be called with the tree lock held. |
| 500 | */ |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 501 | static void merge_state(struct extent_io_tree *tree, |
| 502 | struct extent_state *state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 503 | { |
| 504 | struct extent_state *other; |
| 505 | struct rb_node *other_node; |
| 506 | |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 507 | if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY)) |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 508 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 509 | |
| 510 | other_node = rb_prev(&state->rb_node); |
| 511 | if (other_node) { |
| 512 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 513 | if (other->end == state->start - 1 && |
| 514 | other->state == state->state) { |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 515 | if (tree->private_data && |
| 516 | is_data_inode(tree->private_data)) |
| 517 | btrfs_merge_delalloc_extent(tree->private_data, |
| 518 | state, other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 519 | state->start = other->start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 520 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 521 | RB_CLEAR_NODE(&other->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 522 | free_extent_state(other); |
| 523 | } |
| 524 | } |
| 525 | other_node = rb_next(&state->rb_node); |
| 526 | if (other_node) { |
| 527 | other = rb_entry(other_node, struct extent_state, rb_node); |
| 528 | if (other->start == state->end + 1 && |
| 529 | other->state == state->state) { |
Nikolay Borisov | 5c84819 | 2018-11-01 14:09:52 +0200 | [diff] [blame] | 530 | if (tree->private_data && |
| 531 | is_data_inode(tree->private_data)) |
| 532 | btrfs_merge_delalloc_extent(tree->private_data, |
| 533 | state, other); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 534 | state->end = other->end; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 535 | rb_erase(&other->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 536 | RB_CLEAR_NODE(&other->rb_node); |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 537 | free_extent_state(other); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 538 | } |
| 539 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 540 | } |
| 541 | |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 542 | static void set_state_bits(struct extent_io_tree *tree, |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 543 | struct extent_state *state, u32 bits, |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 544 | struct extent_changeset *changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 545 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 546 | /* |
| 547 | * insert an extent_state struct into the tree. 'bits' are set on the |
| 548 | * struct before it is inserted. |
| 549 | * |
| 550 | * This may return -EEXIST if the extent is already there, in which case the |
| 551 | * state struct is freed. |
| 552 | * |
| 553 | * The tree lock is not taken internally. This is a utility function and |
| 554 | * probably isn't what you want to call (see set/clear_extent_bit). |
| 555 | */ |
| 556 | static int insert_state(struct extent_io_tree *tree, |
David Sterba | cee5126 | 2020-06-25 17:18:24 +0200 | [diff] [blame] | 557 | struct extent_state *state, |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 558 | u32 bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 559 | { |
David Sterba | c7e118c | 2020-06-25 17:14:17 +0200 | [diff] [blame] | 560 | struct rb_node **node; |
| 561 | struct rb_node *parent; |
David Sterba | cee5126 | 2020-06-25 17:18:24 +0200 | [diff] [blame] | 562 | const u64 end = state->end; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 563 | |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 564 | set_state_bits(tree, state, bits, changeset); |
Xiao Guangrong | 3150b69 | 2011-07-14 03:19:08 +0000 | [diff] [blame] | 565 | |
David Sterba | c7e118c | 2020-06-25 17:14:17 +0200 | [diff] [blame] | 566 | node = &tree->state.rb_node; |
| 567 | while (*node) { |
| 568 | struct tree_entry *entry; |
| 569 | |
| 570 | parent = *node; |
| 571 | entry = rb_entry(parent, struct tree_entry, rb_node); |
| 572 | |
| 573 | if (end < entry->start) { |
| 574 | node = &(*node)->rb_left; |
| 575 | } else if (end > entry->end) { |
| 576 | node = &(*node)->rb_right; |
| 577 | } else { |
| 578 | btrfs_err(tree->fs_info, |
| 579 | "found node %llu %llu on insert of %llu %llu", |
David Sterba | cee5126 | 2020-06-25 17:18:24 +0200 | [diff] [blame] | 580 | entry->start, entry->end, state->start, end); |
David Sterba | c7e118c | 2020-06-25 17:14:17 +0200 | [diff] [blame] | 581 | return -EEXIST; |
| 582 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 583 | } |
David Sterba | c7e118c | 2020-06-25 17:14:17 +0200 | [diff] [blame] | 584 | |
David Sterba | c7e118c | 2020-06-25 17:14:17 +0200 | [diff] [blame] | 585 | rb_link_node(&state->rb_node, parent, node); |
| 586 | rb_insert_color(&state->rb_node, &tree->state); |
| 587 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 588 | merge_state(tree, state); |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | /* |
David Sterba | fb8f07d | 2020-06-25 18:11:31 +0200 | [diff] [blame] | 593 | * Insert state to @tree to the location given by @node and @parent. |
| 594 | */ |
| 595 | static void insert_state_fast(struct extent_io_tree *tree, |
| 596 | struct extent_state *state, struct rb_node **node, |
| 597 | struct rb_node *parent, unsigned bits, |
| 598 | struct extent_changeset *changeset) |
| 599 | { |
| 600 | set_state_bits(tree, state, bits, changeset); |
| 601 | rb_link_node(&state->rb_node, parent, node); |
| 602 | rb_insert_color(&state->rb_node, &tree->state); |
| 603 | merge_state(tree, state); |
| 604 | } |
| 605 | |
| 606 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 607 | * split a given extent state struct in two, inserting the preallocated |
| 608 | * struct 'prealloc' as the newly created second half. 'split' indicates an |
| 609 | * offset inside 'orig' where it should be split. |
| 610 | * |
| 611 | * Before calling, |
| 612 | * the tree has 'orig' at [orig->start, orig->end]. After calling, there |
| 613 | * are two extent state structs in the tree: |
| 614 | * prealloc: [orig->start, split - 1] |
| 615 | * orig: [ split, orig->end ] |
| 616 | * |
| 617 | * The tree locks are not taken by this function. They need to be held |
| 618 | * by the caller. |
| 619 | */ |
| 620 | static int split_state(struct extent_io_tree *tree, struct extent_state *orig, |
| 621 | struct extent_state *prealloc, u64 split) |
| 622 | { |
David Sterba | 12c9cdd | 2020-06-25 16:49:48 +0200 | [diff] [blame] | 623 | struct rb_node *parent = NULL; |
| 624 | struct rb_node **node; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 625 | |
Nikolay Borisov | abbb55f | 2018-11-01 14:09:53 +0200 | [diff] [blame] | 626 | if (tree->private_data && is_data_inode(tree->private_data)) |
| 627 | btrfs_split_delalloc_extent(tree->private_data, orig, split); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 628 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 629 | prealloc->start = orig->start; |
| 630 | prealloc->end = split - 1; |
| 631 | prealloc->state = orig->state; |
| 632 | orig->start = split; |
| 633 | |
David Sterba | 12c9cdd | 2020-06-25 16:49:48 +0200 | [diff] [blame] | 634 | parent = &orig->rb_node; |
| 635 | node = &parent; |
| 636 | while (*node) { |
| 637 | struct tree_entry *entry; |
| 638 | |
| 639 | parent = *node; |
| 640 | entry = rb_entry(parent, struct tree_entry, rb_node); |
| 641 | |
| 642 | if (prealloc->end < entry->start) { |
| 643 | node = &(*node)->rb_left; |
| 644 | } else if (prealloc->end > entry->end) { |
| 645 | node = &(*node)->rb_right; |
| 646 | } else { |
| 647 | free_extent_state(prealloc); |
| 648 | return -EEXIST; |
| 649 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 650 | } |
David Sterba | 12c9cdd | 2020-06-25 16:49:48 +0200 | [diff] [blame] | 651 | |
| 652 | rb_link_node(&prealloc->rb_node, parent, node); |
| 653 | rb_insert_color(&prealloc->rb_node, &tree->state); |
| 654 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 658 | static struct extent_state *next_state(struct extent_state *state) |
| 659 | { |
| 660 | struct rb_node *next = rb_next(&state->rb_node); |
| 661 | if (next) |
| 662 | return rb_entry(next, struct extent_state, rb_node); |
| 663 | else |
| 664 | return NULL; |
| 665 | } |
| 666 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 667 | /* |
| 668 | * utility function to clear some bits in an extent state struct. |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 669 | * it will optionally wake up anyone waiting on this state (wake == 1). |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 670 | * |
| 671 | * If no bits are set on the state struct after clearing things, the |
| 672 | * struct is freed and removed from the tree |
| 673 | */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 674 | static struct extent_state *clear_state_bit(struct extent_io_tree *tree, |
| 675 | struct extent_state *state, |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 676 | u32 bits, int wake, |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 677 | struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 678 | { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 679 | struct extent_state *next; |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 680 | u32 bits_to_clear = bits & ~EXTENT_CTLBITS; |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 681 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 682 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 683 | if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 684 | u64 range = state->end - state->start + 1; |
| 685 | WARN_ON(range > tree->dirty_bytes); |
| 686 | tree->dirty_bytes -= range; |
| 687 | } |
Nikolay Borisov | a36bb5f | 2018-11-01 14:09:51 +0200 | [diff] [blame] | 688 | |
| 689 | if (tree->private_data && is_data_inode(tree->private_data)) |
| 690 | btrfs_clear_delalloc_extent(tree->private_data, state, bits); |
| 691 | |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 692 | ret = add_extent_changeset(state, bits_to_clear, changeset, 0); |
| 693 | BUG_ON(ret < 0); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 694 | state->state &= ~bits_to_clear; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 695 | if (wake) |
| 696 | wake_up(&state->wq); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 697 | if (state->state == 0) { |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 698 | next = next_state(state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 699 | if (extent_state_in_tree(state)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 700 | rb_erase(&state->rb_node, &tree->state); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 701 | RB_CLEAR_NODE(&state->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 702 | free_extent_state(state); |
| 703 | } else { |
| 704 | WARN_ON(1); |
| 705 | } |
| 706 | } else { |
| 707 | merge_state(tree, state); |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 708 | next = next_state(state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 709 | } |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 710 | return next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 711 | } |
| 712 | |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 713 | static struct extent_state * |
| 714 | alloc_extent_state_atomic(struct extent_state *prealloc) |
| 715 | { |
| 716 | if (!prealloc) |
| 717 | prealloc = alloc_extent_state(GFP_ATOMIC); |
| 718 | |
| 719 | return prealloc; |
| 720 | } |
| 721 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 722 | 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] | 723 | { |
Su Yue | 29b665c | 2021-01-03 17:28:03 +0800 | [diff] [blame] | 724 | btrfs_panic(tree->fs_info, err, |
David Sterba | 05912a3 | 2018-07-18 19:23:45 +0200 | [diff] [blame] | 725 | "locking error: extent tree was modified by another thread while locked"); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 726 | } |
| 727 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 728 | /* |
| 729 | * clear some bits on a range in the tree. This may require splitting |
| 730 | * or inserting elements in the tree, so the gfp mask is used to |
| 731 | * indicate which allocations or sleeping are allowed. |
| 732 | * |
| 733 | * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove |
| 734 | * the given range from the tree regardless of state (ie for truncate). |
| 735 | * |
| 736 | * the range [start, end] is inclusive. |
| 737 | * |
Jeff Mahoney | 6763af8 | 2012-03-01 14:56:29 +0100 | [diff] [blame] | 738 | * 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] | 739 | */ |
David Sterba | 66b0c88 | 2017-10-31 16:30:47 +0100 | [diff] [blame] | 740 | int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 741 | u32 bits, int wake, int delete, |
| 742 | struct extent_state **cached_state, |
| 743 | gfp_t mask, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 744 | { |
| 745 | struct extent_state *state; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 746 | struct extent_state *cached; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 747 | struct extent_state *prealloc = NULL; |
| 748 | struct rb_node *node; |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 749 | u64 last_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 750 | int err; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 751 | int clear = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 752 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 753 | btrfs_debug_check_extent_io_range(tree, start, end); |
Qu Wenruo | a1d1984 | 2019-03-01 10:48:00 +0800 | [diff] [blame] | 754 | trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 755 | |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 756 | if (bits & EXTENT_DELALLOC) |
| 757 | bits |= EXTENT_NORESERVE; |
| 758 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 759 | if (delete) |
| 760 | bits |= ~EXTENT_CTLBITS; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 761 | |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 762 | if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY)) |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 763 | clear = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 764 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 765 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
Filipe Manana | c7bc631 | 2014-11-03 14:12:57 +0000 | [diff] [blame] | 766 | /* |
| 767 | * Don't care for allocation failure here because we might end |
| 768 | * up not needing the pre-allocated extent state at all, which |
| 769 | * is the case if we only have in the tree extent states that |
| 770 | * cover our input range and don't cover too any other range. |
| 771 | * If we end up needing a new extent state we allocate it later. |
| 772 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 773 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 774 | } |
| 775 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 776 | spin_lock(&tree->lock); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 777 | if (cached_state) { |
| 778 | cached = *cached_state; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 779 | |
| 780 | if (clear) { |
| 781 | *cached_state = NULL; |
| 782 | cached_state = NULL; |
| 783 | } |
| 784 | |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 785 | if (cached && extent_state_in_tree(cached) && |
| 786 | cached->start <= start && cached->end > start) { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 787 | if (clear) |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 788 | refcount_dec(&cached->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 789 | state = cached; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 790 | goto hit_next; |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 791 | } |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 792 | if (clear) |
| 793 | free_extent_state(cached); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 794 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 795 | /* |
| 796 | * this search will find the extents that end after |
| 797 | * our range starts |
| 798 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 799 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 800 | if (!node) |
| 801 | goto out; |
| 802 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 803 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 804 | if (state->start > end) |
| 805 | goto out; |
| 806 | WARN_ON(state->end < start); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 807 | last_end = state->end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 808 | |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 809 | /* the state doesn't have the wanted bits, go ahead */ |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 810 | if (!(state->state & bits)) { |
| 811 | state = next_state(state); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 812 | goto next; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 813 | } |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 814 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 815 | /* |
| 816 | * | ---- desired range ---- | |
| 817 | * | state | or |
| 818 | * | ------------- state -------------- | |
| 819 | * |
| 820 | * We need to split the extent we found, and may flip |
| 821 | * bits on second half. |
| 822 | * |
| 823 | * If the extent we found extends past our range, we |
| 824 | * just split and search again. It'll get split again |
| 825 | * the next time though. |
| 826 | * |
| 827 | * If the extent we found is inside our range, we clear |
| 828 | * the desired bit on it. |
| 829 | */ |
| 830 | |
| 831 | if (state->start < start) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 832 | prealloc = alloc_extent_state_atomic(prealloc); |
| 833 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 834 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 835 | if (err) |
| 836 | extent_io_tree_panic(tree, err); |
| 837 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 838 | prealloc = NULL; |
| 839 | if (err) |
| 840 | goto out; |
| 841 | if (state->end <= end) { |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 842 | state = clear_state_bit(tree, state, bits, wake, changeset); |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 843 | goto next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 844 | } |
| 845 | goto search_again; |
| 846 | } |
| 847 | /* |
| 848 | * | ---- desired range ---- | |
| 849 | * | state | |
| 850 | * We need to split the extent, and clear the bit |
| 851 | * on the first half |
| 852 | */ |
| 853 | if (state->start <= end && state->end > end) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 854 | prealloc = alloc_extent_state_atomic(prealloc); |
| 855 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 856 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 857 | if (err) |
| 858 | extent_io_tree_panic(tree, err); |
| 859 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 860 | if (wake) |
| 861 | wake_up(&state->wq); |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 862 | |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 863 | clear_state_bit(tree, prealloc, bits, wake, changeset); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 864 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 865 | prealloc = NULL; |
| 866 | goto out; |
| 867 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 868 | |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 869 | state = clear_state_bit(tree, state, bits, wake, changeset); |
Liu Bo | 0449314 | 2012-02-16 18:34:37 +0800 | [diff] [blame] | 870 | next: |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 871 | if (last_end == (u64)-1) |
| 872 | goto out; |
| 873 | start = last_end + 1; |
Li Zefan | cdc6a39 | 2012-03-12 16:39:48 +0800 | [diff] [blame] | 874 | if (start <= end && state && !need_resched()) |
Liu Bo | 692e575 | 2012-02-16 18:34:36 +0800 | [diff] [blame] | 875 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 876 | |
| 877 | search_again: |
| 878 | if (start > end) |
| 879 | goto out; |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 880 | spin_unlock(&tree->lock); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 881 | if (gfpflags_allow_blocking(mask)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 882 | cond_resched(); |
| 883 | goto again; |
David Sterba | 7ab5cb2 | 2016-04-27 01:02:15 +0200 | [diff] [blame] | 884 | |
| 885 | out: |
| 886 | spin_unlock(&tree->lock); |
| 887 | if (prealloc) |
| 888 | free_extent_state(prealloc); |
| 889 | |
| 890 | return 0; |
| 891 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 892 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 893 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 894 | static void wait_on_state(struct extent_io_tree *tree, |
| 895 | struct extent_state *state) |
Christoph Hellwig | 641f521 | 2008-12-02 06:36:10 -0500 | [diff] [blame] | 896 | __releases(tree->lock) |
| 897 | __acquires(tree->lock) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 898 | { |
| 899 | DEFINE_WAIT(wait); |
| 900 | prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 901 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 902 | schedule(); |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 903 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 904 | finish_wait(&state->wq, &wait); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | /* |
| 908 | * waits for one or more bits to clear on a range in the state tree. |
| 909 | * The range [start, end] is inclusive. |
| 910 | * The tree lock is taken by this function |
| 911 | */ |
David Sterba | 4107488 | 2013-04-29 13:38:46 +0000 | [diff] [blame] | 912 | static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 913 | u32 bits) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 914 | { |
| 915 | struct extent_state *state; |
| 916 | struct rb_node *node; |
| 917 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 918 | btrfs_debug_check_extent_io_range(tree, start, end); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 919 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 920 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 921 | again: |
| 922 | while (1) { |
| 923 | /* |
| 924 | * this search will find all the extents that end after |
| 925 | * our range starts |
| 926 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 927 | node = tree_search(tree, start); |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 928 | process_node: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 929 | if (!node) |
| 930 | break; |
| 931 | |
| 932 | state = rb_entry(node, struct extent_state, rb_node); |
| 933 | |
| 934 | if (state->start > end) |
| 935 | goto out; |
| 936 | |
| 937 | if (state->state & bits) { |
| 938 | start = state->start; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 939 | refcount_inc(&state->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 940 | wait_on_state(tree, state); |
| 941 | free_extent_state(state); |
| 942 | goto again; |
| 943 | } |
| 944 | start = state->end + 1; |
| 945 | |
| 946 | if (start > end) |
| 947 | break; |
| 948 | |
Filipe Manana | c50d3e7 | 2014-03-31 14:53:25 +0100 | [diff] [blame] | 949 | if (!cond_resched_lock(&tree->lock)) { |
| 950 | node = rb_next(node); |
| 951 | goto process_node; |
| 952 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 953 | } |
| 954 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 955 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 956 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 957 | |
Jeff Mahoney | 1bf8504 | 2011-07-21 16:56:09 +0000 | [diff] [blame] | 958 | static void set_state_bits(struct extent_io_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 959 | struct extent_state *state, |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 960 | u32 bits, struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 961 | { |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 962 | u32 bits_to_set = bits & ~EXTENT_CTLBITS; |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 963 | int ret; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 964 | |
Nikolay Borisov | e06a1fc | 2018-11-01 14:09:50 +0200 | [diff] [blame] | 965 | if (tree->private_data && is_data_inode(tree->private_data)) |
| 966 | btrfs_set_delalloc_extent(tree->private_data, state, bits); |
| 967 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 968 | if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 969 | u64 range = state->end - state->start + 1; |
| 970 | tree->dirty_bytes += range; |
| 971 | } |
David Sterba | 57599c7 | 2018-03-01 17:56:34 +0100 | [diff] [blame] | 972 | ret = add_extent_changeset(state, bits_to_set, changeset, 1); |
| 973 | BUG_ON(ret < 0); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 974 | state->state |= bits_to_set; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 975 | } |
| 976 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 977 | static void cache_state_if_flags(struct extent_state *state, |
| 978 | struct extent_state **cached_ptr, |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 979 | unsigned flags) |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 980 | { |
| 981 | if (cached_ptr && !(*cached_ptr)) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 982 | if (!flags || (state->state & flags)) { |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 983 | *cached_ptr = state; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 984 | refcount_inc(&state->refs); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | } |
| 988 | |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 989 | static void cache_state(struct extent_state *state, |
| 990 | struct extent_state **cached_ptr) |
| 991 | { |
| 992 | return cache_state_if_flags(state, cached_ptr, |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 993 | EXTENT_LOCKED | EXTENT_BOUNDARY); |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 994 | } |
| 995 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 996 | /* |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 997 | * set some bits on a range in the tree. This may require allocations or |
| 998 | * sleeping, so the gfp mask is used to indicate what is allowed. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 999 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1000 | * If any of the exclusive bits are set, this will fail with -EEXIST if some |
| 1001 | * part of the range already has the desired bits set. The start of the |
| 1002 | * existing range is returned in failed_start in this case. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1003 | * |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1004 | * [start, end] is inclusive This takes the tree lock. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1005 | */ |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 1006 | int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits, |
| 1007 | u32 exclusive_bits, u64 *failed_start, |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 1008 | struct extent_state **cached_state, gfp_t mask, |
| 1009 | struct extent_changeset *changeset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1010 | { |
| 1011 | struct extent_state *state; |
| 1012 | struct extent_state *prealloc = NULL; |
| 1013 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1014 | struct rb_node **p; |
| 1015 | struct rb_node *parent; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1016 | int err = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1017 | u64 last_start; |
| 1018 | u64 last_end; |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 1019 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 1020 | btrfs_debug_check_extent_io_range(tree, start, end); |
Qu Wenruo | a1d1984 | 2019-03-01 10:48:00 +0800 | [diff] [blame] | 1021 | trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 1022 | |
Qu Wenruo | 3f6bb4a | 2020-10-21 14:24:51 +0800 | [diff] [blame] | 1023 | if (exclusive_bits) |
| 1024 | ASSERT(failed_start); |
| 1025 | else |
| 1026 | ASSERT(failed_start == NULL); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1027 | again: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 1028 | if (!prealloc && gfpflags_allow_blocking(mask)) { |
David Sterba | 059f791 | 2016-04-27 01:03:45 +0200 | [diff] [blame] | 1029 | /* |
| 1030 | * Don't care for allocation failure here because we might end |
| 1031 | * up not needing the pre-allocated extent state at all, which |
| 1032 | * is the case if we only have in the tree extent states that |
| 1033 | * cover our input range and don't cover too any other range. |
| 1034 | * If we end up needing a new extent state we allocate it later. |
| 1035 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1036 | prealloc = alloc_extent_state(mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1037 | } |
| 1038 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1039 | spin_lock(&tree->lock); |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1040 | if (cached_state && *cached_state) { |
| 1041 | state = *cached_state; |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 1042 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1043 | extent_state_in_tree(state)) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 1044 | node = &state->rb_node; |
| 1045 | goto hit_next; |
| 1046 | } |
| 1047 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1048 | /* |
| 1049 | * this search will find all the extents that end after |
| 1050 | * our range starts. |
| 1051 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1052 | node = tree_search_for_insert(tree, start, &p, &parent); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1053 | if (!node) { |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1054 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1055 | BUG_ON(!prealloc); |
David Sterba | cee5126 | 2020-06-25 17:18:24 +0200 | [diff] [blame] | 1056 | prealloc->start = start; |
| 1057 | prealloc->end = end; |
David Sterba | fb8f07d | 2020-06-25 18:11:31 +0200 | [diff] [blame] | 1058 | insert_state_fast(tree, prealloc, p, parent, bits, changeset); |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 1059 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1060 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1061 | goto out; |
| 1062 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1063 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | 40431d6 | 2009-08-05 12:57:59 -0400 | [diff] [blame] | 1064 | hit_next: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1065 | last_start = state->start; |
| 1066 | last_end = state->end; |
| 1067 | |
| 1068 | /* |
| 1069 | * | ---- desired range ---- | |
| 1070 | * | state | |
| 1071 | * |
| 1072 | * Just lock what we found and keep going |
| 1073 | */ |
| 1074 | if (state->start == start && state->end <= end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1075 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1076 | *failed_start = state->start; |
| 1077 | err = -EEXIST; |
| 1078 | goto out; |
| 1079 | } |
Chris Mason | 42daec2 | 2009-09-23 19:51:09 -0400 | [diff] [blame] | 1080 | |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 1081 | set_state_bits(tree, state, bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1082 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1083 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 1084 | if (last_end == (u64)-1) |
| 1085 | goto out; |
| 1086 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1087 | state = next_state(state); |
| 1088 | if (start < end && state && state->start == start && |
| 1089 | !need_resched()) |
| 1090 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1091 | goto search_again; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * | ---- desired range ---- | |
| 1096 | * | state | |
| 1097 | * or |
| 1098 | * | ------------- state -------------- | |
| 1099 | * |
| 1100 | * We need to split the extent we found, and may flip bits on |
| 1101 | * second half. |
| 1102 | * |
| 1103 | * If the extent we found extends past our |
| 1104 | * range, we just split and search again. It'll get split |
| 1105 | * again the next time though. |
| 1106 | * |
| 1107 | * If the extent we found is inside our range, we set the |
| 1108 | * desired bit on it. |
| 1109 | */ |
| 1110 | if (state->start < start) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1111 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1112 | *failed_start = start; |
| 1113 | err = -EEXIST; |
| 1114 | goto out; |
| 1115 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1116 | |
Filipe Manana | 55ffaab | 2020-02-13 10:20:02 +0000 | [diff] [blame] | 1117 | /* |
| 1118 | * If this extent already has all the bits we want set, then |
| 1119 | * skip it, not necessary to split it or do anything with it. |
| 1120 | */ |
| 1121 | if ((state->state & bits) == bits) { |
| 1122 | start = state->end + 1; |
| 1123 | cache_state(state, cached_state); |
| 1124 | goto search_again; |
| 1125 | } |
| 1126 | |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1127 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1128 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1129 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1130 | if (err) |
| 1131 | extent_io_tree_panic(tree, err); |
| 1132 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1133 | prealloc = NULL; |
| 1134 | if (err) |
| 1135 | goto out; |
| 1136 | if (state->end <= end) { |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 1137 | set_state_bits(tree, state, bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1138 | cache_state(state, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1139 | merge_state(tree, state); |
Yan Zheng | 5c939df | 2009-05-27 09:16:03 -0400 | [diff] [blame] | 1140 | if (last_end == (u64)-1) |
| 1141 | goto out; |
| 1142 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1143 | state = next_state(state); |
| 1144 | if (start < end && state && state->start == start && |
| 1145 | !need_resched()) |
| 1146 | goto hit_next; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1147 | } |
| 1148 | goto search_again; |
| 1149 | } |
| 1150 | /* |
| 1151 | * | ---- desired range ---- | |
| 1152 | * | state | or | state | |
| 1153 | * |
| 1154 | * There's a hole, we need to insert something in it and |
| 1155 | * ignore the extent we found. |
| 1156 | */ |
| 1157 | if (state->start > start) { |
| 1158 | u64 this_end; |
| 1159 | if (end < last_start) |
| 1160 | this_end = end; |
| 1161 | else |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1162 | this_end = last_start - 1; |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1163 | |
| 1164 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1165 | BUG_ON(!prealloc); |
Xiao Guangrong | c7f895a | 2011-04-20 06:45:49 +0000 | [diff] [blame] | 1166 | |
| 1167 | /* |
| 1168 | * Avoid to free 'prealloc' if it can be merged with |
| 1169 | * the later extent. |
| 1170 | */ |
David Sterba | cee5126 | 2020-06-25 17:18:24 +0200 | [diff] [blame] | 1171 | prealloc->start = start; |
| 1172 | prealloc->end = this_end; |
David Sterba | c367602 | 2020-06-25 18:15:31 +0200 | [diff] [blame] | 1173 | err = insert_state(tree, prealloc, bits, changeset); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1174 | if (err) |
| 1175 | extent_io_tree_panic(tree, err); |
| 1176 | |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1177 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1178 | prealloc = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1179 | start = this_end + 1; |
| 1180 | goto search_again; |
| 1181 | } |
| 1182 | /* |
| 1183 | * | ---- desired range ---- | |
| 1184 | * | state | |
| 1185 | * We need to split the extent, and set the bit |
| 1186 | * on the first half |
| 1187 | */ |
| 1188 | if (state->start <= end && state->end > end) { |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1189 | if (state->state & exclusive_bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1190 | *failed_start = start; |
| 1191 | err = -EEXIST; |
| 1192 | goto out; |
| 1193 | } |
Xiao Guangrong | 8233767 | 2011-04-20 06:44:57 +0000 | [diff] [blame] | 1194 | |
| 1195 | prealloc = alloc_extent_state_atomic(prealloc); |
| 1196 | BUG_ON(!prealloc); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1197 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1198 | if (err) |
| 1199 | extent_io_tree_panic(tree, err); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1200 | |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 1201 | set_state_bits(tree, prealloc, bits, changeset); |
Chris Mason | 2c64c53 | 2009-09-02 15:04:12 -0400 | [diff] [blame] | 1202 | cache_state(prealloc, cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1203 | merge_state(tree, prealloc); |
| 1204 | prealloc = NULL; |
| 1205 | goto out; |
| 1206 | } |
| 1207 | |
David Sterba | b5a4ba14 | 2016-04-27 01:02:15 +0200 | [diff] [blame] | 1208 | search_again: |
| 1209 | if (start > end) |
| 1210 | goto out; |
| 1211 | spin_unlock(&tree->lock); |
| 1212 | if (gfpflags_allow_blocking(mask)) |
| 1213 | cond_resched(); |
| 1214 | goto again; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1215 | |
| 1216 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1217 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1218 | if (prealloc) |
| 1219 | free_extent_state(prealloc); |
| 1220 | |
| 1221 | return err; |
| 1222 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1223 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1224 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1225 | /** |
Liu Bo | 10983f2 | 2012-07-11 15:26:19 +0800 | [diff] [blame] | 1226 | * convert_extent_bit - convert all bits in a given range from one bit to |
| 1227 | * another |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1228 | * @tree: the io tree to search |
| 1229 | * @start: the start offset in bytes |
| 1230 | * @end: the end offset in bytes (inclusive) |
| 1231 | * @bits: the bits to set in this range |
| 1232 | * @clear_bits: the bits to clear in this range |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1233 | * @cached_state: state that we're going to cache |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1234 | * |
| 1235 | * This will go through and set bits for the given range. If any states exist |
| 1236 | * already in this range they are set with the given bit and cleared of the |
| 1237 | * clear_bits. This is only meant to be used by things that are mergeable, ie |
| 1238 | * converting from say DELALLOC to DIRTY. This is not meant to be used with |
| 1239 | * boundary bits like LOCK. |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1240 | * |
| 1241 | * All allocations are done with GFP_NOFS. |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1242 | */ |
| 1243 | int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 1244 | u32 bits, u32 clear_bits, |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1245 | struct extent_state **cached_state) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1246 | { |
| 1247 | struct extent_state *state; |
| 1248 | struct extent_state *prealloc = NULL; |
| 1249 | struct rb_node *node; |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1250 | struct rb_node **p; |
| 1251 | struct rb_node *parent; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1252 | int err = 0; |
| 1253 | u64 last_start; |
| 1254 | u64 last_end; |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1255 | bool first_iteration = true; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1256 | |
Josef Bacik | a5dee37 | 2013-12-13 10:02:44 -0500 | [diff] [blame] | 1257 | btrfs_debug_check_extent_io_range(tree, start, end); |
Qu Wenruo | a1d1984 | 2019-03-01 10:48:00 +0800 | [diff] [blame] | 1258 | trace_btrfs_convert_extent_bit(tree, start, end - start + 1, bits, |
| 1259 | clear_bits); |
David Sterba | 8d599ae | 2013-04-30 15:22:23 +0000 | [diff] [blame] | 1260 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1261 | again: |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1262 | if (!prealloc) { |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1263 | /* |
| 1264 | * Best effort, don't worry if extent state allocation fails |
| 1265 | * here for the first iteration. We might have a cached state |
| 1266 | * that matches exactly the target range, in which case no |
| 1267 | * extent state allocations are needed. We'll only know this |
| 1268 | * after locking the tree. |
| 1269 | */ |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1270 | prealloc = alloc_extent_state(GFP_NOFS); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1271 | if (!prealloc && !first_iteration) |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1272 | return -ENOMEM; |
| 1273 | } |
| 1274 | |
| 1275 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1276 | if (cached_state && *cached_state) { |
| 1277 | state = *cached_state; |
| 1278 | if (state->start <= start && state->end > start && |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1279 | extent_state_in_tree(state)) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1280 | node = &state->rb_node; |
| 1281 | goto hit_next; |
| 1282 | } |
| 1283 | } |
| 1284 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1285 | /* |
| 1286 | * this search will find all the extents that end after |
| 1287 | * our range starts. |
| 1288 | */ |
Filipe David Borba Manana | 12cfbad | 2013-11-26 15:41:47 +0000 | [diff] [blame] | 1289 | node = tree_search_for_insert(tree, start, &p, &parent); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1290 | if (!node) { |
| 1291 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1292 | if (!prealloc) { |
| 1293 | err = -ENOMEM; |
| 1294 | goto out; |
| 1295 | } |
David Sterba | cee5126 | 2020-06-25 17:18:24 +0200 | [diff] [blame] | 1296 | prealloc->start = start; |
| 1297 | prealloc->end = end; |
David Sterba | fb8f07d | 2020-06-25 18:11:31 +0200 | [diff] [blame] | 1298 | insert_state_fast(tree, prealloc, p, parent, bits, NULL); |
Filipe David Borba Manana | c42ac0b | 2013-11-26 15:01:34 +0000 | [diff] [blame] | 1299 | cache_state(prealloc, cached_state); |
| 1300 | prealloc = NULL; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1301 | goto out; |
| 1302 | } |
| 1303 | state = rb_entry(node, struct extent_state, rb_node); |
| 1304 | hit_next: |
| 1305 | last_start = state->start; |
| 1306 | last_end = state->end; |
| 1307 | |
| 1308 | /* |
| 1309 | * | ---- desired range ---- | |
| 1310 | * | state | |
| 1311 | * |
| 1312 | * Just lock what we found and keep going |
| 1313 | */ |
| 1314 | if (state->start == start && state->end <= end) { |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 1315 | set_state_bits(tree, state, bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1316 | cache_state(state, cached_state); |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 1317 | state = clear_state_bit(tree, state, clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1318 | if (last_end == (u64)-1) |
| 1319 | goto out; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1320 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1321 | if (start < end && state && state->start == start && |
| 1322 | !need_resched()) |
| 1323 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1324 | goto search_again; |
| 1325 | } |
| 1326 | |
| 1327 | /* |
| 1328 | * | ---- desired range ---- | |
| 1329 | * | state | |
| 1330 | * or |
| 1331 | * | ------------- state -------------- | |
| 1332 | * |
| 1333 | * We need to split the extent we found, and may flip bits on |
| 1334 | * second half. |
| 1335 | * |
| 1336 | * If the extent we found extends past our |
| 1337 | * range, we just split and search again. It'll get split |
| 1338 | * again the next time though. |
| 1339 | * |
| 1340 | * If the extent we found is inside our range, we set the |
| 1341 | * desired bit on it. |
| 1342 | */ |
| 1343 | if (state->start < start) { |
| 1344 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1345 | if (!prealloc) { |
| 1346 | err = -ENOMEM; |
| 1347 | goto out; |
| 1348 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1349 | err = split_state(tree, state, prealloc, start); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1350 | if (err) |
| 1351 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1352 | prealloc = NULL; |
| 1353 | if (err) |
| 1354 | goto out; |
| 1355 | if (state->end <= end) { |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 1356 | set_state_bits(tree, state, bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1357 | cache_state(state, cached_state); |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 1358 | state = clear_state_bit(tree, state, clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1359 | if (last_end == (u64)-1) |
| 1360 | goto out; |
| 1361 | start = last_end + 1; |
Liu Bo | d1ac6e4 | 2012-05-10 18:10:39 +0800 | [diff] [blame] | 1362 | if (start < end && state && state->start == start && |
| 1363 | !need_resched()) |
| 1364 | goto hit_next; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1365 | } |
| 1366 | goto search_again; |
| 1367 | } |
| 1368 | /* |
| 1369 | * | ---- desired range ---- | |
| 1370 | * | state | or | state | |
| 1371 | * |
| 1372 | * There's a hole, we need to insert something in it and |
| 1373 | * ignore the extent we found. |
| 1374 | */ |
| 1375 | if (state->start > start) { |
| 1376 | u64 this_end; |
| 1377 | if (end < last_start) |
| 1378 | this_end = end; |
| 1379 | else |
| 1380 | this_end = last_start - 1; |
| 1381 | |
| 1382 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1383 | if (!prealloc) { |
| 1384 | err = -ENOMEM; |
| 1385 | goto out; |
| 1386 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1387 | |
| 1388 | /* |
| 1389 | * Avoid to free 'prealloc' if it can be merged with |
| 1390 | * the later extent. |
| 1391 | */ |
David Sterba | cee5126 | 2020-06-25 17:18:24 +0200 | [diff] [blame] | 1392 | prealloc->start = start; |
| 1393 | prealloc->end = this_end; |
David Sterba | c367602 | 2020-06-25 18:15:31 +0200 | [diff] [blame] | 1394 | err = insert_state(tree, prealloc, bits, NULL); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1395 | if (err) |
| 1396 | extent_io_tree_panic(tree, err); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1397 | cache_state(prealloc, cached_state); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1398 | prealloc = NULL; |
| 1399 | start = this_end + 1; |
| 1400 | goto search_again; |
| 1401 | } |
| 1402 | /* |
| 1403 | * | ---- desired range ---- | |
| 1404 | * | state | |
| 1405 | * We need to split the extent, and set the bit |
| 1406 | * on the first half |
| 1407 | */ |
| 1408 | if (state->start <= end && state->end > end) { |
| 1409 | prealloc = alloc_extent_state_atomic(prealloc); |
Liu Bo | 1cf4ffd | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 1410 | if (!prealloc) { |
| 1411 | err = -ENOMEM; |
| 1412 | goto out; |
| 1413 | } |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1414 | |
| 1415 | err = split_state(tree, state, prealloc, end + 1); |
Jeff Mahoney | c2d904e | 2011-10-03 23:22:32 -0400 | [diff] [blame] | 1416 | if (err) |
| 1417 | extent_io_tree_panic(tree, err); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1418 | |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 1419 | set_state_bits(tree, prealloc, bits, NULL); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1420 | cache_state(prealloc, cached_state); |
David Sterba | 6d92b30 | 2020-06-25 17:54:54 +0200 | [diff] [blame] | 1421 | clear_state_bit(tree, prealloc, clear_bits, 0, NULL); |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1422 | prealloc = NULL; |
| 1423 | goto out; |
| 1424 | } |
| 1425 | |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1426 | search_again: |
| 1427 | if (start > end) |
| 1428 | goto out; |
| 1429 | spin_unlock(&tree->lock); |
David Sterba | 210aa27 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1430 | cond_resched(); |
Filipe Manana | c8fd3de | 2014-10-13 12:28:39 +0100 | [diff] [blame] | 1431 | first_iteration = false; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1432 | goto again; |
Josef Bacik | 462d6fa | 2011-09-26 13:56:12 -0400 | [diff] [blame] | 1433 | |
| 1434 | out: |
| 1435 | spin_unlock(&tree->lock); |
| 1436 | if (prealloc) |
| 1437 | free_extent_state(prealloc); |
| 1438 | |
| 1439 | return err; |
| 1440 | } |
| 1441 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1442 | /* wrappers around set/clear extent bit */ |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1443 | int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 1444 | u32 bits, struct extent_changeset *changeset) |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1445 | { |
| 1446 | /* |
| 1447 | * We don't support EXTENT_LOCKED yet, as current changeset will |
| 1448 | * record any bits changed, so for EXTENT_LOCKED case, it will |
| 1449 | * either fail with -EEXIST or changeset will record the whole |
| 1450 | * range. |
| 1451 | */ |
| 1452 | BUG_ON(bits & EXTENT_LOCKED); |
| 1453 | |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 1454 | return set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS, |
| 1455 | changeset); |
Qu Wenruo | d38ed27 | 2015-10-12 14:53:37 +0800 | [diff] [blame] | 1456 | } |
| 1457 | |
Nikolay Borisov | 4ca7365 | 2019-03-27 14:24:10 +0200 | [diff] [blame] | 1458 | int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end, |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 1459 | u32 bits) |
Nikolay Borisov | 4ca7365 | 2019-03-27 14:24:10 +0200 | [diff] [blame] | 1460 | { |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 1461 | return set_extent_bit(tree, start, end, bits, 0, NULL, NULL, |
| 1462 | GFP_NOWAIT, NULL); |
Nikolay Borisov | 4ca7365 | 2019-03-27 14:24:10 +0200 | [diff] [blame] | 1463 | } |
| 1464 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1465 | int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 1466 | u32 bits, int wake, int delete, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1467 | struct extent_state **cached) |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1468 | { |
| 1469 | return __clear_extent_bit(tree, start, end, bits, wake, delete, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1470 | cached, GFP_NOFS, NULL); |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1471 | } |
| 1472 | |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1473 | int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 1474 | u32 bits, struct extent_changeset *changeset) |
Qu Wenruo | fefdc55 | 2015-10-12 15:35:38 +0800 | [diff] [blame] | 1475 | { |
| 1476 | /* |
| 1477 | * Don't support EXTENT_LOCKED case, same reason as |
| 1478 | * set_record_extent_bits(). |
| 1479 | */ |
| 1480 | BUG_ON(bits & EXTENT_LOCKED); |
| 1481 | |
David Sterba | f734c44 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 1482 | 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] | 1483 | changeset); |
| 1484 | } |
| 1485 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1486 | /* |
| 1487 | * either insert or lock state struct between start and end use mask to tell |
| 1488 | * us if waiting is desired. |
| 1489 | */ |
Chris Mason | 1edbb73 | 2009-09-02 13:24:36 -0400 | [diff] [blame] | 1490 | 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] | 1491 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1492 | { |
| 1493 | int err; |
| 1494 | u64 failed_start; |
David Sterba | 9ee49a04 | 2015-01-14 19:52:13 +0100 | [diff] [blame] | 1495 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1496 | while (1) { |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 1497 | err = set_extent_bit(tree, start, end, EXTENT_LOCKED, |
| 1498 | EXTENT_LOCKED, &failed_start, |
| 1499 | cached_state, GFP_NOFS, NULL); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1500 | if (err == -EEXIST) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1501 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
| 1502 | start = failed_start; |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1503 | } else |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1504 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1505 | WARN_ON(start > end); |
| 1506 | } |
| 1507 | return err; |
| 1508 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1509 | |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 1510 | int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end) |
Josef Bacik | 25179201 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1511 | { |
| 1512 | int err; |
| 1513 | u64 failed_start; |
| 1514 | |
Nikolay Borisov | 1cab5e7 | 2020-11-05 11:08:00 +0200 | [diff] [blame] | 1515 | err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, |
| 1516 | &failed_start, NULL, GFP_NOFS, NULL); |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1517 | if (err == -EEXIST) { |
| 1518 | if (failed_start > start) |
| 1519 | clear_extent_bit(tree, start, failed_start - 1, |
David Sterba | ae0f162 | 2017-10-31 16:37:52 +0100 | [diff] [blame] | 1520 | EXTENT_LOCKED, 1, 0, NULL); |
Josef Bacik | 25179201 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1521 | return 0; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 1522 | } |
Josef Bacik | 25179201 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1523 | return 1; |
| 1524 | } |
Josef Bacik | 25179201 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1525 | |
David Sterba | bd1fa4f | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1526 | 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] | 1527 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1528 | unsigned long index = start >> PAGE_SHIFT; |
| 1529 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1530 | struct page *page; |
| 1531 | |
| 1532 | while (index <= end_index) { |
| 1533 | page = find_get_page(inode->i_mapping, index); |
| 1534 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
| 1535 | clear_page_dirty_for_io(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1536 | put_page(page); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1537 | index++; |
| 1538 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1539 | } |
| 1540 | |
David Sterba | f631157 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 1541 | 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] | 1542 | { |
Matthew Wilcox (Oracle) | ebf55c8 | 2022-02-09 20:22:04 +0000 | [diff] [blame] | 1543 | struct address_space *mapping = inode->i_mapping; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1544 | unsigned long index = start >> PAGE_SHIFT; |
| 1545 | unsigned long end_index = end >> PAGE_SHIFT; |
Matthew Wilcox (Oracle) | ebf55c8 | 2022-02-09 20:22:04 +0000 | [diff] [blame] | 1546 | struct folio *folio; |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1547 | |
| 1548 | while (index <= end_index) { |
Matthew Wilcox (Oracle) | ebf55c8 | 2022-02-09 20:22:04 +0000 | [diff] [blame] | 1549 | folio = filemap_get_folio(mapping, index); |
| 1550 | filemap_dirty_folio(mapping, folio); |
| 1551 | folio_account_redirty(folio); |
| 1552 | index += folio_nr_pages(folio); |
| 1553 | folio_put(folio); |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1554 | } |
Chris Mason | 4adaa61 | 2013-03-26 13:07:00 -0400 | [diff] [blame] | 1555 | } |
| 1556 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1557 | /* find the first state struct with 'bits' set after 'start', and |
| 1558 | * return it. tree->lock must be held. NULL will returned if |
| 1559 | * nothing was found after 'start' |
| 1560 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1561 | static struct extent_state * |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 1562 | find_first_extent_bit_state(struct extent_io_tree *tree, u64 start, u32 bits) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1563 | { |
| 1564 | struct rb_node *node; |
| 1565 | struct extent_state *state; |
| 1566 | |
| 1567 | /* |
| 1568 | * this search will find all the extents that end after |
| 1569 | * our range starts. |
| 1570 | */ |
| 1571 | node = tree_search(tree, start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1572 | if (!node) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1573 | goto out; |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1574 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1575 | while (1) { |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1576 | state = rb_entry(node, struct extent_state, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1577 | if (state->end >= start && (state->state & bits)) |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1578 | return state; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1579 | |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1580 | node = rb_next(node); |
| 1581 | if (!node) |
| 1582 | break; |
| 1583 | } |
| 1584 | out: |
| 1585 | return NULL; |
| 1586 | } |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 1587 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1588 | /* |
Qu Wenruo | 03509b7 | 2020-10-21 14:24:50 +0800 | [diff] [blame] | 1589 | * Find the first offset in the io tree with one or more @bits set. |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1590 | * |
Qu Wenruo | 03509b7 | 2020-10-21 14:24:50 +0800 | [diff] [blame] | 1591 | * Note: If there are multiple bits set in @bits, any of them will match. |
| 1592 | * |
| 1593 | * Return 0 if we find something, and update @start_ret and @end_ret. |
| 1594 | * Return 1 if we found nothing. |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1595 | */ |
| 1596 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 1597 | u64 *start_ret, u64 *end_ret, u32 bits, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1598 | struct extent_state **cached_state) |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1599 | { |
| 1600 | struct extent_state *state; |
| 1601 | int ret = 1; |
| 1602 | |
| 1603 | spin_lock(&tree->lock); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1604 | if (cached_state && *cached_state) { |
| 1605 | state = *cached_state; |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 1606 | if (state->end == start - 1 && extent_state_in_tree(state)) { |
Liu Bo | 9688e9a | 2018-08-23 03:14:53 +0800 | [diff] [blame] | 1607 | while ((state = next_state(state)) != NULL) { |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1608 | if (state->state & bits) |
| 1609 | goto got_it; |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1610 | } |
| 1611 | free_extent_state(*cached_state); |
| 1612 | *cached_state = NULL; |
| 1613 | goto out; |
| 1614 | } |
| 1615 | free_extent_state(*cached_state); |
| 1616 | *cached_state = NULL; |
| 1617 | } |
| 1618 | |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1619 | state = find_first_extent_bit_state(tree, start, bits); |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1620 | got_it: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1621 | if (state) { |
Filipe Manana | e38e2ed | 2014-10-13 12:28:38 +0100 | [diff] [blame] | 1622 | cache_state_if_flags(state, cached_state, 0); |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1623 | *start_ret = state->start; |
| 1624 | *end_ret = state->end; |
| 1625 | ret = 0; |
| 1626 | } |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 1627 | out: |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1628 | spin_unlock(&tree->lock); |
| 1629 | return ret; |
| 1630 | } |
| 1631 | |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1632 | /** |
Nikolay Borisov | 3bed2da | 2021-01-22 11:58:03 +0200 | [diff] [blame] | 1633 | * Find a contiguous area of bits |
| 1634 | * |
| 1635 | * @tree: io tree to check |
| 1636 | * @start: offset to start the search from |
| 1637 | * @start_ret: the first offset we found with the bits set |
| 1638 | * @end_ret: the final contiguous range of the bits that were set |
| 1639 | * @bits: bits to look for |
Josef Bacik | 41a2ee7 | 2020-01-17 09:02:21 -0500 | [diff] [blame] | 1640 | * |
| 1641 | * set_extent_bit and clear_extent_bit can temporarily split contiguous ranges |
| 1642 | * to set bits appropriately, and then merge them again. During this time it |
| 1643 | * will drop the tree->lock, so use this helper if you want to find the actual |
| 1644 | * contiguous area for given bits. We will search to the first bit we find, and |
| 1645 | * then walk down the tree until we find a non-contiguous area. The area |
| 1646 | * returned will be the full contiguous area with the bits set. |
| 1647 | */ |
| 1648 | int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start, |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 1649 | u64 *start_ret, u64 *end_ret, u32 bits) |
Josef Bacik | 41a2ee7 | 2020-01-17 09:02:21 -0500 | [diff] [blame] | 1650 | { |
| 1651 | struct extent_state *state; |
| 1652 | int ret = 1; |
| 1653 | |
| 1654 | spin_lock(&tree->lock); |
| 1655 | state = find_first_extent_bit_state(tree, start, bits); |
| 1656 | if (state) { |
| 1657 | *start_ret = state->start; |
| 1658 | *end_ret = state->end; |
| 1659 | while ((state = next_state(state)) != NULL) { |
| 1660 | if (state->start > (*end_ret + 1)) |
| 1661 | break; |
| 1662 | *end_ret = state->end; |
| 1663 | } |
| 1664 | ret = 0; |
| 1665 | } |
| 1666 | spin_unlock(&tree->lock); |
| 1667 | return ret; |
| 1668 | } |
| 1669 | |
| 1670 | /** |
Nikolay Borisov | 3bed2da | 2021-01-22 11:58:03 +0200 | [diff] [blame] | 1671 | * Find the first range that has @bits not set. This range could start before |
| 1672 | * @start. |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1673 | * |
Nikolay Borisov | 3bed2da | 2021-01-22 11:58:03 +0200 | [diff] [blame] | 1674 | * @tree: the tree to search |
| 1675 | * @start: offset at/after which the found extent should start |
| 1676 | * @start_ret: records the beginning of the range |
| 1677 | * @end_ret: records the end of the range (inclusive) |
| 1678 | * @bits: the set of bits which must be unset |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1679 | * |
| 1680 | * Since unallocated range is also considered one which doesn't have the bits |
| 1681 | * set it's possible that @end_ret contains -1, this happens in case the range |
| 1682 | * spans (last_range_end, end of device]. In this case it's up to the caller to |
| 1683 | * trim @end_ret to the appropriate size. |
| 1684 | */ |
| 1685 | void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start, |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 1686 | u64 *start_ret, u64 *end_ret, u32 bits) |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1687 | { |
| 1688 | struct extent_state *state; |
| 1689 | struct rb_node *node, *prev = NULL, *next; |
| 1690 | |
| 1691 | spin_lock(&tree->lock); |
| 1692 | |
| 1693 | /* Find first extent with bits cleared */ |
| 1694 | while (1) { |
David Sterba | 9db3389 | 2020-06-25 19:03:41 +0200 | [diff] [blame] | 1695 | node = tree_search_prev_next(tree, start, &prev, &next); |
Nikolay Borisov | 5750c37 | 2020-01-27 11:59:26 +0200 | [diff] [blame] | 1696 | if (!node && !next && !prev) { |
| 1697 | /* |
| 1698 | * Tree is completely empty, send full range and let |
| 1699 | * caller deal with it |
| 1700 | */ |
| 1701 | *start_ret = 0; |
| 1702 | *end_ret = -1; |
| 1703 | goto out; |
| 1704 | } else if (!node && !next) { |
| 1705 | /* |
| 1706 | * We are past the last allocated chunk, set start at |
| 1707 | * the end of the last extent. |
| 1708 | */ |
| 1709 | state = rb_entry(prev, struct extent_state, rb_node); |
| 1710 | *start_ret = state->end + 1; |
| 1711 | *end_ret = -1; |
| 1712 | goto out; |
| 1713 | } else if (!node) { |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1714 | node = next; |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1715 | } |
Nikolay Borisov | 1eaebb3 | 2019-06-03 13:06:02 +0300 | [diff] [blame] | 1716 | /* |
| 1717 | * At this point 'node' either contains 'start' or start is |
| 1718 | * before 'node' |
| 1719 | */ |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1720 | state = rb_entry(node, struct extent_state, rb_node); |
Nikolay Borisov | 1eaebb3 | 2019-06-03 13:06:02 +0300 | [diff] [blame] | 1721 | |
| 1722 | if (in_range(start, state->start, state->end - state->start + 1)) { |
| 1723 | if (state->state & bits) { |
| 1724 | /* |
| 1725 | * |--range with bits sets--| |
| 1726 | * | |
| 1727 | * start |
| 1728 | */ |
| 1729 | start = state->end + 1; |
| 1730 | } else { |
| 1731 | /* |
| 1732 | * 'start' falls within a range that doesn't |
| 1733 | * have the bits set, so take its start as |
| 1734 | * the beginning of the desired range |
| 1735 | * |
| 1736 | * |--range with bits cleared----| |
| 1737 | * | |
| 1738 | * start |
| 1739 | */ |
| 1740 | *start_ret = state->start; |
| 1741 | break; |
| 1742 | } |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1743 | } else { |
Nikolay Borisov | 1eaebb3 | 2019-06-03 13:06:02 +0300 | [diff] [blame] | 1744 | /* |
| 1745 | * |---prev range---|---hole/unset---|---node range---| |
| 1746 | * | |
| 1747 | * start |
| 1748 | * |
| 1749 | * or |
| 1750 | * |
| 1751 | * |---hole/unset--||--first node--| |
| 1752 | * 0 | |
| 1753 | * start |
| 1754 | */ |
| 1755 | if (prev) { |
| 1756 | state = rb_entry(prev, struct extent_state, |
| 1757 | rb_node); |
| 1758 | *start_ret = state->end + 1; |
| 1759 | } else { |
| 1760 | *start_ret = 0; |
| 1761 | } |
Nikolay Borisov | 45bfcfc | 2019-03-27 14:24:17 +0200 | [diff] [blame] | 1762 | break; |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | /* |
| 1767 | * Find the longest stretch from start until an entry which has the |
| 1768 | * bits set |
| 1769 | */ |
| 1770 | while (1) { |
| 1771 | state = rb_entry(node, struct extent_state, rb_node); |
| 1772 | if (state->end >= start && !(state->state & bits)) { |
| 1773 | *end_ret = state->end; |
| 1774 | } else { |
| 1775 | *end_ret = state->start - 1; |
| 1776 | break; |
| 1777 | } |
| 1778 | |
| 1779 | node = rb_next(node); |
| 1780 | if (!node) |
| 1781 | break; |
| 1782 | } |
| 1783 | out: |
| 1784 | spin_unlock(&tree->lock); |
| 1785 | } |
| 1786 | |
Xiao Guangrong | 69261c4 | 2011-07-14 03:19:45 +0000 | [diff] [blame] | 1787 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1788 | * find a contiguous range of bytes in the file marked as delalloc, not |
| 1789 | * more than 'max_bytes'. start and end are used to return the range, |
| 1790 | * |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1791 | * 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] | 1792 | */ |
Josef Bacik | 083e75e | 2019-09-23 10:05:20 -0400 | [diff] [blame] | 1793 | bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start, |
| 1794 | u64 *end, u64 max_bytes, |
| 1795 | struct extent_state **cached_state) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1796 | { |
| 1797 | struct rb_node *node; |
| 1798 | struct extent_state *state; |
| 1799 | u64 cur_start = *start; |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1800 | bool found = false; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1801 | u64 total_bytes = 0; |
| 1802 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1803 | spin_lock(&tree->lock); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1804 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1805 | /* |
| 1806 | * this search will find all the extents that end after |
| 1807 | * our range starts. |
| 1808 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1809 | node = tree_search(tree, cur_start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 1810 | if (!node) { |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1811 | *end = (u64)-1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1812 | goto out; |
| 1813 | } |
| 1814 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1815 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1816 | state = rb_entry(node, struct extent_state, rb_node); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 1817 | if (found && (state->start != cur_start || |
| 1818 | (state->state & EXTENT_BOUNDARY))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1819 | goto out; |
| 1820 | } |
| 1821 | if (!(state->state & EXTENT_DELALLOC)) { |
| 1822 | if (!found) |
| 1823 | *end = state->end; |
| 1824 | goto out; |
| 1825 | } |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1826 | if (!found) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1827 | *start = state->start; |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1828 | *cached_state = state; |
Elena Reshetova | b7ac31b | 2017-03-03 10:55:19 +0200 | [diff] [blame] | 1829 | refcount_inc(&state->refs); |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 1830 | } |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 1831 | found = true; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1832 | *end = state->end; |
| 1833 | cur_start = state->end + 1; |
| 1834 | node = rb_next(node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1835 | total_bytes += state->end - state->start + 1; |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 1836 | if (total_bytes >= max_bytes) |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1837 | break; |
Josef Bacik | 573aeca | 2013-08-30 14:38:49 -0400 | [diff] [blame] | 1838 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1839 | break; |
| 1840 | } |
| 1841 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 1842 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1843 | return found; |
| 1844 | } |
| 1845 | |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1846 | /* |
| 1847 | * Process one page for __process_pages_contig(). |
| 1848 | * |
| 1849 | * Return >0 if we hit @page == @locked_page. |
| 1850 | * Return 0 if we updated the page status. |
| 1851 | * Return -EGAIN if the we need to try again. |
| 1852 | * (For PAGE_LOCK case but got dirty page or page not belong to mapping) |
| 1853 | */ |
Qu Wenruo | e38992b | 2021-05-31 16:50:42 +0800 | [diff] [blame] | 1854 | static int process_one_page(struct btrfs_fs_info *fs_info, |
| 1855 | struct address_space *mapping, |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1856 | struct page *page, struct page *locked_page, |
Qu Wenruo | e38992b | 2021-05-31 16:50:42 +0800 | [diff] [blame] | 1857 | unsigned long page_ops, u64 start, u64 end) |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1858 | { |
Qu Wenruo | e38992b | 2021-05-31 16:50:42 +0800 | [diff] [blame] | 1859 | u32 len; |
| 1860 | |
| 1861 | ASSERT(end + 1 - start != 0 && end + 1 - start < U32_MAX); |
| 1862 | len = end + 1 - start; |
| 1863 | |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1864 | if (page_ops & PAGE_SET_ORDERED) |
Qu Wenruo | b945a46 | 2021-05-31 16:50:46 +0800 | [diff] [blame] | 1865 | btrfs_page_clamp_set_ordered(fs_info, page, start, len); |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1866 | if (page_ops & PAGE_SET_ERROR) |
Qu Wenruo | e38992b | 2021-05-31 16:50:42 +0800 | [diff] [blame] | 1867 | btrfs_page_clamp_set_error(fs_info, page, start, len); |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1868 | if (page_ops & PAGE_START_WRITEBACK) { |
Qu Wenruo | e38992b | 2021-05-31 16:50:42 +0800 | [diff] [blame] | 1869 | btrfs_page_clamp_clear_dirty(fs_info, page, start, len); |
| 1870 | btrfs_page_clamp_set_writeback(fs_info, page, start, len); |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1871 | } |
| 1872 | if (page_ops & PAGE_END_WRITEBACK) |
Qu Wenruo | e38992b | 2021-05-31 16:50:42 +0800 | [diff] [blame] | 1873 | btrfs_page_clamp_clear_writeback(fs_info, page, start, len); |
Qu Wenruo | a33a8e9 | 2021-05-31 16:50:47 +0800 | [diff] [blame] | 1874 | |
| 1875 | if (page == locked_page) |
| 1876 | return 1; |
| 1877 | |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1878 | if (page_ops & PAGE_LOCK) { |
Qu Wenruo | 1e1de38 | 2021-05-31 16:50:44 +0800 | [diff] [blame] | 1879 | int ret; |
| 1880 | |
| 1881 | ret = btrfs_page_start_writer_lock(fs_info, page, start, len); |
| 1882 | if (ret) |
| 1883 | return ret; |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1884 | if (!PageDirty(page) || page->mapping != mapping) { |
Qu Wenruo | 1e1de38 | 2021-05-31 16:50:44 +0800 | [diff] [blame] | 1885 | btrfs_page_end_writer_lock(fs_info, page, start, len); |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1886 | return -EAGAIN; |
| 1887 | } |
| 1888 | } |
| 1889 | if (page_ops & PAGE_UNLOCK) |
Qu Wenruo | 1e1de38 | 2021-05-31 16:50:44 +0800 | [diff] [blame] | 1890 | btrfs_page_end_writer_lock(fs_info, page, start, len); |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1891 | return 0; |
| 1892 | } |
| 1893 | |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1894 | static int __process_pages_contig(struct address_space *mapping, |
| 1895 | struct page *locked_page, |
Qu Wenruo | 98af9ab | 2021-05-31 16:50:37 +0800 | [diff] [blame] | 1896 | u64 start, u64 end, unsigned long page_ops, |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1897 | u64 *processed_end) |
| 1898 | { |
Qu Wenruo | e38992b | 2021-05-31 16:50:42 +0800 | [diff] [blame] | 1899 | struct btrfs_fs_info *fs_info = btrfs_sb(mapping->host->i_sb); |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1900 | pgoff_t start_index = start >> PAGE_SHIFT; |
| 1901 | pgoff_t end_index = end >> PAGE_SHIFT; |
| 1902 | pgoff_t index = start_index; |
| 1903 | unsigned long nr_pages = end_index - start_index + 1; |
| 1904 | unsigned long pages_processed = 0; |
| 1905 | struct page *pages[16]; |
| 1906 | int err = 0; |
| 1907 | int i; |
| 1908 | |
| 1909 | if (page_ops & PAGE_LOCK) { |
| 1910 | ASSERT(page_ops == PAGE_LOCK); |
| 1911 | ASSERT(processed_end && *processed_end == start); |
| 1912 | } |
| 1913 | |
| 1914 | if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0) |
| 1915 | mapping_set_error(mapping, -EIO); |
| 1916 | |
| 1917 | while (nr_pages > 0) { |
| 1918 | int found_pages; |
| 1919 | |
| 1920 | found_pages = find_get_pages_contig(mapping, index, |
| 1921 | min_t(unsigned long, |
| 1922 | nr_pages, ARRAY_SIZE(pages)), pages); |
| 1923 | if (found_pages == 0) { |
| 1924 | /* |
| 1925 | * Only if we're going to lock these pages, we can find |
| 1926 | * nothing at @index. |
| 1927 | */ |
| 1928 | ASSERT(page_ops & PAGE_LOCK); |
| 1929 | err = -EAGAIN; |
| 1930 | goto out; |
| 1931 | } |
| 1932 | |
| 1933 | for (i = 0; i < found_pages; i++) { |
| 1934 | int process_ret; |
| 1935 | |
Qu Wenruo | e38992b | 2021-05-31 16:50:42 +0800 | [diff] [blame] | 1936 | process_ret = process_one_page(fs_info, mapping, |
| 1937 | pages[i], locked_page, page_ops, |
| 1938 | start, end); |
Qu Wenruo | ed8f13b | 2021-05-31 16:50:38 +0800 | [diff] [blame] | 1939 | if (process_ret < 0) { |
| 1940 | for (; i < found_pages; i++) |
| 1941 | put_page(pages[i]); |
| 1942 | err = -EAGAIN; |
| 1943 | goto out; |
| 1944 | } |
| 1945 | put_page(pages[i]); |
| 1946 | pages_processed++; |
| 1947 | } |
| 1948 | nr_pages -= found_pages; |
| 1949 | index += found_pages; |
| 1950 | cond_resched(); |
| 1951 | } |
| 1952 | out: |
| 1953 | if (err && processed_end) { |
| 1954 | /* |
| 1955 | * Update @processed_end. I know this is awful since it has |
| 1956 | * two different return value patterns (inclusive vs exclusive). |
| 1957 | * |
| 1958 | * But the exclusive pattern is necessary if @start is 0, or we |
| 1959 | * underflow and check against processed_end won't work as |
| 1960 | * expected. |
| 1961 | */ |
| 1962 | if (pages_processed) |
| 1963 | *processed_end = min(end, |
| 1964 | ((u64)(start_index + pages_processed) << PAGE_SHIFT) - 1); |
| 1965 | else |
| 1966 | *processed_end = start; |
| 1967 | } |
| 1968 | return err; |
| 1969 | } |
Liu Bo | da2c700 | 2017-02-10 16:41:05 +0100 | [diff] [blame] | 1970 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1971 | static noinline void __unlock_for_delalloc(struct inode *inode, |
| 1972 | struct page *locked_page, |
| 1973 | u64 start, u64 end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1974 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1975 | unsigned long index = start >> PAGE_SHIFT; |
| 1976 | unsigned long end_index = end >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1977 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1978 | ASSERT(locked_page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1979 | if (index == locked_page->index && end_index == index) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1980 | return; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1981 | |
Qu Wenruo | 98af9ab | 2021-05-31 16:50:37 +0800 | [diff] [blame] | 1982 | __process_pages_contig(inode->i_mapping, locked_page, start, end, |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1983 | PAGE_UNLOCK, NULL); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | static noinline int lock_delalloc_pages(struct inode *inode, |
| 1987 | struct page *locked_page, |
| 1988 | u64 delalloc_start, |
| 1989 | u64 delalloc_end) |
| 1990 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1991 | unsigned long index = delalloc_start >> PAGE_SHIFT; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1992 | unsigned long end_index = delalloc_end >> PAGE_SHIFT; |
Qu Wenruo | 98af9ab | 2021-05-31 16:50:37 +0800 | [diff] [blame] | 1993 | u64 processed_end = delalloc_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1994 | int ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1995 | |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 1996 | ASSERT(locked_page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1997 | if (index == locked_page->index && index == end_index) |
| 1998 | return 0; |
| 1999 | |
Qu Wenruo | 98af9ab | 2021-05-31 16:50:37 +0800 | [diff] [blame] | 2000 | ret = __process_pages_contig(inode->i_mapping, locked_page, delalloc_start, |
| 2001 | delalloc_end, PAGE_LOCK, &processed_end); |
| 2002 | if (ret == -EAGAIN && processed_end > delalloc_start) |
Liu Bo | 76c0021 | 2017-02-10 16:42:14 +0100 | [diff] [blame] | 2003 | __unlock_for_delalloc(inode, locked_page, delalloc_start, |
Qu Wenruo | 98af9ab | 2021-05-31 16:50:37 +0800 | [diff] [blame] | 2004 | processed_end); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2005 | return ret; |
| 2006 | } |
| 2007 | |
| 2008 | /* |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 2009 | * Find and lock a contiguous range of bytes in the file marked as delalloc, no |
Qu Wenruo | 2749f7e | 2021-09-27 15:22:07 +0800 | [diff] [blame] | 2010 | * more than @max_bytes. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2011 | * |
Qu Wenruo | 2749f7e | 2021-09-27 15:22:07 +0800 | [diff] [blame] | 2012 | * @start: The original start bytenr to search. |
| 2013 | * Will store the extent range start bytenr. |
| 2014 | * @end: The original end bytenr of the search range |
| 2015 | * Will store the extent range end bytenr. |
| 2016 | * |
| 2017 | * Return true if we find a delalloc range which starts inside the original |
| 2018 | * range, and @start/@end will store the delalloc range start/end. |
| 2019 | * |
| 2020 | * Return false if we can't find any delalloc range which starts inside the |
| 2021 | * original range, and @start/@end will be the non-delalloc range start/end. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2022 | */ |
Johannes Thumshirn | ce9f967 | 2018-11-19 10:38:17 +0100 | [diff] [blame] | 2023 | EXPORT_FOR_TESTS |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 2024 | noinline_for_stack bool find_lock_delalloc_range(struct inode *inode, |
Josef Bacik | 294e30f | 2013-10-09 12:00:56 -0400 | [diff] [blame] | 2025 | struct page *locked_page, u64 *start, |
Nikolay Borisov | 917aace | 2018-10-26 14:43:20 +0300 | [diff] [blame] | 2026 | u64 *end) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2027 | { |
Naohiro Aota | f7b12a6 | 2022-07-09 08:18:40 +0900 | [diff] [blame] | 2028 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Goldwyn Rodrigues | 9978059 | 2019-06-21 10:02:54 -0500 | [diff] [blame] | 2029 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Qu Wenruo | 2749f7e | 2021-09-27 15:22:07 +0800 | [diff] [blame] | 2030 | const u64 orig_start = *start; |
| 2031 | const u64 orig_end = *end; |
Naohiro Aota | f7b12a6 | 2022-07-09 08:18:40 +0900 | [diff] [blame] | 2032 | /* The sanity tests may not set a valid fs_info. */ |
| 2033 | u64 max_bytes = fs_info ? fs_info->max_extent_size : BTRFS_MAX_EXTENT_SIZE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2034 | u64 delalloc_start; |
| 2035 | u64 delalloc_end; |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 2036 | bool found; |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2037 | struct extent_state *cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2038 | int ret; |
| 2039 | int loops = 0; |
| 2040 | |
Qu Wenruo | 2749f7e | 2021-09-27 15:22:07 +0800 | [diff] [blame] | 2041 | /* Caller should pass a valid @end to indicate the search range end */ |
| 2042 | ASSERT(orig_end > orig_start); |
| 2043 | |
| 2044 | /* The range should at least cover part of the page */ |
| 2045 | ASSERT(!(orig_start >= page_offset(locked_page) + PAGE_SIZE || |
| 2046 | orig_end <= page_offset(locked_page))); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2047 | again: |
| 2048 | /* step one, find a bunch of delalloc bytes starting at start */ |
| 2049 | delalloc_start = *start; |
| 2050 | delalloc_end = 0; |
Josef Bacik | 083e75e | 2019-09-23 10:05:20 -0400 | [diff] [blame] | 2051 | found = btrfs_find_delalloc_range(tree, &delalloc_start, &delalloc_end, |
| 2052 | max_bytes, &cached_state); |
Qu Wenruo | 2749f7e | 2021-09-27 15:22:07 +0800 | [diff] [blame] | 2053 | if (!found || delalloc_end <= *start || delalloc_start > orig_end) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2054 | *start = delalloc_start; |
Qu Wenruo | 2749f7e | 2021-09-27 15:22:07 +0800 | [diff] [blame] | 2055 | |
| 2056 | /* @delalloc_end can be -1, never go beyond @orig_end */ |
| 2057 | *end = min(delalloc_end, orig_end); |
Josef Bacik | c2a128d | 2010-02-02 21:19:11 +0000 | [diff] [blame] | 2058 | free_extent_state(cached_state); |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 2059 | return false; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | /* |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 2063 | * start comes from the offset of locked_page. We have to lock |
| 2064 | * pages in order, so we can't process delalloc bytes before |
| 2065 | * locked_page |
| 2066 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2067 | if (delalloc_start < *start) |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 2068 | delalloc_start = *start; |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 2069 | |
| 2070 | /* |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2071 | * 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] | 2072 | */ |
Josef Bacik | 7bf811a5 | 2013-10-07 22:11:09 -0400 | [diff] [blame] | 2073 | if (delalloc_end + 1 - delalloc_start > max_bytes) |
| 2074 | delalloc_end = delalloc_start + max_bytes - 1; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2075 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2076 | /* step two, lock all the pages after the page that has start */ |
| 2077 | ret = lock_delalloc_pages(inode, locked_page, |
| 2078 | delalloc_start, delalloc_end); |
Nikolay Borisov | 9bfd61d | 2018-10-26 14:43:21 +0300 | [diff] [blame] | 2079 | ASSERT(!ret || ret == -EAGAIN); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2080 | if (ret == -EAGAIN) { |
| 2081 | /* some of the pages are gone, lets avoid looping by |
| 2082 | * shortening the size of the delalloc range we're searching |
| 2083 | */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2084 | free_extent_state(cached_state); |
Chris Mason | 7d78874 | 2014-05-21 05:49:54 -0700 | [diff] [blame] | 2085 | cached_state = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2086 | if (!loops) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2087 | max_bytes = PAGE_SIZE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2088 | loops = 1; |
| 2089 | goto again; |
| 2090 | } else { |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 2091 | found = false; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2092 | goto out_failed; |
| 2093 | } |
| 2094 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2095 | |
| 2096 | /* step three, lock the state bits for the whole range */ |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 2097 | lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2098 | |
| 2099 | /* then test to make sure it is all still delalloc */ |
| 2100 | ret = test_range_bit(tree, delalloc_start, delalloc_end, |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2101 | EXTENT_DELALLOC, 1, cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2102 | if (!ret) { |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2103 | unlock_extent_cached(tree, delalloc_start, delalloc_end, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 2104 | &cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2105 | __unlock_for_delalloc(inode, locked_page, |
| 2106 | delalloc_start, delalloc_end); |
| 2107 | cond_resched(); |
| 2108 | goto again; |
| 2109 | } |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2110 | free_extent_state(cached_state); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2111 | *start = delalloc_start; |
| 2112 | *end = delalloc_end; |
| 2113 | out_failed: |
| 2114 | return found; |
| 2115 | } |
| 2116 | |
Nikolay Borisov | ad7ff17 | 2020-06-03 08:55:06 +0300 | [diff] [blame] | 2117 | void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end, |
Nikolay Borisov | 74e9194 | 2019-07-17 16:18:16 +0300 | [diff] [blame] | 2118 | struct page *locked_page, |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 2119 | u32 clear_bits, unsigned long page_ops) |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 2120 | { |
Nikolay Borisov | ad7ff17 | 2020-06-03 08:55:06 +0300 | [diff] [blame] | 2121 | clear_extent_bit(&inode->io_tree, start, end, clear_bits, 1, 0, NULL); |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 2122 | |
Nikolay Borisov | ad7ff17 | 2020-06-03 08:55:06 +0300 | [diff] [blame] | 2123 | __process_pages_contig(inode->vfs_inode.i_mapping, locked_page, |
Qu Wenruo | 98af9ab | 2021-05-31 16:50:37 +0800 | [diff] [blame] | 2124 | start, end, page_ops, NULL); |
Liu Bo | 873695b | 2017-02-02 17:49:22 -0800 | [diff] [blame] | 2125 | } |
| 2126 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2127 | /* |
| 2128 | * count the number of bytes in the tree that have a given bit(s) |
| 2129 | * set. This can be fairly slow, except for EXTENT_DIRTY which is |
| 2130 | * cached. The total number found is returned. |
| 2131 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2132 | u64 count_range_bits(struct extent_io_tree *tree, |
| 2133 | u64 *start, u64 search_end, u64 max_bytes, |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 2134 | u32 bits, int contig) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2135 | { |
| 2136 | struct rb_node *node; |
| 2137 | struct extent_state *state; |
| 2138 | u64 cur_start = *start; |
| 2139 | u64 total_bytes = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 2140 | u64 last = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2141 | int found = 0; |
| 2142 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2143 | if (WARN_ON(search_end <= cur_start)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2144 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2145 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2146 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2147 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
| 2148 | total_bytes = tree->dirty_bytes; |
| 2149 | goto out; |
| 2150 | } |
| 2151 | /* |
| 2152 | * this search will find all the extents that end after |
| 2153 | * our range starts. |
| 2154 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2155 | node = tree_search(tree, cur_start); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2156 | if (!node) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2157 | goto out; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2158 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2159 | while (1) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2160 | state = rb_entry(node, struct extent_state, rb_node); |
| 2161 | if (state->start > search_end) |
| 2162 | break; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 2163 | if (contig && found && state->start > last + 1) |
| 2164 | break; |
| 2165 | if (state->end >= cur_start && (state->state & bits) == bits) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2166 | total_bytes += min(search_end, state->end) + 1 - |
| 2167 | max(cur_start, state->start); |
| 2168 | if (total_bytes >= max_bytes) |
| 2169 | break; |
| 2170 | if (!found) { |
Josef Bacik | af60bed | 2011-05-04 11:11:17 -0400 | [diff] [blame] | 2171 | *start = max(cur_start, state->start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2172 | found = 1; |
| 2173 | } |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 2174 | last = state->end; |
| 2175 | } else if (contig && found) { |
| 2176 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2177 | } |
| 2178 | node = rb_next(node); |
| 2179 | if (!node) |
| 2180 | break; |
| 2181 | } |
| 2182 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2183 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2184 | return total_bytes; |
| 2185 | } |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2186 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2187 | /* |
| 2188 | * set the private field for a given byte offset in the tree. If there isn't |
| 2189 | * an extent_state there already, this does nothing. |
| 2190 | */ |
Josef Bacik | b3f167a | 2019-09-23 10:05:21 -0400 | [diff] [blame] | 2191 | int set_state_failrec(struct extent_io_tree *tree, u64 start, |
| 2192 | struct io_failure_record *failrec) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2193 | { |
| 2194 | struct rb_node *node; |
| 2195 | struct extent_state *state; |
| 2196 | int ret = 0; |
| 2197 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2198 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2199 | /* |
| 2200 | * this search will find all the extents that end after |
| 2201 | * our range starts. |
| 2202 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2203 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 2204 | if (!node) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2205 | ret = -ENOENT; |
| 2206 | goto out; |
| 2207 | } |
| 2208 | state = rb_entry(node, struct extent_state, rb_node); |
| 2209 | if (state->start != start) { |
| 2210 | ret = -ENOENT; |
| 2211 | goto out; |
| 2212 | } |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2213 | state->failrec = failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2214 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2215 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2216 | return ret; |
| 2217 | } |
| 2218 | |
Nikolay Borisov | 2279a27 | 2020-07-02 15:23:28 +0300 | [diff] [blame] | 2219 | struct io_failure_record *get_state_failrec(struct extent_io_tree *tree, u64 start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2220 | { |
| 2221 | struct rb_node *node; |
| 2222 | struct extent_state *state; |
Nikolay Borisov | 2279a27 | 2020-07-02 15:23:28 +0300 | [diff] [blame] | 2223 | struct io_failure_record *failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2224 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2225 | spin_lock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2226 | /* |
| 2227 | * this search will find all the extents that end after |
| 2228 | * our range starts. |
| 2229 | */ |
Chris Mason | 80ea96b | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2230 | node = tree_search(tree, start); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 2231 | if (!node) { |
Nikolay Borisov | 2279a27 | 2020-07-02 15:23:28 +0300 | [diff] [blame] | 2232 | failrec = ERR_PTR(-ENOENT); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2233 | goto out; |
| 2234 | } |
| 2235 | state = rb_entry(node, struct extent_state, rb_node); |
| 2236 | if (state->start != start) { |
Nikolay Borisov | 2279a27 | 2020-07-02 15:23:28 +0300 | [diff] [blame] | 2237 | failrec = ERR_PTR(-ENOENT); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2238 | goto out; |
| 2239 | } |
Nikolay Borisov | 2279a27 | 2020-07-02 15:23:28 +0300 | [diff] [blame] | 2240 | |
| 2241 | failrec = state->failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2242 | out: |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2243 | spin_unlock(&tree->lock); |
Nikolay Borisov | 2279a27 | 2020-07-02 15:23:28 +0300 | [diff] [blame] | 2244 | return failrec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | /* |
| 2248 | * searches a range in the state tree for a given mask. |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 2249 | * 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] | 2250 | * has the bits set. Otherwise, 1 is returned if any bit in the |
| 2251 | * range is found set. |
| 2252 | */ |
| 2253 | int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, |
Qu Wenruo | f97e27e | 2020-11-13 20:51:40 +0800 | [diff] [blame] | 2254 | u32 bits, int filled, struct extent_state *cached) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2255 | { |
| 2256 | struct extent_state *state = NULL; |
| 2257 | struct rb_node *node; |
| 2258 | int bitset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2259 | |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2260 | spin_lock(&tree->lock); |
Filipe Manana | 27a3507 | 2014-07-06 20:09:59 +0100 | [diff] [blame] | 2261 | if (cached && extent_state_in_tree(cached) && cached->start <= start && |
Josef Bacik | df98b6e | 2011-06-20 14:53:48 -0400 | [diff] [blame] | 2262 | cached->end > start) |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 2263 | node = &cached->rb_node; |
| 2264 | else |
| 2265 | node = tree_search(tree, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2266 | while (node && start <= end) { |
| 2267 | state = rb_entry(node, struct extent_state, rb_node); |
| 2268 | |
| 2269 | if (filled && state->start > start) { |
| 2270 | bitset = 0; |
| 2271 | break; |
| 2272 | } |
| 2273 | |
| 2274 | if (state->start > end) |
| 2275 | break; |
| 2276 | |
| 2277 | if (state->state & bits) { |
| 2278 | bitset = 1; |
| 2279 | if (!filled) |
| 2280 | break; |
| 2281 | } else if (filled) { |
| 2282 | bitset = 0; |
| 2283 | break; |
| 2284 | } |
Chris Mason | 46562ce | 2009-09-23 20:23:16 -0400 | [diff] [blame] | 2285 | |
| 2286 | if (state->end == (u64)-1) |
| 2287 | break; |
| 2288 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2289 | start = state->end + 1; |
| 2290 | if (start > end) |
| 2291 | break; |
| 2292 | node = rb_next(node); |
| 2293 | if (!node) { |
| 2294 | if (filled) |
| 2295 | bitset = 0; |
| 2296 | break; |
| 2297 | } |
| 2298 | } |
Chris Mason | cad321a | 2008-12-17 14:51:42 -0500 | [diff] [blame] | 2299 | spin_unlock(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2300 | return bitset; |
| 2301 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2302 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2303 | int free_io_failure(struct extent_io_tree *failure_tree, |
| 2304 | struct extent_io_tree *io_tree, |
| 2305 | struct io_failure_record *rec) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2306 | { |
| 2307 | int ret; |
| 2308 | int err = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2309 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2310 | set_state_failrec(failure_tree, rec->start, NULL); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2311 | ret = clear_extent_bits(failure_tree, rec->start, |
| 2312 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2313 | EXTENT_LOCKED | EXTENT_DIRTY); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2314 | if (ret) |
| 2315 | err = ret; |
| 2316 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2317 | ret = clear_extent_bits(io_tree, rec->start, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2318 | rec->start + rec->len - 1, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 2319 | EXTENT_DAMAGED); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2320 | if (ret && !err) |
| 2321 | err = ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2322 | |
| 2323 | kfree(rec); |
| 2324 | return err; |
| 2325 | } |
| 2326 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2327 | /* |
| 2328 | * this bypasses the standard btrfs submit functions deliberately, as |
| 2329 | * the standard behavior is to write all copies in a raid setup. here we only |
| 2330 | * want to write the one bad copy. so we do the mapping for ourselves and issue |
| 2331 | * submit_bio directly. |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2332 | * to avoid any synchronization issues, wait for the data after writing, which |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2333 | * actually prevents the read that triggered the error from finishing. |
| 2334 | * currently, there can be no more than two copies of every data bit. thus, |
| 2335 | * exactly one rewrite is required. |
| 2336 | */ |
Qu Wenruo | 38d5e54 | 2021-09-03 20:45:14 +0800 | [diff] [blame] | 2337 | static int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start, |
| 2338 | u64 length, u64 logical, struct page *page, |
| 2339 | unsigned int pg_offset, int mirror_num) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2340 | { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2341 | struct btrfs_device *dev; |
Christoph Hellwig | e9458bf | 2022-04-04 06:45:20 +0200 | [diff] [blame] | 2342 | struct bio_vec bvec; |
| 2343 | struct bio bio; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2344 | u64 map_length = 0; |
| 2345 | u64 sector; |
Qu Wenruo | 4c66461 | 2021-09-15 15:17:16 +0800 | [diff] [blame] | 2346 | struct btrfs_io_context *bioc = NULL; |
Christoph Hellwig | e9458bf | 2022-04-04 06:45:20 +0200 | [diff] [blame] | 2347 | int ret = 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2348 | |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2349 | ASSERT(!(fs_info->sb->s_flags & SB_RDONLY)); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2350 | BUG_ON(!mirror_num); |
| 2351 | |
Johannes Thumshirn | 554aed7 | 2021-12-07 06:28:36 -0800 | [diff] [blame] | 2352 | if (btrfs_repair_one_zone(fs_info, logical)) |
| 2353 | return 0; |
Naohiro Aota | f7ef528 | 2021-02-04 19:22:16 +0900 | [diff] [blame] | 2354 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2355 | map_length = length; |
| 2356 | |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2357 | /* |
Qu Wenruo | 4c66461 | 2021-09-15 15:17:16 +0800 | [diff] [blame] | 2358 | * Avoid races with device replace and make sure our bioc has devices |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2359 | * associated to its stripes that don't go away while we are doing the |
| 2360 | * read repair operation. |
| 2361 | */ |
| 2362 | btrfs_bio_counter_inc_blocked(fs_info); |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 2363 | if (btrfs_is_parity_mirror(fs_info, logical, length)) { |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2364 | /* |
| 2365 | * Note that we don't use BTRFS_MAP_WRITE because it's supposed |
| 2366 | * to update all raid stripes, but here we just want to correct |
| 2367 | * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad |
| 2368 | * stripe's dev and sector. |
| 2369 | */ |
| 2370 | ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical, |
Qu Wenruo | 4c66461 | 2021-09-15 15:17:16 +0800 | [diff] [blame] | 2371 | &map_length, &bioc, 0); |
Christoph Hellwig | e9458bf | 2022-04-04 06:45:20 +0200 | [diff] [blame] | 2372 | if (ret) |
| 2373 | goto out_counter_dec; |
Qu Wenruo | 4c66461 | 2021-09-15 15:17:16 +0800 | [diff] [blame] | 2374 | ASSERT(bioc->mirror_num == 1); |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2375 | } else { |
| 2376 | ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical, |
Qu Wenruo | 4c66461 | 2021-09-15 15:17:16 +0800 | [diff] [blame] | 2377 | &map_length, &bioc, mirror_num); |
Christoph Hellwig | e9458bf | 2022-04-04 06:45:20 +0200 | [diff] [blame] | 2378 | if (ret) |
| 2379 | goto out_counter_dec; |
Qu Wenruo | 4c66461 | 2021-09-15 15:17:16 +0800 | [diff] [blame] | 2380 | BUG_ON(mirror_num != bioc->mirror_num); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2381 | } |
Liu Bo | c725328 | 2017-03-29 10:53:58 -0700 | [diff] [blame] | 2382 | |
Qu Wenruo | 4c66461 | 2021-09-15 15:17:16 +0800 | [diff] [blame] | 2383 | sector = bioc->stripes[bioc->mirror_num - 1].physical >> 9; |
Qu Wenruo | 4c66461 | 2021-09-15 15:17:16 +0800 | [diff] [blame] | 2384 | dev = bioc->stripes[bioc->mirror_num - 1].dev; |
| 2385 | btrfs_put_bioc(bioc); |
Christoph Hellwig | e9458bf | 2022-04-04 06:45:20 +0200 | [diff] [blame] | 2386 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2387 | if (!dev || !dev->bdev || |
| 2388 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) { |
Christoph Hellwig | e9458bf | 2022-04-04 06:45:20 +0200 | [diff] [blame] | 2389 | ret = -EIO; |
| 2390 | goto out_counter_dec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2391 | } |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2392 | |
Christoph Hellwig | e9458bf | 2022-04-04 06:45:20 +0200 | [diff] [blame] | 2393 | bio_init(&bio, dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC); |
| 2394 | bio.bi_iter.bi_sector = sector; |
| 2395 | __bio_add_page(&bio, page, length, pg_offset); |
| 2396 | |
| 2397 | btrfsic_check_bio(&bio); |
| 2398 | ret = submit_bio_wait(&bio); |
| 2399 | if (ret) { |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2400 | /* try to remap that extent elsewhere? */ |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2401 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
Christoph Hellwig | e9458bf | 2022-04-04 06:45:20 +0200 | [diff] [blame] | 2402 | goto out_bio_uninit; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2403 | } |
| 2404 | |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 2405 | btrfs_info_rl_in_rcu(fs_info, |
| 2406 | "read error corrected: ino %llu off %llu (dev %s sector %llu)", |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 2407 | ino, start, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2408 | rcu_str_deref(dev->name), sector); |
Christoph Hellwig | e9458bf | 2022-04-04 06:45:20 +0200 | [diff] [blame] | 2409 | ret = 0; |
| 2410 | |
| 2411 | out_bio_uninit: |
| 2412 | bio_uninit(&bio); |
| 2413 | out_counter_dec: |
Filipe Manana | b5de8d0 | 2016-05-27 22:21:27 +0100 | [diff] [blame] | 2414 | btrfs_bio_counter_dec(fs_info); |
Christoph Hellwig | e9458bf | 2022-04-04 06:45:20 +0200 | [diff] [blame] | 2415 | return ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2416 | } |
| 2417 | |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 2418 | int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num) |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2419 | { |
David Sterba | 20a1fbf9 | 2019-03-20 11:23:44 +0100 | [diff] [blame] | 2420 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2421 | u64 start = eb->start; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 2422 | int i, num_pages = num_extent_pages(eb); |
Chris Mason | d95603b | 2012-04-12 15:55:15 -0400 | [diff] [blame] | 2423 | int ret = 0; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2424 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 2425 | if (sb_rdonly(fs_info->sb)) |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2426 | return -EROFS; |
| 2427 | |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2428 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 2429 | struct page *p = eb->pages[i]; |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2430 | |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 2431 | ret = repair_io_failure(fs_info, 0, start, PAGE_SIZE, start, p, |
Miao Xie | 1203b68 | 2014-09-12 18:44:01 +0800 | [diff] [blame] | 2432 | start - page_offset(p), mirror_num); |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2433 | if (ret) |
| 2434 | break; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2435 | start += PAGE_SIZE; |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | return ret; |
| 2439 | } |
| 2440 | |
Christoph Hellwig | c144c63 | 2022-07-07 07:33:26 +0200 | [diff] [blame] | 2441 | static int next_mirror(const struct io_failure_record *failrec, int cur_mirror) |
| 2442 | { |
| 2443 | if (cur_mirror == failrec->num_copies) |
| 2444 | return cur_mirror + 1 - failrec->num_copies; |
| 2445 | return cur_mirror + 1; |
| 2446 | } |
| 2447 | |
| 2448 | static int prev_mirror(const struct io_failure_record *failrec, int cur_mirror) |
| 2449 | { |
| 2450 | if (cur_mirror == 1) |
| 2451 | return failrec->num_copies; |
| 2452 | return cur_mirror - 1; |
| 2453 | } |
| 2454 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2455 | /* |
| 2456 | * each time an IO finishes, we do a fast check in the IO failure tree |
| 2457 | * to see if we need to process or clean up an io_failure_record |
| 2458 | */ |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2459 | int clean_io_failure(struct btrfs_fs_info *fs_info, |
| 2460 | struct extent_io_tree *failure_tree, |
| 2461 | struct extent_io_tree *io_tree, u64 start, |
| 2462 | struct page *page, u64 ino, unsigned int pg_offset) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2463 | { |
| 2464 | u64 private; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2465 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2466 | struct extent_state *state; |
Christoph Hellwig | c144c63 | 2022-07-07 07:33:26 +0200 | [diff] [blame] | 2467 | int mirror; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2468 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2469 | |
| 2470 | private = 0; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2471 | ret = count_range_bits(failure_tree, &private, (u64)-1, 1, |
| 2472 | EXTENT_DIRTY, 0); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2473 | if (!ret) |
| 2474 | return 0; |
| 2475 | |
Nikolay Borisov | 2279a27 | 2020-07-02 15:23:28 +0300 | [diff] [blame] | 2476 | failrec = get_state_failrec(failure_tree, start); |
| 2477 | if (IS_ERR(failrec)) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2478 | return 0; |
| 2479 | |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2480 | BUG_ON(!failrec->this_mirror); |
| 2481 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 2482 | if (sb_rdonly(fs_info->sb)) |
Ilya Dryomov | 908960c | 2013-11-03 19:06:39 +0200 | [diff] [blame] | 2483 | goto out; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2484 | |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2485 | spin_lock(&io_tree->lock); |
| 2486 | state = find_first_extent_bit_state(io_tree, |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2487 | failrec->start, |
| 2488 | EXTENT_LOCKED); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2489 | spin_unlock(&io_tree->lock); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2490 | |
Christoph Hellwig | c144c63 | 2022-07-07 07:33:26 +0200 | [diff] [blame] | 2491 | if (!state || state->start > failrec->start || |
| 2492 | state->end < failrec->start + failrec->len - 1) |
| 2493 | goto out; |
| 2494 | |
| 2495 | mirror = failrec->this_mirror; |
| 2496 | do { |
| 2497 | mirror = prev_mirror(failrec, mirror); |
| 2498 | repair_io_failure(fs_info, ino, start, failrec->len, |
| 2499 | failrec->logical, page, pg_offset, mirror); |
| 2500 | } while (mirror != failrec->failed_mirror); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2501 | |
| 2502 | out: |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2503 | free_io_failure(failure_tree, io_tree, failrec); |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2504 | return 0; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2505 | } |
| 2506 | |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2507 | /* |
| 2508 | * Can be called when |
| 2509 | * - hold extent lock |
| 2510 | * - under ordered extent |
| 2511 | * - the inode is freeing |
| 2512 | */ |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2513 | 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] | 2514 | { |
Nikolay Borisov | 7ab7956 | 2017-02-20 13:50:57 +0200 | [diff] [blame] | 2515 | struct extent_io_tree *failure_tree = &inode->io_failure_tree; |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2516 | struct io_failure_record *failrec; |
| 2517 | struct extent_state *state, *next; |
| 2518 | |
| 2519 | if (RB_EMPTY_ROOT(&failure_tree->state)) |
| 2520 | return; |
| 2521 | |
| 2522 | spin_lock(&failure_tree->lock); |
| 2523 | state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY); |
| 2524 | while (state) { |
| 2525 | if (state->start > end) |
| 2526 | break; |
| 2527 | |
| 2528 | ASSERT(state->end <= end); |
| 2529 | |
| 2530 | next = next_state(state); |
| 2531 | |
David Sterba | 47dc196 | 2016-02-11 13:24:13 +0100 | [diff] [blame] | 2532 | failrec = state->failrec; |
Miao Xie | f612496 | 2014-09-12 18:44:04 +0800 | [diff] [blame] | 2533 | free_extent_state(state); |
| 2534 | kfree(failrec); |
| 2535 | |
| 2536 | state = next; |
| 2537 | } |
| 2538 | spin_unlock(&failure_tree->lock); |
| 2539 | } |
| 2540 | |
Nikolay Borisov | 3526302 | 2020-07-02 15:23:29 +0300 | [diff] [blame] | 2541 | static struct io_failure_record *btrfs_get_io_failure_record(struct inode *inode, |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 2542 | struct btrfs_bio *bbio, |
| 2543 | unsigned int bio_offset) |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2544 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2545 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 2546 | u64 start = bbio->file_offset + bio_offset; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2547 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2548 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
| 2549 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2550 | const u32 sectorsize = fs_info->sectorsize; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2551 | int ret; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2552 | |
Nikolay Borisov | 2279a27 | 2020-07-02 15:23:28 +0300 | [diff] [blame] | 2553 | failrec = get_state_failrec(failure_tree, start); |
Nikolay Borisov | 3526302 | 2020-07-02 15:23:29 +0300 | [diff] [blame] | 2554 | if (!IS_ERR(failrec)) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2555 | btrfs_debug(fs_info, |
Qu Wenruo | 1245835 | 2021-05-03 10:08:56 +0800 | [diff] [blame] | 2556 | "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu", |
| 2557 | failrec->logical, failrec->start, failrec->len); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2558 | /* |
| 2559 | * when data can be on disk more than twice, add to failrec here |
| 2560 | * (e.g. with a list for failed_mirror) to make |
| 2561 | * clean_io_failure() clean all those errors at once. |
| 2562 | */ |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 2563 | ASSERT(failrec->this_mirror == bbio->mirror_num); |
Christoph Hellwig | c144c63 | 2022-07-07 07:33:26 +0200 | [diff] [blame] | 2564 | ASSERT(failrec->len == fs_info->sectorsize); |
Nikolay Borisov | 3526302 | 2020-07-02 15:23:29 +0300 | [diff] [blame] | 2565 | return failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2566 | } |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2567 | |
Nikolay Borisov | 3526302 | 2020-07-02 15:23:29 +0300 | [diff] [blame] | 2568 | failrec = kzalloc(sizeof(*failrec), GFP_NOFS); |
| 2569 | if (!failrec) |
| 2570 | return ERR_PTR(-ENOMEM); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2571 | |
Nikolay Borisov | 3526302 | 2020-07-02 15:23:29 +0300 | [diff] [blame] | 2572 | failrec->start = start; |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2573 | failrec->len = sectorsize; |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 2574 | failrec->failed_mirror = bbio->mirror_num; |
| 2575 | failrec->this_mirror = bbio->mirror_num; |
Christoph Hellwig | 81bd932 | 2022-07-07 07:33:30 +0200 | [diff] [blame] | 2576 | failrec->logical = (bbio->iter.bi_sector << SECTOR_SHIFT) + bio_offset; |
Nikolay Borisov | 3526302 | 2020-07-02 15:23:29 +0300 | [diff] [blame] | 2577 | |
| 2578 | btrfs_debug(fs_info, |
Christoph Hellwig | 81bd932 | 2022-07-07 07:33:30 +0200 | [diff] [blame] | 2579 | "new io failure record logical %llu start %llu", |
| 2580 | failrec->logical, start); |
Nikolay Borisov | 3526302 | 2020-07-02 15:23:29 +0300 | [diff] [blame] | 2581 | |
Christoph Hellwig | 81bd932 | 2022-07-07 07:33:30 +0200 | [diff] [blame] | 2582 | failrec->num_copies = btrfs_num_copies(fs_info, failrec->logical, sectorsize); |
Christoph Hellwig | c144c63 | 2022-07-07 07:33:26 +0200 | [diff] [blame] | 2583 | if (failrec->num_copies == 1) { |
| 2584 | /* |
| 2585 | * We only have a single copy of the data, so don't bother with |
| 2586 | * all the retry and error correction code that follows. No |
| 2587 | * matter what the error is, it is very likely to persist. |
| 2588 | */ |
| 2589 | btrfs_debug(fs_info, |
| 2590 | "cannot repair logical %llu num_copies %d", |
| 2591 | failrec->logical, failrec->num_copies); |
| 2592 | kfree(failrec); |
| 2593 | return ERR_PTR(-EIO); |
| 2594 | } |
Nikolay Borisov | 3526302 | 2020-07-02 15:23:29 +0300 | [diff] [blame] | 2595 | |
| 2596 | /* Set the bits in the private failure tree */ |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2597 | ret = set_extent_bits(failure_tree, start, start + sectorsize - 1, |
Nikolay Borisov | 3526302 | 2020-07-02 15:23:29 +0300 | [diff] [blame] | 2598 | EXTENT_LOCKED | EXTENT_DIRTY); |
| 2599 | if (ret >= 0) { |
| 2600 | ret = set_state_failrec(failure_tree, start, failrec); |
| 2601 | /* Set the bits in the inode's tree */ |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2602 | ret = set_extent_bits(tree, start, start + sectorsize - 1, |
| 2603 | EXTENT_DAMAGED); |
Nikolay Borisov | 3526302 | 2020-07-02 15:23:29 +0300 | [diff] [blame] | 2604 | } else if (ret < 0) { |
| 2605 | kfree(failrec); |
| 2606 | return ERR_PTR(ret); |
| 2607 | } |
| 2608 | |
| 2609 | return failrec; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2610 | } |
| 2611 | |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 2612 | int btrfs_repair_one_sector(struct inode *inode, struct btrfs_bio *failed_bbio, |
| 2613 | u32 bio_offset, struct page *page, unsigned int pgoff, |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2614 | submit_bio_hook_t *submit_bio_hook) |
Liu Bo | c3cfb65 | 2017-07-13 15:00:50 -0700 | [diff] [blame] | 2615 | { |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 2616 | u64 start = failed_bbio->file_offset + bio_offset; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2617 | struct io_failure_record *failrec; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2618 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2619 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2620 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 2621 | struct bio *failed_bio = &failed_bbio->bio; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 2622 | const int icsum = bio_offset >> fs_info->sectorsize_bits; |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 2623 | struct bio *repair_bio; |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 2624 | struct btrfs_bio *repair_bbio; |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2625 | |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 2626 | btrfs_debug(fs_info, |
| 2627 | "repair read error: read error at %llu", start); |
Tsutomu Itoh | e627ee7 | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 2628 | |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 2629 | BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2630 | |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 2631 | failrec = btrfs_get_io_failure_record(inode, failed_bbio, bio_offset); |
Nikolay Borisov | 3526302 | 2020-07-02 15:23:29 +0300 | [diff] [blame] | 2632 | if (IS_ERR(failrec)) |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2633 | return PTR_ERR(failrec); |
Miao Xie | 454ff3d | 2014-09-12 18:43:58 +0800 | [diff] [blame] | 2634 | |
Tsutomu Itoh | e627ee7 | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 2635 | /* |
| 2636 | * There are two premises: |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2637 | * a) deliver good data to the caller |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2638 | * b) correct the bad sectors on disk |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2639 | * |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2640 | * Since we're only doing repair for one sector, we only need to get |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2641 | * a good copy of the failed sector and if we succeed, we have setup |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2642 | * everything for repair_io_failure to do the rest for us. |
| 2643 | */ |
Christoph Hellwig | c144c63 | 2022-07-07 07:33:26 +0200 | [diff] [blame] | 2644 | failrec->this_mirror = next_mirror(failrec, failrec->this_mirror); |
| 2645 | if (failrec->this_mirror == failrec->failed_mirror) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 2646 | btrfs_debug(fs_info, |
Christoph Hellwig | c144c63 | 2022-07-07 07:33:26 +0200 | [diff] [blame] | 2647 | "failed to repair num_copies %d this_mirror %d failed_mirror %d", |
| 2648 | failrec->num_copies, failrec->this_mirror, failrec->failed_mirror); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2649 | free_io_failure(failure_tree, tree, failrec); |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2650 | return -EIO; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2651 | } |
| 2652 | |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 2653 | repair_bio = btrfs_bio_alloc(1); |
| 2654 | repair_bbio = btrfs_bio(repair_bio); |
Christoph Hellwig | 00d8252 | 2022-03-24 17:06:27 +0100 | [diff] [blame] | 2655 | repair_bbio->file_offset = start; |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 2656 | repair_bio->bi_opf = REQ_OP_READ; |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 2657 | repair_bio->bi_end_io = failed_bio->bi_end_io; |
| 2658 | repair_bio->bi_iter.bi_sector = failrec->logical >> 9; |
| 2659 | repair_bio->bi_private = failed_bio->bi_private; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2660 | |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 2661 | if (failed_bbio->csum) { |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 2662 | const u32 csum_size = fs_info->csum_size; |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 2663 | |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 2664 | repair_bbio->csum = repair_bbio->csum_inline; |
| 2665 | memcpy(repair_bbio->csum, |
| 2666 | failed_bbio->csum + csum_size * icsum, csum_size); |
Omar Sandoval | 77d5d68 | 2020-04-16 14:46:25 -0700 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | bio_add_page(repair_bio, page, failrec->len, pgoff); |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 2670 | repair_bbio->iter = repair_bio->bi_iter; |
Miao Xie | 2fe6303 | 2014-09-12 18:43:59 +0800 | [diff] [blame] | 2671 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 2672 | btrfs_debug(btrfs_sb(inode->i_sb), |
Qu Wenruo | 1245835 | 2021-05-03 10:08:56 +0800 | [diff] [blame] | 2673 | "repair read error: submitting new read to mirror %d", |
| 2674 | failrec->this_mirror); |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2675 | |
Josef Bacik | 8cbc300 | 2022-02-18 10:03:29 -0500 | [diff] [blame] | 2676 | /* |
| 2677 | * At this point we have a bio, so any errors from submit_bio_hook() |
| 2678 | * will be handled by the endio on the repair_bio, so we can't return an |
| 2679 | * error here. |
| 2680 | */ |
Christoph Hellwig | 81bd932 | 2022-07-07 07:33:30 +0200 | [diff] [blame] | 2681 | submit_bio_hook(inode, repair_bio, failrec->this_mirror, 0); |
Josef Bacik | 8cbc300 | 2022-02-18 10:03:29 -0500 | [diff] [blame] | 2682 | return BLK_STS_OK; |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | static void end_page_read(struct page *page, bool uptodate, u64 start, u32 len) |
| 2686 | { |
| 2687 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); |
| 2688 | |
| 2689 | ASSERT(page_offset(page) <= start && |
| 2690 | start + len <= page_offset(page) + PAGE_SIZE); |
| 2691 | |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2692 | if (uptodate) { |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 2693 | if (fsverity_active(page->mapping->host) && |
| 2694 | !PageError(page) && |
| 2695 | !PageUptodate(page) && |
| 2696 | start < i_size_read(page->mapping->host) && |
| 2697 | !fsverity_verify_page(page)) { |
| 2698 | btrfs_page_set_error(fs_info, page, start, len); |
| 2699 | } else { |
| 2700 | btrfs_page_set_uptodate(fs_info, page, start, len); |
| 2701 | } |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2702 | } else { |
| 2703 | btrfs_page_clear_uptodate(fs_info, page, start, len); |
| 2704 | btrfs_page_set_error(fs_info, page, start, len); |
| 2705 | } |
| 2706 | |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 2707 | if (!btrfs_is_subpage(fs_info, page)) |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2708 | unlock_page(page); |
Qu Wenruo | 3d078ef | 2021-06-07 17:02:58 +0800 | [diff] [blame] | 2709 | else |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2710 | btrfs_subpage_end_reader(fs_info, page, start, len); |
| 2711 | } |
| 2712 | |
Christoph Hellwig | a5aa7ab | 2022-05-22 13:47:50 +0200 | [diff] [blame] | 2713 | static void end_sector_io(struct page *page, u64 offset, bool uptodate) |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2714 | { |
Christoph Hellwig | a5aa7ab | 2022-05-22 13:47:50 +0200 | [diff] [blame] | 2715 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 2716 | const u32 sectorsize = inode->root->fs_info->sectorsize; |
| 2717 | struct extent_state *cached = NULL; |
| 2718 | |
| 2719 | end_page_read(page, uptodate, offset, sectorsize); |
| 2720 | if (uptodate) |
| 2721 | set_extent_uptodate(&inode->io_tree, offset, |
| 2722 | offset + sectorsize - 1, &cached, GFP_ATOMIC); |
| 2723 | unlock_extent_cached_atomic(&inode->io_tree, offset, |
| 2724 | offset + sectorsize - 1, &cached); |
| 2725 | } |
| 2726 | |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 2727 | static void submit_data_read_repair(struct inode *inode, |
| 2728 | struct btrfs_bio *failed_bbio, |
Qu Wenruo | fd5a6f6 | 2022-05-22 13:47:49 +0200 | [diff] [blame] | 2729 | u32 bio_offset, const struct bio_vec *bvec, |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 2730 | unsigned int error_bitmap) |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2731 | { |
Qu Wenruo | fd5a6f6 | 2022-05-22 13:47:49 +0200 | [diff] [blame] | 2732 | const unsigned int pgoff = bvec->bv_offset; |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2733 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Qu Wenruo | fd5a6f6 | 2022-05-22 13:47:49 +0200 | [diff] [blame] | 2734 | struct page *page = bvec->bv_page; |
| 2735 | const u64 start = page_offset(bvec->bv_page) + bvec->bv_offset; |
| 2736 | const u64 end = start + bvec->bv_len - 1; |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2737 | const u32 sectorsize = fs_info->sectorsize; |
| 2738 | const int nr_bits = (end + 1 - start) >> fs_info->sectorsize_bits; |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2739 | int i; |
| 2740 | |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 2741 | BUG_ON(bio_op(&failed_bbio->bio) == REQ_OP_WRITE); |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2742 | |
Qu Wenruo | c0111c4 | 2022-03-21 13:48:42 +0800 | [diff] [blame] | 2743 | /* This repair is only for data */ |
| 2744 | ASSERT(is_data_inode(inode)); |
| 2745 | |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2746 | /* We're here because we had some read errors or csum mismatch */ |
| 2747 | ASSERT(error_bitmap); |
| 2748 | |
| 2749 | /* |
| 2750 | * We only get called on buffered IO, thus page must be mapped and bio |
| 2751 | * must not be cloned. |
| 2752 | */ |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 2753 | ASSERT(page->mapping && !bio_flagged(&failed_bbio->bio, BIO_CLONED)); |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2754 | |
| 2755 | /* Iterate through all the sectors in the range */ |
| 2756 | for (i = 0; i < nr_bits; i++) { |
| 2757 | const unsigned int offset = i * sectorsize; |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2758 | bool uptodate = false; |
| 2759 | int ret; |
| 2760 | |
| 2761 | if (!(error_bitmap & (1U << i))) { |
| 2762 | /* |
| 2763 | * This sector has no error, just end the page read |
| 2764 | * and unlock the range. |
| 2765 | */ |
| 2766 | uptodate = true; |
| 2767 | goto next; |
| 2768 | } |
| 2769 | |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 2770 | ret = btrfs_repair_one_sector(inode, failed_bbio, |
| 2771 | bio_offset + offset, page, pgoff + offset, |
| 2772 | btrfs_submit_data_read_bio); |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2773 | if (!ret) { |
| 2774 | /* |
| 2775 | * We have submitted the read repair, the page release |
| 2776 | * will be handled by the endio function of the |
| 2777 | * submitted repair bio. |
| 2778 | * Thus we don't need to do any thing here. |
| 2779 | */ |
| 2780 | continue; |
| 2781 | } |
| 2782 | /* |
Qu Wenruo | fd5a6f6 | 2022-05-22 13:47:49 +0200 | [diff] [blame] | 2783 | * Continue on failed repair, otherwise the remaining sectors |
| 2784 | * will not be properly unlocked. |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2785 | */ |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2786 | next: |
Christoph Hellwig | a5aa7ab | 2022-05-22 13:47:50 +0200 | [diff] [blame] | 2787 | end_sector_io(page, start + offset, uptodate); |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 2788 | } |
Jan Schmidt | 4a54c8c | 2011-07-22 15:41:52 +0200 | [diff] [blame] | 2789 | } |
| 2790 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2791 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
| 2792 | |
David Sterba | b5227c0 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 2793 | 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] | 2794 | { |
Qu Wenruo | 38a39ac7 | 2021-04-08 20:32:27 +0800 | [diff] [blame] | 2795 | struct btrfs_inode *inode; |
David Sterba | 25c1252 | 2021-07-26 14:15:08 +0200 | [diff] [blame] | 2796 | const bool uptodate = (err == 0); |
Eric Sandeen | 3e2426b | 2014-06-12 00:39:58 -0500 | [diff] [blame] | 2797 | int ret = 0; |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2798 | |
Qu Wenruo | 38a39ac7 | 2021-04-08 20:32:27 +0800 | [diff] [blame] | 2799 | ASSERT(page && page->mapping); |
| 2800 | inode = BTRFS_I(page->mapping->host); |
| 2801 | btrfs_writepage_endio_finish_ordered(inode, page, start, end, uptodate); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2802 | |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2803 | if (!uptodate) { |
Qu Wenruo | 963e4db | 2021-07-26 14:35:07 +0800 | [diff] [blame] | 2804 | const struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 2805 | u32 len; |
| 2806 | |
| 2807 | ASSERT(end + 1 - start <= U32_MAX); |
| 2808 | len = end + 1 - start; |
| 2809 | |
| 2810 | btrfs_page_clear_uptodate(fs_info, page, start, len); |
| 2811 | btrfs_page_set_error(fs_info, page, start, len); |
Colin Ian King | bff5baf | 2017-05-09 18:14:01 +0100 | [diff] [blame] | 2812 | ret = err < 0 ? err : -EIO; |
Liu Bo | 5dca6ee | 2014-05-12 12:47:36 +0800 | [diff] [blame] | 2813 | mapping_set_error(page->mapping, ret); |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2814 | } |
Jeff Mahoney | 87826df | 2012-02-15 16:23:57 +0100 | [diff] [blame] | 2815 | } |
| 2816 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2817 | /* |
| 2818 | * after a writepage IO is done, we need to: |
| 2819 | * clear the uptodate bits on error |
| 2820 | * clear the writeback bits in the extent tree for this IO |
| 2821 | * end_page_writeback if the page has no more pending IO |
| 2822 | * |
| 2823 | * Scheduling is not allowed, so the extent state tree is expected |
| 2824 | * to have one and only one object corresponding to this IO. |
| 2825 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2826 | static void end_bio_extent_writepage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2827 | { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2828 | int error = blk_status_to_errno(bio->bi_status); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2829 | struct bio_vec *bvec; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2830 | u64 start; |
| 2831 | u64 end; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 2832 | struct bvec_iter_all iter_all; |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 2833 | bool first_bvec = true; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2834 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 2835 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 2836 | bio_for_each_segment_all(bvec, bio, iter_all) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2837 | struct page *page = bvec->bv_page; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2838 | struct inode *inode = page->mapping->host; |
| 2839 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Qu Wenruo | 321a02d | 2021-05-31 16:50:40 +0800 | [diff] [blame] | 2840 | const u32 sectorsize = fs_info->sectorsize; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 2841 | |
Qu Wenruo | 321a02d | 2021-05-31 16:50:40 +0800 | [diff] [blame] | 2842 | /* Our read/write should always be sector aligned. */ |
| 2843 | if (!IS_ALIGNED(bvec->bv_offset, sectorsize)) |
| 2844 | btrfs_err(fs_info, |
| 2845 | "partial page write in btrfs with offset %u and length %u", |
| 2846 | bvec->bv_offset, bvec->bv_len); |
| 2847 | else if (!IS_ALIGNED(bvec->bv_len, sectorsize)) |
| 2848 | btrfs_info(fs_info, |
| 2849 | "incomplete page write with offset %u and length %u", |
| 2850 | bvec->bv_offset, bvec->bv_len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2851 | |
Qu Wenruo | 321a02d | 2021-05-31 16:50:40 +0800 | [diff] [blame] | 2852 | start = page_offset(page) + bvec->bv_offset; |
| 2853 | end = start + bvec->bv_len - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2854 | |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 2855 | if (first_bvec) { |
| 2856 | btrfs_record_physical_zoned(inode, start, bio); |
| 2857 | first_bvec = false; |
| 2858 | } |
| 2859 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2860 | end_extent_writepage(page, error, start, end); |
Qu Wenruo | 9047e31 | 2021-05-31 16:50:43 +0800 | [diff] [blame] | 2861 | |
| 2862 | btrfs_page_clear_writeback(fs_info, page, start, bvec->bv_len); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2863 | } |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 2864 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2865 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2866 | } |
| 2867 | |
Qu Wenruo | 94e8c95 | 2020-11-13 20:51:28 +0800 | [diff] [blame] | 2868 | /* |
| 2869 | * Record previously processed extent range |
| 2870 | * |
| 2871 | * For endio_readpage_release_extent() to handle a full extent range, reducing |
| 2872 | * the extent io operations. |
| 2873 | */ |
| 2874 | struct processed_extent { |
| 2875 | struct btrfs_inode *inode; |
| 2876 | /* Start of the range in @inode */ |
| 2877 | u64 start; |
Nigel Christian | 2e626e5 | 2021-01-24 20:41:41 -0500 | [diff] [blame] | 2878 | /* End of the range in @inode */ |
Qu Wenruo | 94e8c95 | 2020-11-13 20:51:28 +0800 | [diff] [blame] | 2879 | u64 end; |
| 2880 | bool uptodate; |
| 2881 | }; |
| 2882 | |
| 2883 | /* |
| 2884 | * Try to release processed extent range |
| 2885 | * |
| 2886 | * May not release the extent range right now if the current range is |
| 2887 | * contiguous to processed extent. |
| 2888 | * |
| 2889 | * Will release processed extent when any of @inode, @uptodate, the range is |
| 2890 | * no longer contiguous to the processed range. |
| 2891 | * |
| 2892 | * Passing @inode == NULL will force processed extent to be released. |
| 2893 | */ |
| 2894 | static void endio_readpage_release_extent(struct processed_extent *processed, |
| 2895 | struct btrfs_inode *inode, u64 start, u64 end, |
| 2896 | bool uptodate) |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2897 | { |
| 2898 | struct extent_state *cached = NULL; |
Qu Wenruo | 94e8c95 | 2020-11-13 20:51:28 +0800 | [diff] [blame] | 2899 | struct extent_io_tree *tree; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2900 | |
Qu Wenruo | 94e8c95 | 2020-11-13 20:51:28 +0800 | [diff] [blame] | 2901 | /* The first extent, initialize @processed */ |
| 2902 | if (!processed->inode) |
| 2903 | goto update; |
| 2904 | |
| 2905 | /* |
| 2906 | * Contiguous to processed extent, just uptodate the end. |
| 2907 | * |
| 2908 | * Several things to notice: |
| 2909 | * |
| 2910 | * - bio can be merged as long as on-disk bytenr is contiguous |
| 2911 | * This means we can have page belonging to other inodes, thus need to |
| 2912 | * check if the inode still matches. |
| 2913 | * - bvec can contain range beyond current page for multi-page bvec |
| 2914 | * Thus we need to do processed->end + 1 >= start check |
| 2915 | */ |
| 2916 | if (processed->inode == inode && processed->uptodate == uptodate && |
| 2917 | processed->end + 1 >= start && end >= processed->end) { |
| 2918 | processed->end = end; |
| 2919 | return; |
| 2920 | } |
| 2921 | |
| 2922 | tree = &processed->inode->io_tree; |
| 2923 | /* |
| 2924 | * Now we don't have range contiguous to the processed range, release |
| 2925 | * the processed range now. |
| 2926 | */ |
| 2927 | if (processed->uptodate && tree->track_uptodate) |
| 2928 | set_extent_uptodate(tree, processed->start, processed->end, |
| 2929 | &cached, GFP_ATOMIC); |
| 2930 | unlock_extent_cached_atomic(tree, processed->start, processed->end, |
| 2931 | &cached); |
| 2932 | |
| 2933 | update: |
| 2934 | /* Update processed to current range */ |
| 2935 | processed->inode = inode; |
| 2936 | processed->start = start; |
| 2937 | processed->end = end; |
| 2938 | processed->uptodate = uptodate; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 2939 | } |
| 2940 | |
Qu Wenruo | 92082d4 | 2021-02-02 10:28:36 +0800 | [diff] [blame] | 2941 | static void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page) |
| 2942 | { |
| 2943 | ASSERT(PageLocked(page)); |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 2944 | if (!btrfs_is_subpage(fs_info, page)) |
Qu Wenruo | 92082d4 | 2021-02-02 10:28:36 +0800 | [diff] [blame] | 2945 | return; |
| 2946 | |
| 2947 | ASSERT(PagePrivate(page)); |
| 2948 | btrfs_subpage_start_reader(fs_info, page, page_offset(page), PAGE_SIZE); |
| 2949 | } |
| 2950 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2951 | /* |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 2952 | * Find extent buffer for a givne bytenr. |
Qu Wenruo | d9bb77d | 2021-03-15 13:39:14 +0800 | [diff] [blame] | 2953 | * |
| 2954 | * This is for end_bio_extent_readpage(), thus we can't do any unsafe locking |
| 2955 | * in endio context. |
| 2956 | */ |
| 2957 | static struct extent_buffer *find_extent_buffer_readpage( |
| 2958 | struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr) |
| 2959 | { |
| 2960 | struct extent_buffer *eb; |
| 2961 | |
| 2962 | /* |
| 2963 | * For regular sectorsize, we can use page->private to grab extent |
| 2964 | * buffer |
| 2965 | */ |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 2966 | if (fs_info->nodesize >= PAGE_SIZE) { |
Qu Wenruo | d9bb77d | 2021-03-15 13:39:14 +0800 | [diff] [blame] | 2967 | ASSERT(PagePrivate(page) && page->private); |
| 2968 | return (struct extent_buffer *)page->private; |
| 2969 | } |
| 2970 | |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 2971 | /* For subpage case, we need to lookup buffer radix tree */ |
| 2972 | rcu_read_lock(); |
| 2973 | eb = radix_tree_lookup(&fs_info->buffer_radix, |
| 2974 | bytenr >> fs_info->sectorsize_bits); |
| 2975 | rcu_read_unlock(); |
Qu Wenruo | d9bb77d | 2021-03-15 13:39:14 +0800 | [diff] [blame] | 2976 | ASSERT(eb); |
| 2977 | return eb; |
| 2978 | } |
| 2979 | |
| 2980 | /* |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2981 | * after a readpage IO is done, we need to: |
| 2982 | * clear the uptodate bits on error |
| 2983 | * set the uptodate bits if things worked |
| 2984 | * set the page up to date if all extents in the tree are uptodate |
| 2985 | * clear the lock bit in the extent tree |
| 2986 | * unlock the page if there are no other extents locked for it |
| 2987 | * |
| 2988 | * Scheduling is not allowed, so the extent state tree is expected |
| 2989 | * to have one and only one object corresponding to this IO. |
| 2990 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2991 | static void end_bio_extent_readpage(struct bio *bio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2992 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 2993 | struct bio_vec *bvec; |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 2994 | struct btrfs_bio *bbio = btrfs_bio(bio); |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 2995 | struct extent_io_tree *tree, *failure_tree; |
Qu Wenruo | 94e8c95 | 2020-11-13 20:51:28 +0800 | [diff] [blame] | 2996 | struct processed_extent processed = { 0 }; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 2997 | /* |
| 2998 | * The offset to the beginning of a bio, since one bio can never be |
| 2999 | * larger than UINT_MAX, u32 here is enough. |
| 3000 | */ |
| 3001 | u32 bio_offset = 0; |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 3002 | int mirror; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 3003 | struct bvec_iter_all iter_all; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3004 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 3005 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 3006 | bio_for_each_segment_all(bvec, bio, iter_all) { |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 3007 | bool uptodate = !bio->bi_status; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3008 | struct page *page = bvec->bv_page; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 3009 | struct inode *inode = page->mapping->host; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 3010 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3011 | const u32 sectorsize = fs_info->sectorsize; |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 3012 | unsigned int error_bitmap = (unsigned int)-1; |
Christoph Hellwig | 97861cd | 2022-05-22 13:47:51 +0200 | [diff] [blame] | 3013 | bool repair = false; |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3014 | u64 start; |
| 3015 | u64 end; |
| 3016 | u32 len; |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3017 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 3018 | btrfs_debug(fs_info, |
| 3019 | "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u", |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 3020 | bio->bi_iter.bi_sector, bio->bi_status, |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3021 | bbio->mirror_num); |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 3022 | tree = &BTRFS_I(inode)->io_tree; |
Josef Bacik | 7870d08 | 2017-05-05 11:57:15 -0400 | [diff] [blame] | 3023 | failure_tree = &BTRFS_I(inode)->io_failure_tree; |
David Woodhouse | 902b22f | 2008-08-20 08:51:49 -0400 | [diff] [blame] | 3024 | |
Qu Wenruo | 8b8bbd4 | 2020-10-21 14:24:58 +0800 | [diff] [blame] | 3025 | /* |
| 3026 | * We always issue full-sector reads, but if some block in a |
| 3027 | * page fails to read, blk_update_request() will advance |
| 3028 | * bv_offset and adjust bv_len to compensate. Print a warning |
| 3029 | * for unaligned offsets, and an error if they don't add up to |
| 3030 | * a full sector. |
| 3031 | */ |
| 3032 | if (!IS_ALIGNED(bvec->bv_offset, sectorsize)) |
| 3033 | btrfs_err(fs_info, |
| 3034 | "partial page read in btrfs with offset %u and length %u", |
| 3035 | bvec->bv_offset, bvec->bv_len); |
| 3036 | else if (!IS_ALIGNED(bvec->bv_offset + bvec->bv_len, |
| 3037 | sectorsize)) |
| 3038 | btrfs_info(fs_info, |
| 3039 | "incomplete page read with offset %u and length %u", |
| 3040 | bvec->bv_offset, bvec->bv_len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3041 | |
Qu Wenruo | 8b8bbd4 | 2020-10-21 14:24:58 +0800 | [diff] [blame] | 3042 | start = page_offset(page) + bvec->bv_offset; |
| 3043 | end = start + bvec->bv_len - 1; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 3044 | len = bvec->bv_len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3045 | |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3046 | mirror = bbio->mirror_num; |
Nikolay Borisov | 78e62c0 | 2018-11-22 10:17:49 +0200 | [diff] [blame] | 3047 | if (likely(uptodate)) { |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 3048 | if (is_data_inode(inode)) { |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3049 | error_bitmap = btrfs_verify_data_csum(bbio, |
Goldwyn Rodrigues | 5e29576 | 2021-03-03 06:55:37 -0600 | [diff] [blame] | 3050 | bio_offset, page, start, end); |
Christoph Hellwig | 97861cd | 2022-05-22 13:47:51 +0200 | [diff] [blame] | 3051 | if (error_bitmap) |
| 3052 | uptodate = false; |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 3053 | } else { |
Christoph Hellwig | 97861cd | 2022-05-22 13:47:51 +0200 | [diff] [blame] | 3054 | if (btrfs_validate_metadata_buffer(bbio, |
| 3055 | page, start, end, mirror)) |
| 3056 | uptodate = false; |
Qu Wenruo | 150e4b0 | 2021-05-03 10:08:55 +0800 | [diff] [blame] | 3057 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3058 | } |
Josef Bacik | ea46679 | 2012-03-26 21:57:36 -0400 | [diff] [blame] | 3059 | |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 3060 | if (likely(uptodate)) { |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 3061 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3062 | pgoff_t end_index = i_size >> PAGE_SHIFT; |
Josef Bacik | a71754f | 2013-06-17 17:14:39 -0400 | [diff] [blame] | 3063 | |
Christoph Hellwig | 97861cd | 2022-05-22 13:47:51 +0200 | [diff] [blame] | 3064 | clean_io_failure(BTRFS_I(inode)->root->fs_info, |
| 3065 | failure_tree, tree, start, page, |
| 3066 | btrfs_ino(BTRFS_I(inode)), 0); |
| 3067 | |
Qu Wenruo | c28ea61 | 2021-03-01 16:44:22 +0800 | [diff] [blame] | 3068 | /* |
| 3069 | * Zero out the remaining part if this range straddles |
| 3070 | * i_size. |
| 3071 | * |
| 3072 | * Here we should only zero the range inside the bvec, |
| 3073 | * not touch anything else. |
| 3074 | * |
| 3075 | * NOTE: i_size is exclusive while end is inclusive. |
| 3076 | */ |
| 3077 | if (page->index == end_index && i_size <= end) { |
| 3078 | u32 zero_start = max(offset_in_page(i_size), |
Qu Wenruo | d2dcc8e | 2021-03-08 17:20:17 +0800 | [diff] [blame] | 3079 | offset_in_page(start)); |
Qu Wenruo | c28ea61 | 2021-03-01 16:44:22 +0800 | [diff] [blame] | 3080 | |
| 3081 | zero_user_segment(page, zero_start, |
| 3082 | offset_in_page(end) + 1); |
| 3083 | } |
Christoph Hellwig | 97861cd | 2022-05-22 13:47:51 +0200 | [diff] [blame] | 3084 | } else if (is_data_inode(inode)) { |
| 3085 | /* |
| 3086 | * Only try to repair bios that actually made it to a |
| 3087 | * device. If the bio failed to be submitted mirror |
| 3088 | * is 0 and we need to fail it without retrying. |
Christoph Hellwig | 81bd932 | 2022-07-07 07:33:30 +0200 | [diff] [blame] | 3089 | * |
| 3090 | * This also includes the high level bios for compressed |
| 3091 | * extents - these never make it to a device and repair |
| 3092 | * is already handled on the lower compressed bio. |
Christoph Hellwig | 97861cd | 2022-05-22 13:47:51 +0200 | [diff] [blame] | 3093 | */ |
| 3094 | if (mirror > 0) |
| 3095 | repair = true; |
| 3096 | } else { |
| 3097 | struct extent_buffer *eb; |
| 3098 | |
| 3099 | eb = find_extent_buffer_readpage(fs_info, page, start); |
| 3100 | set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
| 3101 | eb->read_mirror = mirror; |
| 3102 | atomic_dec(&eb->io_pages); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3103 | } |
Christoph Hellwig | 97861cd | 2022-05-22 13:47:51 +0200 | [diff] [blame] | 3104 | |
| 3105 | if (repair) { |
| 3106 | /* |
| 3107 | * submit_data_read_repair() will handle all the good |
| 3108 | * and bad sectors, we just continue to the next bvec. |
| 3109 | */ |
Christoph Hellwig | 7aa5123 | 2022-07-07 07:33:28 +0200 | [diff] [blame] | 3110 | submit_data_read_repair(inode, bbio, bio_offset, bvec, |
| 3111 | error_bitmap); |
Christoph Hellwig | 97861cd | 2022-05-22 13:47:51 +0200 | [diff] [blame] | 3112 | } else { |
| 3113 | /* Update page status and unlock */ |
| 3114 | end_page_read(page, uptodate, start, len); |
| 3115 | endio_readpage_release_extent(&processed, BTRFS_I(inode), |
| 3116 | start, end, PageUptodate(page)); |
| 3117 | } |
| 3118 | |
Qu Wenruo | 7ffd27e | 2020-12-02 14:47:58 +0800 | [diff] [blame] | 3119 | ASSERT(bio_offset + len > bio_offset); |
| 3120 | bio_offset += len; |
Miao Xie | 883d0de | 2013-07-25 19:22:35 +0800 | [diff] [blame] | 3121 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 3122 | } |
Qu Wenruo | 94e8c95 | 2020-11-13 20:51:28 +0800 | [diff] [blame] | 3123 | /* Release the last extent */ |
| 3124 | endio_readpage_release_extent(&processed, NULL, 0, 0, false); |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3125 | btrfs_bio_free_csum(bbio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3126 | bio_put(bio); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3127 | } |
| 3128 | |
Sweet Tea Dorminy | dd137dd | 2022-03-30 16:11:22 -0400 | [diff] [blame] | 3129 | /** |
| 3130 | * Populate every free slot in a provided array with pages. |
| 3131 | * |
| 3132 | * @nr_pages: number of pages to allocate |
| 3133 | * @page_array: the array to fill with pages; any existing non-null entries in |
| 3134 | * the array will be skipped |
| 3135 | * |
| 3136 | * Return: 0 if all pages were able to be allocated; |
| 3137 | * -ENOMEM otherwise, and the caller is responsible for freeing all |
| 3138 | * non-null page pointers in the array. |
| 3139 | */ |
| 3140 | int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array) |
| 3141 | { |
Sweet Tea Dorminy | 91d6ac1 | 2022-03-30 16:11:23 -0400 | [diff] [blame] | 3142 | unsigned int allocated; |
Sweet Tea Dorminy | dd137dd | 2022-03-30 16:11:22 -0400 | [diff] [blame] | 3143 | |
Sweet Tea Dorminy | 91d6ac1 | 2022-03-30 16:11:23 -0400 | [diff] [blame] | 3144 | for (allocated = 0; allocated < nr_pages;) { |
| 3145 | unsigned int last = allocated; |
Sweet Tea Dorminy | dd137dd | 2022-03-30 16:11:22 -0400 | [diff] [blame] | 3146 | |
Sweet Tea Dorminy | 91d6ac1 | 2022-03-30 16:11:23 -0400 | [diff] [blame] | 3147 | allocated = alloc_pages_bulk_array(GFP_NOFS, nr_pages, page_array); |
| 3148 | |
Sweet Tea Dorminy | 395cb57 | 2022-04-06 14:24:18 -0400 | [diff] [blame] | 3149 | if (allocated == nr_pages) |
| 3150 | return 0; |
| 3151 | |
Sweet Tea Dorminy | 91d6ac1 | 2022-03-30 16:11:23 -0400 | [diff] [blame] | 3152 | /* |
| 3153 | * During this iteration, no page could be allocated, even |
| 3154 | * though alloc_pages_bulk_array() falls back to alloc_page() |
| 3155 | * if it could not bulk-allocate. So we must be out of memory. |
| 3156 | */ |
| 3157 | if (allocated == last) |
Sweet Tea Dorminy | dd137dd | 2022-03-30 16:11:22 -0400 | [diff] [blame] | 3158 | return -ENOMEM; |
Sweet Tea Dorminy | 395cb57 | 2022-04-06 14:24:18 -0400 | [diff] [blame] | 3159 | |
| 3160 | memalloc_retry_wait(GFP_NOFS); |
Sweet Tea Dorminy | dd137dd | 2022-03-30 16:11:22 -0400 | [diff] [blame] | 3161 | } |
| 3162 | return 0; |
| 3163 | } |
| 3164 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 3165 | /* |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 3166 | * Initialize the members up to but not including 'bio'. Use after allocating a |
| 3167 | * new bio by bio_alloc_bioset as it does not initialize the bytes outside of |
| 3168 | * 'bio' because use of __GFP_ZERO is not supported. |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 3169 | */ |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3170 | static inline void btrfs_bio_init(struct btrfs_bio *bbio) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3171 | { |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3172 | memset(bbio, 0, offsetof(struct btrfs_bio, bio)); |
David Sterba | 184f999 | 2017-06-12 17:29:39 +0200 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | /* |
Qu Wenruo | cd8e0cc | 2021-09-15 15:17:17 +0800 | [diff] [blame] | 3176 | * Allocate a btrfs_io_bio, with @nr_iovecs as maximum number of iovecs. |
| 3177 | * |
| 3178 | * The bio allocation is backed by bioset and does not fail. |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3179 | */ |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3180 | struct bio *btrfs_bio_alloc(unsigned int nr_iovecs) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3181 | { |
| 3182 | struct bio *bio; |
| 3183 | |
Qu Wenruo | cd8e0cc | 2021-09-15 15:17:17 +0800 | [diff] [blame] | 3184 | ASSERT(0 < nr_iovecs && nr_iovecs <= BIO_MAX_VECS); |
Christoph Hellwig | 609be10 | 2022-01-24 10:11:03 +0100 | [diff] [blame] | 3185 | bio = bio_alloc_bioset(NULL, nr_iovecs, 0, GFP_NOFS, &btrfs_bioset); |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3186 | btrfs_bio_init(btrfs_bio(bio)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3187 | return bio; |
| 3188 | } |
| 3189 | |
Chaitanya Kulkarni | 21dda65 | 2021-07-21 21:43:33 +0900 | [diff] [blame] | 3190 | struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size) |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 3191 | { |
| 3192 | struct bio *bio; |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3193 | struct btrfs_bio *bbio; |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 3194 | |
Chaitanya Kulkarni | 21dda65 | 2021-07-21 21:43:33 +0900 | [diff] [blame] | 3195 | ASSERT(offset <= UINT_MAX && size <= UINT_MAX); |
| 3196 | |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 3197 | /* this will never fail when it's backed by a bioset */ |
Christoph Hellwig | abfc426 | 2022-02-02 17:01:09 +0100 | [diff] [blame] | 3198 | bio = bio_alloc_clone(orig->bi_bdev, orig, GFP_NOFS, &btrfs_bioset); |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 3199 | ASSERT(bio); |
| 3200 | |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3201 | bbio = btrfs_bio(bio); |
| 3202 | btrfs_bio_init(bbio); |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 3203 | |
| 3204 | bio_trim(bio, offset >> 9, size >> 9); |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3205 | bbio->iter = bio->bi_iter; |
Liu Bo | 2f8e914 | 2017-05-15 17:43:31 -0700 | [diff] [blame] | 3206 | return bio; |
| 3207 | } |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 3208 | |
Naohiro Aota | 953651e | 2021-02-04 19:21:57 +0900 | [diff] [blame] | 3209 | /** |
| 3210 | * Attempt to add a page to bio |
| 3211 | * |
Yang Li | be8d1a2 | 2021-12-20 15:23:06 +0800 | [diff] [blame] | 3212 | * @bio_ctrl: record both the bio, and its bio_flags |
Naohiro Aota | 953651e | 2021-02-04 19:21:57 +0900 | [diff] [blame] | 3213 | * @page: page to add to the bio |
| 3214 | * @disk_bytenr: offset of the new bio or to check whether we are adding |
| 3215 | * a contiguous page to the previous one |
Naohiro Aota | 953651e | 2021-02-04 19:21:57 +0900 | [diff] [blame] | 3216 | * @size: portion of page that we want to write |
Yang Li | be8d1a2 | 2021-12-20 15:23:06 +0800 | [diff] [blame] | 3217 | * @pg_offset: starting offset in the page |
David Sterba | cb3a12d | 2021-07-27 14:59:41 +0200 | [diff] [blame] | 3218 | * @compress_type: compression type of the current bio to see if we can merge them |
Naohiro Aota | 953651e | 2021-02-04 19:21:57 +0900 | [diff] [blame] | 3219 | * |
| 3220 | * Attempt to add a page to bio considering stripe alignment etc. |
| 3221 | * |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3222 | * Return >= 0 for the number of bytes added to the bio. |
| 3223 | * Can return 0 if the current bio is already at stripe/zone boundary. |
| 3224 | * Return <0 for error. |
Naohiro Aota | 953651e | 2021-02-04 19:21:57 +0900 | [diff] [blame] | 3225 | */ |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3226 | static int btrfs_bio_add_page(struct btrfs_bio_ctrl *bio_ctrl, |
| 3227 | struct page *page, |
| 3228 | u64 disk_bytenr, unsigned int size, |
| 3229 | unsigned int pg_offset, |
David Sterba | cb3a12d | 2021-07-27 14:59:41 +0200 | [diff] [blame] | 3230 | enum btrfs_compression_type compress_type) |
Naohiro Aota | 953651e | 2021-02-04 19:21:57 +0900 | [diff] [blame] | 3231 | { |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3232 | struct bio *bio = bio_ctrl->bio; |
| 3233 | u32 bio_size = bio->bi_iter.bi_size; |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3234 | u32 real_size; |
Naohiro Aota | 953651e | 2021-02-04 19:21:57 +0900 | [diff] [blame] | 3235 | const sector_t sector = disk_bytenr >> SECTOR_SHIFT; |
| 3236 | bool contig; |
Naohiro Aota | e1326f0 | 2021-02-04 19:21:58 +0900 | [diff] [blame] | 3237 | int ret; |
Naohiro Aota | 953651e | 2021-02-04 19:21:57 +0900 | [diff] [blame] | 3238 | |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3239 | ASSERT(bio); |
| 3240 | /* The limit should be calculated when bio_ctrl->bio is allocated */ |
| 3241 | ASSERT(bio_ctrl->len_to_oe_boundary && bio_ctrl->len_to_stripe_boundary); |
David Sterba | 0f07003 | 2021-07-27 15:11:53 +0200 | [diff] [blame] | 3242 | if (bio_ctrl->compress_type != compress_type) |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3243 | return 0; |
Naohiro Aota | 953651e | 2021-02-04 19:21:57 +0900 | [diff] [blame] | 3244 | |
David Sterba | 0f07003 | 2021-07-27 15:11:53 +0200 | [diff] [blame] | 3245 | if (bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) |
Naohiro Aota | 953651e | 2021-02-04 19:21:57 +0900 | [diff] [blame] | 3246 | contig = bio->bi_iter.bi_sector == sector; |
| 3247 | else |
| 3248 | contig = bio_end_sector(bio) == sector; |
| 3249 | if (!contig) |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3250 | return 0; |
Naohiro Aota | 953651e | 2021-02-04 19:21:57 +0900 | [diff] [blame] | 3251 | |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3252 | real_size = min(bio_ctrl->len_to_oe_boundary, |
| 3253 | bio_ctrl->len_to_stripe_boundary) - bio_size; |
| 3254 | real_size = min(real_size, size); |
| 3255 | |
| 3256 | /* |
| 3257 | * If real_size is 0, never call bio_add_*_page(), as even size is 0, |
| 3258 | * bio will still execute its endio function on the page! |
| 3259 | */ |
| 3260 | if (real_size == 0) |
| 3261 | return 0; |
Naohiro Aota | 953651e | 2021-02-04 19:21:57 +0900 | [diff] [blame] | 3262 | |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3263 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3264 | ret = bio_add_zone_append_page(bio, page, real_size, pg_offset); |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3265 | else |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3266 | ret = bio_add_page(bio, page, real_size, pg_offset); |
Naohiro Aota | e1326f0 | 2021-02-04 19:21:58 +0900 | [diff] [blame] | 3267 | |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3268 | return ret; |
Naohiro Aota | 953651e | 2021-02-04 19:21:57 +0900 | [diff] [blame] | 3269 | } |
| 3270 | |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3271 | static int calc_bio_boundaries(struct btrfs_bio_ctrl *bio_ctrl, |
Naohiro Aota | 939c7feb | 2021-08-11 15:37:08 +0900 | [diff] [blame] | 3272 | struct btrfs_inode *inode, u64 file_offset) |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3273 | { |
| 3274 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 3275 | struct btrfs_io_geometry geom; |
| 3276 | struct btrfs_ordered_extent *ordered; |
| 3277 | struct extent_map *em; |
| 3278 | u64 logical = (bio_ctrl->bio->bi_iter.bi_sector << SECTOR_SHIFT); |
| 3279 | int ret; |
| 3280 | |
| 3281 | /* |
| 3282 | * Pages for compressed extent are never submitted to disk directly, |
| 3283 | * thus it has no real boundary, just set them to U32_MAX. |
| 3284 | * |
| 3285 | * The split happens for real compressed bio, which happens in |
| 3286 | * btrfs_submit_compressed_read/write(). |
| 3287 | */ |
David Sterba | 0f07003 | 2021-07-27 15:11:53 +0200 | [diff] [blame] | 3288 | if (bio_ctrl->compress_type != BTRFS_COMPRESS_NONE) { |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3289 | bio_ctrl->len_to_oe_boundary = U32_MAX; |
| 3290 | bio_ctrl->len_to_stripe_boundary = U32_MAX; |
| 3291 | return 0; |
| 3292 | } |
| 3293 | em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize); |
| 3294 | if (IS_ERR(em)) |
| 3295 | return PTR_ERR(em); |
| 3296 | ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio_ctrl->bio), |
| 3297 | logical, &geom); |
| 3298 | free_extent_map(em); |
| 3299 | if (ret < 0) { |
| 3300 | return ret; |
| 3301 | } |
| 3302 | if (geom.len > U32_MAX) |
| 3303 | bio_ctrl->len_to_stripe_boundary = U32_MAX; |
| 3304 | else |
| 3305 | bio_ctrl->len_to_stripe_boundary = (u32)geom.len; |
| 3306 | |
Johannes Thumshirn | 7367271 | 2021-12-07 06:28:37 -0800 | [diff] [blame] | 3307 | if (bio_op(bio_ctrl->bio) != REQ_OP_ZONE_APPEND) { |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3308 | bio_ctrl->len_to_oe_boundary = U32_MAX; |
| 3309 | return 0; |
| 3310 | } |
| 3311 | |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3312 | /* Ordered extent not yet created, so we're good */ |
Naohiro Aota | 939c7feb | 2021-08-11 15:37:08 +0900 | [diff] [blame] | 3313 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3314 | if (!ordered) { |
| 3315 | bio_ctrl->len_to_oe_boundary = U32_MAX; |
| 3316 | return 0; |
| 3317 | } |
| 3318 | |
| 3319 | bio_ctrl->len_to_oe_boundary = min_t(u32, U32_MAX, |
| 3320 | ordered->disk_bytenr + ordered->disk_num_bytes - logical); |
| 3321 | btrfs_put_ordered_extent(ordered); |
| 3322 | return 0; |
| 3323 | } |
| 3324 | |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3325 | static int alloc_new_bio(struct btrfs_inode *inode, |
| 3326 | struct btrfs_bio_ctrl *bio_ctrl, |
| 3327 | struct writeback_control *wbc, |
Bart Van Assche | bf9486d | 2022-07-14 11:07:16 -0700 | [diff] [blame] | 3328 | blk_opf_t opf, |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3329 | bio_end_io_t end_io_func, |
Naohiro Aota | 939c7feb | 2021-08-11 15:37:08 +0900 | [diff] [blame] | 3330 | u64 disk_bytenr, u32 offset, u64 file_offset, |
David Sterba | cb3a12d | 2021-07-27 14:59:41 +0200 | [diff] [blame] | 3331 | enum btrfs_compression_type compress_type) |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3332 | { |
| 3333 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 3334 | struct bio *bio; |
| 3335 | int ret; |
| 3336 | |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 3337 | bio = btrfs_bio_alloc(BIO_MAX_VECS); |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3338 | /* |
| 3339 | * For compressed page range, its disk_bytenr is always @disk_bytenr |
| 3340 | * passed in, no matter if we have added any range into previous bio. |
| 3341 | */ |
David Sterba | cb3a12d | 2021-07-27 14:59:41 +0200 | [diff] [blame] | 3342 | if (compress_type != BTRFS_COMPRESS_NONE) |
Qu Wenruo | cd8e0cc | 2021-09-15 15:17:17 +0800 | [diff] [blame] | 3343 | bio->bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT; |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3344 | else |
Qu Wenruo | cd8e0cc | 2021-09-15 15:17:17 +0800 | [diff] [blame] | 3345 | bio->bi_iter.bi_sector = (disk_bytenr + offset) >> SECTOR_SHIFT; |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3346 | bio_ctrl->bio = bio; |
David Sterba | 0f07003 | 2021-07-27 15:11:53 +0200 | [diff] [blame] | 3347 | bio_ctrl->compress_type = compress_type; |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3348 | bio->bi_end_io = end_io_func; |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3349 | bio->bi_opf = opf; |
Naohiro Aota | 939c7feb | 2021-08-11 15:37:08 +0900 | [diff] [blame] | 3350 | ret = calc_bio_boundaries(bio_ctrl, inode, file_offset); |
| 3351 | if (ret < 0) |
| 3352 | goto error; |
Christoph Hellwig | 50f1cff | 2022-03-24 17:52:10 +0100 | [diff] [blame] | 3353 | |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3354 | if (wbc) { |
Christoph Hellwig | 50f1cff | 2022-03-24 17:52:10 +0100 | [diff] [blame] | 3355 | /* |
| 3356 | * For Zone append we need the correct block_device that we are |
| 3357 | * going to write to set in the bio to be able to respect the |
| 3358 | * hardware limitation. Look it up here: |
| 3359 | */ |
| 3360 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) { |
| 3361 | struct btrfs_device *dev; |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3362 | |
Christoph Hellwig | 50f1cff | 2022-03-24 17:52:10 +0100 | [diff] [blame] | 3363 | dev = btrfs_zoned_get_device(fs_info, disk_bytenr, |
| 3364 | fs_info->sectorsize); |
| 3365 | if (IS_ERR(dev)) { |
| 3366 | ret = PTR_ERR(dev); |
| 3367 | goto error; |
| 3368 | } |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3369 | |
Christoph Hellwig | 50f1cff | 2022-03-24 17:52:10 +0100 | [diff] [blame] | 3370 | bio_set_dev(bio, dev->bdev); |
| 3371 | } else { |
| 3372 | /* |
| 3373 | * Otherwise pick the last added device to support |
| 3374 | * cgroup writeback. For multi-device file systems this |
| 3375 | * means blk-cgroup policies have to always be set on the |
| 3376 | * last added/replaced device. This is a bit odd but has |
| 3377 | * been like that for a long time. |
| 3378 | */ |
| 3379 | bio_set_dev(bio, fs_info->fs_devices->latest_dev->bdev); |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3380 | } |
Christoph Hellwig | 50f1cff | 2022-03-24 17:52:10 +0100 | [diff] [blame] | 3381 | wbc_init_bio(wbc, bio); |
| 3382 | } else { |
| 3383 | ASSERT(bio_op(bio) != REQ_OP_ZONE_APPEND); |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3384 | } |
| 3385 | return 0; |
| 3386 | error: |
| 3387 | bio_ctrl->bio = NULL; |
| 3388 | bio->bi_status = errno_to_blk_status(ret); |
| 3389 | bio_endio(bio); |
| 3390 | return ret; |
| 3391 | } |
| 3392 | |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3393 | /* |
| 3394 | * @opf: bio REQ_OP_* and REQ_* flags as one value |
David Sterba | b8b3d62 | 2017-06-12 19:50:41 +0200 | [diff] [blame] | 3395 | * @wbc: optional writeback control for io accounting |
| 3396 | * @page: page to add to the bio |
Qu Wenruo | 0c64c33 | 2021-01-06 09:01:40 +0800 | [diff] [blame] | 3397 | * @disk_bytenr: logical bytenr where the write will be |
| 3398 | * @size: portion of page that we want to write to |
David Sterba | b8b3d62 | 2017-06-12 19:50:41 +0200 | [diff] [blame] | 3399 | * @pg_offset: offset of the new bio or to check whether we are adding |
| 3400 | * a contiguous page to the previous one |
David Sterba | 5c2b1fd | 2017-06-06 19:22:55 +0200 | [diff] [blame] | 3401 | * @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] | 3402 | * @end_io_func: end_io callback for new bio |
| 3403 | * @mirror_num: desired mirror to read/write |
| 3404 | * @prev_bio_flags: flags of previous bio to see if we can merge the current one |
David Sterba | cb3a12d | 2021-07-27 14:59:41 +0200 | [diff] [blame] | 3405 | * @compress_type: compress type for current bio |
David Sterba | 4b81ba4 | 2017-06-06 19:14:26 +0200 | [diff] [blame] | 3406 | */ |
Bart Van Assche | bf9486d | 2022-07-14 11:07:16 -0700 | [diff] [blame] | 3407 | static int submit_extent_page(blk_opf_t opf, |
Chris Mason | da2f0f7 | 2015-07-02 13:57:22 -0700 | [diff] [blame] | 3408 | struct writeback_control *wbc, |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3409 | struct btrfs_bio_ctrl *bio_ctrl, |
Qu Wenruo | 0c64c33 | 2021-01-06 09:01:40 +0800 | [diff] [blame] | 3410 | struct page *page, u64 disk_bytenr, |
David Sterba | 6c5a4e2 | 2017-10-04 17:10:34 +0200 | [diff] [blame] | 3411 | size_t size, unsigned long pg_offset, |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 3412 | bio_end_io_t end_io_func, |
David Sterba | cb3a12d | 2021-07-27 14:59:41 +0200 | [diff] [blame] | 3413 | enum btrfs_compression_type compress_type, |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3414 | bool force_bio_submit) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3415 | { |
| 3416 | int ret = 0; |
Naohiro Aota | e1326f0 | 2021-02-04 19:21:58 +0900 | [diff] [blame] | 3417 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3418 | unsigned int cur = pg_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3419 | |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3420 | ASSERT(bio_ctrl); |
David Sterba | 5c2b1fd | 2017-06-06 19:22:55 +0200 | [diff] [blame] | 3421 | |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3422 | ASSERT(pg_offset < PAGE_SIZE && size <= PAGE_SIZE && |
| 3423 | pg_offset + size <= PAGE_SIZE); |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 3424 | if (force_bio_submit) |
| 3425 | submit_one_bio(bio_ctrl); |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3426 | |
| 3427 | while (cur < pg_offset + size) { |
| 3428 | u32 offset = cur - pg_offset; |
| 3429 | int added; |
| 3430 | |
| 3431 | /* Allocate new bio if needed */ |
| 3432 | if (!bio_ctrl->bio) { |
| 3433 | ret = alloc_new_bio(inode, bio_ctrl, wbc, opf, |
| 3434 | end_io_func, disk_bytenr, offset, |
Naohiro Aota | 939c7feb | 2021-08-11 15:37:08 +0900 | [diff] [blame] | 3435 | page_offset(page) + cur, |
David Sterba | cb3a12d | 2021-07-27 14:59:41 +0200 | [diff] [blame] | 3436 | compress_type); |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3437 | if (ret < 0) |
| 3438 | return ret; |
| 3439 | } |
| 3440 | /* |
| 3441 | * We must go through btrfs_bio_add_page() to ensure each |
| 3442 | * page range won't cross various boundaries. |
| 3443 | */ |
David Sterba | cb3a12d | 2021-07-27 14:59:41 +0200 | [diff] [blame] | 3444 | if (compress_type != BTRFS_COMPRESS_NONE) |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3445 | added = btrfs_bio_add_page(bio_ctrl, page, disk_bytenr, |
| 3446 | size - offset, pg_offset + offset, |
David Sterba | cb3a12d | 2021-07-27 14:59:41 +0200 | [diff] [blame] | 3447 | compress_type); |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3448 | else |
| 3449 | added = btrfs_bio_add_page(bio_ctrl, page, |
| 3450 | disk_bytenr + offset, size - offset, |
David Sterba | cb3a12d | 2021-07-27 14:59:41 +0200 | [diff] [blame] | 3451 | pg_offset + offset, compress_type); |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3452 | |
| 3453 | /* Metadata page range should never be split */ |
| 3454 | if (!is_data_inode(&inode->vfs_inode)) |
| 3455 | ASSERT(added == 0 || added == size - offset); |
| 3456 | |
| 3457 | /* At least we added some page, update the account */ |
| 3458 | if (wbc && added) |
| 3459 | wbc_account_cgroup_owner(wbc, page, added); |
| 3460 | |
| 3461 | /* We have reached boundary, submit right now */ |
| 3462 | if (added < size - offset) { |
| 3463 | /* The bio should contain some page(s) */ |
| 3464 | ASSERT(bio_ctrl->bio->bi_iter.bi_size); |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 3465 | submit_one_bio(bio_ctrl); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3466 | } |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3467 | cur += added; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3468 | } |
Qu Wenruo | e0eefe0 | 2021-07-26 14:35:00 +0800 | [diff] [blame] | 3469 | return 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3470 | } |
| 3471 | |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 3472 | static int attach_extent_buffer_page(struct extent_buffer *eb, |
| 3473 | struct page *page, |
| 3474 | struct btrfs_subpage *prealloc) |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 3475 | { |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 3476 | struct btrfs_fs_info *fs_info = eb->fs_info; |
| 3477 | int ret = 0; |
| 3478 | |
Qu Wenruo | 0d01e24 | 2020-10-21 14:25:02 +0800 | [diff] [blame] | 3479 | /* |
| 3480 | * If the page is mapped to btree inode, we should hold the private |
| 3481 | * lock to prevent race. |
| 3482 | * For cloned or dummy extent buffers, their pages are not mapped and |
| 3483 | * will not race with any other ebs. |
| 3484 | */ |
| 3485 | if (page->mapping) |
| 3486 | lockdep_assert_held(&page->mapping->private_lock); |
| 3487 | |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 3488 | if (fs_info->nodesize >= PAGE_SIZE) { |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 3489 | if (!PagePrivate(page)) |
| 3490 | attach_page_private(page, eb); |
| 3491 | else |
| 3492 | WARN_ON(page->private != (unsigned long)eb); |
| 3493 | return 0; |
| 3494 | } |
| 3495 | |
| 3496 | /* Already mapped, just free prealloc */ |
| 3497 | if (PagePrivate(page)) { |
| 3498 | btrfs_free_subpage(prealloc); |
| 3499 | return 0; |
| 3500 | } |
| 3501 | |
| 3502 | if (prealloc) |
| 3503 | /* Has preallocated memory for subpage */ |
| 3504 | attach_page_private(page, prealloc); |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 3505 | else |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 3506 | /* Do new allocation to attach subpage */ |
| 3507 | ret = btrfs_attach_subpage(fs_info, page, |
| 3508 | BTRFS_SUBPAGE_METADATA); |
| 3509 | return ret; |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 3510 | } |
| 3511 | |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 3512 | int set_page_extent_mapped(struct page *page) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3513 | { |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 3514 | struct btrfs_fs_info *fs_info; |
| 3515 | |
| 3516 | ASSERT(page->mapping); |
| 3517 | |
| 3518 | if (PagePrivate(page)) |
| 3519 | return 0; |
| 3520 | |
| 3521 | fs_info = btrfs_sb(page->mapping->host->i_sb); |
| 3522 | |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 3523 | if (btrfs_is_subpage(fs_info, page)) |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 3524 | return btrfs_attach_subpage(fs_info, page, BTRFS_SUBPAGE_DATA); |
| 3525 | |
| 3526 | attach_page_private(page, (void *)EXTENT_PAGE_PRIVATE); |
| 3527 | return 0; |
| 3528 | } |
| 3529 | |
| 3530 | void clear_page_extent_mapped(struct page *page) |
| 3531 | { |
| 3532 | struct btrfs_fs_info *fs_info; |
| 3533 | |
| 3534 | ASSERT(page->mapping); |
| 3535 | |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 3536 | if (!PagePrivate(page)) |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 3537 | return; |
| 3538 | |
| 3539 | fs_info = btrfs_sb(page->mapping->host->i_sb); |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 3540 | if (btrfs_is_subpage(fs_info, page)) |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 3541 | return btrfs_detach_subpage(fs_info, page); |
| 3542 | |
| 3543 | detach_page_private(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3544 | } |
| 3545 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3546 | static struct extent_map * |
| 3547 | __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset, |
Nikolay Borisov | 1a5ee1e | 2020-09-14 12:37:06 +0300 | [diff] [blame] | 3548 | u64 start, u64 len, struct extent_map **em_cached) |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3549 | { |
| 3550 | struct extent_map *em; |
| 3551 | |
| 3552 | if (em_cached && *em_cached) { |
| 3553 | em = *em_cached; |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 3554 | if (extent_map_in_tree(em) && start >= em->start && |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3555 | start < extent_map_end(em)) { |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 3556 | refcount_inc(&em->refs); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3557 | return em; |
| 3558 | } |
| 3559 | |
| 3560 | free_extent_map(em); |
| 3561 | *em_cached = NULL; |
| 3562 | } |
| 3563 | |
Nikolay Borisov | 1a5ee1e | 2020-09-14 12:37:06 +0300 | [diff] [blame] | 3564 | em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, start, len); |
Filipe Manana | c034755 | 2022-02-03 15:36:42 +0000 | [diff] [blame] | 3565 | if (em_cached && !IS_ERR(em)) { |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3566 | BUG_ON(*em_cached); |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 3567 | refcount_inc(&em->refs); |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3568 | *em_cached = em; |
| 3569 | } |
| 3570 | return em; |
| 3571 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3572 | /* |
| 3573 | * basic readpage implementation. Locked extent state structs are inserted |
| 3574 | * into the tree that are removed when the IO is done (by the end_io |
| 3575 | * handlers) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3576 | * XXX JDM: This needs looking at to ensure proper page locking |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3577 | * return 0 on success, otherwise return error |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3578 | */ |
Christoph Hellwig | 7aab8b3 | 2022-04-15 16:33:24 +0200 | [diff] [blame] | 3579 | static int btrfs_do_readpage(struct page *page, struct extent_map **em_cached, |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3580 | struct btrfs_bio_ctrl *bio_ctrl, |
Bart Van Assche | bf9486d | 2022-07-14 11:07:16 -0700 | [diff] [blame] | 3581 | blk_opf_t read_flags, u64 *prev_em_start) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3582 | { |
| 3583 | struct inode *inode = page->mapping->host; |
Qu Wenruo | 92082d4 | 2021-02-02 10:28:36 +0800 | [diff] [blame] | 3584 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 3585 | u64 start = page_offset(page); |
David Sterba | 8eec829 | 2017-06-06 19:50:13 +0200 | [diff] [blame] | 3586 | const u64 end = start + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3587 | u64 cur = start; |
| 3588 | u64 extent_offset; |
| 3589 | u64 last_byte = i_size_read(inode); |
| 3590 | u64 block_start; |
| 3591 | u64 cur_end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3592 | struct extent_map *em; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3593 | int ret = 0; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3594 | size_t pg_offset = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3595 | size_t iosize; |
| 3596 | size_t blocksize = inode->i_sb->s_blocksize; |
David Sterba | f657a31 | 2020-02-05 19:09:42 +0100 | [diff] [blame] | 3597 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; |
David Sterba | ae6957e | 2020-02-05 19:09:30 +0100 | [diff] [blame] | 3598 | |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 3599 | ret = set_page_extent_mapped(page); |
| 3600 | if (ret < 0) { |
| 3601 | unlock_extent(tree, start, end); |
Qu Wenruo | 92082d4 | 2021-02-02 10:28:36 +0800 | [diff] [blame] | 3602 | btrfs_page_set_error(fs_info, page, start, PAGE_SIZE); |
| 3603 | unlock_page(page); |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 3604 | goto out; |
| 3605 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3606 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3607 | if (page->index == last_byte >> PAGE_SHIFT) { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 3608 | size_t zero_offset = offset_in_page(last_byte); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3609 | |
| 3610 | if (zero_offset) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3611 | iosize = PAGE_SIZE - zero_offset; |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 3612 | memzero_page(page, zero_offset, iosize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3613 | } |
| 3614 | } |
Qu Wenruo | 92082d4 | 2021-02-02 10:28:36 +0800 | [diff] [blame] | 3615 | begin_page_read(fs_info, page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3616 | while (cur <= end) { |
Qu Wenruo | 4c37a79 | 2021-07-26 14:34:50 +0800 | [diff] [blame] | 3617 | unsigned long this_bio_flag = 0; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3618 | bool force_bio_submit = false; |
Qu Wenruo | 0c64c33 | 2021-01-06 09:01:40 +0800 | [diff] [blame] | 3619 | u64 disk_bytenr; |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 3620 | |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 3621 | ASSERT(IS_ALIGNED(cur, fs_info->sectorsize)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3622 | if (cur >= last_byte) { |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3623 | struct extent_state *cached = NULL; |
| 3624 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3625 | iosize = PAGE_SIZE - pg_offset; |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 3626 | memzero_page(page, pg_offset, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3627 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3628 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3629 | unlock_extent_cached(tree, cur, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 3630 | cur + iosize - 1, &cached); |
Qu Wenruo | 92082d4 | 2021-02-02 10:28:36 +0800 | [diff] [blame] | 3631 | end_page_read(page, true, cur, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3632 | break; |
| 3633 | } |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 3634 | em = __get_extent_map(inode, page, pg_offset, cur, |
Nikolay Borisov | 1a5ee1e | 2020-09-14 12:37:06 +0300 | [diff] [blame] | 3635 | end - cur + 1, em_cached); |
Filipe Manana | c034755 | 2022-02-03 15:36:42 +0000 | [diff] [blame] | 3636 | if (IS_ERR(em)) { |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3637 | unlock_extent(tree, cur, end); |
Qu Wenruo | 92082d4 | 2021-02-02 10:28:36 +0800 | [diff] [blame] | 3638 | end_page_read(page, false, cur, end + 1 - cur); |
Filipe Manana | bbf0ea7 | 2022-02-03 15:36:43 +0000 | [diff] [blame] | 3639 | ret = PTR_ERR(em); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3640 | break; |
| 3641 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3642 | extent_offset = cur - em->start; |
| 3643 | BUG_ON(extent_map_end(em) <= cur); |
| 3644 | BUG_ON(end < cur); |
| 3645 | |
David Sterba | 7f6ca7f | 2021-07-27 14:49:32 +0200 | [diff] [blame] | 3646 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 3647 | this_bio_flag = em->compress_type; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3648 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3649 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
| 3650 | cur_end = min(extent_map_end(em) - 1, end); |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3651 | iosize = ALIGN(iosize, blocksize); |
David Sterba | 2a5232a | 2021-07-27 14:47:09 +0200 | [diff] [blame] | 3652 | if (this_bio_flag != BTRFS_COMPRESS_NONE) |
Qu Wenruo | 0c64c33 | 2021-01-06 09:01:40 +0800 | [diff] [blame] | 3653 | disk_bytenr = em->block_start; |
Goldwyn Rodrigues | 949b327 | 2020-09-15 10:41:40 -0500 | [diff] [blame] | 3654 | else |
Qu Wenruo | 0c64c33 | 2021-01-06 09:01:40 +0800 | [diff] [blame] | 3655 | disk_bytenr = em->block_start + extent_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3656 | block_start = em->block_start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3657 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 3658 | block_start = EXTENT_MAP_HOLE; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3659 | |
| 3660 | /* |
| 3661 | * If we have a file range that points to a compressed extent |
Randy Dunlap | 260db43 | 2020-08-04 19:48:34 -0700 | [diff] [blame] | 3662 | * and it's followed by a consecutive file range that points |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3663 | * to the same compressed extent (possibly with a different |
| 3664 | * offset and/or length, so it either points to the whole extent |
| 3665 | * or only part of it), we must make sure we do not submit a |
| 3666 | * single bio to populate the pages for the 2 ranges because |
| 3667 | * this makes the compressed extent read zero out the pages |
| 3668 | * belonging to the 2nd range. Imagine the following scenario: |
| 3669 | * |
| 3670 | * File layout |
| 3671 | * [0 - 8K] [8K - 24K] |
| 3672 | * | | |
| 3673 | * | | |
| 3674 | * points to extent X, points to extent X, |
| 3675 | * offset 4K, length of 8K offset 0, length 16K |
| 3676 | * |
| 3677 | * [extent X, compressed length = 4K uncompressed length = 16K] |
| 3678 | * |
| 3679 | * If the bio to read the compressed extent covers both ranges, |
| 3680 | * it will decompress extent X into the pages belonging to the |
| 3681 | * first range and then it will stop, zeroing out the remaining |
| 3682 | * pages that belong to the other range that points to extent X. |
| 3683 | * So here we make sure we submit 2 bios, one for the first |
| 3684 | * range and another one for the third range. Both will target |
| 3685 | * the same physical extent from disk, but we can't currently |
| 3686 | * make the compressed bio endio callback populate the pages |
| 3687 | * for both ranges because each compressed bio is tightly |
| 3688 | * coupled with a single extent map, and each range can have |
| 3689 | * an extent map with a different offset value relative to the |
| 3690 | * uncompressed data of our extent and different lengths. This |
| 3691 | * is a corner case so we prioritize correctness over |
| 3692 | * non-optimal behavior (submitting 2 bios for the same extent). |
| 3693 | */ |
| 3694 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) && |
| 3695 | prev_em_start && *prev_em_start != (u64)-1 && |
Filipe Manana | 8e92821 | 2019-02-14 15:17:20 +0000 | [diff] [blame] | 3696 | *prev_em_start != em->start) |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3697 | force_bio_submit = true; |
| 3698 | |
| 3699 | if (prev_em_start) |
Filipe Manana | 8e92821 | 2019-02-14 15:17:20 +0000 | [diff] [blame] | 3700 | *prev_em_start = em->start; |
Filipe Manana | 005efed | 2015-09-14 09:09:31 +0100 | [diff] [blame] | 3701 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3702 | free_extent_map(em); |
| 3703 | em = NULL; |
| 3704 | |
| 3705 | /* we've found a hole, just zero and go on */ |
| 3706 | if (block_start == EXTENT_MAP_HOLE) { |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3707 | struct extent_state *cached = NULL; |
| 3708 | |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 3709 | memzero_page(page, pg_offset, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3710 | |
| 3711 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
Arne Jansen | 507903b | 2011-04-06 10:02:20 +0000 | [diff] [blame] | 3712 | &cached, GFP_NOFS); |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3713 | unlock_extent_cached(tree, cur, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 3714 | cur + iosize - 1, &cached); |
Qu Wenruo | 92082d4 | 2021-02-02 10:28:36 +0800 | [diff] [blame] | 3715 | end_page_read(page, true, cur, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3716 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3717 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3718 | continue; |
| 3719 | } |
| 3720 | /* the get_extent function already copied into the page */ |
Chris Mason | 9655d29 | 2009-09-02 15:22:30 -0400 | [diff] [blame] | 3721 | if (test_range_bit(tree, cur, cur_end, |
| 3722 | EXTENT_UPTODATE, 1, NULL)) { |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3723 | unlock_extent(tree, cur, cur + iosize - 1); |
Qu Wenruo | 92082d4 | 2021-02-02 10:28:36 +0800 | [diff] [blame] | 3724 | end_page_read(page, true, cur, iosize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3725 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3726 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3727 | continue; |
| 3728 | } |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3729 | /* we have an inline extent but it didn't get marked up |
| 3730 | * to date. Error out |
| 3731 | */ |
| 3732 | if (block_start == EXTENT_MAP_INLINE) { |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 3733 | unlock_extent(tree, cur, cur + iosize - 1); |
Qu Wenruo | 92082d4 | 2021-02-02 10:28:36 +0800 | [diff] [blame] | 3734 | end_page_read(page, false, cur, iosize); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3735 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3736 | pg_offset += iosize; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 3737 | continue; |
| 3738 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3739 | |
David Sterba | 0ceb34b | 2020-02-05 19:09:28 +0100 | [diff] [blame] | 3740 | ret = submit_extent_page(REQ_OP_READ | read_flags, NULL, |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3741 | bio_ctrl, page, disk_bytenr, iosize, |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 3742 | pg_offset, end_bio_extent_readpage, |
| 3743 | this_bio_flag, force_bio_submit); |
Filipe Manana | ad3fc794 | 2022-02-03 15:36:44 +0000 | [diff] [blame] | 3744 | if (ret) { |
Qu Wenruo | 10f7f6f | 2022-04-12 20:30:14 +0800 | [diff] [blame] | 3745 | /* |
| 3746 | * We have to unlock the remaining range, or the page |
| 3747 | * will never be unlocked. |
| 3748 | */ |
| 3749 | unlock_extent(tree, cur, end); |
| 3750 | end_page_read(page, false, cur, end + 1 - cur); |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3751 | goto out; |
Josef Bacik | edd33c9 | 2012-10-05 16:40:32 -0400 | [diff] [blame] | 3752 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3753 | cur = cur + iosize; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 3754 | pg_offset += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3755 | } |
Dan Magenheimer | 90a887c | 2011-05-26 10:01:56 -0600 | [diff] [blame] | 3756 | out: |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 3757 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3758 | } |
| 3759 | |
Linus Torvalds | fdaf9a5 | 2022-05-24 19:55:07 -0700 | [diff] [blame] | 3760 | int btrfs_read_folio(struct file *file, struct folio *folio) |
Christoph Hellwig | 7aab8b3 | 2022-04-15 16:33:24 +0200 | [diff] [blame] | 3761 | { |
Linus Torvalds | fdaf9a5 | 2022-05-24 19:55:07 -0700 | [diff] [blame] | 3762 | struct page *page = &folio->page; |
Christoph Hellwig | 7aab8b3 | 2022-04-15 16:33:24 +0200 | [diff] [blame] | 3763 | struct btrfs_inode *inode = BTRFS_I(page->mapping->host); |
| 3764 | u64 start = page_offset(page); |
| 3765 | u64 end = start + PAGE_SIZE - 1; |
| 3766 | struct btrfs_bio_ctrl bio_ctrl = { 0 }; |
| 3767 | int ret; |
| 3768 | |
| 3769 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
| 3770 | |
| 3771 | ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL); |
| 3772 | /* |
| 3773 | * If btrfs_do_readpage() failed we will want to submit the assembled |
| 3774 | * bio to do the cleanup. |
| 3775 | */ |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 3776 | submit_one_bio(&bio_ctrl); |
Christoph Hellwig | 7aab8b3 | 2022-04-15 16:33:24 +0200 | [diff] [blame] | 3777 | return ret; |
| 3778 | } |
| 3779 | |
David Sterba | b6660e8 | 2020-02-05 19:09:40 +0100 | [diff] [blame] | 3780 | static inline void contiguous_readpages(struct page *pages[], int nr_pages, |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3781 | u64 start, u64 end, |
| 3782 | struct extent_map **em_cached, |
| 3783 | struct btrfs_bio_ctrl *bio_ctrl, |
| 3784 | u64 *prev_em_start) |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3785 | { |
Nikolay Borisov | 23d31bd | 2019-05-07 10:19:23 +0300 | [diff] [blame] | 3786 | struct btrfs_inode *inode = BTRFS_I(pages[0]->mapping->host); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3787 | int index; |
| 3788 | |
David Sterba | b272ae2 | 2020-02-05 19:09:33 +0100 | [diff] [blame] | 3789 | btrfs_lock_and_flush_ordered_range(inode, start, end, NULL); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3790 | |
| 3791 | for (index = 0; index < nr_pages; index++) { |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 3792 | btrfs_do_readpage(pages[index], em_cached, bio_ctrl, |
Nikolay Borisov | 0f20881 | 2020-09-14 14:39:16 +0300 | [diff] [blame] | 3793 | REQ_RAHEAD, prev_em_start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3794 | put_page(pages[index]); |
Miao Xie | 9974090 | 2013-07-25 19:22:36 +0800 | [diff] [blame] | 3795 | } |
| 3796 | } |
| 3797 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3798 | /* |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3799 | * helper for __extent_writepage, doing all of the delayed allocation setup. |
| 3800 | * |
Nikolay Borisov | 5eaad97 | 2018-11-01 14:09:46 +0200 | [diff] [blame] | 3801 | * 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] | 3802 | * to write the page (copy into inline extent). In this case the IO has |
| 3803 | * been started and the page is already unlocked. |
| 3804 | * |
| 3805 | * This returns 0 if all went well (page still locked) |
| 3806 | * This returns < 0 if there were errors (page still locked) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3807 | */ |
Nikolay Borisov | cd4c0bf94 | 2020-06-05 10:42:10 +0300 | [diff] [blame] | 3808 | static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode, |
Qu Wenruo | 83f1b68 | 2021-11-12 13:33:14 +0800 | [diff] [blame] | 3809 | struct page *page, struct writeback_control *wbc) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3810 | { |
Qu Wenruo | 2749f7e | 2021-09-27 15:22:07 +0800 | [diff] [blame] | 3811 | const u64 page_end = page_offset(page) + PAGE_SIZE - 1; |
Qu Wenruo | cf3075f | 2021-09-27 15:21:44 +0800 | [diff] [blame] | 3812 | u64 delalloc_start = page_offset(page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3813 | u64 delalloc_to_write = 0; |
Qu Wenruo | 83f1b68 | 2021-11-12 13:33:14 +0800 | [diff] [blame] | 3814 | /* How many pages are started by btrfs_run_delalloc_range() */ |
| 3815 | unsigned long nr_written = 0; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3816 | int ret; |
| 3817 | int page_started = 0; |
| 3818 | |
Qu Wenruo | 2749f7e | 2021-09-27 15:22:07 +0800 | [diff] [blame] | 3819 | while (delalloc_start < page_end) { |
| 3820 | u64 delalloc_end = page_end; |
| 3821 | bool found; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3822 | |
Nikolay Borisov | cd4c0bf94 | 2020-06-05 10:42:10 +0300 | [diff] [blame] | 3823 | found = find_lock_delalloc_range(&inode->vfs_inode, page, |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3824 | &delalloc_start, |
Nikolay Borisov | 917aace | 2018-10-26 14:43:20 +0300 | [diff] [blame] | 3825 | &delalloc_end); |
Lu Fengqi | 3522e90 | 2018-11-29 11:33:38 +0800 | [diff] [blame] | 3826 | if (!found) { |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3827 | delalloc_start = delalloc_end + 1; |
| 3828 | continue; |
| 3829 | } |
Nikolay Borisov | cd4c0bf94 | 2020-06-05 10:42:10 +0300 | [diff] [blame] | 3830 | ret = btrfs_run_delalloc_range(inode, page, delalloc_start, |
Qu Wenruo | 83f1b68 | 2021-11-12 13:33:14 +0800 | [diff] [blame] | 3831 | delalloc_end, &page_started, &nr_written, wbc); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3832 | if (ret) { |
Qu Wenruo | 963e4db | 2021-07-26 14:35:07 +0800 | [diff] [blame] | 3833 | btrfs_page_set_error(inode->root->fs_info, page, |
| 3834 | page_offset(page), PAGE_SIZE); |
Qu Wenruo | 7361b4ae | 2021-07-28 14:05:05 +0800 | [diff] [blame] | 3835 | return ret; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3836 | } |
| 3837 | /* |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3838 | * delalloc_end is already one less than the total length, so |
| 3839 | * we don't subtract one from PAGE_SIZE |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3840 | */ |
| 3841 | delalloc_to_write += (delalloc_end - delalloc_start + |
Kirill A. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 3842 | PAGE_SIZE) >> PAGE_SHIFT; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3843 | delalloc_start = delalloc_end + 1; |
| 3844 | } |
| 3845 | if (wbc->nr_to_write < delalloc_to_write) { |
| 3846 | int thresh = 8192; |
| 3847 | |
| 3848 | if (delalloc_to_write < thresh * 2) |
| 3849 | thresh = delalloc_to_write; |
| 3850 | wbc->nr_to_write = min_t(u64, delalloc_to_write, |
| 3851 | thresh); |
| 3852 | } |
| 3853 | |
Qu Wenruo | 83f1b68 | 2021-11-12 13:33:14 +0800 | [diff] [blame] | 3854 | /* Did btrfs_run_dealloc_range() already unlock and start the IO? */ |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3855 | if (page_started) { |
| 3856 | /* |
Qu Wenruo | 83f1b68 | 2021-11-12 13:33:14 +0800 | [diff] [blame] | 3857 | * We've unlocked the page, so we can't update the mapping's |
| 3858 | * writeback index, just update nr_to_write. |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3859 | */ |
Qu Wenruo | 83f1b68 | 2021-11-12 13:33:14 +0800 | [diff] [blame] | 3860 | wbc->nr_to_write -= nr_written; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3861 | return 1; |
| 3862 | } |
| 3863 | |
Nikolay Borisov | b69d1ee | 2020-07-16 18:17:19 +0300 | [diff] [blame] | 3864 | return 0; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | /* |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 3868 | * Find the first byte we need to write. |
| 3869 | * |
| 3870 | * For subpage, one page can contain several sectors, and |
| 3871 | * __extent_writepage_io() will just grab all extent maps in the page |
| 3872 | * range and try to submit all non-inline/non-compressed extents. |
| 3873 | * |
| 3874 | * This is a big problem for subpage, we shouldn't re-submit already written |
| 3875 | * data at all. |
| 3876 | * This function will lookup subpage dirty bit to find which range we really |
| 3877 | * need to submit. |
| 3878 | * |
| 3879 | * Return the next dirty range in [@start, @end). |
| 3880 | * If no dirty range is found, @start will be page_offset(page) + PAGE_SIZE. |
| 3881 | */ |
| 3882 | static void find_next_dirty_byte(struct btrfs_fs_info *fs_info, |
| 3883 | struct page *page, u64 *start, u64 *end) |
| 3884 | { |
| 3885 | struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private; |
Qu Wenruo | 72a69cd | 2021-08-17 17:38:52 +0800 | [diff] [blame] | 3886 | struct btrfs_subpage_info *spi = fs_info->subpage_info; |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 3887 | u64 orig_start = *start; |
| 3888 | /* Declare as unsigned long so we can use bitmap ops */ |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 3889 | unsigned long flags; |
Qu Wenruo | 72a69cd | 2021-08-17 17:38:52 +0800 | [diff] [blame] | 3890 | int range_start_bit; |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 3891 | int range_end_bit; |
| 3892 | |
| 3893 | /* |
| 3894 | * For regular sector size == page size case, since one page only |
| 3895 | * contains one sector, we return the page offset directly. |
| 3896 | */ |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 3897 | if (!btrfs_is_subpage(fs_info, page)) { |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 3898 | *start = page_offset(page); |
| 3899 | *end = page_offset(page) + PAGE_SIZE; |
| 3900 | return; |
| 3901 | } |
| 3902 | |
Qu Wenruo | 72a69cd | 2021-08-17 17:38:52 +0800 | [diff] [blame] | 3903 | range_start_bit = spi->dirty_offset + |
| 3904 | (offset_in_page(orig_start) >> fs_info->sectorsize_bits); |
| 3905 | |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 3906 | /* We should have the page locked, but just in case */ |
| 3907 | spin_lock_irqsave(&subpage->lock, flags); |
Qu Wenruo | 72a69cd | 2021-08-17 17:38:52 +0800 | [diff] [blame] | 3908 | bitmap_next_set_region(subpage->bitmaps, &range_start_bit, &range_end_bit, |
| 3909 | spi->dirty_offset + spi->bitmap_nr_bits); |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 3910 | spin_unlock_irqrestore(&subpage->lock, flags); |
| 3911 | |
Qu Wenruo | 72a69cd | 2021-08-17 17:38:52 +0800 | [diff] [blame] | 3912 | range_start_bit -= spi->dirty_offset; |
| 3913 | range_end_bit -= spi->dirty_offset; |
| 3914 | |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 3915 | *start = page_offset(page) + range_start_bit * fs_info->sectorsize; |
| 3916 | *end = page_offset(page) + range_end_bit * fs_info->sectorsize; |
| 3917 | } |
| 3918 | |
| 3919 | /* |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3920 | * helper for __extent_writepage. This calls the writepage start hooks, |
| 3921 | * and does the loop to map the page into extents and bios. |
| 3922 | * |
| 3923 | * We return 1 if the IO is started and the page is unlocked, |
| 3924 | * 0 if all went well (page still locked) |
| 3925 | * < 0 if there were errors (page still locked) |
| 3926 | */ |
Nikolay Borisov | d4580fe | 2020-06-03 08:55:33 +0300 | [diff] [blame] | 3927 | static noinline_for_stack int __extent_writepage_io(struct btrfs_inode *inode, |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3928 | struct page *page, |
| 3929 | struct writeback_control *wbc, |
| 3930 | struct extent_page_data *epd, |
| 3931 | loff_t i_size, |
David Sterba | 57e5ffe | 2019-10-29 18:28:55 +0100 | [diff] [blame] | 3932 | int *nr_ret) |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3933 | { |
Qu Wenruo | 6bc5636 | 2021-01-06 09:01:41 +0800 | [diff] [blame] | 3934 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Qu Wenruo | a129ffb | 2021-07-27 13:41:32 +0800 | [diff] [blame] | 3935 | u64 cur = page_offset(page); |
| 3936 | u64 end = cur + PAGE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3937 | u64 extent_offset; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3938 | u64 block_start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3939 | struct extent_map *em; |
Qu Wenruo | 44e5801 | 2022-04-12 20:30:15 +0800 | [diff] [blame] | 3940 | int saved_ret = 0; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3941 | int ret = 0; |
| 3942 | int nr = 0; |
Bart Van Assche | bf9486d | 2022-07-14 11:07:16 -0700 | [diff] [blame] | 3943 | enum req_op op = REQ_OP_WRITE; |
| 3944 | const blk_opf_t write_flags = wbc_to_write_flags(wbc); |
Qu Wenruo | 44e5801 | 2022-04-12 20:30:15 +0800 | [diff] [blame] | 3945 | bool has_error = false; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3946 | bool compressed; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3947 | |
Qu Wenruo | a129ffb | 2021-07-27 13:41:32 +0800 | [diff] [blame] | 3948 | ret = btrfs_writepage_cow_fixup(page); |
Nikolay Borisov | d75855b | 2018-11-01 14:09:47 +0200 | [diff] [blame] | 3949 | if (ret) { |
| 3950 | /* Fixup worker will requeue */ |
Josef Bacik | 5ab5805 | 2020-01-21 11:51:43 -0500 | [diff] [blame] | 3951 | redirty_page_for_writepage(wbc, page); |
Nikolay Borisov | d75855b | 2018-11-01 14:09:47 +0200 | [diff] [blame] | 3952 | unlock_page(page); |
| 3953 | return 1; |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 3954 | } |
| 3955 | |
Chris Mason | 11c8349 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 3956 | /* |
| 3957 | * we don't want to touch the inode after unlocking the page, |
| 3958 | * so we update the mapping writeback index now |
| 3959 | */ |
David Sterba | 572f3da | 2021-07-27 12:45:11 +0200 | [diff] [blame] | 3960 | wbc->nr_to_write--; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 3961 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3962 | while (cur <= end) { |
Qu Wenruo | 0c64c33 | 2021-01-06 09:01:40 +0800 | [diff] [blame] | 3963 | u64 disk_bytenr; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3964 | u64 em_end; |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 3965 | u64 dirty_range_start = cur; |
| 3966 | u64 dirty_range_end; |
Qu Wenruo | 6bc5636 | 2021-01-06 09:01:41 +0800 | [diff] [blame] | 3967 | u32 iosize; |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 3968 | |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 3969 | if (cur >= i_size) { |
Qu Wenruo | 38a39ac7 | 2021-04-08 20:32:27 +0800 | [diff] [blame] | 3970 | btrfs_writepage_endio_finish_ordered(inode, page, cur, |
David Sterba | 25c1252 | 2021-07-26 14:15:08 +0200 | [diff] [blame] | 3971 | end, true); |
Qu Wenruo | cc1d0d9 | 2021-07-26 14:34:58 +0800 | [diff] [blame] | 3972 | /* |
| 3973 | * This range is beyond i_size, thus we don't need to |
| 3974 | * bother writing back. |
| 3975 | * But we still need to clear the dirty subpage bit, or |
| 3976 | * the next time the page gets dirtied, we will try to |
| 3977 | * writeback the sectors with subpage dirty bits, |
| 3978 | * causing writeback without ordered extent. |
| 3979 | */ |
| 3980 | btrfs_page_clear_dirty(fs_info, page, cur, end + 1 - cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3981 | break; |
| 3982 | } |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 3983 | |
| 3984 | find_next_dirty_byte(fs_info, page, &dirty_range_start, |
| 3985 | &dirty_range_end); |
| 3986 | if (cur < dirty_range_start) { |
| 3987 | cur = dirty_range_start; |
| 3988 | continue; |
| 3989 | } |
| 3990 | |
Nikolay Borisov | d4580fe | 2020-06-03 08:55:33 +0300 | [diff] [blame] | 3991 | em = btrfs_get_extent(inode, NULL, 0, cur, end - cur + 1); |
Filipe Manana | c034755 | 2022-02-03 15:36:42 +0000 | [diff] [blame] | 3992 | if (IS_ERR(em)) { |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 3993 | btrfs_page_set_error(fs_info, page, cur, end - cur + 1); |
Filipe Manana | 61391d5 | 2014-05-09 17:17:40 +0100 | [diff] [blame] | 3994 | ret = PTR_ERR_OR_ZERO(em); |
Qu Wenruo | 44e5801 | 2022-04-12 20:30:15 +0800 | [diff] [blame] | 3995 | has_error = true; |
| 3996 | if (!saved_ret) |
| 3997 | saved_ret = ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3998 | break; |
| 3999 | } |
| 4000 | |
| 4001 | extent_offset = cur - em->start; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4002 | em_end = extent_map_end(em); |
Qu Wenruo | 6bc5636 | 2021-01-06 09:01:41 +0800 | [diff] [blame] | 4003 | ASSERT(cur <= em_end); |
| 4004 | ASSERT(cur < end); |
| 4005 | ASSERT(IS_ALIGNED(em->start, fs_info->sectorsize)); |
| 4006 | ASSERT(IS_ALIGNED(em->len, fs_info->sectorsize)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4007 | block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4008 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Qu Wenruo | 6bc5636 | 2021-01-06 09:01:41 +0800 | [diff] [blame] | 4009 | disk_bytenr = em->block_start + extent_offset; |
| 4010 | |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 4011 | /* |
| 4012 | * Note that em_end from extent_map_end() and dirty_range_end from |
| 4013 | * find_next_dirty_byte() are all exclusive |
| 4014 | */ |
| 4015 | iosize = min(min(em_end, end + 1), dirty_range_end) - cur; |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 4016 | |
Johannes Thumshirn | e380adf | 2021-05-19 00:40:27 +0900 | [diff] [blame] | 4017 | if (btrfs_use_zone_append(inode, em->block_start)) |
Bart Van Assche | bf9486d | 2022-07-14 11:07:16 -0700 | [diff] [blame] | 4018 | op = REQ_OP_ZONE_APPEND; |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 4019 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4020 | free_extent_map(em); |
| 4021 | em = NULL; |
| 4022 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4023 | /* |
| 4024 | * compressed and inline extents are written through other |
| 4025 | * paths in the FS |
| 4026 | */ |
| 4027 | if (compressed || block_start == EXTENT_MAP_HOLE || |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4028 | block_start == EXTENT_MAP_INLINE) { |
Omar Sandoval | c8b0403 | 2019-12-02 17:34:24 -0800 | [diff] [blame] | 4029 | if (compressed) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4030 | nr++; |
Omar Sandoval | c8b0403 | 2019-12-02 17:34:24 -0800 | [diff] [blame] | 4031 | else |
Qu Wenruo | 38a39ac7 | 2021-04-08 20:32:27 +0800 | [diff] [blame] | 4032 | btrfs_writepage_endio_finish_ordered(inode, |
David Sterba | 25c1252 | 2021-07-26 14:15:08 +0200 | [diff] [blame] | 4033 | page, cur, cur + iosize - 1, true); |
Qu Wenruo | cc1d0d9 | 2021-07-26 14:34:58 +0800 | [diff] [blame] | 4034 | btrfs_page_clear_dirty(fs_info, page, cur, iosize); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4035 | cur += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4036 | continue; |
| 4037 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4038 | |
Qu Wenruo | d2a9106 | 2021-05-31 16:50:49 +0800 | [diff] [blame] | 4039 | btrfs_set_range_writeback(inode, cur, cur + iosize - 1); |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 4040 | if (!PageWriteback(page)) { |
Nikolay Borisov | d4580fe | 2020-06-03 08:55:33 +0300 | [diff] [blame] | 4041 | btrfs_err(inode->root->fs_info, |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 4042 | "page %lu not writeback, cur %llu end %llu", |
| 4043 | page->index, cur, end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4044 | } |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 4045 | |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 4046 | /* |
| 4047 | * Although the PageDirty bit is cleared before entering this |
| 4048 | * function, subpage dirty bit is not cleared. |
| 4049 | * So clear subpage dirty bit here so next time we won't submit |
| 4050 | * page for range already written to disk. |
| 4051 | */ |
| 4052 | btrfs_page_clear_dirty(fs_info, page, cur, iosize); |
| 4053 | |
Bart Van Assche | bf9486d | 2022-07-14 11:07:16 -0700 | [diff] [blame] | 4054 | ret = submit_extent_page(op | write_flags, wbc, |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 4055 | &epd->bio_ctrl, page, |
Naohiro Aota | d8e3fb1 | 2021-02-04 19:22:05 +0900 | [diff] [blame] | 4056 | disk_bytenr, iosize, |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 4057 | cur - page_offset(page), |
David Sterba | 58409ed | 2016-05-04 11:46:10 +0200 | [diff] [blame] | 4058 | end_bio_extent_writepage, |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 4059 | 0, false); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 4060 | if (ret) { |
Qu Wenruo | 44e5801 | 2022-04-12 20:30:15 +0800 | [diff] [blame] | 4061 | has_error = true; |
| 4062 | if (!saved_ret) |
| 4063 | saved_ret = ret; |
| 4064 | |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 4065 | btrfs_page_set_error(fs_info, page, cur, iosize); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 4066 | if (PageWriteback(page)) |
Qu Wenruo | c5ef5c6 | 2021-05-31 16:50:50 +0800 | [diff] [blame] | 4067 | btrfs_page_clear_writeback(fs_info, page, cur, |
| 4068 | iosize); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 4069 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 4070 | |
Qu Wenruo | 6bc5636 | 2021-01-06 09:01:41 +0800 | [diff] [blame] | 4071 | cur += iosize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4072 | nr++; |
| 4073 | } |
Qu Wenruo | cc1d0d9 | 2021-07-26 14:34:58 +0800 | [diff] [blame] | 4074 | /* |
| 4075 | * If we finish without problem, we should not only clear page dirty, |
| 4076 | * but also empty subpage dirty bits |
| 4077 | */ |
Qu Wenruo | 44e5801 | 2022-04-12 20:30:15 +0800 | [diff] [blame] | 4078 | if (!has_error) |
Qu Wenruo | cc1d0d9 | 2021-07-26 14:34:58 +0800 | [diff] [blame] | 4079 | btrfs_page_assert_not_dirty(fs_info, page); |
Qu Wenruo | 44e5801 | 2022-04-12 20:30:15 +0800 | [diff] [blame] | 4080 | else |
| 4081 | ret = saved_ret; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4082 | *nr_ret = nr; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4083 | return ret; |
| 4084 | } |
| 4085 | |
| 4086 | /* |
| 4087 | * the writepage semantics are similar to regular writepage. extent |
| 4088 | * records are inserted to lock ranges in the tree, and as dirty areas |
| 4089 | * are found, they are marked writeback. Then the lock bits are removed |
| 4090 | * and the end_io handler clears the writeback ranges |
Qu Wenruo | 3065976 | 2019-03-20 14:27:42 +0800 | [diff] [blame] | 4091 | * |
| 4092 | * Return 0 if everything goes well. |
| 4093 | * Return <0 for error. |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4094 | */ |
| 4095 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 4096 | struct extent_page_data *epd) |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4097 | { |
Matthew Wilcox (Oracle) | 8e1dec8 | 2022-02-09 20:21:29 +0000 | [diff] [blame] | 4098 | struct folio *folio = page_folio(page); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4099 | struct inode *inode = page->mapping->host; |
Qu Wenruo | e55a0de | 2021-09-27 15:22:05 +0800 | [diff] [blame] | 4100 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Qu Wenruo | cf3075f | 2021-09-27 15:21:44 +0800 | [diff] [blame] | 4101 | const u64 page_start = page_offset(page); |
| 4102 | const u64 page_end = page_start + PAGE_SIZE - 1; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4103 | int ret; |
| 4104 | int nr = 0; |
Omar Sandoval | eb70d22 | 2019-12-02 17:34:20 -0800 | [diff] [blame] | 4105 | size_t pg_offset; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4106 | loff_t i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4107 | unsigned long end_index = i_size >> PAGE_SHIFT; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4108 | |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4109 | trace___extent_writepage(page, inode, wbc); |
| 4110 | |
| 4111 | WARN_ON(!PageLocked(page)); |
| 4112 | |
Qu Wenruo | 963e4db | 2021-07-26 14:35:07 +0800 | [diff] [blame] | 4113 | btrfs_page_clear_error(btrfs_sb(inode->i_sb), page, |
| 4114 | page_offset(page), PAGE_SIZE); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4115 | |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 4116 | pg_offset = offset_in_page(i_size); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4117 | if (page->index > end_index || |
| 4118 | (page->index == end_index && !pg_offset)) { |
Matthew Wilcox (Oracle) | 8e1dec8 | 2022-02-09 20:21:29 +0000 | [diff] [blame] | 4119 | folio_invalidate(folio, 0, folio_size(folio)); |
| 4120 | folio_unlock(folio); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4121 | return 0; |
| 4122 | } |
| 4123 | |
David Sterba | 21a8935 | 2022-06-01 13:47:54 +0200 | [diff] [blame] | 4124 | if (page->index == end_index) |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 4125 | memzero_page(page, pg_offset, PAGE_SIZE - pg_offset); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4126 | |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 4127 | ret = set_page_extent_mapped(page); |
| 4128 | if (ret < 0) { |
| 4129 | SetPageError(page); |
| 4130 | goto done; |
| 4131 | } |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4132 | |
Nikolay Borisov | 7789a55 | 2018-11-08 10:18:06 +0200 | [diff] [blame] | 4133 | if (!epd->extent_locked) { |
Qu Wenruo | 83f1b68 | 2021-11-12 13:33:14 +0800 | [diff] [blame] | 4134 | ret = writepage_delalloc(BTRFS_I(inode), page, wbc); |
Nikolay Borisov | 7789a55 | 2018-11-08 10:18:06 +0200 | [diff] [blame] | 4135 | if (ret == 1) |
Omar Sandoval | 169d2c8 | 2019-12-02 17:34:21 -0800 | [diff] [blame] | 4136 | return 0; |
Nikolay Borisov | 7789a55 | 2018-11-08 10:18:06 +0200 | [diff] [blame] | 4137 | if (ret) |
| 4138 | goto done; |
| 4139 | } |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4140 | |
Nikolay Borisov | d4580fe | 2020-06-03 08:55:33 +0300 | [diff] [blame] | 4141 | ret = __extent_writepage_io(BTRFS_I(inode), page, wbc, epd, i_size, |
Qu Wenruo | 83f1b68 | 2021-11-12 13:33:14 +0800 | [diff] [blame] | 4142 | &nr); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4143 | if (ret == 1) |
Omar Sandoval | 169d2c8 | 2019-12-02 17:34:21 -0800 | [diff] [blame] | 4144 | return 0; |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4145 | |
| 4146 | done: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4147 | if (nr == 0) { |
| 4148 | /* make sure the mapping tag for page dirty gets cleared */ |
| 4149 | set_page_writeback(page); |
| 4150 | end_page_writeback(page); |
| 4151 | } |
Qu Wenruo | 963e4db | 2021-07-26 14:35:07 +0800 | [diff] [blame] | 4152 | /* |
| 4153 | * Here we used to have a check for PageError() and then set @ret and |
| 4154 | * call end_extent_writepage(). |
| 4155 | * |
| 4156 | * But in fact setting @ret here will cause different error paths |
| 4157 | * between subpage and regular sectorsize. |
| 4158 | * |
| 4159 | * For regular page size, we never submit current page, but only add |
| 4160 | * current page to current bio. |
| 4161 | * The bio submission can only happen in next page. |
| 4162 | * Thus if we hit the PageError() branch, @ret is already set to |
| 4163 | * non-zero value and will not get updated for regular sectorsize. |
| 4164 | * |
| 4165 | * But for subpage case, it's possible we submit part of current page, |
| 4166 | * thus can get PageError() set by submitted bio of the same page, |
| 4167 | * while our @ret is still 0. |
| 4168 | * |
| 4169 | * So here we unify the behavior and don't set @ret. |
| 4170 | * Error can still be properly passed to higher layer as page will |
| 4171 | * be set error, here we just don't handle the IO failure. |
| 4172 | * |
| 4173 | * NOTE: This is just a hotfix for subpage. |
| 4174 | * The root fix will be properly ending ordered extent when we hit |
| 4175 | * an error during writeback. |
| 4176 | * |
| 4177 | * But that needs a bigger refactoring, as we not only need to grab the |
| 4178 | * submitted OE, but also need to know exactly at which bytenr we hit |
| 4179 | * the error. |
| 4180 | * Currently the full page based __extent_writepage_io() is not |
| 4181 | * capable of that. |
| 4182 | */ |
| 4183 | if (PageError(page)) |
Qu Wenruo | cf3075f | 2021-09-27 15:21:44 +0800 | [diff] [blame] | 4184 | end_extent_writepage(page, ret, page_start, page_end); |
Qu Wenruo | e55a0de | 2021-09-27 15:22:05 +0800 | [diff] [blame] | 4185 | if (epd->extent_locked) { |
| 4186 | /* |
| 4187 | * If epd->extent_locked, it's from extent_write_locked_range(), |
| 4188 | * the page can either be locked by lock_page() or |
| 4189 | * process_one_page(). |
| 4190 | * Let btrfs_page_unlock_writer() handle both cases. |
| 4191 | */ |
| 4192 | ASSERT(wbc); |
| 4193 | btrfs_page_unlock_writer(fs_info, page, wbc->range_start, |
| 4194 | wbc->range_end + 1 - wbc->range_start); |
| 4195 | } else { |
| 4196 | unlock_page(page); |
| 4197 | } |
Qu Wenruo | 3065976 | 2019-03-20 14:27:42 +0800 | [diff] [blame] | 4198 | ASSERT(ret <= 0); |
Chris Mason | 40f7658 | 2014-05-21 13:35:51 -0700 | [diff] [blame] | 4199 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4200 | } |
| 4201 | |
Josef Bacik | fd8b2b6 | 2013-04-24 16:41:19 -0400 | [diff] [blame] | 4202 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4203 | { |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 4204 | wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK, |
| 4205 | TASK_UNINTERRUPTIBLE); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4206 | } |
| 4207 | |
Filipe Manana | 18dfa71 | 2019-09-11 17:42:00 +0100 | [diff] [blame] | 4208 | static void end_extent_buffer_writeback(struct extent_buffer *eb) |
| 4209 | { |
| 4210 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
| 4211 | smp_mb__after_atomic(); |
| 4212 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); |
| 4213 | } |
| 4214 | |
Qu Wenruo | 2e3c251 | 2019-03-20 14:27:46 +0800 | [diff] [blame] | 4215 | /* |
Qu Wenruo | a3efb2f | 2020-10-21 14:24:49 +0800 | [diff] [blame] | 4216 | * Lock extent buffer status and pages for writeback. |
Qu Wenruo | 2e3c251 | 2019-03-20 14:27:46 +0800 | [diff] [blame] | 4217 | * |
Qu Wenruo | a3efb2f | 2020-10-21 14:24:49 +0800 | [diff] [blame] | 4218 | * May try to flush write bio if we can't get the lock. |
| 4219 | * |
| 4220 | * Return 0 if the extent buffer doesn't need to be submitted. |
| 4221 | * (E.g. the extent buffer is not dirty) |
| 4222 | * Return >0 is the extent buffer is submitted to bio. |
| 4223 | * Return <0 if something went wrong, no page is locked. |
Qu Wenruo | 2e3c251 | 2019-03-20 14:27:46 +0800 | [diff] [blame] | 4224 | */ |
David Sterba | 9df76fb | 2019-03-20 11:21:41 +0100 | [diff] [blame] | 4225 | 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] | 4226 | struct extent_page_data *epd) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4227 | { |
David Sterba | 9df76fb | 2019-03-20 11:21:41 +0100 | [diff] [blame] | 4228 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Qu Wenruo | c9583ad | 2022-04-12 20:30:13 +0800 | [diff] [blame] | 4229 | int i, num_pages; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4230 | int flush = 0; |
| 4231 | int ret = 0; |
| 4232 | |
| 4233 | if (!btrfs_try_tree_write_lock(eb)) { |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 4234 | submit_write_bio(epd, 0); |
Qu Wenruo | 2e3c251 | 2019-03-20 14:27:46 +0800 | [diff] [blame] | 4235 | flush = 1; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4236 | btrfs_tree_lock(eb); |
| 4237 | } |
| 4238 | |
| 4239 | if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { |
| 4240 | btrfs_tree_unlock(eb); |
| 4241 | if (!epd->sync_io) |
| 4242 | return 0; |
| 4243 | if (!flush) { |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 4244 | submit_write_bio(epd, 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4245 | flush = 1; |
| 4246 | } |
Chris Mason | a098d8e8 | 2012-03-21 12:09:56 -0400 | [diff] [blame] | 4247 | while (1) { |
| 4248 | wait_on_extent_buffer_writeback(eb); |
| 4249 | btrfs_tree_lock(eb); |
| 4250 | if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) |
| 4251 | break; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4252 | btrfs_tree_unlock(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4253 | } |
| 4254 | } |
| 4255 | |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 4256 | /* |
| 4257 | * We need to do this to prevent races in people who check if the eb is |
| 4258 | * under IO since we can end up having no IO bits set for a short period |
| 4259 | * of time. |
| 4260 | */ |
| 4261 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4262 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
| 4263 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 4264 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4265 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 4266 | percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, |
| 4267 | -eb->len, |
| 4268 | fs_info->dirty_metadata_batch); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4269 | ret = 1; |
Josef Bacik | 51561ff | 2012-07-20 16:25:24 -0400 | [diff] [blame] | 4270 | } else { |
| 4271 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4272 | } |
| 4273 | |
| 4274 | btrfs_tree_unlock(eb); |
| 4275 | |
Qu Wenruo | f3156df | 2021-04-06 08:36:02 +0800 | [diff] [blame] | 4276 | /* |
| 4277 | * Either we don't need to submit any tree block, or we're submitting |
| 4278 | * subpage eb. |
| 4279 | * Subpage metadata doesn't use page locking at all, so we can skip |
| 4280 | * the page locking. |
| 4281 | */ |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 4282 | if (!ret || fs_info->nodesize < PAGE_SIZE) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4283 | return ret; |
| 4284 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 4285 | num_pages = num_extent_pages(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4286 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4287 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4288 | |
| 4289 | if (!trylock_page(p)) { |
| 4290 | if (!flush) { |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 4291 | submit_write_bio(epd, 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4292 | flush = 1; |
| 4293 | } |
| 4294 | lock_page(p); |
| 4295 | } |
| 4296 | } |
| 4297 | |
| 4298 | return ret; |
| 4299 | } |
| 4300 | |
Qu Wenruo | 5a2c607 | 2021-03-25 15:14:44 +0800 | [diff] [blame] | 4301 | static void set_btree_ioerr(struct page *page, struct extent_buffer *eb) |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4302 | { |
Qu Wenruo | 5a2c607 | 2021-03-25 15:14:44 +0800 | [diff] [blame] | 4303 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4304 | |
Qu Wenruo | 5a2c607 | 2021-03-25 15:14:44 +0800 | [diff] [blame] | 4305 | btrfs_page_set_error(fs_info, page, eb->start, eb->len); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4306 | if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) |
| 4307 | return; |
| 4308 | |
| 4309 | /* |
Josef Bacik | c2e3930 | 2021-11-24 14:14:23 -0500 | [diff] [blame] | 4310 | * A read may stumble upon this buffer later, make sure that it gets an |
| 4311 | * error and knows there was an error. |
| 4312 | */ |
| 4313 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
| 4314 | |
| 4315 | /* |
Josef Bacik | 68b8558 | 2021-11-24 14:14:25 -0500 | [diff] [blame] | 4316 | * We need to set the mapping with the io error as well because a write |
| 4317 | * error will flip the file system readonly, and then syncfs() will |
| 4318 | * return a 0 because we are readonly if we don't modify the err seq for |
| 4319 | * the superblock. |
| 4320 | */ |
| 4321 | mapping_set_error(page->mapping, -EIO); |
| 4322 | |
| 4323 | /* |
Dennis Zhou | eb5b64f | 2019-09-13 14:54:07 +0100 | [diff] [blame] | 4324 | * If we error out, we should add back the dirty_metadata_bytes |
| 4325 | * to make it consistent. |
| 4326 | */ |
Dennis Zhou | eb5b64f | 2019-09-13 14:54:07 +0100 | [diff] [blame] | 4327 | percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, |
| 4328 | eb->len, fs_info->dirty_metadata_batch); |
| 4329 | |
| 4330 | /* |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4331 | * If writeback for a btree extent that doesn't belong to a log tree |
| 4332 | * failed, increment the counter transaction->eb_write_errors. |
| 4333 | * We do this because while the transaction is running and before it's |
| 4334 | * committing (when we call filemap_fdata[write|wait]_range against |
| 4335 | * the btree inode), we might have |
| 4336 | * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it |
| 4337 | * returns an error or an error happens during writeback, when we're |
| 4338 | * committing the transaction we wouldn't know about it, since the pages |
| 4339 | * can be no longer dirty nor marked anymore for writeback (if a |
| 4340 | * subsequent modification to the extent buffer didn't happen before the |
| 4341 | * transaction commit), which makes filemap_fdata[write|wait]_range not |
| 4342 | * able to find the pages tagged with SetPageError at transaction |
| 4343 | * commit time. So if this happens we must abort the transaction, |
| 4344 | * otherwise we commit a super block with btree roots that point to |
| 4345 | * btree nodes/leafs whose content on disk is invalid - either garbage |
| 4346 | * or the content of some node/leaf from a past generation that got |
| 4347 | * cowed or deleted and is no longer valid. |
| 4348 | * |
| 4349 | * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would |
| 4350 | * not be enough - we need to distinguish between log tree extents vs |
| 4351 | * non-log tree extents, and the next filemap_fdatawait_range() call |
| 4352 | * will catch and clear such errors in the mapping - and that call might |
| 4353 | * be from a log sync and not from a transaction commit. Also, checking |
| 4354 | * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is |
| 4355 | * not done and would not be reliable - the eb might have been released |
| 4356 | * from memory and reading it back again means that flag would not be |
| 4357 | * set (since it's a runtime flag, not persisted on disk). |
| 4358 | * |
| 4359 | * Using the flags below in the btree inode also makes us achieve the |
| 4360 | * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started |
| 4361 | * writeback for all dirty pages and before filemap_fdatawait_range() |
| 4362 | * is called, the writeback for all dirty pages had already finished |
| 4363 | * with errors - because we were not using AS_EIO/AS_ENOSPC, |
| 4364 | * filemap_fdatawait_range() would return success, as it could not know |
| 4365 | * that writeback errors happened (the pages were no longer tagged for |
| 4366 | * writeback). |
| 4367 | */ |
| 4368 | switch (eb->log_index) { |
| 4369 | case -1: |
Qu Wenruo | 5a2c607 | 2021-03-25 15:14:44 +0800 | [diff] [blame] | 4370 | set_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4371 | break; |
| 4372 | case 0: |
Qu Wenruo | 5a2c607 | 2021-03-25 15:14:44 +0800 | [diff] [blame] | 4373 | set_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4374 | break; |
| 4375 | case 1: |
Qu Wenruo | 5a2c607 | 2021-03-25 15:14:44 +0800 | [diff] [blame] | 4376 | set_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4377 | break; |
| 4378 | default: |
| 4379 | BUG(); /* unexpected, logic error */ |
| 4380 | } |
| 4381 | } |
| 4382 | |
Qu Wenruo | 2f3186d | 2021-04-06 08:36:00 +0800 | [diff] [blame] | 4383 | /* |
| 4384 | * The endio specific version which won't touch any unsafe spinlock in endio |
| 4385 | * context. |
| 4386 | */ |
| 4387 | static struct extent_buffer *find_extent_buffer_nolock( |
| 4388 | struct btrfs_fs_info *fs_info, u64 start) |
| 4389 | { |
| 4390 | struct extent_buffer *eb; |
| 4391 | |
| 4392 | rcu_read_lock(); |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 4393 | eb = radix_tree_lookup(&fs_info->buffer_radix, |
| 4394 | start >> fs_info->sectorsize_bits); |
Qu Wenruo | 2f3186d | 2021-04-06 08:36:00 +0800 | [diff] [blame] | 4395 | if (eb && atomic_inc_not_zero(&eb->refs)) { |
| 4396 | rcu_read_unlock(); |
| 4397 | return eb; |
| 4398 | } |
| 4399 | rcu_read_unlock(); |
| 4400 | return NULL; |
| 4401 | } |
| 4402 | |
| 4403 | /* |
| 4404 | * The endio function for subpage extent buffer write. |
| 4405 | * |
| 4406 | * Unlike end_bio_extent_buffer_writepage(), we only call end_page_writeback() |
| 4407 | * after all extent buffers in the page has finished their writeback. |
| 4408 | */ |
Qu Wenruo | fa04c16 | 2021-04-27 12:53:35 +0800 | [diff] [blame] | 4409 | static void end_bio_subpage_eb_writepage(struct bio *bio) |
Qu Wenruo | 2f3186d | 2021-04-06 08:36:00 +0800 | [diff] [blame] | 4410 | { |
Qu Wenruo | fa04c16 | 2021-04-27 12:53:35 +0800 | [diff] [blame] | 4411 | struct btrfs_fs_info *fs_info; |
Qu Wenruo | 2f3186d | 2021-04-06 08:36:00 +0800 | [diff] [blame] | 4412 | struct bio_vec *bvec; |
| 4413 | struct bvec_iter_all iter_all; |
| 4414 | |
Qu Wenruo | fa04c16 | 2021-04-27 12:53:35 +0800 | [diff] [blame] | 4415 | fs_info = btrfs_sb(bio_first_page_all(bio)->mapping->host->i_sb); |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 4416 | ASSERT(fs_info->nodesize < PAGE_SIZE); |
Qu Wenruo | fa04c16 | 2021-04-27 12:53:35 +0800 | [diff] [blame] | 4417 | |
Qu Wenruo | 2f3186d | 2021-04-06 08:36:00 +0800 | [diff] [blame] | 4418 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
| 4419 | bio_for_each_segment_all(bvec, bio, iter_all) { |
| 4420 | struct page *page = bvec->bv_page; |
| 4421 | u64 bvec_start = page_offset(page) + bvec->bv_offset; |
| 4422 | u64 bvec_end = bvec_start + bvec->bv_len - 1; |
| 4423 | u64 cur_bytenr = bvec_start; |
| 4424 | |
| 4425 | ASSERT(IS_ALIGNED(bvec->bv_len, fs_info->nodesize)); |
| 4426 | |
| 4427 | /* Iterate through all extent buffers in the range */ |
| 4428 | while (cur_bytenr <= bvec_end) { |
| 4429 | struct extent_buffer *eb; |
| 4430 | int done; |
| 4431 | |
| 4432 | /* |
| 4433 | * Here we can't use find_extent_buffer(), as it may |
| 4434 | * try to lock eb->refs_lock, which is not safe in endio |
| 4435 | * context. |
| 4436 | */ |
| 4437 | eb = find_extent_buffer_nolock(fs_info, cur_bytenr); |
| 4438 | ASSERT(eb); |
| 4439 | |
| 4440 | cur_bytenr = eb->start + eb->len; |
| 4441 | |
| 4442 | ASSERT(test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)); |
| 4443 | done = atomic_dec_and_test(&eb->io_pages); |
| 4444 | ASSERT(done); |
| 4445 | |
| 4446 | if (bio->bi_status || |
| 4447 | test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) { |
| 4448 | ClearPageUptodate(page); |
| 4449 | set_btree_ioerr(page, eb); |
| 4450 | } |
| 4451 | |
| 4452 | btrfs_subpage_clear_writeback(fs_info, page, eb->start, |
| 4453 | eb->len); |
| 4454 | end_extent_buffer_writeback(eb); |
| 4455 | /* |
| 4456 | * free_extent_buffer() will grab spinlock which is not |
| 4457 | * safe in endio context. Thus here we manually dec |
| 4458 | * the ref. |
| 4459 | */ |
| 4460 | atomic_dec(&eb->refs); |
| 4461 | } |
| 4462 | } |
| 4463 | bio_put(bio); |
| 4464 | } |
| 4465 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 4466 | static void end_bio_extent_buffer_writepage(struct bio *bio) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4467 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 4468 | struct bio_vec *bvec; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4469 | struct extent_buffer *eb; |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 4470 | int done; |
Ming Lei | 6dc4f10 | 2019-02-15 19:13:19 +0800 | [diff] [blame] | 4471 | struct bvec_iter_all iter_all; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4472 | |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 4473 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Christoph Hellwig | 2b070cf | 2019-04-25 09:03:00 +0200 | [diff] [blame] | 4474 | bio_for_each_segment_all(bvec, bio, iter_all) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4475 | struct page *page = bvec->bv_page; |
| 4476 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4477 | eb = (struct extent_buffer *)page->private; |
| 4478 | BUG_ON(!eb); |
| 4479 | done = atomic_dec_and_test(&eb->io_pages); |
| 4480 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 4481 | if (bio->bi_status || |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 4482 | test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4483 | ClearPageUptodate(page); |
Qu Wenruo | 5a2c607 | 2021-03-25 15:14:44 +0800 | [diff] [blame] | 4484 | set_btree_ioerr(page, eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4485 | } |
| 4486 | |
| 4487 | end_page_writeback(page); |
| 4488 | |
| 4489 | if (!done) |
| 4490 | continue; |
| 4491 | |
| 4492 | end_extent_buffer_writeback(eb); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 4493 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4494 | |
| 4495 | bio_put(bio); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4496 | } |
| 4497 | |
Qu Wenruo | fa04c16 | 2021-04-27 12:53:35 +0800 | [diff] [blame] | 4498 | static void prepare_eb_write(struct extent_buffer *eb) |
| 4499 | { |
| 4500 | u32 nritems; |
| 4501 | unsigned long start; |
| 4502 | unsigned long end; |
| 4503 | |
| 4504 | clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags); |
| 4505 | atomic_set(&eb->io_pages, num_extent_pages(eb)); |
| 4506 | |
| 4507 | /* Set btree blocks beyond nritems with 0 to avoid stale content */ |
| 4508 | nritems = btrfs_header_nritems(eb); |
| 4509 | if (btrfs_header_level(eb) > 0) { |
| 4510 | end = btrfs_node_key_ptr_offset(nritems); |
| 4511 | memzero_extent_buffer(eb, end, eb->len - end); |
| 4512 | } else { |
| 4513 | /* |
| 4514 | * Leaf: |
| 4515 | * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0 |
| 4516 | */ |
| 4517 | start = btrfs_item_nr_offset(nritems); |
| 4518 | end = BTRFS_LEAF_DATA_OFFSET + leaf_data_end(eb); |
| 4519 | memzero_extent_buffer(eb, start, end - start); |
| 4520 | } |
| 4521 | } |
| 4522 | |
Qu Wenruo | 35b6ddf | 2021-04-06 08:36:01 +0800 | [diff] [blame] | 4523 | /* |
| 4524 | * Unlike the work in write_one_eb(), we rely completely on extent locking. |
| 4525 | * Page locking is only utilized at minimum to keep the VMM code happy. |
Qu Wenruo | 35b6ddf | 2021-04-06 08:36:01 +0800 | [diff] [blame] | 4526 | */ |
| 4527 | static int write_one_subpage_eb(struct extent_buffer *eb, |
| 4528 | struct writeback_control *wbc, |
| 4529 | struct extent_page_data *epd) |
| 4530 | { |
| 4531 | struct btrfs_fs_info *fs_info = eb->fs_info; |
| 4532 | struct page *page = eb->pages[0]; |
Linus Torvalds | 353767e | 2022-08-03 14:54:52 -0700 | [diff] [blame] | 4533 | blk_opf_t write_flags = wbc_to_write_flags(wbc); |
Qu Wenruo | 35b6ddf | 2021-04-06 08:36:01 +0800 | [diff] [blame] | 4534 | bool no_dirty_ebs = false; |
| 4535 | int ret; |
| 4536 | |
Qu Wenruo | fa04c16 | 2021-04-27 12:53:35 +0800 | [diff] [blame] | 4537 | prepare_eb_write(eb); |
| 4538 | |
Qu Wenruo | 35b6ddf | 2021-04-06 08:36:01 +0800 | [diff] [blame] | 4539 | /* clear_page_dirty_for_io() in subpage helper needs page locked */ |
| 4540 | lock_page(page); |
| 4541 | btrfs_subpage_set_writeback(fs_info, page, eb->start, eb->len); |
| 4542 | |
| 4543 | /* Check if this is the last dirty bit to update nr_written */ |
| 4544 | no_dirty_ebs = btrfs_subpage_clear_and_test_dirty(fs_info, page, |
| 4545 | eb->start, eb->len); |
| 4546 | if (no_dirty_ebs) |
| 4547 | clear_page_dirty_for_io(page); |
| 4548 | |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 4549 | ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc, |
| 4550 | &epd->bio_ctrl, page, eb->start, eb->len, |
| 4551 | eb->start - page_offset(page), |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 4552 | end_bio_subpage_eb_writepage, 0, false); |
Qu Wenruo | 35b6ddf | 2021-04-06 08:36:01 +0800 | [diff] [blame] | 4553 | if (ret) { |
| 4554 | btrfs_subpage_clear_writeback(fs_info, page, eb->start, eb->len); |
| 4555 | set_btree_ioerr(page, eb); |
| 4556 | unlock_page(page); |
| 4557 | |
| 4558 | if (atomic_dec_and_test(&eb->io_pages)) |
| 4559 | end_extent_buffer_writeback(eb); |
| 4560 | return -EIO; |
| 4561 | } |
| 4562 | unlock_page(page); |
| 4563 | /* |
| 4564 | * Submission finished without problem, if no range of the page is |
| 4565 | * dirty anymore, we have submitted a page. Update nr_written in wbc. |
| 4566 | */ |
| 4567 | if (no_dirty_ebs) |
David Sterba | 572f3da | 2021-07-27 12:45:11 +0200 | [diff] [blame] | 4568 | wbc->nr_to_write--; |
Qu Wenruo | 35b6ddf | 2021-04-06 08:36:01 +0800 | [diff] [blame] | 4569 | return ret; |
| 4570 | } |
| 4571 | |
Chris Mason | 0e378df | 2014-05-19 20:55:27 -0700 | [diff] [blame] | 4572 | static noinline_for_stack int write_one_eb(struct extent_buffer *eb, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4573 | struct writeback_control *wbc, |
| 4574 | struct extent_page_data *epd) |
| 4575 | { |
Qu Wenruo | 0c64c33 | 2021-01-06 09:01:40 +0800 | [diff] [blame] | 4576 | u64 disk_bytenr = eb->start; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 4577 | int i, num_pages; |
Linus Torvalds | 353767e | 2022-08-03 14:54:52 -0700 | [diff] [blame] | 4578 | blk_opf_t write_flags = wbc_to_write_flags(wbc); |
Josef Bacik | d7dbe9e | 2012-04-23 14:00:51 -0400 | [diff] [blame] | 4579 | int ret = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4580 | |
Qu Wenruo | fa04c16 | 2021-04-27 12:53:35 +0800 | [diff] [blame] | 4581 | prepare_eb_write(eb); |
| 4582 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 4583 | num_pages = num_extent_pages(eb); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4584 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 4585 | struct page *p = eb->pages[i]; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4586 | |
| 4587 | clear_page_dirty_for_io(p); |
| 4588 | set_page_writeback(p); |
David Sterba | 0ceb34b | 2020-02-05 19:09:28 +0100 | [diff] [blame] | 4589 | ret = submit_extent_page(REQ_OP_WRITE | write_flags, wbc, |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 4590 | &epd->bio_ctrl, p, disk_bytenr, |
| 4591 | PAGE_SIZE, 0, |
Mike Christie | 1f7ad75 | 2016-06-05 14:31:51 -0500 | [diff] [blame] | 4592 | end_bio_extent_buffer_writepage, |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 4593 | 0, false); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4594 | if (ret) { |
Qu Wenruo | 5a2c607 | 2021-03-25 15:14:44 +0800 | [diff] [blame] | 4595 | set_btree_ioerr(p, eb); |
Takafumi Kubota | fe01aa6 | 2017-02-09 17:24:33 +0900 | [diff] [blame] | 4596 | if (PageWriteback(p)) |
| 4597 | end_page_writeback(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4598 | if (atomic_sub_and_test(num_pages - i, &eb->io_pages)) |
| 4599 | end_extent_buffer_writeback(eb); |
| 4600 | ret = -EIO; |
| 4601 | break; |
| 4602 | } |
Qu Wenruo | 0c64c33 | 2021-01-06 09:01:40 +0800 | [diff] [blame] | 4603 | disk_bytenr += PAGE_SIZE; |
David Sterba | 572f3da | 2021-07-27 12:45:11 +0200 | [diff] [blame] | 4604 | wbc->nr_to_write--; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4605 | unlock_page(p); |
| 4606 | } |
| 4607 | |
| 4608 | if (unlikely(ret)) { |
| 4609 | for (; i < num_pages; i++) { |
Chris Mason | bbf65cf | 2014-10-04 09:56:45 -0700 | [diff] [blame] | 4610 | struct page *p = eb->pages[i]; |
Liu Bo | 8146502 | 2014-09-23 22:22:33 +0800 | [diff] [blame] | 4611 | clear_page_dirty_for_io(p); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4612 | unlock_page(p); |
| 4613 | } |
| 4614 | } |
| 4615 | |
| 4616 | return ret; |
| 4617 | } |
| 4618 | |
Qu Wenruo | f91e0d0 | 2020-12-02 14:48:00 +0800 | [diff] [blame] | 4619 | /* |
Qu Wenruo | c4aec29 | 2021-04-06 08:36:03 +0800 | [diff] [blame] | 4620 | * Submit one subpage btree page. |
| 4621 | * |
| 4622 | * The main difference to submit_eb_page() is: |
| 4623 | * - Page locking |
| 4624 | * For subpage, we don't rely on page locking at all. |
| 4625 | * |
| 4626 | * - Flush write bio |
| 4627 | * We only flush bio if we may be unable to fit current extent buffers into |
| 4628 | * current bio. |
| 4629 | * |
| 4630 | * Return >=0 for the number of submitted extent buffers. |
| 4631 | * Return <0 for fatal error. |
| 4632 | */ |
| 4633 | static int submit_eb_subpage(struct page *page, |
| 4634 | struct writeback_control *wbc, |
| 4635 | struct extent_page_data *epd) |
| 4636 | { |
| 4637 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); |
| 4638 | int submitted = 0; |
| 4639 | u64 page_start = page_offset(page); |
| 4640 | int bit_start = 0; |
Qu Wenruo | c4aec29 | 2021-04-06 08:36:03 +0800 | [diff] [blame] | 4641 | int sectors_per_node = fs_info->nodesize >> fs_info->sectorsize_bits; |
| 4642 | int ret; |
| 4643 | |
| 4644 | /* Lock and write each dirty extent buffers in the range */ |
Qu Wenruo | 72a69cd | 2021-08-17 17:38:52 +0800 | [diff] [blame] | 4645 | while (bit_start < fs_info->subpage_info->bitmap_nr_bits) { |
Qu Wenruo | c4aec29 | 2021-04-06 08:36:03 +0800 | [diff] [blame] | 4646 | struct btrfs_subpage *subpage = (struct btrfs_subpage *)page->private; |
| 4647 | struct extent_buffer *eb; |
| 4648 | unsigned long flags; |
| 4649 | u64 start; |
| 4650 | |
| 4651 | /* |
| 4652 | * Take private lock to ensure the subpage won't be detached |
| 4653 | * in the meantime. |
| 4654 | */ |
| 4655 | spin_lock(&page->mapping->private_lock); |
| 4656 | if (!PagePrivate(page)) { |
| 4657 | spin_unlock(&page->mapping->private_lock); |
| 4658 | break; |
| 4659 | } |
| 4660 | spin_lock_irqsave(&subpage->lock, flags); |
Qu Wenruo | 72a69cd | 2021-08-17 17:38:52 +0800 | [diff] [blame] | 4661 | if (!test_bit(bit_start + fs_info->subpage_info->dirty_offset, |
| 4662 | subpage->bitmaps)) { |
Qu Wenruo | c4aec29 | 2021-04-06 08:36:03 +0800 | [diff] [blame] | 4663 | spin_unlock_irqrestore(&subpage->lock, flags); |
| 4664 | spin_unlock(&page->mapping->private_lock); |
| 4665 | bit_start++; |
| 4666 | continue; |
| 4667 | } |
| 4668 | |
| 4669 | start = page_start + bit_start * fs_info->sectorsize; |
| 4670 | bit_start += sectors_per_node; |
| 4671 | |
| 4672 | /* |
| 4673 | * Here we just want to grab the eb without touching extra |
| 4674 | * spin locks, so call find_extent_buffer_nolock(). |
| 4675 | */ |
| 4676 | eb = find_extent_buffer_nolock(fs_info, start); |
| 4677 | spin_unlock_irqrestore(&subpage->lock, flags); |
| 4678 | spin_unlock(&page->mapping->private_lock); |
| 4679 | |
| 4680 | /* |
| 4681 | * The eb has already reached 0 refs thus find_extent_buffer() |
| 4682 | * doesn't return it. We don't need to write back such eb |
| 4683 | * anyway. |
| 4684 | */ |
| 4685 | if (!eb) |
| 4686 | continue; |
| 4687 | |
| 4688 | ret = lock_extent_buffer_for_io(eb, epd); |
| 4689 | if (ret == 0) { |
| 4690 | free_extent_buffer(eb); |
| 4691 | continue; |
| 4692 | } |
| 4693 | if (ret < 0) { |
| 4694 | free_extent_buffer(eb); |
| 4695 | goto cleanup; |
| 4696 | } |
Qu Wenruo | fa04c16 | 2021-04-27 12:53:35 +0800 | [diff] [blame] | 4697 | ret = write_one_subpage_eb(eb, wbc, epd); |
Qu Wenruo | c4aec29 | 2021-04-06 08:36:03 +0800 | [diff] [blame] | 4698 | free_extent_buffer(eb); |
| 4699 | if (ret < 0) |
| 4700 | goto cleanup; |
| 4701 | submitted++; |
| 4702 | } |
| 4703 | return submitted; |
| 4704 | |
| 4705 | cleanup: |
| 4706 | /* We hit error, end bio for the submitted extent buffers */ |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 4707 | submit_write_bio(epd, ret); |
Qu Wenruo | c4aec29 | 2021-04-06 08:36:03 +0800 | [diff] [blame] | 4708 | return ret; |
| 4709 | } |
| 4710 | |
| 4711 | /* |
Qu Wenruo | f91e0d0 | 2020-12-02 14:48:00 +0800 | [diff] [blame] | 4712 | * Submit all page(s) of one extent buffer. |
| 4713 | * |
| 4714 | * @page: the page of one extent buffer |
| 4715 | * @eb_context: to determine if we need to submit this page, if current page |
| 4716 | * belongs to this eb, we don't need to submit |
| 4717 | * |
| 4718 | * The caller should pass each page in their bytenr order, and here we use |
| 4719 | * @eb_context to determine if we have submitted pages of one extent buffer. |
| 4720 | * |
| 4721 | * If we have, we just skip until we hit a new page that doesn't belong to |
| 4722 | * current @eb_context. |
| 4723 | * |
| 4724 | * If not, we submit all the page(s) of the extent buffer. |
| 4725 | * |
| 4726 | * Return >0 if we have submitted the extent buffer successfully. |
| 4727 | * Return 0 if we don't need to submit the page, as it's already submitted by |
| 4728 | * previous call. |
| 4729 | * Return <0 for fatal error. |
| 4730 | */ |
| 4731 | static int submit_eb_page(struct page *page, struct writeback_control *wbc, |
| 4732 | struct extent_page_data *epd, |
| 4733 | struct extent_buffer **eb_context) |
| 4734 | { |
| 4735 | struct address_space *mapping = page->mapping; |
Naohiro Aota | 0bc09ca | 2021-02-04 19:22:08 +0900 | [diff] [blame] | 4736 | struct btrfs_block_group *cache = NULL; |
Qu Wenruo | f91e0d0 | 2020-12-02 14:48:00 +0800 | [diff] [blame] | 4737 | struct extent_buffer *eb; |
| 4738 | int ret; |
| 4739 | |
| 4740 | if (!PagePrivate(page)) |
| 4741 | return 0; |
| 4742 | |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 4743 | if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) |
Qu Wenruo | c4aec29 | 2021-04-06 08:36:03 +0800 | [diff] [blame] | 4744 | return submit_eb_subpage(page, wbc, epd); |
| 4745 | |
Qu Wenruo | f91e0d0 | 2020-12-02 14:48:00 +0800 | [diff] [blame] | 4746 | spin_lock(&mapping->private_lock); |
| 4747 | if (!PagePrivate(page)) { |
| 4748 | spin_unlock(&mapping->private_lock); |
| 4749 | return 0; |
| 4750 | } |
| 4751 | |
| 4752 | eb = (struct extent_buffer *)page->private; |
| 4753 | |
| 4754 | /* |
| 4755 | * Shouldn't happen and normally this would be a BUG_ON but no point |
| 4756 | * crashing the machine for something we can survive anyway. |
| 4757 | */ |
| 4758 | if (WARN_ON(!eb)) { |
| 4759 | spin_unlock(&mapping->private_lock); |
| 4760 | return 0; |
| 4761 | } |
| 4762 | |
| 4763 | if (eb == *eb_context) { |
| 4764 | spin_unlock(&mapping->private_lock); |
| 4765 | return 0; |
| 4766 | } |
| 4767 | ret = atomic_inc_not_zero(&eb->refs); |
| 4768 | spin_unlock(&mapping->private_lock); |
| 4769 | if (!ret) |
| 4770 | return 0; |
| 4771 | |
Naohiro Aota | 0bc09ca | 2021-02-04 19:22:08 +0900 | [diff] [blame] | 4772 | if (!btrfs_check_meta_write_pointer(eb->fs_info, eb, &cache)) { |
| 4773 | /* |
| 4774 | * If for_sync, this hole will be filled with |
| 4775 | * trasnsaction commit. |
| 4776 | */ |
| 4777 | if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) |
| 4778 | ret = -EAGAIN; |
| 4779 | else |
| 4780 | ret = 0; |
| 4781 | free_extent_buffer(eb); |
| 4782 | return ret; |
| 4783 | } |
| 4784 | |
Qu Wenruo | f91e0d0 | 2020-12-02 14:48:00 +0800 | [diff] [blame] | 4785 | *eb_context = eb; |
| 4786 | |
| 4787 | ret = lock_extent_buffer_for_io(eb, epd); |
| 4788 | if (ret <= 0) { |
Naohiro Aota | 0bc09ca | 2021-02-04 19:22:08 +0900 | [diff] [blame] | 4789 | btrfs_revert_meta_write_pointer(cache, eb); |
| 4790 | if (cache) |
| 4791 | btrfs_put_block_group(cache); |
Qu Wenruo | f91e0d0 | 2020-12-02 14:48:00 +0800 | [diff] [blame] | 4792 | free_extent_buffer(eb); |
| 4793 | return ret; |
| 4794 | } |
Naohiro Aota | be1a1d7 | 2021-08-19 21:19:23 +0900 | [diff] [blame] | 4795 | if (cache) { |
Nikolay Borisov | d3e2996 | 2022-03-07 15:30:02 +0200 | [diff] [blame] | 4796 | /* |
| 4797 | * Implies write in zoned mode. Mark the last eb in a block group. |
| 4798 | */ |
Naohiro Aota | 56fbb0a | 2022-05-03 17:48:53 -0700 | [diff] [blame] | 4799 | btrfs_schedule_zone_finish_bg(cache, eb); |
Nikolay Borisov | d3e2996 | 2022-03-07 15:30:02 +0200 | [diff] [blame] | 4800 | btrfs_put_block_group(cache); |
Naohiro Aota | be1a1d7 | 2021-08-19 21:19:23 +0900 | [diff] [blame] | 4801 | } |
Qu Wenruo | f91e0d0 | 2020-12-02 14:48:00 +0800 | [diff] [blame] | 4802 | ret = write_one_eb(eb, wbc, epd); |
| 4803 | free_extent_buffer(eb); |
| 4804 | if (ret < 0) |
| 4805 | return ret; |
| 4806 | return 1; |
| 4807 | } |
| 4808 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4809 | int btree_write_cache_pages(struct address_space *mapping, |
| 4810 | struct writeback_control *wbc) |
| 4811 | { |
Qu Wenruo | f91e0d0 | 2020-12-02 14:48:00 +0800 | [diff] [blame] | 4812 | struct extent_buffer *eb_context = NULL; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4813 | struct extent_page_data epd = { |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 4814 | .bio_ctrl = { 0 }, |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4815 | .extent_locked = 0, |
| 4816 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
| 4817 | }; |
Qu Wenruo | b3ff8f1 | 2020-02-12 14:12:44 +0800 | [diff] [blame] | 4818 | struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4819 | int ret = 0; |
| 4820 | int done = 0; |
| 4821 | int nr_to_write_done = 0; |
| 4822 | struct pagevec pvec; |
| 4823 | int nr_pages; |
| 4824 | pgoff_t index; |
| 4825 | pgoff_t end; /* Inclusive */ |
| 4826 | int scanned = 0; |
Matthew Wilcox | 10bbd23 | 2017-12-05 17:30:38 -0500 | [diff] [blame] | 4827 | xa_mark_t tag; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4828 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 4829 | pagevec_init(&pvec); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4830 | if (wbc->range_cyclic) { |
| 4831 | index = mapping->writeback_index; /* Start from prev offset */ |
| 4832 | end = -1; |
Josef Bacik | 556755a | 2020-01-03 10:38:44 -0500 | [diff] [blame] | 4833 | /* |
| 4834 | * Start from the beginning does not need to cycle over the |
| 4835 | * range, mark it as scanned. |
| 4836 | */ |
| 4837 | scanned = (index == 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4838 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4839 | index = wbc->range_start >> PAGE_SHIFT; |
| 4840 | end = wbc->range_end >> PAGE_SHIFT; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4841 | scanned = 1; |
| 4842 | } |
| 4843 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 4844 | tag = PAGECACHE_TAG_TOWRITE; |
| 4845 | else |
| 4846 | tag = PAGECACHE_TAG_DIRTY; |
Naohiro Aota | 0bc09ca | 2021-02-04 19:22:08 +0900 | [diff] [blame] | 4847 | btrfs_zoned_meta_io_lock(fs_info); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4848 | retry: |
| 4849 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 4850 | tag_pages_for_writeback(mapping, index, end); |
| 4851 | while (!done && !nr_to_write_done && (index <= end) && |
Jan Kara | 4006f43 | 2017-11-15 17:34:37 -0800 | [diff] [blame] | 4852 | (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, |
Jan Kara | 67fd707 | 2017-11-15 17:35:19 -0800 | [diff] [blame] | 4853 | tag))) { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4854 | unsigned i; |
| 4855 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4856 | for (i = 0; i < nr_pages; i++) { |
| 4857 | struct page *page = pvec.pages[i]; |
| 4858 | |
Qu Wenruo | f91e0d0 | 2020-12-02 14:48:00 +0800 | [diff] [blame] | 4859 | ret = submit_eb_page(page, wbc, &epd, &eb_context); |
| 4860 | if (ret == 0) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4861 | continue; |
Qu Wenruo | f91e0d0 | 2020-12-02 14:48:00 +0800 | [diff] [blame] | 4862 | if (ret < 0) { |
Filipe Manana | 0607eb1d | 2019-09-11 17:42:28 +0100 | [diff] [blame] | 4863 | done = 1; |
Filipe Manana | 0607eb1d | 2019-09-11 17:42:28 +0100 | [diff] [blame] | 4864 | break; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4865 | } |
| 4866 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4867 | /* |
| 4868 | * the filesystem may choose to bump up nr_to_write. |
| 4869 | * We have to make sure to honor the new nr_to_write |
| 4870 | * at any time |
| 4871 | */ |
| 4872 | nr_to_write_done = wbc->nr_to_write <= 0; |
| 4873 | } |
| 4874 | pagevec_release(&pvec); |
| 4875 | cond_resched(); |
| 4876 | } |
| 4877 | if (!scanned && !done) { |
| 4878 | /* |
| 4879 | * We hit the last page and there is more work to be done: wrap |
| 4880 | * back to the start of the file |
| 4881 | */ |
| 4882 | scanned = 1; |
| 4883 | index = 0; |
| 4884 | goto retry; |
| 4885 | } |
Qu Wenruo | b3ff8f1 | 2020-02-12 14:12:44 +0800 | [diff] [blame] | 4886 | /* |
| 4887 | * If something went wrong, don't allow any metadata write bio to be |
| 4888 | * submitted. |
| 4889 | * |
| 4890 | * This would prevent use-after-free if we had dirty pages not |
| 4891 | * cleaned up, which can still happen by fuzzed images. |
| 4892 | * |
| 4893 | * - Bad extent tree |
| 4894 | * Allowing existing tree block to be allocated for other trees. |
| 4895 | * |
| 4896 | * - Log tree operations |
| 4897 | * Exiting tree blocks get allocated to log tree, bumps its |
| 4898 | * generation, then get cleaned in tree re-balance. |
| 4899 | * Such tree block will not be written back, since it's clean, |
| 4900 | * thus no WRITTEN flag set. |
| 4901 | * And after log writes back, this tree block is not traced by |
| 4902 | * any dirty extent_io_tree. |
| 4903 | * |
| 4904 | * - Offending tree block gets re-dirtied from its original owner |
| 4905 | * Since it has bumped generation, no WRITTEN flag, it can be |
| 4906 | * reused without COWing. This tree block will not be traced |
| 4907 | * by btrfs_transaction::dirty_pages. |
| 4908 | * |
| 4909 | * Now such dirty tree block will not be cleaned by any dirty |
| 4910 | * extent io tree. Thus we don't want to submit such wild eb |
| 4911 | * if the fs already has error. |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 4912 | * |
Qu Wenruo | c9583ad | 2022-04-12 20:30:13 +0800 | [diff] [blame] | 4913 | * We can get ret > 0 from submit_extent_page() indicating how many ebs |
| 4914 | * were submitted. Reset it to 0 to avoid false alerts for the caller. |
| 4915 | */ |
| 4916 | if (ret > 0) |
| 4917 | ret = 0; |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 4918 | if (!ret && BTRFS_FS_ERROR(fs_info)) |
| 4919 | ret = -EROFS; |
| 4920 | submit_write_bio(&epd, ret); |
| 4921 | |
| 4922 | btrfs_zoned_meta_io_unlock(fs_info); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 4923 | return ret; |
| 4924 | } |
| 4925 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4926 | /** |
Nikolay Borisov | 3bed2da | 2021-01-22 11:58:03 +0200 | [diff] [blame] | 4927 | * Walk the list of dirty pages of the given address space and write all of them. |
| 4928 | * |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4929 | * @mapping: address space structure to write |
Nikolay Borisov | 3bed2da | 2021-01-22 11:58:03 +0200 | [diff] [blame] | 4930 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 4931 | * @epd: holds context for the write, namely the bio |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4932 | * |
| 4933 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 4934 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 4935 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 4936 | * and msync() need to guarantee that all the data which was dirty at the time |
| 4937 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 4938 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 4939 | * existing IO to complete. |
| 4940 | */ |
David Sterba | 4242b64 | 2017-02-10 19:38:24 +0100 | [diff] [blame] | 4941 | static int extent_write_cache_pages(struct address_space *mapping, |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 4942 | struct writeback_control *wbc, |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 4943 | struct extent_page_data *epd) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4944 | { |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4945 | struct inode *inode = mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4946 | int ret = 0; |
| 4947 | int done = 0; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 4948 | int nr_to_write_done = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4949 | struct pagevec pvec; |
| 4950 | int nr_pages; |
| 4951 | pgoff_t index; |
| 4952 | pgoff_t end; /* Inclusive */ |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4953 | pgoff_t done_index; |
| 4954 | int range_whole = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4955 | int scanned = 0; |
Matthew Wilcox | 10bbd23 | 2017-12-05 17:30:38 -0500 | [diff] [blame] | 4956 | xa_mark_t tag; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4957 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 4958 | /* |
| 4959 | * We have to hold onto the inode so that ordered extents can do their |
| 4960 | * work when the IO finishes. The alternative to this is failing to add |
| 4961 | * an ordered extent if the igrab() fails there and that is a huge pain |
| 4962 | * to deal with, so instead just hold onto the inode throughout the |
| 4963 | * writepages operation. If it fails here we are freeing up the inode |
| 4964 | * anyway and we'd rather not waste our time writing out stuff that is |
| 4965 | * going to be truncated anyway. |
| 4966 | */ |
| 4967 | if (!igrab(inode)) |
| 4968 | return 0; |
| 4969 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 4970 | pagevec_init(&pvec); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4971 | if (wbc->range_cyclic) { |
| 4972 | index = mapping->writeback_index; /* Start from prev offset */ |
| 4973 | end = -1; |
Josef Bacik | 556755a | 2020-01-03 10:38:44 -0500 | [diff] [blame] | 4974 | /* |
| 4975 | * Start from the beginning does not need to cycle over the |
| 4976 | * range, mark it as scanned. |
| 4977 | */ |
| 4978 | scanned = (index == 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4979 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4980 | index = wbc->range_start >> PAGE_SHIFT; |
| 4981 | end = wbc->range_end >> PAGE_SHIFT; |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 4982 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 4983 | range_whole = 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4984 | scanned = 1; |
| 4985 | } |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 4986 | |
| 4987 | /* |
| 4988 | * We do the tagged writepage as long as the snapshot flush bit is set |
| 4989 | * and we are the first one who do the filemap_flush() on this inode. |
| 4990 | * |
| 4991 | * The nr_to_write == LONG_MAX is needed to make sure other flushers do |
| 4992 | * not race in and drop the bit. |
| 4993 | */ |
| 4994 | if (range_whole && wbc->nr_to_write == LONG_MAX && |
| 4995 | test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH, |
| 4996 | &BTRFS_I(inode)->runtime_flags)) |
| 4997 | wbc->tagged_writepages = 1; |
| 4998 | |
| 4999 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 5000 | tag = PAGECACHE_TAG_TOWRITE; |
| 5001 | else |
| 5002 | tag = PAGECACHE_TAG_DIRTY; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5003 | retry: |
Ethan Lien | 3cd24c6 | 2018-11-01 14:49:03 +0800 | [diff] [blame] | 5004 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
Josef Bacik | f7aaa06 | 2011-07-15 21:26:38 +0000 | [diff] [blame] | 5005 | tag_pages_for_writeback(mapping, index, end); |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 5006 | done_index = index; |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 5007 | while (!done && !nr_to_write_done && (index <= end) && |
Jan Kara | 67fd707 | 2017-11-15 17:35:19 -0800 | [diff] [blame] | 5008 | (nr_pages = pagevec_lookup_range_tag(&pvec, mapping, |
| 5009 | &index, end, tag))) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5010 | unsigned i; |
| 5011 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5012 | for (i = 0; i < nr_pages; i++) { |
| 5013 | struct page *page = pvec.pages[i]; |
| 5014 | |
Tejun Heo | f7bddf1 | 2019-10-03 07:27:13 -0700 | [diff] [blame] | 5015 | done_index = page->index + 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5016 | /* |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 5017 | * At this point we hold neither the i_pages lock nor |
| 5018 | * the page lock: the page may be truncated or |
| 5019 | * invalidated (changing page->mapping to NULL), |
| 5020 | * or even swizzled back from swapper_space to |
| 5021 | * tmpfs file mapping |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5022 | */ |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 5023 | if (!trylock_page(page)) { |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 5024 | submit_write_bio(epd, 0); |
Josef Bacik | c8f2f24 | 2013-02-11 11:33:00 -0500 | [diff] [blame] | 5025 | lock_page(page); |
Chris Mason | 01d658f | 2011-11-01 10:08:06 -0400 | [diff] [blame] | 5026 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5027 | |
| 5028 | if (unlikely(page->mapping != mapping)) { |
| 5029 | unlock_page(page); |
| 5030 | continue; |
| 5031 | } |
| 5032 | |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 5033 | if (wbc->sync_mode != WB_SYNC_NONE) { |
Qu Wenruo | c9583ad | 2022-04-12 20:30:13 +0800 | [diff] [blame] | 5034 | if (PageWriteback(page)) |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 5035 | submit_write_bio(epd, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5036 | wait_on_page_writeback(page); |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 5037 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5038 | |
| 5039 | if (PageWriteback(page) || |
| 5040 | !clear_page_dirty_for_io(page)) { |
| 5041 | unlock_page(page); |
| 5042 | continue; |
| 5043 | } |
| 5044 | |
David Sterba | aab6e9e | 2017-11-30 18:00:02 +0100 | [diff] [blame] | 5045 | ret = __extent_writepage(page, wbc, epd); |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 5046 | if (ret < 0) { |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 5047 | done = 1; |
| 5048 | break; |
| 5049 | } |
Chris Mason | f85d7d6c | 2009-09-18 16:03:16 -0400 | [diff] [blame] | 5050 | |
| 5051 | /* |
| 5052 | * the filesystem may choose to bump up nr_to_write. |
| 5053 | * We have to make sure to honor the new nr_to_write |
| 5054 | * at any time |
| 5055 | */ |
| 5056 | nr_to_write_done = wbc->nr_to_write <= 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5057 | } |
| 5058 | pagevec_release(&pvec); |
| 5059 | cond_resched(); |
| 5060 | } |
Liu Bo | 894b36e | 2016-03-07 16:56:22 -0800 | [diff] [blame] | 5061 | if (!scanned && !done) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5062 | /* |
| 5063 | * We hit the last page and there is more work to be done: wrap |
| 5064 | * back to the start of the file |
| 5065 | */ |
| 5066 | scanned = 1; |
| 5067 | index = 0; |
Josef Bacik | 42ffb0b | 2020-01-23 15:33:02 -0500 | [diff] [blame] | 5068 | |
| 5069 | /* |
| 5070 | * If we're looping we could run into a page that is locked by a |
| 5071 | * writer and that writer could be waiting on writeback for a |
| 5072 | * page in our current bio, and thus deadlock, so flush the |
| 5073 | * write bio here. |
| 5074 | */ |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 5075 | submit_write_bio(epd, 0); |
Qu Wenruo | c9583ad | 2022-04-12 20:30:13 +0800 | [diff] [blame] | 5076 | goto retry; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5077 | } |
Liu Bo | a9132667 | 2016-03-07 16:56:21 -0800 | [diff] [blame] | 5078 | |
| 5079 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole)) |
| 5080 | mapping->writeback_index = done_index; |
| 5081 | |
Josef Bacik | 7fd1a3f | 2012-06-27 17:18:41 -0400 | [diff] [blame] | 5082 | btrfs_add_delayed_iput(inode); |
Liu Bo | 894b36e | 2016-03-07 16:56:22 -0800 | [diff] [blame] | 5083 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5084 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5085 | |
Qu Wenruo | 2bd0fc9 | 2021-09-27 15:21:58 +0800 | [diff] [blame] | 5086 | /* |
| 5087 | * Submit the pages in the range to bio for call sites which delalloc range has |
| 5088 | * already been ran (aka, ordered extent inserted) and all pages are still |
| 5089 | * locked. |
| 5090 | */ |
| 5091 | int extent_write_locked_range(struct inode *inode, u64 start, u64 end) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5092 | { |
Qu Wenruo | 2bd0fc9 | 2021-09-27 15:21:58 +0800 | [diff] [blame] | 5093 | bool found_error = false; |
| 5094 | int first_error = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5095 | int ret = 0; |
| 5096 | struct address_space *mapping = inode->i_mapping; |
| 5097 | struct page *page; |
Qu Wenruo | 2bd0fc9 | 2021-09-27 15:21:58 +0800 | [diff] [blame] | 5098 | u64 cur = start; |
Qu Wenruo | 66448b9 | 2021-09-27 15:22:02 +0800 | [diff] [blame] | 5099 | unsigned long nr_pages; |
| 5100 | const u32 sectorsize = btrfs_sb(inode->i_sb)->sectorsize; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5101 | struct extent_page_data epd = { |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 5102 | .bio_ctrl = { 0 }, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5103 | .extent_locked = 1, |
Qu Wenruo | 2bd0fc9 | 2021-09-27 15:21:58 +0800 | [diff] [blame] | 5104 | .sync_io = 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5105 | }; |
| 5106 | struct writeback_control wbc_writepages = { |
Qu Wenruo | 2bd0fc9 | 2021-09-27 15:21:58 +0800 | [diff] [blame] | 5107 | .sync_mode = WB_SYNC_ALL, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5108 | .range_start = start, |
| 5109 | .range_end = end + 1, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 5110 | /* We're called from an async helper function */ |
| 5111 | .punt_to_cgroup = 1, |
| 5112 | .no_cgroup_owner = 1, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5113 | }; |
| 5114 | |
Qu Wenruo | 66448b9 | 2021-09-27 15:22:02 +0800 | [diff] [blame] | 5115 | ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(end + 1, sectorsize)); |
| 5116 | nr_pages = (round_up(end, PAGE_SIZE) - round_down(start, PAGE_SIZE)) >> |
| 5117 | PAGE_SHIFT; |
| 5118 | wbc_writepages.nr_to_write = nr_pages * 2; |
| 5119 | |
Chris Mason | dbb70be | 2019-07-10 12:28:18 -0700 | [diff] [blame] | 5120 | wbc_attach_fdatawrite_inode(&wbc_writepages, inode); |
Qu Wenruo | 2bd0fc9 | 2021-09-27 15:21:58 +0800 | [diff] [blame] | 5121 | while (cur <= end) { |
Qu Wenruo | 66448b9 | 2021-09-27 15:22:02 +0800 | [diff] [blame] | 5122 | u64 cur_end = min(round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end); |
| 5123 | |
Qu Wenruo | 2bd0fc9 | 2021-09-27 15:21:58 +0800 | [diff] [blame] | 5124 | page = find_get_page(mapping, cur >> PAGE_SHIFT); |
| 5125 | /* |
| 5126 | * All pages in the range are locked since |
| 5127 | * btrfs_run_delalloc_range(), thus there is no way to clear |
| 5128 | * the page dirty flag. |
| 5129 | */ |
Qu Wenruo | 66448b9 | 2021-09-27 15:22:02 +0800 | [diff] [blame] | 5130 | ASSERT(PageLocked(page)); |
Qu Wenruo | 2bd0fc9 | 2021-09-27 15:21:58 +0800 | [diff] [blame] | 5131 | ASSERT(PageDirty(page)); |
| 5132 | clear_page_dirty_for_io(page); |
| 5133 | ret = __extent_writepage(page, &wbc_writepages, &epd); |
| 5134 | ASSERT(ret <= 0); |
| 5135 | if (ret < 0) { |
| 5136 | found_error = true; |
| 5137 | first_error = ret; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5138 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5139 | put_page(page); |
Qu Wenruo | 66448b9 | 2021-09-27 15:22:02 +0800 | [diff] [blame] | 5140 | cur = cur_end + 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5141 | } |
| 5142 | |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 5143 | submit_write_bio(&epd, found_error ? ret : 0); |
Chris Mason | dbb70be | 2019-07-10 12:28:18 -0700 | [diff] [blame] | 5144 | |
| 5145 | wbc_detach_inode(&wbc_writepages); |
Qu Wenruo | 2bd0fc9 | 2021-09-27 15:21:58 +0800 | [diff] [blame] | 5146 | if (found_error) |
| 5147 | return first_error; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5148 | return ret; |
| 5149 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5150 | |
Nikolay Borisov | 8ae225a | 2018-04-19 10:46:38 +0300 | [diff] [blame] | 5151 | int extent_writepages(struct address_space *mapping, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5152 | struct writeback_control *wbc) |
| 5153 | { |
Johannes Thumshirn | 35156d8 | 2021-09-09 01:19:27 +0900 | [diff] [blame] | 5154 | struct inode *inode = mapping->host; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5155 | int ret = 0; |
| 5156 | struct extent_page_data epd = { |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 5157 | .bio_ctrl = { 0 }, |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 5158 | .extent_locked = 0, |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5159 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5160 | }; |
| 5161 | |
Johannes Thumshirn | 35156d8 | 2021-09-09 01:19:27 +0900 | [diff] [blame] | 5162 | /* |
| 5163 | * Allow only a single thread to do the reloc work in zoned mode to |
| 5164 | * protect the write pointer updates. |
| 5165 | */ |
Johannes Thumshirn | 869f4cd | 2021-12-07 06:28:34 -0800 | [diff] [blame] | 5166 | btrfs_zoned_data_reloc_lock(BTRFS_I(inode)); |
David Sterba | 935db85 | 2017-06-23 04:30:28 +0200 | [diff] [blame] | 5167 | ret = extent_write_cache_pages(mapping, wbc, &epd); |
Christoph Hellwig | 9845e5d | 2022-06-03 09:11:02 +0200 | [diff] [blame] | 5168 | submit_write_bio(&epd, ret); |
Naohiro Aota | 19ab78c | 2022-06-07 16:08:30 +0900 | [diff] [blame] | 5169 | btrfs_zoned_data_reloc_unlock(BTRFS_I(inode)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5170 | return ret; |
| 5171 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5172 | |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 5173 | void extent_readahead(struct readahead_control *rac) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5174 | { |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 5175 | struct btrfs_bio_ctrl bio_ctrl = { 0 }; |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 5176 | struct page *pagepool[16]; |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 5177 | struct extent_map *em_cached = NULL; |
Filipe Manana | 808f80b | 2015-09-28 09:56:26 +0100 | [diff] [blame] | 5178 | u64 prev_em_start = (u64)-1; |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 5179 | int nr; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5180 | |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 5181 | while ((nr = readahead_page_batch(rac, pagepool))) { |
Matthew Wilcox (Oracle) | 32c0a6b | 2021-03-21 21:03:11 +0000 | [diff] [blame] | 5182 | u64 contig_start = readahead_pos(rac); |
| 5183 | u64 contig_end = contig_start + readahead_batch_length(rac) - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5184 | |
Matthew Wilcox (Oracle) | ba206a0 | 2020-06-01 21:47:05 -0700 | [diff] [blame] | 5185 | contiguous_readpages(pagepool, nr, contig_start, contig_end, |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 5186 | &em_cached, &bio_ctrl, &prev_em_start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5187 | } |
Liu Bo | 67c9684 | 2012-07-20 21:43:09 -0600 | [diff] [blame] | 5188 | |
Miao Xie | 125bac01 | 2013-07-25 19:22:37 +0800 | [diff] [blame] | 5189 | if (em_cached) |
| 5190 | free_extent_map(em_cached); |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 5191 | submit_one_bio(&bio_ctrl); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5192 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5193 | |
| 5194 | /* |
Matthew Wilcox (Oracle) | 895586e | 2022-02-09 20:21:39 +0000 | [diff] [blame] | 5195 | * basic invalidate_folio code, this waits on any locked or writeback |
| 5196 | * ranges corresponding to the folio, and then deletes any extent state |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5197 | * records from the tree |
| 5198 | */ |
Matthew Wilcox (Oracle) | 895586e | 2022-02-09 20:21:39 +0000 | [diff] [blame] | 5199 | int extent_invalidate_folio(struct extent_io_tree *tree, |
| 5200 | struct folio *folio, size_t offset) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5201 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5202 | struct extent_state *cached_state = NULL; |
Matthew Wilcox (Oracle) | 895586e | 2022-02-09 20:21:39 +0000 | [diff] [blame] | 5203 | u64 start = folio_pos(folio); |
| 5204 | u64 end = start + folio_size(folio) - 1; |
| 5205 | size_t blocksize = folio->mapping->host->i_sb->s_blocksize; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5206 | |
Qu Wenruo | 829ddec | 2020-11-13 20:51:39 +0800 | [diff] [blame] | 5207 | /* This function is only called for the btree inode */ |
| 5208 | ASSERT(tree->owner == IO_TREE_BTREE_INODE_IO); |
| 5209 | |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5210 | start += ALIGN(offset, blocksize); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5211 | if (start > end) |
| 5212 | return 0; |
| 5213 | |
David Sterba | ff13db4 | 2015-12-03 14:30:40 +0100 | [diff] [blame] | 5214 | lock_extent_bits(tree, start, end, &cached_state); |
Matthew Wilcox (Oracle) | 895586e | 2022-02-09 20:21:39 +0000 | [diff] [blame] | 5215 | folio_wait_writeback(folio); |
Qu Wenruo | 829ddec | 2020-11-13 20:51:39 +0800 | [diff] [blame] | 5216 | |
| 5217 | /* |
| 5218 | * Currently for btree io tree, only EXTENT_LOCKED is utilized, |
| 5219 | * so here we only need to unlock the extent range to free any |
| 5220 | * existing extent state. |
| 5221 | */ |
| 5222 | unlock_extent_cached(tree, start, end, &cached_state); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5223 | return 0; |
| 5224 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5225 | |
| 5226 | /* |
Matthew Wilcox (Oracle) | f913cff | 2022-04-30 23:15:16 -0400 | [diff] [blame] | 5227 | * a helper for release_folio, this tests for areas of the page that |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 5228 | * are locked or under IO and drops the related state bits if it is safe |
| 5229 | * to drop the page. |
| 5230 | */ |
Nikolay Borisov | 29c68b2d | 2018-04-19 10:46:35 +0300 | [diff] [blame] | 5231 | static int try_release_extent_state(struct extent_io_tree *tree, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5232 | struct page *page, gfp_t mask) |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 5233 | { |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 5234 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5235 | u64 end = start + PAGE_SIZE - 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 5236 | int ret = 1; |
| 5237 | |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 5238 | if (test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL)) { |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 5239 | ret = 0; |
Nikolay Borisov | 8882679 | 2019-03-14 15:28:31 +0200 | [diff] [blame] | 5240 | } else { |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 5241 | /* |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 5242 | * At this point we can safely clear everything except the |
| 5243 | * locked bit, the nodatasum bit and the delalloc new bit. |
| 5244 | * The delalloc new bit will be cleared by ordered extent |
| 5245 | * completion. |
Chris Mason | 11ef160 | 2009-09-23 20:28:46 -0400 | [diff] [blame] | 5246 | */ |
David Sterba | 66b0c88 | 2017-10-31 16:30:47 +0100 | [diff] [blame] | 5247 | ret = __clear_extent_bit(tree, start, end, |
Filipe Manana | 2766ff6 | 2020-11-04 11:07:34 +0000 | [diff] [blame] | 5248 | ~(EXTENT_LOCKED | EXTENT_NODATASUM | EXTENT_DELALLOC_NEW), |
| 5249 | 0, 0, NULL, mask, NULL); |
Chris Mason | e3f24cc | 2011-02-14 12:52:08 -0500 | [diff] [blame] | 5250 | |
| 5251 | /* if clear_extent_bit failed for enomem reasons, |
| 5252 | * we can't allow the release to continue. |
| 5253 | */ |
| 5254 | if (ret < 0) |
| 5255 | ret = 0; |
| 5256 | else |
| 5257 | ret = 1; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 5258 | } |
| 5259 | return ret; |
| 5260 | } |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 5261 | |
| 5262 | /* |
Matthew Wilcox (Oracle) | f913cff | 2022-04-30 23:15:16 -0400 | [diff] [blame] | 5263 | * a helper for release_folio. As long as there are no locked extents |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5264 | * in the range corresponding to the page, both state records and extent |
| 5265 | * map records are removed |
| 5266 | */ |
Nikolay Borisov | 477a30b | 2018-04-19 10:46:34 +0300 | [diff] [blame] | 5267 | int try_release_extent_mapping(struct page *page, gfp_t mask) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5268 | { |
| 5269 | struct extent_map *em; |
Miao Xie | 4eee4fa | 2012-12-21 09:17:45 +0000 | [diff] [blame] | 5270 | u64 start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5271 | u64 end = start + PAGE_SIZE - 1; |
Filipe Manana | bd3599a | 2018-07-12 01:36:43 +0100 | [diff] [blame] | 5272 | struct btrfs_inode *btrfs_inode = BTRFS_I(page->mapping->host); |
| 5273 | struct extent_io_tree *tree = &btrfs_inode->io_tree; |
| 5274 | struct extent_map_tree *map = &btrfs_inode->extent_tree; |
Chris Mason | 7b13b7b | 2008-04-18 10:29:50 -0400 | [diff] [blame] | 5275 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 5276 | if (gfpflags_allow_blocking(mask) && |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 5277 | page->mapping->host->i_size > SZ_16M) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 5278 | u64 len; |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5279 | while (start <= end) { |
Filipe Manana | fbc2bd7 | 2020-07-22 12:28:52 +0100 | [diff] [blame] | 5280 | struct btrfs_fs_info *fs_info; |
| 5281 | u64 cur_gen; |
| 5282 | |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 5283 | len = end - start + 1; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5284 | write_lock(&map->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 5285 | em = lookup_extent_mapping(map, start, len); |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 5286 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5287 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5288 | break; |
| 5289 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 5290 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
| 5291 | em->start != start) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5292 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5293 | free_extent_map(em); |
| 5294 | break; |
| 5295 | } |
Filipe Manana | 3d6448e | 2020-07-22 12:28:37 +0100 | [diff] [blame] | 5296 | if (test_range_bit(tree, em->start, |
| 5297 | extent_map_end(em) - 1, |
| 5298 | EXTENT_LOCKED, 0, NULL)) |
| 5299 | goto next; |
| 5300 | /* |
| 5301 | * If it's not in the list of modified extents, used |
| 5302 | * by a fast fsync, we can remove it. If it's being |
| 5303 | * logged we can safely remove it since fsync took an |
| 5304 | * extra reference on the em. |
| 5305 | */ |
| 5306 | if (list_empty(&em->list) || |
Filipe Manana | fbc2bd7 | 2020-07-22 12:28:52 +0100 | [diff] [blame] | 5307 | test_bit(EXTENT_FLAG_LOGGING, &em->flags)) |
| 5308 | goto remove_em; |
| 5309 | /* |
| 5310 | * If it's in the list of modified extents, remove it |
| 5311 | * only if its generation is older then the current one, |
| 5312 | * in which case we don't need it for a fast fsync. |
| 5313 | * Otherwise don't remove it, we could be racing with an |
| 5314 | * ongoing fast fsync that could miss the new extent. |
| 5315 | */ |
| 5316 | fs_info = btrfs_inode->root->fs_info; |
| 5317 | spin_lock(&fs_info->trans_lock); |
| 5318 | cur_gen = fs_info->generation; |
| 5319 | spin_unlock(&fs_info->trans_lock); |
| 5320 | if (em->generation >= cur_gen) |
| 5321 | goto next; |
| 5322 | remove_em: |
Filipe Manana | 5e548b3 | 2020-07-22 12:29:01 +0100 | [diff] [blame] | 5323 | /* |
| 5324 | * We only remove extent maps that are not in the list of |
| 5325 | * modified extents or that are in the list but with a |
| 5326 | * generation lower then the current generation, so there |
| 5327 | * is no need to set the full fsync flag on the inode (it |
| 5328 | * hurts the fsync performance for workloads with a data |
| 5329 | * size that exceeds or is close to the system's memory). |
| 5330 | */ |
Filipe Manana | fbc2bd7 | 2020-07-22 12:28:52 +0100 | [diff] [blame] | 5331 | remove_extent_mapping(map, em); |
| 5332 | /* once for the rb tree */ |
| 5333 | free_extent_map(em); |
Filipe Manana | 3d6448e | 2020-07-22 12:28:37 +0100 | [diff] [blame] | 5334 | next: |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5335 | start = extent_map_end(em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5336 | write_unlock(&map->lock); |
Chris Mason | 70dec80 | 2008-01-29 09:59:12 -0500 | [diff] [blame] | 5337 | |
| 5338 | /* once for us */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5339 | free_extent_map(em); |
Paul E. McKenney | 9f47eb5 | 2020-05-08 14:15:37 -0700 | [diff] [blame] | 5340 | |
| 5341 | cond_resched(); /* Allow large-extent preemption. */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5342 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5343 | } |
Nikolay Borisov | 29c68b2d | 2018-04-19 10:46:35 +0300 | [diff] [blame] | 5344 | return try_release_extent_state(tree, page, mask); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5345 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5346 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5347 | /* |
| 5348 | * helper function for fiemap, which doesn't want to see any holes. |
| 5349 | * This maps until we find something past 'last' |
| 5350 | */ |
Nikolay Borisov | f1bbde8 | 2020-08-31 14:42:45 +0300 | [diff] [blame] | 5351 | static struct extent_map *get_extent_skip_holes(struct btrfs_inode *inode, |
David Sterba | e3350e1 | 2017-06-23 04:09:57 +0200 | [diff] [blame] | 5352 | u64 offset, u64 last) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5353 | { |
Nikolay Borisov | f1bbde8 | 2020-08-31 14:42:45 +0300 | [diff] [blame] | 5354 | u64 sectorsize = btrfs_inode_sectorsize(inode); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5355 | struct extent_map *em; |
| 5356 | u64 len; |
| 5357 | |
| 5358 | if (offset >= last) |
| 5359 | return NULL; |
| 5360 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 5361 | while (1) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5362 | len = last - offset; |
| 5363 | if (len == 0) |
| 5364 | break; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5365 | len = ALIGN(len, sectorsize); |
Nikolay Borisov | f1bbde8 | 2020-08-31 14:42:45 +0300 | [diff] [blame] | 5366 | em = btrfs_get_extent_fiemap(inode, offset, len); |
Johannes Thumshirn | 6b5b7a4 | 2022-02-04 04:06:27 -0800 | [diff] [blame] | 5367 | if (IS_ERR(em)) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5368 | return em; |
| 5369 | |
| 5370 | /* if this isn't a hole return it */ |
Nikolay Borisov | 4a2d25c | 2017-11-23 10:51:43 +0200 | [diff] [blame] | 5371 | if (em->block_start != EXTENT_MAP_HOLE) |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5372 | return em; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5373 | |
| 5374 | /* this is a hole, advance to the next extent */ |
| 5375 | offset = extent_map_end(em); |
| 5376 | free_extent_map(em); |
| 5377 | if (offset >= last) |
| 5378 | break; |
| 5379 | } |
| 5380 | return NULL; |
| 5381 | } |
| 5382 | |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 5383 | /* |
| 5384 | * To cache previous fiemap extent |
| 5385 | * |
| 5386 | * Will be used for merging fiemap extent |
| 5387 | */ |
| 5388 | struct fiemap_cache { |
| 5389 | u64 offset; |
| 5390 | u64 phys; |
| 5391 | u64 len; |
| 5392 | u32 flags; |
| 5393 | bool cached; |
| 5394 | }; |
| 5395 | |
| 5396 | /* |
| 5397 | * Helper to submit fiemap extent. |
| 5398 | * |
| 5399 | * Will try to merge current fiemap extent specified by @offset, @phys, |
| 5400 | * @len and @flags with cached one. |
| 5401 | * And only when we fails to merge, cached one will be submitted as |
| 5402 | * fiemap extent. |
| 5403 | * |
| 5404 | * Return value is the same as fiemap_fill_next_extent(). |
| 5405 | */ |
| 5406 | static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo, |
| 5407 | struct fiemap_cache *cache, |
| 5408 | u64 offset, u64 phys, u64 len, u32 flags) |
| 5409 | { |
| 5410 | int ret = 0; |
| 5411 | |
| 5412 | if (!cache->cached) |
| 5413 | goto assign; |
| 5414 | |
| 5415 | /* |
| 5416 | * Sanity check, extent_fiemap() should have ensured that new |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 5417 | * fiemap extent won't overlap with cached one. |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 5418 | * Not recoverable. |
| 5419 | * |
| 5420 | * NOTE: Physical address can overlap, due to compression |
| 5421 | */ |
| 5422 | if (cache->offset + cache->len > offset) { |
| 5423 | WARN_ON(1); |
| 5424 | return -EINVAL; |
| 5425 | } |
| 5426 | |
| 5427 | /* |
| 5428 | * Only merges fiemap extents if |
| 5429 | * 1) Their logical addresses are continuous |
| 5430 | * |
| 5431 | * 2) Their physical addresses are continuous |
| 5432 | * So truly compressed (physical size smaller than logical size) |
| 5433 | * extents won't get merged with each other |
| 5434 | * |
| 5435 | * 3) Share same flags except FIEMAP_EXTENT_LAST |
| 5436 | * So regular extent won't get merged with prealloc extent |
| 5437 | */ |
| 5438 | if (cache->offset + cache->len == offset && |
| 5439 | cache->phys + cache->len == phys && |
| 5440 | (cache->flags & ~FIEMAP_EXTENT_LAST) == |
| 5441 | (flags & ~FIEMAP_EXTENT_LAST)) { |
| 5442 | cache->len += len; |
| 5443 | cache->flags |= flags; |
| 5444 | goto try_submit_last; |
| 5445 | } |
| 5446 | |
| 5447 | /* Not mergeable, need to submit cached one */ |
| 5448 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, |
| 5449 | cache->len, cache->flags); |
| 5450 | cache->cached = false; |
| 5451 | if (ret) |
| 5452 | return ret; |
| 5453 | assign: |
| 5454 | cache->cached = true; |
| 5455 | cache->offset = offset; |
| 5456 | cache->phys = phys; |
| 5457 | cache->len = len; |
| 5458 | cache->flags = flags; |
| 5459 | try_submit_last: |
| 5460 | if (cache->flags & FIEMAP_EXTENT_LAST) { |
| 5461 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, |
| 5462 | cache->phys, cache->len, cache->flags); |
| 5463 | cache->cached = false; |
| 5464 | } |
| 5465 | return ret; |
| 5466 | } |
| 5467 | |
| 5468 | /* |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 5469 | * Emit last fiemap cache |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 5470 | * |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 5471 | * The last fiemap cache may still be cached in the following case: |
| 5472 | * 0 4k 8k |
| 5473 | * |<- Fiemap range ->| |
| 5474 | * |<------------ First extent ----------->| |
| 5475 | * |
| 5476 | * In this case, the first extent range will be cached but not emitted. |
| 5477 | * So we must emit it before ending extent_fiemap(). |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 5478 | */ |
David Sterba | 5c5aff9 | 2019-03-20 11:29:46 +0100 | [diff] [blame] | 5479 | static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo, |
Qu Wenruo | 848c23b | 2017-06-22 10:01:21 +0800 | [diff] [blame] | 5480 | struct fiemap_cache *cache) |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 5481 | { |
| 5482 | int ret; |
| 5483 | |
| 5484 | if (!cache->cached) |
| 5485 | return 0; |
| 5486 | |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 5487 | ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys, |
| 5488 | cache->len, cache->flags); |
| 5489 | cache->cached = false; |
| 5490 | if (ret > 0) |
| 5491 | ret = 0; |
| 5492 | return ret; |
| 5493 | } |
| 5494 | |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 5495 | int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo, |
David Sterba | bab16e2 | 2020-06-23 20:56:12 +0200 | [diff] [blame] | 5496 | u64 start, u64 len) |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5497 | { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5498 | int ret = 0; |
Boris Burkov | 15c7745 | 2021-04-06 15:31:18 -0700 | [diff] [blame] | 5499 | u64 off; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5500 | u64 max = start + len; |
| 5501 | u32 flags = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5502 | u32 found_type; |
| 5503 | u64 last; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5504 | u64 last_for_get_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5505 | u64 disko = 0; |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 5506 | u64 isize = i_size_read(&inode->vfs_inode); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5507 | struct btrfs_key found_key; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5508 | struct extent_map *em = NULL; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 5509 | struct extent_state *cached_state = NULL; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5510 | struct btrfs_path *path; |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 5511 | struct btrfs_root *root = inode->root; |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 5512 | struct fiemap_cache cache = { 0 }; |
David Sterba | 5911c8f | 2019-05-15 15:31:04 +0200 | [diff] [blame] | 5513 | struct ulist *roots; |
| 5514 | struct ulist *tmp_ulist; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5515 | int end = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5516 | u64 em_start = 0; |
| 5517 | u64 em_len = 0; |
| 5518 | u64 em_end = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5519 | |
| 5520 | if (len == 0) |
| 5521 | return -EINVAL; |
| 5522 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5523 | path = btrfs_alloc_path(); |
| 5524 | if (!path) |
| 5525 | return -ENOMEM; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5526 | |
David Sterba | 5911c8f | 2019-05-15 15:31:04 +0200 | [diff] [blame] | 5527 | roots = ulist_alloc(GFP_KERNEL); |
| 5528 | tmp_ulist = ulist_alloc(GFP_KERNEL); |
| 5529 | if (!roots || !tmp_ulist) { |
| 5530 | ret = -ENOMEM; |
| 5531 | goto out_free_ulist; |
| 5532 | } |
| 5533 | |
Boris Burkov | 15c7745 | 2021-04-06 15:31:18 -0700 | [diff] [blame] | 5534 | /* |
| 5535 | * We can't initialize that to 'start' as this could miss extents due |
| 5536 | * to extent item merging |
| 5537 | */ |
| 5538 | off = 0; |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 5539 | start = round_down(start, btrfs_inode_sectorsize(inode)); |
| 5540 | len = round_up(max, btrfs_inode_sectorsize(inode)) - start; |
Josef Bacik | 4d479cf | 2011-11-17 11:34:31 -0500 | [diff] [blame] | 5541 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5542 | /* |
| 5543 | * lookup the last file extent. We're not using i_size here |
| 5544 | * because there might be preallocation past i_size |
| 5545 | */ |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 5546 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1, |
| 5547 | 0); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5548 | if (ret < 0) { |
David Sterba | 5911c8f | 2019-05-15 15:31:04 +0200 | [diff] [blame] | 5549 | goto out_free_ulist; |
Liu Bo | 2d324f5 | 2016-05-17 17:21:48 -0700 | [diff] [blame] | 5550 | } else { |
| 5551 | WARN_ON(!ret); |
| 5552 | if (ret == 1) |
| 5553 | ret = 0; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5554 | } |
Liu Bo | 2d324f5 | 2016-05-17 17:21:48 -0700 | [diff] [blame] | 5555 | |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5556 | path->slots[0]--; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5557 | 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] | 5558 | found_type = found_key.type; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5559 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5560 | /* No extents, but there might be delalloc bits */ |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 5561 | if (found_key.objectid != btrfs_ino(inode) || |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5562 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5563 | /* have to trust i_size as the end */ |
| 5564 | last = (u64)-1; |
| 5565 | last_for_get_extent = isize; |
| 5566 | } else { |
| 5567 | /* |
| 5568 | * remember the start of the last extent. There are a |
| 5569 | * bunch of different factors that go into the length of the |
| 5570 | * extent, so its much less complex to remember where it started |
| 5571 | */ |
| 5572 | last = found_key.offset; |
| 5573 | last_for_get_extent = last + 1; |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5574 | } |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 5575 | btrfs_release_path(path); |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5576 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5577 | /* |
| 5578 | * we might have some extents allocated but more delalloc past those |
| 5579 | * extents. so, we trust isize unless the start of the last extent is |
| 5580 | * beyond isize |
| 5581 | */ |
| 5582 | if (last < isize) { |
| 5583 | last = (u64)-1; |
| 5584 | last_for_get_extent = isize; |
| 5585 | } |
| 5586 | |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 5587 | lock_extent_bits(&inode->io_tree, start, start + len - 1, |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 5588 | &cached_state); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5589 | |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 5590 | em = get_extent_skip_holes(inode, start, last_for_get_extent); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5591 | if (!em) |
| 5592 | goto out; |
| 5593 | if (IS_ERR(em)) { |
| 5594 | ret = PTR_ERR(em); |
| 5595 | goto out; |
| 5596 | } |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5597 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5598 | while (!end) { |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 5599 | u64 offset_in_extent = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5600 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 5601 | /* break if the extent we found is outside the range */ |
| 5602 | if (em->start >= max || extent_map_end(em) < off) |
| 5603 | break; |
| 5604 | |
| 5605 | /* |
| 5606 | * get_extent may return an extent that starts before our |
| 5607 | * requested range. We have to make sure the ranges |
| 5608 | * we return to fiemap always move forward and don't |
| 5609 | * overlap, so adjust the offsets here |
| 5610 | */ |
| 5611 | em_start = max(em->start, off); |
| 5612 | |
| 5613 | /* |
| 5614 | * record the offset from the start of the extent |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 5615 | * for adjusting the disk offset below. Only do this if the |
| 5616 | * extent isn't compressed since our in ram offset may be past |
| 5617 | * what we have actually allocated on disk. |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 5618 | */ |
Josef Bacik | b76bb70 | 2013-07-05 13:52:51 -0400 | [diff] [blame] | 5619 | if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 5620 | offset_in_extent = em_start - em->start; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5621 | em_end = extent_map_end(em); |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 5622 | em_len = em_end - em_start; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5623 | flags = 0; |
Filipe Manana | f098631 | 2018-06-20 10:02:30 +0100 | [diff] [blame] | 5624 | if (em->block_start < EXTENT_MAP_LAST_BYTE) |
| 5625 | disko = em->block_start + offset_in_extent; |
| 5626 | else |
| 5627 | disko = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5628 | |
Chris Mason | ea8efc7 | 2011-03-08 11:54:40 -0500 | [diff] [blame] | 5629 | /* |
| 5630 | * bump off for our next call to get_extent |
| 5631 | */ |
| 5632 | off = extent_map_end(em); |
| 5633 | if (off >= max) |
| 5634 | end = 1; |
| 5635 | |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 5636 | if (em->block_start == EXTENT_MAP_LAST_BYTE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5637 | end = 1; |
| 5638 | flags |= FIEMAP_EXTENT_LAST; |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 5639 | } else if (em->block_start == EXTENT_MAP_INLINE) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5640 | flags |= (FIEMAP_EXTENT_DATA_INLINE | |
| 5641 | FIEMAP_EXTENT_NOT_ALIGNED); |
Heiko Carstens | 93dbfad | 2009-04-03 10:33:45 -0400 | [diff] [blame] | 5642 | } else if (em->block_start == EXTENT_MAP_DELALLOC) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5643 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 5644 | FIEMAP_EXTENT_UNKNOWN); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 5645 | } else if (fieinfo->fi_extents_max) { |
| 5646 | u64 bytenr = em->block_start - |
| 5647 | (em->start - em->orig_start); |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 5648 | |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 5649 | /* |
| 5650 | * As btrfs supports shared space, this information |
| 5651 | * can be exported to userspace tools via |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 5652 | * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0 |
| 5653 | * then we're just getting a count and we can skip the |
| 5654 | * lookup stuff. |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 5655 | */ |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 5656 | ret = btrfs_check_shared(root, btrfs_ino(inode), |
David Sterba | 5911c8f | 2019-05-15 15:31:04 +0200 | [diff] [blame] | 5657 | bytenr, roots, tmp_ulist); |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 5658 | if (ret < 0) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 5659 | goto out_free; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 5660 | if (ret) |
Liu Bo | fe09e16 | 2013-09-22 12:54:23 +0800 | [diff] [blame] | 5661 | flags |= FIEMAP_EXTENT_SHARED; |
Josef Bacik | dc046b1 | 2014-09-10 16:20:45 -0400 | [diff] [blame] | 5662 | ret = 0; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5663 | } |
| 5664 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
| 5665 | flags |= FIEMAP_EXTENT_ENCODED; |
Josef Bacik | 0d2b237 | 2015-05-19 10:44:04 -0400 | [diff] [blame] | 5666 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 5667 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5668 | |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5669 | free_extent_map(em); |
| 5670 | em = NULL; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5671 | if ((em_start >= last) || em_len == (u64)-1 || |
| 5672 | (last == (u64)-1 && isize <= em_end)) { |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5673 | flags |= FIEMAP_EXTENT_LAST; |
| 5674 | end = 1; |
| 5675 | } |
| 5676 | |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5677 | /* now scan forward to see if this is really the last extent. */ |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 5678 | em = get_extent_skip_holes(inode, off, last_for_get_extent); |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5679 | if (IS_ERR(em)) { |
| 5680 | ret = PTR_ERR(em); |
| 5681 | goto out; |
| 5682 | } |
| 5683 | if (!em) { |
Josef Bacik | 975f84f | 2010-11-23 19:36:57 +0000 | [diff] [blame] | 5684 | flags |= FIEMAP_EXTENT_LAST; |
| 5685 | end = 1; |
| 5686 | } |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 5687 | ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko, |
| 5688 | em_len, flags); |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 5689 | if (ret) { |
| 5690 | if (ret == 1) |
| 5691 | ret = 0; |
Chris Mason | ec29ed5 | 2011-02-23 16:23:20 -0500 | [diff] [blame] | 5692 | goto out_free; |
Chengyu Song | 26e726a | 2015-03-24 18:12:56 -0400 | [diff] [blame] | 5693 | } |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5694 | } |
| 5695 | out_free: |
Qu Wenruo | 4751832 | 2017-04-07 10:43:15 +0800 | [diff] [blame] | 5696 | if (!ret) |
David Sterba | 5c5aff9 | 2019-03-20 11:29:46 +0100 | [diff] [blame] | 5697 | ret = emit_last_fiemap_cache(fieinfo, &cache); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5698 | free_extent_map(em); |
| 5699 | out: |
Nikolay Borisov | facee0a | 2020-08-31 14:42:49 +0300 | [diff] [blame] | 5700 | unlock_extent_cached(&inode->io_tree, start, start + len - 1, |
David Sterba | e43bbe5 | 2017-12-12 21:43:52 +0100 | [diff] [blame] | 5701 | &cached_state); |
David Sterba | 5911c8f | 2019-05-15 15:31:04 +0200 | [diff] [blame] | 5702 | |
| 5703 | out_free_ulist: |
Colin Ian King | e02d48e | 2019-07-05 08:26:24 +0100 | [diff] [blame] | 5704 | btrfs_free_path(path); |
David Sterba | 5911c8f | 2019-05-15 15:31:04 +0200 | [diff] [blame] | 5705 | ulist_free(roots); |
| 5706 | ulist_free(tmp_ulist); |
Yehuda Sadeh | 1506fcc | 2009-01-21 14:39:14 -0500 | [diff] [blame] | 5707 | return ret; |
| 5708 | } |
| 5709 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5710 | static void __free_extent_buffer(struct extent_buffer *eb) |
| 5711 | { |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 5712 | kmem_cache_free(extent_buffer_cache, eb); |
| 5713 | } |
| 5714 | |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 5715 | int extent_buffer_under_io(const struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5716 | { |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5717 | return (atomic_read(&eb->io_pages) || |
| 5718 | test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || |
| 5719 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5720 | } |
| 5721 | |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 5722 | static bool page_range_has_eb(struct btrfs_fs_info *fs_info, struct page *page) |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5723 | { |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 5724 | struct btrfs_subpage *subpage; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5725 | |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 5726 | lockdep_assert_held(&page->mapping->private_lock); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5727 | |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 5728 | if (PagePrivate(page)) { |
| 5729 | subpage = (struct btrfs_subpage *)page->private; |
| 5730 | if (atomic_read(&subpage->eb_refs)) |
| 5731 | return true; |
Qu Wenruo | 3d078ef | 2021-06-07 17:02:58 +0800 | [diff] [blame] | 5732 | /* |
| 5733 | * Even there is no eb refs here, we may still have |
| 5734 | * end_page_read() call relying on page::private. |
| 5735 | */ |
| 5736 | if (atomic_read(&subpage->readers)) |
| 5737 | return true; |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 5738 | } |
| 5739 | return false; |
| 5740 | } |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5741 | |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 5742 | static void detach_extent_buffer_page(struct extent_buffer *eb, struct page *page) |
| 5743 | { |
| 5744 | struct btrfs_fs_info *fs_info = eb->fs_info; |
| 5745 | const bool mapped = !test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); |
| 5746 | |
| 5747 | /* |
| 5748 | * For mapped eb, we're going to change the page private, which should |
| 5749 | * be done under the private_lock. |
| 5750 | */ |
| 5751 | if (mapped) |
| 5752 | spin_lock(&page->mapping->private_lock); |
| 5753 | |
| 5754 | if (!PagePrivate(page)) { |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 5755 | if (mapped) |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 5756 | spin_unlock(&page->mapping->private_lock); |
| 5757 | return; |
| 5758 | } |
| 5759 | |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 5760 | if (fs_info->nodesize >= PAGE_SIZE) { |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 5761 | /* |
| 5762 | * We do this since we'll remove the pages after we've |
| 5763 | * removed the eb from the radix tree, so we could race |
| 5764 | * and have this page now attached to the new eb. So |
| 5765 | * only clear page_private if it's still connected to |
| 5766 | * this eb. |
| 5767 | */ |
| 5768 | if (PagePrivate(page) && |
| 5769 | page->private == (unsigned long)eb) { |
| 5770 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
| 5771 | BUG_ON(PageDirty(page)); |
| 5772 | BUG_ON(PageWriteback(page)); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5773 | /* |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 5774 | * We need to make sure we haven't be attached |
| 5775 | * to a new eb. |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5776 | */ |
Guoqing Jiang | d1b89bc | 2020-06-01 21:47:45 -0700 | [diff] [blame] | 5777 | detach_page_private(page); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 5778 | } |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 5779 | if (mapped) |
| 5780 | spin_unlock(&page->mapping->private_lock); |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 5781 | return; |
| 5782 | } |
| 5783 | |
| 5784 | /* |
| 5785 | * For subpage, we can have dummy eb with page private. In this case, |
| 5786 | * we can directly detach the private as such page is only attached to |
| 5787 | * one dummy eb, no sharing. |
| 5788 | */ |
| 5789 | if (!mapped) { |
| 5790 | btrfs_detach_subpage(fs_info, page); |
| 5791 | return; |
| 5792 | } |
| 5793 | |
| 5794 | btrfs_page_dec_eb_refs(fs_info, page); |
| 5795 | |
| 5796 | /* |
| 5797 | * We can only detach the page private if there are no other ebs in the |
Qu Wenruo | 3d078ef | 2021-06-07 17:02:58 +0800 | [diff] [blame] | 5798 | * page range and no unfinished IO. |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 5799 | */ |
| 5800 | if (!page_range_has_eb(fs_info, page)) |
| 5801 | btrfs_detach_subpage(fs_info, page); |
| 5802 | |
| 5803 | spin_unlock(&page->mapping->private_lock); |
| 5804 | } |
| 5805 | |
| 5806 | /* Release all pages attached to the extent buffer */ |
| 5807 | static void btrfs_release_extent_buffer_pages(struct extent_buffer *eb) |
| 5808 | { |
| 5809 | int i; |
| 5810 | int num_pages; |
| 5811 | |
| 5812 | ASSERT(!extent_buffer_under_io(eb)); |
| 5813 | |
| 5814 | num_pages = num_extent_pages(eb); |
| 5815 | for (i = 0; i < num_pages; i++) { |
| 5816 | struct page *page = eb->pages[i]; |
| 5817 | |
| 5818 | if (!page) |
| 5819 | continue; |
| 5820 | |
| 5821 | detach_extent_buffer_page(eb, page); |
Forrest Liu | 5d2361d | 2015-02-09 17:31:45 +0800 | [diff] [blame] | 5822 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5823 | /* One for when we allocated the page */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 5824 | put_page(page); |
Nikolay Borisov | d64766f | 2018-06-27 16:38:22 +0300 | [diff] [blame] | 5825 | } |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5826 | } |
| 5827 | |
| 5828 | /* |
| 5829 | * Helper for releasing the extent buffer. |
| 5830 | */ |
| 5831 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) |
| 5832 | { |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 5833 | btrfs_release_extent_buffer_pages(eb); |
Josef Bacik | 8c38938 | 2020-02-14 16:11:42 -0500 | [diff] [blame] | 5834 | btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list); |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 5835 | __free_extent_buffer(eb); |
| 5836 | } |
| 5837 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5838 | static struct extent_buffer * |
| 5839 | __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start, |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 5840 | unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5841 | { |
| 5842 | struct extent_buffer *eb = NULL; |
| 5843 | |
Michal Hocko | d1b5c567 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 5844 | eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5845 | eb->start = start; |
| 5846 | eb->len = len; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 5847 | eb->fs_info = fs_info; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5848 | eb->bflags = 0; |
Josef Bacik | 196d59a | 2020-08-20 11:46:09 -0400 | [diff] [blame] | 5849 | init_rwsem(&eb->lock); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5850 | |
Josef Bacik | 3fd6372 | 2020-02-14 16:11:40 -0500 | [diff] [blame] | 5851 | btrfs_leak_debug_add(&fs_info->eb_leak_lock, &eb->leak_list, |
| 5852 | &fs_info->allocated_ebs); |
Naohiro Aota | d3575156 | 2021-02-04 19:21:54 +0900 | [diff] [blame] | 5853 | INIT_LIST_HEAD(&eb->release_list); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5854 | |
| 5855 | spin_lock_init(&eb->refs_lock); |
| 5856 | atomic_set(&eb->refs, 1); |
| 5857 | atomic_set(&eb->io_pages, 0); |
| 5858 | |
Qu Wenruo | deb6789 | 2020-12-02 14:48:01 +0800 | [diff] [blame] | 5859 | ASSERT(len <= BTRFS_MAX_METADATA_BLOCKSIZE); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5860 | |
| 5861 | return eb; |
| 5862 | } |
| 5863 | |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 5864 | struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5865 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5866 | int i; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5867 | struct extent_buffer *new; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5868 | int num_pages = num_extent_pages(src); |
Sweet Tea Dorminy | dd137dd | 2022-03-30 16:11:22 -0400 | [diff] [blame] | 5869 | int ret; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5870 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 5871 | new = __alloc_extent_buffer(src->fs_info, src->start, src->len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5872 | if (new == NULL) |
| 5873 | return NULL; |
| 5874 | |
Qu Wenruo | 62c053f | 2021-01-26 16:33:46 +0800 | [diff] [blame] | 5875 | /* |
| 5876 | * Set UNMAPPED before calling btrfs_release_extent_buffer(), as |
| 5877 | * btrfs_release_extent_buffer() have different behavior for |
| 5878 | * UNMAPPED subpage extent buffer. |
| 5879 | */ |
| 5880 | set_bit(EXTENT_BUFFER_UNMAPPED, &new->bflags); |
| 5881 | |
Sweet Tea Dorminy | dd137dd | 2022-03-30 16:11:22 -0400 | [diff] [blame] | 5882 | memset(new->pages, 0, sizeof(*new->pages) * num_pages); |
| 5883 | ret = btrfs_alloc_page_array(num_pages, new->pages); |
| 5884 | if (ret) { |
| 5885 | btrfs_release_extent_buffer(new); |
| 5886 | return NULL; |
| 5887 | } |
| 5888 | |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5889 | for (i = 0; i < num_pages; i++) { |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 5890 | int ret; |
Sweet Tea Dorminy | dd137dd | 2022-03-30 16:11:22 -0400 | [diff] [blame] | 5891 | struct page *p = new->pages[i]; |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 5892 | |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 5893 | ret = attach_extent_buffer_page(new, p, NULL); |
| 5894 | if (ret < 0) { |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 5895 | btrfs_release_extent_buffer(new); |
| 5896 | return NULL; |
| 5897 | } |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5898 | WARN_ON(PageDirty(p)); |
David Sterba | fba1acf | 2016-11-08 17:56:24 +0100 | [diff] [blame] | 5899 | copy_page(page_address(p), page_address(src->pages[i])); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5900 | } |
Qu Wenruo | 92d83e9 | 2021-01-26 16:33:55 +0800 | [diff] [blame] | 5901 | set_extent_buffer_uptodate(new); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5902 | |
| 5903 | return new; |
| 5904 | } |
| 5905 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 5906 | struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, |
| 5907 | u64 start, unsigned long len) |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5908 | { |
| 5909 | struct extent_buffer *eb; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5910 | int num_pages; |
| 5911 | int i; |
Sweet Tea Dorminy | dd137dd | 2022-03-30 16:11:22 -0400 | [diff] [blame] | 5912 | int ret; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5913 | |
David Sterba | 3f556f7 | 2014-06-15 03:20:26 +0200 | [diff] [blame] | 5914 | eb = __alloc_extent_buffer(fs_info, start, len); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5915 | if (!eb) |
| 5916 | return NULL; |
| 5917 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5918 | num_pages = num_extent_pages(eb); |
Sweet Tea Dorminy | dd137dd | 2022-03-30 16:11:22 -0400 | [diff] [blame] | 5919 | ret = btrfs_alloc_page_array(num_pages, eb->pages); |
| 5920 | if (ret) |
| 5921 | goto err; |
Qu Wenruo | 09bc1f0 | 2021-01-26 16:33:51 +0800 | [diff] [blame] | 5922 | |
Sweet Tea Dorminy | dd137dd | 2022-03-30 16:11:22 -0400 | [diff] [blame] | 5923 | for (i = 0; i < num_pages; i++) { |
| 5924 | struct page *p = eb->pages[i]; |
| 5925 | |
| 5926 | ret = attach_extent_buffer_page(eb, p, NULL); |
Qu Wenruo | 09bc1f0 | 2021-01-26 16:33:51 +0800 | [diff] [blame] | 5927 | if (ret < 0) |
| 5928 | goto err; |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5929 | } |
Sweet Tea Dorminy | dd137dd | 2022-03-30 16:11:22 -0400 | [diff] [blame] | 5930 | |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5931 | set_extent_buffer_uptodate(eb); |
| 5932 | btrfs_set_header_nritems(eb, 0); |
Nikolay Borisov | b0132a3b | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 5933 | set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5934 | |
| 5935 | return eb; |
| 5936 | err: |
Sweet Tea Dorminy | dd137dd | 2022-03-30 16:11:22 -0400 | [diff] [blame] | 5937 | for (i = 0; i < num_pages; i++) { |
| 5938 | if (eb->pages[i]) { |
| 5939 | detach_extent_buffer_page(eb, eb->pages[i]); |
| 5940 | __free_page(eb->pages[i]); |
| 5941 | } |
Qu Wenruo | 09bc1f0 | 2021-01-26 16:33:51 +0800 | [diff] [blame] | 5942 | } |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 5943 | __free_extent_buffer(eb); |
| 5944 | return NULL; |
| 5945 | } |
| 5946 | |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 5947 | 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] | 5948 | u64 start) |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 5949 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 5950 | return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize); |
Omar Sandoval | 0f33122 | 2015-09-29 20:50:31 -0700 | [diff] [blame] | 5951 | } |
| 5952 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5953 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
| 5954 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5955 | int refs; |
Boris Burkov | 6bf9cd2 | 2020-06-17 11:35:19 -0700 | [diff] [blame] | 5956 | /* |
| 5957 | * The TREE_REF bit is first set when the extent_buffer is added |
| 5958 | * to the radix tree. It is also reset, if unset, when a new reference |
| 5959 | * is created by find_extent_buffer. |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5960 | * |
Boris Burkov | 6bf9cd2 | 2020-06-17 11:35:19 -0700 | [diff] [blame] | 5961 | * It is only cleared in two cases: freeing the last non-tree |
| 5962 | * reference to the extent_buffer when its STALE bit is set or |
Matthew Wilcox (Oracle) | f913cff | 2022-04-30 23:15:16 -0400 | [diff] [blame] | 5963 | * calling release_folio when the tree reference is the only reference. |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5964 | * |
Boris Burkov | 6bf9cd2 | 2020-06-17 11:35:19 -0700 | [diff] [blame] | 5965 | * In both cases, care is taken to ensure that the extent_buffer's |
Matthew Wilcox (Oracle) | f913cff | 2022-04-30 23:15:16 -0400 | [diff] [blame] | 5966 | * pages are not under io. However, release_folio can be concurrently |
Boris Burkov | 6bf9cd2 | 2020-06-17 11:35:19 -0700 | [diff] [blame] | 5967 | * called with creating new references, which is prone to race |
| 5968 | * conditions between the calls to check_buffer_tree_ref in those |
| 5969 | * codepaths and clearing TREE_REF in try_release_extent_buffer. |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5970 | * |
Boris Burkov | 6bf9cd2 | 2020-06-17 11:35:19 -0700 | [diff] [blame] | 5971 | * The actual lifetime of the extent_buffer in the radix tree is |
| 5972 | * adequately protected by the refcount, but the TREE_REF bit and |
| 5973 | * its corresponding reference are not. To protect against this |
| 5974 | * class of races, we call check_buffer_tree_ref from the codepaths |
| 5975 | * which trigger io after they set eb->io_pages. Note that once io is |
| 5976 | * initiated, TREE_REF can no longer be cleared, so that is the |
| 5977 | * moment at which any such race is best fixed. |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5978 | */ |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 5979 | refs = atomic_read(&eb->refs); |
| 5980 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 5981 | return; |
| 5982 | |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 5983 | spin_lock(&eb->refs_lock); |
| 5984 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5985 | atomic_inc(&eb->refs); |
Josef Bacik | 594831c | 2012-07-20 16:11:08 -0400 | [diff] [blame] | 5986 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5987 | } |
| 5988 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 5989 | static void mark_extent_buffer_accessed(struct extent_buffer *eb, |
| 5990 | struct page *accessed) |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 5991 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 5992 | int num_pages, i; |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 5993 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 5994 | check_buffer_tree_ref(eb); |
| 5995 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 5996 | num_pages = num_extent_pages(eb); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 5997 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 5998 | struct page *p = eb->pages[i]; |
| 5999 | |
Mel Gorman | 2457aec | 2014-06-04 16:10:31 -0700 | [diff] [blame] | 6000 | if (p != accessed) |
| 6001 | mark_page_accessed(p); |
Josef Bacik | 5df4235 | 2012-03-15 18:24:42 -0400 | [diff] [blame] | 6002 | } |
| 6003 | } |
| 6004 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 6005 | struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, |
| 6006 | u64 start) |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 6007 | { |
| 6008 | struct extent_buffer *eb; |
| 6009 | |
Qu Wenruo | 2f3186d | 2021-04-06 08:36:00 +0800 | [diff] [blame] | 6010 | eb = find_extent_buffer_nolock(fs_info, start); |
| 6011 | if (!eb) |
| 6012 | return NULL; |
| 6013 | /* |
| 6014 | * Lock our eb's refs_lock to avoid races with free_extent_buffer(). |
| 6015 | * When we get our eb it might be flagged with EXTENT_BUFFER_STALE and |
| 6016 | * another task running free_extent_buffer() might have seen that flag |
| 6017 | * set, eb->refs == 2, that the buffer isn't under IO (dirty and |
| 6018 | * writeback flags not set) and it's still in the tree (flag |
| 6019 | * EXTENT_BUFFER_TREE_REF set), therefore being in the process of |
| 6020 | * decrementing the extent buffer's reference count twice. So here we |
| 6021 | * could race and increment the eb's reference count, clear its stale |
| 6022 | * flag, mark it as dirty and drop our reference before the other task |
| 6023 | * finishes executing free_extent_buffer, which would later result in |
| 6024 | * an attempt to free an extent buffer that is dirty. |
| 6025 | */ |
| 6026 | if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) { |
| 6027 | spin_lock(&eb->refs_lock); |
| 6028 | spin_unlock(&eb->refs_lock); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 6029 | } |
Qu Wenruo | 2f3186d | 2021-04-06 08:36:00 +0800 | [diff] [blame] | 6030 | mark_extent_buffer_accessed(eb, NULL); |
| 6031 | return eb; |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 6032 | } |
| 6033 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 6034 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 6035 | 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] | 6036 | u64 start) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 6037 | { |
| 6038 | struct extent_buffer *eb, *exists = NULL; |
| 6039 | int ret; |
| 6040 | |
| 6041 | eb = find_extent_buffer(fs_info, start); |
| 6042 | if (eb) |
| 6043 | return eb; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 6044 | eb = alloc_dummy_extent_buffer(fs_info, start); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 6045 | if (!eb) |
Dan Carpenter | b6293c8 | 2019-12-03 14:24:58 +0300 | [diff] [blame] | 6046 | return ERR_PTR(-ENOMEM); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 6047 | eb->fs_info = fs_info; |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 6048 | again: |
| 6049 | ret = radix_tree_preload(GFP_NOFS); |
| 6050 | if (ret) { |
| 6051 | exists = ERR_PTR(ret); |
| 6052 | goto free_eb; |
| 6053 | } |
| 6054 | spin_lock(&fs_info->buffer_lock); |
| 6055 | ret = radix_tree_insert(&fs_info->buffer_radix, |
| 6056 | start >> fs_info->sectorsize_bits, eb); |
| 6057 | spin_unlock(&fs_info->buffer_lock); |
| 6058 | radix_tree_preload_end(); |
| 6059 | if (ret == -EEXIST) { |
| 6060 | exists = find_extent_buffer(fs_info, start); |
| 6061 | if (exists) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 6062 | goto free_eb; |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 6063 | else |
| 6064 | goto again; |
| 6065 | } |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 6066 | check_buffer_tree_ref(eb); |
| 6067 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
| 6068 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 6069 | return eb; |
| 6070 | free_eb: |
| 6071 | btrfs_release_extent_buffer(eb); |
| 6072 | return exists; |
| 6073 | } |
| 6074 | #endif |
| 6075 | |
Qu Wenruo | 81982210 | 2021-01-26 16:33:49 +0800 | [diff] [blame] | 6076 | static struct extent_buffer *grab_extent_buffer( |
| 6077 | struct btrfs_fs_info *fs_info, struct page *page) |
Qu Wenruo | c0f0a9e | 2021-01-06 09:01:45 +0800 | [diff] [blame] | 6078 | { |
| 6079 | struct extent_buffer *exists; |
| 6080 | |
Qu Wenruo | 81982210 | 2021-01-26 16:33:49 +0800 | [diff] [blame] | 6081 | /* |
| 6082 | * For subpage case, we completely rely on radix tree to ensure we |
| 6083 | * don't try to insert two ebs for the same bytenr. So here we always |
| 6084 | * return NULL and just continue. |
| 6085 | */ |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 6086 | if (fs_info->nodesize < PAGE_SIZE) |
Qu Wenruo | 81982210 | 2021-01-26 16:33:49 +0800 | [diff] [blame] | 6087 | return NULL; |
| 6088 | |
Qu Wenruo | c0f0a9e | 2021-01-06 09:01:45 +0800 | [diff] [blame] | 6089 | /* Page not yet attached to an extent buffer */ |
| 6090 | if (!PagePrivate(page)) |
| 6091 | return NULL; |
| 6092 | |
| 6093 | /* |
| 6094 | * We could have already allocated an eb for this page and attached one |
| 6095 | * so lets see if we can get a ref on the existing eb, and if we can we |
| 6096 | * know it's good and we can just return that one, else we know we can |
| 6097 | * just overwrite page->private. |
| 6098 | */ |
| 6099 | exists = (struct extent_buffer *)page->private; |
| 6100 | if (atomic_inc_not_zero(&exists->refs)) |
| 6101 | return exists; |
| 6102 | |
| 6103 | WARN_ON(PageDirty(page)); |
| 6104 | detach_page_private(page); |
| 6105 | return NULL; |
| 6106 | } |
| 6107 | |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 6108 | static int check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start) |
| 6109 | { |
| 6110 | if (!IS_ALIGNED(start, fs_info->sectorsize)) { |
| 6111 | btrfs_err(fs_info, "bad tree block start %llu", start); |
| 6112 | return -EINVAL; |
| 6113 | } |
| 6114 | |
| 6115 | if (fs_info->nodesize < PAGE_SIZE && |
| 6116 | offset_in_page(start) + fs_info->nodesize > PAGE_SIZE) { |
| 6117 | btrfs_err(fs_info, |
| 6118 | "tree block crosses page boundary, start %llu nodesize %u", |
| 6119 | start, fs_info->nodesize); |
| 6120 | return -EINVAL; |
| 6121 | } |
| 6122 | if (fs_info->nodesize >= PAGE_SIZE && |
Fanjun Kong | 1280d2d | 2022-05-26 22:35:40 +0800 | [diff] [blame] | 6123 | !PAGE_ALIGNED(start)) { |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 6124 | btrfs_err(fs_info, |
| 6125 | "tree block is not page aligned, start %llu nodesize %u", |
| 6126 | start, fs_info->nodesize); |
| 6127 | return -EINVAL; |
| 6128 | } |
| 6129 | return 0; |
| 6130 | } |
| 6131 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 6132 | struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, |
Josef Bacik | 3fbaf25 | 2020-11-05 10:45:20 -0500 | [diff] [blame] | 6133 | u64 start, u64 owner_root, int level) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6134 | { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 6135 | unsigned long len = fs_info->nodesize; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 6136 | int num_pages; |
| 6137 | int i; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6138 | unsigned long index = start >> PAGE_SHIFT; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6139 | struct extent_buffer *eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 6140 | struct extent_buffer *exists = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6141 | struct page *p; |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 6142 | struct address_space *mapping = fs_info->btree_inode->i_mapping; |
Josef Bacik | b40130b | 2022-07-26 16:24:04 -0400 | [diff] [blame] | 6143 | u64 lockdep_owner = owner_root; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6144 | int uptodate = 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 6145 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6146 | |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 6147 | if (check_eb_alignment(fs_info, start)) |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6148 | return ERR_PTR(-EINVAL); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6149 | |
Qu Wenruo | e9306ad | 2021-02-25 09:18:14 +0800 | [diff] [blame] | 6150 | #if BITS_PER_LONG == 32 |
| 6151 | if (start >= MAX_LFS_FILESIZE) { |
| 6152 | btrfs_err_rl(fs_info, |
| 6153 | "extent buffer %llu is beyond 32bit page cache limit", start); |
| 6154 | btrfs_err_32bit_limit(fs_info); |
| 6155 | return ERR_PTR(-EOVERFLOW); |
| 6156 | } |
| 6157 | if (start >= BTRFS_32BIT_EARLY_WARN_THRESHOLD) |
| 6158 | btrfs_warn_32bit_limit(fs_info); |
| 6159 | #endif |
| 6160 | |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 6161 | eb = find_extent_buffer(fs_info, start); |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 6162 | if (eb) |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 6163 | return eb; |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 6164 | |
David Sterba | 23d79d8 | 2014-06-15 02:55:29 +0200 | [diff] [blame] | 6165 | eb = __alloc_extent_buffer(fs_info, start, len); |
Peter | 2b114d1 | 2008-04-01 11:21:40 -0400 | [diff] [blame] | 6166 | if (!eb) |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6167 | return ERR_PTR(-ENOMEM); |
Josef Bacik | b40130b | 2022-07-26 16:24:04 -0400 | [diff] [blame] | 6168 | |
| 6169 | /* |
| 6170 | * The reloc trees are just snapshots, so we need them to appear to be |
| 6171 | * just like any other fs tree WRT lockdep. |
| 6172 | */ |
| 6173 | if (lockdep_owner == BTRFS_TREE_RELOC_OBJECTID) |
| 6174 | lockdep_owner = BTRFS_FS_TREE_OBJECTID; |
| 6175 | |
| 6176 | btrfs_set_buffer_lockdep_class(lockdep_owner, eb, level); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6177 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 6178 | num_pages = num_extent_pages(eb); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6179 | for (i = 0; i < num_pages; i++, index++) { |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 6180 | struct btrfs_subpage *prealloc = NULL; |
| 6181 | |
Michal Hocko | d1b5c567 | 2015-08-19 14:17:40 +0200 | [diff] [blame] | 6182 | p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6183 | if (!p) { |
| 6184 | exists = ERR_PTR(-ENOMEM); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 6185 | goto free_eb; |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6186 | } |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 6187 | |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 6188 | /* |
| 6189 | * Preallocate page->private for subpage case, so that we won't |
| 6190 | * allocate memory with private_lock hold. The memory will be |
| 6191 | * freed by attach_extent_buffer_page() or freed manually if |
| 6192 | * we exit earlier. |
| 6193 | * |
| 6194 | * Although we have ensured one subpage eb can only have one |
| 6195 | * page, but it may change in the future for 16K page size |
| 6196 | * support, so we still preallocate the memory in the loop. |
| 6197 | */ |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 6198 | if (fs_info->nodesize < PAGE_SIZE) { |
Qu Wenruo | 651fb41 | 2021-08-17 17:38:50 +0800 | [diff] [blame] | 6199 | prealloc = btrfs_alloc_subpage(fs_info, BTRFS_SUBPAGE_METADATA); |
| 6200 | if (IS_ERR(prealloc)) { |
| 6201 | ret = PTR_ERR(prealloc); |
Qu Wenruo | fdf250d | 2021-08-17 17:38:49 +0800 | [diff] [blame] | 6202 | unlock_page(p); |
| 6203 | put_page(p); |
| 6204 | exists = ERR_PTR(ret); |
| 6205 | goto free_eb; |
| 6206 | } |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 6207 | } |
| 6208 | |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 6209 | spin_lock(&mapping->private_lock); |
Qu Wenruo | 81982210 | 2021-01-26 16:33:49 +0800 | [diff] [blame] | 6210 | exists = grab_extent_buffer(fs_info, p); |
Qu Wenruo | c0f0a9e | 2021-01-06 09:01:45 +0800 | [diff] [blame] | 6211 | if (exists) { |
| 6212 | spin_unlock(&mapping->private_lock); |
| 6213 | unlock_page(p); |
| 6214 | put_page(p); |
| 6215 | mark_extent_buffer_accessed(exists, p); |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 6216 | btrfs_free_subpage(prealloc); |
Qu Wenruo | c0f0a9e | 2021-01-06 09:01:45 +0800 | [diff] [blame] | 6217 | goto free_eb; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6218 | } |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 6219 | /* Should not fail, as we have preallocated the memory */ |
| 6220 | ret = attach_extent_buffer_page(eb, p, prealloc); |
| 6221 | ASSERT(!ret); |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 6222 | /* |
| 6223 | * To inform we have extra eb under allocation, so that |
| 6224 | * detach_extent_buffer_page() won't release the page private |
| 6225 | * when the eb hasn't yet been inserted into radix tree. |
| 6226 | * |
| 6227 | * The ref will be decreased when the eb released the page, in |
| 6228 | * detach_extent_buffer_page(). |
| 6229 | * Thus needs no special handling in error path. |
| 6230 | */ |
| 6231 | btrfs_page_inc_eb_refs(fs_info, p); |
Josef Bacik | 4f2de97a | 2012-03-07 16:20:05 -0500 | [diff] [blame] | 6232 | spin_unlock(&mapping->private_lock); |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 6233 | |
Qu Wenruo | 1e5eb3d | 2021-03-25 15:14:41 +0800 | [diff] [blame] | 6234 | WARN_ON(btrfs_page_test_dirty(fs_info, p, eb->start, eb->len)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6235 | eb->pages[i] = p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6236 | if (!PageUptodate(p)) |
| 6237 | uptodate = 0; |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 6238 | |
| 6239 | /* |
Nikolay Borisov | b16d011 | 2018-07-04 10:24:52 +0300 | [diff] [blame] | 6240 | * We can't unlock the pages just yet since the extent buffer |
| 6241 | * hasn't been properly inserted in the radix tree, this |
Matthew Wilcox (Oracle) | f913cff | 2022-04-30 23:15:16 -0400 | [diff] [blame] | 6242 | * opens a race with btree_release_folio which can free a page |
Nikolay Borisov | b16d011 | 2018-07-04 10:24:52 +0300 | [diff] [blame] | 6243 | * while we are still filling in all pages for the buffer and |
| 6244 | * we could crash. |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 6245 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6246 | } |
| 6247 | if (uptodate) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 6248 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 6249 | again: |
| 6250 | ret = radix_tree_preload(GFP_NOFS); |
| 6251 | if (ret) { |
| 6252 | exists = ERR_PTR(ret); |
| 6253 | goto free_eb; |
| 6254 | } |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 6255 | |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 6256 | spin_lock(&fs_info->buffer_lock); |
| 6257 | ret = radix_tree_insert(&fs_info->buffer_radix, |
| 6258 | start >> fs_info->sectorsize_bits, eb); |
| 6259 | spin_unlock(&fs_info->buffer_lock); |
| 6260 | radix_tree_preload_end(); |
| 6261 | if (ret == -EEXIST) { |
| 6262 | exists = find_extent_buffer(fs_info, start); |
| 6263 | if (exists) |
Chandra Seetharaman | 452c75c | 2013-10-07 10:45:25 -0500 | [diff] [blame] | 6264 | goto free_eb; |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 6265 | else |
| 6266 | goto again; |
| 6267 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 6268 | /* add one reference for the tree */ |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 6269 | check_buffer_tree_ref(eb); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 6270 | set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 6271 | |
| 6272 | /* |
Nikolay Borisov | b16d011 | 2018-07-04 10:24:52 +0300 | [diff] [blame] | 6273 | * Now it's safe to unlock the pages because any calls to |
Matthew Wilcox (Oracle) | f913cff | 2022-04-30 23:15:16 -0400 | [diff] [blame] | 6274 | * btree_release_folio will correctly detect that a page belongs to a |
Nikolay Borisov | b16d011 | 2018-07-04 10:24:52 +0300 | [diff] [blame] | 6275 | * live buffer and won't free them prematurely. |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 6276 | */ |
Nikolay Borisov | 28187ae | 2018-07-04 10:24:51 +0300 | [diff] [blame] | 6277 | for (i = 0; i < num_pages; i++) |
| 6278 | unlock_page(eb->pages[i]); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6279 | return eb; |
| 6280 | |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 6281 | free_eb: |
Omar Sandoval | 5ca64f4 | 2015-02-24 02:47:05 -0800 | [diff] [blame] | 6282 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6283 | for (i = 0; i < num_pages; i++) { |
| 6284 | if (eb->pages[i]) |
| 6285 | unlock_page(eb->pages[i]); |
| 6286 | } |
Chris Mason | eb14ab8 | 2011-02-10 12:35:00 -0500 | [diff] [blame] | 6287 | |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 6288 | btrfs_release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 6289 | return exists; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6290 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6291 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6292 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
| 6293 | { |
| 6294 | struct extent_buffer *eb = |
| 6295 | container_of(head, struct extent_buffer, rcu_head); |
| 6296 | |
| 6297 | __free_extent_buffer(eb); |
| 6298 | } |
| 6299 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 6300 | static int release_extent_buffer(struct extent_buffer *eb) |
Jules Irenge | 5ce48d0 | 2020-02-23 23:16:42 +0000 | [diff] [blame] | 6301 | __releases(&eb->refs_lock) |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6302 | { |
Nikolay Borisov | 07e21c4 | 2018-06-27 16:38:23 +0300 | [diff] [blame] | 6303 | lockdep_assert_held(&eb->refs_lock); |
| 6304 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6305 | WARN_ON(atomic_read(&eb->refs) == 0); |
| 6306 | if (atomic_dec_and_test(&eb->refs)) { |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 6307 | if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) { |
Josef Bacik | f28491e | 2013-12-16 13:24:27 -0500 | [diff] [blame] | 6308 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6309 | |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 6310 | spin_unlock(&eb->refs_lock); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6311 | |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 6312 | spin_lock(&fs_info->buffer_lock); |
| 6313 | radix_tree_delete(&fs_info->buffer_radix, |
| 6314 | eb->start >> fs_info->sectorsize_bits); |
| 6315 | spin_unlock(&fs_info->buffer_lock); |
Josef Bacik | 34b41ac | 2013-12-13 10:41:51 -0500 | [diff] [blame] | 6316 | } else { |
| 6317 | spin_unlock(&eb->refs_lock); |
Jan Schmidt | 815a51c | 2012-05-16 17:00:02 +0200 | [diff] [blame] | 6318 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6319 | |
Josef Bacik | 8c38938 | 2020-02-14 16:11:42 -0500 | [diff] [blame] | 6320 | btrfs_leak_debug_del(&eb->fs_info->eb_leak_lock, &eb->leak_list); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6321 | /* Should be safe to release our pages at this point */ |
David Sterba | 55ac013 | 2018-07-19 17:24:32 +0200 | [diff] [blame] | 6322 | btrfs_release_extent_buffer_pages(eb); |
Josef Bacik | bcb7e44 | 2015-03-16 17:38:02 -0400 | [diff] [blame] | 6323 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Nikolay Borisov | b0132a3b | 2018-06-27 16:38:24 +0300 | [diff] [blame] | 6324 | if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags))) { |
Josef Bacik | bcb7e44 | 2015-03-16 17:38:02 -0400 | [diff] [blame] | 6325 | __free_extent_buffer(eb); |
| 6326 | return 1; |
| 6327 | } |
| 6328 | #endif |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6329 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 6330 | return 1; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6331 | } |
| 6332 | spin_unlock(&eb->refs_lock); |
Josef Bacik | e64860a | 2012-07-20 16:05:36 -0400 | [diff] [blame] | 6333 | |
| 6334 | return 0; |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6335 | } |
| 6336 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6337 | void free_extent_buffer(struct extent_buffer *eb) |
| 6338 | { |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 6339 | int refs; |
| 6340 | int old; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6341 | if (!eb) |
| 6342 | return; |
| 6343 | |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 6344 | while (1) { |
| 6345 | refs = atomic_read(&eb->refs); |
Nikolay Borisov | 46cc775 | 2018-10-15 17:04:01 +0300 | [diff] [blame] | 6346 | if ((!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && refs <= 3) |
| 6347 | || (test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags) && |
| 6348 | refs == 1)) |
Chris Mason | 242e18c | 2013-01-29 17:49:37 -0500 | [diff] [blame] | 6349 | break; |
| 6350 | old = atomic_cmpxchg(&eb->refs, refs, refs - 1); |
| 6351 | if (old == refs) |
| 6352 | return; |
| 6353 | } |
| 6354 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6355 | spin_lock(&eb->refs_lock); |
| 6356 | if (atomic_read(&eb->refs) == 2 && |
| 6357 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 6358 | !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6359 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 6360 | atomic_dec(&eb->refs); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6361 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6362 | /* |
| 6363 | * I know this is terrible, but it's temporary until we stop tracking |
| 6364 | * the uptodate bits and such for the extent buffers. |
| 6365 | */ |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 6366 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6367 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6368 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6369 | void free_extent_buffer_stale(struct extent_buffer *eb) |
| 6370 | { |
| 6371 | if (!eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6372 | return; |
| 6373 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6374 | spin_lock(&eb->refs_lock); |
| 6375 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); |
| 6376 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 6377 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6378 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
| 6379 | atomic_dec(&eb->refs); |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 6380 | release_extent_buffer(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6381 | } |
| 6382 | |
Qu Wenruo | 0d27797 | 2021-03-25 15:14:43 +0800 | [diff] [blame] | 6383 | static void btree_clear_page_dirty(struct page *page) |
| 6384 | { |
| 6385 | ASSERT(PageDirty(page)); |
| 6386 | ASSERT(PageLocked(page)); |
| 6387 | clear_page_dirty_for_io(page); |
| 6388 | xa_lock_irq(&page->mapping->i_pages); |
| 6389 | if (!PageDirty(page)) |
| 6390 | __xa_clear_mark(&page->mapping->i_pages, |
| 6391 | page_index(page), PAGECACHE_TAG_DIRTY); |
| 6392 | xa_unlock_irq(&page->mapping->i_pages); |
| 6393 | } |
| 6394 | |
| 6395 | static void clear_subpage_extent_buffer_dirty(const struct extent_buffer *eb) |
| 6396 | { |
| 6397 | struct btrfs_fs_info *fs_info = eb->fs_info; |
| 6398 | struct page *page = eb->pages[0]; |
| 6399 | bool last; |
| 6400 | |
| 6401 | /* btree_clear_page_dirty() needs page locked */ |
| 6402 | lock_page(page); |
| 6403 | last = btrfs_subpage_clear_and_test_dirty(fs_info, page, eb->start, |
| 6404 | eb->len); |
| 6405 | if (last) |
| 6406 | btree_clear_page_dirty(page); |
| 6407 | unlock_page(page); |
| 6408 | WARN_ON(atomic_read(&eb->refs) == 0); |
| 6409 | } |
| 6410 | |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 6411 | void clear_extent_buffer_dirty(const struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6412 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 6413 | int i; |
| 6414 | int num_pages; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6415 | struct page *page; |
| 6416 | |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 6417 | if (eb->fs_info->nodesize < PAGE_SIZE) |
Qu Wenruo | 0d27797 | 2021-03-25 15:14:43 +0800 | [diff] [blame] | 6418 | return clear_subpage_extent_buffer_dirty(eb); |
| 6419 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 6420 | num_pages = num_extent_pages(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6421 | |
| 6422 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 6423 | page = eb->pages[i]; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6424 | if (!PageDirty(page)) |
Chris Mason | d2c3f4f | 2008-11-19 12:44:22 -0500 | [diff] [blame] | 6425 | continue; |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 6426 | lock_page(page); |
Qu Wenruo | 0d27797 | 2021-03-25 15:14:43 +0800 | [diff] [blame] | 6427 | btree_clear_page_dirty(page); |
Chris Mason | bf0da8c | 2011-11-04 12:29:37 -0400 | [diff] [blame] | 6428 | ClearPageError(page); |
Chris Mason | a61e6f2 | 2008-07-22 11:18:08 -0400 | [diff] [blame] | 6429 | unlock_page(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6430 | } |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 6431 | WARN_ON(atomic_read(&eb->refs) == 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6432 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6433 | |
Liu Bo | abb57ef | 2018-09-14 01:44:42 +0800 | [diff] [blame] | 6434 | bool set_extent_buffer_dirty(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6435 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 6436 | int i; |
| 6437 | int num_pages; |
Liu Bo | abb57ef | 2018-09-14 01:44:42 +0800 | [diff] [blame] | 6438 | bool was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6439 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 6440 | check_buffer_tree_ref(eb); |
| 6441 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6442 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 6443 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 6444 | num_pages = num_extent_pages(eb); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6445 | WARN_ON(atomic_read(&eb->refs) == 0); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 6446 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
| 6447 | |
Qu Wenruo | 0d27797 | 2021-03-25 15:14:43 +0800 | [diff] [blame] | 6448 | if (!was_dirty) { |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 6449 | bool subpage = eb->fs_info->nodesize < PAGE_SIZE; |
Liu Bo | 51995c3 | 2018-09-14 01:46:08 +0800 | [diff] [blame] | 6450 | |
Qu Wenruo | 0d27797 | 2021-03-25 15:14:43 +0800 | [diff] [blame] | 6451 | /* |
| 6452 | * For subpage case, we can have other extent buffers in the |
| 6453 | * same page, and in clear_subpage_extent_buffer_dirty() we |
| 6454 | * have to clear page dirty without subpage lock held. |
| 6455 | * This can cause race where our page gets dirty cleared after |
| 6456 | * we just set it. |
| 6457 | * |
| 6458 | * Thankfully, clear_subpage_extent_buffer_dirty() has locked |
| 6459 | * its page for other reasons, we can use page lock to prevent |
| 6460 | * the above race. |
| 6461 | */ |
| 6462 | if (subpage) |
| 6463 | lock_page(eb->pages[0]); |
| 6464 | for (i = 0; i < num_pages; i++) |
| 6465 | btrfs_page_set_dirty(eb->fs_info, eb->pages[i], |
| 6466 | eb->start, eb->len); |
| 6467 | if (subpage) |
| 6468 | unlock_page(eb->pages[0]); |
| 6469 | } |
Liu Bo | 51995c3 | 2018-09-14 01:46:08 +0800 | [diff] [blame] | 6470 | #ifdef CONFIG_BTRFS_DEBUG |
| 6471 | for (i = 0; i < num_pages; i++) |
| 6472 | ASSERT(PageDirty(eb->pages[i])); |
| 6473 | #endif |
| 6474 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6475 | return was_dirty; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6476 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6477 | |
David Sterba | 69ba392 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 6478 | void clear_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6479 | { |
Qu Wenruo | 251f2ac | 2021-01-26 16:33:54 +0800 | [diff] [blame] | 6480 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6481 | struct page *page; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 6482 | int num_pages; |
Qu Wenruo | 251f2ac | 2021-01-26 16:33:54 +0800 | [diff] [blame] | 6483 | int i; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6484 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 6485 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 6486 | num_pages = num_extent_pages(eb); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6487 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 6488 | page = eb->pages[i]; |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 6489 | if (!page) |
| 6490 | continue; |
| 6491 | |
| 6492 | /* |
| 6493 | * This is special handling for metadata subpage, as regular |
| 6494 | * btrfs_is_subpage() can not handle cloned/dummy metadata. |
| 6495 | */ |
| 6496 | if (fs_info->nodesize >= PAGE_SIZE) |
| 6497 | ClearPageUptodate(page); |
| 6498 | else |
| 6499 | btrfs_subpage_clear_uptodate(fs_info, page, eb->start, |
| 6500 | eb->len); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6501 | } |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6502 | } |
| 6503 | |
David Sterba | 09c25a8 | 2015-12-03 13:08:59 +0100 | [diff] [blame] | 6504 | void set_extent_buffer_uptodate(struct extent_buffer *eb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6505 | { |
Qu Wenruo | 251f2ac | 2021-01-26 16:33:54 +0800 | [diff] [blame] | 6506 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6507 | struct page *page; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 6508 | int num_pages; |
Qu Wenruo | 251f2ac | 2021-01-26 16:33:54 +0800 | [diff] [blame] | 6509 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6510 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 6511 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 6512 | num_pages = num_extent_pages(eb); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6513 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 6514 | page = eb->pages[i]; |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 6515 | |
| 6516 | /* |
| 6517 | * This is special handling for metadata subpage, as regular |
| 6518 | * btrfs_is_subpage() can not handle cloned/dummy metadata. |
| 6519 | */ |
| 6520 | if (fs_info->nodesize >= PAGE_SIZE) |
| 6521 | SetPageUptodate(page); |
| 6522 | else |
| 6523 | btrfs_subpage_set_uptodate(fs_info, page, eb->start, |
| 6524 | eb->len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6525 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6526 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6527 | |
Qu Wenruo | 4012daf | 2021-01-26 16:33:57 +0800 | [diff] [blame] | 6528 | static int read_extent_buffer_subpage(struct extent_buffer *eb, int wait, |
| 6529 | int mirror_num) |
| 6530 | { |
| 6531 | struct btrfs_fs_info *fs_info = eb->fs_info; |
| 6532 | struct extent_io_tree *io_tree; |
| 6533 | struct page *page = eb->pages[0]; |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 6534 | struct btrfs_bio_ctrl bio_ctrl = { |
| 6535 | .mirror_num = mirror_num, |
| 6536 | }; |
Qu Wenruo | 4012daf | 2021-01-26 16:33:57 +0800 | [diff] [blame] | 6537 | int ret = 0; |
| 6538 | |
| 6539 | ASSERT(!test_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags)); |
| 6540 | ASSERT(PagePrivate(page)); |
| 6541 | io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree; |
| 6542 | |
| 6543 | if (wait == WAIT_NONE) { |
Goldwyn Rodrigues | dc56219 | 2021-04-08 07:40:25 -0500 | [diff] [blame] | 6544 | if (!try_lock_extent(io_tree, eb->start, eb->start + eb->len - 1)) |
| 6545 | return -EAGAIN; |
Qu Wenruo | 4012daf | 2021-01-26 16:33:57 +0800 | [diff] [blame] | 6546 | } else { |
| 6547 | ret = lock_extent(io_tree, eb->start, eb->start + eb->len - 1); |
| 6548 | if (ret < 0) |
| 6549 | return ret; |
| 6550 | } |
| 6551 | |
| 6552 | ret = 0; |
| 6553 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags) || |
| 6554 | PageUptodate(page) || |
| 6555 | btrfs_subpage_test_uptodate(fs_info, page, eb->start, eb->len)) { |
| 6556 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
| 6557 | unlock_extent(io_tree, eb->start, eb->start + eb->len - 1); |
| 6558 | return ret; |
| 6559 | } |
| 6560 | |
| 6561 | clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
| 6562 | eb->read_mirror = 0; |
| 6563 | atomic_set(&eb->io_pages, 1); |
| 6564 | check_buffer_tree_ref(eb); |
| 6565 | btrfs_subpage_clear_error(fs_info, page, eb->start, eb->len); |
| 6566 | |
Qu Wenruo | 3d078ef | 2021-06-07 17:02:58 +0800 | [diff] [blame] | 6567 | btrfs_subpage_start_reader(fs_info, page, eb->start, eb->len); |
Christoph Hellwig | 08a6f46 | 2022-05-26 09:36:39 +0200 | [diff] [blame] | 6568 | ret = submit_extent_page(REQ_OP_READ, NULL, &bio_ctrl, |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 6569 | page, eb->start, eb->len, |
| 6570 | eb->start - page_offset(page), |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 6571 | end_bio_extent_readpage, 0, true); |
Qu Wenruo | 4012daf | 2021-01-26 16:33:57 +0800 | [diff] [blame] | 6572 | if (ret) { |
| 6573 | /* |
| 6574 | * In the endio function, if we hit something wrong we will |
| 6575 | * increase the io_pages, so here we need to decrease it for |
| 6576 | * error path. |
| 6577 | */ |
| 6578 | atomic_dec(&eb->io_pages); |
| 6579 | } |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 6580 | submit_one_bio(&bio_ctrl); |
Qu Wenruo | 4012daf | 2021-01-26 16:33:57 +0800 | [diff] [blame] | 6581 | if (ret || wait != WAIT_COMPLETE) |
| 6582 | return ret; |
| 6583 | |
| 6584 | wait_extent_bit(io_tree, eb->start, eb->start + eb->len - 1, EXTENT_LOCKED); |
| 6585 | if (!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
| 6586 | ret = -EIO; |
| 6587 | return ret; |
| 6588 | } |
| 6589 | |
Nikolay Borisov | c2ccfbc | 2019-04-10 17:24:40 +0300 | [diff] [blame] | 6590 | 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] | 6591 | { |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 6592 | int i; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6593 | struct page *page; |
| 6594 | int err; |
| 6595 | int ret = 0; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6596 | int locked_pages = 0; |
| 6597 | int all_uptodate = 1; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 6598 | int num_pages; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6599 | unsigned long num_reads = 0; |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 6600 | struct btrfs_bio_ctrl bio_ctrl = { |
| 6601 | .mirror_num = mirror_num, |
| 6602 | }; |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 6603 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 6604 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6605 | return 0; |
| 6606 | |
Josef Bacik | 651740a5 | 2021-12-13 14:22:33 -0500 | [diff] [blame] | 6607 | /* |
| 6608 | * We could have had EXTENT_BUFFER_UPTODATE cleared by the write |
| 6609 | * operation, which could potentially still be in flight. In this case |
| 6610 | * we simply want to return an error. |
| 6611 | */ |
| 6612 | if (unlikely(test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))) |
| 6613 | return -EIO; |
| 6614 | |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 6615 | if (eb->fs_info->nodesize < PAGE_SIZE) |
Qu Wenruo | 4012daf | 2021-01-26 16:33:57 +0800 | [diff] [blame] | 6616 | return read_extent_buffer_subpage(eb, wait, mirror_num); |
| 6617 | |
David Sterba | 65ad010 | 2018-06-29 10:56:49 +0200 | [diff] [blame] | 6618 | num_pages = num_extent_pages(eb); |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 6619 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 6620 | page = eb->pages[i]; |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 6621 | if (wait == WAIT_NONE) { |
Qu Wenruo | 2c4d8cb | 2021-01-28 19:25:08 +0800 | [diff] [blame] | 6622 | /* |
| 6623 | * WAIT_NONE is only utilized by readahead. If we can't |
| 6624 | * acquire the lock atomically it means either the eb |
| 6625 | * is being read out or under modification. |
| 6626 | * Either way the eb will be or has been cached, |
| 6627 | * readahead can exit safely. |
| 6628 | */ |
David Woodhouse | 2db0496 | 2008-08-07 11:19:43 -0400 | [diff] [blame] | 6629 | if (!trylock_page(page)) |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6630 | goto unlock_exit; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6631 | } else { |
| 6632 | lock_page(page); |
| 6633 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6634 | locked_pages++; |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 6635 | } |
| 6636 | /* |
| 6637 | * We need to firstly lock all pages to make sure that |
| 6638 | * the uptodate bit of our pages won't be affected by |
| 6639 | * clear_extent_buffer_uptodate(). |
| 6640 | */ |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 6641 | for (i = 0; i < num_pages; i++) { |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 6642 | page = eb->pages[i]; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6643 | if (!PageUptodate(page)) { |
| 6644 | num_reads++; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6645 | all_uptodate = 0; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6646 | } |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6647 | } |
Liu Bo | 2571e73 | 2016-08-03 12:33:01 -0700 | [diff] [blame] | 6648 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6649 | if (all_uptodate) { |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 6650 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6651 | goto unlock_exit; |
| 6652 | } |
| 6653 | |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 6654 | clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags); |
Josef Bacik | 5cf1ab5 | 2012-04-16 09:42:26 -0400 | [diff] [blame] | 6655 | eb->read_mirror = 0; |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 6656 | atomic_set(&eb->io_pages, num_reads); |
Boris Burkov | 6bf9cd2 | 2020-06-17 11:35:19 -0700 | [diff] [blame] | 6657 | /* |
Matthew Wilcox (Oracle) | f913cff | 2022-04-30 23:15:16 -0400 | [diff] [blame] | 6658 | * It is possible for release_folio to clear the TREE_REF bit before we |
Boris Burkov | 6bf9cd2 | 2020-06-17 11:35:19 -0700 | [diff] [blame] | 6659 | * set io_pages. See check_buffer_tree_ref for a more detailed comment. |
| 6660 | */ |
| 6661 | check_buffer_tree_ref(eb); |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 6662 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 6663 | page = eb->pages[i]; |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 6664 | |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6665 | if (!PageUptodate(page)) { |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 6666 | if (ret) { |
| 6667 | atomic_dec(&eb->io_pages); |
| 6668 | unlock_page(page); |
| 6669 | continue; |
| 6670 | } |
| 6671 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6672 | ClearPageError(page); |
Christoph Hellwig | 08a6f46 | 2022-05-26 09:36:39 +0200 | [diff] [blame] | 6673 | err = submit_extent_page(REQ_OP_READ, NULL, |
Qu Wenruo | 390ed29b8 | 2021-04-14 16:42:15 +0800 | [diff] [blame] | 6674 | &bio_ctrl, page, page_offset(page), |
| 6675 | PAGE_SIZE, 0, end_bio_extent_readpage, |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 6676 | 0, false); |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 6677 | if (err) { |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 6678 | /* |
Nikolay Borisov | 0420177 | 2020-09-14 12:37:04 +0300 | [diff] [blame] | 6679 | * We failed to submit the bio so it's the |
| 6680 | * caller's responsibility to perform cleanup |
| 6681 | * i.e unlock page/set error bit. |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 6682 | */ |
Nikolay Borisov | 0420177 | 2020-09-14 12:37:04 +0300 | [diff] [blame] | 6683 | ret = err; |
| 6684 | SetPageError(page); |
| 6685 | unlock_page(page); |
Liu Bo | baf863b | 2016-07-11 10:39:07 -0700 | [diff] [blame] | 6686 | atomic_dec(&eb->io_pages); |
| 6687 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6688 | } else { |
| 6689 | unlock_page(page); |
| 6690 | } |
| 6691 | } |
| 6692 | |
Christoph Hellwig | 722c82a | 2022-06-03 09:11:03 +0200 | [diff] [blame] | 6693 | submit_one_bio(&bio_ctrl); |
Chris Mason | a86c12c | 2008-02-07 10:50:54 -0500 | [diff] [blame] | 6694 | |
Arne Jansen | bb82ab8 | 2011-06-10 14:06:53 +0200 | [diff] [blame] | 6695 | if (ret || wait != WAIT_COMPLETE) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6696 | return ret; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6697 | |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 6698 | for (i = 0; i < num_pages; i++) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 6699 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6700 | wait_on_page_locked(page); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6701 | if (!PageUptodate(page)) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6702 | ret = -EIO; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6703 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6704 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6705 | return ret; |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6706 | |
| 6707 | unlock_exit: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6708 | while (locked_pages > 0) { |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6709 | locked_pages--; |
Josef Bacik | 8436ea91 | 2016-09-02 15:40:03 -0400 | [diff] [blame] | 6710 | page = eb->pages[locked_pages]; |
| 6711 | unlock_page(page); |
Chris Mason | ce9adaa | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6712 | } |
| 6713 | return ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6714 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6715 | |
Qu Wenruo | f98b621 | 2020-08-19 14:35:47 +0800 | [diff] [blame] | 6716 | static bool report_eb_range(const struct extent_buffer *eb, unsigned long start, |
| 6717 | unsigned long len) |
| 6718 | { |
| 6719 | btrfs_warn(eb->fs_info, |
| 6720 | "access to eb bytenr %llu len %lu out of range start %lu len %lu", |
| 6721 | eb->start, eb->len, start, len); |
| 6722 | WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); |
| 6723 | |
| 6724 | return true; |
| 6725 | } |
| 6726 | |
| 6727 | /* |
| 6728 | * Check if the [start, start + len) range is valid before reading/writing |
| 6729 | * the eb. |
| 6730 | * NOTE: @start and @len are offset inside the eb, not logical address. |
| 6731 | * |
| 6732 | * Caller should not touch the dst/src memory if this function returns error. |
| 6733 | */ |
| 6734 | static inline int check_eb_range(const struct extent_buffer *eb, |
| 6735 | unsigned long start, unsigned long len) |
| 6736 | { |
| 6737 | unsigned long offset; |
| 6738 | |
| 6739 | /* start, start + len should not go beyond eb->len nor overflow */ |
| 6740 | if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len)) |
| 6741 | return report_eb_range(eb, start, len); |
| 6742 | |
| 6743 | return false; |
| 6744 | } |
| 6745 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 6746 | void read_extent_buffer(const struct extent_buffer *eb, void *dstv, |
| 6747 | unsigned long start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6748 | { |
| 6749 | size_t cur; |
| 6750 | size_t offset; |
| 6751 | struct page *page; |
| 6752 | char *kaddr; |
| 6753 | char *dst = (char *)dstv; |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 6754 | unsigned long i = get_eb_page_index(start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6755 | |
Qu Wenruo | f98b621 | 2020-08-19 14:35:47 +0800 | [diff] [blame] | 6756 | if (check_eb_range(eb, start, len)) |
Liu Bo | f716abd | 2017-08-09 11:10:16 -0600 | [diff] [blame] | 6757 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6758 | |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 6759 | offset = get_eb_offset_in_page(eb, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6760 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6761 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 6762 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6763 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6764 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 6765 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6766 | memcpy(dst, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6767 | |
| 6768 | dst += cur; |
| 6769 | len -= cur; |
| 6770 | offset = 0; |
| 6771 | i++; |
| 6772 | } |
| 6773 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6774 | |
Josef Bacik | a48b73e | 2020-08-10 11:42:27 -0400 | [diff] [blame] | 6775 | int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb, |
| 6776 | void __user *dstv, |
| 6777 | unsigned long start, unsigned long len) |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 6778 | { |
| 6779 | size_t cur; |
| 6780 | size_t offset; |
| 6781 | struct page *page; |
| 6782 | char *kaddr; |
| 6783 | char __user *dst = (char __user *)dstv; |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 6784 | unsigned long i = get_eb_page_index(start); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 6785 | int ret = 0; |
| 6786 | |
| 6787 | WARN_ON(start > eb->len); |
| 6788 | WARN_ON(start + len > eb->start + eb->len); |
| 6789 | |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 6790 | offset = get_eb_offset_in_page(eb, start); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 6791 | |
| 6792 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 6793 | page = eb->pages[i]; |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 6794 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6795 | cur = min(len, (PAGE_SIZE - offset)); |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 6796 | kaddr = page_address(page); |
Josef Bacik | a48b73e | 2020-08-10 11:42:27 -0400 | [diff] [blame] | 6797 | if (copy_to_user_nofault(dst, kaddr + offset, cur)) { |
Gerhard Heift | 550ac1d | 2014-01-30 16:24:01 +0100 | [diff] [blame] | 6798 | ret = -EFAULT; |
| 6799 | break; |
| 6800 | } |
| 6801 | |
| 6802 | dst += cur; |
| 6803 | len -= cur; |
| 6804 | offset = 0; |
| 6805 | i++; |
| 6806 | } |
| 6807 | |
| 6808 | return ret; |
| 6809 | } |
| 6810 | |
Jeff Mahoney | 1cbb1f4 | 2017-06-28 21:56:53 -0600 | [diff] [blame] | 6811 | int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, |
| 6812 | unsigned long start, unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6813 | { |
| 6814 | size_t cur; |
| 6815 | size_t offset; |
| 6816 | struct page *page; |
| 6817 | char *kaddr; |
| 6818 | char *ptr = (char *)ptrv; |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 6819 | unsigned long i = get_eb_page_index(start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6820 | int ret = 0; |
| 6821 | |
Qu Wenruo | f98b621 | 2020-08-19 14:35:47 +0800 | [diff] [blame] | 6822 | if (check_eb_range(eb, start, len)) |
| 6823 | return -EINVAL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6824 | |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 6825 | offset = get_eb_offset_in_page(eb, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6826 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6827 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 6828 | page = eb->pages[i]; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6829 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6830 | cur = min(len, (PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6831 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 6832 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6833 | ret = memcmp(ptr, kaddr + offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6834 | if (ret) |
| 6835 | break; |
| 6836 | |
| 6837 | ptr += cur; |
| 6838 | len -= cur; |
| 6839 | offset = 0; |
| 6840 | i++; |
| 6841 | } |
| 6842 | return ret; |
| 6843 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6844 | |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 6845 | /* |
| 6846 | * Check that the extent buffer is uptodate. |
| 6847 | * |
| 6848 | * For regular sector size == PAGE_SIZE case, check if @page is uptodate. |
| 6849 | * For subpage case, check if the range covered by the eb has EXTENT_UPTODATE. |
| 6850 | */ |
| 6851 | static void assert_eb_page_uptodate(const struct extent_buffer *eb, |
| 6852 | struct page *page) |
| 6853 | { |
| 6854 | struct btrfs_fs_info *fs_info = eb->fs_info; |
| 6855 | |
Josef Bacik | a50e1fc | 2022-02-18 10:17:39 -0500 | [diff] [blame] | 6856 | /* |
| 6857 | * If we are using the commit root we could potentially clear a page |
| 6858 | * Uptodate while we're using the extent buffer that we've previously |
| 6859 | * looked up. We don't want to complain in this case, as the page was |
| 6860 | * valid before, we just didn't write it out. Instead we want to catch |
| 6861 | * the case where we didn't actually read the block properly, which |
| 6862 | * would have !PageUptodate && !PageError, as we clear PageError before |
| 6863 | * reading. |
| 6864 | */ |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 6865 | if (fs_info->nodesize < PAGE_SIZE) { |
Josef Bacik | a50e1fc | 2022-02-18 10:17:39 -0500 | [diff] [blame] | 6866 | bool uptodate, error; |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 6867 | |
| 6868 | uptodate = btrfs_subpage_test_uptodate(fs_info, page, |
| 6869 | eb->start, eb->len); |
Josef Bacik | a50e1fc | 2022-02-18 10:17:39 -0500 | [diff] [blame] | 6870 | error = btrfs_subpage_test_error(fs_info, page, eb->start, eb->len); |
| 6871 | WARN_ON(!uptodate && !error); |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 6872 | } else { |
Josef Bacik | a50e1fc | 2022-02-18 10:17:39 -0500 | [diff] [blame] | 6873 | WARN_ON(!PageUptodate(page) && !PageError(page)); |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 6874 | } |
| 6875 | } |
| 6876 | |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 6877 | void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb, |
David Sterba | f157bf7 | 2016-11-09 17:43:38 +0100 | [diff] [blame] | 6878 | const void *srcv) |
| 6879 | { |
| 6880 | char *kaddr; |
| 6881 | |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 6882 | assert_eb_page_uptodate(eb, eb->pages[0]); |
David Sterba | 24880be5 | 2020-09-21 22:07:14 +0200 | [diff] [blame] | 6883 | kaddr = page_address(eb->pages[0]) + |
| 6884 | get_eb_offset_in_page(eb, offsetof(struct btrfs_header, |
| 6885 | chunk_tree_uuid)); |
| 6886 | memcpy(kaddr, srcv, BTRFS_FSID_SIZE); |
David Sterba | f157bf7 | 2016-11-09 17:43:38 +0100 | [diff] [blame] | 6887 | } |
| 6888 | |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 6889 | void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *srcv) |
David Sterba | f157bf7 | 2016-11-09 17:43:38 +0100 | [diff] [blame] | 6890 | { |
| 6891 | char *kaddr; |
| 6892 | |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 6893 | assert_eb_page_uptodate(eb, eb->pages[0]); |
David Sterba | 24880be5 | 2020-09-21 22:07:14 +0200 | [diff] [blame] | 6894 | kaddr = page_address(eb->pages[0]) + |
| 6895 | get_eb_offset_in_page(eb, offsetof(struct btrfs_header, fsid)); |
| 6896 | memcpy(kaddr, srcv, BTRFS_FSID_SIZE); |
David Sterba | f157bf7 | 2016-11-09 17:43:38 +0100 | [diff] [blame] | 6897 | } |
| 6898 | |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 6899 | void write_extent_buffer(const struct extent_buffer *eb, const void *srcv, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6900 | unsigned long start, unsigned long len) |
| 6901 | { |
| 6902 | size_t cur; |
| 6903 | size_t offset; |
| 6904 | struct page *page; |
| 6905 | char *kaddr; |
| 6906 | char *src = (char *)srcv; |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 6907 | unsigned long i = get_eb_page_index(start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6908 | |
Naohiro Aota | d3575156 | 2021-02-04 19:21:54 +0900 | [diff] [blame] | 6909 | WARN_ON(test_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags)); |
| 6910 | |
Qu Wenruo | f98b621 | 2020-08-19 14:35:47 +0800 | [diff] [blame] | 6911 | if (check_eb_range(eb, start, len)) |
| 6912 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6913 | |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 6914 | offset = get_eb_offset_in_page(eb, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6915 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6916 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 6917 | page = eb->pages[i]; |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 6918 | assert_eb_page_uptodate(eb, page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6919 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6920 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 6921 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6922 | memcpy(kaddr + offset, src, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6923 | |
| 6924 | src += cur; |
| 6925 | len -= cur; |
| 6926 | offset = 0; |
| 6927 | i++; |
| 6928 | } |
| 6929 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6930 | |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 6931 | void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start, |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6932 | unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6933 | { |
| 6934 | size_t cur; |
| 6935 | size_t offset; |
| 6936 | struct page *page; |
| 6937 | char *kaddr; |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 6938 | unsigned long i = get_eb_page_index(start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6939 | |
Qu Wenruo | f98b621 | 2020-08-19 14:35:47 +0800 | [diff] [blame] | 6940 | if (check_eb_range(eb, start, len)) |
| 6941 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6942 | |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 6943 | offset = get_eb_offset_in_page(eb, start); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6944 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6945 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 6946 | page = eb->pages[i]; |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 6947 | assert_eb_page_uptodate(eb, page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6948 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6949 | cur = min(len, PAGE_SIZE - offset); |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 6950 | kaddr = page_address(page); |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 6951 | memset(kaddr + offset, 0, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6952 | |
| 6953 | len -= cur; |
| 6954 | offset = 0; |
| 6955 | i++; |
| 6956 | } |
| 6957 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6958 | |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 6959 | void copy_extent_buffer_full(const struct extent_buffer *dst, |
| 6960 | const struct extent_buffer *src) |
David Sterba | 58e8012c | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 6961 | { |
| 6962 | int i; |
David Sterba | cc5e31a | 2018-03-01 18:20:27 +0100 | [diff] [blame] | 6963 | int num_pages; |
David Sterba | 58e8012c | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 6964 | |
| 6965 | ASSERT(dst->len == src->len); |
| 6966 | |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 6967 | if (dst->fs_info->nodesize >= PAGE_SIZE) { |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 6968 | num_pages = num_extent_pages(dst); |
| 6969 | for (i = 0; i < num_pages; i++) |
| 6970 | copy_page(page_address(dst->pages[i]), |
| 6971 | page_address(src->pages[i])); |
| 6972 | } else { |
| 6973 | size_t src_offset = get_eb_offset_in_page(src, 0); |
| 6974 | size_t dst_offset = get_eb_offset_in_page(dst, 0); |
| 6975 | |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 6976 | ASSERT(src->fs_info->nodesize < PAGE_SIZE); |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 6977 | memcpy(page_address(dst->pages[0]) + dst_offset, |
| 6978 | page_address(src->pages[0]) + src_offset, |
| 6979 | src->len); |
| 6980 | } |
David Sterba | 58e8012c | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 6981 | } |
| 6982 | |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 6983 | void copy_extent_buffer(const struct extent_buffer *dst, |
| 6984 | const struct extent_buffer *src, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6985 | unsigned long dst_offset, unsigned long src_offset, |
| 6986 | unsigned long len) |
| 6987 | { |
| 6988 | u64 dst_len = dst->len; |
| 6989 | size_t cur; |
| 6990 | size_t offset; |
| 6991 | struct page *page; |
| 6992 | char *kaddr; |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 6993 | unsigned long i = get_eb_page_index(dst_offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6994 | |
Qu Wenruo | f98b621 | 2020-08-19 14:35:47 +0800 | [diff] [blame] | 6995 | if (check_eb_range(dst, dst_offset, len) || |
| 6996 | check_eb_range(src, src_offset, len)) |
| 6997 | return; |
| 6998 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6999 | WARN_ON(src->len != dst_len); |
| 7000 | |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 7001 | offset = get_eb_offset_in_page(dst, dst_offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7002 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7003 | while (len > 0) { |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 7004 | page = dst->pages[i]; |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 7005 | assert_eb_page_uptodate(dst, page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7006 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7007 | cur = min(len, (unsigned long)(PAGE_SIZE - offset)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7008 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 7009 | kaddr = page_address(page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7010 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7011 | |
| 7012 | src_offset += cur; |
| 7013 | len -= cur; |
| 7014 | offset = 0; |
| 7015 | i++; |
| 7016 | } |
| 7017 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7018 | |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7019 | /* |
| 7020 | * eb_bitmap_offset() - calculate the page and offset of the byte containing the |
| 7021 | * given bit number |
| 7022 | * @eb: the extent buffer |
| 7023 | * @start: offset of the bitmap item in the extent buffer |
| 7024 | * @nr: bit number |
| 7025 | * @page_index: return index of the page in the extent buffer that contains the |
| 7026 | * given bit number |
| 7027 | * @page_offset: return offset into the page given by page_index |
| 7028 | * |
| 7029 | * This helper hides the ugliness of finding the byte in an extent buffer which |
| 7030 | * contains a given bit. |
| 7031 | */ |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 7032 | static inline void eb_bitmap_offset(const struct extent_buffer *eb, |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7033 | unsigned long start, unsigned long nr, |
| 7034 | unsigned long *page_index, |
| 7035 | size_t *page_offset) |
| 7036 | { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7037 | size_t byte_offset = BIT_BYTE(nr); |
| 7038 | size_t offset; |
| 7039 | |
| 7040 | /* |
| 7041 | * The byte we want is the offset of the extent buffer + the offset of |
| 7042 | * the bitmap item in the extent buffer + the offset of the byte in the |
| 7043 | * bitmap item. |
| 7044 | */ |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 7045 | offset = start + offset_in_page(eb->start) + byte_offset; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7046 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7047 | *page_index = offset >> PAGE_SHIFT; |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 7048 | *page_offset = offset_in_page(offset); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7049 | } |
| 7050 | |
| 7051 | /** |
| 7052 | * extent_buffer_test_bit - determine whether a bit in a bitmap item is set |
| 7053 | * @eb: the extent buffer |
| 7054 | * @start: offset of the bitmap item in the extent buffer |
| 7055 | * @nr: bit number to test |
| 7056 | */ |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 7057 | int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start, |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7058 | unsigned long nr) |
| 7059 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 7060 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7061 | struct page *page; |
| 7062 | unsigned long i; |
| 7063 | size_t offset; |
| 7064 | |
| 7065 | eb_bitmap_offset(eb, start, nr, &i, &offset); |
| 7066 | page = eb->pages[i]; |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 7067 | assert_eb_page_uptodate(eb, page); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7068 | kaddr = page_address(page); |
| 7069 | return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1))); |
| 7070 | } |
| 7071 | |
| 7072 | /** |
| 7073 | * extent_buffer_bitmap_set - set an area of a bitmap |
| 7074 | * @eb: the extent buffer |
| 7075 | * @start: offset of the bitmap item in the extent buffer |
| 7076 | * @pos: bit number of the first bit |
| 7077 | * @len: number of bits to set |
| 7078 | */ |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 7079 | void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start, |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7080 | unsigned long pos, unsigned long len) |
| 7081 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 7082 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7083 | struct page *page; |
| 7084 | unsigned long i; |
| 7085 | size_t offset; |
| 7086 | const unsigned int size = pos + len; |
| 7087 | int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 7088 | u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7089 | |
| 7090 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 7091 | page = eb->pages[i]; |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 7092 | assert_eb_page_uptodate(eb, page); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7093 | kaddr = page_address(page); |
| 7094 | |
| 7095 | while (len >= bits_to_set) { |
| 7096 | kaddr[offset] |= mask_to_set; |
| 7097 | len -= bits_to_set; |
| 7098 | bits_to_set = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 7099 | mask_to_set = ~0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7100 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7101 | offset = 0; |
| 7102 | page = eb->pages[++i]; |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 7103 | assert_eb_page_uptodate(eb, page); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7104 | kaddr = page_address(page); |
| 7105 | } |
| 7106 | } |
| 7107 | if (len) { |
| 7108 | mask_to_set &= BITMAP_LAST_BYTE_MASK(size); |
| 7109 | kaddr[offset] |= mask_to_set; |
| 7110 | } |
| 7111 | } |
| 7112 | |
| 7113 | |
| 7114 | /** |
| 7115 | * extent_buffer_bitmap_clear - clear an area of a bitmap |
| 7116 | * @eb: the extent buffer |
| 7117 | * @start: offset of the bitmap item in the extent buffer |
| 7118 | * @pos: bit number of the first bit |
| 7119 | * @len: number of bits to clear |
| 7120 | */ |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 7121 | void extent_buffer_bitmap_clear(const struct extent_buffer *eb, |
| 7122 | unsigned long start, unsigned long pos, |
| 7123 | unsigned long len) |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7124 | { |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 7125 | u8 *kaddr; |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7126 | struct page *page; |
| 7127 | unsigned long i; |
| 7128 | size_t offset; |
| 7129 | const unsigned int size = pos + len; |
| 7130 | int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE); |
Omar Sandoval | 2fe1d55 | 2016-09-22 17:24:20 -0700 | [diff] [blame] | 7131 | u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7132 | |
| 7133 | eb_bitmap_offset(eb, start, pos, &i, &offset); |
| 7134 | page = eb->pages[i]; |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 7135 | assert_eb_page_uptodate(eb, page); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7136 | kaddr = page_address(page); |
| 7137 | |
| 7138 | while (len >= bits_to_clear) { |
| 7139 | kaddr[offset] &= ~mask_to_clear; |
| 7140 | len -= bits_to_clear; |
| 7141 | bits_to_clear = BITS_PER_BYTE; |
Dan Carpenter | 9c89469 | 2016-10-12 11:33:21 +0300 | [diff] [blame] | 7142 | mask_to_clear = ~0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7143 | if (++offset >= PAGE_SIZE && len > 0) { |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7144 | offset = 0; |
| 7145 | page = eb->pages[++i]; |
Qu Wenruo | b8f9577 | 2021-03-25 15:14:42 +0800 | [diff] [blame] | 7146 | assert_eb_page_uptodate(eb, page); |
Omar Sandoval | 3e1e8bb | 2015-09-29 20:50:30 -0700 | [diff] [blame] | 7147 | kaddr = page_address(page); |
| 7148 | } |
| 7149 | } |
| 7150 | if (len) { |
| 7151 | mask_to_clear &= BITMAP_LAST_BYTE_MASK(size); |
| 7152 | kaddr[offset] &= ~mask_to_clear; |
| 7153 | } |
| 7154 | } |
| 7155 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 7156 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
| 7157 | { |
| 7158 | unsigned long distance = (src > dst) ? src - dst : dst - src; |
| 7159 | return distance < len; |
| 7160 | } |
| 7161 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7162 | static void copy_pages(struct page *dst_page, struct page *src_page, |
| 7163 | unsigned long dst_off, unsigned long src_off, |
| 7164 | unsigned long len) |
| 7165 | { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 7166 | char *dst_kaddr = page_address(dst_page); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7167 | char *src_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 7168 | int must_memmove = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7169 | |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 7170 | if (dst_page != src_page) { |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 7171 | src_kaddr = page_address(src_page); |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 7172 | } else { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7173 | src_kaddr = dst_kaddr; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 7174 | if (areas_overlap(src_off, dst_off, len)) |
| 7175 | must_memmove = 1; |
Sergei Trofimovich | 3387206 | 2011-04-11 21:52:52 +0000 | [diff] [blame] | 7176 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7177 | |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 7178 | if (must_memmove) |
| 7179 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); |
| 7180 | else |
| 7181 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7182 | } |
| 7183 | |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 7184 | void memcpy_extent_buffer(const struct extent_buffer *dst, |
| 7185 | unsigned long dst_offset, unsigned long src_offset, |
| 7186 | unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7187 | { |
| 7188 | size_t cur; |
| 7189 | size_t dst_off_in_page; |
| 7190 | size_t src_off_in_page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7191 | unsigned long dst_i; |
| 7192 | unsigned long src_i; |
| 7193 | |
Qu Wenruo | f98b621 | 2020-08-19 14:35:47 +0800 | [diff] [blame] | 7194 | if (check_eb_range(dst, dst_offset, len) || |
| 7195 | check_eb_range(dst, src_offset, len)) |
| 7196 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7197 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7198 | while (len > 0) { |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 7199 | dst_off_in_page = get_eb_offset_in_page(dst, dst_offset); |
| 7200 | src_off_in_page = get_eb_offset_in_page(dst, src_offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7201 | |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 7202 | dst_i = get_eb_page_index(dst_offset); |
| 7203 | src_i = get_eb_page_index(src_offset); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7204 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7205 | cur = min(len, (unsigned long)(PAGE_SIZE - |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7206 | src_off_in_page)); |
| 7207 | cur = min_t(unsigned long, cur, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 7208 | (unsigned long)(PAGE_SIZE - dst_off_in_page)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7209 | |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 7210 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7211 | dst_off_in_page, src_off_in_page, cur); |
| 7212 | |
| 7213 | src_offset += cur; |
| 7214 | dst_offset += cur; |
| 7215 | len -= cur; |
| 7216 | } |
| 7217 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7218 | |
David Sterba | 2b48966 | 2020-04-29 03:04:10 +0200 | [diff] [blame] | 7219 | void memmove_extent_buffer(const struct extent_buffer *dst, |
| 7220 | unsigned long dst_offset, unsigned long src_offset, |
| 7221 | unsigned long len) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7222 | { |
| 7223 | size_t cur; |
| 7224 | size_t dst_off_in_page; |
| 7225 | size_t src_off_in_page; |
| 7226 | unsigned long dst_end = dst_offset + len - 1; |
| 7227 | unsigned long src_end = src_offset + len - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7228 | unsigned long dst_i; |
| 7229 | unsigned long src_i; |
| 7230 | |
Qu Wenruo | f98b621 | 2020-08-19 14:35:47 +0800 | [diff] [blame] | 7231 | if (check_eb_range(dst, dst_offset, len) || |
| 7232 | check_eb_range(dst, src_offset, len)) |
| 7233 | return; |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 7234 | if (dst_offset < src_offset) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7235 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
| 7236 | return; |
| 7237 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7238 | while (len > 0) { |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 7239 | dst_i = get_eb_page_index(dst_end); |
| 7240 | src_i = get_eb_page_index(src_end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7241 | |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 7242 | dst_off_in_page = get_eb_offset_in_page(dst, dst_end); |
| 7243 | src_off_in_page = get_eb_offset_in_page(dst, src_end); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7244 | |
| 7245 | cur = min_t(unsigned long, len, src_off_in_page + 1); |
| 7246 | cur = min(cur, dst_off_in_page + 1); |
David Sterba | fb85fc9 | 2014-07-31 01:03:53 +0200 | [diff] [blame] | 7247 | copy_pages(dst->pages[dst_i], dst->pages[src_i], |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 7248 | dst_off_in_page - cur + 1, |
| 7249 | src_off_in_page - cur + 1, cur); |
| 7250 | |
| 7251 | dst_end -= cur; |
| 7252 | src_end -= cur; |
| 7253 | len -= cur; |
| 7254 | } |
| 7255 | } |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 7256 | |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 7257 | #define GANG_LOOKUP_SIZE 16 |
Qu Wenruo | d1e86e3 | 2021-01-26 16:33:56 +0800 | [diff] [blame] | 7258 | static struct extent_buffer *get_next_extent_buffer( |
| 7259 | struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr) |
| 7260 | { |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 7261 | struct extent_buffer *gang[GANG_LOOKUP_SIZE]; |
| 7262 | struct extent_buffer *found = NULL; |
Qu Wenruo | d1e86e3 | 2021-01-26 16:33:56 +0800 | [diff] [blame] | 7263 | u64 page_start = page_offset(page); |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 7264 | u64 cur = page_start; |
Qu Wenruo | d1e86e3 | 2021-01-26 16:33:56 +0800 | [diff] [blame] | 7265 | |
| 7266 | ASSERT(in_range(bytenr, page_start, PAGE_SIZE)); |
Qu Wenruo | d1e86e3 | 2021-01-26 16:33:56 +0800 | [diff] [blame] | 7267 | lockdep_assert_held(&fs_info->buffer_lock); |
| 7268 | |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 7269 | while (cur < page_start + PAGE_SIZE) { |
| 7270 | int ret; |
| 7271 | int i; |
| 7272 | |
| 7273 | ret = radix_tree_gang_lookup(&fs_info->buffer_radix, |
| 7274 | (void **)gang, cur >> fs_info->sectorsize_bits, |
| 7275 | min_t(unsigned int, GANG_LOOKUP_SIZE, |
| 7276 | PAGE_SIZE / fs_info->nodesize)); |
| 7277 | if (ret == 0) |
| 7278 | goto out; |
| 7279 | for (i = 0; i < ret; i++) { |
| 7280 | /* Already beyond page end */ |
| 7281 | if (gang[i]->start >= page_start + PAGE_SIZE) |
| 7282 | goto out; |
| 7283 | /* Found one */ |
| 7284 | if (gang[i]->start >= bytenr) { |
| 7285 | found = gang[i]; |
| 7286 | goto out; |
| 7287 | } |
| 7288 | } |
| 7289 | cur = gang[ret - 1]->start + gang[ret - 1]->len; |
Qu Wenruo | d1e86e3 | 2021-01-26 16:33:56 +0800 | [diff] [blame] | 7290 | } |
David Sterba | 01cd390 | 2022-07-15 13:59:31 +0200 | [diff] [blame] | 7291 | out: |
| 7292 | return found; |
Qu Wenruo | d1e86e3 | 2021-01-26 16:33:56 +0800 | [diff] [blame] | 7293 | } |
| 7294 | |
| 7295 | static int try_release_subpage_extent_buffer(struct page *page) |
| 7296 | { |
| 7297 | struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb); |
| 7298 | u64 cur = page_offset(page); |
| 7299 | const u64 end = page_offset(page) + PAGE_SIZE; |
| 7300 | int ret; |
| 7301 | |
| 7302 | while (cur < end) { |
| 7303 | struct extent_buffer *eb = NULL; |
| 7304 | |
| 7305 | /* |
| 7306 | * Unlike try_release_extent_buffer() which uses page->private |
| 7307 | * to grab buffer, for subpage case we rely on radix tree, thus |
| 7308 | * we need to ensure radix tree consistency. |
| 7309 | * |
| 7310 | * We also want an atomic snapshot of the radix tree, thus go |
| 7311 | * with spinlock rather than RCU. |
| 7312 | */ |
| 7313 | spin_lock(&fs_info->buffer_lock); |
| 7314 | eb = get_next_extent_buffer(fs_info, page, cur); |
| 7315 | if (!eb) { |
| 7316 | /* No more eb in the page range after or at cur */ |
| 7317 | spin_unlock(&fs_info->buffer_lock); |
| 7318 | break; |
| 7319 | } |
| 7320 | cur = eb->start + eb->len; |
| 7321 | |
| 7322 | /* |
| 7323 | * The same as try_release_extent_buffer(), to ensure the eb |
| 7324 | * won't disappear out from under us. |
| 7325 | */ |
| 7326 | spin_lock(&eb->refs_lock); |
| 7327 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
| 7328 | spin_unlock(&eb->refs_lock); |
| 7329 | spin_unlock(&fs_info->buffer_lock); |
| 7330 | break; |
| 7331 | } |
| 7332 | spin_unlock(&fs_info->buffer_lock); |
| 7333 | |
| 7334 | /* |
| 7335 | * If tree ref isn't set then we know the ref on this eb is a |
| 7336 | * real ref, so just return, this eb will likely be freed soon |
| 7337 | * anyway. |
| 7338 | */ |
| 7339 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
| 7340 | spin_unlock(&eb->refs_lock); |
| 7341 | break; |
| 7342 | } |
| 7343 | |
| 7344 | /* |
| 7345 | * Here we don't care about the return value, we will always |
| 7346 | * check the page private at the end. And |
| 7347 | * release_extent_buffer() will release the refs_lock. |
| 7348 | */ |
| 7349 | release_extent_buffer(eb); |
| 7350 | } |
| 7351 | /* |
| 7352 | * Finally to check if we have cleared page private, as if we have |
| 7353 | * released all ebs in the page, the page private should be cleared now. |
| 7354 | */ |
| 7355 | spin_lock(&page->mapping->private_lock); |
| 7356 | if (!PagePrivate(page)) |
| 7357 | ret = 1; |
| 7358 | else |
| 7359 | ret = 0; |
| 7360 | spin_unlock(&page->mapping->private_lock); |
| 7361 | return ret; |
| 7362 | |
| 7363 | } |
| 7364 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 7365 | int try_release_extent_buffer(struct page *page) |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 7366 | { |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 7367 | struct extent_buffer *eb; |
Miao Xie | 897ca6e9 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 7368 | |
Qu Wenruo | fbca46e | 2022-01-13 13:22:09 +0800 | [diff] [blame] | 7369 | if (btrfs_sb(page->mapping->host->i_sb)->nodesize < PAGE_SIZE) |
Qu Wenruo | d1e86e3 | 2021-01-26 16:33:56 +0800 | [diff] [blame] | 7370 | return try_release_subpage_extent_buffer(page); |
| 7371 | |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 7372 | /* |
Qu Wenruo | d1e86e3 | 2021-01-26 16:33:56 +0800 | [diff] [blame] | 7373 | * We need to make sure nobody is changing page->private, as we rely on |
| 7374 | * page->private as the pointer to extent buffer. |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 7375 | */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 7376 | spin_lock(&page->mapping->private_lock); |
| 7377 | if (!PagePrivate(page)) { |
| 7378 | spin_unlock(&page->mapping->private_lock); |
| 7379 | return 1; |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 7380 | } |
| 7381 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 7382 | eb = (struct extent_buffer *)page->private; |
| 7383 | BUG_ON(!eb); |
Miao Xie | 19fe0a8 | 2010-10-26 20:57:29 -0400 | [diff] [blame] | 7384 | |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 7385 | /* |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 7386 | * This is a little awful but should be ok, we need to make sure that |
| 7387 | * the eb doesn't disappear out from under us while we're looking at |
| 7388 | * this page. |
| 7389 | */ |
| 7390 | spin_lock(&eb->refs_lock); |
Josef Bacik | 0b32f4b | 2012-03-13 09:38:00 -0400 | [diff] [blame] | 7391 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 7392 | spin_unlock(&eb->refs_lock); |
| 7393 | spin_unlock(&page->mapping->private_lock); |
| 7394 | return 0; |
| 7395 | } |
| 7396 | spin_unlock(&page->mapping->private_lock); |
| 7397 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 7398 | /* |
| 7399 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
| 7400 | * so just return, this page will likely be freed soon anyway. |
| 7401 | */ |
| 7402 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
| 7403 | spin_unlock(&eb->refs_lock); |
| 7404 | return 0; |
| 7405 | } |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 7406 | |
David Sterba | f7a52a4 | 2013-04-26 14:56:29 +0000 | [diff] [blame] | 7407 | return release_extent_buffer(eb); |
Chris Mason | 6af118ce | 2008-07-22 11:18:07 -0400 | [diff] [blame] | 7408 | } |
Josef Bacik | bfb484d | 2020-11-05 10:45:09 -0500 | [diff] [blame] | 7409 | |
| 7410 | /* |
| 7411 | * btrfs_readahead_tree_block - attempt to readahead a child block |
| 7412 | * @fs_info: the fs_info |
| 7413 | * @bytenr: bytenr to read |
Josef Bacik | 3fbaf25 | 2020-11-05 10:45:20 -0500 | [diff] [blame] | 7414 | * @owner_root: objectid of the root that owns this eb |
Josef Bacik | bfb484d | 2020-11-05 10:45:09 -0500 | [diff] [blame] | 7415 | * @gen: generation for the uptodate check, can be 0 |
Josef Bacik | 3fbaf25 | 2020-11-05 10:45:20 -0500 | [diff] [blame] | 7416 | * @level: level for the eb |
Josef Bacik | bfb484d | 2020-11-05 10:45:09 -0500 | [diff] [blame] | 7417 | * |
| 7418 | * Attempt to readahead a tree block at @bytenr. If @gen is 0 then we do a |
| 7419 | * normal uptodate check of the eb, without checking the generation. If we have |
| 7420 | * to read the block we will not block on anything. |
| 7421 | */ |
| 7422 | void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info, |
Josef Bacik | 3fbaf25 | 2020-11-05 10:45:20 -0500 | [diff] [blame] | 7423 | u64 bytenr, u64 owner_root, u64 gen, int level) |
Josef Bacik | bfb484d | 2020-11-05 10:45:09 -0500 | [diff] [blame] | 7424 | { |
| 7425 | struct extent_buffer *eb; |
| 7426 | int ret; |
| 7427 | |
Josef Bacik | 3fbaf25 | 2020-11-05 10:45:20 -0500 | [diff] [blame] | 7428 | eb = btrfs_find_create_tree_block(fs_info, bytenr, owner_root, level); |
Josef Bacik | bfb484d | 2020-11-05 10:45:09 -0500 | [diff] [blame] | 7429 | if (IS_ERR(eb)) |
| 7430 | return; |
| 7431 | |
| 7432 | if (btrfs_buffer_uptodate(eb, gen, 1)) { |
| 7433 | free_extent_buffer(eb); |
| 7434 | return; |
| 7435 | } |
| 7436 | |
| 7437 | ret = read_extent_buffer_pages(eb, WAIT_NONE, 0); |
| 7438 | if (ret < 0) |
| 7439 | free_extent_buffer_stale(eb); |
| 7440 | else |
| 7441 | free_extent_buffer(eb); |
| 7442 | } |
| 7443 | |
| 7444 | /* |
| 7445 | * btrfs_readahead_node_child - readahead a node's child block |
| 7446 | * @node: parent node we're reading from |
| 7447 | * @slot: slot in the parent node for the child we want to read |
| 7448 | * |
| 7449 | * A helper for btrfs_readahead_tree_block, we simply read the bytenr pointed at |
| 7450 | * the slot in the node provided. |
| 7451 | */ |
| 7452 | void btrfs_readahead_node_child(struct extent_buffer *node, int slot) |
| 7453 | { |
| 7454 | btrfs_readahead_tree_block(node->fs_info, |
| 7455 | btrfs_node_blockptr(node, slot), |
Josef Bacik | 3fbaf25 | 2020-11-05 10:45:20 -0500 | [diff] [blame] | 7456 | btrfs_header_owner(node), |
| 7457 | btrfs_node_ptr_generation(node, slot), |
| 7458 | btrfs_header_level(node) - 1); |
Josef Bacik | bfb484d | 2020-11-05 10:45:09 -0500 | [diff] [blame] | 7459 | } |