David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 4 | */ |
David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 5 | |
Zach Brown | ec6b910 | 2007-07-11 10:00:37 -0400 | [diff] [blame] | 6 | #include <linux/sched.h> |
Filipe Manana | d7cd4dd | 2019-08-07 11:21:46 +0100 | [diff] [blame] | 7 | #include <linux/sched/mm.h> |
Ingo Molnar | f361bf4 | 2017-02-03 23:47:37 +0100 | [diff] [blame] | 8 | #include <linux/sched/signal.h> |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9 | #include <linux/pagemap.h> |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 10 | #include <linux/writeback.h> |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 11 | #include <linux/blkdev.h> |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 12 | #include <linux/sort.h> |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 13 | #include <linux/rcupdate.h> |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 14 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
David Sterba | dff51cd | 2011-06-14 12:52:17 +0200 | [diff] [blame] | 16 | #include <linux/ratelimit.h> |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 17 | #include <linux/percpu_counter.h> |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 18 | #include <linux/lockdep.h> |
Nikolay Borisov | 9678c54 | 2018-01-08 11:45:05 +0200 | [diff] [blame] | 19 | #include <linux/crc32c.h> |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 20 | #include "tree-log.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 21 | #include "disk-io.h" |
| 22 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 23 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 24 | #include "raid56.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 25 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 26 | #include "free-space-cache.h" |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 27 | #include "free-space-tree.h" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 28 | #include "math.h" |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 29 | #include "sysfs.h" |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 30 | #include "qgroup.h" |
Josef Bacik | fd708b8 | 2017-09-29 15:43:50 -0400 | [diff] [blame] | 31 | #include "ref-verify.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 32 | #include "space-info.h" |
Josef Bacik | d12ffdd | 2019-06-19 13:47:17 -0400 | [diff] [blame] | 33 | #include "block-rsv.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 34 | #include "delalloc-space.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 35 | #include "block-group.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 36 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 37 | #undef SCRAMBLE_DELAYED_REFS |
| 38 | |
Qu Wenruo | 9f9b8e8 | 2018-10-24 20:24:01 +0800 | [diff] [blame] | 39 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 40 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 41 | struct btrfs_delayed_ref_node *node, u64 parent, |
| 42 | u64 root_objectid, u64 owner_objectid, |
| 43 | u64 owner_offset, int refs_to_drop, |
| 44 | struct btrfs_delayed_extent_op *extra_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 45 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 46 | struct extent_buffer *leaf, |
| 47 | struct btrfs_extent_item *ei); |
| 48 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 49 | u64 parent, u64 root_objectid, |
| 50 | u64 flags, u64 owner, u64 offset, |
| 51 | struct btrfs_key *ins, int ref_mod); |
| 52 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 53 | struct btrfs_delayed_ref_node *node, |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 54 | struct btrfs_delayed_extent_op *extent_op); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 55 | static int find_next_key(struct btrfs_path *path, int level, |
| 56 | struct btrfs_key *key); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 57 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 58 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 59 | { |
| 60 | return (cache->flags & bits) == bits; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * this adds the block group to the fs_info rb tree for the block group |
| 65 | * cache |
| 66 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 67 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 68 | struct btrfs_block_group_cache *block_group) |
| 69 | { |
| 70 | struct rb_node **p; |
| 71 | struct rb_node *parent = NULL; |
| 72 | struct btrfs_block_group_cache *cache; |
| 73 | |
| 74 | spin_lock(&info->block_group_cache_lock); |
| 75 | p = &info->block_group_cache_tree.rb_node; |
| 76 | |
| 77 | while (*p) { |
| 78 | parent = *p; |
| 79 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 80 | cache_node); |
| 81 | if (block_group->key.objectid < cache->key.objectid) { |
| 82 | p = &(*p)->rb_left; |
| 83 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 84 | p = &(*p)->rb_right; |
| 85 | } else { |
| 86 | spin_unlock(&info->block_group_cache_lock); |
| 87 | return -EEXIST; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | rb_link_node(&block_group->cache_node, parent, p); |
| 92 | rb_insert_color(&block_group->cache_node, |
| 93 | &info->block_group_cache_tree); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 94 | |
| 95 | if (info->first_logical_byte > block_group->key.objectid) |
| 96 | info->first_logical_byte = block_group->key.objectid; |
| 97 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 98 | spin_unlock(&info->block_group_cache_lock); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 103 | int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info, |
| 104 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 105 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 106 | u64 end = start + num_bytes - 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 107 | set_extent_bits(&fs_info->freed_extents[0], |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 108 | start, end, EXTENT_UPTODATE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 109 | set_extent_bits(&fs_info->freed_extents[1], |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 110 | start, end, EXTENT_UPTODATE); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 111 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 114 | void btrfs_free_excluded_extents(struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 115 | { |
Nikolay Borisov | 9e715da | 2018-06-20 15:49:08 +0300 | [diff] [blame] | 116 | struct btrfs_fs_info *fs_info = cache->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 117 | u64 start, end; |
| 118 | |
| 119 | start = cache->key.objectid; |
| 120 | end = start + cache->key.offset - 1; |
| 121 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 122 | clear_extent_bits(&fs_info->freed_extents[0], |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 123 | start, end, EXTENT_UPTODATE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 124 | clear_extent_bits(&fs_info->freed_extents[1], |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 125 | start, end, EXTENT_UPTODATE); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Nikolay Borisov | 3c4da65 | 2018-06-20 15:49:09 +0300 | [diff] [blame] | 128 | static int exclude_super_stripes(struct btrfs_block_group_cache *cache) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 129 | { |
Nikolay Borisov | 3c4da65 | 2018-06-20 15:49:09 +0300 | [diff] [blame] | 130 | struct btrfs_fs_info *fs_info = cache->fs_info; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 131 | u64 bytenr; |
| 132 | u64 *logical; |
| 133 | int stripe_len; |
| 134 | int i, nr, ret; |
| 135 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 136 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 137 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 138 | cache->bytes_super += stripe_len; |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 139 | ret = btrfs_add_excluded_extent(fs_info, cache->key.objectid, |
| 140 | stripe_len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 141 | if (ret) |
| 142 | return ret; |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 145 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 146 | bytenr = btrfs_sb_offset(i); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 147 | ret = btrfs_rmap_block(fs_info, cache->key.objectid, |
Nikolay Borisov | 63a9c7b | 2018-05-04 10:53:05 +0300 | [diff] [blame] | 148 | bytenr, &logical, &nr, &stripe_len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 149 | if (ret) |
| 150 | return ret; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 151 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 152 | while (nr--) { |
Josef Bacik | 51bf5f0 | 2013-04-23 12:55:21 -0400 | [diff] [blame] | 153 | u64 start, len; |
| 154 | |
| 155 | if (logical[nr] > cache->key.objectid + |
| 156 | cache->key.offset) |
| 157 | continue; |
| 158 | |
| 159 | if (logical[nr] + stripe_len <= cache->key.objectid) |
| 160 | continue; |
| 161 | |
| 162 | start = logical[nr]; |
| 163 | if (start < cache->key.objectid) { |
| 164 | start = cache->key.objectid; |
| 165 | len = (logical[nr] + stripe_len) - start; |
| 166 | } else { |
| 167 | len = min_t(u64, stripe_len, |
| 168 | cache->key.objectid + |
| 169 | cache->key.offset - start); |
| 170 | } |
| 171 | |
| 172 | cache->bytes_super += len; |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 173 | ret = btrfs_add_excluded_extent(fs_info, start, len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 174 | if (ret) { |
| 175 | kfree(logical); |
| 176 | return ret; |
| 177 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 178 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 179 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 180 | kfree(logical); |
| 181 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 185 | struct btrfs_caching_control *btrfs_get_caching_control( |
| 186 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 187 | { |
| 188 | struct btrfs_caching_control *ctl; |
| 189 | |
| 190 | spin_lock(&cache->lock); |
Josef Bacik | dde5abe | 2010-09-16 16:17:03 -0400 | [diff] [blame] | 191 | if (!cache->caching_ctl) { |
| 192 | spin_unlock(&cache->lock); |
| 193 | return NULL; |
| 194 | } |
| 195 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 196 | ctl = cache->caching_ctl; |
Elena Reshetova | 1e4f471 | 2017-03-03 10:55:14 +0200 | [diff] [blame] | 197 | refcount_inc(&ctl->count); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 198 | spin_unlock(&cache->lock); |
| 199 | return ctl; |
| 200 | } |
| 201 | |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 202 | void btrfs_put_caching_control(struct btrfs_caching_control *ctl) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 203 | { |
Elena Reshetova | 1e4f471 | 2017-03-03 10:55:14 +0200 | [diff] [blame] | 204 | if (refcount_dec_and_test(&ctl->count)) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 205 | kfree(ctl); |
| 206 | } |
| 207 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 208 | #ifdef CONFIG_BTRFS_DEBUG |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 209 | static void fragment_free_space(struct btrfs_block_group_cache *block_group) |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 210 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 211 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 212 | u64 start = block_group->key.objectid; |
| 213 | u64 len = block_group->key.offset; |
| 214 | u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 215 | fs_info->nodesize : fs_info->sectorsize; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 216 | u64 step = chunk << 1; |
| 217 | |
| 218 | while (len > chunk) { |
| 219 | btrfs_remove_free_space(block_group, start, chunk); |
| 220 | start += step; |
| 221 | if (len < step) |
| 222 | len = 0; |
| 223 | else |
| 224 | len -= step; |
| 225 | } |
| 226 | } |
| 227 | #endif |
| 228 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 229 | /* |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 230 | * This is only called by btrfs_cache_block_group, since we could have freed |
| 231 | * extents we need to check the pinned_extents for any extents that can't be |
| 232 | * used yet since their free space will be released as soon as the transaction |
| 233 | * commits. |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 234 | */ |
Omar Sandoval | a5ed918 | 2015-09-29 20:50:35 -0700 | [diff] [blame] | 235 | u64 add_new_free_space(struct btrfs_block_group_cache *block_group, |
Nikolay Borisov | 4457c1c | 2018-05-10 15:44:45 +0300 | [diff] [blame] | 236 | u64 start, u64 end) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 237 | { |
Nikolay Borisov | 4457c1c | 2018-05-10 15:44:45 +0300 | [diff] [blame] | 238 | struct btrfs_fs_info *info = block_group->fs_info; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 239 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 240 | int ret; |
| 241 | |
| 242 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 243 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 244 | &extent_start, &extent_end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 245 | EXTENT_DIRTY | EXTENT_UPTODATE, |
| 246 | NULL); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 247 | if (ret) |
| 248 | break; |
| 249 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 250 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 251 | start = extent_end + 1; |
| 252 | } else if (extent_start > start && extent_start < end) { |
| 253 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 254 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 255 | ret = btrfs_add_free_space(block_group, start, |
| 256 | size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 257 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 258 | start = extent_end + 1; |
| 259 | } else { |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (start < end) { |
| 265 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 266 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 267 | ret = btrfs_add_free_space(block_group, start, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 268 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 269 | } |
| 270 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 271 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 272 | } |
| 273 | |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 274 | static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 275 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 276 | struct btrfs_block_group_cache *block_group = caching_ctl->block_group; |
| 277 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
| 278 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 279 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 280 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 281 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 282 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 283 | u64 last = 0; |
| 284 | u32 nritems; |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 285 | int ret; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 286 | bool wakeup = true; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 287 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 288 | path = btrfs_alloc_path(); |
| 289 | if (!path) |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 290 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 291 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 292 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 293 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 294 | #ifdef CONFIG_BTRFS_DEBUG |
| 295 | /* |
| 296 | * If we're fragmenting we don't want to make anybody think we can |
| 297 | * allocate from this block group until we've had a chance to fragment |
| 298 | * the free space. |
| 299 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 300 | if (btrfs_should_fragment_free_space(block_group)) |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 301 | wakeup = false; |
| 302 | #endif |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 303 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 304 | * We don't want to deadlock with somebody trying to allocate a new |
| 305 | * extent for the extent root while also trying to search the extent |
| 306 | * root to add free space. So we skip locking and search the commit |
| 307 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 308 | */ |
| 309 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 310 | path->search_commit_root = 1; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 311 | path->reada = READA_FORWARD; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 312 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 313 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 314 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 315 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 316 | |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 317 | next: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 318 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 319 | if (ret < 0) |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 320 | goto out; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 321 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 322 | leaf = path->nodes[0]; |
| 323 | nritems = btrfs_header_nritems(leaf); |
| 324 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 325 | while (1) { |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 326 | if (btrfs_fs_closing(fs_info) > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 327 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 328 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 329 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 330 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 331 | if (path->slots[0] < nritems) { |
| 332 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 333 | } else { |
| 334 | ret = find_next_key(path, 0, &key); |
| 335 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 336 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 337 | |
Josef Bacik | c9ea7b2 | 2013-09-19 10:02:11 -0400 | [diff] [blame] | 338 | if (need_resched() || |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 339 | rwsem_is_contended(&fs_info->commit_root_sem)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 340 | if (wakeup) |
| 341 | caching_ctl->progress = last; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 342 | btrfs_release_path(path); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 343 | up_read(&fs_info->commit_root_sem); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 344 | mutex_unlock(&caching_ctl->mutex); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 345 | cond_resched(); |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 346 | mutex_lock(&caching_ctl->mutex); |
| 347 | down_read(&fs_info->commit_root_sem); |
| 348 | goto next; |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 349 | } |
Josef Bacik | 0a3896d | 2013-04-19 14:37:26 -0400 | [diff] [blame] | 350 | |
| 351 | ret = btrfs_next_leaf(extent_root, path); |
| 352 | if (ret < 0) |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 353 | goto out; |
Josef Bacik | 0a3896d | 2013-04-19 14:37:26 -0400 | [diff] [blame] | 354 | if (ret) |
| 355 | break; |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 356 | leaf = path->nodes[0]; |
| 357 | nritems = btrfs_header_nritems(leaf); |
| 358 | continue; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 359 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 360 | |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 361 | if (key.objectid < last) { |
| 362 | key.objectid = last; |
| 363 | key.offset = 0; |
| 364 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 365 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 366 | if (wakeup) |
| 367 | caching_ctl->progress = last; |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 368 | btrfs_release_path(path); |
| 369 | goto next; |
| 370 | } |
| 371 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 372 | if (key.objectid < block_group->key.objectid) { |
| 373 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 374 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 375 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 376 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 377 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 378 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 379 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 380 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 381 | if (key.type == BTRFS_EXTENT_ITEM_KEY || |
| 382 | key.type == BTRFS_METADATA_ITEM_KEY) { |
Nikolay Borisov | 4457c1c | 2018-05-10 15:44:45 +0300 | [diff] [blame] | 383 | total_found += add_new_free_space(block_group, last, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 384 | key.objectid); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 385 | if (key.type == BTRFS_METADATA_ITEM_KEY) |
| 386 | last = key.objectid + |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 387 | fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 388 | else |
| 389 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 390 | |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 391 | if (total_found > CACHING_CTL_WAKE_UP) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 392 | total_found = 0; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 393 | if (wakeup) |
| 394 | wake_up(&caching_ctl->wait); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 395 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 396 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 397 | path->slots[0]++; |
| 398 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 399 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 400 | |
Nikolay Borisov | 4457c1c | 2018-05-10 15:44:45 +0300 | [diff] [blame] | 401 | total_found += add_new_free_space(block_group, last, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 402 | block_group->key.objectid + |
| 403 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 404 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 405 | |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 406 | out: |
| 407 | btrfs_free_path(path); |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | static noinline void caching_thread(struct btrfs_work *work) |
| 412 | { |
| 413 | struct btrfs_block_group_cache *block_group; |
| 414 | struct btrfs_fs_info *fs_info; |
| 415 | struct btrfs_caching_control *caching_ctl; |
| 416 | int ret; |
| 417 | |
| 418 | caching_ctl = container_of(work, struct btrfs_caching_control, work); |
| 419 | block_group = caching_ctl->block_group; |
| 420 | fs_info = block_group->fs_info; |
| 421 | |
| 422 | mutex_lock(&caching_ctl->mutex); |
| 423 | down_read(&fs_info->commit_root_sem); |
| 424 | |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 425 | if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) |
| 426 | ret = load_free_space_tree(caching_ctl); |
| 427 | else |
| 428 | ret = load_extent_tree_free(caching_ctl); |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 429 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 430 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 431 | block_group->caching_ctl = NULL; |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 432 | block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 433 | spin_unlock(&block_group->lock); |
| 434 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 435 | #ifdef CONFIG_BTRFS_DEBUG |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 436 | if (btrfs_should_fragment_free_space(block_group)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 437 | u64 bytes_used; |
| 438 | |
| 439 | spin_lock(&block_group->space_info->lock); |
| 440 | spin_lock(&block_group->lock); |
| 441 | bytes_used = block_group->key.offset - |
| 442 | btrfs_block_group_used(&block_group->item); |
| 443 | block_group->space_info->bytes_used += bytes_used >> 1; |
| 444 | spin_unlock(&block_group->lock); |
| 445 | spin_unlock(&block_group->space_info->lock); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 446 | fragment_free_space(block_group); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 447 | } |
| 448 | #endif |
| 449 | |
| 450 | caching_ctl->progress = (u64)-1; |
Chris Mason | f7d3d2f | 2015-12-18 11:11:10 -0800 | [diff] [blame] | 451 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 452 | up_read(&fs_info->commit_root_sem); |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 453 | btrfs_free_excluded_extents(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 454 | mutex_unlock(&caching_ctl->mutex); |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 455 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 456 | wake_up(&caching_ctl->wait); |
| 457 | |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 458 | btrfs_put_caching_control(caching_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 459 | btrfs_put_block_group(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 460 | } |
| 461 | |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 462 | int btrfs_cache_block_group(struct btrfs_block_group_cache *cache, |
| 463 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 464 | { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 465 | DEFINE_WAIT(wait); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 466 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 467 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 468 | int ret = 0; |
| 469 | |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 470 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 471 | if (!caching_ctl) |
| 472 | return -ENOMEM; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 473 | |
| 474 | INIT_LIST_HEAD(&caching_ctl->list); |
| 475 | mutex_init(&caching_ctl->mutex); |
| 476 | init_waitqueue_head(&caching_ctl->wait); |
| 477 | caching_ctl->block_group = cache; |
| 478 | caching_ctl->progress = cache->key.objectid; |
Elena Reshetova | 1e4f471 | 2017-03-03 10:55:14 +0200 | [diff] [blame] | 479 | refcount_set(&caching_ctl->count, 1); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 480 | btrfs_init_work(&caching_ctl->work, btrfs_cache_helper, |
| 481 | caching_thread, NULL, NULL); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 482 | |
| 483 | spin_lock(&cache->lock); |
| 484 | /* |
| 485 | * This should be a rare occasion, but this could happen I think in the |
| 486 | * case where one thread starts to load the space cache info, and then |
| 487 | * some other thread starts a transaction commit which tries to do an |
| 488 | * allocation while the other thread is still loading the space cache |
| 489 | * info. The previous loop should have kept us from choosing this block |
| 490 | * group, but if we've moved to the state where we will wait on caching |
| 491 | * block groups we need to first check if we're doing a fast load here, |
| 492 | * so we can wait for it to finish, otherwise we could end up allocating |
| 493 | * from a block group who's cache gets evicted for one reason or |
| 494 | * another. |
| 495 | */ |
| 496 | while (cache->cached == BTRFS_CACHE_FAST) { |
| 497 | struct btrfs_caching_control *ctl; |
| 498 | |
| 499 | ctl = cache->caching_ctl; |
Elena Reshetova | 1e4f471 | 2017-03-03 10:55:14 +0200 | [diff] [blame] | 500 | refcount_inc(&ctl->count); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 501 | prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 502 | spin_unlock(&cache->lock); |
| 503 | |
| 504 | schedule(); |
| 505 | |
| 506 | finish_wait(&ctl->wait, &wait); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 507 | btrfs_put_caching_control(ctl); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 508 | spin_lock(&cache->lock); |
| 509 | } |
| 510 | |
| 511 | if (cache->cached != BTRFS_CACHE_NO) { |
| 512 | spin_unlock(&cache->lock); |
| 513 | kfree(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 514 | return 0; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 515 | } |
| 516 | WARN_ON(cache->caching_ctl); |
| 517 | cache->caching_ctl = caching_ctl; |
| 518 | cache->cached = BTRFS_CACHE_FAST; |
| 519 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 520 | |
Satoru Takeuchi | d8953d6 | 2017-09-12 20:08:08 +0900 | [diff] [blame] | 521 | if (btrfs_test_opt(fs_info, SPACE_CACHE)) { |
Josef Bacik | cb83b7b | 2014-11-26 11:52:54 -0500 | [diff] [blame] | 522 | mutex_lock(&caching_ctl->mutex); |
David Sterba | bb6cb1c | 2019-03-20 13:47:15 +0100 | [diff] [blame] | 523 | ret = load_free_space_cache(cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 524 | |
| 525 | spin_lock(&cache->lock); |
| 526 | if (ret == 1) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 527 | cache->caching_ctl = NULL; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 528 | cache->cached = BTRFS_CACHE_FINISHED; |
| 529 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | cb83b7b | 2014-11-26 11:52:54 -0500 | [diff] [blame] | 530 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 531 | } else { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 532 | if (load_cache_only) { |
| 533 | cache->caching_ctl = NULL; |
| 534 | cache->cached = BTRFS_CACHE_NO; |
| 535 | } else { |
| 536 | cache->cached = BTRFS_CACHE_STARTED; |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 537 | cache->has_caching_ctl = 1; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 538 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 539 | } |
| 540 | spin_unlock(&cache->lock); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 541 | #ifdef CONFIG_BTRFS_DEBUG |
| 542 | if (ret == 1 && |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 543 | btrfs_should_fragment_free_space(cache)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 544 | u64 bytes_used; |
| 545 | |
| 546 | spin_lock(&cache->space_info->lock); |
| 547 | spin_lock(&cache->lock); |
| 548 | bytes_used = cache->key.offset - |
| 549 | btrfs_block_group_used(&cache->item); |
| 550 | cache->space_info->bytes_used += bytes_used >> 1; |
| 551 | spin_unlock(&cache->lock); |
| 552 | spin_unlock(&cache->space_info->lock); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 553 | fragment_free_space(cache); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 554 | } |
| 555 | #endif |
Josef Bacik | cb83b7b | 2014-11-26 11:52:54 -0500 | [diff] [blame] | 556 | mutex_unlock(&caching_ctl->mutex); |
| 557 | |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 558 | wake_up(&caching_ctl->wait); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 559 | if (ret == 1) { |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 560 | btrfs_put_caching_control(caching_ctl); |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 561 | btrfs_free_excluded_extents(cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 562 | return 0; |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 563 | } |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 564 | } else { |
| 565 | /* |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 566 | * We're either using the free space tree or no caching at all. |
| 567 | * Set cached to the appropriate value and wakeup any waiters. |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 568 | */ |
| 569 | spin_lock(&cache->lock); |
| 570 | if (load_cache_only) { |
| 571 | cache->caching_ctl = NULL; |
| 572 | cache->cached = BTRFS_CACHE_NO; |
| 573 | } else { |
| 574 | cache->cached = BTRFS_CACHE_STARTED; |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 575 | cache->has_caching_ctl = 1; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 576 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 577 | spin_unlock(&cache->lock); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 578 | wake_up(&caching_ctl->wait); |
| 579 | } |
| 580 | |
| 581 | if (load_cache_only) { |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 582 | btrfs_put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 583 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 584 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 585 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 586 | down_write(&fs_info->commit_root_sem); |
Elena Reshetova | 1e4f471 | 2017-03-03 10:55:14 +0200 | [diff] [blame] | 587 | refcount_inc(&caching_ctl->count); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 588 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 589 | up_write(&fs_info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 590 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 591 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 592 | |
Qu Wenruo | e66f0bb | 2014-02-28 10:46:12 +0800 | [diff] [blame] | 593 | btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 594 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 595 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 596 | } |
| 597 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 598 | |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 599 | static u64 generic_ref_to_space_flags(struct btrfs_ref *ref) |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 600 | { |
Qu Wenruo | ddf30cf | 2019-04-04 14:45:34 +0800 | [diff] [blame] | 601 | if (ref->type == BTRFS_REF_METADATA) { |
| 602 | if (ref->tree_ref.root == BTRFS_CHUNK_TREE_OBJECTID) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 603 | return BTRFS_BLOCK_GROUP_SYSTEM; |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 604 | else |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 605 | return BTRFS_BLOCK_GROUP_METADATA; |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 606 | } |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 607 | return BTRFS_BLOCK_GROUP_DATA; |
| 608 | } |
| 609 | |
| 610 | static void add_pinned_bytes(struct btrfs_fs_info *fs_info, |
| 611 | struct btrfs_ref *ref) |
| 612 | { |
| 613 | struct btrfs_space_info *space_info; |
| 614 | u64 flags = generic_ref_to_space_flags(ref); |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 615 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 616 | space_info = btrfs_find_space_info(fs_info, flags); |
Omar Sandoval | 55e8196 | 2017-06-06 16:45:27 -0700 | [diff] [blame] | 617 | ASSERT(space_info); |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 618 | percpu_counter_add_batch(&space_info->total_bytes_pinned, ref->len, |
| 619 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
| 620 | } |
| 621 | |
| 622 | static void sub_pinned_bytes(struct btrfs_fs_info *fs_info, |
| 623 | struct btrfs_ref *ref) |
| 624 | { |
| 625 | struct btrfs_space_info *space_info; |
| 626 | u64 flags = generic_ref_to_space_flags(ref); |
| 627 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 628 | space_info = btrfs_find_space_info(fs_info, flags); |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 629 | ASSERT(space_info); |
| 630 | percpu_counter_add_batch(&space_info->total_bytes_pinned, -ref->len, |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 631 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Filipe Manana | 1a4ed8f | 2014-10-27 10:44:24 +0000 | [diff] [blame] | 634 | /* simple helper to search for an existing data extent at a given offset */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 635 | int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 636 | { |
| 637 | int ret; |
| 638 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 639 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 640 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 641 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 642 | if (!path) |
| 643 | return -ENOMEM; |
| 644 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 645 | key.objectid = start; |
| 646 | key.offset = len; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 647 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 648 | ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 649 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 650 | return ret; |
| 651 | } |
| 652 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 653 | /* |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 654 | * helper function to lookup reference count and flags of a tree block. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 655 | * |
| 656 | * the head node for delayed ref is used to store the sum of all the |
| 657 | * reference count modifications queued up in the rbtree. the head |
| 658 | * node may also store the extent flags to set. This way you can check |
| 659 | * to see what the reference count and extent flags would be if all of |
| 660 | * the delayed refs are not processed. |
| 661 | */ |
| 662 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 663 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 664 | u64 offset, int metadata, u64 *refs, u64 *flags) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 665 | { |
| 666 | struct btrfs_delayed_ref_head *head; |
| 667 | struct btrfs_delayed_ref_root *delayed_refs; |
| 668 | struct btrfs_path *path; |
| 669 | struct btrfs_extent_item *ei; |
| 670 | struct extent_buffer *leaf; |
| 671 | struct btrfs_key key; |
| 672 | u32 item_size; |
| 673 | u64 num_refs; |
| 674 | u64 extent_flags; |
| 675 | int ret; |
| 676 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 677 | /* |
| 678 | * If we don't have skinny metadata, don't bother doing anything |
| 679 | * different |
| 680 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 681 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) { |
| 682 | offset = fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 683 | metadata = 0; |
| 684 | } |
| 685 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 686 | path = btrfs_alloc_path(); |
| 687 | if (!path) |
| 688 | return -ENOMEM; |
| 689 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 690 | if (!trans) { |
| 691 | path->skip_locking = 1; |
| 692 | path->search_commit_root = 1; |
| 693 | } |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 694 | |
| 695 | search_again: |
| 696 | key.objectid = bytenr; |
| 697 | key.offset = offset; |
| 698 | if (metadata) |
| 699 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 700 | else |
| 701 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 702 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 703 | ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 704 | if (ret < 0) |
| 705 | goto out_free; |
| 706 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 707 | if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) { |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 708 | if (path->slots[0]) { |
| 709 | path->slots[0]--; |
| 710 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 711 | path->slots[0]); |
| 712 | if (key.objectid == bytenr && |
| 713 | key.type == BTRFS_EXTENT_ITEM_KEY && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 714 | key.offset == fs_info->nodesize) |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 715 | ret = 0; |
| 716 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 717 | } |
| 718 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 719 | if (ret == 0) { |
| 720 | leaf = path->nodes[0]; |
| 721 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 722 | if (item_size >= sizeof(*ei)) { |
| 723 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 724 | struct btrfs_extent_item); |
| 725 | num_refs = btrfs_extent_refs(leaf, ei); |
| 726 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 727 | } else { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 728 | ret = -EINVAL; |
| 729 | btrfs_print_v0_err(fs_info); |
| 730 | if (trans) |
| 731 | btrfs_abort_transaction(trans, ret); |
| 732 | else |
| 733 | btrfs_handle_fs_error(fs_info, ret, NULL); |
| 734 | |
| 735 | goto out_free; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 736 | } |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 737 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 738 | BUG_ON(num_refs == 0); |
| 739 | } else { |
| 740 | num_refs = 0; |
| 741 | extent_flags = 0; |
| 742 | ret = 0; |
| 743 | } |
| 744 | |
| 745 | if (!trans) |
| 746 | goto out; |
| 747 | |
| 748 | delayed_refs = &trans->transaction->delayed_refs; |
| 749 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 750 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 751 | if (head) { |
| 752 | if (!mutex_trylock(&head->mutex)) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 753 | refcount_inc(&head->refs); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 754 | spin_unlock(&delayed_refs->lock); |
| 755 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 756 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 757 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 758 | /* |
| 759 | * Mutex was contended, block until it's released and try |
| 760 | * again |
| 761 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 762 | mutex_lock(&head->mutex); |
| 763 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 764 | btrfs_put_delayed_ref_head(head); |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 765 | goto search_again; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 766 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 767 | spin_lock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 768 | if (head->extent_op && head->extent_op->update_flags) |
| 769 | extent_flags |= head->extent_op->flags_to_set; |
| 770 | else |
| 771 | BUG_ON(num_refs == 0); |
| 772 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 773 | num_refs += head->ref_mod; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 774 | spin_unlock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 775 | mutex_unlock(&head->mutex); |
| 776 | } |
| 777 | spin_unlock(&delayed_refs->lock); |
| 778 | out: |
| 779 | WARN_ON(num_refs == 0); |
| 780 | if (refs) |
| 781 | *refs = num_refs; |
| 782 | if (flags) |
| 783 | *flags = extent_flags; |
| 784 | out_free: |
| 785 | btrfs_free_path(path); |
| 786 | return ret; |
| 787 | } |
| 788 | |
| 789 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 790 | * Back reference rules. Back refs have three main goals: |
| 791 | * |
| 792 | * 1) differentiate between all holders of references to an extent so that |
| 793 | * when a reference is dropped we can make sure it was a valid reference |
| 794 | * before freeing the extent. |
| 795 | * |
| 796 | * 2) Provide enough information to quickly find the holders of an extent |
| 797 | * if we notice a given block is corrupted or bad. |
| 798 | * |
| 799 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 800 | * maintenance. This is actually the same as #2, but with a slightly |
| 801 | * different use case. |
| 802 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 803 | * There are two kinds of back refs. The implicit back refs is optimized |
| 804 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 805 | * back refs of this kind provide information about the block's owner tree |
| 806 | * and the pointer's key. These information allow us to find the block by |
| 807 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 808 | * referenced by their owner trees. The location of tree block is recorded |
| 809 | * in the back refs. Actually the full back refs is generic, and can be |
| 810 | * used in all cases the implicit back refs is used. The major shortcoming |
| 811 | * of the full back refs is its overhead. Every time a tree block gets |
| 812 | * COWed, we have to update back refs entry for all pointers in it. |
| 813 | * |
| 814 | * For a newly allocated tree block, we use implicit back refs for |
| 815 | * pointers in it. This means most tree related operations only involve |
| 816 | * implicit back refs. For a tree block created in old transaction, the |
| 817 | * only way to drop a reference to it is COW it. So we can detect the |
| 818 | * event that tree block loses its owner tree's reference and do the |
| 819 | * back refs conversion. |
| 820 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 821 | * When a tree block is COWed through a tree, there are four cases: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 822 | * |
| 823 | * The reference count of the block is one and the tree is the block's |
| 824 | * owner tree. Nothing to do in this case. |
| 825 | * |
| 826 | * The reference count of the block is one and the tree is not the |
| 827 | * block's owner tree. In this case, full back refs is used for pointers |
| 828 | * in the block. Remove these full back refs, add implicit back refs for |
| 829 | * every pointers in the new block. |
| 830 | * |
| 831 | * The reference count of the block is greater than one and the tree is |
| 832 | * the block's owner tree. In this case, implicit back refs is used for |
| 833 | * pointers in the block. Add full back refs for every pointers in the |
| 834 | * block, increase lower level extents' reference counts. The original |
| 835 | * implicit back refs are entailed to the new block. |
| 836 | * |
| 837 | * The reference count of the block is greater than one and the tree is |
| 838 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 839 | * the new block, increase lower level extents' reference count. |
| 840 | * |
| 841 | * Back Reference Key composing: |
| 842 | * |
| 843 | * The key objectid corresponds to the first byte in the extent, |
| 844 | * The key type is used to differentiate between types of back refs. |
| 845 | * There are different meanings of the key offset for different types |
| 846 | * of back refs. |
| 847 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 848 | * File extents can be referenced by: |
| 849 | * |
| 850 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 851 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 852 | * - different offsets inside a file (bookend extents in file.c) |
| 853 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 854 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 855 | * |
| 856 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 857 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 858 | * - original offset in the file |
| 859 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 860 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 861 | * The key offset for the implicit back refs is hash of the first |
| 862 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 863 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 864 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 865 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 866 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 867 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 868 | * The key offset for the implicit back refs is the first byte of |
| 869 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 870 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 871 | * When a file extent is allocated, The implicit back refs is used. |
| 872 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 873 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 874 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 875 | * |
| 876 | * When a file extent is removed file truncation, we find the |
| 877 | * corresponding implicit back refs and check the following fields: |
| 878 | * |
| 879 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 880 | * |
| 881 | * Btree extents can be referenced by: |
| 882 | * |
| 883 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 884 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 885 | * Both the implicit back refs and the full back refs for tree blocks |
| 886 | * only consist of key. The key offset for the implicit back refs is |
| 887 | * objectid of block's owner tree. The key offset for the full back refs |
| 888 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 889 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 890 | * When implicit back refs is used, information about the lowest key and |
| 891 | * level of the tree block are required. These information are stored in |
| 892 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 893 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 894 | |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 895 | /* |
| 896 | * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 897 | * is_data == BTRFS_REF_TYPE_DATA, data type is requiried, |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 898 | * is_data == BTRFS_REF_TYPE_ANY, either type is OK. |
| 899 | */ |
| 900 | int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, |
| 901 | struct btrfs_extent_inline_ref *iref, |
| 902 | enum btrfs_inline_ref_type is_data) |
| 903 | { |
| 904 | int type = btrfs_extent_inline_ref_type(eb, iref); |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 905 | u64 offset = btrfs_extent_inline_ref_offset(eb, iref); |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 906 | |
| 907 | if (type == BTRFS_TREE_BLOCK_REF_KEY || |
| 908 | type == BTRFS_SHARED_BLOCK_REF_KEY || |
| 909 | type == BTRFS_SHARED_DATA_REF_KEY || |
| 910 | type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 911 | if (is_data == BTRFS_REF_TYPE_BLOCK) { |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 912 | if (type == BTRFS_TREE_BLOCK_REF_KEY) |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 913 | return type; |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 914 | if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 915 | ASSERT(eb->fs_info); |
| 916 | /* |
| 917 | * Every shared one has parent tree |
| 918 | * block, which must be aligned to |
| 919 | * nodesize. |
| 920 | */ |
| 921 | if (offset && |
| 922 | IS_ALIGNED(offset, eb->fs_info->nodesize)) |
| 923 | return type; |
| 924 | } |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 925 | } else if (is_data == BTRFS_REF_TYPE_DATA) { |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 926 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 927 | return type; |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 928 | if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 929 | ASSERT(eb->fs_info); |
| 930 | /* |
| 931 | * Every shared one has parent tree |
| 932 | * block, which must be aligned to |
| 933 | * nodesize. |
| 934 | */ |
| 935 | if (offset && |
| 936 | IS_ALIGNED(offset, eb->fs_info->nodesize)) |
| 937 | return type; |
| 938 | } |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 939 | } else { |
| 940 | ASSERT(is_data == BTRFS_REF_TYPE_ANY); |
| 941 | return type; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | btrfs_print_leaf((struct extent_buffer *)eb); |
| 946 | btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d", |
| 947 | eb->start, type); |
| 948 | WARN_ON(1); |
| 949 | |
| 950 | return BTRFS_REF_TYPE_INVALID; |
| 951 | } |
| 952 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 953 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 954 | { |
| 955 | u32 high_crc = ~(u32)0; |
| 956 | u32 low_crc = ~(u32)0; |
| 957 | __le64 lenum; |
| 958 | |
| 959 | lenum = cpu_to_le64(root_objectid); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 960 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 961 | lenum = cpu_to_le64(owner); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 962 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 963 | lenum = cpu_to_le64(offset); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 964 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 965 | |
| 966 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 967 | } |
| 968 | |
| 969 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 970 | struct btrfs_extent_data_ref *ref) |
| 971 | { |
| 972 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 973 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 974 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 975 | } |
| 976 | |
| 977 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 978 | struct btrfs_extent_data_ref *ref, |
| 979 | u64 root_objectid, u64 owner, u64 offset) |
| 980 | { |
| 981 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 982 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 983 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 984 | return 0; |
| 985 | return 1; |
| 986 | } |
| 987 | |
| 988 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 989 | struct btrfs_path *path, |
| 990 | u64 bytenr, u64 parent, |
| 991 | u64 root_objectid, |
| 992 | u64 owner, u64 offset) |
| 993 | { |
Nikolay Borisov | bd1d53e | 2018-06-20 15:48:51 +0300 | [diff] [blame] | 994 | struct btrfs_root *root = trans->fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 995 | struct btrfs_key key; |
| 996 | struct btrfs_extent_data_ref *ref; |
| 997 | struct extent_buffer *leaf; |
| 998 | u32 nritems; |
| 999 | int ret; |
| 1000 | int recow; |
| 1001 | int err = -ENOENT; |
| 1002 | |
| 1003 | key.objectid = bytenr; |
| 1004 | if (parent) { |
| 1005 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1006 | key.offset = parent; |
| 1007 | } else { |
| 1008 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1009 | key.offset = hash_extent_data_ref(root_objectid, |
| 1010 | owner, offset); |
| 1011 | } |
| 1012 | again: |
| 1013 | recow = 0; |
| 1014 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1015 | if (ret < 0) { |
| 1016 | err = ret; |
| 1017 | goto fail; |
| 1018 | } |
| 1019 | |
| 1020 | if (parent) { |
| 1021 | if (!ret) |
| 1022 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1023 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1027 | nritems = btrfs_header_nritems(leaf); |
| 1028 | while (1) { |
| 1029 | if (path->slots[0] >= nritems) { |
| 1030 | ret = btrfs_next_leaf(root, path); |
| 1031 | if (ret < 0) |
| 1032 | err = ret; |
| 1033 | if (ret) |
| 1034 | goto fail; |
| 1035 | |
| 1036 | leaf = path->nodes[0]; |
| 1037 | nritems = btrfs_header_nritems(leaf); |
| 1038 | recow = 1; |
| 1039 | } |
| 1040 | |
| 1041 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1042 | if (key.objectid != bytenr || |
| 1043 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1044 | goto fail; |
| 1045 | |
| 1046 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1047 | struct btrfs_extent_data_ref); |
| 1048 | |
| 1049 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1050 | owner, offset)) { |
| 1051 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1052 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1053 | goto again; |
| 1054 | } |
| 1055 | err = 0; |
| 1056 | break; |
| 1057 | } |
| 1058 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1059 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1060 | fail: |
| 1061 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1062 | } |
| 1063 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1064 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1065 | struct btrfs_path *path, |
| 1066 | u64 bytenr, u64 parent, |
| 1067 | u64 root_objectid, u64 owner, |
| 1068 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1069 | { |
Nikolay Borisov | 62b895a | 2018-06-20 15:48:44 +0300 | [diff] [blame] | 1070 | struct btrfs_root *root = trans->fs_info->extent_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1071 | struct btrfs_key key; |
| 1072 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1073 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1074 | u32 num_refs; |
| 1075 | int ret; |
| 1076 | |
| 1077 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1078 | if (parent) { |
| 1079 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1080 | key.offset = parent; |
| 1081 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1082 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1083 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1084 | key.offset = hash_extent_data_ref(root_objectid, |
| 1085 | owner, offset); |
| 1086 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1087 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1088 | |
| 1089 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1090 | if (ret && ret != -EEXIST) |
| 1091 | goto fail; |
| 1092 | |
| 1093 | leaf = path->nodes[0]; |
| 1094 | if (parent) { |
| 1095 | struct btrfs_shared_data_ref *ref; |
| 1096 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1097 | struct btrfs_shared_data_ref); |
| 1098 | if (ret == 0) { |
| 1099 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1100 | } else { |
| 1101 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1102 | num_refs += refs_to_add; |
| 1103 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1104 | } |
| 1105 | } else { |
| 1106 | struct btrfs_extent_data_ref *ref; |
| 1107 | while (ret == -EEXIST) { |
| 1108 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1109 | struct btrfs_extent_data_ref); |
| 1110 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1111 | owner, offset)) |
| 1112 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1113 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1114 | key.offset++; |
| 1115 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1116 | size); |
| 1117 | if (ret && ret != -EEXIST) |
| 1118 | goto fail; |
| 1119 | |
| 1120 | leaf = path->nodes[0]; |
| 1121 | } |
| 1122 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1123 | struct btrfs_extent_data_ref); |
| 1124 | if (ret == 0) { |
| 1125 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1126 | root_objectid); |
| 1127 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1128 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1129 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1130 | } else { |
| 1131 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1132 | num_refs += refs_to_add; |
| 1133 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1134 | } |
| 1135 | } |
| 1136 | btrfs_mark_buffer_dirty(leaf); |
| 1137 | ret = 0; |
| 1138 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1139 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1140 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1141 | } |
| 1142 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1143 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1144 | struct btrfs_path *path, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1145 | int refs_to_drop, int *last_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1146 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1147 | struct btrfs_key key; |
| 1148 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1149 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1150 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1151 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1152 | int ret = 0; |
| 1153 | |
| 1154 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1155 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1156 | |
| 1157 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1158 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1159 | struct btrfs_extent_data_ref); |
| 1160 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1161 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1162 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1163 | struct btrfs_shared_data_ref); |
| 1164 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 1165 | } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 1166 | btrfs_print_v0_err(trans->fs_info); |
| 1167 | btrfs_abort_transaction(trans, -EINVAL); |
| 1168 | return -EINVAL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1169 | } else { |
| 1170 | BUG(); |
| 1171 | } |
| 1172 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1173 | BUG_ON(num_refs < refs_to_drop); |
| 1174 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1175 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1176 | if (num_refs == 0) { |
Nikolay Borisov | e9f6290 | 2018-06-20 15:48:46 +0300 | [diff] [blame] | 1177 | ret = btrfs_del_item(trans, trans->fs_info->extent_root, path); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1178 | *last_ref = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1179 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1180 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1181 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1182 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1183 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1184 | btrfs_mark_buffer_dirty(leaf); |
| 1185 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1186 | return ret; |
| 1187 | } |
| 1188 | |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 1189 | static noinline u32 extent_data_ref_count(struct btrfs_path *path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1190 | struct btrfs_extent_inline_ref *iref) |
| 1191 | { |
| 1192 | struct btrfs_key key; |
| 1193 | struct extent_buffer *leaf; |
| 1194 | struct btrfs_extent_data_ref *ref1; |
| 1195 | struct btrfs_shared_data_ref *ref2; |
| 1196 | u32 num_refs = 0; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 1197 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1198 | |
| 1199 | leaf = path->nodes[0]; |
| 1200 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 1201 | |
| 1202 | BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1203 | if (iref) { |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 1204 | /* |
| 1205 | * If type is invalid, we should have bailed out earlier than |
| 1206 | * this call. |
| 1207 | */ |
| 1208 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); |
| 1209 | ASSERT(type != BTRFS_REF_TYPE_INVALID); |
| 1210 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1211 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1212 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1213 | } else { |
| 1214 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1215 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1216 | } |
| 1217 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1218 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1219 | struct btrfs_extent_data_ref); |
| 1220 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1221 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1222 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1223 | struct btrfs_shared_data_ref); |
| 1224 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1225 | } else { |
| 1226 | WARN_ON(1); |
| 1227 | } |
| 1228 | return num_refs; |
| 1229 | } |
| 1230 | |
| 1231 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1232 | struct btrfs_path *path, |
| 1233 | u64 bytenr, u64 parent, |
| 1234 | u64 root_objectid) |
| 1235 | { |
Nikolay Borisov | b8582ee | 2018-06-20 15:48:50 +0300 | [diff] [blame] | 1236 | struct btrfs_root *root = trans->fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1237 | struct btrfs_key key; |
| 1238 | int ret; |
| 1239 | |
| 1240 | key.objectid = bytenr; |
| 1241 | if (parent) { |
| 1242 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1243 | key.offset = parent; |
| 1244 | } else { |
| 1245 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1246 | key.offset = root_objectid; |
| 1247 | } |
| 1248 | |
| 1249 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1250 | if (ret > 0) |
| 1251 | ret = -ENOENT; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1252 | return ret; |
| 1253 | } |
| 1254 | |
| 1255 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1256 | struct btrfs_path *path, |
| 1257 | u64 bytenr, u64 parent, |
| 1258 | u64 root_objectid) |
| 1259 | { |
| 1260 | struct btrfs_key key; |
| 1261 | int ret; |
| 1262 | |
| 1263 | key.objectid = bytenr; |
| 1264 | if (parent) { |
| 1265 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1266 | key.offset = parent; |
| 1267 | } else { |
| 1268 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1269 | key.offset = root_objectid; |
| 1270 | } |
| 1271 | |
Nikolay Borisov | 1072840 | 2018-06-20 15:48:43 +0300 | [diff] [blame] | 1272 | ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1273 | path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1274 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1275 | return ret; |
| 1276 | } |
| 1277 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1278 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1279 | { |
| 1280 | int type; |
| 1281 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1282 | if (parent > 0) |
| 1283 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1284 | else |
| 1285 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1286 | } else { |
| 1287 | if (parent > 0) |
| 1288 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1289 | else |
| 1290 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1291 | } |
| 1292 | return type; |
| 1293 | } |
| 1294 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1295 | static int find_next_key(struct btrfs_path *path, int level, |
| 1296 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1297 | |
| 1298 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1299 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1300 | if (!path->nodes[level]) |
| 1301 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1302 | if (path->slots[level] + 1 >= |
| 1303 | btrfs_header_nritems(path->nodes[level])) |
| 1304 | continue; |
| 1305 | if (level == 0) |
| 1306 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1307 | path->slots[level] + 1); |
| 1308 | else |
| 1309 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1310 | path->slots[level] + 1); |
| 1311 | return 0; |
| 1312 | } |
| 1313 | return 1; |
| 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1318 | * to the address of inline back ref, and 0 is returned. |
| 1319 | * |
| 1320 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1321 | * should be inserted, and -ENOENT is returned. |
| 1322 | * |
| 1323 | * if insert is true and there are too many inline back refs, the path |
| 1324 | * points to the extent item, and -EAGAIN is returned. |
| 1325 | * |
| 1326 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1327 | * items in the tree are ordered. |
| 1328 | */ |
| 1329 | static noinline_for_stack |
| 1330 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1331 | struct btrfs_path *path, |
| 1332 | struct btrfs_extent_inline_ref **ref_ret, |
| 1333 | u64 bytenr, u64 num_bytes, |
| 1334 | u64 parent, u64 root_objectid, |
| 1335 | u64 owner, u64 offset, int insert) |
| 1336 | { |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 1337 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1338 | struct btrfs_root *root = fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1339 | struct btrfs_key key; |
| 1340 | struct extent_buffer *leaf; |
| 1341 | struct btrfs_extent_item *ei; |
| 1342 | struct btrfs_extent_inline_ref *iref; |
| 1343 | u64 flags; |
| 1344 | u64 item_size; |
| 1345 | unsigned long ptr; |
| 1346 | unsigned long end; |
| 1347 | int extra_size; |
| 1348 | int type; |
| 1349 | int want; |
| 1350 | int ret; |
| 1351 | int err = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1352 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 1353 | int needed; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1354 | |
| 1355 | key.objectid = bytenr; |
| 1356 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1357 | key.offset = num_bytes; |
| 1358 | |
| 1359 | want = extent_ref_type(parent, owner); |
| 1360 | if (insert) { |
| 1361 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1362 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1363 | } else |
| 1364 | extra_size = -1; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1365 | |
| 1366 | /* |
Nikolay Borisov | 16d1c06 | 2018-06-18 14:59:26 +0300 | [diff] [blame] | 1367 | * Owner is our level, so we can just add one to get the level for the |
| 1368 | * block we are interested in. |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1369 | */ |
| 1370 | if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1371 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 1372 | key.offset = owner; |
| 1373 | } |
| 1374 | |
| 1375 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1376 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1377 | if (ret < 0) { |
| 1378 | err = ret; |
| 1379 | goto out; |
| 1380 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1381 | |
| 1382 | /* |
| 1383 | * We may be a newly converted file system which still has the old fat |
| 1384 | * extent entries for metadata, so try and see if we have one of those. |
| 1385 | */ |
| 1386 | if (ret > 0 && skinny_metadata) { |
| 1387 | skinny_metadata = false; |
| 1388 | if (path->slots[0]) { |
| 1389 | path->slots[0]--; |
| 1390 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1391 | path->slots[0]); |
| 1392 | if (key.objectid == bytenr && |
| 1393 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 1394 | key.offset == num_bytes) |
| 1395 | ret = 0; |
| 1396 | } |
| 1397 | if (ret) { |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 1398 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1399 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1400 | key.offset = num_bytes; |
| 1401 | btrfs_release_path(path); |
| 1402 | goto again; |
| 1403 | } |
| 1404 | } |
| 1405 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1406 | if (ret && !insert) { |
| 1407 | err = -ENOENT; |
| 1408 | goto out; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1409 | } else if (WARN_ON(ret)) { |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 1410 | err = -EIO; |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 1411 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1412 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1413 | |
| 1414 | leaf = path->nodes[0]; |
| 1415 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 1416 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 1417 | err = -EINVAL; |
| 1418 | btrfs_print_v0_err(fs_info); |
| 1419 | btrfs_abort_transaction(trans, err); |
| 1420 | goto out; |
| 1421 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1422 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1423 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1424 | flags = btrfs_extent_flags(leaf, ei); |
| 1425 | |
| 1426 | ptr = (unsigned long)(ei + 1); |
| 1427 | end = (unsigned long)ei + item_size; |
| 1428 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1429 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1430 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1431 | BUG_ON(ptr > end); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1432 | } |
| 1433 | |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 1434 | if (owner >= BTRFS_FIRST_FREE_OBJECTID) |
| 1435 | needed = BTRFS_REF_TYPE_DATA; |
| 1436 | else |
| 1437 | needed = BTRFS_REF_TYPE_BLOCK; |
| 1438 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1439 | err = -ENOENT; |
| 1440 | while (1) { |
| 1441 | if (ptr >= end) { |
| 1442 | WARN_ON(ptr > end); |
| 1443 | break; |
| 1444 | } |
| 1445 | iref = (struct btrfs_extent_inline_ref *)ptr; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 1446 | type = btrfs_get_extent_inline_ref_type(leaf, iref, needed); |
| 1447 | if (type == BTRFS_REF_TYPE_INVALID) { |
Su Yue | af431dc | 2018-06-22 16:18:01 +0800 | [diff] [blame] | 1448 | err = -EUCLEAN; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 1449 | goto out; |
| 1450 | } |
| 1451 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1452 | if (want < type) |
| 1453 | break; |
| 1454 | if (want > type) { |
| 1455 | ptr += btrfs_extent_inline_ref_size(type); |
| 1456 | continue; |
| 1457 | } |
| 1458 | |
| 1459 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1460 | struct btrfs_extent_data_ref *dref; |
| 1461 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1462 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1463 | owner, offset)) { |
| 1464 | err = 0; |
| 1465 | break; |
| 1466 | } |
| 1467 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1468 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1469 | break; |
| 1470 | } else { |
| 1471 | u64 ref_offset; |
| 1472 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1473 | if (parent > 0) { |
| 1474 | if (parent == ref_offset) { |
| 1475 | err = 0; |
| 1476 | break; |
| 1477 | } |
| 1478 | if (ref_offset < parent) |
| 1479 | break; |
| 1480 | } else { |
| 1481 | if (root_objectid == ref_offset) { |
| 1482 | err = 0; |
| 1483 | break; |
| 1484 | } |
| 1485 | if (ref_offset < root_objectid) |
| 1486 | break; |
| 1487 | } |
| 1488 | } |
| 1489 | ptr += btrfs_extent_inline_ref_size(type); |
| 1490 | } |
| 1491 | if (err == -ENOENT && insert) { |
| 1492 | if (item_size + extra_size >= |
| 1493 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1494 | err = -EAGAIN; |
| 1495 | goto out; |
| 1496 | } |
| 1497 | /* |
| 1498 | * To add new inline back ref, we have to make sure |
| 1499 | * there is no corresponding back ref item. |
| 1500 | * For simplicity, we just do not add new inline back |
| 1501 | * ref if there is any kind of item for this block |
| 1502 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1503 | if (find_next_key(path, 0, &key) == 0 && |
| 1504 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1505 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1506 | err = -EAGAIN; |
| 1507 | goto out; |
| 1508 | } |
| 1509 | } |
| 1510 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1511 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1512 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1513 | path->keep_locks = 0; |
| 1514 | btrfs_unlock_up_safe(path, 1); |
| 1515 | } |
| 1516 | return err; |
| 1517 | } |
| 1518 | |
| 1519 | /* |
| 1520 | * helper to add new inline back ref |
| 1521 | */ |
| 1522 | static noinline_for_stack |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1523 | void setup_inline_extent_backref(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1524 | struct btrfs_path *path, |
| 1525 | struct btrfs_extent_inline_ref *iref, |
| 1526 | u64 parent, u64 root_objectid, |
| 1527 | u64 owner, u64 offset, int refs_to_add, |
| 1528 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1529 | { |
| 1530 | struct extent_buffer *leaf; |
| 1531 | struct btrfs_extent_item *ei; |
| 1532 | unsigned long ptr; |
| 1533 | unsigned long end; |
| 1534 | unsigned long item_offset; |
| 1535 | u64 refs; |
| 1536 | int size; |
| 1537 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1538 | |
| 1539 | leaf = path->nodes[0]; |
| 1540 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1541 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1542 | |
| 1543 | type = extent_ref_type(parent, owner); |
| 1544 | size = btrfs_extent_inline_ref_size(type); |
| 1545 | |
David Sterba | c71dd88 | 2019-03-20 14:51:10 +0100 | [diff] [blame] | 1546 | btrfs_extend_item(path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1547 | |
| 1548 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1549 | refs = btrfs_extent_refs(leaf, ei); |
| 1550 | refs += refs_to_add; |
| 1551 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1552 | if (extent_op) |
| 1553 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1554 | |
| 1555 | ptr = (unsigned long)ei + item_offset; |
| 1556 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1557 | if (ptr < end - size) |
| 1558 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1559 | end - size - ptr); |
| 1560 | |
| 1561 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1562 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1563 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1564 | struct btrfs_extent_data_ref *dref; |
| 1565 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1566 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1567 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1568 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1569 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1570 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1571 | struct btrfs_shared_data_ref *sref; |
| 1572 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1573 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1574 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1575 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1576 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1577 | } else { |
| 1578 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1579 | } |
| 1580 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1584 | struct btrfs_path *path, |
| 1585 | struct btrfs_extent_inline_ref **ref_ret, |
| 1586 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1587 | u64 root_objectid, u64 owner, u64 offset) |
| 1588 | { |
| 1589 | int ret; |
| 1590 | |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 1591 | ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr, |
| 1592 | num_bytes, parent, root_objectid, |
| 1593 | owner, offset, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1594 | if (ret != -ENOENT) |
| 1595 | return ret; |
| 1596 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1597 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1598 | *ref_ret = NULL; |
| 1599 | |
| 1600 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Nikolay Borisov | b8582ee | 2018-06-20 15:48:50 +0300 | [diff] [blame] | 1601 | ret = lookup_tree_block_ref(trans, path, bytenr, parent, |
| 1602 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1603 | } else { |
Nikolay Borisov | bd1d53e | 2018-06-20 15:48:51 +0300 | [diff] [blame] | 1604 | ret = lookup_extent_data_ref(trans, path, bytenr, parent, |
| 1605 | root_objectid, owner, offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1606 | } |
| 1607 | return ret; |
| 1608 | } |
| 1609 | |
| 1610 | /* |
| 1611 | * helper to update/remove inline back ref |
| 1612 | */ |
| 1613 | static noinline_for_stack |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1614 | void update_inline_extent_backref(struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1615 | struct btrfs_extent_inline_ref *iref, |
| 1616 | int refs_to_mod, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1617 | struct btrfs_delayed_extent_op *extent_op, |
| 1618 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1619 | { |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1620 | struct extent_buffer *leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1621 | struct btrfs_extent_item *ei; |
| 1622 | struct btrfs_extent_data_ref *dref = NULL; |
| 1623 | struct btrfs_shared_data_ref *sref = NULL; |
| 1624 | unsigned long ptr; |
| 1625 | unsigned long end; |
| 1626 | u32 item_size; |
| 1627 | int size; |
| 1628 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1629 | u64 refs; |
| 1630 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1631 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1632 | refs = btrfs_extent_refs(leaf, ei); |
| 1633 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1634 | refs += refs_to_mod; |
| 1635 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1636 | if (extent_op) |
| 1637 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1638 | |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 1639 | /* |
| 1640 | * If type is invalid, we should have bailed out after |
| 1641 | * lookup_inline_extent_backref(). |
| 1642 | */ |
| 1643 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY); |
| 1644 | ASSERT(type != BTRFS_REF_TYPE_INVALID); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1645 | |
| 1646 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1647 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1648 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1649 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1650 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1651 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1652 | } else { |
| 1653 | refs = 1; |
| 1654 | BUG_ON(refs_to_mod != -1); |
| 1655 | } |
| 1656 | |
| 1657 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1658 | refs += refs_to_mod; |
| 1659 | |
| 1660 | if (refs > 0) { |
| 1661 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1662 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1663 | else |
| 1664 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1665 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1666 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1667 | size = btrfs_extent_inline_ref_size(type); |
| 1668 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1669 | ptr = (unsigned long)iref; |
| 1670 | end = (unsigned long)ei + item_size; |
| 1671 | if (ptr + size < end) |
| 1672 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1673 | end - ptr - size); |
| 1674 | item_size -= size; |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 1675 | btrfs_truncate_item(path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1676 | } |
| 1677 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | static noinline_for_stack |
| 1681 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1682 | struct btrfs_path *path, |
| 1683 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1684 | u64 root_objectid, u64 owner, |
| 1685 | u64 offset, int refs_to_add, |
| 1686 | struct btrfs_delayed_extent_op *extent_op) |
| 1687 | { |
| 1688 | struct btrfs_extent_inline_ref *iref; |
| 1689 | int ret; |
| 1690 | |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 1691 | ret = lookup_inline_extent_backref(trans, path, &iref, bytenr, |
| 1692 | num_bytes, parent, root_objectid, |
| 1693 | owner, offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1694 | if (ret == 0) { |
| 1695 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1696 | update_inline_extent_backref(path, iref, refs_to_add, |
| 1697 | extent_op, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1698 | } else if (ret == -ENOENT) { |
Nikolay Borisov | a639cde | 2018-06-20 15:49:10 +0300 | [diff] [blame] | 1699 | setup_inline_extent_backref(trans->fs_info, path, iref, parent, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1700 | root_objectid, owner, offset, |
| 1701 | refs_to_add, extent_op); |
| 1702 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1703 | } |
| 1704 | return ret; |
| 1705 | } |
| 1706 | |
| 1707 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1708 | struct btrfs_path *path, |
| 1709 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1710 | u64 owner, u64 offset, int refs_to_add) |
| 1711 | { |
| 1712 | int ret; |
| 1713 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1714 | BUG_ON(refs_to_add != 1); |
Nikolay Borisov | 1072840 | 2018-06-20 15:48:43 +0300 | [diff] [blame] | 1715 | ret = insert_tree_block_ref(trans, path, bytenr, parent, |
| 1716 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1717 | } else { |
Nikolay Borisov | 62b895a | 2018-06-20 15:48:44 +0300 | [diff] [blame] | 1718 | ret = insert_extent_data_ref(trans, path, bytenr, parent, |
| 1719 | root_objectid, owner, offset, |
| 1720 | refs_to_add); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1721 | } |
| 1722 | return ret; |
| 1723 | } |
| 1724 | |
| 1725 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1726 | struct btrfs_path *path, |
| 1727 | struct btrfs_extent_inline_ref *iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1728 | int refs_to_drop, int is_data, int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1729 | { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1730 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1731 | |
| 1732 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1733 | if (iref) { |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1734 | update_inline_extent_backref(path, iref, -refs_to_drop, NULL, |
| 1735 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1736 | } else if (is_data) { |
Nikolay Borisov | e9f6290 | 2018-06-20 15:48:46 +0300 | [diff] [blame] | 1737 | ret = remove_extent_data_ref(trans, path, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1738 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1739 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1740 | *last_ref = 1; |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 1741 | ret = btrfs_del_item(trans, trans->fs_info->extent_root, path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1742 | } |
| 1743 | return ret; |
| 1744 | } |
| 1745 | |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1746 | static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, |
| 1747 | u64 *discarded_bytes) |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1748 | { |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1749 | int j, ret = 0; |
| 1750 | u64 bytes_left, end; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1751 | u64 aligned_start = ALIGN(start, 1 << 9); |
| 1752 | |
| 1753 | if (WARN_ON(start != aligned_start)) { |
| 1754 | len -= aligned_start - start; |
| 1755 | len = round_down(len, 1 << 9); |
| 1756 | start = aligned_start; |
| 1757 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1758 | |
| 1759 | *discarded_bytes = 0; |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1760 | |
| 1761 | if (!len) |
| 1762 | return 0; |
| 1763 | |
| 1764 | end = start + len; |
| 1765 | bytes_left = len; |
| 1766 | |
| 1767 | /* Skip any superblocks on this device. */ |
| 1768 | for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) { |
| 1769 | u64 sb_start = btrfs_sb_offset(j); |
| 1770 | u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE; |
| 1771 | u64 size = sb_start - start; |
| 1772 | |
| 1773 | if (!in_range(sb_start, start, bytes_left) && |
| 1774 | !in_range(sb_end, start, bytes_left) && |
| 1775 | !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE)) |
| 1776 | continue; |
| 1777 | |
| 1778 | /* |
| 1779 | * Superblock spans beginning of range. Adjust start and |
| 1780 | * try again. |
| 1781 | */ |
| 1782 | if (sb_start <= start) { |
| 1783 | start += sb_end - start; |
| 1784 | if (start > end) { |
| 1785 | bytes_left = 0; |
| 1786 | break; |
| 1787 | } |
| 1788 | bytes_left = end - start; |
| 1789 | continue; |
| 1790 | } |
| 1791 | |
| 1792 | if (size) { |
| 1793 | ret = blkdev_issue_discard(bdev, start >> 9, size >> 9, |
| 1794 | GFP_NOFS, 0); |
| 1795 | if (!ret) |
| 1796 | *discarded_bytes += size; |
| 1797 | else if (ret != -EOPNOTSUPP) |
| 1798 | return ret; |
| 1799 | } |
| 1800 | |
| 1801 | start = sb_end; |
| 1802 | if (start > end) { |
| 1803 | bytes_left = 0; |
| 1804 | break; |
| 1805 | } |
| 1806 | bytes_left = end - start; |
| 1807 | } |
| 1808 | |
| 1809 | if (bytes_left) { |
| 1810 | ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9, |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1811 | GFP_NOFS, 0); |
| 1812 | if (!ret) |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1813 | *discarded_bytes += bytes_left; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1814 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1815 | return ret; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1816 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1817 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1818 | int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, |
Filipe Manana | 1edb647b | 2014-12-08 14:01:12 +0000 | [diff] [blame] | 1819 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1820 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1821 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1822 | u64 discarded_bytes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1823 | struct btrfs_bio *bbio = NULL; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1824 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1825 | |
Filipe Manana | 2999241 | 2016-05-27 17:42:05 +0100 | [diff] [blame] | 1826 | /* |
| 1827 | * Avoid races with device replace and make sure our bbio has devices |
| 1828 | * associated to its stripes that don't go away while we are discarding. |
| 1829 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1830 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1831 | /* Tell the block device(s) that the sectors can be discarded */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1832 | ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes, |
| 1833 | &bbio, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1834 | /* Error condition is -ENOMEM */ |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1835 | if (!ret) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1836 | struct btrfs_bio_stripe *stripe = bbio->stripes; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1837 | int i; |
| 1838 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1839 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1840 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1841 | u64 bytes; |
Anand Jain | 38b5f68 | 2017-11-29 18:53:43 +0800 | [diff] [blame] | 1842 | struct request_queue *req_q; |
| 1843 | |
Filipe Manana | 627e087 | 2018-01-30 18:40:22 +0000 | [diff] [blame] | 1844 | if (!stripe->dev->bdev) { |
| 1845 | ASSERT(btrfs_test_opt(fs_info, DEGRADED)); |
| 1846 | continue; |
| 1847 | } |
Anand Jain | 38b5f68 | 2017-11-29 18:53:43 +0800 | [diff] [blame] | 1848 | req_q = bdev_get_queue(stripe->dev->bdev); |
| 1849 | if (!blk_queue_discard(req_q)) |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1850 | continue; |
| 1851 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1852 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 1853 | stripe->physical, |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1854 | stripe->length, |
| 1855 | &bytes); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1856 | if (!ret) |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1857 | discarded_bytes += bytes; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1858 | else if (ret != -EOPNOTSUPP) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1859 | break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */ |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1860 | |
| 1861 | /* |
| 1862 | * Just in case we get back EOPNOTSUPP for some reason, |
| 1863 | * just ignore the return value so we don't screw up |
| 1864 | * people calling discard_extent. |
| 1865 | */ |
| 1866 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1867 | } |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 1868 | btrfs_put_bbio(bbio); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1869 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1870 | btrfs_bio_counter_dec(fs_info); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1871 | |
| 1872 | if (actual_bytes) |
| 1873 | *actual_bytes = discarded_bytes; |
| 1874 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1875 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1876 | if (ret == -EOPNOTSUPP) |
| 1877 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1878 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1879 | } |
| 1880 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1881 | /* Can return -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1882 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1883 | struct btrfs_ref *generic_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1884 | { |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1885 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1886 | int old_ref_mod, new_ref_mod; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1887 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1888 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1889 | ASSERT(generic_ref->type != BTRFS_REF_NOT_SET && |
| 1890 | generic_ref->action); |
| 1891 | BUG_ON(generic_ref->type == BTRFS_REF_METADATA && |
| 1892 | generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1893 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1894 | if (generic_ref->type == BTRFS_REF_METADATA) |
| 1895 | ret = btrfs_add_delayed_tree_ref(trans, generic_ref, |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 1896 | NULL, &old_ref_mod, &new_ref_mod); |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1897 | else |
| 1898 | ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1899 | &old_ref_mod, &new_ref_mod); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1900 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1901 | btrfs_ref_tree_mod(fs_info, generic_ref); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 1902 | |
Qu Wenruo | ddf30cf | 2019-04-04 14:45:34 +0800 | [diff] [blame] | 1903 | if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 1904 | sub_pinned_bytes(fs_info, generic_ref); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1905 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1906 | return ret; |
| 1907 | } |
| 1908 | |
Nikolay Borisov | bd3c685 | 2018-06-18 14:59:25 +0300 | [diff] [blame] | 1909 | /* |
| 1910 | * __btrfs_inc_extent_ref - insert backreference for a given extent |
| 1911 | * |
| 1912 | * @trans: Handle of transaction |
| 1913 | * |
| 1914 | * @node: The delayed ref node used to get the bytenr/length for |
| 1915 | * extent whose references are incremented. |
| 1916 | * |
| 1917 | * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/ |
| 1918 | * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical |
| 1919 | * bytenr of the parent block. Since new extents are always |
| 1920 | * created with indirect references, this will only be the case |
| 1921 | * when relocating a shared extent. In that case, root_objectid |
| 1922 | * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must |
| 1923 | * be 0 |
| 1924 | * |
| 1925 | * @root_objectid: The id of the root where this modification has originated, |
| 1926 | * this can be either one of the well-known metadata trees or |
| 1927 | * the subvolume id which references this extent. |
| 1928 | * |
| 1929 | * @owner: For data extents it is the inode number of the owning file. |
| 1930 | * For metadata extents this parameter holds the level in the |
| 1931 | * tree of the extent. |
| 1932 | * |
| 1933 | * @offset: For metadata extents the offset is ignored and is currently |
| 1934 | * always passed as 0. For data extents it is the fileoffset |
| 1935 | * this extent belongs to. |
| 1936 | * |
| 1937 | * @refs_to_add Number of references to add |
| 1938 | * |
| 1939 | * @extent_op Pointer to a structure, holding information necessary when |
| 1940 | * updating a tree block's flags |
| 1941 | * |
| 1942 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1943 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1944 | struct btrfs_delayed_ref_node *node, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1945 | u64 parent, u64 root_objectid, |
| 1946 | u64 owner, u64 offset, int refs_to_add, |
| 1947 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1948 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1949 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1950 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1951 | struct btrfs_extent_item *item; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1952 | struct btrfs_key key; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1953 | u64 bytenr = node->bytenr; |
| 1954 | u64 num_bytes = node->num_bytes; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1955 | u64 refs; |
| 1956 | int ret; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1957 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1958 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1959 | if (!path) |
| 1960 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1961 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1962 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1963 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1964 | /* this will setup the path even if it fails to insert the back ref */ |
Nikolay Borisov | a639cde | 2018-06-20 15:49:10 +0300 | [diff] [blame] | 1965 | ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes, |
| 1966 | parent, root_objectid, owner, |
| 1967 | offset, refs_to_add, extent_op); |
Qu Wenruo | 0ed4792 | 2015-04-16 16:55:08 +0800 | [diff] [blame] | 1968 | if ((ret < 0 && ret != -EAGAIN) || !ret) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1969 | goto out; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1970 | |
| 1971 | /* |
| 1972 | * Ok we had -EAGAIN which means we didn't have space to insert and |
| 1973 | * inline extent ref, so just update the reference count and add a |
| 1974 | * normal backref. |
| 1975 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1976 | leaf = path->nodes[0]; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1977 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1978 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1979 | refs = btrfs_extent_refs(leaf, item); |
| 1980 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1981 | if (extent_op) |
| 1982 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1983 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1984 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1985 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1986 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1987 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1988 | path->leave_spinning = 1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1989 | /* now insert the actual backref */ |
Nikolay Borisov | 3759341 | 2018-06-20 15:48:45 +0300 | [diff] [blame] | 1990 | ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid, |
| 1991 | owner, offset, refs_to_add); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1992 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1993 | btrfs_abort_transaction(trans, ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1994 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1995 | btrfs_free_path(path); |
Liu Bo | 30d133f | 2013-10-11 16:30:23 +0800 | [diff] [blame] | 1996 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1997 | } |
| 1998 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1999 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2000 | struct btrfs_delayed_ref_node *node, |
| 2001 | struct btrfs_delayed_extent_op *extent_op, |
| 2002 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2003 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2004 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2005 | struct btrfs_delayed_data_ref *ref; |
| 2006 | struct btrfs_key ins; |
| 2007 | u64 parent = 0; |
| 2008 | u64 ref_root = 0; |
| 2009 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2010 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2011 | ins.objectid = node->bytenr; |
| 2012 | ins.offset = node->num_bytes; |
| 2013 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2014 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2015 | ref = btrfs_delayed_node_to_data_ref(node); |
Nikolay Borisov | 2bf98ef | 2018-06-20 15:49:00 +0300 | [diff] [blame] | 2016 | trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2017 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2018 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2019 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2020 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2021 | |
| 2022 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2023 | if (extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2024 | flags |= extent_op->flags_to_set; |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 2025 | ret = alloc_reserved_file_extent(trans, parent, ref_root, |
| 2026 | flags, ref->objectid, |
| 2027 | ref->offset, &ins, |
| 2028 | node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2029 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Nikolay Borisov | 2590d0f | 2018-06-20 15:48:59 +0300 | [diff] [blame] | 2030 | ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root, |
| 2031 | ref->objectid, ref->offset, |
| 2032 | node->ref_mod, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2033 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 2034 | ret = __btrfs_free_extent(trans, node, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2035 | ref_root, ref->objectid, |
| 2036 | ref->offset, node->ref_mod, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2037 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2038 | } else { |
| 2039 | BUG(); |
| 2040 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2041 | return ret; |
| 2042 | } |
| 2043 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2044 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 2045 | struct extent_buffer *leaf, |
| 2046 | struct btrfs_extent_item *ei) |
| 2047 | { |
| 2048 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 2049 | if (extent_op->update_flags) { |
| 2050 | flags |= extent_op->flags_to_set; |
| 2051 | btrfs_set_extent_flags(leaf, ei, flags); |
| 2052 | } |
| 2053 | |
| 2054 | if (extent_op->update_key) { |
| 2055 | struct btrfs_tree_block_info *bi; |
| 2056 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 2057 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 2058 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2063 | struct btrfs_delayed_ref_head *head, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2064 | struct btrfs_delayed_extent_op *extent_op) |
| 2065 | { |
Nikolay Borisov | 20b9a2d | 2018-06-20 15:49:01 +0300 | [diff] [blame] | 2066 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2067 | struct btrfs_key key; |
| 2068 | struct btrfs_path *path; |
| 2069 | struct btrfs_extent_item *ei; |
| 2070 | struct extent_buffer *leaf; |
| 2071 | u32 item_size; |
| 2072 | int ret; |
| 2073 | int err = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2074 | int metadata = !extent_op->is_data; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2075 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2076 | if (trans->aborted) |
| 2077 | return 0; |
| 2078 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2079 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2080 | metadata = 0; |
| 2081 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2082 | path = btrfs_alloc_path(); |
| 2083 | if (!path) |
| 2084 | return -ENOMEM; |
| 2085 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2086 | key.objectid = head->bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2087 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2088 | if (metadata) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2089 | key.type = BTRFS_METADATA_ITEM_KEY; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2090 | key.offset = extent_op->level; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2091 | } else { |
| 2092 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2093 | key.offset = head->num_bytes; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | again: |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2097 | path->reada = READA_FORWARD; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2098 | path->leave_spinning = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2099 | ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2100 | if (ret < 0) { |
| 2101 | err = ret; |
| 2102 | goto out; |
| 2103 | } |
| 2104 | if (ret > 0) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2105 | if (metadata) { |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2106 | if (path->slots[0] > 0) { |
| 2107 | path->slots[0]--; |
| 2108 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 2109 | path->slots[0]); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2110 | if (key.objectid == head->bytenr && |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2111 | key.type == BTRFS_EXTENT_ITEM_KEY && |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2112 | key.offset == head->num_bytes) |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2113 | ret = 0; |
| 2114 | } |
| 2115 | if (ret > 0) { |
| 2116 | btrfs_release_path(path); |
| 2117 | metadata = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2118 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2119 | key.objectid = head->bytenr; |
| 2120 | key.offset = head->num_bytes; |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2121 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2122 | goto again; |
| 2123 | } |
| 2124 | } else { |
| 2125 | err = -EIO; |
| 2126 | goto out; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2127 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | leaf = path->nodes[0]; |
| 2131 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 2132 | |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 2133 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 2134 | err = -EINVAL; |
| 2135 | btrfs_print_v0_err(fs_info); |
| 2136 | btrfs_abort_transaction(trans, err); |
| 2137 | goto out; |
| 2138 | } |
| 2139 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2140 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2141 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 2142 | |
| 2143 | btrfs_mark_buffer_dirty(leaf); |
| 2144 | out: |
| 2145 | btrfs_free_path(path); |
| 2146 | return err; |
| 2147 | } |
| 2148 | |
| 2149 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2150 | struct btrfs_delayed_ref_node *node, |
| 2151 | struct btrfs_delayed_extent_op *extent_op, |
| 2152 | int insert_reserved) |
| 2153 | { |
| 2154 | int ret = 0; |
| 2155 | struct btrfs_delayed_tree_ref *ref; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2156 | u64 parent = 0; |
| 2157 | u64 ref_root = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2158 | |
| 2159 | ref = btrfs_delayed_node_to_tree_ref(node); |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 2160 | trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2161 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2162 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2163 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2164 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2165 | |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 2166 | if (node->ref_mod != 1) { |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 2167 | btrfs_err(trans->fs_info, |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 2168 | "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu", |
| 2169 | node->bytenr, node->ref_mod, node->action, ref_root, |
| 2170 | parent); |
| 2171 | return -EIO; |
| 2172 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2173 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2174 | BUG_ON(!extent_op || !extent_op->update_flags); |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 2175 | ret = alloc_reserved_tree_block(trans, node, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2176 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Nikolay Borisov | 2590d0f | 2018-06-20 15:48:59 +0300 | [diff] [blame] | 2177 | ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root, |
| 2178 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2179 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 2180 | ret = __btrfs_free_extent(trans, node, parent, ref_root, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2181 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2182 | } else { |
| 2183 | BUG(); |
| 2184 | } |
| 2185 | return ret; |
| 2186 | } |
| 2187 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2188 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2189 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2190 | struct btrfs_delayed_ref_node *node, |
| 2191 | struct btrfs_delayed_extent_op *extent_op, |
| 2192 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2193 | { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2194 | int ret = 0; |
| 2195 | |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2196 | if (trans->aborted) { |
| 2197 | if (insert_reserved) |
Nikolay Borisov | 5fac7f9 | 2018-06-20 15:49:11 +0300 | [diff] [blame] | 2198 | btrfs_pin_extent(trans->fs_info, node->bytenr, |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2199 | node->num_bytes, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2200 | return 0; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2201 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2202 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2203 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2204 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 2205 | ret = run_delayed_tree_ref(trans, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2206 | insert_reserved); |
| 2207 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2208 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
Nikolay Borisov | 2bf98ef | 2018-06-20 15:49:00 +0300 | [diff] [blame] | 2209 | ret = run_delayed_data_ref(trans, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2210 | insert_reserved); |
| 2211 | else |
| 2212 | BUG(); |
Josef Bacik | 80ee54b | 2018-10-11 15:54:22 -0400 | [diff] [blame] | 2213 | if (ret && insert_reserved) |
| 2214 | btrfs_pin_extent(trans->fs_info, node->bytenr, |
| 2215 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2216 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2217 | } |
| 2218 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2219 | static inline struct btrfs_delayed_ref_node * |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2220 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2221 | { |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2222 | struct btrfs_delayed_ref_node *ref; |
| 2223 | |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 2224 | if (RB_EMPTY_ROOT(&head->ref_tree.rb_root)) |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2225 | return NULL; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2226 | |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2227 | /* |
| 2228 | * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2229 | * This is to prevent a ref count from going down to zero, which deletes |
| 2230 | * the extent item from the extent tree, when there still are references |
| 2231 | * to add, which would fail because they would not find the extent item. |
| 2232 | */ |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 2233 | if (!list_empty(&head->ref_add_list)) |
| 2234 | return list_first_entry(&head->ref_add_list, |
| 2235 | struct btrfs_delayed_ref_node, add_list); |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2236 | |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 2237 | ref = rb_entry(rb_first_cached(&head->ref_tree), |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2238 | struct btrfs_delayed_ref_node, ref_node); |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 2239 | ASSERT(list_empty(&ref->add_list)); |
| 2240 | return ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2241 | } |
| 2242 | |
Josef Bacik | 2eadaa2 | 2017-09-29 15:43:52 -0400 | [diff] [blame] | 2243 | static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, |
| 2244 | struct btrfs_delayed_ref_head *head) |
| 2245 | { |
| 2246 | spin_lock(&delayed_refs->lock); |
| 2247 | head->processing = 0; |
| 2248 | delayed_refs->num_heads_ready++; |
| 2249 | spin_unlock(&delayed_refs->lock); |
| 2250 | btrfs_delayed_ref_unlock(head); |
| 2251 | } |
| 2252 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 2253 | static struct btrfs_delayed_extent_op *cleanup_extent_op( |
| 2254 | struct btrfs_delayed_ref_head *head) |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2255 | { |
| 2256 | struct btrfs_delayed_extent_op *extent_op = head->extent_op; |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 2257 | |
| 2258 | if (!extent_op) |
| 2259 | return NULL; |
| 2260 | |
| 2261 | if (head->must_insert_reserved) { |
| 2262 | head->extent_op = NULL; |
| 2263 | btrfs_free_delayed_extent_op(extent_op); |
| 2264 | return NULL; |
| 2265 | } |
| 2266 | return extent_op; |
| 2267 | } |
| 2268 | |
| 2269 | static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans, |
| 2270 | struct btrfs_delayed_ref_head *head) |
| 2271 | { |
| 2272 | struct btrfs_delayed_extent_op *extent_op; |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2273 | int ret; |
| 2274 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 2275 | extent_op = cleanup_extent_op(head); |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2276 | if (!extent_op) |
| 2277 | return 0; |
| 2278 | head->extent_op = NULL; |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2279 | spin_unlock(&head->lock); |
Nikolay Borisov | 20b9a2d | 2018-06-20 15:49:01 +0300 | [diff] [blame] | 2280 | ret = run_delayed_extent_op(trans, head, extent_op); |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2281 | btrfs_free_delayed_extent_op(extent_op); |
| 2282 | return ret ? ret : 1; |
| 2283 | } |
| 2284 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 2285 | void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info, |
| 2286 | struct btrfs_delayed_ref_root *delayed_refs, |
| 2287 | struct btrfs_delayed_ref_head *head) |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 2288 | { |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 2289 | int nr_items = 1; /* Dropping this ref head update. */ |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 2290 | |
| 2291 | if (head->total_ref_mod < 0) { |
| 2292 | struct btrfs_space_info *space_info; |
| 2293 | u64 flags; |
| 2294 | |
| 2295 | if (head->is_data) |
| 2296 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 2297 | else if (head->is_system) |
| 2298 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 2299 | else |
| 2300 | flags = BTRFS_BLOCK_GROUP_METADATA; |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 2301 | space_info = btrfs_find_space_info(fs_info, flags); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 2302 | ASSERT(space_info); |
| 2303 | percpu_counter_add_batch(&space_info->total_bytes_pinned, |
| 2304 | -head->num_bytes, |
| 2305 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
| 2306 | |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 2307 | /* |
| 2308 | * We had csum deletions accounted for in our delayed refs rsv, |
| 2309 | * we need to drop the csum leaves for this update from our |
| 2310 | * delayed_refs_rsv. |
| 2311 | */ |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 2312 | if (head->is_data) { |
| 2313 | spin_lock(&delayed_refs->lock); |
| 2314 | delayed_refs->pending_csums -= head->num_bytes; |
| 2315 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 2316 | nr_items += btrfs_csum_bytes_to_leaves(fs_info, |
| 2317 | head->num_bytes); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 2318 | } |
| 2319 | } |
| 2320 | |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 2321 | btrfs_delayed_refs_rsv_release(fs_info, nr_items); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 2322 | } |
| 2323 | |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2324 | static int cleanup_ref_head(struct btrfs_trans_handle *trans, |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2325 | struct btrfs_delayed_ref_head *head) |
| 2326 | { |
Nikolay Borisov | f9871ed | 2018-06-20 15:49:03 +0300 | [diff] [blame] | 2327 | |
| 2328 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2329 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2330 | int ret; |
| 2331 | |
| 2332 | delayed_refs = &trans->transaction->delayed_refs; |
| 2333 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 2334 | ret = run_and_cleanup_extent_op(trans, head); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2335 | if (ret < 0) { |
| 2336 | unselect_delayed_ref_head(delayed_refs, head); |
| 2337 | btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret); |
| 2338 | return ret; |
| 2339 | } else if (ret) { |
| 2340 | return ret; |
| 2341 | } |
| 2342 | |
| 2343 | /* |
| 2344 | * Need to drop our head ref lock and re-acquire the delayed ref lock |
| 2345 | * and then re-check to make sure nobody got added. |
| 2346 | */ |
| 2347 | spin_unlock(&head->lock); |
| 2348 | spin_lock(&delayed_refs->lock); |
| 2349 | spin_lock(&head->lock); |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 2350 | if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) { |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2351 | spin_unlock(&head->lock); |
| 2352 | spin_unlock(&delayed_refs->lock); |
| 2353 | return 1; |
| 2354 | } |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 2355 | btrfs_delete_ref_head(delayed_refs, head); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 2356 | spin_unlock(&head->lock); |
Nikolay Borisov | 1e7a142 | 2018-04-11 11:21:18 +0300 | [diff] [blame] | 2357 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 2358 | |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 2359 | if (head->must_insert_reserved) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2360 | btrfs_pin_extent(fs_info, head->bytenr, |
| 2361 | head->num_bytes, 1); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 2362 | if (head->is_data) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2363 | ret = btrfs_del_csums(trans, fs_info, head->bytenr, |
| 2364 | head->num_bytes); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 2365 | } |
| 2366 | } |
| 2367 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 2368 | btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 2369 | |
| 2370 | trace_run_delayed_ref_head(fs_info, head, 0); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 2371 | btrfs_delayed_ref_unlock(head); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2372 | btrfs_put_delayed_ref_head(head); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2373 | return 0; |
| 2374 | } |
| 2375 | |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 2376 | static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head( |
| 2377 | struct btrfs_trans_handle *trans) |
| 2378 | { |
| 2379 | struct btrfs_delayed_ref_root *delayed_refs = |
| 2380 | &trans->transaction->delayed_refs; |
| 2381 | struct btrfs_delayed_ref_head *head = NULL; |
| 2382 | int ret; |
| 2383 | |
| 2384 | spin_lock(&delayed_refs->lock); |
Lu Fengqi | 5637c74 | 2018-10-11 13:40:33 +0800 | [diff] [blame] | 2385 | head = btrfs_select_ref_head(delayed_refs); |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 2386 | if (!head) { |
| 2387 | spin_unlock(&delayed_refs->lock); |
| 2388 | return head; |
| 2389 | } |
| 2390 | |
| 2391 | /* |
| 2392 | * Grab the lock that says we are going to process all the refs for |
| 2393 | * this head |
| 2394 | */ |
Lu Fengqi | 9e920a6 | 2018-10-11 13:40:34 +0800 | [diff] [blame] | 2395 | ret = btrfs_delayed_ref_lock(delayed_refs, head); |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 2396 | spin_unlock(&delayed_refs->lock); |
| 2397 | |
| 2398 | /* |
| 2399 | * We may have dropped the spin lock to get the head mutex lock, and |
| 2400 | * that might have given someone else time to free the head. If that's |
| 2401 | * true, it has been removed from our list and we can move on. |
| 2402 | */ |
| 2403 | if (ret == -EAGAIN) |
| 2404 | head = ERR_PTR(-EAGAIN); |
| 2405 | |
| 2406 | return head; |
| 2407 | } |
| 2408 | |
Nikolay Borisov | e726138 | 2018-08-15 10:39:55 +0300 | [diff] [blame] | 2409 | static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans, |
| 2410 | struct btrfs_delayed_ref_head *locked_ref, |
| 2411 | unsigned long *run_refs) |
| 2412 | { |
| 2413 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 2414 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2415 | struct btrfs_delayed_extent_op *extent_op; |
| 2416 | struct btrfs_delayed_ref_node *ref; |
| 2417 | int must_insert_reserved = 0; |
| 2418 | int ret; |
| 2419 | |
| 2420 | delayed_refs = &trans->transaction->delayed_refs; |
| 2421 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2422 | lockdep_assert_held(&locked_ref->mutex); |
| 2423 | lockdep_assert_held(&locked_ref->lock); |
| 2424 | |
Nikolay Borisov | e726138 | 2018-08-15 10:39:55 +0300 | [diff] [blame] | 2425 | while ((ref = select_delayed_ref(locked_ref))) { |
| 2426 | if (ref->seq && |
| 2427 | btrfs_check_delayed_seq(fs_info, ref->seq)) { |
| 2428 | spin_unlock(&locked_ref->lock); |
| 2429 | unselect_delayed_ref_head(delayed_refs, locked_ref); |
| 2430 | return -EAGAIN; |
| 2431 | } |
| 2432 | |
| 2433 | (*run_refs)++; |
| 2434 | ref->in_tree = 0; |
| 2435 | rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree); |
| 2436 | RB_CLEAR_NODE(&ref->ref_node); |
| 2437 | if (!list_empty(&ref->add_list)) |
| 2438 | list_del(&ref->add_list); |
| 2439 | /* |
| 2440 | * When we play the delayed ref, also correct the ref_mod on |
| 2441 | * head |
| 2442 | */ |
| 2443 | switch (ref->action) { |
| 2444 | case BTRFS_ADD_DELAYED_REF: |
| 2445 | case BTRFS_ADD_DELAYED_EXTENT: |
| 2446 | locked_ref->ref_mod -= ref->ref_mod; |
| 2447 | break; |
| 2448 | case BTRFS_DROP_DELAYED_REF: |
| 2449 | locked_ref->ref_mod += ref->ref_mod; |
| 2450 | break; |
| 2451 | default: |
| 2452 | WARN_ON(1); |
| 2453 | } |
| 2454 | atomic_dec(&delayed_refs->num_entries); |
| 2455 | |
| 2456 | /* |
| 2457 | * Record the must_insert_reserved flag before we drop the |
| 2458 | * spin lock. |
| 2459 | */ |
| 2460 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2461 | locked_ref->must_insert_reserved = 0; |
| 2462 | |
| 2463 | extent_op = locked_ref->extent_op; |
| 2464 | locked_ref->extent_op = NULL; |
| 2465 | spin_unlock(&locked_ref->lock); |
| 2466 | |
| 2467 | ret = run_one_delayed_ref(trans, ref, extent_op, |
| 2468 | must_insert_reserved); |
| 2469 | |
| 2470 | btrfs_free_delayed_extent_op(extent_op); |
| 2471 | if (ret) { |
| 2472 | unselect_delayed_ref_head(delayed_refs, locked_ref); |
| 2473 | btrfs_put_delayed_ref(ref); |
| 2474 | btrfs_debug(fs_info, "run_one_delayed_ref returned %d", |
| 2475 | ret); |
| 2476 | return ret; |
| 2477 | } |
| 2478 | |
| 2479 | btrfs_put_delayed_ref(ref); |
| 2480 | cond_resched(); |
| 2481 | |
| 2482 | spin_lock(&locked_ref->lock); |
| 2483 | btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref); |
| 2484 | } |
| 2485 | |
| 2486 | return 0; |
| 2487 | } |
| 2488 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2489 | /* |
| 2490 | * Returns 0 on success or if called with an already aborted transaction. |
| 2491 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. |
| 2492 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2493 | static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2494 | unsigned long nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2495 | { |
Nikolay Borisov | 0a1e458 | 2018-03-15 16:00:27 +0200 | [diff] [blame] | 2496 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2497 | struct btrfs_delayed_ref_root *delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2498 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2499 | ktime_t start = ktime_get(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2500 | int ret; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2501 | unsigned long count = 0; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2502 | unsigned long actual_count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2503 | |
| 2504 | delayed_refs = &trans->transaction->delayed_refs; |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2505 | do { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2506 | if (!locked_ref) { |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 2507 | locked_ref = btrfs_obtain_ref_head(trans); |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2508 | if (IS_ERR_OR_NULL(locked_ref)) { |
| 2509 | if (PTR_ERR(locked_ref) == -EAGAIN) { |
| 2510 | continue; |
| 2511 | } else { |
| 2512 | break; |
| 2513 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2514 | } |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2515 | count++; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2516 | } |
Filipe Manana | 2c3cf7d | 2015-10-22 09:47:34 +0100 | [diff] [blame] | 2517 | /* |
| 2518 | * We need to try and merge add/drops of the same ref since we |
| 2519 | * can run into issues with relocate dropping the implicit ref |
| 2520 | * and then it being added back again before the drop can |
| 2521 | * finish. If we merged anything we need to re-loop so we can |
| 2522 | * get a good ref. |
| 2523 | * Or we can get node references of the same type that weren't |
| 2524 | * merged when created due to bumps in the tree mod seq, and |
| 2525 | * we need to merge them to prevent adding an inline extent |
| 2526 | * backref before dropping it (triggering a BUG_ON at |
| 2527 | * insert_inline_extent_backref()). |
| 2528 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2529 | spin_lock(&locked_ref->lock); |
Nikolay Borisov | be97f13 | 2018-04-19 11:06:39 +0300 | [diff] [blame] | 2530 | btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref); |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 2531 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2532 | ret = btrfs_run_delayed_refs_for_head(trans, locked_ref, |
| 2533 | &actual_count); |
| 2534 | if (ret < 0 && ret != -EAGAIN) { |
| 2535 | /* |
| 2536 | * Error, btrfs_run_delayed_refs_for_head already |
| 2537 | * unlocked everything so just bail out |
| 2538 | */ |
| 2539 | return ret; |
| 2540 | } else if (!ret) { |
| 2541 | /* |
| 2542 | * Success, perform the usual cleanup of a processed |
| 2543 | * head |
| 2544 | */ |
Nikolay Borisov | f9871ed | 2018-06-20 15:49:03 +0300 | [diff] [blame] | 2545 | ret = cleanup_ref_head(trans, locked_ref); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2546 | if (ret > 0 ) { |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2547 | /* We dropped our lock, we need to loop. */ |
| 2548 | ret = 0; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2549 | continue; |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2550 | } else if (ret) { |
| 2551 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2552 | } |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 2553 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2554 | |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2555 | /* |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2556 | * Either success case or btrfs_run_delayed_refs_for_head |
| 2557 | * returned -EAGAIN, meaning we need to select another head |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2558 | */ |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2559 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2560 | locked_ref = NULL; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2561 | cond_resched(); |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2562 | } while ((nr != -1 && count < nr) || locked_ref); |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2563 | |
| 2564 | /* |
| 2565 | * We don't want to include ref heads since we can have empty ref heads |
| 2566 | * and those will drastically skew our runtime down since we just do |
| 2567 | * accounting, no actual extent tree updates. |
| 2568 | */ |
| 2569 | if (actual_count > 0) { |
| 2570 | u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 2571 | u64 avg; |
| 2572 | |
| 2573 | /* |
| 2574 | * We weigh the current average higher than our current runtime |
| 2575 | * to avoid large swings in the average. |
| 2576 | */ |
| 2577 | spin_lock(&delayed_refs->lock); |
| 2578 | avg = fs_info->avg_delayed_ref_runtime * 3 + runtime; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 2579 | fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */ |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2580 | spin_unlock(&delayed_refs->lock); |
| 2581 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2582 | return 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2583 | } |
| 2584 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2585 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2586 | /* |
| 2587 | * Normally delayed refs get processed in ascending bytenr order. This |
| 2588 | * correlates in most cases to the order added. To expose dependencies on this |
| 2589 | * order, we start to process the tree in the middle instead of the beginning |
| 2590 | */ |
| 2591 | static u64 find_middle(struct rb_root *root) |
| 2592 | { |
| 2593 | struct rb_node *n = root->rb_node; |
| 2594 | struct btrfs_delayed_ref_node *entry; |
| 2595 | int alt = 1; |
| 2596 | u64 middle; |
| 2597 | u64 first = 0, last = 0; |
| 2598 | |
| 2599 | n = rb_first(root); |
| 2600 | if (n) { |
| 2601 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2602 | first = entry->bytenr; |
| 2603 | } |
| 2604 | n = rb_last(root); |
| 2605 | if (n) { |
| 2606 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2607 | last = entry->bytenr; |
| 2608 | } |
| 2609 | n = root->rb_node; |
| 2610 | |
| 2611 | while (n) { |
| 2612 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2613 | WARN_ON(!entry->in_tree); |
| 2614 | |
| 2615 | middle = entry->bytenr; |
| 2616 | |
| 2617 | if (alt) |
| 2618 | n = n->rb_left; |
| 2619 | else |
| 2620 | n = n->rb_right; |
| 2621 | |
| 2622 | alt = 1 - alt; |
| 2623 | } |
| 2624 | return middle; |
| 2625 | } |
| 2626 | #endif |
| 2627 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2628 | static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2629 | { |
| 2630 | u64 num_bytes; |
| 2631 | |
| 2632 | num_bytes = heads * (sizeof(struct btrfs_extent_item) + |
| 2633 | sizeof(struct btrfs_extent_inline_ref)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2634 | if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2635 | num_bytes += heads * sizeof(struct btrfs_tree_block_info); |
| 2636 | |
| 2637 | /* |
| 2638 | * We don't ever fill up leaves all the way so multiply by 2 just to be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2639 | * closer to what we're really going to want to use. |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2640 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2641 | return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info)); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2642 | } |
| 2643 | |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2644 | /* |
| 2645 | * Takes the number of bytes to be csumm'ed and figures out how many leaves it |
| 2646 | * would require to store the csums for that many bytes. |
| 2647 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2648 | u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes) |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2649 | { |
| 2650 | u64 csum_size; |
| 2651 | u64 num_csums_per_leaf; |
| 2652 | u64 num_csums; |
| 2653 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2654 | csum_size = BTRFS_MAX_ITEM_SIZE(fs_info); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2655 | num_csums_per_leaf = div64_u64(csum_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2656 | (u64)btrfs_super_csum_size(fs_info->super_copy)); |
| 2657 | num_csums = div64_u64(csum_bytes, fs_info->sectorsize); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2658 | num_csums += num_csums_per_leaf - 1; |
| 2659 | num_csums = div64_u64(num_csums, num_csums_per_leaf); |
| 2660 | return num_csums; |
| 2661 | } |
| 2662 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2663 | /* |
| 2664 | * this starts processing the delayed reference count updates and |
| 2665 | * extent insertions we have queued up so far. count can be |
| 2666 | * 0, which means to process everything in the tree at the start |
| 2667 | * of the run (but not newly added entries), or it can be some target |
| 2668 | * number you'd like to process. |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2669 | * |
| 2670 | * Returns 0 on success or if called with an aborted transaction |
| 2671 | * Returns <0 on error and aborts the transaction |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2672 | */ |
| 2673 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Nikolay Borisov | c79a70b | 2018-03-15 17:27:37 +0200 | [diff] [blame] | 2674 | unsigned long count) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2675 | { |
Nikolay Borisov | c79a70b | 2018-03-15 17:27:37 +0200 | [diff] [blame] | 2676 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2677 | struct rb_node *node; |
| 2678 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2679 | struct btrfs_delayed_ref_head *head; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2680 | int ret; |
| 2681 | int run_all = count == (unsigned long)-1; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2682 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2683 | /* We'll clean this up in btrfs_cleanup_transaction */ |
| 2684 | if (trans->aborted) |
| 2685 | return 0; |
| 2686 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2687 | if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags)) |
Chris Mason | 511711a | 2015-12-30 07:52:35 -0800 | [diff] [blame] | 2688 | return 0; |
| 2689 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2690 | delayed_refs = &trans->transaction->delayed_refs; |
Liu Bo | 26455d3 | 2014-12-17 16:14:09 +0800 | [diff] [blame] | 2691 | if (count == 0) |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2692 | count = atomic_read(&delayed_refs->num_entries) * 2; |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2693 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2694 | again: |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2695 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2696 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); |
| 2697 | #endif |
Nikolay Borisov | 0a1e458 | 2018-03-15 16:00:27 +0200 | [diff] [blame] | 2698 | ret = __btrfs_run_delayed_refs(trans, count); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2699 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2700 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2701 | return ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2702 | } |
| 2703 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2704 | if (run_all) { |
Josef Bacik | 119e80d | 2018-11-21 14:05:42 -0500 | [diff] [blame] | 2705 | btrfs_create_pending_block_groups(trans); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2706 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2707 | spin_lock(&delayed_refs->lock); |
Liu Bo | 5c9d028 | 2018-08-23 03:51:49 +0800 | [diff] [blame] | 2708 | node = rb_first_cached(&delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2709 | if (!node) { |
| 2710 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2711 | goto out; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2712 | } |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2713 | head = rb_entry(node, struct btrfs_delayed_ref_head, |
| 2714 | href_node); |
| 2715 | refcount_inc(&head->refs); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2716 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2717 | |
| 2718 | /* Mutex was contended, block until it's released and retry. */ |
| 2719 | mutex_lock(&head->mutex); |
| 2720 | mutex_unlock(&head->mutex); |
| 2721 | |
| 2722 | btrfs_put_delayed_ref_head(head); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2723 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2724 | goto again; |
| 2725 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2726 | out: |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2727 | return 0; |
| 2728 | } |
| 2729 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2730 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2731 | u64 bytenr, u64 num_bytes, u64 flags, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2732 | int level, int is_data) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2733 | { |
| 2734 | struct btrfs_delayed_extent_op *extent_op; |
| 2735 | int ret; |
| 2736 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2737 | extent_op = btrfs_alloc_delayed_extent_op(); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2738 | if (!extent_op) |
| 2739 | return -ENOMEM; |
| 2740 | |
| 2741 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 2742 | extent_op->update_flags = true; |
| 2743 | extent_op->update_key = false; |
| 2744 | extent_op->is_data = is_data ? true : false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2745 | extent_op->level = level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2746 | |
David Sterba | c6e340b | 2019-03-20 11:42:34 +0100 | [diff] [blame] | 2747 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2748 | if (ret) |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2749 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2750 | return ret; |
| 2751 | } |
| 2752 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2753 | static noinline int check_delayed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2754 | struct btrfs_path *path, |
| 2755 | u64 objectid, u64 offset, u64 bytenr) |
| 2756 | { |
| 2757 | struct btrfs_delayed_ref_head *head; |
| 2758 | struct btrfs_delayed_ref_node *ref; |
| 2759 | struct btrfs_delayed_data_ref *data_ref; |
| 2760 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2761 | struct btrfs_transaction *cur_trans; |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2762 | struct rb_node *node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2763 | int ret = 0; |
| 2764 | |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2765 | spin_lock(&root->fs_info->trans_lock); |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2766 | cur_trans = root->fs_info->running_transaction; |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2767 | if (cur_trans) |
| 2768 | refcount_inc(&cur_trans->use_count); |
| 2769 | spin_unlock(&root->fs_info->trans_lock); |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2770 | if (!cur_trans) |
| 2771 | return 0; |
| 2772 | |
| 2773 | delayed_refs = &cur_trans->delayed_refs; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2774 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 2775 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2776 | if (!head) { |
| 2777 | spin_unlock(&delayed_refs->lock); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2778 | btrfs_put_transaction(cur_trans); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2779 | return 0; |
| 2780 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2781 | |
| 2782 | if (!mutex_trylock(&head->mutex)) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2783 | refcount_inc(&head->refs); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2784 | spin_unlock(&delayed_refs->lock); |
| 2785 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2786 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2787 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2788 | /* |
| 2789 | * Mutex was contended, block until it's released and let |
| 2790 | * caller try again |
| 2791 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2792 | mutex_lock(&head->mutex); |
| 2793 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2794 | btrfs_put_delayed_ref_head(head); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2795 | btrfs_put_transaction(cur_trans); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2796 | return -EAGAIN; |
| 2797 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2798 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2799 | |
| 2800 | spin_lock(&head->lock); |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2801 | /* |
| 2802 | * XXX: We should replace this with a proper search function in the |
| 2803 | * future. |
| 2804 | */ |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 2805 | for (node = rb_first_cached(&head->ref_tree); node; |
| 2806 | node = rb_next(node)) { |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2807 | ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2808 | /* If it's a shared ref we know a cross reference exists */ |
| 2809 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) { |
| 2810 | ret = 1; |
| 2811 | break; |
| 2812 | } |
| 2813 | |
| 2814 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2815 | |
| 2816 | /* |
| 2817 | * If our ref doesn't match the one we're currently looking at |
| 2818 | * then we have a cross reference. |
| 2819 | */ |
| 2820 | if (data_ref->root != root->root_key.objectid || |
| 2821 | data_ref->objectid != objectid || |
| 2822 | data_ref->offset != offset) { |
| 2823 | ret = 1; |
| 2824 | break; |
| 2825 | } |
| 2826 | } |
| 2827 | spin_unlock(&head->lock); |
| 2828 | mutex_unlock(&head->mutex); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2829 | btrfs_put_transaction(cur_trans); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2830 | return ret; |
| 2831 | } |
| 2832 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2833 | static noinline int check_committed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2834 | struct btrfs_path *path, |
| 2835 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2836 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2837 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2838 | struct btrfs_root *extent_root = fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2839 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2840 | struct btrfs_extent_data_ref *ref; |
| 2841 | struct btrfs_extent_inline_ref *iref; |
| 2842 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2843 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2844 | u32 item_size; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2845 | int type; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2846 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2847 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2848 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2849 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2850 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2851 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2852 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2853 | if (ret < 0) |
| 2854 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2855 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2856 | |
| 2857 | ret = -ENOENT; |
| 2858 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2859 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2860 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2861 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2862 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2863 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2864 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2865 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2866 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2867 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2868 | ret = 1; |
| 2869 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2870 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2871 | |
| 2872 | if (item_size != sizeof(*ei) + |
| 2873 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2874 | goto out; |
| 2875 | |
| 2876 | if (btrfs_extent_generation(leaf, ei) <= |
| 2877 | btrfs_root_last_snapshot(&root->root_item)) |
| 2878 | goto out; |
| 2879 | |
| 2880 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2881 | |
| 2882 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); |
| 2883 | if (type != BTRFS_EXTENT_DATA_REF_KEY) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2884 | goto out; |
| 2885 | |
| 2886 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2887 | if (btrfs_extent_refs(leaf, ei) != |
| 2888 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2889 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2890 | root->root_key.objectid || |
| 2891 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2892 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2893 | goto out; |
| 2894 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2895 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2896 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2897 | return ret; |
| 2898 | } |
| 2899 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2900 | int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset, |
| 2901 | u64 bytenr) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2902 | { |
| 2903 | struct btrfs_path *path; |
| 2904 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2905 | |
| 2906 | path = btrfs_alloc_path(); |
| 2907 | if (!path) |
Su Yue | 9132c4f | 2018-05-30 14:49:10 +0800 | [diff] [blame] | 2908 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2909 | |
| 2910 | do { |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2911 | ret = check_committed_ref(root, path, objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2912 | offset, bytenr); |
| 2913 | if (ret && ret != -ENOENT) |
| 2914 | goto out; |
| 2915 | |
Misono Tomohiro | 380fd06 | 2018-08-30 10:59:16 +0900 | [diff] [blame] | 2916 | ret = check_delayed_ref(root, path, objectid, offset, bytenr); |
| 2917 | } while (ret == -EAGAIN); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2918 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2919 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2920 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2921 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2922 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2923 | return ret; |
| 2924 | } |
| 2925 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2926 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2927 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2928 | struct extent_buffer *buf, |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2929 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2930 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2931 | struct btrfs_fs_info *fs_info = root->fs_info; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2932 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2933 | u64 num_bytes; |
| 2934 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2935 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2936 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2937 | struct btrfs_key key; |
| 2938 | struct btrfs_file_extent_item *fi; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2939 | struct btrfs_ref generic_ref = { 0 }; |
| 2940 | bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2941 | int i; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2942 | int action; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2943 | int level; |
| 2944 | int ret = 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 2945 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2946 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 2947 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 2948 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2949 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2950 | nritems = btrfs_header_nritems(buf); |
| 2951 | level = btrfs_header_level(buf); |
| 2952 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 2953 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2954 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2955 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2956 | if (full_backref) |
| 2957 | parent = buf->start; |
| 2958 | else |
| 2959 | parent = 0; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2960 | if (inc) |
| 2961 | action = BTRFS_ADD_DELAYED_REF; |
| 2962 | else |
| 2963 | action = BTRFS_DROP_DELAYED_REF; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2964 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2965 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2966 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2967 | btrfs_item_key_to_cpu(buf, &key, i); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 2968 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2969 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2970 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2971 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2972 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2973 | BTRFS_FILE_EXTENT_INLINE) |
| 2974 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2975 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2976 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2977 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2978 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2979 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2980 | key.offset -= btrfs_file_extent_offset(buf, fi); |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2981 | btrfs_init_generic_ref(&generic_ref, action, bytenr, |
| 2982 | num_bytes, parent); |
| 2983 | generic_ref.real_root = root->root_key.objectid; |
| 2984 | btrfs_init_data_ref(&generic_ref, ref_root, key.objectid, |
| 2985 | key.offset); |
| 2986 | generic_ref.skip_qgroup = for_reloc; |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2987 | if (inc) |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2988 | ret = btrfs_inc_extent_ref(trans, &generic_ref); |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2989 | else |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 2990 | ret = btrfs_free_extent(trans, &generic_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2991 | if (ret) |
| 2992 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2993 | } else { |
| 2994 | bytenr = btrfs_node_blockptr(buf, i); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2995 | num_bytes = fs_info->nodesize; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2996 | btrfs_init_generic_ref(&generic_ref, action, bytenr, |
| 2997 | num_bytes, parent); |
| 2998 | generic_ref.real_root = root->root_key.objectid; |
| 2999 | btrfs_init_tree_ref(&generic_ref, level - 1, ref_root); |
| 3000 | generic_ref.skip_qgroup = for_reloc; |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 3001 | if (inc) |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 3002 | ret = btrfs_inc_extent_ref(trans, &generic_ref); |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 3003 | else |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3004 | ret = btrfs_free_extent(trans, &generic_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3005 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3006 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3007 | } |
| 3008 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3009 | return 0; |
| 3010 | fail: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3011 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3012 | } |
| 3013 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3014 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3015 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3016 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3017 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3018 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3019 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3020 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3021 | struct extent_buffer *buf, int full_backref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3022 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3023 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3024 | } |
| 3025 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3026 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3027 | struct btrfs_path *path, |
| 3028 | struct btrfs_block_group_cache *cache) |
| 3029 | { |
David Sterba | 39db232 | 2019-03-20 11:57:46 +0100 | [diff] [blame] | 3030 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3031 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3032 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3033 | unsigned long bi; |
| 3034 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3035 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3036 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3037 | if (ret) { |
| 3038 | if (ret > 0) |
| 3039 | ret = -ENOENT; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3040 | goto fail; |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3041 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3042 | |
| 3043 | leaf = path->nodes[0]; |
| 3044 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3045 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 3046 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3047 | fail: |
Filipe Manana | 24b89d0 | 2015-04-25 18:31:05 +0100 | [diff] [blame] | 3048 | btrfs_release_path(path); |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3049 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3050 | |
| 3051 | } |
| 3052 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3053 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 3054 | struct btrfs_trans_handle *trans, |
| 3055 | struct btrfs_path *path) |
| 3056 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3057 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
| 3058 | struct btrfs_root *root = fs_info->tree_root; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3059 | struct inode *inode = NULL; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3060 | struct extent_changeset *data_reserved = NULL; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3061 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3062 | int dcs = BTRFS_DC_ERROR; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 3063 | u64 num_pages = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3064 | int retries = 0; |
| 3065 | int ret = 0; |
| 3066 | |
| 3067 | /* |
| 3068 | * If this block group is smaller than 100 megs don't bother caching the |
| 3069 | * block group. |
| 3070 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 3071 | if (block_group->key.offset < (100 * SZ_1M)) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3072 | spin_lock(&block_group->lock); |
| 3073 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 3074 | spin_unlock(&block_group->lock); |
| 3075 | return 0; |
| 3076 | } |
| 3077 | |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3078 | if (trans->aborted) |
| 3079 | return 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3080 | again: |
David Sterba | 7949f33 | 2019-03-20 13:40:19 +0100 | [diff] [blame] | 3081 | inode = lookup_free_space_inode(block_group, path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3082 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 3083 | ret = PTR_ERR(inode); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3084 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3085 | goto out; |
| 3086 | } |
| 3087 | |
| 3088 | if (IS_ERR(inode)) { |
| 3089 | BUG_ON(retries); |
| 3090 | retries++; |
| 3091 | |
| 3092 | if (block_group->ro) |
| 3093 | goto out_free; |
| 3094 | |
David Sterba | 4ca75f1 | 2019-03-20 13:42:57 +0100 | [diff] [blame] | 3095 | ret = create_free_space_inode(trans, block_group, path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3096 | if (ret) |
| 3097 | goto out_free; |
| 3098 | goto again; |
| 3099 | } |
| 3100 | |
| 3101 | /* |
| 3102 | * We want to set the generation to 0, that way if anything goes wrong |
| 3103 | * from here on out we know not to trust this cache when we load up next |
| 3104 | * time. |
| 3105 | */ |
| 3106 | BTRFS_I(inode)->generation = 0; |
| 3107 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3108 | if (ret) { |
| 3109 | /* |
| 3110 | * So theoretically we could recover from this, simply set the |
| 3111 | * super cache generation to 0 so we know to invalidate the |
| 3112 | * cache, but then we'd have to keep track of the block groups |
| 3113 | * that fail this way so we know we _have_ to reset this cache |
| 3114 | * before the next commit or risk reading stale cache. So to |
| 3115 | * limit our exposure to horrible edge cases lets just abort the |
| 3116 | * transaction, this only happens in really bad situations |
| 3117 | * anyway. |
| 3118 | */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3119 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3120 | goto out_put; |
| 3121 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3122 | WARN_ON(ret); |
| 3123 | |
Josef Bacik | 8e138e0 | 2017-11-17 14:50:46 -0500 | [diff] [blame] | 3124 | /* We've already setup this transaction, go ahead and exit */ |
| 3125 | if (block_group->cache_generation == trans->transid && |
| 3126 | i_size_read(inode)) { |
| 3127 | dcs = BTRFS_DC_SETUP; |
| 3128 | goto out_put; |
| 3129 | } |
| 3130 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3131 | if (i_size_read(inode) > 0) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3132 | ret = btrfs_check_trunc_cache_free_space(fs_info, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3133 | &fs_info->global_block_rsv); |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 3134 | if (ret) |
| 3135 | goto out_put; |
| 3136 | |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 3137 | ret = btrfs_truncate_free_space_cache(trans, NULL, inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3138 | if (ret) |
| 3139 | goto out_put; |
| 3140 | } |
| 3141 | |
| 3142 | spin_lock(&block_group->lock); |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 3143 | if (block_group->cached != BTRFS_CACHE_FINISHED || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3144 | !btrfs_test_opt(fs_info, SPACE_CACHE)) { |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 3145 | /* |
| 3146 | * don't bother trying to write stuff out _if_ |
| 3147 | * a) we're not cached, |
Liu Bo | 1a79c1f | 2017-03-06 13:49:02 -0800 | [diff] [blame] | 3148 | * b) we're with nospace_cache mount option, |
| 3149 | * c) we're with v2 space_cache (FREE_SPACE_TREE). |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 3150 | */ |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3151 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3152 | spin_unlock(&block_group->lock); |
| 3153 | goto out_put; |
| 3154 | } |
| 3155 | spin_unlock(&block_group->lock); |
| 3156 | |
Josef Bacik | 6fc823b | 2012-08-06 13:46:38 -0600 | [diff] [blame] | 3157 | /* |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3158 | * We hit an ENOSPC when setting up the cache in this transaction, just |
| 3159 | * skip doing the setup, we've already cleared the cache so we're safe. |
| 3160 | */ |
| 3161 | if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { |
| 3162 | ret = -ENOSPC; |
| 3163 | goto out_put; |
| 3164 | } |
| 3165 | |
| 3166 | /* |
Josef Bacik | 6fc823b | 2012-08-06 13:46:38 -0600 | [diff] [blame] | 3167 | * Try to preallocate enough space based on how big the block group is. |
| 3168 | * Keep in mind this has to include any pinned space which could end up |
| 3169 | * taking up quite a bit since it's not folded into the other space |
| 3170 | * cache. |
| 3171 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 3172 | num_pages = div_u64(block_group->key.offset, SZ_256M); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3173 | if (!num_pages) |
| 3174 | num_pages = 1; |
| 3175 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3176 | num_pages *= 16; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3177 | num_pages *= PAGE_SIZE; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3178 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3179 | ret = btrfs_check_data_free_space(inode, &data_reserved, 0, num_pages); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3180 | if (ret) |
| 3181 | goto out_put; |
| 3182 | |
| 3183 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 3184 | num_pages, num_pages, |
| 3185 | &alloc_hint); |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3186 | /* |
| 3187 | * Our cache requires contiguous chunks so that we don't modify a bunch |
| 3188 | * of metadata or split extents when writing the cache out, which means |
| 3189 | * we can enospc if we are heavily fragmented in addition to just normal |
| 3190 | * out of space conditions. So if we hit this just skip setting up any |
| 3191 | * other block groups for this transaction, maybe we'll unpin enough |
| 3192 | * space the next time around. |
| 3193 | */ |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3194 | if (!ret) |
| 3195 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3196 | else if (ret == -ENOSPC) |
| 3197 | set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 3198 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3199 | out_put: |
| 3200 | iput(inode); |
| 3201 | out_free: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3202 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3203 | out: |
| 3204 | spin_lock(&block_group->lock); |
Josef Bacik | e65cbb9 | 2011-12-13 16:04:54 -0500 | [diff] [blame] | 3205 | if (!ret && dcs == BTRFS_DC_SETUP) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3206 | block_group->cache_generation = trans->transid; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3207 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3208 | spin_unlock(&block_group->lock); |
| 3209 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3210 | extent_changeset_free(data_reserved); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3211 | return ret; |
| 3212 | } |
| 3213 | |
David Sterba | bbebb3e | 2019-03-20 12:02:55 +0100 | [diff] [blame] | 3214 | int btrfs_setup_space_cache(struct btrfs_trans_handle *trans) |
Josef Bacik | dcdf7f6 | 2015-03-02 16:37:31 -0500 | [diff] [blame] | 3215 | { |
David Sterba | bbebb3e | 2019-03-20 12:02:55 +0100 | [diff] [blame] | 3216 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | dcdf7f6 | 2015-03-02 16:37:31 -0500 | [diff] [blame] | 3217 | struct btrfs_block_group_cache *cache, *tmp; |
| 3218 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3219 | struct btrfs_path *path; |
| 3220 | |
| 3221 | if (list_empty(&cur_trans->dirty_bgs) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3222 | !btrfs_test_opt(fs_info, SPACE_CACHE)) |
Josef Bacik | dcdf7f6 | 2015-03-02 16:37:31 -0500 | [diff] [blame] | 3223 | return 0; |
| 3224 | |
| 3225 | path = btrfs_alloc_path(); |
| 3226 | if (!path) |
| 3227 | return -ENOMEM; |
| 3228 | |
| 3229 | /* Could add new block groups, use _safe just in case */ |
| 3230 | list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, |
| 3231 | dirty_list) { |
| 3232 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 3233 | cache_save_setup(cache, trans, path); |
| 3234 | } |
| 3235 | |
| 3236 | btrfs_free_path(path); |
| 3237 | return 0; |
| 3238 | } |
| 3239 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3240 | /* |
| 3241 | * transaction commit does final block group cache writeback during a |
| 3242 | * critical section where nothing is allowed to change the FS. This is |
| 3243 | * required in order for the cache to actually match the block group, |
| 3244 | * but can introduce a lot of latency into the commit. |
| 3245 | * |
| 3246 | * So, btrfs_start_dirty_block_groups is here to kick off block group |
| 3247 | * cache IO. There's a chance we'll have to redo some of it if the |
| 3248 | * block group changes again during the commit, but it greatly reduces |
| 3249 | * the commit latency by getting rid of the easy block groups while |
| 3250 | * we're still allowing others to join the commit. |
| 3251 | */ |
Nikolay Borisov | 2121705 | 2018-02-07 17:55:41 +0200 | [diff] [blame] | 3252 | int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans) |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3253 | { |
Nikolay Borisov | 2121705 | 2018-02-07 17:55:41 +0200 | [diff] [blame] | 3254 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3255 | struct btrfs_block_group_cache *cache; |
| 3256 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3257 | int ret = 0; |
| 3258 | int should_put; |
| 3259 | struct btrfs_path *path = NULL; |
| 3260 | LIST_HEAD(dirty); |
| 3261 | struct list_head *io = &cur_trans->io_bgs; |
| 3262 | int num_started = 0; |
| 3263 | int loops = 0; |
| 3264 | |
| 3265 | spin_lock(&cur_trans->dirty_bgs_lock); |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3266 | if (list_empty(&cur_trans->dirty_bgs)) { |
| 3267 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3268 | return 0; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3269 | } |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3270 | list_splice_init(&cur_trans->dirty_bgs, &dirty); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3271 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3272 | |
| 3273 | again: |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3274 | /* |
| 3275 | * make sure all the block groups on our dirty list actually |
| 3276 | * exist |
| 3277 | */ |
Nikolay Borisov | 6c686b3 | 2018-02-07 17:55:40 +0200 | [diff] [blame] | 3278 | btrfs_create_pending_block_groups(trans); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3279 | |
| 3280 | if (!path) { |
| 3281 | path = btrfs_alloc_path(); |
| 3282 | if (!path) |
| 3283 | return -ENOMEM; |
| 3284 | } |
| 3285 | |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3286 | /* |
| 3287 | * cache_write_mutex is here only to save us from balance or automatic |
| 3288 | * removal of empty block groups deleting this block group while we are |
| 3289 | * writing out the cache |
| 3290 | */ |
| 3291 | mutex_lock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3292 | while (!list_empty(&dirty)) { |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 3293 | bool drop_reserve = true; |
| 3294 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3295 | cache = list_first_entry(&dirty, |
| 3296 | struct btrfs_block_group_cache, |
| 3297 | dirty_list); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3298 | /* |
| 3299 | * this can happen if something re-dirties a block |
| 3300 | * group that is already under IO. Just wait for it to |
| 3301 | * finish and then do it all again |
| 3302 | */ |
| 3303 | if (!list_empty(&cache->io_list)) { |
| 3304 | list_del_init(&cache->io_list); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 3305 | btrfs_wait_cache_io(trans, cache, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3306 | btrfs_put_block_group(cache); |
| 3307 | } |
| 3308 | |
| 3309 | |
| 3310 | /* |
| 3311 | * btrfs_wait_cache_io uses the cache->dirty_list to decide |
| 3312 | * if it should update the cache_state. Don't delete |
| 3313 | * until after we wait. |
| 3314 | * |
| 3315 | * Since we're not running in the commit critical section |
| 3316 | * we need the dirty_bgs_lock to protect from update_block_group |
| 3317 | */ |
| 3318 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3319 | list_del_init(&cache->dirty_list); |
| 3320 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3321 | |
| 3322 | should_put = 1; |
| 3323 | |
| 3324 | cache_save_setup(cache, trans, path); |
| 3325 | |
| 3326 | if (cache->disk_cache_state == BTRFS_DC_SETUP) { |
| 3327 | cache->io_ctl.inode = NULL; |
David Sterba | fe04153 | 2019-03-20 13:51:56 +0100 | [diff] [blame] | 3328 | ret = btrfs_write_out_cache(trans, cache, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3329 | if (ret == 0 && cache->io_ctl.inode) { |
| 3330 | num_started++; |
| 3331 | should_put = 0; |
| 3332 | |
| 3333 | /* |
Nikolay Borisov | 45ae2c1 | 2018-02-08 18:25:18 +0200 | [diff] [blame] | 3334 | * The cache_write_mutex is protecting the |
| 3335 | * io_list, also refer to the definition of |
| 3336 | * btrfs_transaction::io_bgs for more details |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3337 | */ |
| 3338 | list_add_tail(&cache->io_list, io); |
| 3339 | } else { |
| 3340 | /* |
| 3341 | * if we failed to write the cache, the |
| 3342 | * generation will be bad and life goes on |
| 3343 | */ |
| 3344 | ret = 0; |
| 3345 | } |
| 3346 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3347 | if (!ret) { |
David Sterba | 39db232 | 2019-03-20 11:57:46 +0100 | [diff] [blame] | 3348 | ret = write_one_cache_group(trans, path, cache); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3349 | /* |
| 3350 | * Our block group might still be attached to the list |
| 3351 | * of new block groups in the transaction handle of some |
| 3352 | * other task (struct btrfs_trans_handle->new_bgs). This |
| 3353 | * means its block group item isn't yet in the extent |
| 3354 | * tree. If this happens ignore the error, as we will |
| 3355 | * try again later in the critical section of the |
| 3356 | * transaction commit. |
| 3357 | */ |
| 3358 | if (ret == -ENOENT) { |
| 3359 | ret = 0; |
| 3360 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3361 | if (list_empty(&cache->dirty_list)) { |
| 3362 | list_add_tail(&cache->dirty_list, |
| 3363 | &cur_trans->dirty_bgs); |
| 3364 | btrfs_get_block_group(cache); |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 3365 | drop_reserve = false; |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3366 | } |
| 3367 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3368 | } else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3369 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3370 | } |
| 3371 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3372 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 3373 | /* if it's not on the io list, we need to put the block group */ |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3374 | if (should_put) |
| 3375 | btrfs_put_block_group(cache); |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 3376 | if (drop_reserve) |
| 3377 | btrfs_delayed_refs_rsv_release(fs_info, 1); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3378 | |
| 3379 | if (ret) |
| 3380 | break; |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3381 | |
| 3382 | /* |
| 3383 | * Avoid blocking other tasks for too long. It might even save |
| 3384 | * us from writing caches for block groups that are going to be |
| 3385 | * removed. |
| 3386 | */ |
| 3387 | mutex_unlock(&trans->transaction->cache_write_mutex); |
| 3388 | mutex_lock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3389 | } |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3390 | mutex_unlock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3391 | |
| 3392 | /* |
| 3393 | * go through delayed refs for all the stuff we've just kicked off |
| 3394 | * and then loop back (just once) |
| 3395 | */ |
Nikolay Borisov | c79a70b | 2018-03-15 17:27:37 +0200 | [diff] [blame] | 3396 | ret = btrfs_run_delayed_refs(trans, 0); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3397 | if (!ret && loops == 0) { |
| 3398 | loops++; |
| 3399 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3400 | list_splice_init(&cur_trans->dirty_bgs, &dirty); |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3401 | /* |
| 3402 | * dirty_bgs_lock protects us from concurrent block group |
| 3403 | * deletes too (not just cache_write_mutex). |
| 3404 | */ |
| 3405 | if (!list_empty(&dirty)) { |
| 3406 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3407 | goto again; |
| 3408 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3409 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Liu Bo | c79a175 | 2016-07-20 17:44:12 -0700 | [diff] [blame] | 3410 | } else if (ret < 0) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3411 | btrfs_cleanup_dirty_bgs(cur_trans, fs_info); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | btrfs_free_path(path); |
| 3415 | return ret; |
| 3416 | } |
| 3417 | |
David Sterba | 5742d15 | 2019-03-20 12:04:08 +0100 | [diff] [blame] | 3418 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3419 | { |
David Sterba | 5742d15 | 2019-03-20 12:04:08 +0100 | [diff] [blame] | 3420 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3421 | struct btrfs_block_group_cache *cache; |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3422 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3423 | int ret = 0; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3424 | int should_put; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3425 | struct btrfs_path *path; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3426 | struct list_head *io = &cur_trans->io_bgs; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3427 | int num_started = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3428 | |
| 3429 | path = btrfs_alloc_path(); |
| 3430 | if (!path) |
| 3431 | return -ENOMEM; |
| 3432 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3433 | /* |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3434 | * Even though we are in the critical section of the transaction commit, |
| 3435 | * we can still have concurrent tasks adding elements to this |
| 3436 | * transaction's list of dirty block groups. These tasks correspond to |
| 3437 | * endio free space workers started when writeback finishes for a |
| 3438 | * space cache, which run inode.c:btrfs_finish_ordered_io(), and can |
| 3439 | * allocate new block groups as a result of COWing nodes of the root |
| 3440 | * tree when updating the free space inode. The writeback for the space |
| 3441 | * caches is triggered by an earlier call to |
| 3442 | * btrfs_start_dirty_block_groups() and iterations of the following |
| 3443 | * loop. |
| 3444 | * Also we want to do the cache_save_setup first and then run the |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3445 | * delayed refs to make sure we have the best chance at doing this all |
| 3446 | * in one shot. |
| 3447 | */ |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3448 | spin_lock(&cur_trans->dirty_bgs_lock); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3449 | while (!list_empty(&cur_trans->dirty_bgs)) { |
| 3450 | cache = list_first_entry(&cur_trans->dirty_bgs, |
| 3451 | struct btrfs_block_group_cache, |
| 3452 | dirty_list); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3453 | |
| 3454 | /* |
| 3455 | * this can happen if cache_save_setup re-dirties a block |
| 3456 | * group that is already under IO. Just wait for it to |
| 3457 | * finish and then do it all again |
| 3458 | */ |
| 3459 | if (!list_empty(&cache->io_list)) { |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3460 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3461 | list_del_init(&cache->io_list); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 3462 | btrfs_wait_cache_io(trans, cache, path); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3463 | btrfs_put_block_group(cache); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3464 | spin_lock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3465 | } |
| 3466 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3467 | /* |
| 3468 | * don't remove from the dirty list until after we've waited |
| 3469 | * on any pending IO |
| 3470 | */ |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3471 | list_del_init(&cache->dirty_list); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3472 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3473 | should_put = 1; |
| 3474 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3475 | cache_save_setup(cache, trans, path); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3476 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3477 | if (!ret) |
Nikolay Borisov | c79a70b | 2018-03-15 17:27:37 +0200 | [diff] [blame] | 3478 | ret = btrfs_run_delayed_refs(trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3479 | (unsigned long) -1); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3480 | |
| 3481 | if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { |
| 3482 | cache->io_ctl.inode = NULL; |
David Sterba | fe04153 | 2019-03-20 13:51:56 +0100 | [diff] [blame] | 3483 | ret = btrfs_write_out_cache(trans, cache, path); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3484 | if (ret == 0 && cache->io_ctl.inode) { |
| 3485 | num_started++; |
| 3486 | should_put = 0; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3487 | list_add_tail(&cache->io_list, io); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3488 | } else { |
| 3489 | /* |
| 3490 | * if we failed to write the cache, the |
| 3491 | * generation will be bad and life goes on |
| 3492 | */ |
| 3493 | ret = 0; |
| 3494 | } |
| 3495 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3496 | if (!ret) { |
David Sterba | 39db232 | 2019-03-20 11:57:46 +0100 | [diff] [blame] | 3497 | ret = write_one_cache_group(trans, path, cache); |
Filipe Manana | 2bc0bb5 | 2015-12-30 02:42:30 +0000 | [diff] [blame] | 3498 | /* |
| 3499 | * One of the free space endio workers might have |
| 3500 | * created a new block group while updating a free space |
| 3501 | * cache's inode (at inode.c:btrfs_finish_ordered_io()) |
| 3502 | * and hasn't released its transaction handle yet, in |
| 3503 | * which case the new block group is still attached to |
| 3504 | * its transaction handle and its creation has not |
| 3505 | * finished yet (no block group item in the extent tree |
| 3506 | * yet, etc). If this is the case, wait for all free |
| 3507 | * space endio workers to finish and retry. This is a |
| 3508 | * a very rare case so no need for a more efficient and |
| 3509 | * complex approach. |
| 3510 | */ |
| 3511 | if (ret == -ENOENT) { |
| 3512 | wait_event(cur_trans->writer_wait, |
| 3513 | atomic_read(&cur_trans->num_writers) == 1); |
David Sterba | 39db232 | 2019-03-20 11:57:46 +0100 | [diff] [blame] | 3514 | ret = write_one_cache_group(trans, path, cache); |
Filipe Manana | 2bc0bb5 | 2015-12-30 02:42:30 +0000 | [diff] [blame] | 3515 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3516 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3517 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3518 | } |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3519 | |
| 3520 | /* if its not on the io list, we need to put the block group */ |
| 3521 | if (should_put) |
| 3522 | btrfs_put_block_group(cache); |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 3523 | btrfs_delayed_refs_rsv_release(fs_info, 1); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3524 | spin_lock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3525 | } |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3526 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3527 | |
Nikolay Borisov | 45ae2c1 | 2018-02-08 18:25:18 +0200 | [diff] [blame] | 3528 | /* |
| 3529 | * Refer to the definition of io_bgs member for details why it's safe |
| 3530 | * to use it without any locking |
| 3531 | */ |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3532 | while (!list_empty(io)) { |
| 3533 | cache = list_first_entry(io, struct btrfs_block_group_cache, |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3534 | io_list); |
| 3535 | list_del_init(&cache->io_list); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 3536 | btrfs_wait_cache_io(trans, cache, path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3537 | btrfs_put_block_group(cache); |
| 3538 | } |
| 3539 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3540 | btrfs_free_path(path); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3541 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3542 | } |
| 3543 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3544 | int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3545 | { |
| 3546 | struct btrfs_block_group_cache *block_group; |
| 3547 | int readonly = 0; |
| 3548 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3549 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3550 | if (!block_group || block_group->ro) |
| 3551 | readonly = 1; |
| 3552 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3553 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3554 | return readonly; |
| 3555 | } |
| 3556 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3557 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 3558 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3559 | u64 extra_flags = chunk_to_extended(flags) & |
| 3560 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3561 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3562 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3563 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3564 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 3565 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3566 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 3567 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3568 | fs_info->avail_system_alloc_bits |= extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3569 | write_sequnlock(&fs_info->profiles_lock); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3570 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3571 | |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3572 | /* |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3573 | * returns target flags in extended format or 0 if restripe for this |
| 3574 | * chunk_type is not in progress |
Ilya Dryomov | c6664b4 | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 3575 | * |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 3576 | * should be called with balance_lock held |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3577 | */ |
| 3578 | static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags) |
| 3579 | { |
| 3580 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3581 | u64 target = 0; |
| 3582 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3583 | if (!bctl) |
| 3584 | return 0; |
| 3585 | |
| 3586 | if (flags & BTRFS_BLOCK_GROUP_DATA && |
| 3587 | bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3588 | target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; |
| 3589 | } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && |
| 3590 | bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3591 | target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; |
| 3592 | } else if (flags & BTRFS_BLOCK_GROUP_METADATA && |
| 3593 | bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3594 | target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; |
| 3595 | } |
| 3596 | |
| 3597 | return target; |
| 3598 | } |
| 3599 | |
| 3600 | /* |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3601 | * @flags: available profiles in extended format (see ctree.h) |
| 3602 | * |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3603 | * Returns reduced profile in chunk format. If profile changing is in |
| 3604 | * progress (either running or paused) picks the target profile (if it's |
| 3605 | * already available), otherwise falls back to plain reducing. |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3606 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3607 | static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3608 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3609 | u64 num_devices = fs_info->fs_devices->rw_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3610 | u64 target; |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 3611 | u64 raid_type; |
| 3612 | u64 allowed = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3613 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3614 | /* |
| 3615 | * see if restripe for this chunk_type is in progress, if so |
| 3616 | * try to reduce to the target profile |
| 3617 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3618 | spin_lock(&fs_info->balance_lock); |
| 3619 | target = get_restripe_target(fs_info, flags); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3620 | if (target) { |
| 3621 | /* pick target profile only if it's already available */ |
| 3622 | if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3623 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3624 | return extended_to_chunk(target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3625 | } |
| 3626 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3627 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3628 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3629 | /* First, mask out the RAID levels which aren't possible */ |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 3630 | for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { |
| 3631 | if (num_devices >= btrfs_raid_array[raid_type].devs_min) |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 3632 | allowed |= btrfs_raid_array[raid_type].bg_flag; |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 3633 | } |
| 3634 | allowed &= flags; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3635 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 3636 | if (allowed & BTRFS_BLOCK_GROUP_RAID6) |
| 3637 | allowed = BTRFS_BLOCK_GROUP_RAID6; |
| 3638 | else if (allowed & BTRFS_BLOCK_GROUP_RAID5) |
| 3639 | allowed = BTRFS_BLOCK_GROUP_RAID5; |
| 3640 | else if (allowed & BTRFS_BLOCK_GROUP_RAID10) |
| 3641 | allowed = BTRFS_BLOCK_GROUP_RAID10; |
| 3642 | else if (allowed & BTRFS_BLOCK_GROUP_RAID1) |
| 3643 | allowed = BTRFS_BLOCK_GROUP_RAID1; |
| 3644 | else if (allowed & BTRFS_BLOCK_GROUP_RAID0) |
| 3645 | allowed = BTRFS_BLOCK_GROUP_RAID0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3646 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 3647 | flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3648 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 3649 | return extended_to_chunk(flags | allowed); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3650 | } |
| 3651 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3652 | static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3653 | { |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3654 | unsigned seq; |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 3655 | u64 flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3656 | |
| 3657 | do { |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 3658 | flags = orig_flags; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3659 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3660 | |
| 3661 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3662 | flags |= fs_info->avail_data_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3663 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3664 | flags |= fs_info->avail_system_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3665 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3666 | flags |= fs_info->avail_metadata_alloc_bits; |
| 3667 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | 6fef8df | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3668 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3669 | return btrfs_reduce_alloc_profile(fs_info, flags); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3670 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3671 | |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 3672 | static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3673 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3674 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3675 | u64 flags; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3676 | u64 ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3677 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3678 | if (data) |
| 3679 | flags = BTRFS_BLOCK_GROUP_DATA; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3680 | else if (root == fs_info->chunk_root) |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3681 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 3682 | else |
| 3683 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 3684 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3685 | ret = get_alloc_profile(fs_info, flags); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3686 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3687 | } |
| 3688 | |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 3689 | u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info) |
| 3690 | { |
| 3691 | return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 3692 | } |
| 3693 | |
| 3694 | u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info) |
| 3695 | { |
| 3696 | return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 3697 | } |
| 3698 | |
| 3699 | u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info) |
| 3700 | { |
| 3701 | return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3702 | } |
| 3703 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3704 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 3705 | { |
| 3706 | struct list_head *head = &info->space_info; |
| 3707 | struct btrfs_space_info *found; |
| 3708 | |
| 3709 | rcu_read_lock(); |
| 3710 | list_for_each_entry_rcu(found, head, list) { |
| 3711 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3712 | found->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3713 | } |
| 3714 | rcu_read_unlock(); |
| 3715 | } |
| 3716 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3717 | static int should_alloc_chunk(struct btrfs_fs_info *fs_info, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 3718 | struct btrfs_space_info *sinfo, int force) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3719 | { |
Nikolay Borisov | 8d8aafe | 2017-06-22 09:51:48 -0400 | [diff] [blame] | 3720 | u64 bytes_used = btrfs_space_info_used(sinfo, false); |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3721 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3722 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3723 | if (force == CHUNK_ALLOC_FORCE) |
| 3724 | return 1; |
| 3725 | |
| 3726 | /* |
| 3727 | * in limited mode, we want to have some free space up to |
| 3728 | * about 1% of the FS size. |
| 3729 | */ |
| 3730 | if (force == CHUNK_ALLOC_LIMITED) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3731 | thresh = btrfs_super_total_bytes(fs_info->super_copy); |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 3732 | thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1)); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3733 | |
Nikolay Borisov | 8d8aafe | 2017-06-22 09:51:48 -0400 | [diff] [blame] | 3734 | if (sinfo->total_bytes - bytes_used < thresh) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3735 | return 1; |
| 3736 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3737 | |
Nikolay Borisov | 8d8aafe | 2017-06-22 09:51:48 -0400 | [diff] [blame] | 3738 | if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 3739 | return 0; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3740 | return 1; |
| 3741 | } |
| 3742 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3743 | static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type) |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3744 | { |
| 3745 | u64 num_dev; |
| 3746 | |
David Sterba | 9fa02ac | 2019-05-17 11:43:20 +0200 | [diff] [blame] | 3747 | num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max; |
| 3748 | if (!num_dev) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3749 | num_dev = fs_info->fs_devices->rw_devices; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3750 | |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 3751 | return num_dev; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3752 | } |
| 3753 | |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 3754 | /* |
| 3755 | * If @is_allocation is true, reserve space in the system space info necessary |
| 3756 | * for allocating a chunk, otherwise if it's false, reserve space necessary for |
| 3757 | * removing a chunk. |
| 3758 | */ |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 3759 | void check_system_chunk(struct btrfs_trans_handle *trans, u64 type) |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3760 | { |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 3761 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3762 | struct btrfs_space_info *info; |
| 3763 | u64 left; |
| 3764 | u64 thresh; |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 3765 | int ret = 0; |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 3766 | u64 num_devs; |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 3767 | |
| 3768 | /* |
| 3769 | * Needed because we can end up allocating a system chunk and for an |
| 3770 | * atomic and race free space reservation in the chunk block reserve. |
| 3771 | */ |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 3772 | lockdep_assert_held(&fs_info->chunk_mutex); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3773 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 3774 | info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3775 | spin_lock(&info->lock); |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 3776 | left = info->total_bytes - btrfs_space_info_used(info, true); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3777 | spin_unlock(&info->lock); |
| 3778 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3779 | num_devs = get_profile_num_devs(fs_info, type); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 3780 | |
| 3781 | /* num_devs device items to update and 1 chunk item to add or remove */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3782 | thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) + |
| 3783 | btrfs_calc_trans_metadata_size(fs_info, 1); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 3784 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3785 | if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
| 3786 | btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu", |
| 3787 | left, thresh, type); |
Josef Bacik | 5da6afe | 2019-06-18 16:09:24 -0400 | [diff] [blame] | 3788 | btrfs_dump_space_info(fs_info, info, 0, 0); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | if (left < thresh) { |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 3792 | u64 flags = btrfs_system_alloc_profile(fs_info); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3793 | |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 3794 | /* |
| 3795 | * Ignore failure to create system chunk. We might end up not |
| 3796 | * needing it, as we might not need to COW all nodes/leafs from |
| 3797 | * the paths we visit in the chunk tree (they were already COWed |
| 3798 | * or created in the current transaction for example). |
| 3799 | */ |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 3800 | ret = btrfs_alloc_chunk(trans, flags); |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | if (!ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3804 | ret = btrfs_block_rsv_add(fs_info->chunk_root, |
| 3805 | &fs_info->chunk_block_rsv, |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 3806 | thresh, BTRFS_RESERVE_NO_FLUSH); |
| 3807 | if (!ret) |
| 3808 | trans->chunk_bytes_reserved += thresh; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3809 | } |
| 3810 | } |
| 3811 | |
Liu Bo | 28b737f | 2016-07-29 11:09:50 -0700 | [diff] [blame] | 3812 | /* |
| 3813 | * If force is CHUNK_ALLOC_FORCE: |
| 3814 | * - return 1 if it successfully allocates a chunk, |
| 3815 | * - return errors including -ENOSPC otherwise. |
| 3816 | * If force is NOT CHUNK_ALLOC_FORCE: |
| 3817 | * - return 0 if it doesn't need to allocate a new chunk, |
| 3818 | * - return 1 if it successfully allocates a chunk, |
| 3819 | * - return errors including -ENOSPC otherwise. |
| 3820 | */ |
Josef Bacik | fc471cb | 2019-06-18 16:09:17 -0400 | [diff] [blame] | 3821 | int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags, |
| 3822 | enum btrfs_chunk_alloc_enum force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3823 | { |
Nikolay Borisov | 0145882 | 2018-06-20 15:49:05 +0300 | [diff] [blame] | 3824 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3825 | struct btrfs_space_info *space_info; |
Nikolay Borisov | 2556fbb | 2018-04-18 10:27:57 +0300 | [diff] [blame] | 3826 | bool wait_for_alloc = false; |
| 3827 | bool should_alloc = false; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3828 | int ret = 0; |
| 3829 | |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 3830 | /* Don't re-enter if we're already allocating a chunk */ |
| 3831 | if (trans->allocating_chunk) |
| 3832 | return -ENOSPC; |
| 3833 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 3834 | space_info = btrfs_find_space_info(fs_info, flags); |
Jeff Mahoney | dc2d300 | 2018-03-20 15:25:25 -0400 | [diff] [blame] | 3835 | ASSERT(space_info); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3836 | |
Nikolay Borisov | 2556fbb | 2018-04-18 10:27:57 +0300 | [diff] [blame] | 3837 | do { |
| 3838 | spin_lock(&space_info->lock); |
| 3839 | if (force < space_info->force_alloc) |
| 3840 | force = space_info->force_alloc; |
| 3841 | should_alloc = should_alloc_chunk(fs_info, space_info, force); |
| 3842 | if (space_info->full) { |
| 3843 | /* No more free physical space */ |
| 3844 | if (should_alloc) |
| 3845 | ret = -ENOSPC; |
| 3846 | else |
| 3847 | ret = 0; |
| 3848 | spin_unlock(&space_info->lock); |
| 3849 | return ret; |
| 3850 | } else if (!should_alloc) { |
| 3851 | spin_unlock(&space_info->lock); |
| 3852 | return 0; |
| 3853 | } else if (space_info->chunk_alloc) { |
| 3854 | /* |
| 3855 | * Someone is already allocating, so we need to block |
| 3856 | * until this someone is finished and then loop to |
| 3857 | * recheck if we should continue with our allocation |
| 3858 | * attempt. |
| 3859 | */ |
| 3860 | wait_for_alloc = true; |
| 3861 | spin_unlock(&space_info->lock); |
| 3862 | mutex_lock(&fs_info->chunk_mutex); |
| 3863 | mutex_unlock(&fs_info->chunk_mutex); |
| 3864 | } else { |
| 3865 | /* Proceed with allocation */ |
| 3866 | space_info->chunk_alloc = 1; |
| 3867 | wait_for_alloc = false; |
| 3868 | spin_unlock(&space_info->lock); |
| 3869 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3870 | |
Nikolay Borisov | 2556fbb | 2018-04-18 10:27:57 +0300 | [diff] [blame] | 3871 | cond_resched(); |
| 3872 | } while (wait_for_alloc); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3873 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3874 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 3875 | trans->allocating_chunk = true; |
| 3876 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3877 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3878 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 3879 | * allocating mixed chunks instead of individual chunks. |
| 3880 | */ |
| 3881 | if (btrfs_mixed_space_info(space_info)) |
| 3882 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 3883 | |
| 3884 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3885 | * if we're doing a data chunk, go ahead and make sure that |
| 3886 | * we keep a reasonable number of metadata chunks allocated in the |
| 3887 | * FS as well. |
| 3888 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3889 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3890 | fs_info->data_chunk_allocations++; |
| 3891 | if (!(fs_info->data_chunk_allocations % |
| 3892 | fs_info->metadata_ratio)) |
| 3893 | force_metadata_allocation(fs_info); |
| 3894 | } |
| 3895 | |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3896 | /* |
| 3897 | * Check if we have enough space in SYSTEM chunk because we may need |
| 3898 | * to update devices. |
| 3899 | */ |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 3900 | check_system_chunk(trans, flags); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3901 | |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 3902 | ret = btrfs_alloc_chunk(trans, flags); |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 3903 | trans->allocating_chunk = false; |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 3904 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3905 | spin_lock(&space_info->lock); |
Nikolay Borisov | 57f1642 | 2018-04-11 11:21:19 +0300 | [diff] [blame] | 3906 | if (ret < 0) { |
| 3907 | if (ret == -ENOSPC) |
| 3908 | space_info->full = 1; |
| 3909 | else |
| 3910 | goto out; |
| 3911 | } else { |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3912 | ret = 1; |
Josef Bacik | 21a94f7 | 2018-10-11 15:54:03 -0400 | [diff] [blame] | 3913 | space_info->max_extent_size = 0; |
Nikolay Borisov | 57f1642 | 2018-04-11 11:21:19 +0300 | [diff] [blame] | 3914 | } |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3915 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3916 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Alexandre Oliva | a81cb9a | 2013-02-21 21:15:14 +0000 | [diff] [blame] | 3917 | out: |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3918 | space_info->chunk_alloc = 0; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3919 | spin_unlock(&space_info->lock); |
Dan Carpenter | a25c75d | 2012-04-18 09:59:29 +0300 | [diff] [blame] | 3920 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 00d80e3 | 2015-07-20 14:56:20 +0100 | [diff] [blame] | 3921 | /* |
| 3922 | * When we allocate a new chunk we reserve space in the chunk block |
| 3923 | * reserve to make sure we can COW nodes/leafs in the chunk tree or |
| 3924 | * add new nodes/leafs to it if we end up needing to do it when |
| 3925 | * inserting the chunk item and updating device items as part of the |
| 3926 | * second phase of chunk allocation, performed by |
| 3927 | * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a |
| 3928 | * large number of new block groups to create in our transaction |
| 3929 | * handle's new_bgs list to avoid exhausting the chunk block reserve |
| 3930 | * in extreme cases - like having a single transaction create many new |
| 3931 | * block groups when starting to write out the free space caches of all |
| 3932 | * the block groups that were made dirty during the lifetime of the |
| 3933 | * transaction. |
| 3934 | */ |
Filipe Manana | 5ce5555 | 2018-10-12 10:03:55 +0100 | [diff] [blame] | 3935 | if (trans->chunk_bytes_reserved >= (u64)SZ_2M) |
Nikolay Borisov | 6c686b3 | 2018-02-07 17:55:40 +0200 | [diff] [blame] | 3936 | btrfs_create_pending_block_groups(trans); |
Filipe Manana | 5ce5555 | 2018-10-12 10:03:55 +0100 | [diff] [blame] | 3937 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3938 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3939 | } |
| 3940 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3941 | static int update_block_group(struct btrfs_trans_handle *trans, |
David Sterba | 6b27940 | 2019-03-20 12:10:15 +0100 | [diff] [blame] | 3942 | u64 bytenr, u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3943 | { |
David Sterba | 6b27940 | 2019-03-20 12:10:15 +0100 | [diff] [blame] | 3944 | struct btrfs_fs_info *info = trans->fs_info; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3945 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3946 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3947 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3948 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3949 | int factor; |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 3950 | int ret = 0; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 3951 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3952 | /* block accounting for super block */ |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 3953 | spin_lock(&info->delalloc_root_lock); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3954 | old_val = btrfs_super_bytes_used(info->super_copy); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3955 | if (alloc) |
| 3956 | old_val += num_bytes; |
| 3957 | else |
| 3958 | old_val -= num_bytes; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3959 | btrfs_set_super_bytes_used(info->super_copy, old_val); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 3960 | spin_unlock(&info->delalloc_root_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3961 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3962 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3963 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 3964 | if (!cache) { |
| 3965 | ret = -ENOENT; |
| 3966 | break; |
| 3967 | } |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 3968 | factor = btrfs_bg_type_to_factor(cache->flags); |
| 3969 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 3970 | /* |
| 3971 | * If this block group has free space cache written out, we |
| 3972 | * need to make sure to load it if we are removing space. This |
| 3973 | * is because we need the unpinning stage to actually add the |
| 3974 | * space back to the block group, otherwise we will leak space. |
| 3975 | */ |
| 3976 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 3977 | btrfs_cache_block_group(cache, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3978 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3979 | byte_in_group = bytenr - cache->key.objectid; |
| 3980 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3981 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3982 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3983 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3984 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 3985 | if (btrfs_test_opt(info, SPACE_CACHE) && |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3986 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 3987 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 3988 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3989 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3990 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3991 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3992 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3993 | btrfs_set_block_group_used(&cache->item, old_val); |
| 3994 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3995 | cache->space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3996 | cache->space_info->bytes_used += num_bytes; |
| 3997 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3998 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3999 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4000 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4001 | old_val -= num_bytes; |
Filipe Manana | ae0ab00 | 2014-11-26 15:28:52 +0000 | [diff] [blame] | 4002 | btrfs_set_block_group_used(&cache->item, old_val); |
| 4003 | cache->pinned += num_bytes; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 4004 | btrfs_space_info_update_bytes_pinned(info, |
| 4005 | cache->space_info, num_bytes); |
Filipe Manana | ae0ab00 | 2014-11-26 15:28:52 +0000 | [diff] [blame] | 4006 | cache->space_info->bytes_used -= num_bytes; |
| 4007 | cache->space_info->disk_used -= num_bytes * factor; |
| 4008 | spin_unlock(&cache->lock); |
| 4009 | spin_unlock(&cache->space_info->lock); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 4010 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4011 | trace_btrfs_space_reservation(info, "pinned", |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 4012 | cache->space_info->flags, |
| 4013 | num_bytes, 1); |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 4014 | percpu_counter_add_batch(&cache->space_info->total_bytes_pinned, |
| 4015 | num_bytes, |
| 4016 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Filipe Manana | ae0ab00 | 2014-11-26 15:28:52 +0000 | [diff] [blame] | 4017 | set_extent_dirty(info->pinned_extents, |
| 4018 | bytenr, bytenr + num_bytes - 1, |
| 4019 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4020 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 4021 | |
| 4022 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 4023 | if (list_empty(&cache->dirty_list)) { |
| 4024 | list_add_tail(&cache->dirty_list, |
| 4025 | &trans->transaction->dirty_bgs); |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 4026 | trans->delayed_ref_updates++; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 4027 | btrfs_get_block_group(cache); |
| 4028 | } |
| 4029 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 4030 | |
Filipe Manana | 036a934 | 2015-11-23 15:25:16 +0000 | [diff] [blame] | 4031 | /* |
| 4032 | * No longer have used bytes in this block group, queue it for |
| 4033 | * deletion. We do this after adding the block group to the |
| 4034 | * dirty list to avoid races between cleaner kthread and space |
| 4035 | * cache writeout. |
| 4036 | */ |
Qu Wenruo | 031f24d | 2018-05-22 16:43:47 +0800 | [diff] [blame] | 4037 | if (!alloc && old_val == 0) |
| 4038 | btrfs_mark_bg_unused(cache); |
Filipe Manana | 036a934 | 2015-11-23 15:25:16 +0000 | [diff] [blame] | 4039 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4040 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4041 | total -= num_bytes; |
| 4042 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4043 | } |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 4044 | |
| 4045 | /* Modified block groups are accounted for in the delayed_refs_rsv. */ |
| 4046 | btrfs_update_delayed_refs_rsv(trans); |
| 4047 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4048 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4049 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4050 | static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4051 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4052 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4053 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4054 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4055 | spin_lock(&fs_info->block_group_cache_lock); |
| 4056 | bytenr = fs_info->first_logical_byte; |
| 4057 | spin_unlock(&fs_info->block_group_cache_lock); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 4058 | |
| 4059 | if (bytenr < (u64)-1) |
| 4060 | return bytenr; |
| 4061 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4062 | cache = btrfs_lookup_first_block_group(fs_info, search_start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4063 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4064 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4065 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4066 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4067 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4068 | |
| 4069 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4070 | } |
| 4071 | |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 4072 | static int pin_down_extent(struct btrfs_block_group_cache *cache, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4073 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4074 | { |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 4075 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 4076 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4077 | spin_lock(&cache->space_info->lock); |
| 4078 | spin_lock(&cache->lock); |
| 4079 | cache->pinned += num_bytes; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 4080 | btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info, |
| 4081 | num_bytes); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4082 | if (reserved) { |
| 4083 | cache->reserved -= num_bytes; |
| 4084 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4085 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4086 | spin_unlock(&cache->lock); |
| 4087 | spin_unlock(&cache->space_info->lock); |
| 4088 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4089 | trace_btrfs_space_reservation(fs_info, "pinned", |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 4090 | cache->space_info->flags, num_bytes, 1); |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 4091 | percpu_counter_add_batch(&cache->space_info->total_bytes_pinned, |
| 4092 | num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4093 | set_extent_dirty(fs_info->pinned_extents, bytenr, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4094 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4095 | return 0; |
| 4096 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4097 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4098 | /* |
| 4099 | * this function must be called within transaction |
| 4100 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4101 | int btrfs_pin_extent(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4102 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4103 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4104 | struct btrfs_block_group_cache *cache; |
| 4105 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4106 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4107 | BUG_ON(!cache); /* Logic error */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4108 | |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 4109 | pin_down_extent(cache, bytenr, num_bytes, reserved); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4110 | |
| 4111 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4112 | return 0; |
| 4113 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4114 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4115 | /* |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4116 | * this function must be called within transaction |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4117 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4118 | int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info, |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4119 | u64 bytenr, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4120 | { |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4121 | struct btrfs_block_group_cache *cache; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 4122 | int ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4123 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4124 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 4125 | if (!cache) |
| 4126 | return -EINVAL; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4127 | |
| 4128 | /* |
| 4129 | * pull in the free space cache (if any) so that our pin |
| 4130 | * removes the free space from the cache. We have load_only set |
| 4131 | * to one because the slow code to read in the free extents does check |
| 4132 | * the pinned extents. |
| 4133 | */ |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 4134 | btrfs_cache_block_group(cache, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4135 | |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 4136 | pin_down_extent(cache, bytenr, num_bytes, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4137 | |
| 4138 | /* remove us from the free space cache (if we're there at all) */ |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 4139 | ret = btrfs_remove_free_space(cache, bytenr, num_bytes); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4140 | btrfs_put_block_group(cache); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 4141 | return ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4142 | } |
| 4143 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4144 | static int __exclude_logged_extent(struct btrfs_fs_info *fs_info, |
| 4145 | u64 start, u64 num_bytes) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4146 | { |
| 4147 | int ret; |
| 4148 | struct btrfs_block_group_cache *block_group; |
| 4149 | struct btrfs_caching_control *caching_ctl; |
| 4150 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4151 | block_group = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4152 | if (!block_group) |
| 4153 | return -EINVAL; |
| 4154 | |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 4155 | btrfs_cache_block_group(block_group, 0); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 4156 | caching_ctl = btrfs_get_caching_control(block_group); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4157 | |
| 4158 | if (!caching_ctl) { |
| 4159 | /* Logic error */ |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 4160 | BUG_ON(!btrfs_block_group_cache_done(block_group)); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4161 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 4162 | } else { |
| 4163 | mutex_lock(&caching_ctl->mutex); |
| 4164 | |
| 4165 | if (start >= caching_ctl->progress) { |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 4166 | ret = btrfs_add_excluded_extent(fs_info, start, |
| 4167 | num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4168 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 4169 | ret = btrfs_remove_free_space(block_group, |
| 4170 | start, num_bytes); |
| 4171 | } else { |
| 4172 | num_bytes = caching_ctl->progress - start; |
| 4173 | ret = btrfs_remove_free_space(block_group, |
| 4174 | start, num_bytes); |
| 4175 | if (ret) |
| 4176 | goto out_lock; |
| 4177 | |
| 4178 | num_bytes = (start + num_bytes) - |
| 4179 | caching_ctl->progress; |
| 4180 | start = caching_ctl->progress; |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 4181 | ret = btrfs_add_excluded_extent(fs_info, start, |
| 4182 | num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4183 | } |
| 4184 | out_lock: |
| 4185 | mutex_unlock(&caching_ctl->mutex); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 4186 | btrfs_put_caching_control(caching_ctl); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4187 | } |
| 4188 | btrfs_put_block_group(block_group); |
| 4189 | return ret; |
| 4190 | } |
| 4191 | |
David Sterba | bcdc428 | 2019-03-20 12:14:33 +0100 | [diff] [blame] | 4192 | int btrfs_exclude_logged_extents(struct extent_buffer *eb) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4193 | { |
David Sterba | bcdc428 | 2019-03-20 12:14:33 +0100 | [diff] [blame] | 4194 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4195 | struct btrfs_file_extent_item *item; |
| 4196 | struct btrfs_key key; |
| 4197 | int found_type; |
| 4198 | int i; |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 4199 | int ret = 0; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4200 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4201 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4202 | return 0; |
| 4203 | |
| 4204 | for (i = 0; i < btrfs_header_nritems(eb); i++) { |
| 4205 | btrfs_item_key_to_cpu(eb, &key, i); |
| 4206 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 4207 | continue; |
| 4208 | item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); |
| 4209 | found_type = btrfs_file_extent_type(eb, item); |
| 4210 | if (found_type == BTRFS_FILE_EXTENT_INLINE) |
| 4211 | continue; |
| 4212 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 4213 | continue; |
| 4214 | key.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 4215 | key.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 4216 | ret = __exclude_logged_extent(fs_info, key.objectid, key.offset); |
| 4217 | if (ret) |
| 4218 | break; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4219 | } |
| 4220 | |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 4221 | return ret; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4222 | } |
| 4223 | |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 4224 | static void |
| 4225 | btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg) |
| 4226 | { |
| 4227 | atomic_inc(&bg->reservations); |
| 4228 | } |
| 4229 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4230 | /** |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 4231 | * btrfs_add_reserved_bytes - update the block_group and space info counters |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4232 | * @cache: The cache we are manipulating |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4233 | * @ram_bytes: The number of bytes of file content, and will be same to |
| 4234 | * @num_bytes except for the compress path. |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4235 | * @num_bytes: The number of bytes in question |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4236 | * @delalloc: The blocks are allocated for the delalloc write |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4237 | * |
Xiaoguang Wang | 745699e | 2016-09-23 12:38:50 +0800 | [diff] [blame] | 4238 | * This is called by the allocator when it reserves space. If this is a |
| 4239 | * reservation and the block group has become read only we cannot make the |
| 4240 | * reservation and return -EAGAIN, otherwise this function always succeeds. |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4241 | */ |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 4242 | static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4243 | u64 ram_bytes, u64 num_bytes, int delalloc) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4244 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4245 | struct btrfs_space_info *space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4246 | int ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4247 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4248 | spin_lock(&space_info->lock); |
| 4249 | spin_lock(&cache->lock); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 4250 | if (cache->ro) { |
| 4251 | ret = -EAGAIN; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4252 | } else { |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 4253 | cache->reserved += num_bytes; |
| 4254 | space_info->bytes_reserved += num_bytes; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 4255 | btrfs_space_info_update_bytes_may_use(cache->fs_info, |
| 4256 | space_info, -ram_bytes); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4257 | if (delalloc) |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 4258 | cache->delalloc_bytes += num_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4259 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4260 | spin_unlock(&cache->lock); |
| 4261 | spin_unlock(&space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4262 | return ret; |
| 4263 | } |
| 4264 | |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 4265 | /** |
| 4266 | * btrfs_free_reserved_bytes - update the block_group and space info counters |
| 4267 | * @cache: The cache we are manipulating |
| 4268 | * @num_bytes: The number of bytes in question |
| 4269 | * @delalloc: The blocks are allocated for the delalloc write |
| 4270 | * |
| 4271 | * This is called by somebody who is freeing space that was never actually used |
| 4272 | * on disk. For example if you reserve some space for a new leaf in transaction |
| 4273 | * A and before transaction A commits you free that leaf, you call this with |
| 4274 | * reserve set to 0 in order to clear the reservation. |
| 4275 | */ |
| 4276 | |
zhong jiang | 556f3ca | 2018-08-17 00:37:14 +0800 | [diff] [blame] | 4277 | static void btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 4278 | u64 num_bytes, int delalloc) |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 4279 | { |
| 4280 | struct btrfs_space_info *space_info = cache->space_info; |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 4281 | |
| 4282 | spin_lock(&space_info->lock); |
| 4283 | spin_lock(&cache->lock); |
| 4284 | if (cache->ro) |
| 4285 | space_info->bytes_readonly += num_bytes; |
| 4286 | cache->reserved -= num_bytes; |
| 4287 | space_info->bytes_reserved -= num_bytes; |
Josef Bacik | 21a94f7 | 2018-10-11 15:54:03 -0400 | [diff] [blame] | 4288 | space_info->max_extent_size = 0; |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 4289 | |
| 4290 | if (delalloc) |
| 4291 | cache->delalloc_bytes -= num_bytes; |
| 4292 | spin_unlock(&cache->lock); |
| 4293 | spin_unlock(&space_info->lock); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 4294 | } |
David Sterba | 8b74c03 | 2017-02-10 19:20:56 +0100 | [diff] [blame] | 4295 | void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4296 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4297 | struct btrfs_caching_control *next; |
| 4298 | struct btrfs_caching_control *caching_ctl; |
| 4299 | struct btrfs_block_group_cache *cache; |
| 4300 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 4301 | down_write(&fs_info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4302 | |
| 4303 | list_for_each_entry_safe(caching_ctl, next, |
| 4304 | &fs_info->caching_block_groups, list) { |
| 4305 | cache = caching_ctl->block_group; |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 4306 | if (btrfs_block_group_cache_done(cache)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4307 | cache->last_byte_to_unpin = (u64)-1; |
| 4308 | list_del_init(&caching_ctl->list); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 4309 | btrfs_put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4310 | } else { |
| 4311 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 4312 | } |
| 4313 | } |
| 4314 | |
| 4315 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4316 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 4317 | else |
| 4318 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 4319 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 4320 | up_write(&fs_info->commit_root_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4321 | |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 4322 | btrfs_update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4323 | } |
| 4324 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 4325 | /* |
| 4326 | * Returns the free cluster for the given space info and sets empty_cluster to |
| 4327 | * what it should be based on the mount options. |
| 4328 | */ |
| 4329 | static struct btrfs_free_cluster * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4330 | fetch_cluster_info(struct btrfs_fs_info *fs_info, |
| 4331 | struct btrfs_space_info *space_info, u64 *empty_cluster) |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 4332 | { |
| 4333 | struct btrfs_free_cluster *ret = NULL; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 4334 | |
| 4335 | *empty_cluster = 0; |
| 4336 | if (btrfs_mixed_space_info(space_info)) |
| 4337 | return ret; |
| 4338 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 4339 | if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4340 | ret = &fs_info->meta_alloc_cluster; |
Hans van Kranenburg | 583b723 | 2017-07-28 08:31:28 +0200 | [diff] [blame] | 4341 | if (btrfs_test_opt(fs_info, SSD)) |
| 4342 | *empty_cluster = SZ_2M; |
| 4343 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4344 | *empty_cluster = SZ_64K; |
Hans van Kranenburg | 583b723 | 2017-07-28 08:31:28 +0200 | [diff] [blame] | 4345 | } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && |
| 4346 | btrfs_test_opt(fs_info, SSD_SPREAD)) { |
| 4347 | *empty_cluster = SZ_2M; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4348 | ret = &fs_info->data_alloc_cluster; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 4349 | } |
| 4350 | |
| 4351 | return ret; |
| 4352 | } |
| 4353 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4354 | static int unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 4355 | u64 start, u64 end, |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 4356 | const bool return_free_space) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4357 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4358 | struct btrfs_block_group_cache *cache = NULL; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 4359 | struct btrfs_space_info *space_info; |
| 4360 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 4361 | struct btrfs_free_cluster *cluster = NULL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4362 | u64 len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 4363 | u64 total_unpinned = 0; |
| 4364 | u64 empty_cluster = 0; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 4365 | bool readonly; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4366 | |
| 4367 | while (start <= end) { |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 4368 | readonly = false; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4369 | if (!cache || |
| 4370 | start >= cache->key.objectid + cache->key.offset) { |
| 4371 | if (cache) |
| 4372 | btrfs_put_block_group(cache); |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 4373 | total_unpinned = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4374 | cache = btrfs_lookup_block_group(fs_info, start); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4375 | BUG_ON(!cache); /* Logic error */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 4376 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4377 | cluster = fetch_cluster_info(fs_info, |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 4378 | cache->space_info, |
| 4379 | &empty_cluster); |
| 4380 | empty_cluster <<= 1; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | len = cache->key.objectid + cache->key.offset - start; |
| 4384 | len = min(len, end + 1 - start); |
| 4385 | |
| 4386 | if (start < cache->last_byte_to_unpin) { |
| 4387 | len = min(len, cache->last_byte_to_unpin - start); |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 4388 | if (return_free_space) |
| 4389 | btrfs_add_free_space(cache, start, len); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4390 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4391 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4392 | start += len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 4393 | total_unpinned += len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 4394 | space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4395 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 4396 | /* |
| 4397 | * If this space cluster has been marked as fragmented and we've |
| 4398 | * unpinned enough in this block group to potentially allow a |
| 4399 | * cluster to be created inside of it go ahead and clear the |
| 4400 | * fragmented check. |
| 4401 | */ |
| 4402 | if (cluster && cluster->fragmented && |
| 4403 | total_unpinned > empty_cluster) { |
| 4404 | spin_lock(&cluster->lock); |
| 4405 | cluster->fragmented = 0; |
| 4406 | spin_unlock(&cluster->lock); |
| 4407 | } |
| 4408 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 4409 | spin_lock(&space_info->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4410 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4411 | cache->pinned -= len; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 4412 | btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len); |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 4413 | |
| 4414 | trace_btrfs_space_reservation(fs_info, "pinned", |
| 4415 | space_info->flags, len, 0); |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4416 | space_info->max_extent_size = 0; |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 4417 | percpu_counter_add_batch(&space_info->total_bytes_pinned, |
| 4418 | -len, BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 4419 | if (cache->ro) { |
| 4420 | space_info->bytes_readonly += len; |
| 4421 | readonly = true; |
| 4422 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4423 | spin_unlock(&cache->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4424 | if (!readonly && return_free_space && |
| 4425 | global_rsv->space_info == space_info) { |
| 4426 | u64 to_add = len; |
Nikolay Borisov | 92ac58e | 2017-08-17 10:52:28 +0300 | [diff] [blame] | 4427 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 4428 | spin_lock(&global_rsv->lock); |
| 4429 | if (!global_rsv->full) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4430 | to_add = min(len, global_rsv->size - |
| 4431 | global_rsv->reserved); |
| 4432 | global_rsv->reserved += to_add; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 4433 | btrfs_space_info_update_bytes_may_use(fs_info, |
| 4434 | space_info, to_add); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 4435 | if (global_rsv->reserved >= global_rsv->size) |
| 4436 | global_rsv->full = 1; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4437 | trace_btrfs_space_reservation(fs_info, |
| 4438 | "space_info", |
| 4439 | space_info->flags, |
| 4440 | to_add, 1); |
| 4441 | len -= to_add; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 4442 | } |
| 4443 | spin_unlock(&global_rsv->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4444 | /* Add to any tickets we may have */ |
| 4445 | if (len) |
Josef Bacik | d44b72a | 2019-06-18 16:09:18 -0400 | [diff] [blame] | 4446 | btrfs_space_info_add_new_bytes(fs_info, |
| 4447 | space_info, len); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 4448 | } |
| 4449 | spin_unlock(&space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4450 | } |
| 4451 | |
| 4452 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4453 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4454 | return 0; |
| 4455 | } |
| 4456 | |
Nikolay Borisov | 5ead2dd | 2018-03-15 16:00:26 +0200 | [diff] [blame] | 4457 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4458 | { |
Nikolay Borisov | 5ead2dd | 2018-03-15 16:00:26 +0200 | [diff] [blame] | 4459 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 4460 | struct btrfs_block_group_cache *block_group, *tmp; |
| 4461 | struct list_head *deleted_bgs; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4462 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4463 | u64 start; |
| 4464 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4465 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4466 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4467 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4468 | unpin = &fs_info->freed_extents[1]; |
| 4469 | else |
| 4470 | unpin = &fs_info->freed_extents[0]; |
| 4471 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 4472 | while (!trans->aborted) { |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 4473 | struct extent_state *cached_state = NULL; |
| 4474 | |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 4475 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4476 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 4477 | EXTENT_DIRTY, &cached_state); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 4478 | if (ret) { |
| 4479 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4480 | break; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 4481 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4482 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4483 | if (btrfs_test_opt(fs_info, DISCARD)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4484 | ret = btrfs_discard_extent(fs_info, start, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 4485 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4486 | |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 4487 | clear_extent_dirty(unpin, start, end, &cached_state); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4488 | unpin_extent_range(fs_info, start, end, true); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 4489 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 4490 | free_extent_state(cached_state); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4491 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4492 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4493 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 4494 | /* |
| 4495 | * Transaction is finished. We don't need the lock anymore. We |
| 4496 | * do need to clean up the block groups in case of a transaction |
| 4497 | * abort. |
| 4498 | */ |
| 4499 | deleted_bgs = &trans->transaction->deleted_bgs; |
| 4500 | list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) { |
| 4501 | u64 trimmed = 0; |
| 4502 | |
| 4503 | ret = -EROFS; |
| 4504 | if (!trans->aborted) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4505 | ret = btrfs_discard_extent(fs_info, |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 4506 | block_group->key.objectid, |
| 4507 | block_group->key.offset, |
| 4508 | &trimmed); |
| 4509 | |
| 4510 | list_del_init(&block_group->bg_list); |
| 4511 | btrfs_put_block_group_trimming(block_group); |
| 4512 | btrfs_put_block_group(block_group); |
| 4513 | |
| 4514 | if (ret) { |
| 4515 | const char *errstr = btrfs_decode_error(ret); |
| 4516 | btrfs_warn(fs_info, |
David Sterba | 913e153 | 2017-07-13 15:32:18 +0200 | [diff] [blame] | 4517 | "discard failed while removing blockgroup: errno=%d %s", |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 4518 | ret, errstr); |
| 4519 | } |
| 4520 | } |
| 4521 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 4522 | return 0; |
| 4523 | } |
| 4524 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4525 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 4526 | struct btrfs_delayed_ref_node *node, u64 parent, |
| 4527 | u64 root_objectid, u64 owner_objectid, |
| 4528 | u64 owner_offset, int refs_to_drop, |
| 4529 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4530 | { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 4531 | struct btrfs_fs_info *info = trans->fs_info; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4532 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4533 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 4534 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4535 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4536 | struct btrfs_extent_item *ei; |
| 4537 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4538 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4539 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4540 | int extent_slot = 0; |
| 4541 | int found_extent = 0; |
| 4542 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4543 | u32 item_size; |
| 4544 | u64 refs; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 4545 | u64 bytenr = node->bytenr; |
| 4546 | u64 num_bytes = node->num_bytes; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 4547 | int last_ref = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4548 | bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 4549 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4550 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4551 | if (!path) |
| 4552 | return -ENOMEM; |
| 4553 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4554 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4555 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4556 | |
| 4557 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 4558 | BUG_ON(!is_data && refs_to_drop != 1); |
| 4559 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4560 | if (is_data) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 4561 | skinny_metadata = false; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4562 | |
Nikolay Borisov | fbe4801 | 2018-06-20 15:48:52 +0300 | [diff] [blame] | 4563 | ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes, |
| 4564 | parent, root_objectid, owner_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4565 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4566 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4567 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4568 | while (extent_slot >= 0) { |
| 4569 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4570 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4571 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4572 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4573 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 4574 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4575 | found_extent = 1; |
| 4576 | break; |
| 4577 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4578 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 4579 | key.offset == owner_objectid) { |
| 4580 | found_extent = 1; |
| 4581 | break; |
| 4582 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4583 | if (path->slots[0] - extent_slot > 5) |
| 4584 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4585 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4586 | } |
Nikolay Borisov | a79865c | 2018-06-21 09:45:00 +0300 | [diff] [blame] | 4587 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4588 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4589 | BUG_ON(iref); |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 4590 | ret = remove_extent_backref(trans, path, NULL, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 4591 | refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 4592 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4593 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4594 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4595 | goto out; |
| 4596 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4597 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4598 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4599 | |
| 4600 | key.objectid = bytenr; |
| 4601 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4602 | key.offset = num_bytes; |
| 4603 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4604 | if (!is_data && skinny_metadata) { |
| 4605 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 4606 | key.offset = owner_objectid; |
| 4607 | } |
| 4608 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4609 | ret = btrfs_search_slot(trans, extent_root, |
| 4610 | &key, path, -1, 1); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4611 | if (ret > 0 && skinny_metadata && path->slots[0]) { |
| 4612 | /* |
| 4613 | * Couldn't find our skinny metadata item, |
| 4614 | * see if we have ye olde extent item. |
| 4615 | */ |
| 4616 | path->slots[0]--; |
| 4617 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 4618 | path->slots[0]); |
| 4619 | if (key.objectid == bytenr && |
| 4620 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 4621 | key.offset == num_bytes) |
| 4622 | ret = 0; |
| 4623 | } |
| 4624 | |
| 4625 | if (ret > 0 && skinny_metadata) { |
| 4626 | skinny_metadata = false; |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 4627 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4628 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4629 | key.offset = num_bytes; |
| 4630 | btrfs_release_path(path); |
| 4631 | ret = btrfs_search_slot(trans, extent_root, |
| 4632 | &key, path, -1, 1); |
| 4633 | } |
| 4634 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4635 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4636 | btrfs_err(info, |
| 4637 | "umm, got %d back from search, was looking for %llu", |
| 4638 | ret, bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 4639 | if (ret > 0) |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4640 | btrfs_print_leaf(path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4641 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4642 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4643 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4644 | goto out; |
| 4645 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4646 | extent_slot = path->slots[0]; |
| 4647 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 4648 | } else if (WARN_ON(ret == -ENOENT)) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4649 | btrfs_print_leaf(path->nodes[0]); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4650 | btrfs_err(info, |
| 4651 | "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4652 | bytenr, parent, root_objectid, owner_objectid, |
| 4653 | owner_offset); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4654 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | c4a050b | 2014-03-14 16:36:53 -0400 | [diff] [blame] | 4655 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4656 | } else { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4657 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4658 | goto out; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4659 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4660 | |
| 4661 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4662 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 4663 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 4664 | ret = -EINVAL; |
| 4665 | btrfs_print_v0_err(info); |
| 4666 | btrfs_abort_transaction(trans, ret); |
| 4667 | goto out; |
| 4668 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4669 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4670 | struct btrfs_extent_item); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4671 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 4672 | key.type == BTRFS_EXTENT_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4673 | struct btrfs_tree_block_info *bi; |
| 4674 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 4675 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 4676 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4677 | } |
| 4678 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4679 | refs = btrfs_extent_refs(leaf, ei); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 4680 | if (refs < refs_to_drop) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4681 | btrfs_err(info, |
| 4682 | "trying to drop %d refs but we only have %Lu for bytenr %Lu", |
| 4683 | refs_to_drop, refs, bytenr); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 4684 | ret = -EINVAL; |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4685 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 4686 | goto out; |
| 4687 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4688 | refs -= refs_to_drop; |
| 4689 | |
| 4690 | if (refs > 0) { |
| 4691 | if (extent_op) |
| 4692 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 4693 | /* |
| 4694 | * In the case of inline back ref, reference count will |
| 4695 | * be updated by remove_extent_backref |
| 4696 | */ |
| 4697 | if (iref) { |
| 4698 | BUG_ON(!found_extent); |
| 4699 | } else { |
| 4700 | btrfs_set_extent_refs(leaf, ei, refs); |
| 4701 | btrfs_mark_buffer_dirty(leaf); |
| 4702 | } |
| 4703 | if (found_extent) { |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 4704 | ret = remove_extent_backref(trans, path, iref, |
| 4705 | refs_to_drop, is_data, |
| 4706 | &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4707 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4708 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4709 | goto out; |
| 4710 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4711 | } |
| 4712 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4713 | if (found_extent) { |
| 4714 | BUG_ON(is_data && refs_to_drop != |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 4715 | extent_data_ref_count(path, iref)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4716 | if (iref) { |
| 4717 | BUG_ON(path->slots[0] != extent_slot); |
| 4718 | } else { |
| 4719 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 4720 | path->slots[0] = extent_slot; |
| 4721 | num_to_del = 2; |
| 4722 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 4723 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4724 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 4725 | last_ref = 1; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4726 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 4727 | num_to_del); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4728 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4729 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4730 | goto out; |
| 4731 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4732 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 4733 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4734 | if (is_data) { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 4735 | ret = btrfs_del_csums(trans, info, bytenr, num_bytes); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4736 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4737 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4738 | goto out; |
| 4739 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4740 | } |
| 4741 | |
Nikolay Borisov | e7355e5 | 2018-05-10 15:44:55 +0300 | [diff] [blame] | 4742 | ret = add_to_free_space_tree(trans, bytenr, num_bytes); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 4743 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4744 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 4745 | goto out; |
| 4746 | } |
| 4747 | |
David Sterba | 6b27940 | 2019-03-20 12:10:15 +0100 | [diff] [blame] | 4748 | ret = update_block_group(trans, bytenr, num_bytes, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4749 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4750 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4751 | goto out; |
| 4752 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4753 | } |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 4754 | btrfs_release_path(path); |
| 4755 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4756 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4757 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4758 | return ret; |
| 4759 | } |
| 4760 | |
| 4761 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4762 | * when we free an block, it is possible (and likely) that we free the last |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4763 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 4764 | * a given extent, and if there are no other delayed refs to be processed, it |
| 4765 | * removes it from the tree. |
| 4766 | */ |
| 4767 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4768 | u64 bytenr) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4769 | { |
| 4770 | struct btrfs_delayed_ref_head *head; |
| 4771 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4772 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4773 | |
| 4774 | delayed_refs = &trans->transaction->delayed_refs; |
| 4775 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 4776 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4777 | if (!head) |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 4778 | goto out_delayed_unlock; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4779 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 4780 | spin_lock(&head->lock); |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 4781 | if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root)) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4782 | goto out; |
| 4783 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 4784 | if (cleanup_extent_op(head) != NULL) |
| 4785 | goto out; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4786 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4787 | /* |
| 4788 | * waiting for the lock here would deadlock. If someone else has it |
| 4789 | * locked they are already in the process of dropping it anyway |
| 4790 | */ |
| 4791 | if (!mutex_trylock(&head->mutex)) |
| 4792 | goto out; |
| 4793 | |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 4794 | btrfs_delete_ref_head(delayed_refs, head); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 4795 | head->processing = 0; |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 4796 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 4797 | spin_unlock(&head->lock); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4798 | spin_unlock(&delayed_refs->lock); |
| 4799 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4800 | BUG_ON(head->extent_op); |
| 4801 | if (head->must_insert_reserved) |
| 4802 | ret = 1; |
| 4803 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 4804 | btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4805 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 4806 | btrfs_put_delayed_ref_head(head); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4807 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4808 | out: |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 4809 | spin_unlock(&head->lock); |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 4810 | |
| 4811 | out_delayed_unlock: |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4812 | spin_unlock(&delayed_refs->lock); |
| 4813 | return 0; |
| 4814 | } |
| 4815 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4816 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 4817 | struct btrfs_root *root, |
| 4818 | struct extent_buffer *buf, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 4819 | u64 parent, int last_ref) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4820 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4821 | struct btrfs_fs_info *fs_info = root->fs_info; |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4822 | struct btrfs_ref generic_ref = { 0 }; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4823 | int pin = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4824 | int ret; |
| 4825 | |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4826 | btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF, |
| 4827 | buf->start, buf->len, parent); |
| 4828 | btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf), |
| 4829 | root->root_key.objectid); |
| 4830 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4831 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 4832 | int old_ref_mod, new_ref_mod; |
| 4833 | |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 4834 | btrfs_ref_tree_mod(fs_info, &generic_ref); |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4835 | ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 4836 | &old_ref_mod, &new_ref_mod); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4837 | BUG_ON(ret); /* -ENOMEM */ |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 4838 | pin = old_ref_mod >= 0 && new_ref_mod < 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4839 | } |
| 4840 | |
Omar Sandoval | 0a16c7d | 2017-06-06 16:45:29 -0700 | [diff] [blame] | 4841 | if (last_ref && btrfs_header_generation(buf) == trans->transid) { |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 4842 | struct btrfs_block_group_cache *cache; |
| 4843 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4844 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4845 | ret = check_ref_cleanup(trans, buf->start); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4846 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 4847 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4848 | } |
| 4849 | |
Omar Sandoval | 4da8b76 | 2017-06-06 16:45:28 -0700 | [diff] [blame] | 4850 | pin = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4851 | cache = btrfs_lookup_block_group(fs_info, buf->start); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 4852 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4853 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 4854 | pin_down_extent(cache, buf->start, buf->len, 1); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 4855 | btrfs_put_block_group(cache); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 4856 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4857 | } |
| 4858 | |
| 4859 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 4860 | |
| 4861 | btrfs_add_free_space(cache, buf->start, buf->len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 4862 | btrfs_free_reserved_bytes(cache, buf->len, 0); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 4863 | btrfs_put_block_group(cache); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 4864 | trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4865 | } |
| 4866 | out: |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4867 | if (pin) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 4868 | add_pinned_bytes(fs_info, &generic_ref); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4869 | |
Omar Sandoval | 0a16c7d | 2017-06-06 16:45:29 -0700 | [diff] [blame] | 4870 | if (last_ref) { |
| 4871 | /* |
| 4872 | * Deleting the buffer, clear the corrupt flag since it doesn't |
| 4873 | * matter anymore. |
| 4874 | */ |
| 4875 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
| 4876 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4877 | } |
| 4878 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4879 | /* Can return -ENOMEM */ |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4880 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4881 | { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4882 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 4883 | int old_ref_mod, new_ref_mod; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4884 | int ret; |
| 4885 | |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 4886 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4887 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 4888 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4889 | /* |
| 4890 | * tree log blocks never actually go into the extent allocation |
| 4891 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4892 | */ |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4893 | if ((ref->type == BTRFS_REF_METADATA && |
| 4894 | ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) || |
| 4895 | (ref->type == BTRFS_REF_DATA && |
| 4896 | ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4897 | /* unlocks the pinned mutex */ |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4898 | btrfs_pin_extent(fs_info, ref->bytenr, ref->len, 1); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 4899 | old_ref_mod = new_ref_mod = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4900 | ret = 0; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4901 | } else if (ref->type == BTRFS_REF_METADATA) { |
| 4902 | ret = btrfs_add_delayed_tree_ref(trans, ref, NULL, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 4903 | &old_ref_mod, &new_ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4904 | } else { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4905 | ret = btrfs_add_delayed_data_ref(trans, ref, 0, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 4906 | &old_ref_mod, &new_ref_mod); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4907 | } |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 4908 | |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4909 | if (!((ref->type == BTRFS_REF_METADATA && |
| 4910 | ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) || |
| 4911 | (ref->type == BTRFS_REF_DATA && |
| 4912 | ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID))) |
| 4913 | btrfs_ref_tree_mod(fs_info, ref); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 4914 | |
Qu Wenruo | ddf30cf | 2019-04-04 14:45:34 +0800 | [diff] [blame] | 4915 | if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 4916 | add_pinned_bytes(fs_info, ref); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 4917 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4918 | return ret; |
| 4919 | } |
| 4920 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4921 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4922 | * when we wait for progress in the block group caching, its because |
| 4923 | * our allocation attempt failed at least once. So, we must sleep |
| 4924 | * and let some progress happen before we try again. |
| 4925 | * |
| 4926 | * This function will sleep at least once waiting for new free space to |
| 4927 | * show up, and then it will check the block group free space numbers |
| 4928 | * for our min num_bytes. Another option is to have it go ahead |
| 4929 | * and look in the rbtree for a free extent of a given size, but this |
| 4930 | * is a good start. |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 4931 | * |
| 4932 | * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using |
| 4933 | * any of the information in this block group. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4934 | */ |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 4935 | void btrfs_wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 4936 | u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4937 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4938 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4939 | |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 4940 | caching_ctl = btrfs_get_caching_control(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4941 | if (!caching_ctl) |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 4942 | return; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4943 | |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 4944 | wait_event(caching_ctl->wait, btrfs_block_group_cache_done(cache) || |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 4945 | (cache->free_space_ctl->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4946 | |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 4947 | btrfs_put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4948 | } |
| 4949 | |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 4950 | int btrfs_wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4951 | { |
| 4952 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 4953 | int ret = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4954 | |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 4955 | caching_ctl = btrfs_get_caching_control(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4956 | if (!caching_ctl) |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 4957 | return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4958 | |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 4959 | wait_event(caching_ctl->wait, btrfs_block_group_cache_done(cache)); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 4960 | if (cache->cached == BTRFS_CACHE_ERROR) |
| 4961 | ret = -EIO; |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 4962 | btrfs_put_caching_control(caching_ctl); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 4963 | return ret; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4964 | } |
| 4965 | |
| 4966 | enum btrfs_loop_type { |
David Sterba | f262fa8 | 2019-06-18 20:00:08 +0200 | [diff] [blame] | 4967 | LOOP_CACHING_NOWAIT, |
| 4968 | LOOP_CACHING_WAIT, |
| 4969 | LOOP_ALLOC_CHUNK, |
| 4970 | LOOP_NO_EMPTY_SIZE, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4971 | }; |
| 4972 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4973 | static inline void |
| 4974 | btrfs_lock_block_group(struct btrfs_block_group_cache *cache, |
| 4975 | int delalloc) |
| 4976 | { |
| 4977 | if (delalloc) |
| 4978 | down_read(&cache->data_rwsem); |
| 4979 | } |
| 4980 | |
| 4981 | static inline void |
| 4982 | btrfs_grab_block_group(struct btrfs_block_group_cache *cache, |
| 4983 | int delalloc) |
| 4984 | { |
| 4985 | btrfs_get_block_group(cache); |
| 4986 | if (delalloc) |
| 4987 | down_read(&cache->data_rwsem); |
| 4988 | } |
| 4989 | |
| 4990 | static struct btrfs_block_group_cache * |
| 4991 | btrfs_lock_cluster(struct btrfs_block_group_cache *block_group, |
| 4992 | struct btrfs_free_cluster *cluster, |
| 4993 | int delalloc) |
| 4994 | { |
Sudip Mukherjee | 89771cc | 2016-02-16 13:32:47 +0530 | [diff] [blame] | 4995 | struct btrfs_block_group_cache *used_bg = NULL; |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 4996 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4997 | spin_lock(&cluster->refill_lock); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 4998 | while (1) { |
| 4999 | used_bg = cluster->block_group; |
| 5000 | if (!used_bg) |
| 5001 | return NULL; |
| 5002 | |
| 5003 | if (used_bg == block_group) |
| 5004 | return used_bg; |
| 5005 | |
| 5006 | btrfs_get_block_group(used_bg); |
| 5007 | |
| 5008 | if (!delalloc) |
| 5009 | return used_bg; |
| 5010 | |
| 5011 | if (down_read_trylock(&used_bg->data_rwsem)) |
| 5012 | return used_bg; |
| 5013 | |
| 5014 | spin_unlock(&cluster->refill_lock); |
| 5015 | |
Liu Bo | e321f8a | 2016-11-30 16:11:04 -0800 | [diff] [blame] | 5016 | /* We should only have one-level nested. */ |
| 5017 | down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 5018 | |
| 5019 | spin_lock(&cluster->refill_lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 5020 | if (used_bg == cluster->block_group) |
| 5021 | return used_bg; |
| 5022 | |
| 5023 | up_read(&used_bg->data_rwsem); |
| 5024 | btrfs_put_block_group(used_bg); |
| 5025 | } |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 5026 | } |
| 5027 | |
| 5028 | static inline void |
| 5029 | btrfs_release_block_group(struct btrfs_block_group_cache *cache, |
| 5030 | int delalloc) |
| 5031 | { |
| 5032 | if (delalloc) |
| 5033 | up_read(&cache->data_rwsem); |
| 5034 | btrfs_put_block_group(cache); |
| 5035 | } |
| 5036 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5037 | /* |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5038 | * Structure used internally for find_free_extent() function. Wraps needed |
| 5039 | * parameters. |
| 5040 | */ |
| 5041 | struct find_free_extent_ctl { |
| 5042 | /* Basic allocation info */ |
| 5043 | u64 ram_bytes; |
| 5044 | u64 num_bytes; |
| 5045 | u64 empty_size; |
| 5046 | u64 flags; |
| 5047 | int delalloc; |
| 5048 | |
| 5049 | /* Where to start the search inside the bg */ |
| 5050 | u64 search_start; |
| 5051 | |
| 5052 | /* For clustered allocation */ |
| 5053 | u64 empty_cluster; |
| 5054 | |
| 5055 | bool have_caching_bg; |
| 5056 | bool orig_have_caching_bg; |
| 5057 | |
| 5058 | /* RAID index, converted from flags */ |
| 5059 | int index; |
| 5060 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 5061 | /* |
| 5062 | * Current loop number, check find_free_extent_update_loop() for details |
| 5063 | */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5064 | int loop; |
| 5065 | |
| 5066 | /* |
| 5067 | * Whether we're refilling a cluster, if true we need to re-search |
| 5068 | * current block group but don't try to refill the cluster again. |
| 5069 | */ |
| 5070 | bool retry_clustered; |
| 5071 | |
| 5072 | /* |
| 5073 | * Whether we're updating free space cache, if true we need to re-search |
| 5074 | * current block group but don't try updating free space cache again. |
| 5075 | */ |
| 5076 | bool retry_unclustered; |
| 5077 | |
| 5078 | /* If current block group is cached */ |
| 5079 | int cached; |
| 5080 | |
| 5081 | /* Max contiguous hole found */ |
| 5082 | u64 max_extent_size; |
| 5083 | |
| 5084 | /* Total free space from free space cache, not always contiguous */ |
| 5085 | u64 total_free_space; |
| 5086 | |
| 5087 | /* Found result */ |
| 5088 | u64 found_offset; |
| 5089 | }; |
| 5090 | |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 5091 | |
| 5092 | /* |
| 5093 | * Helper function for find_free_extent(). |
| 5094 | * |
| 5095 | * Return -ENOENT to inform caller that we need fallback to unclustered mode. |
| 5096 | * Return -EAGAIN to inform caller that we need to re-search this block group |
| 5097 | * Return >0 to inform caller that we find nothing |
| 5098 | * Return 0 means we have found a location and set ffe_ctl->found_offset. |
| 5099 | */ |
| 5100 | static int find_free_extent_clustered(struct btrfs_block_group_cache *bg, |
| 5101 | struct btrfs_free_cluster *last_ptr, |
| 5102 | struct find_free_extent_ctl *ffe_ctl, |
| 5103 | struct btrfs_block_group_cache **cluster_bg_ret) |
| 5104 | { |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 5105 | struct btrfs_block_group_cache *cluster_bg; |
| 5106 | u64 aligned_cluster; |
| 5107 | u64 offset; |
| 5108 | int ret; |
| 5109 | |
| 5110 | cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc); |
| 5111 | if (!cluster_bg) |
| 5112 | goto refill_cluster; |
| 5113 | if (cluster_bg != bg && (cluster_bg->ro || |
| 5114 | !block_group_bits(cluster_bg, ffe_ctl->flags))) |
| 5115 | goto release_cluster; |
| 5116 | |
| 5117 | offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr, |
| 5118 | ffe_ctl->num_bytes, cluster_bg->key.objectid, |
| 5119 | &ffe_ctl->max_extent_size); |
| 5120 | if (offset) { |
| 5121 | /* We have a block, we're done */ |
| 5122 | spin_unlock(&last_ptr->refill_lock); |
| 5123 | trace_btrfs_reserve_extent_cluster(cluster_bg, |
| 5124 | ffe_ctl->search_start, ffe_ctl->num_bytes); |
| 5125 | *cluster_bg_ret = cluster_bg; |
| 5126 | ffe_ctl->found_offset = offset; |
| 5127 | return 0; |
| 5128 | } |
| 5129 | WARN_ON(last_ptr->block_group != cluster_bg); |
| 5130 | |
| 5131 | release_cluster: |
| 5132 | /* |
| 5133 | * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so |
| 5134 | * lets just skip it and let the allocator find whatever block it can |
| 5135 | * find. If we reach this point, we will have tried the cluster |
| 5136 | * allocator plenty of times and not have found anything, so we are |
| 5137 | * likely way too fragmented for the clustering stuff to find anything. |
| 5138 | * |
| 5139 | * However, if the cluster is taken from the current block group, |
| 5140 | * release the cluster first, so that we stand a better chance of |
| 5141 | * succeeding in the unclustered allocation. |
| 5142 | */ |
| 5143 | if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) { |
| 5144 | spin_unlock(&last_ptr->refill_lock); |
| 5145 | btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc); |
| 5146 | return -ENOENT; |
| 5147 | } |
| 5148 | |
| 5149 | /* This cluster didn't work out, free it and start over */ |
| 5150 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 5151 | |
| 5152 | if (cluster_bg != bg) |
| 5153 | btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc); |
| 5154 | |
| 5155 | refill_cluster: |
| 5156 | if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) { |
| 5157 | spin_unlock(&last_ptr->refill_lock); |
| 5158 | return -ENOENT; |
| 5159 | } |
| 5160 | |
| 5161 | aligned_cluster = max_t(u64, |
| 5162 | ffe_ctl->empty_cluster + ffe_ctl->empty_size, |
| 5163 | bg->full_stripe_len); |
David Sterba | 2ceeae2 | 2019-03-20 13:53:49 +0100 | [diff] [blame] | 5164 | ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start, |
| 5165 | ffe_ctl->num_bytes, aligned_cluster); |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 5166 | if (ret == 0) { |
| 5167 | /* Now pull our allocation out of this cluster */ |
| 5168 | offset = btrfs_alloc_from_cluster(bg, last_ptr, |
| 5169 | ffe_ctl->num_bytes, ffe_ctl->search_start, |
| 5170 | &ffe_ctl->max_extent_size); |
| 5171 | if (offset) { |
| 5172 | /* We found one, proceed */ |
| 5173 | spin_unlock(&last_ptr->refill_lock); |
| 5174 | trace_btrfs_reserve_extent_cluster(bg, |
| 5175 | ffe_ctl->search_start, |
| 5176 | ffe_ctl->num_bytes); |
| 5177 | ffe_ctl->found_offset = offset; |
| 5178 | return 0; |
| 5179 | } |
| 5180 | } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT && |
| 5181 | !ffe_ctl->retry_clustered) { |
| 5182 | spin_unlock(&last_ptr->refill_lock); |
| 5183 | |
| 5184 | ffe_ctl->retry_clustered = true; |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 5185 | btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes + |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 5186 | ffe_ctl->empty_cluster + ffe_ctl->empty_size); |
| 5187 | return -EAGAIN; |
| 5188 | } |
| 5189 | /* |
| 5190 | * At this point we either didn't find a cluster or we weren't able to |
| 5191 | * allocate a block from our cluster. Free the cluster we've been |
| 5192 | * trying to use, and go to the next block group. |
| 5193 | */ |
| 5194 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 5195 | spin_unlock(&last_ptr->refill_lock); |
| 5196 | return 1; |
| 5197 | } |
| 5198 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5199 | /* |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 5200 | * Return >0 to inform caller that we find nothing |
| 5201 | * Return 0 when we found an free extent and set ffe_ctrl->found_offset |
| 5202 | * Return -EAGAIN to inform caller that we need to re-search this block group |
| 5203 | */ |
| 5204 | static int find_free_extent_unclustered(struct btrfs_block_group_cache *bg, |
| 5205 | struct btrfs_free_cluster *last_ptr, |
| 5206 | struct find_free_extent_ctl *ffe_ctl) |
| 5207 | { |
| 5208 | u64 offset; |
| 5209 | |
| 5210 | /* |
| 5211 | * We are doing an unclustered allocation, set the fragmented flag so |
| 5212 | * we don't bother trying to setup a cluster again until we get more |
| 5213 | * space. |
| 5214 | */ |
| 5215 | if (unlikely(last_ptr)) { |
| 5216 | spin_lock(&last_ptr->lock); |
| 5217 | last_ptr->fragmented = 1; |
| 5218 | spin_unlock(&last_ptr->lock); |
| 5219 | } |
| 5220 | if (ffe_ctl->cached) { |
| 5221 | struct btrfs_free_space_ctl *free_space_ctl; |
| 5222 | |
| 5223 | free_space_ctl = bg->free_space_ctl; |
| 5224 | spin_lock(&free_space_ctl->tree_lock); |
| 5225 | if (free_space_ctl->free_space < |
| 5226 | ffe_ctl->num_bytes + ffe_ctl->empty_cluster + |
| 5227 | ffe_ctl->empty_size) { |
| 5228 | ffe_ctl->total_free_space = max_t(u64, |
| 5229 | ffe_ctl->total_free_space, |
| 5230 | free_space_ctl->free_space); |
| 5231 | spin_unlock(&free_space_ctl->tree_lock); |
| 5232 | return 1; |
| 5233 | } |
| 5234 | spin_unlock(&free_space_ctl->tree_lock); |
| 5235 | } |
| 5236 | |
| 5237 | offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start, |
| 5238 | ffe_ctl->num_bytes, ffe_ctl->empty_size, |
| 5239 | &ffe_ctl->max_extent_size); |
| 5240 | |
| 5241 | /* |
| 5242 | * If we didn't find a chunk, and we haven't failed on this block group |
| 5243 | * before, and this block group is in the middle of caching and we are |
| 5244 | * ok with waiting, then go ahead and wait for progress to be made, and |
| 5245 | * set @retry_unclustered to true. |
| 5246 | * |
| 5247 | * If @retry_unclustered is true then we've already waited on this |
| 5248 | * block group once and should move on to the next block group. |
| 5249 | */ |
| 5250 | if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached && |
| 5251 | ffe_ctl->loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 5252 | btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes + |
| 5253 | ffe_ctl->empty_size); |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 5254 | ffe_ctl->retry_unclustered = true; |
| 5255 | return -EAGAIN; |
| 5256 | } else if (!offset) { |
| 5257 | return 1; |
| 5258 | } |
| 5259 | ffe_ctl->found_offset = offset; |
| 5260 | return 0; |
| 5261 | } |
| 5262 | |
| 5263 | /* |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 5264 | * Return >0 means caller needs to re-search for free extent |
| 5265 | * Return 0 means we have the needed free extent. |
| 5266 | * Return <0 means we failed to locate any free extent. |
| 5267 | */ |
| 5268 | static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info, |
| 5269 | struct btrfs_free_cluster *last_ptr, |
| 5270 | struct btrfs_key *ins, |
| 5271 | struct find_free_extent_ctl *ffe_ctl, |
| 5272 | int full_search, bool use_cluster) |
| 5273 | { |
| 5274 | struct btrfs_root *root = fs_info->extent_root; |
| 5275 | int ret; |
| 5276 | |
| 5277 | if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) && |
| 5278 | ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg) |
| 5279 | ffe_ctl->orig_have_caching_bg = true; |
| 5280 | |
| 5281 | if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT && |
| 5282 | ffe_ctl->have_caching_bg) |
| 5283 | return 1; |
| 5284 | |
| 5285 | if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES) |
| 5286 | return 1; |
| 5287 | |
| 5288 | if (ins->objectid) { |
| 5289 | if (!use_cluster && last_ptr) { |
| 5290 | spin_lock(&last_ptr->lock); |
| 5291 | last_ptr->window_start = ins->objectid; |
| 5292 | spin_unlock(&last_ptr->lock); |
| 5293 | } |
| 5294 | return 0; |
| 5295 | } |
| 5296 | |
| 5297 | /* |
| 5298 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 5299 | * caching kthreads as we move along |
| 5300 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 5301 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 5302 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 5303 | * again |
| 5304 | */ |
| 5305 | if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) { |
| 5306 | ffe_ctl->index = 0; |
| 5307 | if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) { |
| 5308 | /* |
| 5309 | * We want to skip the LOOP_CACHING_WAIT step if we |
| 5310 | * don't have any uncached bgs and we've already done a |
| 5311 | * full search through. |
| 5312 | */ |
| 5313 | if (ffe_ctl->orig_have_caching_bg || !full_search) |
| 5314 | ffe_ctl->loop = LOOP_CACHING_WAIT; |
| 5315 | else |
| 5316 | ffe_ctl->loop = LOOP_ALLOC_CHUNK; |
| 5317 | } else { |
| 5318 | ffe_ctl->loop++; |
| 5319 | } |
| 5320 | |
| 5321 | if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) { |
| 5322 | struct btrfs_trans_handle *trans; |
| 5323 | int exist = 0; |
| 5324 | |
| 5325 | trans = current->journal_info; |
| 5326 | if (trans) |
| 5327 | exist = 1; |
| 5328 | else |
| 5329 | trans = btrfs_join_transaction(root); |
| 5330 | |
| 5331 | if (IS_ERR(trans)) { |
| 5332 | ret = PTR_ERR(trans); |
| 5333 | return ret; |
| 5334 | } |
| 5335 | |
Josef Bacik | fc471cb | 2019-06-18 16:09:17 -0400 | [diff] [blame] | 5336 | ret = btrfs_chunk_alloc(trans, ffe_ctl->flags, |
| 5337 | CHUNK_ALLOC_FORCE); |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 5338 | |
| 5339 | /* |
| 5340 | * If we can't allocate a new chunk we've already looped |
| 5341 | * through at least once, move on to the NO_EMPTY_SIZE |
| 5342 | * case. |
| 5343 | */ |
| 5344 | if (ret == -ENOSPC) |
| 5345 | ffe_ctl->loop = LOOP_NO_EMPTY_SIZE; |
| 5346 | |
| 5347 | /* Do not bail out on ENOSPC since we can do more. */ |
| 5348 | if (ret < 0 && ret != -ENOSPC) |
| 5349 | btrfs_abort_transaction(trans, ret); |
| 5350 | else |
| 5351 | ret = 0; |
| 5352 | if (!exist) |
| 5353 | btrfs_end_transaction(trans); |
| 5354 | if (ret) |
| 5355 | return ret; |
| 5356 | } |
| 5357 | |
| 5358 | if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) { |
| 5359 | /* |
| 5360 | * Don't loop again if we already have no empty_size and |
| 5361 | * no empty_cluster. |
| 5362 | */ |
| 5363 | if (ffe_ctl->empty_size == 0 && |
| 5364 | ffe_ctl->empty_cluster == 0) |
| 5365 | return -ENOSPC; |
| 5366 | ffe_ctl->empty_size = 0; |
| 5367 | ffe_ctl->empty_cluster = 0; |
| 5368 | } |
| 5369 | return 1; |
| 5370 | } |
| 5371 | return -ENOSPC; |
| 5372 | } |
| 5373 | |
| 5374 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5375 | * walks the btree of allocated extents and find a hole of a given size. |
| 5376 | * The key ins is changed to record the hole: |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 5377 | * ins->objectid == start position |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 5378 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 5379 | * ins->offset == the size of the hole. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5380 | * Any available blocks before search_start are skipped. |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 5381 | * |
| 5382 | * If there is no suitable free space, we will record the max size of |
| 5383 | * the free space extent currently. |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 5384 | * |
| 5385 | * The overall logic and call chain: |
| 5386 | * |
| 5387 | * find_free_extent() |
| 5388 | * |- Iterate through all block groups |
| 5389 | * | |- Get a valid block group |
| 5390 | * | |- Try to do clustered allocation in that block group |
| 5391 | * | |- Try to do unclustered allocation in that block group |
| 5392 | * | |- Check if the result is valid |
| 5393 | * | | |- If valid, then exit |
| 5394 | * | |- Jump to next block group |
| 5395 | * | |
| 5396 | * |- Push harder to find free extents |
| 5397 | * |- If not found, re-iterate all block groups |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5398 | */ |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 5399 | static noinline int find_free_extent(struct btrfs_fs_info *fs_info, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 5400 | u64 ram_bytes, u64 num_bytes, u64 empty_size, |
| 5401 | u64 hint_byte, struct btrfs_key *ins, |
| 5402 | u64 flags, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5403 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5404 | int ret = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5405 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5406 | struct btrfs_block_group_cache *block_group = NULL; |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5407 | struct find_free_extent_ctl ffe_ctl = {0}; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5408 | struct btrfs_space_info *space_info; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5409 | bool use_cluster = true; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 5410 | bool full_search = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5411 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5412 | WARN_ON(num_bytes < fs_info->sectorsize); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5413 | |
| 5414 | ffe_ctl.ram_bytes = ram_bytes; |
| 5415 | ffe_ctl.num_bytes = num_bytes; |
| 5416 | ffe_ctl.empty_size = empty_size; |
| 5417 | ffe_ctl.flags = flags; |
| 5418 | ffe_ctl.search_start = 0; |
| 5419 | ffe_ctl.retry_clustered = false; |
| 5420 | ffe_ctl.retry_unclustered = false; |
| 5421 | ffe_ctl.delalloc = delalloc; |
| 5422 | ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags); |
| 5423 | ffe_ctl.have_caching_bg = false; |
| 5424 | ffe_ctl.orig_have_caching_bg = false; |
| 5425 | ffe_ctl.found_offset = 0; |
| 5426 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 5427 | ins->type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5428 | ins->objectid = 0; |
| 5429 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 5430 | |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 5431 | trace_find_free_extent(fs_info, num_bytes, empty_size, flags); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 5432 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 5433 | space_info = btrfs_find_space_info(fs_info, flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 5434 | if (!space_info) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5435 | btrfs_err(fs_info, "No space info for %llu", flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 5436 | return -ENOSPC; |
| 5437 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5438 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5439 | /* |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 5440 | * If our free space is heavily fragmented we may not be able to make |
| 5441 | * big contiguous allocations, so instead of doing the expensive search |
| 5442 | * for free space, simply return ENOSPC with our max_extent_size so we |
| 5443 | * can go ahead and search for a more manageable chunk. |
| 5444 | * |
| 5445 | * If our max_extent_size is large enough for our allocation simply |
| 5446 | * disable clustering since we will likely not be able to find enough |
| 5447 | * space to create a cluster and induce latency trying. |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5448 | */ |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 5449 | if (unlikely(space_info->max_extent_size)) { |
| 5450 | spin_lock(&space_info->lock); |
| 5451 | if (space_info->max_extent_size && |
| 5452 | num_bytes > space_info->max_extent_size) { |
| 5453 | ins->offset = space_info->max_extent_size; |
| 5454 | spin_unlock(&space_info->lock); |
| 5455 | return -ENOSPC; |
| 5456 | } else if (space_info->max_extent_size) { |
| 5457 | use_cluster = false; |
| 5458 | } |
| 5459 | spin_unlock(&space_info->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5460 | } |
| 5461 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5462 | last_ptr = fetch_cluster_info(fs_info, space_info, |
| 5463 | &ffe_ctl.empty_cluster); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5464 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5465 | spin_lock(&last_ptr->lock); |
| 5466 | if (last_ptr->block_group) |
| 5467 | hint_byte = last_ptr->window_start; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 5468 | if (last_ptr->fragmented) { |
| 5469 | /* |
| 5470 | * We still set window_start so we can keep track of the |
| 5471 | * last place we found an allocation to try and save |
| 5472 | * some time. |
| 5473 | */ |
| 5474 | hint_byte = last_ptr->window_start; |
| 5475 | use_cluster = false; |
| 5476 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5477 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5478 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5479 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5480 | ffe_ctl.search_start = max(ffe_ctl.search_start, |
| 5481 | first_logical_byte(fs_info, 0)); |
| 5482 | ffe_ctl.search_start = max(ffe_ctl.search_start, hint_byte); |
| 5483 | if (ffe_ctl.search_start == hint_byte) { |
| 5484 | block_group = btrfs_lookup_block_group(fs_info, |
| 5485 | ffe_ctl.search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5486 | /* |
| 5487 | * we don't want to use the block group if it doesn't match our |
| 5488 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5489 | * |
| 5490 | * However if we are re-searching with an ideal block group |
| 5491 | * picked out then we don't care that the block group is cached. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5492 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 5493 | if (block_group && block_group_bits(block_group, flags) && |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 5494 | block_group->cached != BTRFS_CACHE_NO) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5495 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5496 | if (list_empty(&block_group->list) || |
| 5497 | block_group->ro) { |
| 5498 | /* |
| 5499 | * someone is removing this block group, |
| 5500 | * we can't jump into the have_block_group |
| 5501 | * target because our list pointers are not |
| 5502 | * valid |
| 5503 | */ |
| 5504 | btrfs_put_block_group(block_group); |
| 5505 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5506 | } else { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5507 | ffe_ctl.index = btrfs_bg_flags_to_raid_index( |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 5508 | block_group->flags); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 5509 | btrfs_lock_block_group(block_group, delalloc); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5510 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5511 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5512 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5513 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5514 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5515 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5516 | search: |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5517 | ffe_ctl.have_caching_bg = false; |
| 5518 | if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) || |
| 5519 | ffe_ctl.index == 0) |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 5520 | full_search = true; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5521 | down_read(&space_info->groups_sem); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5522 | list_for_each_entry(block_group, |
| 5523 | &space_info->block_groups[ffe_ctl.index], list) { |
Jeff Mahoney | 1444393 | 2017-07-19 23:25:51 -0400 | [diff] [blame] | 5524 | /* If the block group is read-only, we can skip it entirely. */ |
| 5525 | if (unlikely(block_group->ro)) |
| 5526 | continue; |
| 5527 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 5528 | btrfs_grab_block_group(block_group, delalloc); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5529 | ffe_ctl.search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5530 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 5531 | /* |
| 5532 | * this can happen if we end up cycling through all the |
| 5533 | * raid types, but we want to make sure we only allocate |
| 5534 | * for the proper type. |
| 5535 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 5536 | if (!block_group_bits(block_group, flags)) { |
Bart Van Assche | bece2e8 | 2018-06-20 10:03:31 -0700 | [diff] [blame] | 5537 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5538 | BTRFS_BLOCK_GROUP_RAID1_MASK | |
David Sterba | a07e8a4 | 2019-05-31 16:54:26 +0200 | [diff] [blame] | 5539 | BTRFS_BLOCK_GROUP_RAID56_MASK | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 5540 | BTRFS_BLOCK_GROUP_RAID10; |
| 5541 | |
| 5542 | /* |
| 5543 | * if they asked for extra copies and this block group |
| 5544 | * doesn't provide them, bail. This does allow us to |
| 5545 | * fill raid0 from raid1. |
| 5546 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 5547 | if ((flags & extra) && !(block_group->flags & extra)) |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 5548 | goto loop; |
Qu Wenruo | 2a28468 | 2019-07-16 17:00:33 +0800 | [diff] [blame] | 5549 | |
| 5550 | /* |
| 5551 | * This block group has different flags than we want. |
| 5552 | * It's possible that we have MIXED_GROUP flag but no |
| 5553 | * block group is mixed. Just skip such block group. |
| 5554 | */ |
| 5555 | btrfs_release_block_group(block_group, delalloc); |
| 5556 | continue; |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 5557 | } |
| 5558 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5559 | have_block_group: |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 5560 | ffe_ctl.cached = btrfs_block_group_cache_done(block_group); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5561 | if (unlikely(!ffe_ctl.cached)) { |
| 5562 | ffe_ctl.have_caching_bg = true; |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 5563 | ret = btrfs_cache_block_group(block_group, 0); |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 5564 | BUG_ON(ret < 0); |
| 5565 | ret = 0; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5566 | } |
| 5567 | |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 5568 | if (unlikely(block_group->cached == BTRFS_CACHE_ERROR)) |
| 5569 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5570 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5571 | /* |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 5572 | * Ok we want to try and use the cluster allocator, so |
| 5573 | * lets look there |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5574 | */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 5575 | if (last_ptr && use_cluster) { |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 5576 | struct btrfs_block_group_cache *cluster_bg = NULL; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5577 | |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 5578 | ret = find_free_extent_clustered(block_group, last_ptr, |
| 5579 | &ffe_ctl, &cluster_bg); |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5580 | |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 5581 | if (ret == 0) { |
| 5582 | if (cluster_bg && cluster_bg != block_group) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 5583 | btrfs_release_block_group(block_group, |
| 5584 | delalloc); |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 5585 | block_group = cluster_bg; |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 5586 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5587 | goto checks; |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 5588 | } else if (ret == -EAGAIN) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5589 | goto have_block_group; |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 5590 | } else if (ret > 0) { |
| 5591 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5592 | } |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 5593 | /* ret == -ENOENT case falls through */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5594 | } |
| 5595 | |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 5596 | ret = find_free_extent_unclustered(block_group, last_ptr, |
| 5597 | &ffe_ctl); |
| 5598 | if (ret == -EAGAIN) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5599 | goto have_block_group; |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 5600 | else if (ret > 0) |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5601 | goto loop; |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 5602 | /* ret == 0 case falls through */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5603 | checks: |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5604 | ffe_ctl.search_start = round_up(ffe_ctl.found_offset, |
| 5605 | fs_info->stripesize); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 5606 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5607 | /* move on to the next group */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5608 | if (ffe_ctl.search_start + num_bytes > |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 5609 | block_group->key.objectid + block_group->key.offset) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5610 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 5611 | num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5612 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5613 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5614 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5615 | if (ffe_ctl.found_offset < ffe_ctl.search_start) |
| 5616 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 5617 | ffe_ctl.search_start - ffe_ctl.found_offset); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5618 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 5619 | ret = btrfs_add_reserved_bytes(block_group, ram_bytes, |
| 5620 | num_bytes, delalloc); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5621 | if (ret == -EAGAIN) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5622 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 5623 | num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5624 | goto loop; |
| 5625 | } |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 5626 | btrfs_inc_block_group_reservations(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5627 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5628 | /* we are all good, lets return */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5629 | ins->objectid = ffe_ctl.search_start; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5630 | ins->offset = num_bytes; |
| 5631 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5632 | trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start, |
| 5633 | num_bytes); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 5634 | btrfs_release_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5635 | break; |
| 5636 | loop: |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5637 | ffe_ctl.retry_clustered = false; |
| 5638 | ffe_ctl.retry_unclustered = false; |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 5639 | BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) != |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5640 | ffe_ctl.index); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 5641 | btrfs_release_block_group(block_group, delalloc); |
Jeff Mahoney | 1444393 | 2017-07-19 23:25:51 -0400 | [diff] [blame] | 5642 | cond_resched(); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5643 | } |
| 5644 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 5645 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 5646 | ret = find_free_extent_update_loop(fs_info, last_ptr, ins, &ffe_ctl, |
| 5647 | full_search, use_cluster); |
| 5648 | if (ret > 0) |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 5649 | goto search; |
| 5650 | |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 5651 | if (ret == -ENOSPC) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5652 | /* |
| 5653 | * Use ffe_ctl->total_free_space as fallback if we can't find |
| 5654 | * any contiguous hole. |
| 5655 | */ |
| 5656 | if (!ffe_ctl.max_extent_size) |
| 5657 | ffe_ctl.max_extent_size = ffe_ctl.total_free_space; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 5658 | spin_lock(&space_info->lock); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5659 | space_info->max_extent_size = ffe_ctl.max_extent_size; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 5660 | spin_unlock(&space_info->lock); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 5661 | ins->offset = ffe_ctl.max_extent_size; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 5662 | } |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5663 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5664 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5665 | |
Nikolay Borisov | 6f47c70 | 2018-03-13 12:22:32 +0200 | [diff] [blame] | 5666 | /* |
| 5667 | * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a |
| 5668 | * hole that is at least as big as @num_bytes. |
| 5669 | * |
| 5670 | * @root - The root that will contain this extent |
| 5671 | * |
| 5672 | * @ram_bytes - The amount of space in ram that @num_bytes take. This |
| 5673 | * is used for accounting purposes. This value differs |
| 5674 | * from @num_bytes only in the case of compressed extents. |
| 5675 | * |
| 5676 | * @num_bytes - Number of bytes to allocate on-disk. |
| 5677 | * |
| 5678 | * @min_alloc_size - Indicates the minimum amount of space that the |
| 5679 | * allocator should try to satisfy. In some cases |
| 5680 | * @num_bytes may be larger than what is required and if |
| 5681 | * the filesystem is fragmented then allocation fails. |
| 5682 | * However, the presence of @min_alloc_size gives a |
| 5683 | * chance to try and satisfy the smaller allocation. |
| 5684 | * |
| 5685 | * @empty_size - A hint that you plan on doing more COW. This is the |
| 5686 | * size in bytes the allocator should try to find free |
| 5687 | * next to the block it returns. This is just a hint and |
| 5688 | * may be ignored by the allocator. |
| 5689 | * |
| 5690 | * @hint_byte - Hint to the allocator to start searching above the byte |
| 5691 | * address passed. It might be ignored. |
| 5692 | * |
| 5693 | * @ins - This key is modified to record the found hole. It will |
| 5694 | * have the following values: |
| 5695 | * ins->objectid == start position |
| 5696 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
| 5697 | * ins->offset == the size of the hole. |
| 5698 | * |
| 5699 | * @is_data - Boolean flag indicating whether an extent is |
| 5700 | * allocated for data (true) or metadata (false) |
| 5701 | * |
| 5702 | * @delalloc - Boolean flag indicating whether this allocation is for |
| 5703 | * delalloc or not. If 'true' data_rwsem of block groups |
| 5704 | * is going to be acquired. |
| 5705 | * |
| 5706 | * |
| 5707 | * Returns 0 when an allocation succeeded or < 0 when an error occurred. In |
| 5708 | * case -ENOSPC is returned then @ins->offset will contain the size of the |
| 5709 | * largest available hole the allocator managed to find. |
| 5710 | */ |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 5711 | int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5712 | u64 num_bytes, u64 min_alloc_size, |
| 5713 | u64 empty_size, u64 hint_byte, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 5714 | struct btrfs_key *ins, int is_data, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5715 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5716 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 36af4e0 | 2015-09-25 16:13:11 -0400 | [diff] [blame] | 5717 | bool final_tried = num_bytes == min_alloc_size; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 5718 | u64 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5719 | int ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5720 | |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5721 | flags = get_alloc_profile_by_root(root, is_data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5722 | again: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5723 | WARN_ON(num_bytes < fs_info->sectorsize); |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 5724 | ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 5725 | hint_byte, ins, flags, delalloc); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 5726 | if (!ret && !is_data) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5727 | btrfs_dec_block_group_reservations(fs_info, ins->objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 5728 | } else if (ret == -ENOSPC) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 5729 | if (!final_tried && ins->offset) { |
| 5730 | num_bytes = min(num_bytes >> 1, ins->offset); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 5731 | num_bytes = round_down(num_bytes, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5732 | fs_info->sectorsize); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 5733 | num_bytes = max(num_bytes, min_alloc_size); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 5734 | ram_bytes = num_bytes; |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 5735 | if (num_bytes == min_alloc_size) |
| 5736 | final_tried = true; |
| 5737 | goto again; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5738 | } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 5739 | struct btrfs_space_info *sinfo; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5740 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 5741 | sinfo = btrfs_find_space_info(fs_info, flags); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5742 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5743 | "allocation failed flags %llu, wanted %llu", |
| 5744 | flags, num_bytes); |
Jeff Mahoney | 5380428 | 2012-03-01 14:56:28 +0100 | [diff] [blame] | 5745 | if (sinfo) |
Josef Bacik | 5da6afe | 2019-06-18 16:09:24 -0400 | [diff] [blame] | 5746 | btrfs_dump_space_info(fs_info, sinfo, |
| 5747 | num_bytes, 1); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 5748 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5749 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5750 | |
| 5751 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5752 | } |
| 5753 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5754 | static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 5755 | u64 start, u64 len, |
| 5756 | int pin, int delalloc) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5757 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5758 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5759 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5760 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5761 | cache = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5762 | if (!cache) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5763 | btrfs_err(fs_info, "Unable to find block group for %llu", |
| 5764 | start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5765 | return -ENOSPC; |
| 5766 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5767 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5768 | if (pin) |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 5769 | pin_down_extent(cache, start, len, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5770 | else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5771 | if (btrfs_test_opt(fs_info, DISCARD)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5772 | ret = btrfs_discard_extent(fs_info, start, len, NULL); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5773 | btrfs_add_free_space(cache, start, len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 5774 | btrfs_free_reserved_bytes(cache, len, delalloc); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 5775 | trace_btrfs_reserved_extent_free(fs_info, start, len); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5776 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 5777 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5778 | btrfs_put_block_group(cache); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5779 | return ret; |
| 5780 | } |
| 5781 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5782 | int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 5783 | u64 start, u64 len, int delalloc) |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5784 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5785 | return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5786 | } |
| 5787 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5788 | int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info, |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5789 | u64 start, u64 len) |
| 5790 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5791 | return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5792 | } |
| 5793 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5794 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5795 | u64 parent, u64 root_objectid, |
| 5796 | u64 flags, u64 owner, u64 offset, |
| 5797 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5798 | { |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 5799 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5800 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5801 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5802 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5803 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5804 | struct extent_buffer *leaf; |
| 5805 | int type; |
| 5806 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 5807 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5808 | if (parent > 0) |
| 5809 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 5810 | else |
| 5811 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5812 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5813 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5814 | |
| 5815 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5816 | if (!path) |
| 5817 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5818 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5819 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5820 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5821 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5822 | if (ret) { |
| 5823 | btrfs_free_path(path); |
| 5824 | return ret; |
| 5825 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5826 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5827 | leaf = path->nodes[0]; |
| 5828 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5829 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5830 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 5831 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5832 | btrfs_set_extent_flags(leaf, extent_item, |
| 5833 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5834 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5835 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 5836 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 5837 | if (parent > 0) { |
| 5838 | struct btrfs_shared_data_ref *ref; |
| 5839 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 5840 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5841 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 5842 | } else { |
| 5843 | struct btrfs_extent_data_ref *ref; |
| 5844 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 5845 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 5846 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 5847 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 5848 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 5849 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5850 | |
| 5851 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5852 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 5853 | |
Nikolay Borisov | 25a356d | 2018-05-10 15:44:54 +0300 | [diff] [blame] | 5854 | ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 5855 | if (ret) |
| 5856 | return ret; |
| 5857 | |
David Sterba | 6b27940 | 2019-03-20 12:10:15 +0100 | [diff] [blame] | 5858 | ret = update_block_group(trans, ins->objectid, ins->offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5859 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5860 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5861 | ins->objectid, ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5862 | BUG(); |
| 5863 | } |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 5864 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5865 | return ret; |
| 5866 | } |
| 5867 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5868 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 5869 | struct btrfs_delayed_ref_node *node, |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 5870 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5871 | { |
Nikolay Borisov | 9dcdbe0 | 2018-05-21 12:27:20 +0300 | [diff] [blame] | 5872 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5873 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5874 | struct btrfs_extent_item *extent_item; |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 5875 | struct btrfs_key extent_key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5876 | struct btrfs_tree_block_info *block_info; |
| 5877 | struct btrfs_extent_inline_ref *iref; |
| 5878 | struct btrfs_path *path; |
| 5879 | struct extent_buffer *leaf; |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 5880 | struct btrfs_delayed_tree_ref *ref; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5881 | u32 size = sizeof(*extent_item) + sizeof(*iref); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 5882 | u64 num_bytes; |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 5883 | u64 flags = extent_op->flags_to_set; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5884 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5885 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 5886 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 5887 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 5888 | extent_key.objectid = node->bytenr; |
| 5889 | if (skinny_metadata) { |
| 5890 | extent_key.offset = ref->level; |
| 5891 | extent_key.type = BTRFS_METADATA_ITEM_KEY; |
| 5892 | num_bytes = fs_info->nodesize; |
| 5893 | } else { |
| 5894 | extent_key.offset = node->num_bytes; |
| 5895 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5896 | size += sizeof(*block_info); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 5897 | num_bytes = node->num_bytes; |
| 5898 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5899 | |
| 5900 | path = btrfs_alloc_path(); |
Josef Bacik | 80ee54b | 2018-10-11 15:54:22 -0400 | [diff] [blame] | 5901 | if (!path) |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5902 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5903 | |
| 5904 | path->leave_spinning = 1; |
| 5905 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 5906 | &extent_key, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5907 | if (ret) { |
Chris Mason | dd82525 | 2015-04-01 08:36:05 -0700 | [diff] [blame] | 5908 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5909 | return ret; |
| 5910 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5911 | |
| 5912 | leaf = path->nodes[0]; |
| 5913 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 5914 | struct btrfs_extent_item); |
| 5915 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 5916 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5917 | btrfs_set_extent_flags(leaf, extent_item, |
| 5918 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5919 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5920 | if (skinny_metadata) { |
| 5921 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 5922 | } else { |
| 5923 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 5924 | btrfs_set_tree_block_key(leaf, block_info, &extent_op->key); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 5925 | btrfs_set_tree_block_level(leaf, block_info, ref->level); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5926 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 5927 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5928 | |
Nikolay Borisov | d4b2073 | 2018-05-21 12:27:23 +0300 | [diff] [blame] | 5929 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5930 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 5931 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5932 | BTRFS_SHARED_BLOCK_REF_KEY); |
Nikolay Borisov | d4b2073 | 2018-05-21 12:27:23 +0300 | [diff] [blame] | 5933 | btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5934 | } else { |
| 5935 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5936 | BTRFS_TREE_BLOCK_REF_KEY); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 5937 | btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5938 | } |
| 5939 | |
| 5940 | btrfs_mark_buffer_dirty(leaf); |
| 5941 | btrfs_free_path(path); |
| 5942 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 5943 | ret = remove_from_free_space_tree(trans, extent_key.objectid, |
| 5944 | num_bytes); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 5945 | if (ret) |
| 5946 | return ret; |
| 5947 | |
David Sterba | 6b27940 | 2019-03-20 12:10:15 +0100 | [diff] [blame] | 5948 | ret = update_block_group(trans, extent_key.objectid, |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 5949 | fs_info->nodesize, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5950 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5951 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 5952 | extent_key.objectid, extent_key.offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5953 | BUG(); |
| 5954 | } |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 5955 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 5956 | trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5957 | fs_info->nodesize); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5958 | return ret; |
| 5959 | } |
| 5960 | |
| 5961 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 5962 | struct btrfs_root *root, u64 owner, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 5963 | u64 offset, u64 ram_bytes, |
| 5964 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5965 | { |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 5966 | struct btrfs_ref generic_ref = { 0 }; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5967 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 5968 | |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 5969 | BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5970 | |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 5971 | btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT, |
| 5972 | ins->objectid, ins->offset, 0); |
| 5973 | btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 5974 | btrfs_ref_tree_mod(root->fs_info, &generic_ref); |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 5975 | ret = btrfs_add_delayed_data_ref(trans, &generic_ref, |
| 5976 | ram_bytes, NULL, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5977 | return ret; |
| 5978 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5979 | |
| 5980 | /* |
| 5981 | * this is used by the tree logging recovery code. It records that |
| 5982 | * an extent has been allocated and makes sure to clear the free |
| 5983 | * space cache bits as well |
| 5984 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5985 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5986 | u64 root_objectid, u64 owner, u64 offset, |
| 5987 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5988 | { |
Nikolay Borisov | 61da2ab | 2018-06-20 15:49:13 +0300 | [diff] [blame] | 5989 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5990 | int ret; |
| 5991 | struct btrfs_block_group_cache *block_group; |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 5992 | struct btrfs_space_info *space_info; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 5993 | |
| 5994 | /* |
| 5995 | * Mixed block groups will exclude before processing the log so we only |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5996 | * need to do the exclude dance if this fs isn't mixed. |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 5997 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5998 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5999 | ret = __exclude_logged_extent(fs_info, ins->objectid, |
| 6000 | ins->offset); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6001 | if (ret) |
| 6002 | return ret; |
| 6003 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6004 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6005 | block_group = btrfs_lookup_block_group(fs_info, ins->objectid); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6006 | if (!block_group) |
| 6007 | return -EINVAL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6008 | |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 6009 | space_info = block_group->space_info; |
| 6010 | spin_lock(&space_info->lock); |
| 6011 | spin_lock(&block_group->lock); |
| 6012 | space_info->bytes_reserved += ins->offset; |
| 6013 | block_group->reserved += ins->offset; |
| 6014 | spin_unlock(&block_group->lock); |
| 6015 | spin_unlock(&space_info->lock); |
| 6016 | |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 6017 | ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner, |
| 6018 | offset, ins, 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6019 | btrfs_put_block_group(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6020 | return ret; |
| 6021 | } |
| 6022 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6023 | static struct extent_buffer * |
| 6024 | btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 6025 | u64 bytenr, int level, u64 owner) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6026 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6027 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6028 | struct extent_buffer *buf; |
| 6029 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6030 | buf = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6031 | if (IS_ERR(buf)) |
| 6032 | return buf; |
| 6033 | |
Qu Wenruo | b72c3ab | 2018-08-21 09:53:47 +0800 | [diff] [blame] | 6034 | /* |
| 6035 | * Extra safety check in case the extent tree is corrupted and extent |
| 6036 | * allocator chooses to use a tree block which is already used and |
| 6037 | * locked. |
| 6038 | */ |
| 6039 | if (buf->lock_owner == current->pid) { |
| 6040 | btrfs_err_rl(fs_info, |
| 6041 | "tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected", |
| 6042 | buf->start, btrfs_header_owner(buf), current->pid); |
| 6043 | free_extent_buffer(buf); |
| 6044 | return ERR_PTR(-EUCLEAN); |
| 6045 | } |
| 6046 | |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6047 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6048 | btrfs_tree_lock(buf); |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 6049 | btrfs_clean_tree_block(buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6050 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 6051 | |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 6052 | btrfs_set_lock_blocking_write(buf); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 6053 | set_extent_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 6054 | |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 6055 | memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header)); |
| 6056 | btrfs_set_header_level(buf, level); |
| 6057 | btrfs_set_header_bytenr(buf, buf->start); |
| 6058 | btrfs_set_header_generation(buf, trans->transid); |
| 6059 | btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV); |
| 6060 | btrfs_set_header_owner(buf, owner); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 6061 | write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid); |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 6062 | write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 6063 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 6064 | buf->log_index = root->log_transid % 2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 6065 | /* |
| 6066 | * we allow two log transactions at a time, use different |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 6067 | * EXTENT bit to differentiate dirty pages. |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 6068 | */ |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 6069 | if (buf->log_index == 0) |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 6070 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 6071 | buf->start + buf->len - 1, GFP_NOFS); |
| 6072 | else |
| 6073 | set_extent_new(&root->dirty_log_pages, buf->start, |
David Sterba | 3744dbe | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 6074 | buf->start + buf->len - 1); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 6075 | } else { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 6076 | buf->log_index = -1; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 6077 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 6078 | buf->start + buf->len - 1, GFP_NOFS); |
| 6079 | } |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 6080 | trans->dirty = true; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 6081 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6082 | return buf; |
| 6083 | } |
| 6084 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6085 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6086 | * finds a free extent and does all the dirty work required for allocation |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 6087 | * returns the tree buffer or an ERR_PTR on error. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6088 | */ |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 6089 | struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 6090 | struct btrfs_root *root, |
| 6091 | u64 parent, u64 root_objectid, |
| 6092 | const struct btrfs_disk_key *key, |
| 6093 | int level, u64 hint, |
| 6094 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6095 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6096 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 6097 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6098 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6099 | struct extent_buffer *buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 6100 | struct btrfs_delayed_extent_op *extent_op; |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 6101 | struct btrfs_ref generic_ref = { 0 }; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6102 | u64 flags = 0; |
| 6103 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6104 | u32 blocksize = fs_info->nodesize; |
| 6105 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6106 | |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 6107 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6108 | if (btrfs_is_testing(fs_info)) { |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 6109 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 6110 | level, root_objectid); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 6111 | if (!IS_ERR(buf)) |
| 6112 | root->alloc_bytenr += blocksize; |
| 6113 | return buf; |
| 6114 | } |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 6115 | #endif |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 6116 | |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 6117 | block_rsv = btrfs_use_block_rsv(trans, root, blocksize); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6118 | if (IS_ERR(block_rsv)) |
| 6119 | return ERR_CAST(block_rsv); |
| 6120 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 6121 | ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6122 | empty_size, hint, &ins, 0, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 6123 | if (ret) |
| 6124 | goto out_unuse; |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 6125 | |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 6126 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, level, |
| 6127 | root_objectid); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 6128 | if (IS_ERR(buf)) { |
| 6129 | ret = PTR_ERR(buf); |
| 6130 | goto out_free_reserved; |
| 6131 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6132 | |
| 6133 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 6134 | if (parent == 0) |
| 6135 | parent = ins.objectid; |
| 6136 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6137 | } else |
| 6138 | BUG_ON(parent > 0); |
| 6139 | |
| 6140 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 6141 | extent_op = btrfs_alloc_delayed_extent_op(); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 6142 | if (!extent_op) { |
| 6143 | ret = -ENOMEM; |
| 6144 | goto out_free_buf; |
| 6145 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6146 | if (key) |
| 6147 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 6148 | else |
| 6149 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 6150 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 6151 | extent_op->update_key = skinny_metadata ? false : true; |
| 6152 | extent_op->update_flags = true; |
| 6153 | extent_op->is_data = false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 6154 | extent_op->level = level; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6155 | |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 6156 | btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT, |
| 6157 | ins.objectid, ins.offset, parent); |
| 6158 | generic_ref.real_root = root->root_key.objectid; |
| 6159 | btrfs_init_tree_ref(&generic_ref, level, root_objectid); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 6160 | btrfs_ref_tree_mod(fs_info, &generic_ref); |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 6161 | ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, |
Omar Sandoval | 7be0791 | 2017-06-06 16:45:30 -0700 | [diff] [blame] | 6162 | extent_op, NULL, NULL); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 6163 | if (ret) |
| 6164 | goto out_free_delayed; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6165 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6166 | return buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 6167 | |
| 6168 | out_free_delayed: |
| 6169 | btrfs_free_delayed_extent_op(extent_op); |
| 6170 | out_free_buf: |
| 6171 | free_extent_buffer(buf); |
| 6172 | out_free_reserved: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6173 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 6174 | out_unuse: |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 6175 | btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 6176 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6177 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6178 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6179 | struct walk_control { |
| 6180 | u64 refs[BTRFS_MAX_LEVEL]; |
| 6181 | u64 flags[BTRFS_MAX_LEVEL]; |
| 6182 | struct btrfs_key update_progress; |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 6183 | struct btrfs_key drop_progress; |
| 6184 | int drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6185 | int stage; |
| 6186 | int level; |
| 6187 | int shared_level; |
| 6188 | int update_ref; |
| 6189 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6190 | int reada_slot; |
| 6191 | int reada_count; |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 6192 | int restarted; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6193 | }; |
| 6194 | |
| 6195 | #define DROP_REFERENCE 1 |
| 6196 | #define UPDATE_BACKREF 2 |
| 6197 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6198 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 6199 | struct btrfs_root *root, |
| 6200 | struct walk_control *wc, |
| 6201 | struct btrfs_path *path) |
| 6202 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6203 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6204 | u64 bytenr; |
| 6205 | u64 generation; |
| 6206 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6207 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6208 | u32 nritems; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6209 | struct btrfs_key key; |
| 6210 | struct extent_buffer *eb; |
| 6211 | int ret; |
| 6212 | int slot; |
| 6213 | int nread = 0; |
| 6214 | |
| 6215 | if (path->slots[wc->level] < wc->reada_slot) { |
| 6216 | wc->reada_count = wc->reada_count * 2 / 3; |
| 6217 | wc->reada_count = max(wc->reada_count, 2); |
| 6218 | } else { |
| 6219 | wc->reada_count = wc->reada_count * 3 / 2; |
| 6220 | wc->reada_count = min_t(int, wc->reada_count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6221 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | eb = path->nodes[wc->level]; |
| 6225 | nritems = btrfs_header_nritems(eb); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6226 | |
| 6227 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 6228 | if (nread >= wc->reada_count) |
| 6229 | break; |
| 6230 | |
| 6231 | cond_resched(); |
| 6232 | bytenr = btrfs_node_blockptr(eb, slot); |
| 6233 | generation = btrfs_node_ptr_generation(eb, slot); |
| 6234 | |
| 6235 | if (slot == path->slots[wc->level]) |
| 6236 | goto reada; |
| 6237 | |
| 6238 | if (wc->stage == UPDATE_BACKREF && |
| 6239 | generation <= root->root_key.offset) |
| 6240 | continue; |
| 6241 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6242 | /* We don't lock the tree block, it's OK to be racy here */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6243 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6244 | wc->level - 1, 1, &refs, |
| 6245 | &flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6246 | /* We don't care about errors in readahead. */ |
| 6247 | if (ret < 0) |
| 6248 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6249 | BUG_ON(refs == 0); |
| 6250 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6251 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6252 | if (refs == 1) |
| 6253 | goto reada; |
| 6254 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6255 | if (wc->level == 1 && |
| 6256 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6257 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6258 | if (!wc->update_ref || |
| 6259 | generation <= root->root_key.offset) |
| 6260 | continue; |
| 6261 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 6262 | ret = btrfs_comp_cpu_keys(&key, |
| 6263 | &wc->update_progress); |
| 6264 | if (ret < 0) |
| 6265 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6266 | } else { |
| 6267 | if (wc->level == 1 && |
| 6268 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6269 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6270 | } |
| 6271 | reada: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6272 | readahead_tree_block(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6273 | nread++; |
| 6274 | } |
| 6275 | wc->reada_slot = slot; |
| 6276 | } |
| 6277 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6278 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 6279 | * helper to process tree block while walking down the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6280 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6281 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 6282 | * back refs for pointers in the block. |
| 6283 | * |
| 6284 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6285 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6286 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 6287 | struct btrfs_root *root, |
| 6288 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6289 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6290 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6291 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6292 | int level = wc->level; |
| 6293 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6294 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6295 | int ret; |
| 6296 | |
| 6297 | if (wc->stage == UPDATE_BACKREF && |
| 6298 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 6299 | return 1; |
| 6300 | |
| 6301 | /* |
| 6302 | * when reference count of tree block is 1, it won't increase |
| 6303 | * again. once full backref flag is set, we never clear it. |
| 6304 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6305 | if (lookup_info && |
| 6306 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 6307 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6308 | BUG_ON(!path->locks[level]); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6309 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6310 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6311 | &wc->refs[level], |
| 6312 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6313 | BUG_ON(ret == -ENOMEM); |
| 6314 | if (ret) |
| 6315 | return ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6316 | BUG_ON(wc->refs[level] == 0); |
| 6317 | } |
| 6318 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6319 | if (wc->stage == DROP_REFERENCE) { |
| 6320 | if (wc->refs[level] > 1) |
| 6321 | return 1; |
| 6322 | |
| 6323 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6324 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6325 | path->locks[level] = 0; |
| 6326 | } |
| 6327 | return 0; |
| 6328 | } |
| 6329 | |
| 6330 | /* wc->stage == UPDATE_BACKREF */ |
| 6331 | if (!(wc->flags[level] & flag)) { |
| 6332 | BUG_ON(!path->locks[level]); |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 6333 | ret = btrfs_inc_ref(trans, root, eb, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6334 | BUG_ON(ret); /* -ENOMEM */ |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 6335 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6336 | BUG_ON(ret); /* -ENOMEM */ |
David Sterba | f5c8daa | 2019-03-20 11:43:36 +0100 | [diff] [blame] | 6337 | ret = btrfs_set_disk_extent_flags(trans, eb->start, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 6338 | eb->len, flag, |
| 6339 | btrfs_header_level(eb), 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6340 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6341 | wc->flags[level] |= flag; |
| 6342 | } |
| 6343 | |
| 6344 | /* |
| 6345 | * the block is shared by multiple trees, so it's not good to |
| 6346 | * keep the tree lock |
| 6347 | */ |
| 6348 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6349 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6350 | path->locks[level] = 0; |
| 6351 | } |
| 6352 | return 0; |
| 6353 | } |
| 6354 | |
| 6355 | /* |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 6356 | * This is used to verify a ref exists for this root to deal with a bug where we |
| 6357 | * would have a drop_progress key that hadn't been updated properly. |
| 6358 | */ |
| 6359 | static int check_ref_exists(struct btrfs_trans_handle *trans, |
| 6360 | struct btrfs_root *root, u64 bytenr, u64 parent, |
| 6361 | int level) |
| 6362 | { |
| 6363 | struct btrfs_path *path; |
| 6364 | struct btrfs_extent_inline_ref *iref; |
| 6365 | int ret; |
| 6366 | |
| 6367 | path = btrfs_alloc_path(); |
| 6368 | if (!path) |
| 6369 | return -ENOMEM; |
| 6370 | |
| 6371 | ret = lookup_extent_backref(trans, path, &iref, bytenr, |
| 6372 | root->fs_info->nodesize, parent, |
| 6373 | root->root_key.objectid, level, 0); |
| 6374 | btrfs_free_path(path); |
| 6375 | if (ret == -ENOENT) |
| 6376 | return 0; |
| 6377 | if (ret < 0) |
| 6378 | return ret; |
| 6379 | return 1; |
| 6380 | } |
| 6381 | |
| 6382 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 6383 | * helper to process tree block pointer. |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6384 | * |
| 6385 | * when wc->stage == DROP_REFERENCE, this function checks |
| 6386 | * reference count of the block pointed to. if the block |
| 6387 | * is shared and we need update back refs for the subtree |
| 6388 | * rooted at the block, this function changes wc->stage to |
| 6389 | * UPDATE_BACKREF. if the block is shared and there is no |
| 6390 | * need to update back, this function drops the reference |
| 6391 | * to the block. |
| 6392 | * |
| 6393 | * NOTE: return value 1 means we should stop walking down. |
| 6394 | */ |
| 6395 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 6396 | struct btrfs_root *root, |
| 6397 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6398 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6399 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6400 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6401 | u64 bytenr; |
| 6402 | u64 generation; |
| 6403 | u64 parent; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6404 | struct btrfs_key key; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 6405 | struct btrfs_key first_key; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 6406 | struct btrfs_ref ref = { 0 }; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6407 | struct extent_buffer *next; |
| 6408 | int level = wc->level; |
| 6409 | int reada = 0; |
| 6410 | int ret = 0; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 6411 | bool need_account = false; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6412 | |
| 6413 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 6414 | path->slots[level]); |
| 6415 | /* |
| 6416 | * if the lower level block was created before the snapshot |
| 6417 | * was created, we know there is no need to update back refs |
| 6418 | * for the subtree |
| 6419 | */ |
| 6420 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6421 | generation <= root->root_key.offset) { |
| 6422 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6423 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6424 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6425 | |
| 6426 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 6427 | btrfs_node_key_to_cpu(path->nodes[level], &first_key, |
| 6428 | path->slots[level]); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6429 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6430 | next = find_extent_buffer(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6431 | if (!next) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6432 | next = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6433 | if (IS_ERR(next)) |
| 6434 | return PTR_ERR(next); |
| 6435 | |
Josef Bacik | b2aaaa3 | 2013-07-05 17:05:38 -0400 | [diff] [blame] | 6436 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, next, |
| 6437 | level - 1); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6438 | reada = 1; |
| 6439 | } |
| 6440 | btrfs_tree_lock(next); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 6441 | btrfs_set_lock_blocking_write(next); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6442 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6443 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6444 | &wc->refs[level - 1], |
| 6445 | &wc->flags[level - 1]); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 6446 | if (ret < 0) |
| 6447 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6448 | |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6449 | if (unlikely(wc->refs[level - 1] == 0)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6450 | btrfs_err(fs_info, "Missing references."); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 6451 | ret = -EIO; |
| 6452 | goto out_unlock; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6453 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6454 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6455 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6456 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6457 | if (wc->refs[level - 1] > 1) { |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 6458 | need_account = true; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6459 | if (level == 1 && |
| 6460 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6461 | goto skip; |
| 6462 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6463 | if (!wc->update_ref || |
| 6464 | generation <= root->root_key.offset) |
| 6465 | goto skip; |
| 6466 | |
| 6467 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 6468 | path->slots[level]); |
| 6469 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 6470 | if (ret < 0) |
| 6471 | goto skip; |
| 6472 | |
| 6473 | wc->stage = UPDATE_BACKREF; |
| 6474 | wc->shared_level = level - 1; |
| 6475 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6476 | } else { |
| 6477 | if (level == 1 && |
| 6478 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6479 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6480 | } |
| 6481 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 6482 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6483 | btrfs_tree_unlock(next); |
| 6484 | free_extent_buffer(next); |
| 6485 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6486 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6487 | } |
| 6488 | |
| 6489 | if (!next) { |
| 6490 | if (reada && level == 1) |
| 6491 | reada_walk_down(trans, root, wc, path); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 6492 | next = read_tree_block(fs_info, bytenr, generation, level - 1, |
| 6493 | &first_key); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 6494 | if (IS_ERR(next)) { |
| 6495 | return PTR_ERR(next); |
| 6496 | } else if (!extent_buffer_uptodate(next)) { |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 6497 | free_extent_buffer(next); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 6498 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 6499 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6500 | btrfs_tree_lock(next); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 6501 | btrfs_set_lock_blocking_write(next); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6502 | } |
| 6503 | |
| 6504 | level--; |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 6505 | ASSERT(level == btrfs_header_level(next)); |
| 6506 | if (level != btrfs_header_level(next)) { |
| 6507 | btrfs_err(root->fs_info, "mismatched level"); |
| 6508 | ret = -EIO; |
| 6509 | goto out_unlock; |
| 6510 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6511 | path->nodes[level] = next; |
| 6512 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6513 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6514 | wc->level = level; |
| 6515 | if (wc->level == 1) |
| 6516 | wc->reada_slot = 0; |
| 6517 | return 0; |
| 6518 | skip: |
| 6519 | wc->refs[level - 1] = 0; |
| 6520 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6521 | if (wc->stage == DROP_REFERENCE) { |
| 6522 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 6523 | parent = path->nodes[level]->start; |
| 6524 | } else { |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 6525 | ASSERT(root->root_key.objectid == |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6526 | btrfs_header_owner(path->nodes[level])); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 6527 | if (root->root_key.objectid != |
| 6528 | btrfs_header_owner(path->nodes[level])) { |
| 6529 | btrfs_err(root->fs_info, |
| 6530 | "mismatched block owner"); |
| 6531 | ret = -EIO; |
| 6532 | goto out_unlock; |
| 6533 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6534 | parent = 0; |
| 6535 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6536 | |
Qu Wenruo | 2cd86d3 | 2018-09-27 14:42:33 +0800 | [diff] [blame] | 6537 | /* |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 6538 | * If we had a drop_progress we need to verify the refs are set |
| 6539 | * as expected. If we find our ref then we know that from here |
| 6540 | * on out everything should be correct, and we can clear the |
| 6541 | * ->restarted flag. |
| 6542 | */ |
| 6543 | if (wc->restarted) { |
| 6544 | ret = check_ref_exists(trans, root, bytenr, parent, |
| 6545 | level - 1); |
| 6546 | if (ret < 0) |
| 6547 | goto out_unlock; |
| 6548 | if (ret == 0) |
| 6549 | goto no_delete; |
| 6550 | ret = 0; |
| 6551 | wc->restarted = 0; |
| 6552 | } |
| 6553 | |
| 6554 | /* |
Qu Wenruo | 2cd86d3 | 2018-09-27 14:42:33 +0800 | [diff] [blame] | 6555 | * Reloc tree doesn't contribute to qgroup numbers, and we have |
| 6556 | * already accounted them at merge time (replace_path), |
| 6557 | * thus we could skip expensive subtree trace here. |
| 6558 | */ |
| 6559 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && |
| 6560 | need_account) { |
Lu Fengqi | deb4062 | 2018-07-18 14:45:38 +0800 | [diff] [blame] | 6561 | ret = btrfs_qgroup_trace_subtree(trans, next, |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 6562 | generation, level - 1); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 6563 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6564 | btrfs_err_rl(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6565 | "Error %d accounting shared subtree. Quota is out of sync, rescan required.", |
| 6566 | ret); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 6567 | } |
| 6568 | } |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 6569 | |
| 6570 | /* |
| 6571 | * We need to update the next key in our walk control so we can |
| 6572 | * update the drop_progress key accordingly. We don't care if |
| 6573 | * find_next_key doesn't find a key because that means we're at |
| 6574 | * the end and are going to clean up now. |
| 6575 | */ |
| 6576 | wc->drop_level = level; |
| 6577 | find_next_key(path, level, &wc->drop_progress); |
| 6578 | |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 6579 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr, |
| 6580 | fs_info->nodesize, parent); |
| 6581 | btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid); |
| 6582 | ret = btrfs_free_extent(trans, &ref); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 6583 | if (ret) |
| 6584 | goto out_unlock; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6585 | } |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 6586 | no_delete: |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 6587 | *lookup_info = 1; |
| 6588 | ret = 1; |
| 6589 | |
| 6590 | out_unlock: |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6591 | btrfs_tree_unlock(next); |
| 6592 | free_extent_buffer(next); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 6593 | |
| 6594 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6595 | } |
| 6596 | |
| 6597 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 6598 | * helper to process tree block while walking up the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6599 | * |
| 6600 | * when wc->stage == DROP_REFERENCE, this function drops |
| 6601 | * reference count on the block. |
| 6602 | * |
| 6603 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 6604 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 6605 | * to UPDATE_BACKREF previously while processing the block. |
| 6606 | * |
| 6607 | * NOTE: return value 1 means we should stop walking up. |
| 6608 | */ |
| 6609 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 6610 | struct btrfs_root *root, |
| 6611 | struct btrfs_path *path, |
| 6612 | struct walk_control *wc) |
| 6613 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6614 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6615 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6616 | int level = wc->level; |
| 6617 | struct extent_buffer *eb = path->nodes[level]; |
| 6618 | u64 parent = 0; |
| 6619 | |
| 6620 | if (wc->stage == UPDATE_BACKREF) { |
| 6621 | BUG_ON(wc->shared_level < level); |
| 6622 | if (level < wc->shared_level) |
| 6623 | goto out; |
| 6624 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6625 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 6626 | if (ret > 0) |
| 6627 | wc->update_ref = 0; |
| 6628 | |
| 6629 | wc->stage = DROP_REFERENCE; |
| 6630 | wc->shared_level = -1; |
| 6631 | path->slots[level] = 0; |
| 6632 | |
| 6633 | /* |
| 6634 | * check reference count again if the block isn't locked. |
| 6635 | * we should start walking down the tree again if reference |
| 6636 | * count is one. |
| 6637 | */ |
| 6638 | if (!path->locks[level]) { |
| 6639 | BUG_ON(level == 0); |
| 6640 | btrfs_tree_lock(eb); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 6641 | btrfs_set_lock_blocking_write(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6642 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6643 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6644 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6645 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6646 | &wc->refs[level], |
| 6647 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6648 | if (ret < 0) { |
| 6649 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 6650 | path->locks[level] = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6651 | return ret; |
| 6652 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6653 | BUG_ON(wc->refs[level] == 0); |
| 6654 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6655 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 6656 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6657 | return 1; |
| 6658 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6659 | } |
| 6660 | } |
| 6661 | |
| 6662 | /* wc->stage == DROP_REFERENCE */ |
| 6663 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 6664 | |
| 6665 | if (wc->refs[level] == 1) { |
| 6666 | if (level == 0) { |
| 6667 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 6668 | ret = btrfs_dec_ref(trans, root, eb, 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6669 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 6670 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6671 | BUG_ON(ret); /* -ENOMEM */ |
Qu Wenruo | c4140cb | 2019-04-04 14:45:37 +0800 | [diff] [blame] | 6672 | if (is_fstree(root->root_key.objectid)) { |
| 6673 | ret = btrfs_qgroup_trace_leaf_items(trans, eb); |
| 6674 | if (ret) { |
| 6675 | btrfs_err_rl(fs_info, |
| 6676 | "error %d accounting leaf items, quota is out of sync, rescan required", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6677 | ret); |
Qu Wenruo | c4140cb | 2019-04-04 14:45:37 +0800 | [diff] [blame] | 6678 | } |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 6679 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6680 | } |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 6681 | /* make block locked assertion in btrfs_clean_tree_block happy */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6682 | if (!path->locks[level] && |
| 6683 | btrfs_header_generation(eb) == trans->transid) { |
| 6684 | btrfs_tree_lock(eb); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 6685 | btrfs_set_lock_blocking_write(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6686 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6687 | } |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 6688 | btrfs_clean_tree_block(eb); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6689 | } |
| 6690 | |
| 6691 | if (eb == root->node) { |
| 6692 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6693 | parent = eb->start; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 6694 | else if (root->root_key.objectid != btrfs_header_owner(eb)) |
| 6695 | goto owner_mismatch; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6696 | } else { |
| 6697 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6698 | parent = path->nodes[level + 1]->start; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 6699 | else if (root->root_key.objectid != |
| 6700 | btrfs_header_owner(path->nodes[level + 1])) |
| 6701 | goto owner_mismatch; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6702 | } |
| 6703 | |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 6704 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6705 | out: |
| 6706 | wc->refs[level] = 0; |
| 6707 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6708 | return 0; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 6709 | |
| 6710 | owner_mismatch: |
| 6711 | btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu", |
| 6712 | btrfs_header_owner(eb), root->root_key.objectid); |
| 6713 | return -EUCLEAN; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6714 | } |
| 6715 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6716 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 6717 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6718 | struct btrfs_path *path, |
| 6719 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6720 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6721 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6722 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6723 | int ret; |
| 6724 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6725 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6726 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6727 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6728 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6729 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6730 | if (level == 0) |
| 6731 | break; |
| 6732 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 6733 | if (path->slots[level] >= |
| 6734 | btrfs_header_nritems(path->nodes[level])) |
| 6735 | break; |
| 6736 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6737 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6738 | if (ret > 0) { |
| 6739 | path->slots[level]++; |
| 6740 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6741 | } else if (ret < 0) |
| 6742 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6743 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6744 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6745 | return 0; |
| 6746 | } |
| 6747 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6748 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6749 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6750 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6751 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6752 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6753 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6754 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6755 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6756 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 6757 | while (level < max_level && path->nodes[level]) { |
| 6758 | wc->level = level; |
| 6759 | if (path->slots[level] + 1 < |
| 6760 | btrfs_header_nritems(path->nodes[level])) { |
| 6761 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6762 | return 0; |
| 6763 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6764 | ret = walk_up_proc(trans, root, path, wc); |
| 6765 | if (ret > 0) |
| 6766 | return 0; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 6767 | if (ret < 0) |
| 6768 | return ret; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6769 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6770 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6771 | btrfs_tree_unlock_rw(path->nodes[level], |
| 6772 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6773 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6774 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6775 | free_extent_buffer(path->nodes[level]); |
| 6776 | path->nodes[level] = NULL; |
| 6777 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6778 | } |
| 6779 | } |
| 6780 | return 1; |
| 6781 | } |
| 6782 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6783 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6784 | * drop a subvolume tree. |
| 6785 | * |
| 6786 | * this function traverses the tree freeing any blocks that only |
| 6787 | * referenced by the tree. |
| 6788 | * |
| 6789 | * when a shared tree block is found. this function decreases its |
| 6790 | * reference count by one. if update_ref is true, this function |
| 6791 | * also make sure backrefs for the shared block and all lower level |
| 6792 | * blocks are properly updated. |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 6793 | * |
| 6794 | * If called with for_reloc == 0, may exit early with -EAGAIN |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6795 | */ |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 6796 | int btrfs_drop_snapshot(struct btrfs_root *root, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6797 | struct btrfs_block_rsv *block_rsv, int update_ref, |
| 6798 | int for_reloc) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6799 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6800 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6801 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6802 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6803 | struct btrfs_root *tree_root = fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6804 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6805 | struct walk_control *wc; |
| 6806 | struct btrfs_key key; |
| 6807 | int err = 0; |
| 6808 | int ret; |
| 6809 | int level; |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 6810 | bool root_dropped = false; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6811 | |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 6812 | btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 6813 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6814 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6815 | if (!path) { |
| 6816 | err = -ENOMEM; |
| 6817 | goto out; |
| 6818 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6819 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6820 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 6821 | if (!wc) { |
| 6822 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6823 | err = -ENOMEM; |
| 6824 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 6825 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6826 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6827 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6828 | if (IS_ERR(trans)) { |
| 6829 | err = PTR_ERR(trans); |
| 6830 | goto out_free; |
| 6831 | } |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6832 | |
Josef Bacik | 0568e82 | 2018-11-30 11:52:14 -0500 | [diff] [blame] | 6833 | err = btrfs_run_delayed_items(trans); |
| 6834 | if (err) |
| 6835 | goto out_end_trans; |
| 6836 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6837 | if (block_rsv) |
| 6838 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6839 | |
Josef Bacik | 83354f0 | 2018-11-30 11:52:13 -0500 | [diff] [blame] | 6840 | /* |
| 6841 | * This will help us catch people modifying the fs tree while we're |
| 6842 | * dropping it. It is unsafe to mess with the fs tree while it's being |
| 6843 | * dropped as we unlock the root node and parent nodes as we walk down |
| 6844 | * the tree, assuming nothing will change. If something does change |
| 6845 | * then we'll have stale information and drop references to blocks we've |
| 6846 | * already dropped. |
| 6847 | */ |
| 6848 | set_bit(BTRFS_ROOT_DELETING, &root->state); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6849 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6850 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6851 | path->nodes[level] = btrfs_lock_root_node(root); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 6852 | btrfs_set_lock_blocking_write(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6853 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6854 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6855 | memset(&wc->update_progress, 0, |
| 6856 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6857 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6858 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6859 | memcpy(&wc->update_progress, &key, |
| 6860 | sizeof(wc->update_progress)); |
| 6861 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6862 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6863 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6864 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6865 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6866 | path->lowest_level = 0; |
| 6867 | if (ret < 0) { |
| 6868 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6869 | goto out_end_trans; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6870 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6871 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6872 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6873 | /* |
| 6874 | * unlock our path, this is safe because only this |
| 6875 | * function is allowed to delete this snapshot |
| 6876 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6877 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6878 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6879 | level = btrfs_header_level(root->node); |
| 6880 | while (1) { |
| 6881 | btrfs_tree_lock(path->nodes[level]); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 6882 | btrfs_set_lock_blocking_write(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 6883 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6884 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6885 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6886 | path->nodes[level]->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6887 | level, 1, &wc->refs[level], |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6888 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6889 | if (ret < 0) { |
| 6890 | err = ret; |
| 6891 | goto out_end_trans; |
| 6892 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6893 | BUG_ON(wc->refs[level] == 0); |
| 6894 | |
| 6895 | if (level == root_item->drop_level) |
| 6896 | break; |
| 6897 | |
| 6898 | btrfs_tree_unlock(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 6899 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6900 | WARN_ON(wc->refs[level] != 1); |
| 6901 | level--; |
| 6902 | } |
| 6903 | } |
| 6904 | |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 6905 | wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6906 | wc->level = level; |
| 6907 | wc->shared_level = -1; |
| 6908 | wc->stage = DROP_REFERENCE; |
| 6909 | wc->update_ref = update_ref; |
| 6910 | wc->keep_locks = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6911 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6912 | |
| 6913 | while (1) { |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 6914 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6915 | ret = walk_down_tree(trans, root, path, wc); |
| 6916 | if (ret < 0) { |
| 6917 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6918 | break; |
| 6919 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6920 | |
| 6921 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 6922 | if (ret < 0) { |
| 6923 | err = ret; |
| 6924 | break; |
| 6925 | } |
| 6926 | |
| 6927 | if (ret > 0) { |
| 6928 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 6929 | break; |
| 6930 | } |
| 6931 | |
| 6932 | if (wc->stage == DROP_REFERENCE) { |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 6933 | wc->drop_level = wc->level; |
| 6934 | btrfs_node_key_to_cpu(path->nodes[wc->drop_level], |
| 6935 | &wc->drop_progress, |
| 6936 | path->slots[wc->drop_level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6937 | } |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 6938 | btrfs_cpu_key_to_disk(&root_item->drop_progress, |
| 6939 | &wc->drop_progress); |
| 6940 | root_item->drop_level = wc->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6941 | |
| 6942 | BUG_ON(wc->level == 0); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6943 | if (btrfs_should_end_transaction(trans) || |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6944 | (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6945 | ret = btrfs_update_root(trans, tree_root, |
| 6946 | &root->root_key, |
| 6947 | root_item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6948 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6949 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6950 | err = ret; |
| 6951 | goto out_end_trans; |
| 6952 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6953 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 6954 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6955 | if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6956 | btrfs_debug(fs_info, |
| 6957 | "drop snapshot early exit"); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 6958 | err = -EAGAIN; |
| 6959 | goto out_free; |
| 6960 | } |
| 6961 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6962 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6963 | if (IS_ERR(trans)) { |
| 6964 | err = PTR_ERR(trans); |
| 6965 | goto out_free; |
| 6966 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6967 | if (block_rsv) |
| 6968 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6969 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6970 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6971 | btrfs_release_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6972 | if (err) |
| 6973 | goto out_end_trans; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6974 | |
Lu Fengqi | ab9ce7d | 2018-08-01 11:32:27 +0800 | [diff] [blame] | 6975 | ret = btrfs_del_root(trans, &root->root_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6976 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6977 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | e19182c | 2017-12-04 13:11:45 -0500 | [diff] [blame] | 6978 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6979 | goto out_end_trans; |
| 6980 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6981 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6982 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6983 | ret = btrfs_find_root(tree_root, &root->root_key, path, |
| 6984 | NULL, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6985 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6986 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6987 | err = ret; |
| 6988 | goto out_end_trans; |
| 6989 | } else if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 6990 | /* if we fail to delete the orphan item this time |
| 6991 | * around, it'll get picked up the next time. |
| 6992 | * |
| 6993 | * The most common failure here is just -ENOENT. |
| 6994 | */ |
| 6995 | btrfs_del_orphan_item(trans, tree_root, |
| 6996 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6997 | } |
| 6998 | } |
| 6999 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 7000 | if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) { |
Josef Bacik | 2b9dbef | 2015-09-15 10:07:04 -0400 | [diff] [blame] | 7001 | btrfs_add_dropped_root(trans, root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7002 | } else { |
| 7003 | free_extent_buffer(root->node); |
| 7004 | free_extent_buffer(root->commit_root); |
Miao Xie | b0feb9d | 2013-05-15 07:48:20 +0000 | [diff] [blame] | 7005 | btrfs_put_fs_root(root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7006 | } |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 7007 | root_dropped = true; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7008 | out_end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 7009 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7010 | out_free: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7011 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 7012 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 7013 | out: |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 7014 | /* |
| 7015 | * So if we need to stop dropping the snapshot for whatever reason we |
| 7016 | * need to make sure to add it back to the dead root list so that we |
| 7017 | * keep trying to do the work later. This also cleans up roots if we |
| 7018 | * don't have it in the radix (like when we recover after a power fail |
| 7019 | * or unmount) so we don't leak memory. |
| 7020 | */ |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 7021 | if (!for_reloc && !root_dropped) |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 7022 | btrfs_add_dead_root(root); |
Wang Shilong | 90515e7 | 2014-01-07 17:26:58 +0800 | [diff] [blame] | 7023 | if (err && err != -EAGAIN) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7024 | btrfs_handle_fs_error(fs_info, err, NULL); |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 7025 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7026 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7027 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7028 | /* |
| 7029 | * drop subtree rooted at tree block 'node'. |
| 7030 | * |
| 7031 | * NOTE: this function will unlock and release tree block 'node' |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7032 | * only used by relocation code |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7033 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7034 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 7035 | struct btrfs_root *root, |
| 7036 | struct extent_buffer *node, |
| 7037 | struct extent_buffer *parent) |
| 7038 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7039 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7040 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7041 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7042 | int level; |
| 7043 | int parent_level; |
| 7044 | int ret = 0; |
| 7045 | int wret; |
| 7046 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7047 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 7048 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7049 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7050 | if (!path) |
| 7051 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7052 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7053 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7054 | if (!wc) { |
| 7055 | btrfs_free_path(path); |
| 7056 | return -ENOMEM; |
| 7057 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7058 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 7059 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7060 | parent_level = btrfs_header_level(parent); |
| 7061 | extent_buffer_get(parent); |
| 7062 | path->nodes[parent_level] = parent; |
| 7063 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 7064 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 7065 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7066 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7067 | path->nodes[level] = node; |
| 7068 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7069 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7070 | |
| 7071 | wc->refs[parent_level] = 1; |
| 7072 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 7073 | wc->level = level; |
| 7074 | wc->shared_level = -1; |
| 7075 | wc->stage = DROP_REFERENCE; |
| 7076 | wc->update_ref = 0; |
| 7077 | wc->keep_locks = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7078 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7079 | |
| 7080 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7081 | wret = walk_down_tree(trans, root, path, wc); |
| 7082 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7083 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7084 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7085 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7086 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7087 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7088 | if (wret < 0) |
| 7089 | ret = wret; |
| 7090 | if (wret != 0) |
| 7091 | break; |
| 7092 | } |
| 7093 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7094 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7095 | btrfs_free_path(path); |
| 7096 | return ret; |
| 7097 | } |
| 7098 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 7099 | static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7100 | { |
| 7101 | u64 num_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7102 | u64 stripped; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7103 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7104 | /* |
| 7105 | * if restripe for this chunk_type is on pick target profile and |
| 7106 | * return, otherwise do the usual balance |
| 7107 | */ |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 7108 | stripped = get_restripe_target(fs_info, flags); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7109 | if (stripped) |
| 7110 | return extended_to_chunk(stripped); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 7111 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 7112 | num_devices = fs_info->fs_devices->rw_devices; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 7113 | |
David Sterba | a07e8a4 | 2019-05-31 16:54:26 +0200 | [diff] [blame] | 7114 | stripped = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID56_MASK | |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 7115 | BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7116 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7117 | if (num_devices == 1) { |
| 7118 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7119 | stripped = flags & ~stripped; |
| 7120 | |
| 7121 | /* turn raid0 into single device chunks */ |
| 7122 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 7123 | return stripped; |
| 7124 | |
| 7125 | /* turn mirroring into duplication */ |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 7126 | if (flags & (BTRFS_BLOCK_GROUP_RAID1_MASK | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7127 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7128 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7129 | } else { |
| 7130 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7131 | if (flags & stripped) |
| 7132 | return flags; |
| 7133 | |
| 7134 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7135 | stripped = flags & ~stripped; |
| 7136 | |
| 7137 | /* switch duplicated blocks with raid1 */ |
| 7138 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 7139 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 7140 | |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 7141 | /* this is drive concat, leave it alone */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7142 | } |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 7143 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7144 | return flags; |
| 7145 | } |
| 7146 | |
Qu Wenruo | e913814 | 2019-07-19 14:51:43 +0800 | [diff] [blame] | 7147 | /* |
| 7148 | * Mark block group @cache read-only, so later write won't happen to block |
| 7149 | * group @cache. |
| 7150 | * |
| 7151 | * If @force is not set, this function will only mark the block group readonly |
| 7152 | * if we have enough free space (1M) in other metadata/system block groups. |
| 7153 | * If @force is not set, this function will mark the block group readonly |
| 7154 | * without checking free space. |
| 7155 | * |
| 7156 | * NOTE: This function doesn't care if other block groups can contain all the |
| 7157 | * data in this block group. That check should be done by relocation routine, |
| 7158 | * not this function. |
| 7159 | */ |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 7160 | static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7161 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7162 | struct btrfs_space_info *sinfo = cache->space_info; |
| 7163 | u64 num_bytes; |
Qu Wenruo | 3ece54e | 2019-01-30 13:07:51 +0800 | [diff] [blame] | 7164 | u64 sinfo_used; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7165 | u64 min_allocable_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7166 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7167 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7168 | /* |
| 7169 | * We need some metadata space and system metadata space for |
| 7170 | * allocating chunks in some corner cases until we force to set |
| 7171 | * it to be readonly. |
| 7172 | */ |
| 7173 | if ((sinfo->flags & |
| 7174 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && |
| 7175 | !force) |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 7176 | min_allocable_bytes = SZ_1M; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7177 | else |
| 7178 | min_allocable_bytes = 0; |
| 7179 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7180 | spin_lock(&sinfo->lock); |
| 7181 | spin_lock(&cache->lock); |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 7182 | |
| 7183 | if (cache->ro) { |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 7184 | cache->ro++; |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 7185 | ret = 0; |
| 7186 | goto out; |
| 7187 | } |
| 7188 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7189 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 7190 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Qu Wenruo | 3ece54e | 2019-01-30 13:07:51 +0800 | [diff] [blame] | 7191 | sinfo_used = btrfs_space_info_used(sinfo, true); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7192 | |
Qu Wenruo | e913814 | 2019-07-19 14:51:43 +0800 | [diff] [blame] | 7193 | /* |
| 7194 | * sinfo_used + num_bytes should always <= sinfo->total_bytes. |
| 7195 | * |
| 7196 | * Here we make sure if we mark this bg RO, we still have enough |
| 7197 | * free space as buffer (if min_allocable_bytes is not 0). |
| 7198 | */ |
Qu Wenruo | 3ece54e | 2019-01-30 13:07:51 +0800 | [diff] [blame] | 7199 | if (sinfo_used + num_bytes + min_allocable_bytes <= |
| 7200 | sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7201 | sinfo->bytes_readonly += num_bytes; |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 7202 | cache->ro++; |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 7203 | list_add_tail(&cache->ro_list, &sinfo->ro_bgs); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7204 | ret = 0; |
| 7205 | } |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 7206 | out: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7207 | spin_unlock(&cache->lock); |
| 7208 | spin_unlock(&sinfo->lock); |
Qu Wenruo | 3ece54e | 2019-01-30 13:07:51 +0800 | [diff] [blame] | 7209 | if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) { |
| 7210 | btrfs_info(cache->fs_info, |
| 7211 | "unable to make block group %llu ro", |
| 7212 | cache->key.objectid); |
| 7213 | btrfs_info(cache->fs_info, |
| 7214 | "sinfo_used=%llu bg_num_bytes=%llu min_allocable=%llu", |
| 7215 | sinfo_used, num_bytes, min_allocable_bytes); |
Josef Bacik | 5da6afe | 2019-06-18 16:09:24 -0400 | [diff] [blame] | 7216 | btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0); |
Qu Wenruo | 3ece54e | 2019-01-30 13:07:51 +0800 | [diff] [blame] | 7217 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7218 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7219 | } |
| 7220 | |
Nikolay Borisov | c83488a | 2018-06-20 15:49:14 +0300 | [diff] [blame] | 7221 | int btrfs_inc_block_group_ro(struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7222 | |
| 7223 | { |
Nikolay Borisov | c83488a | 2018-06-20 15:49:14 +0300 | [diff] [blame] | 7224 | struct btrfs_fs_info *fs_info = cache->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7225 | struct btrfs_trans_handle *trans; |
| 7226 | u64 alloc_flags; |
| 7227 | int ret; |
| 7228 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 7229 | again: |
Jeff Mahoney | 5e00f19 | 2017-02-15 16:28:29 -0500 | [diff] [blame] | 7230 | trans = btrfs_join_transaction(fs_info->extent_root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7231 | if (IS_ERR(trans)) |
| 7232 | return PTR_ERR(trans); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7233 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 7234 | /* |
| 7235 | * we're not allowed to set block groups readonly after the dirty |
| 7236 | * block groups cache has started writing. If it already started, |
| 7237 | * back off and let this transaction commit |
| 7238 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7239 | mutex_lock(&fs_info->ro_block_group_mutex); |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 7240 | if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 7241 | u64 transid = trans->transid; |
| 7242 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7243 | mutex_unlock(&fs_info->ro_block_group_mutex); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 7244 | btrfs_end_transaction(trans); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 7245 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7246 | ret = btrfs_wait_for_commit(fs_info, transid); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 7247 | if (ret) |
| 7248 | return ret; |
| 7249 | goto again; |
| 7250 | } |
| 7251 | |
Chris Mason | 153c35b | 2015-05-19 18:54:41 -0700 | [diff] [blame] | 7252 | /* |
| 7253 | * if we are changing raid levels, try to allocate a corresponding |
| 7254 | * block group with the new raid level. |
| 7255 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7256 | alloc_flags = update_block_group_flags(fs_info, cache->flags); |
Chris Mason | 153c35b | 2015-05-19 18:54:41 -0700 | [diff] [blame] | 7257 | if (alloc_flags != cache->flags) { |
Josef Bacik | fc471cb | 2019-06-18 16:09:17 -0400 | [diff] [blame] | 7258 | ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE); |
Chris Mason | 153c35b | 2015-05-19 18:54:41 -0700 | [diff] [blame] | 7259 | /* |
| 7260 | * ENOSPC is allowed here, we may have enough space |
| 7261 | * already allocated at the new raid level to |
| 7262 | * carry on |
| 7263 | */ |
| 7264 | if (ret == -ENOSPC) |
| 7265 | ret = 0; |
| 7266 | if (ret < 0) |
| 7267 | goto out; |
| 7268 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 7269 | |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 7270 | ret = inc_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7271 | if (!ret) |
| 7272 | goto out; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7273 | alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags); |
Josef Bacik | fc471cb | 2019-06-18 16:09:17 -0400 | [diff] [blame] | 7274 | ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7275 | if (ret < 0) |
| 7276 | goto out; |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 7277 | ret = inc_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7278 | out: |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 7279 | if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7280 | alloc_flags = update_block_group_flags(fs_info, cache->flags); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7281 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 7282 | check_system_chunk(trans, alloc_flags); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7283 | mutex_unlock(&fs_info->chunk_mutex); |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 7284 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7285 | mutex_unlock(&fs_info->ro_block_group_mutex); |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 7286 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 7287 | btrfs_end_transaction(trans); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7288 | return ret; |
| 7289 | } |
| 7290 | |
Nikolay Borisov | 43a7e99 | 2018-06-20 15:49:15 +0300 | [diff] [blame] | 7291 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type) |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 7292 | { |
Nikolay Borisov | 43a7e99 | 2018-06-20 15:49:15 +0300 | [diff] [blame] | 7293 | u64 alloc_flags = get_alloc_profile(trans->fs_info, type); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7294 | |
Josef Bacik | fc471cb | 2019-06-18 16:09:17 -0400 | [diff] [blame] | 7295 | return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE); |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 7296 | } |
| 7297 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 7298 | /* |
| 7299 | * helper to account the unused space of all the readonly block group in the |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 7300 | * space_info. takes mirrors into account. |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 7301 | */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 7302 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 7303 | { |
| 7304 | struct btrfs_block_group_cache *block_group; |
| 7305 | u64 free_bytes = 0; |
| 7306 | int factor; |
| 7307 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7308 | /* It's df, we don't care if it's racy */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 7309 | if (list_empty(&sinfo->ro_bgs)) |
| 7310 | return 0; |
| 7311 | |
| 7312 | spin_lock(&sinfo->lock); |
| 7313 | list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) { |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 7314 | spin_lock(&block_group->lock); |
| 7315 | |
| 7316 | if (!block_group->ro) { |
| 7317 | spin_unlock(&block_group->lock); |
| 7318 | continue; |
| 7319 | } |
| 7320 | |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7321 | factor = btrfs_bg_type_to_factor(block_group->flags); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 7322 | free_bytes += (block_group->key.offset - |
| 7323 | btrfs_block_group_used(&block_group->item)) * |
| 7324 | factor; |
| 7325 | |
| 7326 | spin_unlock(&block_group->lock); |
| 7327 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 7328 | spin_unlock(&sinfo->lock); |
| 7329 | |
| 7330 | return free_bytes; |
| 7331 | } |
| 7332 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7333 | void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7334 | { |
| 7335 | struct btrfs_space_info *sinfo = cache->space_info; |
| 7336 | u64 num_bytes; |
| 7337 | |
| 7338 | BUG_ON(!cache->ro); |
| 7339 | |
| 7340 | spin_lock(&sinfo->lock); |
| 7341 | spin_lock(&cache->lock); |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 7342 | if (!--cache->ro) { |
| 7343 | num_bytes = cache->key.offset - cache->reserved - |
| 7344 | cache->pinned - cache->bytes_super - |
| 7345 | btrfs_block_group_used(&cache->item); |
| 7346 | sinfo->bytes_readonly -= num_bytes; |
| 7347 | list_del_init(&cache->ro_list); |
| 7348 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7349 | spin_unlock(&cache->lock); |
| 7350 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7351 | } |
| 7352 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7353 | static int find_first_block_group(struct btrfs_fs_info *fs_info, |
| 7354 | struct btrfs_path *path, |
| 7355 | struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7356 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7357 | struct btrfs_root *root = fs_info->extent_root; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7358 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7359 | struct btrfs_key found_key; |
| 7360 | struct extent_buffer *leaf; |
Qu Wenruo | 514c7dc | 2018-08-01 10:37:16 +0800 | [diff] [blame] | 7361 | struct btrfs_block_group_item bg; |
| 7362 | u64 flags; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7363 | int slot; |
| 7364 | |
| 7365 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 7366 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7367 | goto out; |
| 7368 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7369 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7370 | slot = path->slots[0]; |
| 7371 | leaf = path->nodes[0]; |
| 7372 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7373 | ret = btrfs_next_leaf(root, path); |
| 7374 | if (ret == 0) |
| 7375 | continue; |
| 7376 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7377 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7378 | break; |
| 7379 | } |
| 7380 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 7381 | |
| 7382 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7383 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Liu Bo | 6fb37b7 | 2016-06-22 18:31:27 -0700 | [diff] [blame] | 7384 | struct extent_map_tree *em_tree; |
| 7385 | struct extent_map *em; |
| 7386 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7387 | em_tree = &root->fs_info->mapping_tree; |
Liu Bo | 6fb37b7 | 2016-06-22 18:31:27 -0700 | [diff] [blame] | 7388 | read_lock(&em_tree->lock); |
| 7389 | em = lookup_extent_mapping(em_tree, found_key.objectid, |
| 7390 | found_key.offset); |
| 7391 | read_unlock(&em_tree->lock); |
| 7392 | if (!em) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7393 | btrfs_err(fs_info, |
Liu Bo | 6fb37b7 | 2016-06-22 18:31:27 -0700 | [diff] [blame] | 7394 | "logical %llu len %llu found bg but no related chunk", |
| 7395 | found_key.objectid, found_key.offset); |
| 7396 | ret = -ENOENT; |
Qu Wenruo | 514c7dc | 2018-08-01 10:37:16 +0800 | [diff] [blame] | 7397 | } else if (em->start != found_key.objectid || |
| 7398 | em->len != found_key.offset) { |
| 7399 | btrfs_err(fs_info, |
| 7400 | "block group %llu len %llu mismatch with chunk %llu len %llu", |
| 7401 | found_key.objectid, found_key.offset, |
| 7402 | em->start, em->len); |
| 7403 | ret = -EUCLEAN; |
Liu Bo | 6fb37b7 | 2016-06-22 18:31:27 -0700 | [diff] [blame] | 7404 | } else { |
Qu Wenruo | 514c7dc | 2018-08-01 10:37:16 +0800 | [diff] [blame] | 7405 | read_extent_buffer(leaf, &bg, |
| 7406 | btrfs_item_ptr_offset(leaf, slot), |
| 7407 | sizeof(bg)); |
| 7408 | flags = btrfs_block_group_flags(&bg) & |
| 7409 | BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 7410 | |
| 7411 | if (flags != (em->map_lookup->type & |
| 7412 | BTRFS_BLOCK_GROUP_TYPE_MASK)) { |
| 7413 | btrfs_err(fs_info, |
| 7414 | "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx", |
| 7415 | found_key.objectid, |
| 7416 | found_key.offset, flags, |
| 7417 | (BTRFS_BLOCK_GROUP_TYPE_MASK & |
| 7418 | em->map_lookup->type)); |
| 7419 | ret = -EUCLEAN; |
| 7420 | } else { |
| 7421 | ret = 0; |
| 7422 | } |
Liu Bo | 6fb37b7 | 2016-06-22 18:31:27 -0700 | [diff] [blame] | 7423 | } |
Josef Bacik | 187ee58 | 2016-08-18 15:30:06 -0400 | [diff] [blame] | 7424 | free_extent_map(em); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7425 | goto out; |
| 7426 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7427 | path->slots[0]++; |
| 7428 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7429 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7430 | return ret; |
| 7431 | } |
| 7432 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7433 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 7434 | { |
| 7435 | struct btrfs_block_group_cache *block_group; |
| 7436 | u64 last = 0; |
| 7437 | |
| 7438 | while (1) { |
| 7439 | struct inode *inode; |
| 7440 | |
| 7441 | block_group = btrfs_lookup_first_block_group(info, last); |
| 7442 | while (block_group) { |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 7443 | btrfs_wait_block_group_cache_done(block_group); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7444 | spin_lock(&block_group->lock); |
| 7445 | if (block_group->iref) |
| 7446 | break; |
| 7447 | spin_unlock(&block_group->lock); |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 7448 | block_group = btrfs_next_block_group(block_group); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7449 | } |
| 7450 | if (!block_group) { |
| 7451 | if (last == 0) |
| 7452 | break; |
| 7453 | last = 0; |
| 7454 | continue; |
| 7455 | } |
| 7456 | |
| 7457 | inode = block_group->inode; |
| 7458 | block_group->iref = 0; |
| 7459 | block_group->inode = NULL; |
| 7460 | spin_unlock(&block_group->lock); |
Liu Bo | f3bca80 | 2016-07-20 17:33:44 -0700 | [diff] [blame] | 7461 | ASSERT(block_group->io_ctl.inode == NULL); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7462 | iput(inode); |
| 7463 | last = block_group->key.objectid + block_group->key.offset; |
| 7464 | btrfs_put_block_group(block_group); |
| 7465 | } |
| 7466 | } |
| 7467 | |
Filipe Manana | 5cdd7db | 2017-02-01 22:39:50 +0000 | [diff] [blame] | 7468 | /* |
| 7469 | * Must be called only after stopping all workers, since we could have block |
| 7470 | * group caching kthreads running, and therefore they could race with us if we |
| 7471 | * freed the block groups before stopping them. |
| 7472 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7473 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 7474 | { |
| 7475 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7476 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7477 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7478 | struct rb_node *n; |
| 7479 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 7480 | down_write(&info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7481 | while (!list_empty(&info->caching_block_groups)) { |
| 7482 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 7483 | struct btrfs_caching_control, list); |
| 7484 | list_del(&caching_ctl->list); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 7485 | btrfs_put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7486 | } |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 7487 | up_write(&info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7488 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 7489 | spin_lock(&info->unused_bgs_lock); |
| 7490 | while (!list_empty(&info->unused_bgs)) { |
| 7491 | block_group = list_first_entry(&info->unused_bgs, |
| 7492 | struct btrfs_block_group_cache, |
| 7493 | bg_list); |
| 7494 | list_del_init(&block_group->bg_list); |
| 7495 | btrfs_put_block_group(block_group); |
| 7496 | } |
| 7497 | spin_unlock(&info->unused_bgs_lock); |
| 7498 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7499 | spin_lock(&info->block_group_cache_lock); |
| 7500 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 7501 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 7502 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7503 | rb_erase(&block_group->cache_node, |
| 7504 | &info->block_group_cache_tree); |
Filipe Manana | 01eacb2 | 2014-12-04 18:38:30 +0000 | [diff] [blame] | 7505 | RB_CLEAR_NODE(&block_group->cache_node); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7506 | spin_unlock(&info->block_group_cache_lock); |
| 7507 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7508 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7509 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7510 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7511 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 7512 | /* |
| 7513 | * We haven't cached this block group, which means we could |
| 7514 | * possibly have excluded extents on this block group. |
| 7515 | */ |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7516 | if (block_group->cached == BTRFS_CACHE_NO || |
| 7517 | block_group->cached == BTRFS_CACHE_ERROR) |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 7518 | btrfs_free_excluded_extents(block_group); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 7519 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7520 | btrfs_remove_free_space_cache(block_group); |
Filipe Manana | 5cdd7db | 2017-02-01 22:39:50 +0000 | [diff] [blame] | 7521 | ASSERT(block_group->cached != BTRFS_CACHE_STARTED); |
Liu Bo | f3bca80 | 2016-07-20 17:33:44 -0700 | [diff] [blame] | 7522 | ASSERT(list_empty(&block_group->dirty_list)); |
| 7523 | ASSERT(list_empty(&block_group->io_list)); |
| 7524 | ASSERT(list_empty(&block_group->bg_list)); |
| 7525 | ASSERT(atomic_read(&block_group->count) == 1); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 7526 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7527 | |
| 7528 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7529 | } |
| 7530 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7531 | |
| 7532 | /* now that all the block groups are freed, go through and |
| 7533 | * free all the space_info structs. This is only called during |
| 7534 | * the final stages of unmount, and so we know nobody is |
| 7535 | * using them. We call synchronize_rcu() once before we start, |
| 7536 | * just to be on the safe side. |
| 7537 | */ |
| 7538 | synchronize_rcu(); |
| 7539 | |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 7540 | btrfs_release_global_block_rsv(info); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 7541 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 7542 | while (!list_empty(&info->space_info)) { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7543 | int i; |
| 7544 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7545 | space_info = list_entry(info->space_info.next, |
| 7546 | struct btrfs_space_info, |
| 7547 | list); |
Josef Bacik | d555b6c | 2016-03-25 13:25:51 -0400 | [diff] [blame] | 7548 | |
| 7549 | /* |
| 7550 | * Do not hide this behind enospc_debug, this is actually |
| 7551 | * important and indicates a real bug if this happens. |
| 7552 | */ |
| 7553 | if (WARN_ON(space_info->bytes_pinned > 0 || |
David Sterba | b069e0c | 2013-02-08 21:28:17 +0000 | [diff] [blame] | 7554 | space_info->bytes_reserved > 0 || |
Josef Bacik | d555b6c | 2016-03-25 13:25:51 -0400 | [diff] [blame] | 7555 | space_info->bytes_may_use > 0)) |
Josef Bacik | 5da6afe | 2019-06-18 16:09:24 -0400 | [diff] [blame] | 7556 | btrfs_dump_space_info(info, space_info, 0, 0); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7557 | list_del(&space_info->list); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7558 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
| 7559 | struct kobject *kobj; |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 7560 | kobj = space_info->block_group_kobjs[i]; |
| 7561 | space_info->block_group_kobjs[i] = NULL; |
| 7562 | if (kobj) { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7563 | kobject_del(kobj); |
| 7564 | kobject_put(kobj); |
| 7565 | } |
| 7566 | } |
| 7567 | kobject_del(&space_info->kobj); |
| 7568 | kobject_put(&space_info->kobj); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7569 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7570 | return 0; |
| 7571 | } |
| 7572 | |
Nikolay Borisov | c434d21 | 2017-08-21 12:43:50 +0300 | [diff] [blame] | 7573 | static void link_block_group(struct btrfs_block_group_cache *cache) |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7574 | { |
Nikolay Borisov | c434d21 | 2017-08-21 12:43:50 +0300 | [diff] [blame] | 7575 | struct btrfs_space_info *space_info = cache->space_info; |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 7576 | struct btrfs_fs_info *fs_info = cache->fs_info; |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 7577 | int index = btrfs_bg_flags_to_raid_index(cache->flags); |
Jeff Mahoney | ed55b6a | 2014-03-26 14:11:26 -0400 | [diff] [blame] | 7578 | bool first = false; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7579 | |
| 7580 | down_write(&space_info->groups_sem); |
Jeff Mahoney | ed55b6a | 2014-03-26 14:11:26 -0400 | [diff] [blame] | 7581 | if (list_empty(&space_info->block_groups[index])) |
| 7582 | first = true; |
| 7583 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 7584 | up_write(&space_info->groups_sem); |
| 7585 | |
| 7586 | if (first) { |
Filipe Manana | d7cd4dd | 2019-08-07 11:21:46 +0100 | [diff] [blame] | 7587 | struct raid_kobject *rkobj; |
| 7588 | unsigned int nofs_flag; |
| 7589 | int ret; |
| 7590 | |
| 7591 | /* |
| 7592 | * Setup a NOFS context because kobject_add(), deep in its call |
| 7593 | * chain, does GFP_KERNEL allocations, and we are often called |
| 7594 | * in a context where if reclaim is triggered we can deadlock |
| 7595 | * (we are either holding a transaction handle or some lock |
| 7596 | * required for a transaction commit). |
| 7597 | */ |
| 7598 | nofs_flag = memalloc_nofs_save(); |
| 7599 | rkobj = kzalloc(sizeof(*rkobj), GFP_KERNEL); |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 7600 | if (!rkobj) { |
Filipe Manana | d7cd4dd | 2019-08-07 11:21:46 +0100 | [diff] [blame] | 7601 | memalloc_nofs_restore(nofs_flag); |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 7602 | btrfs_warn(cache->fs_info, |
| 7603 | "couldn't alloc memory for raid level kobject"); |
| 7604 | return; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7605 | } |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 7606 | rkobj->flags = cache->flags; |
| 7607 | kobject_init(&rkobj->kobj, &btrfs_raid_ktype); |
Filipe Manana | d7cd4dd | 2019-08-07 11:21:46 +0100 | [diff] [blame] | 7608 | ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s", |
| 7609 | btrfs_bg_type_to_raid_name(rkobj->flags)); |
| 7610 | memalloc_nofs_restore(nofs_flag); |
| 7611 | if (ret) { |
| 7612 | kobject_put(&rkobj->kobj); |
| 7613 | btrfs_warn(fs_info, |
| 7614 | "failed to add kobject for block cache, ignoring"); |
| 7615 | return; |
| 7616 | } |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 7617 | space_info->block_group_kobjs[index] = &rkobj->kobj; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7618 | } |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7619 | } |
| 7620 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 7621 | static struct btrfs_block_group_cache * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7622 | btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info, |
| 7623 | u64 start, u64 size) |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 7624 | { |
| 7625 | struct btrfs_block_group_cache *cache; |
| 7626 | |
| 7627 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
| 7628 | if (!cache) |
| 7629 | return NULL; |
| 7630 | |
| 7631 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 7632 | GFP_NOFS); |
| 7633 | if (!cache->free_space_ctl) { |
| 7634 | kfree(cache); |
| 7635 | return NULL; |
| 7636 | } |
| 7637 | |
| 7638 | cache->key.objectid = start; |
| 7639 | cache->key.offset = size; |
| 7640 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
| 7641 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7642 | cache->fs_info = fs_info; |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 7643 | cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 7644 | set_free_space_tree_thresholds(cache); |
| 7645 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 7646 | atomic_set(&cache->count, 1); |
| 7647 | spin_lock_init(&cache->lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7648 | init_rwsem(&cache->data_rwsem); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 7649 | INIT_LIST_HEAD(&cache->list); |
| 7650 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 7651 | INIT_LIST_HEAD(&cache->bg_list); |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 7652 | INIT_LIST_HEAD(&cache->ro_list); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 7653 | INIT_LIST_HEAD(&cache->dirty_list); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 7654 | INIT_LIST_HEAD(&cache->io_list); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 7655 | btrfs_init_free_space_ctl(cache); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 7656 | atomic_set(&cache->trimming, 0); |
Omar Sandoval | a5ed918 | 2015-09-29 20:50:35 -0700 | [diff] [blame] | 7657 | mutex_init(&cache->free_space_lock); |
Qu Wenruo | 0966a7b | 2017-04-14 08:35:54 +0800 | [diff] [blame] | 7658 | btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 7659 | |
| 7660 | return cache; |
| 7661 | } |
| 7662 | |
Qu Wenruo | 7ef4951 | 2018-08-01 10:37:17 +0800 | [diff] [blame] | 7663 | |
| 7664 | /* |
| 7665 | * Iterate all chunks and verify that each of them has the corresponding block |
| 7666 | * group |
| 7667 | */ |
| 7668 | static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info) |
| 7669 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7670 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Qu Wenruo | 7ef4951 | 2018-08-01 10:37:17 +0800 | [diff] [blame] | 7671 | struct extent_map *em; |
| 7672 | struct btrfs_block_group_cache *bg; |
| 7673 | u64 start = 0; |
| 7674 | int ret = 0; |
| 7675 | |
| 7676 | while (1) { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7677 | read_lock(&map_tree->lock); |
Qu Wenruo | 7ef4951 | 2018-08-01 10:37:17 +0800 | [diff] [blame] | 7678 | /* |
| 7679 | * lookup_extent_mapping will return the first extent map |
| 7680 | * intersecting the range, so setting @len to 1 is enough to |
| 7681 | * get the first chunk. |
| 7682 | */ |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7683 | em = lookup_extent_mapping(map_tree, start, 1); |
| 7684 | read_unlock(&map_tree->lock); |
Qu Wenruo | 7ef4951 | 2018-08-01 10:37:17 +0800 | [diff] [blame] | 7685 | if (!em) |
| 7686 | break; |
| 7687 | |
| 7688 | bg = btrfs_lookup_block_group(fs_info, em->start); |
| 7689 | if (!bg) { |
| 7690 | btrfs_err(fs_info, |
| 7691 | "chunk start=%llu len=%llu doesn't have corresponding block group", |
| 7692 | em->start, em->len); |
| 7693 | ret = -EUCLEAN; |
| 7694 | free_extent_map(em); |
| 7695 | break; |
| 7696 | } |
| 7697 | if (bg->key.objectid != em->start || |
| 7698 | bg->key.offset != em->len || |
| 7699 | (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != |
| 7700 | (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { |
| 7701 | btrfs_err(fs_info, |
| 7702 | "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx", |
| 7703 | em->start, em->len, |
| 7704 | em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK, |
| 7705 | bg->key.objectid, bg->key.offset, |
| 7706 | bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK); |
| 7707 | ret = -EUCLEAN; |
| 7708 | free_extent_map(em); |
| 7709 | btrfs_put_block_group(bg); |
| 7710 | break; |
| 7711 | } |
| 7712 | start = em->start + em->len; |
| 7713 | free_extent_map(em); |
| 7714 | btrfs_put_block_group(bg); |
| 7715 | } |
| 7716 | return ret; |
| 7717 | } |
| 7718 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7719 | int btrfs_read_block_groups(struct btrfs_fs_info *info) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7720 | { |
| 7721 | struct btrfs_path *path; |
| 7722 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7723 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7724 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7725 | struct btrfs_key key; |
| 7726 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7727 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7728 | int need_clear = 0; |
| 7729 | u64 cache_gen; |
Liu Bo | 4930338 | 2016-08-25 18:08:27 -0700 | [diff] [blame] | 7730 | u64 feature; |
| 7731 | int mixed; |
| 7732 | |
| 7733 | feature = btrfs_super_incompat_flags(info->super_copy); |
| 7734 | mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7735 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7736 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7737 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 7738 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7739 | path = btrfs_alloc_path(); |
| 7740 | if (!path) |
| 7741 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 7742 | path->reada = READA_FORWARD; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7743 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7744 | cache_gen = btrfs_super_cache_generation(info->super_copy); |
| 7745 | if (btrfs_test_opt(info, SPACE_CACHE) && |
| 7746 | btrfs_super_generation(info->super_copy) != cache_gen) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7747 | need_clear = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7748 | if (btrfs_test_opt(info, CLEAR_CACHE)) |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 7749 | need_clear = 1; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7750 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7751 | while (1) { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7752 | ret = find_first_block_group(info, path, &key); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7753 | if (ret > 0) |
| 7754 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7755 | if (ret != 0) |
| 7756 | goto error; |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 7757 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7758 | leaf = path->nodes[0]; |
| 7759 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 7760 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7761 | cache = btrfs_create_block_group_cache(info, found_key.objectid, |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 7762 | found_key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7763 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7764 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7765 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7766 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7767 | |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 7768 | if (need_clear) { |
| 7769 | /* |
| 7770 | * When we mount with old space cache, we need to |
| 7771 | * set BTRFS_DC_CLEAR and set dirty flag. |
| 7772 | * |
| 7773 | * a) Setting 'BTRFS_DC_CLEAR' makes sure that we |
| 7774 | * truncate the old free space cache inode and |
| 7775 | * setup a new one. |
| 7776 | * b) Setting 'dirty flag' makes sure that we flush |
| 7777 | * the new space cache info onto disk. |
| 7778 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7779 | if (btrfs_test_opt(info, SPACE_CACHE)) |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 7780 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 7781 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7782 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7783 | read_extent_buffer(leaf, &cache->item, |
| 7784 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 7785 | sizeof(cache->item)); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 7786 | cache->flags = btrfs_block_group_flags(&cache->item); |
Liu Bo | 4930338 | 2016-08-25 18:08:27 -0700 | [diff] [blame] | 7787 | if (!mixed && |
| 7788 | ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && |
| 7789 | (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { |
| 7790 | btrfs_err(info, |
| 7791 | "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", |
| 7792 | cache->key.objectid); |
| 7793 | ret = -EINVAL; |
| 7794 | goto error; |
| 7795 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7796 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7797 | key.objectid = found_key.objectid + found_key.offset; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7798 | btrfs_release_path(path); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7799 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7800 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 7801 | * We need to exclude the super stripes now so that the space |
| 7802 | * info has super bytes accounted for, otherwise we'll think |
| 7803 | * we have more space than we actually do. |
| 7804 | */ |
Nikolay Borisov | 3c4da65 | 2018-06-20 15:49:09 +0300 | [diff] [blame] | 7805 | ret = exclude_super_stripes(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 7806 | if (ret) { |
| 7807 | /* |
| 7808 | * We may have excluded something, so call this just in |
| 7809 | * case. |
| 7810 | */ |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 7811 | btrfs_free_excluded_extents(cache); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 7812 | btrfs_put_block_group(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 7813 | goto error; |
| 7814 | } |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 7815 | |
| 7816 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7817 | * check for two cases, either we are full, and therefore |
| 7818 | * don't need to bother with the caching work since we won't |
| 7819 | * find any space, or we are empty, and we can just add all |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 7820 | * the space in and be done with it. This saves us _a_lot_ of |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7821 | * time, particularly in the full case. |
| 7822 | */ |
| 7823 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7824 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7825 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 7826 | btrfs_free_excluded_extents(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7827 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7828 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7829 | cache->cached = BTRFS_CACHE_FINISHED; |
Nikolay Borisov | 4457c1c | 2018-05-10 15:44:45 +0300 | [diff] [blame] | 7830 | add_new_free_space(cache, found_key.objectid, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7831 | found_key.objectid + |
| 7832 | found_key.offset); |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 7833 | btrfs_free_excluded_extents(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7834 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7835 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7836 | ret = btrfs_add_block_group_cache(info, cache); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 7837 | if (ret) { |
| 7838 | btrfs_remove_free_space_cache(cache); |
| 7839 | btrfs_put_block_group(cache); |
| 7840 | goto error; |
| 7841 | } |
| 7842 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7843 | trace_btrfs_add_block_group(info, cache, 0); |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 7844 | btrfs_update_space_info(info, cache->flags, found_key.offset, |
| 7845 | btrfs_block_group_used(&cache->item), |
| 7846 | cache->bytes_super, &space_info); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 7847 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7848 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7849 | |
Nikolay Borisov | c434d21 | 2017-08-21 12:43:50 +0300 | [diff] [blame] | 7850 | link_block_group(cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7851 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7852 | set_avail_alloc_bits(info, cache->flags); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7853 | if (btrfs_chunk_readonly(info, cache->key.objectid)) { |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 7854 | inc_block_group_ro(cache, 1); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 7855 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Qu Wenruo | 031f24d | 2018-05-22 16:43:47 +0800 | [diff] [blame] | 7856 | ASSERT(list_empty(&cache->bg_list)); |
| 7857 | btrfs_mark_bg_unused(cache); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 7858 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7859 | } |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7860 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7861 | list_for_each_entry_rcu(space_info, &info->space_info, list) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7862 | if (!(get_alloc_profile(info, space_info->flags) & |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7863 | (BTRFS_BLOCK_GROUP_RAID10 | |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 7864 | BTRFS_BLOCK_GROUP_RAID1_MASK | |
David Sterba | a07e8a4 | 2019-05-31 16:54:26 +0200 | [diff] [blame] | 7865 | BTRFS_BLOCK_GROUP_RAID56_MASK | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7866 | BTRFS_BLOCK_GROUP_DUP))) |
| 7867 | continue; |
| 7868 | /* |
| 7869 | * avoid allocating from un-mirrored block group if there are |
| 7870 | * mirrored block groups. |
| 7871 | */ |
chandan | 1095cc0 | 2013-07-16 12:28:56 +0530 | [diff] [blame] | 7872 | list_for_each_entry(cache, |
| 7873 | &space_info->block_groups[BTRFS_RAID_RAID0], |
| 7874 | list) |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 7875 | inc_block_group_ro(cache, 1); |
chandan | 1095cc0 | 2013-07-16 12:28:56 +0530 | [diff] [blame] | 7876 | list_for_each_entry(cache, |
| 7877 | &space_info->block_groups[BTRFS_RAID_SINGLE], |
| 7878 | list) |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 7879 | inc_block_group_ro(cache, 1); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7880 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7881 | |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 7882 | btrfs_init_global_block_rsv(info); |
Qu Wenruo | 7ef4951 | 2018-08-01 10:37:17 +0800 | [diff] [blame] | 7883 | ret = check_chunk_block_group_mappings(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7884 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7885 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7886 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7887 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7888 | |
Nikolay Borisov | 6c686b3 | 2018-02-07 17:55:40 +0200 | [diff] [blame] | 7889 | void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7890 | { |
Nikolay Borisov | 6c686b3 | 2018-02-07 17:55:40 +0200 | [diff] [blame] | 7891 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 545e336 | 2018-09-28 07:18:02 -0400 | [diff] [blame] | 7892 | struct btrfs_block_group_cache *block_group; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7893 | struct btrfs_root *extent_root = fs_info->extent_root; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7894 | struct btrfs_block_group_item item; |
| 7895 | struct btrfs_key key; |
| 7896 | int ret = 0; |
| 7897 | |
Filipe Manana | 5ce5555 | 2018-10-12 10:03:55 +0100 | [diff] [blame] | 7898 | if (!trans->can_flush_pending_bgs) |
| 7899 | return; |
| 7900 | |
Josef Bacik | 545e336 | 2018-09-28 07:18:02 -0400 | [diff] [blame] | 7901 | while (!list_empty(&trans->new_bgs)) { |
| 7902 | block_group = list_first_entry(&trans->new_bgs, |
| 7903 | struct btrfs_block_group_cache, |
| 7904 | bg_list); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7905 | if (ret) |
Filipe Manana | c92f6be | 2014-11-26 15:28:55 +0000 | [diff] [blame] | 7906 | goto next; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7907 | |
| 7908 | spin_lock(&block_group->lock); |
| 7909 | memcpy(&item, &block_group->item, sizeof(item)); |
| 7910 | memcpy(&key, &block_group->key, sizeof(key)); |
| 7911 | spin_unlock(&block_group->lock); |
| 7912 | |
| 7913 | ret = btrfs_insert_item(trans, extent_root, &key, &item, |
| 7914 | sizeof(item)); |
| 7915 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7916 | btrfs_abort_transaction(trans, ret); |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 7917 | ret = btrfs_finish_chunk_alloc(trans, key.objectid, key.offset); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 7918 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7919 | btrfs_abort_transaction(trans, ret); |
Nikolay Borisov | e4e0711 | 2018-05-10 15:44:41 +0300 | [diff] [blame] | 7920 | add_block_group_free_space(trans, block_group); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 7921 | /* already aborted the transaction if it failed. */ |
Filipe Manana | c92f6be | 2014-11-26 15:28:55 +0000 | [diff] [blame] | 7922 | next: |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 7923 | btrfs_delayed_refs_rsv_release(fs_info, 1); |
Filipe Manana | c92f6be | 2014-11-26 15:28:55 +0000 | [diff] [blame] | 7924 | list_del_init(&block_group->bg_list); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7925 | } |
Filipe Manana | 5ce5555 | 2018-10-12 10:03:55 +0100 | [diff] [blame] | 7926 | btrfs_trans_release_chunk_metadata(trans); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7927 | } |
| 7928 | |
Nikolay Borisov | e7e0209 | 2018-06-20 15:48:55 +0300 | [diff] [blame] | 7929 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used, |
Nikolay Borisov | 0174484 | 2017-07-27 14:22:11 +0300 | [diff] [blame] | 7930 | u64 type, u64 chunk_offset, u64 size) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7931 | { |
Nikolay Borisov | e7e0209 | 2018-06-20 15:48:55 +0300 | [diff] [blame] | 7932 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7933 | struct btrfs_block_group_cache *cache; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7934 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7935 | |
David Sterba | 9078776 | 2019-03-20 13:28:05 +0100 | [diff] [blame] | 7936 | btrfs_set_log_full_commit(trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7937 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7938 | cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7939 | if (!cache) |
| 7940 | return -ENOMEM; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7941 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7942 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Nikolay Borisov | 0174484 | 2017-07-27 14:22:11 +0300 | [diff] [blame] | 7943 | btrfs_set_block_group_chunk_objectid(&cache->item, |
| 7944 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7945 | btrfs_set_block_group_flags(&cache->item, type); |
| 7946 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 7947 | cache->flags = type; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7948 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7949 | cache->cached = BTRFS_CACHE_FINISHED; |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 7950 | cache->needs_free_space = 1; |
Nikolay Borisov | 3c4da65 | 2018-06-20 15:49:09 +0300 | [diff] [blame] | 7951 | ret = exclude_super_stripes(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 7952 | if (ret) { |
| 7953 | /* |
| 7954 | * We may have excluded something, so call this just in |
| 7955 | * case. |
| 7956 | */ |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 7957 | btrfs_free_excluded_extents(cache); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 7958 | btrfs_put_block_group(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 7959 | return ret; |
| 7960 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7961 | |
Nikolay Borisov | 4457c1c | 2018-05-10 15:44:45 +0300 | [diff] [blame] | 7962 | add_new_free_space(cache, chunk_offset, chunk_offset + size); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7963 | |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 7964 | btrfs_free_excluded_extents(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7965 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 7966 | #ifdef CONFIG_BTRFS_DEBUG |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7967 | if (btrfs_should_fragment_free_space(cache)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 7968 | u64 new_bytes_used = size - bytes_used; |
| 7969 | |
| 7970 | bytes_used += new_bytes_used >> 1; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7971 | fragment_free_space(cache); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 7972 | } |
| 7973 | #endif |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 7974 | /* |
Nikolay Borisov | 2be12ef | 2017-05-22 09:35:49 +0300 | [diff] [blame] | 7975 | * Ensure the corresponding space_info object is created and |
| 7976 | * assigned to our block group. We want our bg to be added to the rbtree |
| 7977 | * with its ->space_info set. |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 7978 | */ |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 7979 | cache->space_info = btrfs_find_space_info(fs_info, cache->flags); |
Jeff Mahoney | dc2d300 | 2018-03-20 15:25:25 -0400 | [diff] [blame] | 7980 | ASSERT(cache->space_info); |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 7981 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7982 | ret = btrfs_add_block_group_cache(fs_info, cache); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 7983 | if (ret) { |
| 7984 | btrfs_remove_free_space_cache(cache); |
| 7985 | btrfs_put_block_group(cache); |
| 7986 | return ret; |
| 7987 | } |
| 7988 | |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 7989 | /* |
| 7990 | * Now that our block group has its ->space_info set and is inserted in |
| 7991 | * the rbtree, update the space info's counters. |
| 7992 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7993 | trace_btrfs_add_block_group(fs_info, cache, 1); |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 7994 | btrfs_update_space_info(fs_info, cache->flags, size, bytes_used, |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 7995 | cache->bytes_super, &cache->space_info); |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 7996 | btrfs_update_global_block_rsv(fs_info); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7997 | |
Nikolay Borisov | c434d21 | 2017-08-21 12:43:50 +0300 | [diff] [blame] | 7998 | link_block_group(cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7999 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8000 | list_add_tail(&cache->bg_list, &trans->new_bgs); |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 8001 | trans->delayed_ref_updates++; |
| 8002 | btrfs_update_delayed_refs_rsv(trans); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8003 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8004 | set_avail_alloc_bits(fs_info, type); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8005 | return 0; |
| 8006 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8007 | |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8008 | static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 8009 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 8010 | u64 extra_flags = chunk_to_extended(flags) & |
| 8011 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8012 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 8013 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8014 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 8015 | fs_info->avail_data_alloc_bits &= ~extra_flags; |
| 8016 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 8017 | fs_info->avail_metadata_alloc_bits &= ~extra_flags; |
| 8018 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 8019 | fs_info->avail_system_alloc_bits &= ~extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 8020 | write_sequnlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8021 | } |
| 8022 | |
David Sterba | 6d58a55 | 2019-06-11 14:31:01 +0200 | [diff] [blame] | 8023 | /* |
| 8024 | * Clear incompat bits for the following feature(s): |
| 8025 | * |
| 8026 | * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group |
| 8027 | * in the whole filesystem |
| 8028 | */ |
| 8029 | static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 8030 | { |
| 8031 | if (flags & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 8032 | struct list_head *head = &fs_info->space_info; |
| 8033 | struct btrfs_space_info *sinfo; |
| 8034 | |
| 8035 | list_for_each_entry_rcu(sinfo, head, list) { |
| 8036 | bool found = false; |
| 8037 | |
| 8038 | down_read(&sinfo->groups_sem); |
| 8039 | if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5])) |
| 8040 | found = true; |
| 8041 | if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6])) |
| 8042 | found = true; |
| 8043 | up_read(&sinfo->groups_sem); |
| 8044 | |
| 8045 | if (found) |
| 8046 | return; |
| 8047 | } |
| 8048 | btrfs_clear_fs_incompat(fs_info, RAID56); |
| 8049 | } |
| 8050 | } |
| 8051 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8052 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 5a98ec0 | 2018-06-20 15:48:56 +0300 | [diff] [blame] | 8053 | u64 group_start, struct extent_map *em) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8054 | { |
Nikolay Borisov | 5a98ec0 | 2018-06-20 15:48:56 +0300 | [diff] [blame] | 8055 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 8056 | struct btrfs_root *root = fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8057 | struct btrfs_path *path; |
| 8058 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8059 | struct btrfs_free_cluster *cluster; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8060 | struct btrfs_root *tree_root = fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8061 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8062 | struct inode *inode; |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 8063 | struct kobject *kobj = NULL; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8064 | int ret; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8065 | int index; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8066 | int factor; |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 8067 | struct btrfs_caching_control *caching_ctl = NULL; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 8068 | bool remove_em; |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 8069 | bool remove_rsv = false; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8070 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 8071 | block_group = btrfs_lookup_block_group(fs_info, group_start); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8072 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8073 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8074 | |
Qu Wenruo | 4ed0a7a | 2018-04-26 17:17:20 +0800 | [diff] [blame] | 8075 | trace_btrfs_remove_block_group(block_group); |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 8076 | /* |
| 8077 | * Free the reserved super bytes from this block group before |
| 8078 | * remove it. |
| 8079 | */ |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 8080 | btrfs_free_excluded_extents(block_group); |
Josef Bacik | fd708b8 | 2017-09-29 15:43:50 -0400 | [diff] [blame] | 8081 | btrfs_free_ref_tree_range(fs_info, block_group->key.objectid, |
| 8082 | block_group->key.offset); |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 8083 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8084 | memcpy(&key, &block_group->key, sizeof(key)); |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 8085 | index = btrfs_bg_flags_to_raid_index(block_group->flags); |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 8086 | factor = btrfs_bg_type_to_factor(block_group->flags); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8087 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8088 | /* make sure this block group isn't part of an allocation cluster */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8089 | cluster = &fs_info->data_alloc_cluster; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8090 | spin_lock(&cluster->refill_lock); |
| 8091 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8092 | spin_unlock(&cluster->refill_lock); |
| 8093 | |
| 8094 | /* |
| 8095 | * make sure this block group isn't part of a metadata |
| 8096 | * allocation cluster |
| 8097 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8098 | cluster = &fs_info->meta_alloc_cluster; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8099 | spin_lock(&cluster->refill_lock); |
| 8100 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8101 | spin_unlock(&cluster->refill_lock); |
| 8102 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8103 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 8104 | if (!path) { |
| 8105 | ret = -ENOMEM; |
| 8106 | goto out; |
| 8107 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8108 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 8109 | /* |
| 8110 | * get the inode first so any iput calls done for the io_list |
| 8111 | * aren't the final iput (no unlinks allowed now) |
| 8112 | */ |
David Sterba | 7949f33 | 2019-03-20 13:40:19 +0100 | [diff] [blame] | 8113 | inode = lookup_free_space_inode(block_group, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 8114 | |
| 8115 | mutex_lock(&trans->transaction->cache_write_mutex); |
| 8116 | /* |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 8117 | * Make sure our free space cache IO is done before removing the |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 8118 | * free space inode |
| 8119 | */ |
| 8120 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 8121 | if (!list_empty(&block_group->io_list)) { |
| 8122 | list_del_init(&block_group->io_list); |
| 8123 | |
| 8124 | WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); |
| 8125 | |
| 8126 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 8127 | btrfs_wait_cache_io(trans, block_group, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 8128 | btrfs_put_block_group(block_group); |
| 8129 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 8130 | } |
| 8131 | |
| 8132 | if (!list_empty(&block_group->dirty_list)) { |
| 8133 | list_del_init(&block_group->dirty_list); |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 8134 | remove_rsv = true; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 8135 | btrfs_put_block_group(block_group); |
| 8136 | } |
| 8137 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 8138 | mutex_unlock(&trans->transaction->cache_write_mutex); |
| 8139 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8140 | if (!IS_ERR(inode)) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 8141 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8142 | if (ret) { |
| 8143 | btrfs_add_delayed_iput(inode); |
| 8144 | goto out; |
| 8145 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8146 | clear_nlink(inode); |
| 8147 | /* One for the block groups ref */ |
| 8148 | spin_lock(&block_group->lock); |
| 8149 | if (block_group->iref) { |
| 8150 | block_group->iref = 0; |
| 8151 | block_group->inode = NULL; |
| 8152 | spin_unlock(&block_group->lock); |
| 8153 | iput(inode); |
| 8154 | } else { |
| 8155 | spin_unlock(&block_group->lock); |
| 8156 | } |
| 8157 | /* One for our lookup ref */ |
Josef Bacik | 455757c | 2011-09-19 12:26:24 -0400 | [diff] [blame] | 8158 | btrfs_add_delayed_iput(inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8159 | } |
| 8160 | |
| 8161 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 8162 | key.offset = block_group->key.objectid; |
| 8163 | key.type = 0; |
| 8164 | |
| 8165 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 8166 | if (ret < 0) |
| 8167 | goto out; |
| 8168 | if (ret > 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 8169 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8170 | if (ret == 0) { |
| 8171 | ret = btrfs_del_item(trans, tree_root, path); |
| 8172 | if (ret) |
| 8173 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 8174 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8175 | } |
| 8176 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8177 | spin_lock(&fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8178 | rb_erase(&block_group->cache_node, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8179 | &fs_info->block_group_cache_tree); |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 8180 | RB_CLEAR_NODE(&block_group->cache_node); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 8181 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8182 | if (fs_info->first_logical_byte == block_group->key.objectid) |
| 8183 | fs_info->first_logical_byte = (u64)-1; |
| 8184 | spin_unlock(&fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8185 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8186 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8187 | /* |
| 8188 | * we must use list_del_init so people can check to see if they |
| 8189 | * are still on the list after taking the semaphore |
| 8190 | */ |
| 8191 | list_del_init(&block_group->list); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 8192 | if (list_empty(&block_group->space_info->block_groups[index])) { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 8193 | kobj = block_group->space_info->block_group_kobjs[index]; |
| 8194 | block_group->space_info->block_group_kobjs[index] = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8195 | clear_avail_alloc_bits(fs_info, block_group->flags); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 8196 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8197 | up_write(&block_group->space_info->groups_sem); |
David Sterba | 6d58a55 | 2019-06-11 14:31:01 +0200 | [diff] [blame] | 8198 | clear_incompat_bg_bits(fs_info, block_group->flags); |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 8199 | if (kobj) { |
| 8200 | kobject_del(kobj); |
| 8201 | kobject_put(kobj); |
| 8202 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8203 | |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 8204 | if (block_group->has_caching_ctl) |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 8205 | caching_ctl = btrfs_get_caching_control(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8206 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 8207 | btrfs_wait_block_group_cache_done(block_group); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 8208 | if (block_group->has_caching_ctl) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8209 | down_write(&fs_info->commit_root_sem); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 8210 | if (!caching_ctl) { |
| 8211 | struct btrfs_caching_control *ctl; |
| 8212 | |
| 8213 | list_for_each_entry(ctl, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8214 | &fs_info->caching_block_groups, list) |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 8215 | if (ctl->block_group == block_group) { |
| 8216 | caching_ctl = ctl; |
Elena Reshetova | 1e4f471 | 2017-03-03 10:55:14 +0200 | [diff] [blame] | 8217 | refcount_inc(&caching_ctl->count); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 8218 | break; |
| 8219 | } |
| 8220 | } |
| 8221 | if (caching_ctl) |
| 8222 | list_del_init(&caching_ctl->list); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8223 | up_write(&fs_info->commit_root_sem); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 8224 | if (caching_ctl) { |
| 8225 | /* Once for the caching bgs list and once for us. */ |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame^] | 8226 | btrfs_put_caching_control(caching_ctl); |
| 8227 | btrfs_put_caching_control(caching_ctl); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 8228 | } |
| 8229 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8230 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 8231 | spin_lock(&trans->transaction->dirty_bgs_lock); |
Nikolay Borisov | 9a0ec83 | 2019-01-30 16:50:49 +0200 | [diff] [blame] | 8232 | WARN_ON(!list_empty(&block_group->dirty_list)); |
| 8233 | WARN_ON(!list_empty(&block_group->io_list)); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 8234 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
Nikolay Borisov | 9a0ec83 | 2019-01-30 16:50:49 +0200 | [diff] [blame] | 8235 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8236 | btrfs_remove_free_space_cache(block_group); |
| 8237 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8238 | spin_lock(&block_group->space_info->lock); |
Filipe Manana | 75c68e9 | 2015-01-16 13:24:40 +0000 | [diff] [blame] | 8239 | list_del_init(&block_group->ro_list); |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 8240 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8241 | if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 8242 | WARN_ON(block_group->space_info->total_bytes |
| 8243 | < block_group->key.offset); |
| 8244 | WARN_ON(block_group->space_info->bytes_readonly |
| 8245 | < block_group->key.offset); |
| 8246 | WARN_ON(block_group->space_info->disk_total |
| 8247 | < block_group->key.offset * factor); |
| 8248 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8249 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 8250 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8251 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 8252 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8253 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 8254 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8255 | memcpy(&key, &block_group->key, sizeof(key)); |
| 8256 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 8257 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 8258 | spin_lock(&block_group->lock); |
| 8259 | block_group->removed = 1; |
| 8260 | /* |
| 8261 | * At this point trimming can't start on this block group, because we |
| 8262 | * removed the block group from the tree fs_info->block_group_cache_tree |
| 8263 | * so no one can't find it anymore and even if someone already got this |
| 8264 | * block group before we removed it from the rbtree, they have already |
| 8265 | * incremented block_group->trimming - if they didn't, they won't find |
| 8266 | * any free space entries because we already removed them all when we |
| 8267 | * called btrfs_remove_free_space_cache(). |
| 8268 | * |
| 8269 | * And we must not remove the extent map from the fs_info->mapping_tree |
| 8270 | * to prevent the same logical address range and physical device space |
| 8271 | * ranges from being reused for a new block group. This is because our |
| 8272 | * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is |
| 8273 | * completely transactionless, so while it is trimming a range the |
| 8274 | * currently running transaction might finish and a new one start, |
| 8275 | * allowing for new block groups to be created that can reuse the same |
| 8276 | * physical device locations unless we take this special care. |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 8277 | * |
| 8278 | * There may also be an implicit trim operation if the file system |
| 8279 | * is mounted with -odiscard. The same protections must remain |
| 8280 | * in place until the extents have been discarded completely when |
| 8281 | * the transaction commit has completed. |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 8282 | */ |
| 8283 | remove_em = (atomic_read(&block_group->trimming) == 0); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 8284 | spin_unlock(&block_group->lock); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 8285 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 8286 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 8dbcd10 | 2014-12-02 18:07:49 +0000 | [diff] [blame] | 8287 | |
Nikolay Borisov | f3f7277 | 2018-05-10 15:44:46 +0300 | [diff] [blame] | 8288 | ret = remove_block_group_free_space(trans, block_group); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 8289 | if (ret) |
| 8290 | goto out; |
| 8291 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8292 | btrfs_put_block_group(block_group); |
| 8293 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8294 | |
| 8295 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 8296 | if (ret > 0) |
| 8297 | ret = -EIO; |
| 8298 | if (ret < 0) |
| 8299 | goto out; |
| 8300 | |
| 8301 | ret = btrfs_del_item(trans, root, path); |
Filipe Manana | 8eaf40c | 2019-06-12 11:05:42 +0100 | [diff] [blame] | 8302 | if (ret) |
| 8303 | goto out; |
| 8304 | |
| 8305 | if (remove_em) { |
| 8306 | struct extent_map_tree *em_tree; |
| 8307 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 8308 | em_tree = &fs_info->mapping_tree; |
Filipe Manana | 8eaf40c | 2019-06-12 11:05:42 +0100 | [diff] [blame] | 8309 | write_lock(&em_tree->lock); |
| 8310 | remove_extent_mapping(em_tree, em); |
| 8311 | write_unlock(&em_tree->lock); |
| 8312 | /* once for the tree */ |
| 8313 | free_extent_map(em); |
| 8314 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8315 | out: |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 8316 | if (remove_rsv) |
| 8317 | btrfs_delayed_refs_rsv_release(fs_info, 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8318 | btrfs_free_path(path); |
| 8319 | return ret; |
| 8320 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8321 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 8322 | struct btrfs_trans_handle * |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 8323 | btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info, |
| 8324 | const u64 chunk_offset) |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 8325 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 8326 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 8327 | struct extent_map *em; |
| 8328 | struct map_lookup *map; |
| 8329 | unsigned int num_items; |
| 8330 | |
| 8331 | read_lock(&em_tree->lock); |
| 8332 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 8333 | read_unlock(&em_tree->lock); |
| 8334 | ASSERT(em && em->start == chunk_offset); |
| 8335 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 8336 | /* |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 8337 | * We need to reserve 3 + N units from the metadata space info in order |
| 8338 | * to remove a block group (done at btrfs_remove_chunk() and at |
| 8339 | * btrfs_remove_block_group()), which are used for: |
| 8340 | * |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 8341 | * 1 unit for adding the free space inode's orphan (located in the tree |
| 8342 | * of tree roots). |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 8343 | * 1 unit for deleting the block group item (located in the extent |
| 8344 | * tree). |
| 8345 | * 1 unit for deleting the free space item (located in tree of tree |
| 8346 | * roots). |
| 8347 | * N units for deleting N device extent items corresponding to each |
| 8348 | * stripe (located in the device tree). |
| 8349 | * |
| 8350 | * In order to remove a block group we also need to reserve units in the |
| 8351 | * system space info in order to update the chunk tree (update one or |
| 8352 | * more device items and remove one chunk item), but this is done at |
| 8353 | * btrfs_remove_chunk() through a call to check_system_chunk(). |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 8354 | */ |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 8355 | map = em->map_lookup; |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 8356 | num_items = 3 + map->num_stripes; |
| 8357 | free_extent_map(em); |
| 8358 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 8359 | return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root, |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 8360 | num_items, 1); |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 8361 | } |
| 8362 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8363 | /* |
| 8364 | * Process the unused_bgs list and remove any that don't have any allocated |
| 8365 | * space inside of them. |
| 8366 | */ |
| 8367 | void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) |
| 8368 | { |
| 8369 | struct btrfs_block_group_cache *block_group; |
| 8370 | struct btrfs_space_info *space_info; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8371 | struct btrfs_trans_handle *trans; |
| 8372 | int ret = 0; |
| 8373 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 8374 | if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8375 | return; |
| 8376 | |
| 8377 | spin_lock(&fs_info->unused_bgs_lock); |
| 8378 | while (!list_empty(&fs_info->unused_bgs)) { |
| 8379 | u64 start, end; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 8380 | int trimming; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8381 | |
| 8382 | block_group = list_first_entry(&fs_info->unused_bgs, |
| 8383 | struct btrfs_block_group_cache, |
| 8384 | bg_list); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8385 | list_del_init(&block_group->bg_list); |
Zhao Lei | aefbe9a | 2015-09-29 21:03:54 +0800 | [diff] [blame] | 8386 | |
| 8387 | space_info = block_group->space_info; |
| 8388 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8389 | if (ret || btrfs_mixed_space_info(space_info)) { |
| 8390 | btrfs_put_block_group(block_group); |
| 8391 | continue; |
| 8392 | } |
| 8393 | spin_unlock(&fs_info->unused_bgs_lock); |
| 8394 | |
Zhao Lei | d5f2e33 | 2015-10-08 18:46:44 +0800 | [diff] [blame] | 8395 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 8396 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8397 | /* Don't want to race with allocators so take the groups_sem */ |
| 8398 | down_write(&space_info->groups_sem); |
| 8399 | spin_lock(&block_group->lock); |
Qu Wenruo | 4379444 | 2018-06-22 12:35:00 +0800 | [diff] [blame] | 8400 | if (block_group->reserved || block_group->pinned || |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8401 | btrfs_block_group_used(&block_group->item) || |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 8402 | block_group->ro || |
Zhao Lei | aefbe9a | 2015-09-29 21:03:54 +0800 | [diff] [blame] | 8403 | list_is_singular(&block_group->list)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8404 | /* |
| 8405 | * We want to bail if we made new allocations or have |
| 8406 | * outstanding allocations in this block group. We do |
| 8407 | * the ro check in case balance is currently acting on |
| 8408 | * this block group. |
| 8409 | */ |
Qu Wenruo | 4ed0a7a | 2018-04-26 17:17:20 +0800 | [diff] [blame] | 8410 | trace_btrfs_skip_unused_block_group(block_group); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8411 | spin_unlock(&block_group->lock); |
| 8412 | up_write(&space_info->groups_sem); |
| 8413 | goto next; |
| 8414 | } |
| 8415 | spin_unlock(&block_group->lock); |
| 8416 | |
| 8417 | /* We don't want to force the issue, only flip if it's ok. */ |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 8418 | ret = inc_block_group_ro(block_group, 0); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8419 | up_write(&space_info->groups_sem); |
| 8420 | if (ret < 0) { |
| 8421 | ret = 0; |
| 8422 | goto next; |
| 8423 | } |
| 8424 | |
| 8425 | /* |
| 8426 | * Want to do this before we do anything else so we can recover |
| 8427 | * properly if we fail to join the transaction. |
| 8428 | */ |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 8429 | trans = btrfs_start_trans_remove_block_group(fs_info, |
| 8430 | block_group->key.objectid); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8431 | if (IS_ERR(trans)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8432 | btrfs_dec_block_group_ro(block_group); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8433 | ret = PTR_ERR(trans); |
| 8434 | goto next; |
| 8435 | } |
| 8436 | |
| 8437 | /* |
| 8438 | * We could have pending pinned extents for this block group, |
| 8439 | * just delete them, we don't care about them anymore. |
| 8440 | */ |
| 8441 | start = block_group->key.objectid; |
| 8442 | end = start + block_group->key.offset - 1; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 8443 | /* |
| 8444 | * Hold the unused_bg_unpin_mutex lock to avoid racing with |
| 8445 | * btrfs_finish_extent_commit(). If we are at transaction N, |
| 8446 | * another task might be running finish_extent_commit() for the |
| 8447 | * previous transaction N - 1, and have seen a range belonging |
| 8448 | * to the block group in freed_extents[] before we were able to |
| 8449 | * clear the whole block group range from freed_extents[]. This |
| 8450 | * means that task can lookup for the block group after we |
| 8451 | * unpinned it from freed_extents[] and removed it, leading to |
| 8452 | * a BUG_ON() at btrfs_unpin_extent_range(). |
| 8453 | */ |
| 8454 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 8455 | ret = clear_extent_bits(&fs_info->freed_extents[0], start, end, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 8456 | EXTENT_DIRTY); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 8457 | if (ret) { |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 8458 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8459 | btrfs_dec_block_group_ro(block_group); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 8460 | goto end_trans; |
| 8461 | } |
| 8462 | ret = clear_extent_bits(&fs_info->freed_extents[1], start, end, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 8463 | EXTENT_DIRTY); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 8464 | if (ret) { |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 8465 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8466 | btrfs_dec_block_group_ro(block_group); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 8467 | goto end_trans; |
| 8468 | } |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 8469 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8470 | |
| 8471 | /* Reset pinned so btrfs_put_block_group doesn't complain */ |
Zhao Lei | c30666d | 2015-02-25 14:17:20 +0800 | [diff] [blame] | 8472 | spin_lock(&space_info->lock); |
| 8473 | spin_lock(&block_group->lock); |
| 8474 | |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 8475 | btrfs_space_info_update_bytes_pinned(fs_info, space_info, |
| 8476 | -block_group->pinned); |
Zhao Lei | c30666d | 2015-02-25 14:17:20 +0800 | [diff] [blame] | 8477 | space_info->bytes_readonly += block_group->pinned; |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 8478 | percpu_counter_add_batch(&space_info->total_bytes_pinned, |
| 8479 | -block_group->pinned, |
| 8480 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8481 | block_group->pinned = 0; |
| 8482 | |
Zhao Lei | c30666d | 2015-02-25 14:17:20 +0800 | [diff] [blame] | 8483 | spin_unlock(&block_group->lock); |
| 8484 | spin_unlock(&space_info->lock); |
| 8485 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 8486 | /* DISCARD can flip during remount */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8487 | trimming = btrfs_test_opt(fs_info, DISCARD); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 8488 | |
| 8489 | /* Implicit trim during transaction commit. */ |
| 8490 | if (trimming) |
| 8491 | btrfs_get_block_group_trimming(block_group); |
| 8492 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8493 | /* |
| 8494 | * Btrfs_remove_chunk will abort the transaction if things go |
| 8495 | * horribly wrong. |
| 8496 | */ |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 8497 | ret = btrfs_remove_chunk(trans, block_group->key.objectid); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 8498 | |
| 8499 | if (ret) { |
| 8500 | if (trimming) |
| 8501 | btrfs_put_block_group_trimming(block_group); |
| 8502 | goto end_trans; |
| 8503 | } |
| 8504 | |
| 8505 | /* |
| 8506 | * If we're not mounted with -odiscard, we can just forget |
| 8507 | * about this block group. Otherwise we'll need to wait |
| 8508 | * until transaction commit to do the actual discard. |
| 8509 | */ |
| 8510 | if (trimming) { |
Filipe Manana | 348a001 | 2015-11-27 12:16:16 +0000 | [diff] [blame] | 8511 | spin_lock(&fs_info->unused_bgs_lock); |
| 8512 | /* |
| 8513 | * A concurrent scrub might have added us to the list |
| 8514 | * fs_info->unused_bgs, so use a list_move operation |
| 8515 | * to add the block group to the deleted_bgs list. |
| 8516 | */ |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 8517 | list_move(&block_group->bg_list, |
| 8518 | &trans->transaction->deleted_bgs); |
Filipe Manana | 348a001 | 2015-11-27 12:16:16 +0000 | [diff] [blame] | 8519 | spin_unlock(&fs_info->unused_bgs_lock); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 8520 | btrfs_get_block_group(block_group); |
| 8521 | } |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 8522 | end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 8523 | btrfs_end_transaction(trans); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8524 | next: |
Zhao Lei | d5f2e33 | 2015-10-08 18:46:44 +0800 | [diff] [blame] | 8525 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 8526 | btrfs_put_block_group(block_group); |
| 8527 | spin_lock(&fs_info->unused_bgs_lock); |
| 8528 | } |
| 8529 | spin_unlock(&fs_info->unused_bgs_lock); |
| 8530 | } |
| 8531 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8532 | int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 8533 | u64 start, u64 end) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8534 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8535 | return unpin_extent_range(fs_info, start, end, false); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8536 | } |
| 8537 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8538 | /* |
| 8539 | * It used to be that old block groups would be left around forever. |
| 8540 | * Iterating over them would be enough to trim unused space. Since we |
| 8541 | * now automatically remove them, we also need to iterate over unallocated |
| 8542 | * space. |
| 8543 | * |
| 8544 | * We don't want a transaction for this since the discard may take a |
| 8545 | * substantial amount of time. We don't require that a transaction be |
| 8546 | * running, but we do need to take a running transaction into account |
Jeff Mahoney | fee7acc | 2018-09-06 17:18:16 -0400 | [diff] [blame] | 8547 | * to ensure that we're not discarding chunks that were released or |
| 8548 | * allocated in the current transaction. |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8549 | * |
| 8550 | * Holding the chunks lock will prevent other threads from allocating |
| 8551 | * or releasing chunks, but it won't prevent a running transaction |
| 8552 | * from committing and releasing the memory that the pending chunks |
| 8553 | * list head uses. For that, we need to take a reference to the |
Jeff Mahoney | fee7acc | 2018-09-06 17:18:16 -0400 | [diff] [blame] | 8554 | * transaction and hold the commit root sem. We only need to hold |
| 8555 | * it while performing the free space search since we have already |
| 8556 | * held back allocations. |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8557 | */ |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 8558 | static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed) |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8559 | { |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 8560 | u64 start = SZ_1M, len = 0, end = 0; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8561 | int ret; |
| 8562 | |
| 8563 | *trimmed = 0; |
| 8564 | |
Jeff Mahoney | 0be88e3 | 2018-09-06 17:18:15 -0400 | [diff] [blame] | 8565 | /* Discard not supported = nothing to do. */ |
| 8566 | if (!blk_queue_discard(bdev_get_queue(device->bdev))) |
| 8567 | return 0; |
| 8568 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 8569 | /* Not writable = nothing to do. */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 8570 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8571 | return 0; |
| 8572 | |
| 8573 | /* No free space = nothing to do. */ |
| 8574 | if (device->total_bytes <= device->bytes_used) |
| 8575 | return 0; |
| 8576 | |
| 8577 | ret = 0; |
| 8578 | |
| 8579 | while (1) { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 8580 | struct btrfs_fs_info *fs_info = device->fs_info; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8581 | u64 bytes; |
| 8582 | |
| 8583 | ret = mutex_lock_interruptible(&fs_info->chunk_mutex); |
| 8584 | if (ret) |
Jeff Mahoney | fee7acc | 2018-09-06 17:18:16 -0400 | [diff] [blame] | 8585 | break; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8586 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 8587 | find_first_clear_extent_bit(&device->alloc_state, start, |
| 8588 | &start, &end, |
| 8589 | CHUNK_TRIMMED | CHUNK_ALLOCATED); |
Nikolay Borisov | 53460a4 | 2019-06-03 13:06:01 +0300 | [diff] [blame] | 8590 | |
| 8591 | /* Ensure we skip the reserved area in the first 1M */ |
| 8592 | start = max_t(u64, start, SZ_1M); |
| 8593 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 8594 | /* |
| 8595 | * If find_first_clear_extent_bit find a range that spans the |
| 8596 | * end of the device it will set end to -1, in this case it's up |
| 8597 | * to the caller to trim the value to the size of the device. |
| 8598 | */ |
| 8599 | end = min(end, device->total_bytes - 1); |
Nikolay Borisov | 53460a4 | 2019-06-03 13:06:01 +0300 | [diff] [blame] | 8600 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 8601 | len = end - start + 1; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8602 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 8603 | /* We didn't find any extents */ |
| 8604 | if (!len) { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8605 | mutex_unlock(&fs_info->chunk_mutex); |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 8606 | ret = 0; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8607 | break; |
| 8608 | } |
| 8609 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 8610 | ret = btrfs_issue_discard(device->bdev, start, len, |
| 8611 | &bytes); |
| 8612 | if (!ret) |
| 8613 | set_extent_bits(&device->alloc_state, start, |
| 8614 | start + bytes - 1, |
| 8615 | CHUNK_TRIMMED); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8616 | mutex_unlock(&fs_info->chunk_mutex); |
| 8617 | |
| 8618 | if (ret) |
| 8619 | break; |
| 8620 | |
| 8621 | start += len; |
| 8622 | *trimmed += bytes; |
| 8623 | |
| 8624 | if (fatal_signal_pending(current)) { |
| 8625 | ret = -ERESTARTSYS; |
| 8626 | break; |
| 8627 | } |
| 8628 | |
| 8629 | cond_resched(); |
| 8630 | } |
| 8631 | |
| 8632 | return ret; |
| 8633 | } |
| 8634 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 8635 | /* |
| 8636 | * Trim the whole filesystem by: |
| 8637 | * 1) trimming the free space in each block group |
| 8638 | * 2) trimming the unallocated space on each device |
| 8639 | * |
| 8640 | * This will also continue trimming even if a block group or device encounters |
| 8641 | * an error. The return value will be the last error, or 0 if nothing bad |
| 8642 | * happens. |
| 8643 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8644 | int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8645 | { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8646 | struct btrfs_block_group_cache *cache = NULL; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8647 | struct btrfs_device *device; |
| 8648 | struct list_head *devices; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8649 | u64 group_trimmed; |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 8650 | u64 range_end = U64_MAX; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8651 | u64 start; |
| 8652 | u64 end; |
| 8653 | u64 trimmed = 0; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 8654 | u64 bg_failed = 0; |
| 8655 | u64 dev_failed = 0; |
| 8656 | int bg_ret = 0; |
| 8657 | int dev_ret = 0; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8658 | int ret = 0; |
| 8659 | |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 8660 | /* |
| 8661 | * Check range overflow if range->len is set. |
| 8662 | * The default range->len is U64_MAX. |
| 8663 | */ |
| 8664 | if (range->len != U64_MAX && |
| 8665 | check_add_overflow(range->start, range->len, &range_end)) |
| 8666 | return -EINVAL; |
| 8667 | |
Qu Wenruo | 6ba9fc8 | 2018-09-07 14:16:24 +0800 | [diff] [blame] | 8668 | cache = btrfs_lookup_first_block_group(fs_info, range->start); |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 8669 | for (; cache; cache = btrfs_next_block_group(cache)) { |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 8670 | if (cache->key.objectid >= range_end) { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8671 | btrfs_put_block_group(cache); |
| 8672 | break; |
| 8673 | } |
| 8674 | |
| 8675 | start = max(range->start, cache->key.objectid); |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 8676 | end = min(range_end, cache->key.objectid + cache->key.offset); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8677 | |
| 8678 | if (end - start >= range->minlen) { |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 8679 | if (!btrfs_block_group_cache_done(cache)) { |
| 8680 | ret = btrfs_cache_block_group(cache, 0); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 8681 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 8682 | bg_failed++; |
| 8683 | bg_ret = ret; |
| 8684 | continue; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 8685 | } |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 8686 | ret = btrfs_wait_block_group_cache_done(cache); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 8687 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 8688 | bg_failed++; |
| 8689 | bg_ret = ret; |
| 8690 | continue; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 8691 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8692 | } |
| 8693 | ret = btrfs_trim_block_group(cache, |
| 8694 | &group_trimmed, |
| 8695 | start, |
| 8696 | end, |
| 8697 | range->minlen); |
| 8698 | |
| 8699 | trimmed += group_trimmed; |
| 8700 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 8701 | bg_failed++; |
| 8702 | bg_ret = ret; |
| 8703 | continue; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8704 | } |
| 8705 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8706 | } |
| 8707 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 8708 | if (bg_failed) |
| 8709 | btrfs_warn(fs_info, |
| 8710 | "failed to trim %llu block group(s), last error %d", |
| 8711 | bg_failed, bg_ret); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8712 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | d4e329d | 2018-09-06 17:18:14 -0400 | [diff] [blame] | 8713 | devices = &fs_info->fs_devices->devices; |
| 8714 | list_for_each_entry(device, devices, dev_list) { |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 8715 | ret = btrfs_trim_free_extents(device, &group_trimmed); |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 8716 | if (ret) { |
| 8717 | dev_failed++; |
| 8718 | dev_ret = ret; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8719 | break; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 8720 | } |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8721 | |
| 8722 | trimmed += group_trimmed; |
| 8723 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8724 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 8725 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 8726 | if (dev_failed) |
| 8727 | btrfs_warn(fs_info, |
| 8728 | "failed to trim %llu device(s), last error %d", |
| 8729 | dev_failed, dev_ret); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8730 | range->len = trimmed; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 8731 | if (bg_ret) |
| 8732 | return bg_ret; |
| 8733 | return dev_ret; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8734 | } |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 8735 | |
| 8736 | /* |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 8737 | * btrfs_{start,end}_write_no_snapshotting() are similar to |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 8738 | * mnt_{want,drop}_write(), they are used to prevent some tasks from writing |
| 8739 | * data into the page cache through nocow before the subvolume is snapshoted, |
| 8740 | * but flush the data into disk after the snapshot creation, or to prevent |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 8741 | * operations while snapshotting is ongoing and that cause the snapshot to be |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 8742 | * inconsistent (writes followed by expanding truncates for example). |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 8743 | */ |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 8744 | void btrfs_end_write_no_snapshotting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 8745 | { |
| 8746 | percpu_counter_dec(&root->subv_writers->counter); |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 8747 | cond_wake_up(&root->subv_writers->wait); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 8748 | } |
| 8749 | |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 8750 | int btrfs_start_write_no_snapshotting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 8751 | { |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 8752 | if (atomic_read(&root->will_be_snapshotted)) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 8753 | return 0; |
| 8754 | |
| 8755 | percpu_counter_inc(&root->subv_writers->counter); |
| 8756 | /* |
| 8757 | * Make sure counter is updated before we check for snapshot creation. |
| 8758 | */ |
| 8759 | smp_mb(); |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 8760 | if (atomic_read(&root->will_be_snapshotted)) { |
| 8761 | btrfs_end_write_no_snapshotting(root); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 8762 | return 0; |
| 8763 | } |
| 8764 | return 1; |
| 8765 | } |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 8766 | |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 8767 | void btrfs_wait_for_snapshot_creation(struct btrfs_root *root) |
| 8768 | { |
| 8769 | while (true) { |
| 8770 | int ret; |
| 8771 | |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 8772 | ret = btrfs_start_write_no_snapshotting(root); |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 8773 | if (ret) |
| 8774 | break; |
Peter Zijlstra | 4625956 | 2018-03-15 11:43:08 +0100 | [diff] [blame] | 8775 | wait_var_event(&root->will_be_snapshotted, |
| 8776 | !atomic_read(&root->will_be_snapshotted)); |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 8777 | } |
| 8778 | } |
Qu Wenruo | 031f24d | 2018-05-22 16:43:47 +0800 | [diff] [blame] | 8779 | |
| 8780 | void btrfs_mark_bg_unused(struct btrfs_block_group_cache *bg) |
| 8781 | { |
| 8782 | struct btrfs_fs_info *fs_info = bg->fs_info; |
| 8783 | |
| 8784 | spin_lock(&fs_info->unused_bgs_lock); |
| 8785 | if (list_empty(&bg->bg_list)) { |
| 8786 | btrfs_get_block_group(bg); |
| 8787 | trace_btrfs_add_unused_block_group(bg); |
| 8788 | list_add_tail(&bg->bg_list, &fs_info->unused_bgs); |
| 8789 | } |
| 8790 | spin_unlock(&fs_info->unused_bgs_lock); |
| 8791 | } |