blob: dab8e90237cda35fd30356e99da6a1c5e5f24f2c [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
David Sterbac1d7c512018-04-03 19:23:33 +02005
Zach Brownec6b9102007-07-11 10:00:37 -04006#include <linux/sched.h>
Filipe Mananad7cd4dd2019-08-07 11:21:46 +01007#include <linux/sched/mm.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +01008#include <linux/sched/signal.h>
Chris Masonedbd8d42007-12-21 16:27:24 -05009#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040010#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010011#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050012#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040013#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040014#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
David Sterbadff51cd2011-06-14 12:52:17 +020016#include <linux/ratelimit.h>
Josef Bacikb150a4f2013-06-19 15:00:04 -040017#include <linux/percpu_counter.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040018#include <linux/lockdep.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020019#include <linux/crc32c.h>
Miao Xie995946d2014-04-02 19:51:06 +080020#include "tree-log.h"
Chris Masonfec577f2007-02-26 10:40:21 -050021#include "disk-io.h"
22#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040023#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050024#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040025#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040026#include "free-space-cache.h"
Omar Sandoval1e144fb2015-09-29 20:50:37 -070027#include "free-space-tree.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060028#include "math.h"
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040029#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070030#include "qgroup.h"
Josef Bacikfd708b82017-09-29 15:43:50 -040031#include "ref-verify.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040032#include "space-info.h"
Josef Bacikd12ffdd2019-06-19 13:47:17 -040033#include "block-rsv.h"
Josef Bacik86736342019-06-19 15:12:00 -040034#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040035#include "block-group.h"
Chris Masonfec577f2007-02-26 10:40:21 -050036
Arne Jansen709c0482011-09-12 12:22:57 +020037#undef SCRAMBLE_DELAYED_REFS
38
Qu Wenruo9f9b8e82018-10-24 20:24:01 +080039
Yan Zheng5d4f98a2009-06-10 10:45:14 -040040static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +030041 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 Zheng5d4f98a2009-06-10 10:45:14 -040045static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
46 struct extent_buffer *leaf,
47 struct btrfs_extent_item *ei);
48static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040049 u64 parent, u64 root_objectid,
50 u64 flags, u64 owner, u64 offset,
51 struct btrfs_key *ins, int ref_mod);
52static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +030053 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +030054 struct btrfs_delayed_extent_op *extent_op);
Yan Zheng11833d62009-09-11 16:11:19 -040055static int find_next_key(struct btrfs_path *path, int level,
56 struct btrfs_key *key);
Josef Bacik6a632092009-02-20 11:00:09 -050057
Josef Bacik0f9dd462008-09-23 13:14:11 -040058static 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 Hellwigb2950862008-12-02 09:54:17 -050067static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040068 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 Boa1897fd2012-12-27 09:01:23 +000094
95 if (info->first_logical_byte > block_group->key.objectid)
96 info->first_logical_byte = block_group->key.objectid;
97
Josef Bacik0f9dd462008-09-23 13:14:11 -040098 spin_unlock(&info->block_group_cache_lock);
99
100 return 0;
101}
102
Josef Bacik6f410d12019-06-20 15:37:49 -0400103int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info,
104 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400105{
Yan Zheng11833d62009-09-11 16:11:19 -0400106 u64 end = start + num_bytes - 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400107 set_extent_bits(&fs_info->freed_extents[0],
David Sterbaceeb0ae2016-04-26 23:54:39 +0200108 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400109 set_extent_bits(&fs_info->freed_extents[1],
David Sterbaceeb0ae2016-04-26 23:54:39 +0200110 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -0400111 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400112}
113
Josef Bacik6f410d12019-06-20 15:37:49 -0400114void btrfs_free_excluded_extents(struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400115{
Nikolay Borisov9e715da2018-06-20 15:49:08 +0300116 struct btrfs_fs_info *fs_info = cache->fs_info;
Yan Zheng11833d62009-09-11 16:11:19 -0400117 u64 start, end;
118
119 start = cache->key.objectid;
120 end = start + cache->key.offset - 1;
121
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400122 clear_extent_bits(&fs_info->freed_extents[0],
David Sterba91166212016-04-26 23:54:39 +0200123 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400124 clear_extent_bits(&fs_info->freed_extents[1],
David Sterba91166212016-04-26 23:54:39 +0200125 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -0400126}
127
Nikolay Borisov3c4da652018-06-20 15:49:09 +0300128static int exclude_super_stripes(struct btrfs_block_group_cache *cache)
Yan Zheng11833d62009-09-11 16:11:19 -0400129{
Nikolay Borisov3c4da652018-06-20 15:49:09 +0300130 struct btrfs_fs_info *fs_info = cache->fs_info;
Josef Bacik817d52f2009-07-13 21:29:25 -0400131 u64 bytenr;
132 u64 *logical;
133 int stripe_len;
134 int i, nr, ret;
135
Yan, Zheng06b23312009-11-26 09:31:11 +0000136 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 Bacik6f410d12019-06-20 15:37:49 -0400139 ret = btrfs_add_excluded_extent(fs_info, cache->key.objectid,
140 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400141 if (ret)
142 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000143 }
144
Josef Bacik817d52f2009-07-13 21:29:25 -0400145 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
146 bytenr = btrfs_sb_offset(i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400147 ret = btrfs_rmap_block(fs_info, cache->key.objectid,
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +0300148 bytenr, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400149 if (ret)
150 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400151
Josef Bacik817d52f2009-07-13 21:29:25 -0400152 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400153 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 Bacik6f410d12019-06-20 15:37:49 -0400173 ret = btrfs_add_excluded_extent(fs_info, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400174 if (ret) {
175 kfree(logical);
176 return ret;
177 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400178 }
Yan Zheng11833d62009-09-11 16:11:19 -0400179
Josef Bacik817d52f2009-07-13 21:29:25 -0400180 kfree(logical);
181 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400182 return 0;
183}
184
Josef Bacike3cb3392019-06-20 15:37:50 -0400185struct btrfs_caching_control *btrfs_get_caching_control(
186 struct btrfs_block_group_cache *cache)
Yan Zheng11833d62009-09-11 16:11:19 -0400187{
188 struct btrfs_caching_control *ctl;
189
190 spin_lock(&cache->lock);
Josef Bacikdde5abe2010-09-16 16:17:03 -0400191 if (!cache->caching_ctl) {
192 spin_unlock(&cache->lock);
193 return NULL;
194 }
195
Yan Zheng11833d62009-09-11 16:11:19 -0400196 ctl = cache->caching_ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200197 refcount_inc(&ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400198 spin_unlock(&cache->lock);
199 return ctl;
200}
201
Josef Bacike3cb3392019-06-20 15:37:50 -0400202void btrfs_put_caching_control(struct btrfs_caching_control *ctl)
Yan Zheng11833d62009-09-11 16:11:19 -0400203{
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200204 if (refcount_dec_and_test(&ctl->count))
Yan Zheng11833d62009-09-11 16:11:19 -0400205 kfree(ctl);
206}
207
Josef Bacikd0bd4562015-09-23 14:54:14 -0400208#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400209static void fragment_free_space(struct btrfs_block_group_cache *block_group)
Josef Bacikd0bd4562015-09-23 14:54:14 -0400210{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400211 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400212 u64 start = block_group->key.objectid;
213 u64 len = block_group->key.offset;
214 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400215 fs_info->nodesize : fs_info->sectorsize;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400216 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 Bacik0f9dd462008-09-23 13:14:11 -0400229/*
Josef Bacik676f1f72019-06-20 15:37:48 -0400230 * 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 Bacik0f9dd462008-09-23 13:14:11 -0400234 */
Omar Sandovala5ed9182015-09-29 20:50:35 -0700235u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Nikolay Borisov4457c1c2018-05-10 15:44:45 +0300236 u64 start, u64 end)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400237{
Nikolay Borisov4457c1c2018-05-10 15:44:45 +0300238 struct btrfs_fs_info *info = block_group->fs_info;
Josef Bacik817d52f2009-07-13 21:29:25 -0400239 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400240 int ret;
241
242 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400243 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400244 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400245 EXTENT_DIRTY | EXTENT_UPTODATE,
246 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400247 if (ret)
248 break;
249
Yan, Zheng06b23312009-11-26 09:31:11 +0000250 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400251 start = extent_end + 1;
252 } else if (extent_start > start && extent_start < end) {
253 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400254 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500255 ret = btrfs_add_free_space(block_group, start,
256 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100257 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400258 start = extent_end + 1;
259 } else {
260 break;
261 }
262 }
263
264 if (start < end) {
265 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400266 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500267 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100268 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400269 }
270
Josef Bacik817d52f2009-07-13 21:29:25 -0400271 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400272}
273
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700274static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
Chris Masone37c9e62007-05-09 20:13:14 -0400275{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400276 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 Masone37c9e62007-05-09 20:13:14 -0400279 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400280 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400281 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400282 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400283 u64 last = 0;
284 u32 nritems;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700285 int ret;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400286 bool wakeup = true;
Chris Masonf510cfe2007-10-15 16:14:48 -0400287
Chris Masone37c9e62007-05-09 20:13:14 -0400288 path = btrfs_alloc_path();
289 if (!path)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700290 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400291
Josef Bacik817d52f2009-07-13 21:29:25 -0400292 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400293
Josef Bacikd0bd4562015-09-23 14:54:14 -0400294#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 Mahoney2ff7e612016-06-22 18:54:24 -0400300 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -0400301 wakeup = false;
302#endif
Chris Mason5cd57b22008-06-25 16:01:30 -0400303 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400304 * 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 Mason5cd57b22008-06-25 16:01:30 -0400308 */
309 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400310 path->search_commit_root = 1;
David Sterbae4058b52015-11-27 16:31:35 +0100311 path->reada = READA_FORWARD;
Josef Bacik817d52f2009-07-13 21:29:25 -0400312
Yan Zhenge4404d62008-12-12 10:03:26 -0500313 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400314 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400315 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400316
Liu Bo52ee28d2013-07-11 17:51:15 +0800317next:
Yan Zheng11833d62009-09-11 16:11:19 -0400318 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400319 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700320 goto out;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500321
Yan Zheng11833d62009-09-11 16:11:19 -0400322 leaf = path->nodes[0];
323 nritems = btrfs_header_nritems(leaf);
324
Chris Masond3977122009-01-05 21:25:51 -0500325 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200326 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400327 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400328 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400329 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400330
Yan Zheng11833d62009-09-11 16:11:19 -0400331 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 Masone37c9e62007-05-09 20:13:14 -0400336 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400337
Josef Bacikc9ea7b22013-09-19 10:02:11 -0400338 if (need_resched() ||
Josef Bacik9e351cc2014-03-13 15:42:13 -0400339 rwsem_is_contended(&fs_info->commit_root_sem)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400340 if (wakeup)
341 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400342 btrfs_release_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400343 up_read(&fs_info->commit_root_sem);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400344 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400345 cond_resched();
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700346 mutex_lock(&caching_ctl->mutex);
347 down_read(&fs_info->commit_root_sem);
348 goto next;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400349 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400350
351 ret = btrfs_next_leaf(extent_root, path);
352 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700353 goto out;
Josef Bacik0a3896d2013-04-19 14:37:26 -0400354 if (ret)
355 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400356 leaf = path->nodes[0];
357 nritems = btrfs_header_nritems(leaf);
358 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400359 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400360
Liu Bo52ee28d2013-07-11 17:51:15 +0800361 if (key.objectid < last) {
362 key.objectid = last;
363 key.offset = 0;
364 key.type = BTRFS_EXTENT_ITEM_KEY;
365
Josef Bacikd0bd4562015-09-23 14:54:14 -0400366 if (wakeup)
367 caching_ctl->progress = last;
Liu Bo52ee28d2013-07-11 17:51:15 +0800368 btrfs_release_path(path);
369 goto next;
370 }
371
Yan Zheng11833d62009-09-11 16:11:19 -0400372 if (key.objectid < block_group->key.objectid) {
373 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400374 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400375 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400376
Chris Masone37c9e62007-05-09 20:13:14 -0400377 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400378 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400379 break;
Yan7d7d6062007-09-14 16:15:28 -0400380
Josef Bacik3173a182013-03-07 14:22:04 -0500381 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
382 key.type == BTRFS_METADATA_ITEM_KEY) {
Nikolay Borisov4457c1c2018-05-10 15:44:45 +0300383 total_found += add_new_free_space(block_group, last,
Josef Bacik817d52f2009-07-13 21:29:25 -0400384 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500385 if (key.type == BTRFS_METADATA_ITEM_KEY)
386 last = key.objectid +
Jeff Mahoneyda170662016-06-15 09:22:56 -0400387 fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500388 else
389 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400390
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700391 if (total_found > CACHING_CTL_WAKE_UP) {
Yan Zheng11833d62009-09-11 16:11:19 -0400392 total_found = 0;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400393 if (wakeup)
394 wake_up(&caching_ctl->wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400395 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400396 }
Chris Masone37c9e62007-05-09 20:13:14 -0400397 path->slots[0]++;
398 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400399 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400400
Nikolay Borisov4457c1c2018-05-10 15:44:45 +0300401 total_found += add_new_free_space(block_group, last,
Josef Bacik817d52f2009-07-13 21:29:25 -0400402 block_group->key.objectid +
403 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400404 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400405
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700406out:
407 btrfs_free_path(path);
408 return ret;
409}
410
411static 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 Sandoval1e144fb2015-09-29 20:50:37 -0700425 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 Sandoval73fa48b2015-09-29 20:50:33 -0700429
Josef Bacik817d52f2009-07-13 21:29:25 -0400430 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400431 block_group->caching_ctl = NULL;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700432 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
Josef Bacik817d52f2009-07-13 21:29:25 -0400433 spin_unlock(&block_group->lock);
434
Josef Bacikd0bd4562015-09-23 14:54:14 -0400435#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400436 if (btrfs_should_fragment_free_space(block_group)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400437 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 Mahoney2ff7e612016-06-22 18:54:24 -0400446 fragment_free_space(block_group);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400447 }
448#endif
449
450 caching_ctl->progress = (u64)-1;
Chris Masonf7d3d2f2015-12-18 11:11:10 -0800451
Josef Bacik9e351cc2014-03-13 15:42:13 -0400452 up_read(&fs_info->commit_root_sem);
Josef Bacik6f410d12019-06-20 15:37:49 -0400453 btrfs_free_excluded_extents(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -0400454 mutex_unlock(&caching_ctl->mutex);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700455
Yan Zheng11833d62009-09-11 16:11:19 -0400456 wake_up(&caching_ctl->wait);
457
Josef Bacike3cb3392019-06-20 15:37:50 -0400458 btrfs_put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000459 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400460}
461
Josef Bacik676f1f72019-06-20 15:37:48 -0400462int btrfs_cache_block_group(struct btrfs_block_group_cache *cache,
463 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400464{
Josef Bacik291c7d22011-11-14 13:52:14 -0500465 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400466 struct btrfs_fs_info *fs_info = cache->fs_info;
467 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400468 int ret = 0;
469
Josef Bacik291c7d22011-11-14 13:52:14 -0500470 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100471 if (!caching_ctl)
472 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500473
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 Reshetova1e4f4712017-03-03 10:55:14 +0200479 refcount_set(&caching_ctl->count, 1);
Liu Bo9e0af232014-08-15 23:36:53 +0800480 btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
481 caching_thread, NULL, NULL);
Josef Bacik291c7d22011-11-14 13:52:14 -0500482
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 Reshetova1e4f4712017-03-03 10:55:14 +0200500 refcount_inc(&ctl->count);
Josef Bacik291c7d22011-11-14 13:52:14 -0500501 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
502 spin_unlock(&cache->lock);
503
504 schedule();
505
506 finish_wait(&ctl->wait, &wait);
Josef Bacike3cb3392019-06-20 15:37:50 -0400507 btrfs_put_caching_control(ctl);
Josef Bacik291c7d22011-11-14 13:52:14 -0500508 spin_lock(&cache->lock);
509 }
510
511 if (cache->cached != BTRFS_CACHE_NO) {
512 spin_unlock(&cache->lock);
513 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400514 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500515 }
516 WARN_ON(cache->caching_ctl);
517 cache->caching_ctl = caching_ctl;
518 cache->cached = BTRFS_CACHE_FAST;
519 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400520
Satoru Takeuchid8953d62017-09-12 20:08:08 +0900521 if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500522 mutex_lock(&caching_ctl->mutex);
David Sterbabb6cb1c2019-03-20 13:47:15 +0100523 ret = load_free_space_cache(cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400524
525 spin_lock(&cache->lock);
526 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500527 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400528 cache->cached = BTRFS_CACHE_FINISHED;
529 cache->last_byte_to_unpin = (u64)-1;
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500530 caching_ctl->progress = (u64)-1;
Josef Bacik9d66e232010-08-25 16:54:15 -0400531 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500532 if (load_cache_only) {
533 cache->caching_ctl = NULL;
534 cache->cached = BTRFS_CACHE_NO;
535 } else {
536 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000537 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500538 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400539 }
540 spin_unlock(&cache->lock);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400541#ifdef CONFIG_BTRFS_DEBUG
542 if (ret == 1 &&
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400543 btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400544 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 Mahoney2ff7e612016-06-22 18:54:24 -0400553 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400554 }
555#endif
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500556 mutex_unlock(&caching_ctl->mutex);
557
Josef Bacik291c7d22011-11-14 13:52:14 -0500558 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000559 if (ret == 1) {
Josef Bacike3cb3392019-06-20 15:37:50 -0400560 btrfs_put_caching_control(caching_ctl);
Josef Bacik6f410d12019-06-20 15:37:49 -0400561 btrfs_free_excluded_extents(cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400562 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000563 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500564 } else {
565 /*
Omar Sandoval1e144fb2015-09-29 20:50:37 -0700566 * 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 Bacik291c7d22011-11-14 13:52:14 -0500568 */
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 Manana4f69cb92014-11-26 15:28:51 +0000575 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500576 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400577 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500578 wake_up(&caching_ctl->wait);
579 }
580
581 if (load_cache_only) {
Josef Bacike3cb3392019-06-20 15:37:50 -0400582 btrfs_put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400583 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400584 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400585
Josef Bacik9e351cc2014-03-13 15:42:13 -0400586 down_write(&fs_info->commit_root_sem);
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200587 refcount_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400588 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400589 up_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -0400590
Josef Bacik11dfe352009-11-13 20:12:59 +0000591 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400592
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800593 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400594
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400595 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400596}
597
Chris Mason0b86a832008-03-24 15:01:56 -0400598
Qu Wenruo78192442019-05-15 07:33:48 +0800599static u64 generic_ref_to_space_flags(struct btrfs_ref *ref)
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700600{
Qu Wenruoddf30cf2019-04-04 14:45:34 +0800601 if (ref->type == BTRFS_REF_METADATA) {
602 if (ref->tree_ref.root == BTRFS_CHUNK_TREE_OBJECTID)
Qu Wenruo78192442019-05-15 07:33:48 +0800603 return BTRFS_BLOCK_GROUP_SYSTEM;
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700604 else
Qu Wenruo78192442019-05-15 07:33:48 +0800605 return BTRFS_BLOCK_GROUP_METADATA;
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700606 }
Qu Wenruo78192442019-05-15 07:33:48 +0800607 return BTRFS_BLOCK_GROUP_DATA;
608}
609
610static 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 Sandoval0d9f8242017-06-06 16:45:26 -0700615
Josef Bacik280c29082019-06-18 16:09:19 -0400616 space_info = btrfs_find_space_info(fs_info, flags);
Omar Sandoval55e81962017-06-06 16:45:27 -0700617 ASSERT(space_info);
Qu Wenruo78192442019-05-15 07:33:48 +0800618 percpu_counter_add_batch(&space_info->total_bytes_pinned, ref->len,
619 BTRFS_TOTAL_BYTES_PINNED_BATCH);
620}
621
622static 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 Bacik280c29082019-06-18 16:09:19 -0400628 space_info = btrfs_find_space_info(fs_info, flags);
Qu Wenruo78192442019-05-15 07:33:48 +0800629 ASSERT(space_info);
630 percpu_counter_add_batch(&space_info->total_bytes_pinned, -ref->len,
Ethan Liendec59fa2018-07-13 16:50:42 +0800631 BTRFS_TOTAL_BYTES_PINNED_BATCH);
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700632}
633
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000634/* simple helper to search for an existing data extent at a given offset */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400635int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400636{
637 int ret;
638 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400639 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400640
Zheng Yan31840ae2008-09-23 13:14:14 -0400641 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700642 if (!path)
643 return -ENOMEM;
644
Chris Masone02119d2008-09-05 16:13:11 -0400645 key.objectid = start;
646 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500647 key.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400648 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400649 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500650 return ret;
651}
652
Chris Masond8d5f3e2007-12-11 12:42:00 -0500653/*
Josef Bacik3173a182013-03-07 14:22:04 -0500654 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400655 *
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 */
662int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400663 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500664 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400665{
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 Bacik3173a182013-03-07 14:22:04 -0500677 /*
678 * If we don't have skinny metadata, don't bother doing anything
679 * different
680 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400681 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
682 offset = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500683 metadata = 0;
684 }
685
Yan, Zhenga22285a2010-05-16 10:48:46 -0400686 path = btrfs_alloc_path();
687 if (!path)
688 return -ENOMEM;
689
Yan, Zhenga22285a2010-05-16 10:48:46 -0400690 if (!trans) {
691 path->skip_locking = 1;
692 path->search_commit_root = 1;
693 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000694
695search_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 Mahoney0b246af2016-06-22 18:54:23 -0400703 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400704 if (ret < 0)
705 goto out_free;
706
Josef Bacik3173a182013-03-07 14:22:04 -0500707 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100708 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 Mahoney0b246af2016-06-22 18:54:23 -0400714 key.offset == fs_info->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100715 ret = 0;
716 }
Josef Bacik3173a182013-03-07 14:22:04 -0500717 }
718
Yan, Zhenga22285a2010-05-16 10:48:46 -0400719 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 Borisovba3c2b12018-06-26 16:57:36 +0300728 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, Zhenga22285a2010-05-16 10:48:46 -0400736 }
Nikolay Borisovba3c2b12018-06-26 16:57:36 +0300737
Yan, Zhenga22285a2010-05-16 10:48:46 -0400738 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 Bof72ad18e2017-01-30 12:24:37 -0800750 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400751 if (head) {
752 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -0400753 refcount_inc(&head->refs);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400754 spin_unlock(&delayed_refs->lock);
755
David Sterbab3b4aa72011-04-21 01:20:15 +0200756 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400757
David Sterba8cc33e52011-05-02 15:29:25 +0200758 /*
759 * Mutex was contended, block until it's released and try
760 * again
761 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400762 mutex_lock(&head->mutex);
763 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -0400764 btrfs_put_delayed_ref_head(head);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000765 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400766 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500767 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400768 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 Bacikd2788502017-09-29 15:43:57 -0400773 num_refs += head->ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500774 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400775 mutex_unlock(&head->mutex);
776 }
777 spin_unlock(&delayed_refs->lock);
778out:
779 WARN_ON(num_refs == 0);
780 if (refs)
781 *refs = num_refs;
782 if (flags)
783 *flags = extent_flags;
784out_free:
785 btrfs_free_path(path);
786 return ret;
787}
788
789/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500790 * 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 Zheng5d4f98a2009-06-10 10:45:14 -0400803 * 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 Steeves01327612016-05-19 21:18:45 -0400821 * When a tree block is COWed through a tree, there are four cases:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400822 *
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 Masond8d5f3e2007-12-11 12:42:00 -0500848 * File extents can be referenced by:
849 *
850 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400851 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500852 * - different offsets inside a file (bookend extents in file.c)
853 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400854 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500855 *
856 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500857 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400858 * - original offset in the file
859 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400860 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400861 * The key offset for the implicit back refs is hash of the first
862 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500863 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400864 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500865 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400866 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500867 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400868 * The key offset for the implicit back refs is the first byte of
869 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500870 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400871 * When a file extent is allocated, The implicit back refs is used.
872 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500873 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400874 * (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 Masond8d5f3e2007-12-11 12:42:00 -0500880 *
881 * Btree extents can be referenced by:
882 *
883 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500884 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400885 * 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 Masond8d5f3e2007-12-11 12:42:00 -0500889 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400890 * 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 Masond8d5f3e2007-12-11 12:42:00 -0500893 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400894
Liu Bo167ce952017-08-18 15:15:18 -0600895/*
896 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
Andrea Gelmini52042d82018-11-28 12:05:13 +0100897 * is_data == BTRFS_REF_TYPE_DATA, data type is requiried,
Liu Bo167ce952017-08-18 15:15:18 -0600898 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
899 */
900int 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 Bo64ecdb62017-08-18 15:15:24 -0600905 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
Liu Bo167ce952017-08-18 15:15:18 -0600906
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 Bo64ecdb62017-08-18 15:15:24 -0600912 if (type == BTRFS_TREE_BLOCK_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -0600913 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -0600914 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 Bo167ce952017-08-18 15:15:18 -0600925 } else if (is_data == BTRFS_REF_TYPE_DATA) {
Liu Bo64ecdb62017-08-18 15:15:24 -0600926 if (type == BTRFS_EXTENT_DATA_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -0600927 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -0600928 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 Bo167ce952017-08-18 15:15:18 -0600939 } 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 Zheng5d4f98a2009-06-10 10:45:14 -0400953static 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 Thumshirn65019df2019-05-22 10:18:59 +0200960 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400961 lenum = cpu_to_le64(owner);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200962 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400963 lenum = cpu_to_le64(offset);
Johannes Thumshirn65019df2019-05-22 10:18:59 +0200964 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400965
966 return ((u64)high_crc << 31) ^ (u64)low_crc;
967}
968
969static 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
977static 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
988static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400989 struct btrfs_path *path,
990 u64 bytenr, u64 parent,
991 u64 root_objectid,
992 u64 owner, u64 offset)
993{
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +0300994 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995 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 }
1012again:
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 Zheng5d4f98a2009-06-10 10:45:14 -04001023 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001024 }
1025
1026 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001027 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 Sterbab3b4aa72011-04-21 01:20:15 +02001052 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001053 goto again;
1054 }
1055 err = 0;
1056 break;
1057 }
1058 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001059 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001060fail:
1061 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001062}
1063
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001064static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001065 struct btrfs_path *path,
1066 u64 bytenr, u64 parent,
1067 u64 root_objectid, u64 owner,
1068 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001069{
Nikolay Borisov62b895a2018-06-20 15:48:44 +03001070 struct btrfs_root *root = trans->fs_info->extent_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04001071 struct btrfs_key key;
1072 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001073 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001074 u32 num_refs;
1075 int ret;
1076
1077 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001078 if (parent) {
1079 key.type = BTRFS_SHARED_DATA_REF_KEY;
1080 key.offset = parent;
1081 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001082 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001083 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 Yan31840ae2008-09-23 13:14:14 -04001087 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001088
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 Sterbab3b4aa72011-04-21 01:20:15 +02001113 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001114 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;
1138fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001139 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001140 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001141}
1142
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001143static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001144 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -07001145 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001146{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001147 struct btrfs_key key;
1148 struct btrfs_extent_data_ref *ref1 = NULL;
1149 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001150 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001151 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001152 int ret = 0;
1153
1154 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001155 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 Sterba6d8ff4e2018-06-26 16:20:59 +02001165 } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001166 btrfs_print_v0_err(trans->fs_info);
1167 btrfs_abort_transaction(trans, -EINVAL);
1168 return -EINVAL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001169 } else {
1170 BUG();
1171 }
1172
Chris Mason56bec292009-03-13 10:10:06 -04001173 BUG_ON(num_refs < refs_to_drop);
1174 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001175
Zheng Yan31840ae2008-09-23 13:14:14 -04001176 if (num_refs == 0) {
Nikolay Borisove9f62902018-06-20 15:48:46 +03001177 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -07001178 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001179 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001180 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 Yan31840ae2008-09-23 13:14:14 -04001184 btrfs_mark_buffer_dirty(leaf);
1185 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001186 return ret;
1187}
1188
Zhaolei9ed0dea2015-08-06 22:16:24 +08001189static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001190 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 Bo3de28d52017-08-18 15:15:19 -06001197 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001198
1199 leaf = path->nodes[0];
1200 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001201
1202 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001203 if (iref) {
Liu Bo3de28d52017-08-18 15:15:19 -06001204 /*
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 Zheng5d4f98a2009-06-10 10:45:14 -04001211 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 Zheng5d4f98a2009-06-10 10:45:14 -04001225 } else {
1226 WARN_ON(1);
1227 }
1228 return num_refs;
1229}
1230
1231static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001232 struct btrfs_path *path,
1233 u64 bytenr, u64 parent,
1234 u64 root_objectid)
1235{
Nikolay Borisovb8582ee2018-06-20 15:48:50 +03001236 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001237 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 Zheng5d4f98a2009-06-10 10:45:14 -04001252 return ret;
1253}
1254
1255static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001256 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 Borisov10728402018-06-20 15:48:43 +03001272 ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001273 path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001274 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001275 return ret;
1276}
1277
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001278static 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 Zheng2c47e6052009-06-27 21:07:35 -04001295static int find_next_key(struct btrfs_path *path, int level,
1296 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001297
1298{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001299 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001300 if (!path->nodes[level])
1301 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001302 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 */
1329static noinline_for_stack
1330int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001331 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 Borisov867cc1f2018-06-20 15:48:48 +03001337 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001338 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001339 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 Mahoney0b246af2016-06-22 18:54:23 -04001352 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Liu Bo3de28d52017-08-18 15:15:19 -06001353 int needed;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001354
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 Zheng85d41982009-06-11 08:51:10 -04001362 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001363 } else
1364 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001365
1366 /*
Nikolay Borisov16d1c062018-06-18 14:59:26 +03001367 * Owner is our level, so we can just add one to get the level for the
1368 * block we are interested in.
Josef Bacik3173a182013-03-07 14:22:04 -05001369 */
1370 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1371 key.type = BTRFS_METADATA_ITEM_KEY;
1372 key.offset = owner;
1373 }
1374
1375again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001376 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1377 if (ret < 0) {
1378 err = ret;
1379 goto out;
1380 }
Josef Bacik3173a182013-03-07 14:22:04 -05001381
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 Manana9ce49a02014-04-24 15:15:28 +01001398 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05001399 key.type = BTRFS_EXTENT_ITEM_KEY;
1400 key.offset = num_bytes;
1401 btrfs_release_path(path);
1402 goto again;
1403 }
1404 }
1405
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001406 if (ret && !insert) {
1407 err = -ENOENT;
1408 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301409 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -05001410 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -05001411 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001412 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001413
1414 leaf = path->nodes[0];
1415 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
David Sterba6d8ff4e2018-06-26 16:20:59 +02001416 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03001417 err = -EINVAL;
1418 btrfs_print_v0_err(fs_info);
1419 btrfs_abort_transaction(trans, err);
1420 goto out;
1421 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001422
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001423 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 Bacik3173a182013-03-07 14:22:04 -05001429 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001430 ptr += sizeof(struct btrfs_tree_block_info);
1431 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001432 }
1433
Liu Bo3de28d52017-08-18 15:15:19 -06001434 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
1435 needed = BTRFS_REF_TYPE_DATA;
1436 else
1437 needed = BTRFS_REF_TYPE_BLOCK;
1438
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001439 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 Bo3de28d52017-08-18 15:15:19 -06001446 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
1447 if (type == BTRFS_REF_TYPE_INVALID) {
Su Yueaf431dc2018-06-22 16:18:01 +08001448 err = -EUCLEAN;
Liu Bo3de28d52017-08-18 15:15:19 -06001449 goto out;
1450 }
1451
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001452 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 Zheng2c47e6052009-06-27 21:07:35 -04001503 if (find_next_key(path, 0, &key) == 0 &&
1504 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001505 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001506 err = -EAGAIN;
1507 goto out;
1508 }
1509 }
1510 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1511out:
Yan Zheng85d41982009-06-11 08:51:10 -04001512 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001513 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 */
1522static noinline_for_stack
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001523void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001524 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 Zheng5d4f98a2009-06-10 10:45:14 -04001529{
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 Zheng5d4f98a2009-06-10 10:45:14 -04001538
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 Sterbac71dd882019-03-20 14:51:10 +01001546 btrfs_extend_item(path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001547
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 Zheng5d4f98a2009-06-10 10:45:14 -04001581}
1582
1583static int lookup_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001584 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 Borisov867cc1f2018-06-20 15:48:48 +03001591 ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
1592 num_bytes, parent, root_objectid,
1593 owner, offset, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001594 if (ret != -ENOENT)
1595 return ret;
1596
David Sterbab3b4aa72011-04-21 01:20:15 +02001597 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001598 *ref_ret = NULL;
1599
1600 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Nikolay Borisovb8582ee2018-06-20 15:48:50 +03001601 ret = lookup_tree_block_ref(trans, path, bytenr, parent,
1602 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001603 } else {
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +03001604 ret = lookup_extent_data_ref(trans, path, bytenr, parent,
1605 root_objectid, owner, offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001606 }
1607 return ret;
1608}
1609
1610/*
1611 * helper to update/remove inline back ref
1612 */
1613static noinline_for_stack
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001614void update_inline_extent_backref(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001615 struct btrfs_extent_inline_ref *iref,
1616 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001617 struct btrfs_delayed_extent_op *extent_op,
1618 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001619{
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001620 struct extent_buffer *leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001621 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 Zheng5d4f98a2009-06-10 10:45:14 -04001629 u64 refs;
1630
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001631 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 Bo3de28d52017-08-18 15:15:19 -06001639 /*
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 Zheng5d4f98a2009-06-10 10:45:14 -04001645
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 Bacikfcebe452014-05-13 17:30:47 -07001666 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001667 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 Sterba78ac4f92019-03-20 14:49:12 +01001675 btrfs_truncate_item(path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001676 }
1677 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001678}
1679
1680static noinline_for_stack
1681int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001682 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 Borisov867cc1f2018-06-20 15:48:48 +03001691 ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
1692 num_bytes, parent, root_objectid,
1693 owner, offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001694 if (ret == 0) {
1695 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001696 update_inline_extent_backref(path, iref, refs_to_add,
1697 extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001698 } else if (ret == -ENOENT) {
Nikolay Borisova639cde2018-06-20 15:49:10 +03001699 setup_inline_extent_backref(trans->fs_info, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001700 root_objectid, owner, offset,
1701 refs_to_add, extent_op);
1702 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001703 }
1704 return ret;
1705}
1706
1707static int insert_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001708 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 Borisov10728402018-06-20 15:48:43 +03001715 ret = insert_tree_block_ref(trans, path, bytenr, parent,
1716 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001717 } else {
Nikolay Borisov62b895a2018-06-20 15:48:44 +03001718 ret = insert_extent_data_ref(trans, path, bytenr, parent,
1719 root_objectid, owner, offset,
1720 refs_to_add);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001721 }
1722 return ret;
1723}
1724
1725static int remove_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001726 struct btrfs_path *path,
1727 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001728 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001729{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001730 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001731
1732 BUG_ON(!is_data && refs_to_drop != 1);
1733 if (iref) {
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001734 update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
1735 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001736 } else if (is_data) {
Nikolay Borisove9f62902018-06-20 15:48:46 +03001737 ret = remove_extent_data_ref(trans, path, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07001738 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001739 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001740 *last_ref = 1;
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03001741 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001742 }
1743 return ret;
1744}
1745
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001746static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
1747 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05001748{
Jeff Mahoney86557862015-06-15 09:41:16 -04001749 int j, ret = 0;
1750 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001751 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 Mahoneyd04c6b82015-06-15 09:41:14 -04001758
1759 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04001760
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 Mahoney4d89d372015-06-15 09:41:15 -04001811 GFP_NOFS, 0);
1812 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04001813 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001814 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001815 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05001816}
Chris Mason15916de2008-11-19 21:17:22 -05001817
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001818int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00001819 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001820{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001821 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001822 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001823 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001824
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001825
Filipe Manana29992412016-05-27 17:42:05 +01001826 /*
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 Mahoney0b246af2016-06-22 18:54:23 -04001830 btrfs_bio_counter_inc_blocked(fs_info);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001831 /* Tell the block device(s) that the sectors can be discarded */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001832 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
1833 &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001834 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001835 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001836 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001837 int i;
1838
Liu Hui1f3c79a2009-01-05 15:57:51 -05001839
Jan Schmidta1d3c472011-08-04 17:15:33 +02001840 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001841 u64 bytes;
Anand Jain38b5f682017-11-29 18:53:43 +08001842 struct request_queue *req_q;
1843
Filipe Manana627e0872018-01-30 18:40:22 +00001844 if (!stripe->dev->bdev) {
1845 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
1846 continue;
1847 }
Anand Jain38b5f682017-11-29 18:53:43 +08001848 req_q = bdev_get_queue(stripe->dev->bdev);
1849 if (!blk_queue_discard(req_q))
Josef Bacikd5e20032011-08-04 14:52:27 +00001850 continue;
1851
Li Dongyang5378e602011-03-24 10:24:27 +00001852 ret = btrfs_issue_discard(stripe->dev->bdev,
1853 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001854 stripe->length,
1855 &bytes);
Li Dongyang5378e602011-03-24 10:24:27 +00001856 if (!ret)
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001857 discarded_bytes += bytes;
Li Dongyang5378e602011-03-24 10:24:27 +00001858 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001859 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001860
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 Hui1f3c79a2009-01-05 15:57:51 -05001867 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08001868 btrfs_put_bbio(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001869 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001870 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00001871
1872 if (actual_bytes)
1873 *actual_bytes = discarded_bytes;
1874
Liu Hui1f3c79a2009-01-05 15:57:51 -05001875
David Woodhouse53b381b2013-01-29 18:40:14 -05001876 if (ret == -EOPNOTSUPP)
1877 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001878 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001879}
1880
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001881/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001882int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruo82fa1132019-04-04 14:45:35 +08001883 struct btrfs_ref *generic_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001884{
Qu Wenruo82fa1132019-04-04 14:45:35 +08001885 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07001886 int old_ref_mod, new_ref_mod;
Zheng Yan31840ae2008-09-23 13:14:14 -04001887 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001888
Qu Wenruo82fa1132019-04-04 14:45:35 +08001889 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 Yan31840ae2008-09-23 13:14:14 -04001893
Qu Wenruo82fa1132019-04-04 14:45:35 +08001894 if (generic_ref->type == BTRFS_REF_METADATA)
1895 ret = btrfs_add_delayed_tree_ref(trans, generic_ref,
Qu Wenruoed4f2552019-04-04 14:45:31 +08001896 NULL, &old_ref_mod, &new_ref_mod);
Qu Wenruo82fa1132019-04-04 14:45:35 +08001897 else
1898 ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07001899 &old_ref_mod, &new_ref_mod);
Omar Sandovald7eae342017-06-06 16:45:31 -07001900
Qu Wenruo82fa1132019-04-04 14:45:35 +08001901 btrfs_ref_tree_mod(fs_info, generic_ref);
Qu Wenruo8a5040f2019-04-04 14:45:33 +08001902
Qu Wenruoddf30cf2019-04-04 14:45:34 +08001903 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0)
Qu Wenruo78192442019-05-15 07:33:48 +08001904 sub_pinned_bytes(fs_info, generic_ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07001905
Zheng Yan31840ae2008-09-23 13:14:14 -04001906 return ret;
1907}
1908
Nikolay Borisovbd3c6852018-06-18 14:59:25 +03001909/*
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 Mason925baed2008-06-25 16:01:30 -04001943static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001944 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001945 u64 parent, u64 root_objectid,
1946 u64 owner, u64 offset, int refs_to_add,
1947 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001948{
Chris Mason5caf2a02007-04-02 11:20:42 -04001949 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001950 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001951 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07001952 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08001953 u64 bytenr = node->bytenr;
1954 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001955 u64 refs;
1956 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05001957
Chris Mason5caf2a02007-04-02 11:20:42 -04001958 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001959 if (!path)
1960 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001961
David Sterbae4058b52015-11-27 16:31:35 +01001962 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001963 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001964 /* this will setup the path even if it fails to insert the back ref */
Nikolay Borisova639cde2018-06-20 15:49:10 +03001965 ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes,
1966 parent, root_objectid, owner,
1967 offset, refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08001968 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001969 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07001970
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 Zheng5d4f98a2009-06-10 10:45:14 -04001976 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07001977 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001978 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 Yan31840ae2008-09-23 13:14:14 -04001983
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001984 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001985 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001986
David Sterbae4058b52015-11-27 16:31:35 +01001987 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04001988 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04001989 /* now insert the actual backref */
Nikolay Borisov37593412018-06-20 15:48:45 +03001990 ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid,
1991 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001992 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04001993 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001994out:
Chris Mason74493f72007-12-11 09:25:06 -05001995 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08001996 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001997}
1998
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001999static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002000 struct btrfs_delayed_ref_node *node,
2001 struct btrfs_delayed_extent_op *extent_op,
2002 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002003{
Chris Mason56bec292009-03-13 10:10:06 -04002004 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002005 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 Mason56bec292009-03-13 10:10:06 -04002010
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002011 ins.objectid = node->bytenr;
2012 ins.offset = node->num_bytes;
2013 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002014
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002015 ref = btrfs_delayed_node_to_data_ref(node);
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03002016 trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002017
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002018 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2019 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002020 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002021
2022 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002023 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002024 flags |= extent_op->flags_to_set;
Nikolay Borisovef89b822018-06-20 15:48:58 +03002025 ret = alloc_reserved_file_extent(trans, parent, ref_root,
2026 flags, ref->objectid,
2027 ref->offset, &ins,
2028 node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002029 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03002030 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
2031 ref->objectid, ref->offset,
2032 node->ref_mod, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002033 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03002034 ret = __btrfs_free_extent(trans, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002035 ref_root, ref->objectid,
2036 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002037 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002038 } else {
2039 BUG();
2040 }
Chris Mason56bec292009-03-13 10:10:06 -04002041 return ret;
2042}
2043
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002044static 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
2062static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Josef Bacikd2788502017-09-29 15:43:57 -04002063 struct btrfs_delayed_ref_head *head,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064 struct btrfs_delayed_extent_op *extent_op)
2065{
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03002066 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002067 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 Bacikb1c79e02013-05-09 13:49:30 -04002074 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002075
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002076 if (trans->aborted)
2077 return 0;
2078
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002079 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05002080 metadata = 0;
2081
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002082 path = btrfs_alloc_path();
2083 if (!path)
2084 return -ENOMEM;
2085
Josef Bacikd2788502017-09-29 15:43:57 -04002086 key.objectid = head->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002087
Josef Bacik3173a182013-03-07 14:22:04 -05002088 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002089 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002090 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002091 } else {
2092 key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacikd2788502017-09-29 15:43:57 -04002093 key.offset = head->num_bytes;
Josef Bacik3173a182013-03-07 14:22:04 -05002094 }
2095
2096again:
David Sterbae4058b52015-11-27 16:31:35 +01002097 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002098 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002099 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002100 if (ret < 0) {
2101 err = ret;
2102 goto out;
2103 }
2104 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002105 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002106 if (path->slots[0] > 0) {
2107 path->slots[0]--;
2108 btrfs_item_key_to_cpu(path->nodes[0], &key,
2109 path->slots[0]);
Josef Bacikd2788502017-09-29 15:43:57 -04002110 if (key.objectid == head->bytenr &&
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002111 key.type == BTRFS_EXTENT_ITEM_KEY &&
Josef Bacikd2788502017-09-29 15:43:57 -04002112 key.offset == head->num_bytes)
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002113 ret = 0;
2114 }
2115 if (ret > 0) {
2116 btrfs_release_path(path);
2117 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002118
Josef Bacikd2788502017-09-29 15:43:57 -04002119 key.objectid = head->bytenr;
2120 key.offset = head->num_bytes;
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002121 key.type = BTRFS_EXTENT_ITEM_KEY;
2122 goto again;
2123 }
2124 } else {
2125 err = -EIO;
2126 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002127 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002128 }
2129
2130 leaf = path->nodes[0];
2131 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03002132
David Sterba6d8ff4e2018-06-26 16:20:59 +02002133 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03002134 err = -EINVAL;
2135 btrfs_print_v0_err(fs_info);
2136 btrfs_abort_transaction(trans, err);
2137 goto out;
2138 }
2139
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002140 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);
2144out:
2145 btrfs_free_path(path);
2146 return err;
2147}
2148
2149static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002150 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 Zheng5d4f98a2009-06-10 10:45:14 -04002156 u64 parent = 0;
2157 u64 ref_root = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002158
2159 ref = btrfs_delayed_node_to_tree_ref(node);
Nikolay Borisovf97806f2018-06-20 15:49:04 +03002160 trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002161
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002162 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2163 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002164 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002165
Liu Bo02794222016-09-14 19:19:05 -07002166 if (node->ref_mod != 1) {
Nikolay Borisovf97806f2018-06-20 15:49:04 +03002167 btrfs_err(trans->fs_info,
Liu Bo02794222016-09-14 19:19:05 -07002168 "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 Zheng5d4f98a2009-06-10 10:45:14 -04002173 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002174 BUG_ON(!extent_op || !extent_op->update_flags);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03002175 ret = alloc_reserved_tree_block(trans, node, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002176 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Nikolay Borisov2590d0f2018-06-20 15:48:59 +03002177 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
2178 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002179 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03002180 ret = __btrfs_free_extent(trans, node, parent, ref_root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002181 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002182 } else {
2183 BUG();
2184 }
2185 return ret;
2186}
2187
Chris Mason56bec292009-03-13 10:10:06 -04002188/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002189static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002190 struct btrfs_delayed_ref_node *node,
2191 struct btrfs_delayed_extent_op *extent_op,
2192 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002193{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002194 int ret = 0;
2195
Josef Bacik857cc2f2013-10-07 15:21:08 -04002196 if (trans->aborted) {
2197 if (insert_reserved)
Nikolay Borisov5fac7f92018-06-20 15:49:11 +03002198 btrfs_pin_extent(trans->fs_info, node->bytenr,
Josef Bacik857cc2f2013-10-07 15:21:08 -04002199 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002200 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002201 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002202
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002203 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2204 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Nikolay Borisovf97806f2018-06-20 15:49:04 +03002205 ret = run_delayed_tree_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002206 insert_reserved);
2207 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2208 node->type == BTRFS_SHARED_DATA_REF_KEY)
Nikolay Borisov2bf98ef2018-06-20 15:49:00 +03002209 ret = run_delayed_data_ref(trans, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002210 insert_reserved);
2211 else
2212 BUG();
Josef Bacik80ee54b2018-10-11 15:54:22 -04002213 if (ret && insert_reserved)
2214 btrfs_pin_extent(trans->fs_info, node->bytenr,
2215 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002216 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002217}
2218
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002219static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04002220select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002221{
Filipe Mananacffc3372015-07-09 13:13:44 +01002222 struct btrfs_delayed_ref_node *ref;
2223
Liu Boe3d03962018-08-23 03:51:50 +08002224 if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002225 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002226
Filipe Mananacffc3372015-07-09 13:13:44 +01002227 /*
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 Xiaoguang1d57ee92016-10-26 18:07:33 +08002233 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 Mananacffc3372015-07-09 13:13:44 +01002236
Liu Boe3d03962018-08-23 03:51:50 +08002237 ref = rb_entry(rb_first_cached(&head->ref_tree),
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002238 struct btrfs_delayed_ref_node, ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002239 ASSERT(list_empty(&ref->add_list));
2240 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04002241}
2242
Josef Bacik2eadaa22017-09-29 15:43:52 -04002243static 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 Bacikbedc66172018-12-03 10:20:31 -05002253static struct btrfs_delayed_extent_op *cleanup_extent_op(
2254 struct btrfs_delayed_ref_head *head)
Josef Bacikb00e6252017-09-29 15:43:53 -04002255{
2256 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
Josef Bacikbedc66172018-12-03 10:20:31 -05002257
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
2269static 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 Bacikb00e6252017-09-29 15:43:53 -04002273 int ret;
2274
Josef Bacikbedc66172018-12-03 10:20:31 -05002275 extent_op = cleanup_extent_op(head);
Josef Bacikb00e6252017-09-29 15:43:53 -04002276 if (!extent_op)
2277 return 0;
2278 head->extent_op = NULL;
Josef Bacikb00e6252017-09-29 15:43:53 -04002279 spin_unlock(&head->lock);
Nikolay Borisov20b9a2d2018-06-20 15:49:01 +03002280 ret = run_delayed_extent_op(trans, head, extent_op);
Josef Bacikb00e6252017-09-29 15:43:53 -04002281 btrfs_free_delayed_extent_op(extent_op);
2282 return ret ? ret : 1;
2283}
2284
Josef Bacik31890da2018-11-21 14:05:41 -05002285void 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 Bacik07c47772018-12-03 10:20:30 -05002288{
Josef Bacikba2c4d42018-12-03 10:20:33 -05002289 int nr_items = 1; /* Dropping this ref head update. */
Josef Bacik07c47772018-12-03 10:20:30 -05002290
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 Bacik280c29082019-06-18 16:09:19 -04002301 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik07c47772018-12-03 10:20:30 -05002302 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 Bacikba2c4d42018-12-03 10:20:33 -05002307 /*
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 Bacik07c47772018-12-03 10:20:30 -05002312 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 Bacikba2c4d42018-12-03 10:20:33 -05002316 nr_items += btrfs_csum_bytes_to_leaves(fs_info,
2317 head->num_bytes);
Josef Bacik07c47772018-12-03 10:20:30 -05002318 }
2319 }
2320
Josef Bacikba2c4d42018-12-03 10:20:33 -05002321 btrfs_delayed_refs_rsv_release(fs_info, nr_items);
Josef Bacik07c47772018-12-03 10:20:30 -05002322}
2323
Josef Bacik194ab0b2017-09-29 15:43:54 -04002324static int cleanup_ref_head(struct btrfs_trans_handle *trans,
Josef Bacik194ab0b2017-09-29 15:43:54 -04002325 struct btrfs_delayed_ref_head *head)
2326{
Nikolay Borisovf9871ed2018-06-20 15:49:03 +03002327
2328 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002329 struct btrfs_delayed_ref_root *delayed_refs;
2330 int ret;
2331
2332 delayed_refs = &trans->transaction->delayed_refs;
2333
Josef Bacikbedc66172018-12-03 10:20:31 -05002334 ret = run_and_cleanup_extent_op(trans, head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002335 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 Boe3d03962018-08-23 03:51:50 +08002350 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04002351 spin_unlock(&head->lock);
2352 spin_unlock(&delayed_refs->lock);
2353 return 1;
2354 }
Josef Bacikd7baffd2018-12-03 10:20:29 -05002355 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikc1103f72017-09-29 15:43:56 -04002356 spin_unlock(&head->lock);
Nikolay Borisov1e7a1422018-04-11 11:21:18 +03002357 spin_unlock(&delayed_refs->lock);
Josef Bacikc1103f72017-09-29 15:43:56 -04002358
Josef Bacikc1103f72017-09-29 15:43:56 -04002359 if (head->must_insert_reserved) {
Josef Bacikd2788502017-09-29 15:43:57 -04002360 btrfs_pin_extent(fs_info, head->bytenr,
2361 head->num_bytes, 1);
Josef Bacikc1103f72017-09-29 15:43:56 -04002362 if (head->is_data) {
Josef Bacikd2788502017-09-29 15:43:57 -04002363 ret = btrfs_del_csums(trans, fs_info, head->bytenr,
2364 head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04002365 }
2366 }
2367
Josef Bacik31890da2018-11-21 14:05:41 -05002368 btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
Josef Bacik07c47772018-12-03 10:20:30 -05002369
2370 trace_run_delayed_ref_head(fs_info, head, 0);
Josef Bacikc1103f72017-09-29 15:43:56 -04002371 btrfs_delayed_ref_unlock(head);
Josef Bacikd2788502017-09-29 15:43:57 -04002372 btrfs_put_delayed_ref_head(head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002373 return 0;
2374}
2375
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03002376static 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 Fengqi5637c742018-10-11 13:40:33 +08002385 head = btrfs_select_ref_head(delayed_refs);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03002386 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 Fengqi9e920a62018-10-11 13:40:34 +08002395 ret = btrfs_delayed_ref_lock(delayed_refs, head);
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03002396 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 Borisove7261382018-08-15 10:39:55 +03002409static 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 Borisov0110a4c2018-08-15 10:39:56 +03002422 lockdep_assert_held(&locked_ref->mutex);
2423 lockdep_assert_held(&locked_ref->lock);
2424
Nikolay Borisove7261382018-08-15 10:39:55 +03002425 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 Mahoney79787ea2012-03-12 16:03:00 +01002489/*
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 Bacikd7df2c72014-01-23 09:21:38 -05002493static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacikd7df2c72014-01-23 09:21:38 -05002494 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002495{
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002496 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002497 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002498 struct btrfs_delayed_ref_head *locked_ref = NULL;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002499 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002500 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002501 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002502 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002503
2504 delayed_refs = &trans->transaction->delayed_refs;
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002505 do {
Chris Mason56bec292009-03-13 10:10:06 -04002506 if (!locked_ref) {
Nikolay Borisovb1cdbcb2018-08-15 10:39:54 +03002507 locked_ref = btrfs_obtain_ref_head(trans);
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002508 if (IS_ERR_OR_NULL(locked_ref)) {
2509 if (PTR_ERR(locked_ref) == -EAGAIN) {
2510 continue;
2511 } else {
2512 break;
2513 }
Chris Mason56bec292009-03-13 10:10:06 -04002514 }
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002515 count++;
Chris Mason56bec292009-03-13 10:10:06 -04002516 }
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002517 /*
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 Bacikd7df2c72014-01-23 09:21:38 -05002529 spin_lock(&locked_ref->lock);
Nikolay Borisovbe97f132018-04-19 11:06:39 +03002530 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002531
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002532 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 Borisovf9871ed2018-06-20 15:49:03 +03002545 ret = cleanup_ref_head(trans, locked_ref);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002546 if (ret > 0 ) {
Josef Bacikb00e6252017-09-29 15:43:53 -04002547 /* We dropped our lock, we need to loop. */
2548 ret = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002549 continue;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002550 } else if (ret) {
2551 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002552 }
Josef Bacikc1103f72017-09-29 15:43:56 -04002553 }
Chris Mason56bec292009-03-13 10:10:06 -04002554
Josef Bacikb00e6252017-09-29 15:43:53 -04002555 /*
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002556 * Either success case or btrfs_run_delayed_refs_for_head
2557 * returned -EAGAIN, meaning we need to select another head
Josef Bacikb00e6252017-09-29 15:43:53 -04002558 */
Josef Bacikb00e6252017-09-29 15:43:53 -04002559
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002560 locked_ref = NULL;
Chris Mason1887be62009-03-13 10:11:24 -04002561 cond_resched();
Nikolay Borisov0110a4c2018-08-15 10:39:56 +03002562 } while ((nr != -1 && count < nr) || locked_ref);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002563
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 Sterbaf8c269d2015-01-16 17:21:12 +01002579 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002580 spin_unlock(&delayed_refs->lock);
2581 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002582 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002583}
2584
Arne Jansen709c0482011-09-12 12:22:57 +02002585#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 */
2591static 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 Mahoney2ff7e612016-06-22 18:54:24 -04002628static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002629{
2630 u64 num_bytes;
2631
2632 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2633 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002634 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002635 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 Steeves01327612016-05-19 21:18:45 -04002639 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002640 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002641 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002642}
2643
Josef Bacik12621332015-02-03 07:50:16 -08002644/*
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 Mahoney2ff7e612016-06-22 18:54:24 -04002648u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002649{
2650 u64 csum_size;
2651 u64 num_csums_per_leaf;
2652 u64 num_csums;
2653
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002654 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002655 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002656 (u64)btrfs_super_csum_size(fs_info->super_copy));
2657 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002658 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 Masonc3e69d52009-03-13 10:17:05 -04002663/*
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 Mahoney79787ea2012-03-12 16:03:00 +01002669 *
2670 * Returns 0 on success or if called with an aborted transaction
2671 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002672 */
2673int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002674 unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04002675{
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002676 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -04002677 struct rb_node *node;
2678 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002679 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002680 int ret;
2681 int run_all = count == (unsigned long)-1;
Chris Masonc3e69d52009-03-13 10:17:05 -04002682
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002683 /* We'll clean this up in btrfs_cleanup_transaction */
2684 if (trans->aborted)
2685 return 0;
2686
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002687 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08002688 return 0;
2689
Chris Masonc3e69d52009-03-13 10:17:05 -04002690 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08002691 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05002692 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002693
Chris Masonc3e69d52009-03-13 10:17:05 -04002694again:
Arne Jansen709c0482011-09-12 12:22:57 +02002695#ifdef SCRAMBLE_DELAYED_REFS
2696 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2697#endif
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002698 ret = __btrfs_run_delayed_refs(trans, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002699 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002700 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002701 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002702 }
2703
Chris Mason56bec292009-03-13 10:10:06 -04002704 if (run_all) {
Josef Bacik119e80d2018-11-21 14:05:42 -05002705 btrfs_create_pending_block_groups(trans);
Josef Bacikea658ba2012-09-11 16:57:25 -04002706
Josef Bacikd7df2c72014-01-23 09:21:38 -05002707 spin_lock(&delayed_refs->lock);
Liu Bo5c9d0282018-08-23 03:51:49 +08002708 node = rb_first_cached(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002709 if (!node) {
2710 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002711 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002712 }
Josef Bacikd2788502017-09-29 15:43:57 -04002713 head = rb_entry(node, struct btrfs_delayed_ref_head,
2714 href_node);
2715 refcount_inc(&head->refs);
Chris Mason56bec292009-03-13 10:10:06 -04002716 spin_unlock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002717
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 Bacikd7df2c72014-01-23 09:21:38 -05002723 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002724 goto again;
2725 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002726out:
Chris Masona28ec192007-03-06 20:08:01 -05002727 return 0;
2728}
2729
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002730int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002731 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002732 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002733{
2734 struct btrfs_delayed_extent_op *extent_op;
2735 int ret;
2736
Miao Xie78a61842012-11-21 02:21:28 +00002737 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002738 if (!extent_op)
2739 return -ENOMEM;
2740
2741 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01002742 extent_op->update_flags = true;
2743 extent_op->update_key = false;
2744 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002745 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002746
David Sterbac6e340b2019-03-20 11:42:34 +01002747 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002748 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002749 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002750 return ret;
2751}
2752
Liu Boe4c3b2d2017-01-30 12:25:28 -08002753static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002754 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 Boe4c3b2d2017-01-30 12:25:28 -08002761 struct btrfs_transaction *cur_trans;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002762 struct rb_node *node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002763 int ret = 0;
2764
ethanwu998ac6d2018-04-29 15:59:42 +08002765 spin_lock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002766 cur_trans = root->fs_info->running_transaction;
ethanwu998ac6d2018-04-29 15:59:42 +08002767 if (cur_trans)
2768 refcount_inc(&cur_trans->use_count);
2769 spin_unlock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08002770 if (!cur_trans)
2771 return 0;
2772
2773 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002774 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08002775 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002776 if (!head) {
2777 spin_unlock(&delayed_refs->lock);
ethanwu998ac6d2018-04-29 15:59:42 +08002778 btrfs_put_transaction(cur_trans);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002779 return 0;
2780 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002781
2782 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04002783 refcount_inc(&head->refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002784 spin_unlock(&delayed_refs->lock);
2785
David Sterbab3b4aa72011-04-21 01:20:15 +02002786 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002787
David Sterba8cc33e52011-05-02 15:29:25 +02002788 /*
2789 * Mutex was contended, block until it's released and let
2790 * caller try again
2791 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002792 mutex_lock(&head->mutex);
2793 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04002794 btrfs_put_delayed_ref_head(head);
ethanwu998ac6d2018-04-29 15:59:42 +08002795 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002796 return -EAGAIN;
2797 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002798 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002799
2800 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002801 /*
2802 * XXX: We should replace this with a proper search function in the
2803 * future.
2804 */
Liu Boe3d03962018-08-23 03:51:50 +08002805 for (node = rb_first_cached(&head->ref_tree); node;
2806 node = rb_next(node)) {
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002807 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002808 /* 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);
ethanwu998ac6d2018-04-29 15:59:42 +08002829 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002830 return ret;
2831}
2832
Liu Boe4c3b2d2017-01-30 12:25:28 -08002833static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002834 struct btrfs_path *path,
2835 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002836{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002837 struct btrfs_fs_info *fs_info = root->fs_info;
2838 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002839 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002840 struct btrfs_extent_data_ref *ref;
2841 struct btrfs_extent_inline_ref *iref;
2842 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002843 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002844 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06002845 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04002846 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002847
Chris Masonbe20aa92007-12-17 20:14:01 -05002848 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002849 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002850 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002851
Chris Masonbe20aa92007-12-17 20:14:01 -05002852 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2853 if (ret < 0)
2854 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002855 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002856
2857 ret = -ENOENT;
2858 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002859 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002860
Zheng Yan31840ae2008-09-23 13:14:14 -04002861 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002862 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002863 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002864
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002865 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002866 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002867
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002868 ret = 1;
2869 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002870 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 Bo3de28d52017-08-18 15:15:19 -06002881
2882 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
2883 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002884 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 Zhengf321e492008-07-30 09:26:11 -04002895 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002896out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002897 return ret;
2898}
2899
Liu Boe4c3b2d2017-01-30 12:25:28 -08002900int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
2901 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002902{
2903 struct btrfs_path *path;
2904 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002905
2906 path = btrfs_alloc_path();
2907 if (!path)
Su Yue9132c4f2018-05-30 14:49:10 +08002908 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002909
2910 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08002911 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002912 offset, bytenr);
2913 if (ret && ret != -ENOENT)
2914 goto out;
2915
Misono Tomohiro380fd062018-08-30 10:59:16 +09002916 ret = check_delayed_ref(root, path, objectid, offset, bytenr);
2917 } while (ret == -EAGAIN);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002918
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002919out:
Yan Zhengf321e492008-07-30 09:26:11 -04002920 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002921 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2922 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002923 return ret;
2924}
2925
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002926static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002927 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002928 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07002929 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04002930{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002931 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04002932 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002933 u64 num_bytes;
2934 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002935 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002936 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002937 struct btrfs_key key;
2938 struct btrfs_file_extent_item *fi;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002939 struct btrfs_ref generic_ref = { 0 };
2940 bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC);
Zheng Yan31840ae2008-09-23 13:14:14 -04002941 int i;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002942 int action;
Zheng Yan31840ae2008-09-23 13:14:14 -04002943 int level;
2944 int ret = 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002945
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002946 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04002947 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02002948
Zheng Yan31840ae2008-09-23 13:14:14 -04002949 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002950 nritems = btrfs_header_nritems(buf);
2951 level = btrfs_header_level(buf);
2952
Miao Xie27cdeb72014-04-02 19:51:05 +08002953 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002954 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05002955
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002956 if (full_backref)
2957 parent = buf->start;
2958 else
2959 parent = 0;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002960 if (inc)
2961 action = BTRFS_ADD_DELAYED_REF;
2962 else
2963 action = BTRFS_DROP_DELAYED_REF;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002964
Zheng Yan31840ae2008-09-23 13:14:14 -04002965 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002966 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002967 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02002968 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04002969 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002970 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04002971 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002972 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04002973 BTRFS_FILE_EXTENT_INLINE)
2974 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002975 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2976 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04002977 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04002978
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002979 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2980 key.offset -= btrfs_file_extent_offset(buf, fi);
Qu Wenruo82fa1132019-04-04 14:45:35 +08002981 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 Wenruodd28b6a2019-04-04 14:45:30 +08002987 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08002988 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08002989 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08002990 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002991 if (ret)
2992 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05002993 } else {
2994 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002995 num_bytes = fs_info->nodesize;
Qu Wenruo82fa1132019-04-04 14:45:35 +08002996 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 Wenruodd28b6a2019-04-04 14:45:30 +08003001 if (inc)
Qu Wenruo82fa1132019-04-04 14:45:35 +08003002 ret = btrfs_inc_extent_ref(trans, &generic_ref);
Qu Wenruodd28b6a2019-04-04 14:45:30 +08003003 else
Qu Wenruoffd4bb22019-04-04 14:45:36 +08003004 ret = btrfs_free_extent(trans, &generic_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003005 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003006 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003007 }
3008 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003009 return 0;
3010fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003011 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003012}
3013
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003014int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003015 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04003016{
Josef Bacike339a6b2014-07-02 10:54:25 -07003017 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003018}
Zheng Yan31840ae2008-09-23 13:14:14 -04003019
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003020int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003021 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003022{
Josef Bacike339a6b2014-07-02 10:54:25 -07003023 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003024}
3025
Chris Mason9078a3e2007-04-26 16:46:15 -04003026static int write_one_cache_group(struct btrfs_trans_handle *trans,
Chris Mason9078a3e2007-04-26 16:46:15 -04003027 struct btrfs_path *path,
3028 struct btrfs_block_group_cache *cache)
3029{
David Sterba39db2322019-03-20 11:57:46 +01003030 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04003031 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003032 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003033 unsigned long bi;
3034 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003035
Chris Mason9078a3e2007-04-26 16:46:15 -04003036 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003037 if (ret) {
3038 if (ret > 0)
3039 ret = -ENOENT;
Chris Mason54aa1f42007-06-22 14:16:25 -04003040 goto fail;
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003041 }
Chris Mason5f39d392007-10-15 16:14:19 -04003042
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 Mason54aa1f42007-06-22 14:16:25 -04003047fail:
Filipe Manana24b89d02015-04-25 18:31:05 +01003048 btrfs_release_path(path);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003049 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003050
3051}
3052
Josef Bacik0af3d002010-06-21 14:48:16 -04003053static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3054 struct btrfs_trans_handle *trans,
3055 struct btrfs_path *path)
3056{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003057 struct btrfs_fs_info *fs_info = block_group->fs_info;
3058 struct btrfs_root *root = fs_info->tree_root;
Josef Bacik0af3d002010-06-21 14:48:16 -04003059 struct inode *inode = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08003060 struct extent_changeset *data_reserved = NULL;
Josef Bacik0af3d002010-06-21 14:48:16 -04003061 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003062 int dcs = BTRFS_DC_ERROR;
David Sterbaf8c269d2015-01-16 17:21:12 +01003063 u64 num_pages = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003064 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 Leeee221842015-12-15 01:42:10 +09003071 if (block_group->key.offset < (100 * SZ_1M)) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003072 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 Bacik0c0ef4b2015-02-12 09:43:51 -05003078 if (trans->aborted)
3079 return 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003080again:
David Sterba7949f332019-03-20 13:40:19 +01003081 inode = lookup_free_space_inode(block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003082 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3083 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003084 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003085 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 Sterba4ca75f12019-03-20 13:42:57 +01003095 ret = create_free_space_inode(trans, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003096 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 Bacik0c0ef4b2015-02-12 09:43:51 -05003108 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 Mahoney66642832016-06-10 18:19:25 -04003119 btrfs_abort_transaction(trans, ret);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003120 goto out_put;
3121 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003122 WARN_ON(ret);
3123
Josef Bacik8e138e02017-11-17 14:50:46 -05003124 /* 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 Bacik0af3d002010-06-21 14:48:16 -04003131 if (i_size_read(inode) > 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003132 ret = btrfs_check_trunc_cache_free_space(fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003133 &fs_info->global_block_rsv);
Miao Xie7b61cd92013-05-13 13:55:09 +00003134 if (ret)
3135 goto out_put;
3136
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003137 ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003138 if (ret)
3139 goto out_put;
3140 }
3141
3142 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003143 if (block_group->cached != BTRFS_CACHE_FINISHED ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003144 !btrfs_test_opt(fs_info, SPACE_CACHE)) {
Liu Bocf7c1ef2012-07-06 03:31:34 -06003145 /*
3146 * don't bother trying to write stuff out _if_
3147 * a) we're not cached,
Liu Bo1a79c1f2017-03-06 13:49:02 -08003148 * b) we're with nospace_cache mount option,
3149 * c) we're with v2 space_cache (FREE_SPACE_TREE).
Liu Bocf7c1ef2012-07-06 03:31:34 -06003150 */
Josef Bacik2b209822010-12-03 13:17:53 -05003151 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003152 spin_unlock(&block_group->lock);
3153 goto out_put;
3154 }
3155 spin_unlock(&block_group->lock);
3156
Josef Bacik6fc823b2012-08-06 13:46:38 -06003157 /*
Josef Bacik2968b1f2015-10-01 12:55:18 -04003158 * 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 Bacik6fc823b2012-08-06 13:46:38 -06003167 * 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 Leeee221842015-12-15 01:42:10 +09003172 num_pages = div_u64(block_group->key.offset, SZ_256M);
Josef Bacik0af3d002010-06-21 14:48:16 -04003173 if (!num_pages)
3174 num_pages = 1;
3175
Josef Bacik0af3d002010-06-21 14:48:16 -04003176 num_pages *= 16;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003177 num_pages *= PAGE_SIZE;
Josef Bacik0af3d002010-06-21 14:48:16 -04003178
Qu Wenruo364ecf32017-02-27 15:10:38 +08003179 ret = btrfs_check_data_free_space(inode, &data_reserved, 0, num_pages);
Josef Bacik0af3d002010-06-21 14:48:16 -04003180 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 Bacik2968b1f2015-10-01 12:55:18 -04003186 /*
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 Bacik2b209822010-12-03 13:17:53 -05003194 if (!ret)
3195 dcs = BTRFS_DC_SETUP;
Josef Bacik2968b1f2015-10-01 12:55:18 -04003196 else if (ret == -ENOSPC)
3197 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
Josef Bacikc09544e2011-08-30 10:19:10 -04003198
Josef Bacik0af3d002010-06-21 14:48:16 -04003199out_put:
3200 iput(inode);
3201out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003202 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003203out:
3204 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003205 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003206 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003207 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003208 spin_unlock(&block_group->lock);
3209
Qu Wenruo364ecf32017-02-27 15:10:38 +08003210 extent_changeset_free(data_reserved);
Josef Bacik0af3d002010-06-21 14:48:16 -04003211 return ret;
3212}
3213
David Sterbabbebb3e2019-03-20 12:02:55 +01003214int btrfs_setup_space_cache(struct btrfs_trans_handle *trans)
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003215{
David Sterbabbebb3e2019-03-20 12:02:55 +01003216 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003217 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 Mahoney0b246af2016-06-22 18:54:23 -04003222 !btrfs_test_opt(fs_info, SPACE_CACHE))
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003223 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 Mason1bbc6212015-04-06 12:46:08 -07003240/*
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 Borisov21217052018-02-07 17:55:41 +02003252int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
Chris Mason1bbc6212015-04-06 12:46:08 -07003253{
Nikolay Borisov21217052018-02-07 17:55:41 +02003254 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason1bbc6212015-04-06 12:46:08 -07003255 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 Mananab58d1a92015-04-25 18:29:16 +01003266 if (list_empty(&cur_trans->dirty_bgs)) {
3267 spin_unlock(&cur_trans->dirty_bgs_lock);
3268 return 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003269 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003270 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Chris Mason1bbc6212015-04-06 12:46:08 -07003271 spin_unlock(&cur_trans->dirty_bgs_lock);
3272
3273again:
Chris Mason1bbc6212015-04-06 12:46:08 -07003274 /*
3275 * make sure all the block groups on our dirty list actually
3276 * exist
3277 */
Nikolay Borisov6c686b32018-02-07 17:55:40 +02003278 btrfs_create_pending_block_groups(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07003279
3280 if (!path) {
3281 path = btrfs_alloc_path();
3282 if (!path)
3283 return -ENOMEM;
3284 }
3285
Filipe Mananab58d1a92015-04-25 18:29:16 +01003286 /*
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 Mason1bbc6212015-04-06 12:46:08 -07003292 while (!list_empty(&dirty)) {
Josef Bacikba2c4d42018-12-03 10:20:33 -05003293 bool drop_reserve = true;
3294
Chris Mason1bbc6212015-04-06 12:46:08 -07003295 cache = list_first_entry(&dirty,
3296 struct btrfs_block_group_cache,
3297 dirty_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07003298 /*
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 Mahoneyafdb5712016-09-09 12:09:35 -04003305 btrfs_wait_cache_io(trans, cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003306 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 Sterbafe041532019-03-20 13:51:56 +01003328 ret = btrfs_write_out_cache(trans, cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003329 if (ret == 0 && cache->io_ctl.inode) {
3330 num_started++;
3331 should_put = 0;
3332
3333 /*
Nikolay Borisov45ae2c12018-02-08 18:25:18 +02003334 * 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 Mason1bbc6212015-04-06 12:46:08 -07003337 */
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 Mananaff1f8252015-05-06 16:15:09 +01003347 if (!ret) {
David Sterba39db2322019-03-20 11:57:46 +01003348 ret = write_one_cache_group(trans, path, cache);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003349 /*
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 Bacikba2c4d42018-12-03 10:20:33 -05003365 drop_reserve = false;
Filipe Mananaff1f8252015-05-06 16:15:09 +01003366 }
3367 spin_unlock(&cur_trans->dirty_bgs_lock);
3368 } else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003369 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003370 }
3371 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003372
Andrea Gelmini52042d82018-11-28 12:05:13 +01003373 /* if it's not on the io list, we need to put the block group */
Chris Mason1bbc6212015-04-06 12:46:08 -07003374 if (should_put)
3375 btrfs_put_block_group(cache);
Josef Bacikba2c4d42018-12-03 10:20:33 -05003376 if (drop_reserve)
3377 btrfs_delayed_refs_rsv_release(fs_info, 1);
Chris Mason1bbc6212015-04-06 12:46:08 -07003378
3379 if (ret)
3380 break;
Filipe Mananab58d1a92015-04-25 18:29:16 +01003381
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 Mason1bbc6212015-04-06 12:46:08 -07003389 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003390 mutex_unlock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003391
3392 /*
3393 * go through delayed refs for all the stuff we've just kicked off
3394 * and then loop back (just once)
3395 */
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003396 ret = btrfs_run_delayed_refs(trans, 0);
Chris Mason1bbc6212015-04-06 12:46:08 -07003397 if (!ret && loops == 0) {
3398 loops++;
3399 spin_lock(&cur_trans->dirty_bgs_lock);
3400 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003401 /*
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 Mason1bbc6212015-04-06 12:46:08 -07003409 spin_unlock(&cur_trans->dirty_bgs_lock);
Liu Boc79a1752016-07-20 17:44:12 -07003410 } else if (ret < 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003411 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
Chris Mason1bbc6212015-04-06 12:46:08 -07003412 }
3413
3414 btrfs_free_path(path);
3415 return ret;
3416}
3417
David Sterba5742d152019-03-20 12:04:08 +01003418int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
Chris Mason9078a3e2007-04-26 16:46:15 -04003419{
David Sterba5742d152019-03-20 12:04:08 +01003420 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003421 struct btrfs_block_group_cache *cache;
Josef Bacikce93ec52014-11-17 15:45:48 -05003422 struct btrfs_transaction *cur_trans = trans->transaction;
3423 int ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003424 int should_put;
Chris Mason9078a3e2007-04-26 16:46:15 -04003425 struct btrfs_path *path;
Chris Mason1bbc6212015-04-06 12:46:08 -07003426 struct list_head *io = &cur_trans->io_bgs;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003427 int num_started = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003428
3429 path = btrfs_alloc_path();
3430 if (!path)
3431 return -ENOMEM;
3432
Josef Bacikce93ec52014-11-17 15:45:48 -05003433 /*
Filipe Mananae44081e2015-12-18 03:02:48 +00003434 * 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 Bacikce93ec52014-11-17 15:45:48 -05003445 * delayed refs to make sure we have the best chance at doing this all
3446 * in one shot.
3447 */
Filipe Mananae44081e2015-12-18 03:02:48 +00003448 spin_lock(&cur_trans->dirty_bgs_lock);
Josef Bacikce93ec52014-11-17 15:45:48 -05003449 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 Masonc9dc4c62015-04-04 17:14:42 -07003453
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 Mananae44081e2015-12-18 03:02:48 +00003460 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003461 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003462 btrfs_wait_cache_io(trans, cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003463 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003464 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003465 }
3466
Chris Mason1bbc6212015-04-06 12:46:08 -07003467 /*
3468 * don't remove from the dirty list until after we've waited
3469 * on any pending IO
3470 */
Josef Bacikce93ec52014-11-17 15:45:48 -05003471 list_del_init(&cache->dirty_list);
Filipe Mananae44081e2015-12-18 03:02:48 +00003472 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003473 should_put = 1;
3474
Chris Mason1bbc6212015-04-06 12:46:08 -07003475 cache_save_setup(cache, trans, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003476
Josef Bacikce93ec52014-11-17 15:45:48 -05003477 if (!ret)
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003478 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003479 (unsigned long) -1);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003480
3481 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3482 cache->io_ctl.inode = NULL;
David Sterbafe041532019-03-20 13:51:56 +01003483 ret = btrfs_write_out_cache(trans, cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003484 if (ret == 0 && cache->io_ctl.inode) {
3485 num_started++;
3486 should_put = 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003487 list_add_tail(&cache->io_list, io);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003488 } 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 Mananaff1f8252015-05-06 16:15:09 +01003496 if (!ret) {
David Sterba39db2322019-03-20 11:57:46 +01003497 ret = write_one_cache_group(trans, path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003498 /*
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 Sterba39db2322019-03-20 11:57:46 +01003514 ret = write_one_cache_group(trans, path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003515 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003516 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003517 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003518 }
Chris Masonc9dc4c62015-04-04 17:14:42 -07003519
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 Bacikba2c4d42018-12-03 10:20:33 -05003523 btrfs_delayed_refs_rsv_release(fs_info, 1);
Filipe Mananae44081e2015-12-18 03:02:48 +00003524 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003525 }
Filipe Mananae44081e2015-12-18 03:02:48 +00003526 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003527
Nikolay Borisov45ae2c12018-02-08 18:25:18 +02003528 /*
3529 * Refer to the definition of io_bgs member for details why it's safe
3530 * to use it without any locking
3531 */
Chris Mason1bbc6212015-04-06 12:46:08 -07003532 while (!list_empty(io)) {
3533 cache = list_first_entry(io, struct btrfs_block_group_cache,
Chris Masonc9dc4c62015-04-04 17:14:42 -07003534 io_list);
3535 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003536 btrfs_wait_cache_io(trans, cache, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003537 btrfs_put_block_group(cache);
3538 }
3539
Chris Mason9078a3e2007-04-26 16:46:15 -04003540 btrfs_free_path(path);
Josef Bacikce93ec52014-11-17 15:45:48 -05003541 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003542}
3543
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003544int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05003545{
3546 struct btrfs_block_group_cache *block_group;
3547 int readonly = 0;
3548
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003549 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003550 if (!block_group || block_group->ro)
3551 readonly = 1;
3552 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003553 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003554 return readonly;
3555}
3556
Chris Mason8790d502008-04-03 16:29:03 -04003557static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3558{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003559 u64 extra_flags = chunk_to_extended(flags) &
3560 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003561
Miao Xiede98ced2013-01-29 10:13:12 +00003562 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003563 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 Xiede98ced2013-01-29 10:13:12 +00003569 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003570}
Chris Mason593060d2008-03-25 16:50:33 -04003571
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003572/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003573 * returns target flags in extended format or 0 if restripe for this
3574 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003575 *
David Sterbadccdb072018-03-21 00:20:05 +01003576 * should be called with balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003577 */
3578static 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 Dryomovfc67c452012-03-27 17:09:17 +03003583 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 Dryomova46d11a2012-01-16 22:04:47 +02003601 * @flags: available profiles in extended format (see ctree.h)
3602 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003603 * 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 Dryomova46d11a2012-01-16 22:04:47 +02003606 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003607static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003608{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003609 u64 num_devices = fs_info->fs_devices->rw_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003610 u64 target;
Zhao Lei9c170b22015-09-15 21:08:08 +08003611 u64 raid_type;
3612 u64 allowed = 0;
Chris Masona061fc82008-05-07 11:43:44 -04003613
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003614 /*
3615 * see if restripe for this chunk_type is in progress, if so
3616 * try to reduce to the target profile
3617 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003618 spin_lock(&fs_info->balance_lock);
3619 target = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003620 if (target) {
3621 /* pick target profile only if it's already available */
3622 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003623 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003624 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003625 }
3626 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003627 spin_unlock(&fs_info->balance_lock);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003628
David Woodhouse53b381b2013-01-29 18:40:14 -05003629 /* First, mask out the RAID levels which aren't possible */
Zhao Lei9c170b22015-09-15 21:08:08 +08003630 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
3631 if (num_devices >= btrfs_raid_array[raid_type].devs_min)
Anand Jain41a6e892018-04-25 19:01:43 +08003632 allowed |= btrfs_raid_array[raid_type].bg_flag;
Zhao Lei9c170b22015-09-15 21:08:08 +08003633 }
3634 allowed &= flags;
Chris Masona061fc82008-05-07 11:43:44 -04003635
Zhao Lei9c170b22015-09-15 21:08:08 +08003636 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 Masonec44a352008-04-28 15:29:52 -04003646
Zhao Lei9c170b22015-09-15 21:08:08 +08003647 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
Chris Masonec44a352008-04-28 15:29:52 -04003648
Zhao Lei9c170b22015-09-15 21:08:08 +08003649 return extended_to_chunk(flags | allowed);
Chris Masonec44a352008-04-28 15:29:52 -04003650}
3651
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003652static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003653{
Miao Xiede98ced2013-01-29 10:13:12 +00003654 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003655 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003656
3657 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003658 flags = orig_flags;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003659 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00003660
3661 if (flags & BTRFS_BLOCK_GROUP_DATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003662 flags |= fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00003663 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003664 flags |= fs_info->avail_system_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00003665 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003666 flags |= fs_info->avail_metadata_alloc_bits;
3667 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003668
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003669 return btrfs_reduce_alloc_profile(fs_info, flags);
Yan, Zhengb742bb822010-05-16 10:46:24 -04003670}
Josef Bacik6a632092009-02-20 11:00:09 -05003671
Jeff Mahoney1b868262017-05-17 11:38:35 -04003672static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04003673{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003674 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003675 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003676 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003677
Yan, Zhengb742bb822010-05-16 10:46:24 -04003678 if (data)
3679 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003680 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb822010-05-16 10:46:24 -04003681 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3682 else
3683 flags = BTRFS_BLOCK_GROUP_METADATA;
3684
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003685 ret = get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05003686 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003687}
3688
Jeff Mahoney1b868262017-05-17 11:38:35 -04003689u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info)
3690{
3691 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_DATA);
3692}
3693
3694u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info)
3695{
3696 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3697}
3698
3699u64 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 Bacik97e728d2009-04-21 17:40:57 -04003704static 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 Mason0e4f8f82011-04-15 16:05:44 -04003712 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003713 }
3714 rcu_read_unlock();
3715}
3716
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003717static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
Josef Bacik698d0082012-09-12 14:08:47 -04003718 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003719{
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04003720 u64 bytes_used = btrfs_space_info_used(sinfo, false);
Chris Masone5bc2452010-10-26 13:37:56 -04003721 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003722
Chris Mason0e4f8f82011-04-15 16:05:44 -04003723 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 Mahoney0b246af2016-06-22 18:54:23 -04003731 thresh = btrfs_super_total_bytes(fs_info->super_copy);
Byongho Leeee221842015-12-15 01:42:10 +09003732 thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
Chris Mason0e4f8f82011-04-15 16:05:44 -04003733
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04003734 if (sinfo->total_bytes - bytes_used < thresh)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003735 return 1;
3736 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003737
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04003738 if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003739 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003740 return 1;
3741}
3742
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003743static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04003744{
3745 u64 num_dev;
3746
David Sterba9fa02ac2019-05-17 11:43:20 +02003747 num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max;
3748 if (!num_dev)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003749 num_dev = fs_info->fs_devices->rw_devices;
Liu Bo15d1ff82012-03-29 09:57:44 -04003750
Filipe Manana39c2d7f2015-05-20 14:01:55 +01003751 return num_dev;
Liu Bo15d1ff82012-03-29 09:57:44 -04003752}
3753
Filipe Manana39c2d7f2015-05-20 14:01:55 +01003754/*
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 Borisov451a2c12018-06-20 15:49:07 +03003759void check_system_chunk(struct btrfs_trans_handle *trans, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04003760{
Nikolay Borisov451a2c12018-06-20 15:49:07 +03003761 struct btrfs_fs_info *fs_info = trans->fs_info;
Liu Bo15d1ff82012-03-29 09:57:44 -04003762 struct btrfs_space_info *info;
3763 u64 left;
3764 u64 thresh;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01003765 int ret = 0;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01003766 u64 num_devs;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01003767
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 Sterbaa32bf9a2018-03-16 02:21:22 +01003772 lockdep_assert_held(&fs_info->chunk_mutex);
Liu Bo15d1ff82012-03-29 09:57:44 -04003773
Josef Bacik280c29082019-06-18 16:09:19 -04003774 info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
Liu Bo15d1ff82012-03-29 09:57:44 -04003775 spin_lock(&info->lock);
Liu Bo41361352017-02-13 15:42:21 -08003776 left = info->total_bytes - btrfs_space_info_used(info, true);
Liu Bo15d1ff82012-03-29 09:57:44 -04003777 spin_unlock(&info->lock);
3778
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003779 num_devs = get_profile_num_devs(fs_info, type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01003780
3781 /* num_devs device items to update and 1 chunk item to add or remove */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003782 thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) +
3783 btrfs_calc_trans_metadata_size(fs_info, 1);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01003784
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003785 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 Bacik5da6afe2019-06-18 16:09:24 -04003788 btrfs_dump_space_info(fs_info, info, 0, 0);
Liu Bo15d1ff82012-03-29 09:57:44 -04003789 }
3790
3791 if (left < thresh) {
Jeff Mahoney1b868262017-05-17 11:38:35 -04003792 u64 flags = btrfs_system_alloc_profile(fs_info);
Liu Bo15d1ff82012-03-29 09:57:44 -04003793
Filipe Manana4fbcdf62015-05-20 14:01:54 +01003794 /*
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 Borisovc216b202018-06-20 15:49:06 +03003800 ret = btrfs_alloc_chunk(trans, flags);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01003801 }
3802
3803 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003804 ret = btrfs_block_rsv_add(fs_info->chunk_root,
3805 &fs_info->chunk_block_rsv,
Filipe Manana4fbcdf62015-05-20 14:01:54 +01003806 thresh, BTRFS_RESERVE_NO_FLUSH);
3807 if (!ret)
3808 trans->chunk_bytes_reserved += thresh;
Liu Bo15d1ff82012-03-29 09:57:44 -04003809 }
3810}
3811
Liu Bo28b737f2016-07-29 11:09:50 -07003812/*
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 Bacikfc471cb2019-06-18 16:09:17 -04003821int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
3822 enum btrfs_chunk_alloc_enum force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003823{
Nikolay Borisov01458822018-06-20 15:49:05 +03003824 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003825 struct btrfs_space_info *space_info;
Nikolay Borisov2556fbb2018-04-18 10:27:57 +03003826 bool wait_for_alloc = false;
3827 bool should_alloc = false;
Yan Zhengc146afa2008-11-12 14:34:12 -05003828 int ret = 0;
3829
Josef Bacikc6b305a2012-12-18 09:16:16 -05003830 /* Don't re-enter if we're already allocating a chunk */
3831 if (trans->allocating_chunk)
3832 return -ENOSPC;
3833
Josef Bacik280c29082019-06-18 16:09:19 -04003834 space_info = btrfs_find_space_info(fs_info, flags);
Jeff Mahoneydc2d3002018-03-20 15:25:25 -04003835 ASSERT(space_info);
Chris Mason6324fbf2008-03-24 15:01:59 -04003836
Nikolay Borisov2556fbb2018-04-18 10:27:57 +03003837 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 Mason6324fbf2008-03-24 15:01:59 -04003870
Nikolay Borisov2556fbb2018-04-18 10:27:57 +03003871 cond_resched();
3872 } while (wait_for_alloc);
Josef Bacik25179202008-10-29 14:49:05 -04003873
Josef Bacik6d741192011-04-11 20:20:11 -04003874 mutex_lock(&fs_info->chunk_mutex);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003875 trans->allocating_chunk = true;
3876
Josef Bacik97e728d2009-04-21 17:40:57 -04003877 /*
Josef Bacik67377732010-09-16 16:19:09 -04003878 * 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 Bacik97e728d2009-04-21 17:40:57 -04003885 * 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 Bacik9ed74f22009-09-11 16:12:44 -04003889 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003890 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 Bo15d1ff82012-03-29 09:57:44 -04003896 /*
3897 * Check if we have enough space in SYSTEM chunk because we may need
3898 * to update devices.
3899 */
Nikolay Borisov451a2c12018-06-20 15:49:07 +03003900 check_system_chunk(trans, flags);
Liu Bo15d1ff82012-03-29 09:57:44 -04003901
Nikolay Borisovc216b202018-06-20 15:49:06 +03003902 ret = btrfs_alloc_chunk(trans, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003903 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003904
Josef Bacik9ed74f22009-09-11 16:12:44 -04003905 spin_lock(&space_info->lock);
Nikolay Borisov57f16422018-04-11 11:21:19 +03003906 if (ret < 0) {
3907 if (ret == -ENOSPC)
3908 space_info->full = 1;
3909 else
3910 goto out;
3911 } else {
Yan, Zheng424499d2010-05-16 10:46:25 -04003912 ret = 1;
Josef Bacik21a94f72018-10-11 15:54:03 -04003913 space_info->max_extent_size = 0;
Nikolay Borisov57f16422018-04-11 11:21:19 +03003914 }
Josef Bacik6d741192011-04-11 20:20:11 -04003915
Chris Mason0e4f8f82011-04-15 16:05:44 -04003916 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003917out:
Josef Bacik6d741192011-04-11 20:20:11 -04003918 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003919 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03003920 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana00d80e32015-07-20 14:56:20 +01003921 /*
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 Manana5ce55552018-10-12 10:03:55 +01003935 if (trans->chunk_bytes_reserved >= (u64)SZ_2M)
Nikolay Borisov6c686b32018-02-07 17:55:40 +02003936 btrfs_create_pending_block_groups(trans);
Filipe Manana5ce55552018-10-12 10:03:55 +01003937
Josef Bacik0f9dd462008-09-23 13:14:11 -04003938 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003939}
3940
Josef Bacikce93ec52014-11-17 15:45:48 -05003941static int update_block_group(struct btrfs_trans_handle *trans,
David Sterba6b279402019-03-20 12:10:15 +01003942 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04003943{
David Sterba6b279402019-03-20 12:10:15 +01003944 struct btrfs_fs_info *info = trans->fs_info;
Josef Bacik0af3d002010-06-21 14:48:16 -04003945 struct btrfs_block_group_cache *cache = NULL;
Chris Masondb945352007-10-15 16:15:53 -04003946 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04003947 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04003948 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04003949 int factor;
Josef Bacikba2c4d42018-12-03 10:20:33 -05003950 int ret = 0;
Chris Mason3e1ad542007-05-07 20:03:49 -04003951
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003952 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00003953 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02003954 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003955 if (alloc)
3956 old_val += num_bytes;
3957 else
3958 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02003959 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00003960 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003961
Chris Masond3977122009-01-05 21:25:51 -05003962 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04003963 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikba2c4d42018-12-03 10:20:33 -05003964 if (!cache) {
3965 ret = -ENOENT;
3966 break;
3967 }
David Sterba46df06b2018-07-13 20:46:30 +02003968 factor = btrfs_bg_type_to_factor(cache->flags);
3969
Josef Bacik9d66e232010-08-25 16:54:15 -04003970 /*
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 Bacik676f1f72019-06-20 15:37:48 -04003977 btrfs_cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04003978
Chris Masondb945352007-10-15 16:15:53 -04003979 byte_in_group = bytenr - cache->key.objectid;
3980 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04003981
Josef Bacik25179202008-10-29 14:49:05 -04003982 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04003983 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04003984
Jeff Mahoney6202df62016-06-22 18:54:22 -04003985 if (btrfs_test_opt(info, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04003986 cache->disk_cache_state < BTRFS_DC_CLEAR)
3987 cache->disk_cache_state = BTRFS_DC_CLEAR;
3988
Chris Mason9078a3e2007-04-26 16:46:15 -04003989 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04003990 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04003991 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04003992 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003993 btrfs_set_block_group_used(&cache->item, old_val);
3994 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04003995 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003996 cache->space_info->bytes_used += num_bytes;
3997 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04003998 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04003999 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04004000 } else {
Chris Masondb945352007-10-15 16:15:53 -04004001 old_val -= num_bytes;
Filipe Mananaae0ab002014-11-26 15:28:52 +00004002 btrfs_set_block_group_used(&cache->item, old_val);
4003 cache->pinned += num_bytes;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04004004 btrfs_space_info_update_bytes_pinned(info,
4005 cache->space_info, num_bytes);
Filipe Mananaae0ab002014-11-26 15:28:52 +00004006 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 Bacik47ab2a62014-09-18 11:20:02 -04004010
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004011 trace_btrfs_space_reservation(info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04004012 cache->space_info->flags,
4013 num_bytes, 1);
Ethan Liendec59fa2018-07-13 16:50:42 +08004014 percpu_counter_add_batch(&cache->space_info->total_bytes_pinned,
4015 num_bytes,
4016 BTRFS_TOTAL_BYTES_PINNED_BATCH);
Filipe Mananaae0ab002014-11-26 15:28:52 +00004017 set_extent_dirty(info->pinned_extents,
4018 bytenr, bytenr + num_bytes - 1,
4019 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04004020 }
Chris Mason1bbc6212015-04-06 12:46:08 -07004021
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 Bacikba2c4d42018-12-03 10:20:33 -05004026 trans->delayed_ref_updates++;
Chris Mason1bbc6212015-04-06 12:46:08 -07004027 btrfs_get_block_group(cache);
4028 }
4029 spin_unlock(&trans->transaction->dirty_bgs_lock);
4030
Filipe Manana036a9342015-11-23 15:25:16 +00004031 /*
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 Wenruo031f24d2018-05-22 16:43:47 +08004037 if (!alloc && old_val == 0)
4038 btrfs_mark_bg_unused(cache);
Filipe Manana036a9342015-11-23 15:25:16 +00004039
Chris Masonfa9c0d792009-04-03 09:47:43 -04004040 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04004041 total -= num_bytes;
4042 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04004043 }
Josef Bacikba2c4d42018-12-03 10:20:33 -05004044
4045 /* Modified block groups are accounted for in the delayed_refs_rsv. */
4046 btrfs_update_delayed_refs_rsv(trans);
4047 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04004048}
Chris Mason6324fbf2008-03-24 15:01:59 -04004049
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004050static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04004051{
Josef Bacik0f9dd462008-09-23 13:14:11 -04004052 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004053 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004054
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004055 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 Boa1897fd2012-12-27 09:01:23 +00004058
4059 if (bytenr < (u64)-1)
4060 return bytenr;
4061
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004062 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004063 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04004064 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04004065
Yan Zhengd2fb3432008-12-11 16:30:39 -05004066 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04004067 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05004068
4069 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04004070}
4071
David Sterbafdf08602019-03-20 12:12:32 +01004072static int pin_down_extent(struct btrfs_block_group_cache *cache,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004073 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05004074{
David Sterbafdf08602019-03-20 12:12:32 +01004075 struct btrfs_fs_info *fs_info = cache->fs_info;
4076
Yan Zheng11833d62009-09-11 16:11:19 -04004077 spin_lock(&cache->space_info->lock);
4078 spin_lock(&cache->lock);
4079 cache->pinned += num_bytes;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04004080 btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info,
4081 num_bytes);
Yan Zheng11833d62009-09-11 16:11:19 -04004082 if (reserved) {
4083 cache->reserved -= num_bytes;
4084 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05004085 }
Yan Zheng11833d62009-09-11 16:11:19 -04004086 spin_unlock(&cache->lock);
4087 spin_unlock(&cache->space_info->lock);
4088
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004089 trace_btrfs_space_reservation(fs_info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04004090 cache->space_info->flags, num_bytes, 1);
Ethan Liendec59fa2018-07-13 16:50:42 +08004091 percpu_counter_add_batch(&cache->space_info->total_bytes_pinned,
4092 num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004093 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004094 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05004095 return 0;
4096}
Chris Mason9078a3e2007-04-26 16:46:15 -04004097
Yan, Zhengf0486c62010-05-16 10:46:25 -04004098/*
4099 * this function must be called within transaction
4100 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004101int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04004102 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04004103{
Yan, Zhengf0486c62010-05-16 10:46:25 -04004104 struct btrfs_block_group_cache *cache;
4105
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004106 cache = btrfs_lookup_block_group(fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004107 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04004108
David Sterbafdf08602019-03-20 12:12:32 +01004109 pin_down_extent(cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004110
4111 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004112 return 0;
4113}
Zheng Yane8569812008-09-26 10:05:48 -04004114
Yan, Zhengf0486c62010-05-16 10:46:25 -04004115/*
Chris Masone688b7252011-10-31 20:52:39 -04004116 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04004117 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004118int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04004119 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004120{
Chris Masone688b7252011-10-31 20:52:39 -04004121 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04004122 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04004123
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004124 cache = btrfs_lookup_block_group(fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004125 if (!cache)
4126 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04004127
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 Bacik676f1f72019-06-20 15:37:48 -04004134 btrfs_cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04004135
David Sterbafdf08602019-03-20 12:12:32 +01004136 pin_down_extent(cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04004137
4138 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04004139 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04004140 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004141 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04004142}
4143
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004144static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
4145 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004146{
4147 int ret;
4148 struct btrfs_block_group_cache *block_group;
4149 struct btrfs_caching_control *caching_ctl;
4150
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004151 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004152 if (!block_group)
4153 return -EINVAL;
4154
Josef Bacik676f1f72019-06-20 15:37:48 -04004155 btrfs_cache_block_group(block_group, 0);
Josef Bacike3cb3392019-06-20 15:37:50 -04004156 caching_ctl = btrfs_get_caching_control(block_group);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004157
4158 if (!caching_ctl) {
4159 /* Logic error */
Josef Bacik676f1f72019-06-20 15:37:48 -04004160 BUG_ON(!btrfs_block_group_cache_done(block_group));
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004161 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 Bacik6f410d12019-06-20 15:37:49 -04004166 ret = btrfs_add_excluded_extent(fs_info, start,
4167 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004168 } 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 Bacik6f410d12019-06-20 15:37:49 -04004181 ret = btrfs_add_excluded_extent(fs_info, start,
4182 num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004183 }
4184out_lock:
4185 mutex_unlock(&caching_ctl->mutex);
Josef Bacike3cb3392019-06-20 15:37:50 -04004186 btrfs_put_caching_control(caching_ctl);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004187 }
4188 btrfs_put_block_group(block_group);
4189 return ret;
4190}
4191
David Sterbabcdc4282019-03-20 12:14:33 +01004192int btrfs_exclude_logged_extents(struct extent_buffer *eb)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004193{
David Sterbabcdc4282019-03-20 12:14:33 +01004194 struct btrfs_fs_info *fs_info = eb->fs_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004195 struct btrfs_file_extent_item *item;
4196 struct btrfs_key key;
4197 int found_type;
4198 int i;
Gu Jinxiangb89311e2018-05-22 17:46:51 +08004199 int ret = 0;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004200
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004201 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004202 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 Jinxiangb89311e2018-05-22 17:46:51 +08004216 ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
4217 if (ret)
4218 break;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004219 }
4220
Gu Jinxiangb89311e2018-05-22 17:46:51 +08004221 return ret;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04004222}
4223
Filipe Manana9cfa3e32016-04-26 15:39:32 +01004224static void
4225btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg)
4226{
4227 atomic_inc(&bg->reservations);
4228}
4229
Josef Bacikfb25e912011-07-26 17:00:46 -04004230/**
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08004231 * btrfs_add_reserved_bytes - update the block_group and space info counters
Josef Bacikfb25e912011-07-26 17:00:46 -04004232 * @cache: The cache we are manipulating
Wang Xiaoguang18513092016-07-25 15:51:40 +08004233 * @ram_bytes: The number of bytes of file content, and will be same to
4234 * @num_bytes except for the compress path.
Josef Bacikfb25e912011-07-26 17:00:46 -04004235 * @num_bytes: The number of bytes in question
Miao Xiee570fd22014-06-19 10:42:50 +08004236 * @delalloc: The blocks are allocated for the delalloc write
Josef Bacikfb25e912011-07-26 17:00:46 -04004237 *
Xiaoguang Wang745699e2016-09-23 12:38:50 +08004238 * 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, Zhengf0486c62010-05-16 10:46:25 -04004241 */
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08004242static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
Wang Xiaoguang18513092016-07-25 15:51:40 +08004243 u64 ram_bytes, u64 num_bytes, int delalloc)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004244{
Josef Bacikfb25e912011-07-26 17:00:46 -04004245 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004246 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004247
Josef Bacikfb25e912011-07-26 17:00:46 -04004248 spin_lock(&space_info->lock);
4249 spin_lock(&cache->lock);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08004250 if (cache->ro) {
4251 ret = -EAGAIN;
Josef Bacikfb25e912011-07-26 17:00:46 -04004252 } else {
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08004253 cache->reserved += num_bytes;
4254 space_info->bytes_reserved += num_bytes;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04004255 btrfs_space_info_update_bytes_may_use(cache->fs_info,
4256 space_info, -ram_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08004257 if (delalloc)
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08004258 cache->delalloc_bytes += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004259 }
Josef Bacikfb25e912011-07-26 17:00:46 -04004260 spin_unlock(&cache->lock);
4261 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004262 return ret;
4263}
4264
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08004265/**
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 jiang556f3ca2018-08-17 00:37:14 +08004277static void btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
4278 u64 num_bytes, int delalloc)
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08004279{
4280 struct btrfs_space_info *space_info = cache->space_info;
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08004281
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 Bacik21a94f72018-10-11 15:54:03 -04004288 space_info->max_extent_size = 0;
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08004289
4290 if (delalloc)
4291 cache->delalloc_bytes -= num_bytes;
4292 spin_unlock(&cache->lock);
4293 spin_unlock(&space_info->lock);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08004294}
David Sterba8b74c032017-02-10 19:20:56 +01004295void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04004296{
Yan Zheng11833d62009-09-11 16:11:19 -04004297 struct btrfs_caching_control *next;
4298 struct btrfs_caching_control *caching_ctl;
4299 struct btrfs_block_group_cache *cache;
4300
Josef Bacik9e351cc2014-03-13 15:42:13 -04004301 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04004302
4303 list_for_each_entry_safe(caching_ctl, next,
4304 &fs_info->caching_block_groups, list) {
4305 cache = caching_ctl->block_group;
Josef Bacik676f1f72019-06-20 15:37:48 -04004306 if (btrfs_block_group_cache_done(cache)) {
Yan Zheng11833d62009-09-11 16:11:19 -04004307 cache->last_byte_to_unpin = (u64)-1;
4308 list_del_init(&caching_ctl->list);
Josef Bacike3cb3392019-06-20 15:37:50 -04004309 btrfs_put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04004310 } 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 Bacik9e351cc2014-03-13 15:42:13 -04004320 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004321
Josef Bacik67f9c222019-06-19 13:47:23 -04004322 btrfs_update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04004323}
4324
Josef Bacikc759c4e2015-10-02 15:25:10 -04004325/*
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 */
4329static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004330fetch_cluster_info(struct btrfs_fs_info *fs_info,
4331 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04004332{
4333 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04004334
4335 *empty_cluster = 0;
4336 if (btrfs_mixed_space_info(space_info))
4337 return ret;
4338
Josef Bacikc759c4e2015-10-02 15:25:10 -04004339 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004340 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02004341 if (btrfs_test_opt(fs_info, SSD))
4342 *empty_cluster = SZ_2M;
4343 else
Byongho Leeee221842015-12-15 01:42:10 +09004344 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02004345 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
4346 btrfs_test_opt(fs_info, SSD_SPREAD)) {
4347 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004348 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04004349 }
4350
4351 return ret;
4352}
4353
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004354static int unpin_extent_range(struct btrfs_fs_info *fs_info,
4355 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00004356 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04004357{
Yan Zheng11833d62009-09-11 16:11:19 -04004358 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04004359 struct btrfs_space_info *space_info;
4360 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04004361 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04004362 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04004363 u64 total_unpinned = 0;
4364 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04004365 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04004366
4367 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04004368 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04004369 if (!cache ||
4370 start >= cache->key.objectid + cache->key.offset) {
4371 if (cache)
4372 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04004373 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04004374 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004375 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04004376
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004377 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04004378 cache->space_info,
4379 &empty_cluster);
4380 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04004381 }
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 Manana678886b2014-12-07 21:31:47 +00004388 if (return_free_space)
4389 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04004390 }
Josef Bacik25179202008-10-29 14:49:05 -04004391
Yan, Zhengf0486c62010-05-16 10:46:25 -04004392 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04004393 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04004394 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004395
Josef Bacikc759c4e2015-10-02 15:25:10 -04004396 /*
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 Bacik7b398f82012-10-22 15:52:28 -04004409 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04004410 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004411 cache->pinned -= len;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04004412 btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len);
Josef Bacikc51e7bb2016-03-25 13:25:54 -04004413
4414 trace_btrfs_space_reservation(fs_info, "pinned",
4415 space_info->flags, len, 0);
Josef Bacik4f4db212015-09-29 11:40:47 -04004416 space_info->max_extent_size = 0;
Ethan Liendec59fa2018-07-13 16:50:42 +08004417 percpu_counter_add_batch(&space_info->total_bytes_pinned,
4418 -len, BTRFS_TOTAL_BYTES_PINNED_BATCH);
Josef Bacik7b398f82012-10-22 15:52:28 -04004419 if (cache->ro) {
4420 space_info->bytes_readonly += len;
4421 readonly = true;
4422 }
Josef Bacik25179202008-10-29 14:49:05 -04004423 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04004424 if (!readonly && return_free_space &&
4425 global_rsv->space_info == space_info) {
4426 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03004427
Josef Bacik7b398f82012-10-22 15:52:28 -04004428 spin_lock(&global_rsv->lock);
4429 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04004430 to_add = min(len, global_rsv->size -
4431 global_rsv->reserved);
4432 global_rsv->reserved += to_add;
Josef Bacikbb96c4e2019-06-18 16:09:21 -04004433 btrfs_space_info_update_bytes_may_use(fs_info,
4434 space_info, to_add);
Josef Bacik7b398f82012-10-22 15:52:28 -04004435 if (global_rsv->reserved >= global_rsv->size)
4436 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04004437 trace_btrfs_space_reservation(fs_info,
4438 "space_info",
4439 space_info->flags,
4440 to_add, 1);
4441 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04004442 }
4443 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04004444 /* Add to any tickets we may have */
4445 if (len)
Josef Bacikd44b72a2019-06-18 16:09:18 -04004446 btrfs_space_info_add_new_bytes(fs_info,
4447 space_info, len);
Josef Bacik7b398f82012-10-22 15:52:28 -04004448 }
4449 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04004450 }
4451
4452 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04004453 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04004454 return 0;
4455}
4456
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02004457int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
Chris Masona28ec192007-03-06 20:08:01 -05004458{
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02004459 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04004460 struct btrfs_block_group_cache *block_group, *tmp;
4461 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04004462 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04004463 u64 start;
4464 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05004465 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05004466
Yan Zheng11833d62009-09-11 16:11:19 -04004467 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 Mahoneye33e17e2015-06-15 09:41:19 -04004472 while (!trans->aborted) {
Filipe Manana0e6ec382018-11-16 13:04:44 +00004473 struct extent_state *cached_state = NULL;
4474
Filipe Mananad4b450c2015-01-29 19:18:25 +00004475 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04004476 ret = find_first_extent_bit(unpin, 0, &start, &end,
Filipe Manana0e6ec382018-11-16 13:04:44 +00004477 EXTENT_DIRTY, &cached_state);
Filipe Mananad4b450c2015-01-29 19:18:25 +00004478 if (ret) {
4479 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05004480 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00004481 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05004482
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004483 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004484 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00004485 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05004486
Filipe Manana0e6ec382018-11-16 13:04:44 +00004487 clear_extent_dirty(unpin, start, end, &cached_state);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004488 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00004489 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana0e6ec382018-11-16 13:04:44 +00004490 free_extent_state(cached_state);
Chris Masonb9473432009-03-13 11:00:37 -04004491 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05004492 }
Josef Bacik817d52f2009-07-13 21:29:25 -04004493
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04004494 /*
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 Mahoney2ff7e612016-06-22 18:54:24 -04004505 ret = btrfs_discard_extent(fs_info,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04004506 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 Sterba913e1532017-07-13 15:32:18 +02004517 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04004518 ret, errstr);
4519 }
4520 }
4521
Chris Masone20d96d2007-03-22 12:13:20 -04004522 return 0;
4523}
4524
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004525static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +03004526 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 Masona28ec192007-03-06 20:08:01 -05004530{
Nikolay Borisove72cb922018-06-20 15:48:57 +03004531 struct btrfs_fs_info *info = trans->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04004532 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004533 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04004534 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04004535 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004536 struct btrfs_extent_item *ei;
4537 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05004538 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004539 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05004540 int extent_slot = 0;
4541 int found_extent = 0;
4542 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004543 u32 item_size;
4544 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08004545 u64 bytenr = node->bytenr;
4546 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07004547 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004548 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05004549
Chris Mason5caf2a02007-04-02 11:20:42 -04004550 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004551 if (!path)
4552 return -ENOMEM;
4553
David Sterbae4058b52015-11-27 16:31:35 +01004554 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04004555 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004556
4557 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4558 BUG_ON(!is_data && refs_to_drop != 1);
4559
Josef Bacik3173a182013-03-07 14:22:04 -05004560 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02004561 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05004562
Nikolay Borisovfbe48012018-06-20 15:48:52 +03004563 ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
4564 parent, root_objectid, owner_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004565 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05004566 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05004567 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004568 while (extent_slot >= 0) {
4569 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05004570 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004571 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05004572 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004573 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4574 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05004575 found_extent = 1;
4576 break;
4577 }
Josef Bacik3173a182013-03-07 14:22:04 -05004578 if (key.type == BTRFS_METADATA_ITEM_KEY &&
4579 key.offset == owner_objectid) {
4580 found_extent = 1;
4581 break;
4582 }
Chris Mason952fcca2008-02-18 16:33:44 -05004583 if (path->slots[0] - extent_slot > 5)
4584 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004585 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05004586 }
Nikolay Borisova79865c2018-06-21 09:45:00 +03004587
Zheng Yan31840ae2008-09-23 13:14:14 -04004588 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004589 BUG_ON(iref);
Nikolay Borisov87cc7a82018-06-20 15:49:12 +03004590 ret = remove_extent_backref(trans, path, NULL,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05004591 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07004592 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06004593 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004594 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06004595 goto out;
4596 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004597 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04004598 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004599
4600 key.objectid = bytenr;
4601 key.type = BTRFS_EXTENT_ITEM_KEY;
4602 key.offset = num_bytes;
4603
Josef Bacik3173a182013-03-07 14:22:04 -05004604 if (!is_data && skinny_metadata) {
4605 key.type = BTRFS_METADATA_ITEM_KEY;
4606 key.offset = owner_objectid;
4607 }
4608
Zheng Yan31840ae2008-09-23 13:14:14 -04004609 ret = btrfs_search_slot(trans, extent_root,
4610 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05004611 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 Manana9ce49a02014-04-24 15:15:28 +01004627 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05004628 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 Bacikf3465ca2008-11-12 14:19:50 -05004635 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004636 btrfs_err(info,
4637 "umm, got %d back from search, was looking for %llu",
4638 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00004639 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02004640 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004641 }
David Sterba005d6422012-09-18 07:52:32 -06004642 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004643 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06004644 goto out;
4645 }
Zheng Yan31840ae2008-09-23 13:14:14 -04004646 extent_slot = path->slots[0];
4647 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05304648 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02004649 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004650 btrfs_err(info,
4651 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004652 bytenr, parent, root_objectid, owner_objectid,
4653 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04004654 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04004655 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004656 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04004657 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06004658 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05004659 }
Chris Mason5f39d392007-10-15 16:14:19 -04004660
4661 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004662 item_size = btrfs_item_size_nr(leaf, extent_slot);
David Sterba6d8ff4e2018-06-26 16:20:59 +02004663 if (unlikely(item_size < sizeof(*ei))) {
Nikolay Borisovba3c2b12018-06-26 16:57:36 +03004664 ret = -EINVAL;
4665 btrfs_print_v0_err(info);
4666 btrfs_abort_transaction(trans, ret);
4667 goto out;
4668 }
Chris Mason952fcca2008-02-18 16:33:44 -05004669 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04004670 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05004671 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
4672 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004673 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 Mason952fcca2008-02-18 16:33:44 -05004677 }
4678
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004679 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04004680 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004681 btrfs_err(info,
4682 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
4683 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04004684 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04004685 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04004686 goto out;
4687 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004688 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 Borisov87cc7a82018-06-20 15:49:12 +03004704 ret = remove_extent_backref(trans, path, iref,
4705 refs_to_drop, is_data,
4706 &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06004707 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004708 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06004709 goto out;
4710 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004711 }
4712 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004713 if (found_extent) {
4714 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08004715 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004716 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 Mason78fae272007-03-25 11:35:08 -04004723 }
Chris Masonb9473432009-03-13 11:00:37 -04004724
Josef Bacikfcebe452014-05-13 17:30:47 -07004725 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05004726 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4727 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06004728 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004729 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06004730 goto out;
4731 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004732 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01004733
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004734 if (is_data) {
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04004735 ret = btrfs_del_csums(trans, info, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06004736 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004737 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06004738 goto out;
4739 }
Chris Mason459931e2008-12-10 09:10:46 -05004740 }
4741
Nikolay Borisove7355e52018-05-10 15:44:55 +03004742 ret = add_to_free_space_tree(trans, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004743 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004744 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07004745 goto out;
4746 }
4747
David Sterba6b279402019-03-20 12:10:15 +01004748 ret = update_block_group(trans, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06004749 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004750 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06004751 goto out;
4752 }
Chris Masona28ec192007-03-06 20:08:01 -05004753 }
Josef Bacikfcebe452014-05-13 17:30:47 -07004754 btrfs_release_path(path);
4755
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004756out:
Chris Mason5caf2a02007-04-02 11:20:42 -04004757 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05004758 return ret;
4759}
4760
4761/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04004762 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04004763 * 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 */
4767static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004768 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04004769{
4770 struct btrfs_delayed_ref_head *head;
4771 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004772 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04004773
4774 delayed_refs = &trans->transaction->delayed_refs;
4775 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08004776 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04004777 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08004778 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04004779
Josef Bacikd7df2c72014-01-23 09:21:38 -05004780 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08004781 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root))
Chris Mason1887be62009-03-13 10:11:24 -04004782 goto out;
4783
Josef Bacikbedc66172018-12-03 10:20:31 -05004784 if (cleanup_extent_op(head) != NULL)
4785 goto out;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004786
Chris Mason1887be62009-03-13 10:11:24 -04004787 /*
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 Bacikd7baffd2018-12-03 10:20:29 -05004794 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05004795 head->processing = 0;
Josef Bacikd7baffd2018-12-03 10:20:29 -05004796
Josef Bacikd7df2c72014-01-23 09:21:38 -05004797 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04004798 spin_unlock(&delayed_refs->lock);
4799
Yan, Zhengf0486c62010-05-16 10:46:25 -04004800 BUG_ON(head->extent_op);
4801 if (head->must_insert_reserved)
4802 ret = 1;
4803
Josef Bacik31890da2018-11-21 14:05:41 -05004804 btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004805 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04004806 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004807 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04004808out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05004809 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08004810
4811out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04004812 spin_unlock(&delayed_refs->lock);
4813 return 0;
4814}
4815
Yan, Zhengf0486c62010-05-16 10:46:25 -04004816void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4817 struct btrfs_root *root,
4818 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02004819 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004820{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004821 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruoed4f2552019-04-04 14:45:31 +08004822 struct btrfs_ref generic_ref = { 0 };
Josef Bacikb150a4f2013-06-19 15:00:04 -04004823 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004824 int ret;
4825
Qu Wenruoed4f2552019-04-04 14:45:31 +08004826 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, Zhengf0486c62010-05-16 10:46:25 -04004831 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07004832 int old_ref_mod, new_ref_mod;
4833
Qu Wenruo8a5040f2019-04-04 14:45:33 +08004834 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08004835 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07004836 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004837 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07004838 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004839 }
4840
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07004841 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
Filipe Manana62198722015-01-06 20:18:45 +00004842 struct btrfs_block_group_cache *cache;
4843
Yan, Zhengf0486c62010-05-16 10:46:25 -04004844 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004845 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004846 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04004847 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004848 }
4849
Omar Sandoval4da8b762017-06-06 16:45:28 -07004850 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004851 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00004852
Yan, Zhengf0486c62010-05-16 10:46:25 -04004853 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
David Sterbafdf08602019-03-20 12:12:32 +01004854 pin_down_extent(cache, buf->start, buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00004855 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04004856 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004857 }
4858
4859 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4860
4861 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08004862 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00004863 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04004864 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004865 }
4866out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04004867 if (pin)
Qu Wenruo78192442019-05-15 07:33:48 +08004868 add_pinned_bytes(fs_info, &generic_ref);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004869
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07004870 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, Zhengf0486c62010-05-16 10:46:25 -04004877}
4878
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004879/* Can return -ENOMEM */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004880int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref)
Chris Mason925baed2008-06-25 16:01:30 -04004881{
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004882 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07004883 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04004884 int ret;
4885
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04004886 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004887 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02004888
Chris Mason56bec292009-03-13 10:10:06 -04004889 /*
4890 * tree log blocks never actually go into the extent allocation
4891 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04004892 */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004893 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 Masonb9473432009-03-13 11:00:37 -04004897 /* unlocks the pinned mutex */
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004898 btrfs_pin_extent(fs_info, ref->bytenr, ref->len, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07004899 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04004900 ret = 0;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004901 } else if (ref->type == BTRFS_REF_METADATA) {
4902 ret = btrfs_add_delayed_tree_ref(trans, ref, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07004903 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004904 } else {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004905 ret = btrfs_add_delayed_data_ref(trans, ref, 0,
Omar Sandovald7eae342017-06-06 16:45:31 -07004906 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04004907 }
Omar Sandovald7eae342017-06-06 16:45:31 -07004908
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004909 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 Wenruo8a5040f2019-04-04 14:45:33 +08004914
Qu Wenruoddf30cf2019-04-04 14:45:34 +08004915 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
Qu Wenruo78192442019-05-15 07:33:48 +08004916 add_pinned_bytes(fs_info, ref);
Omar Sandovald7eae342017-06-06 16:45:31 -07004917
Chris Mason925baed2008-06-25 16:01:30 -04004918 return ret;
4919}
4920
Chris Masonfec577f2007-02-26 10:40:21 -05004921/*
Josef Bacik817d52f2009-07-13 21:29:25 -04004922 * 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 Bacik36cce922013-08-05 11:15:21 -04004931 *
4932 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
4933 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04004934 */
Josef Bacik676f1f72019-06-20 15:37:48 -04004935void btrfs_wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4936 u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -04004937{
Yan Zheng11833d62009-09-11 16:11:19 -04004938 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04004939
Josef Bacike3cb3392019-06-20 15:37:50 -04004940 caching_ctl = btrfs_get_caching_control(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004941 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04004942 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04004943
Josef Bacik676f1f72019-06-20 15:37:48 -04004944 wait_event(caching_ctl->wait, btrfs_block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08004945 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04004946
Josef Bacike3cb3392019-06-20 15:37:50 -04004947 btrfs_put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04004948}
4949
Josef Bacik676f1f72019-06-20 15:37:48 -04004950int btrfs_wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
Yan Zheng11833d62009-09-11 16:11:19 -04004951{
4952 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04004953 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04004954
Josef Bacike3cb3392019-06-20 15:37:50 -04004955 caching_ctl = btrfs_get_caching_control(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04004956 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04004957 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04004958
Josef Bacik676f1f72019-06-20 15:37:48 -04004959 wait_event(caching_ctl->wait, btrfs_block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04004960 if (cache->cached == BTRFS_CACHE_ERROR)
4961 ret = -EIO;
Josef Bacike3cb3392019-06-20 15:37:50 -04004962 btrfs_put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04004963 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04004964}
4965
4966enum btrfs_loop_type {
David Sterbaf262fa82019-06-18 20:00:08 +02004967 LOOP_CACHING_NOWAIT,
4968 LOOP_CACHING_WAIT,
4969 LOOP_ALLOC_CHUNK,
4970 LOOP_NO_EMPTY_SIZE,
Josef Bacik817d52f2009-07-13 21:29:25 -04004971};
4972
Miao Xiee570fd22014-06-19 10:42:50 +08004973static inline void
4974btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
4975 int delalloc)
4976{
4977 if (delalloc)
4978 down_read(&cache->data_rwsem);
4979}
4980
4981static inline void
4982btrfs_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
4990static struct btrfs_block_group_cache *
4991btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
4992 struct btrfs_free_cluster *cluster,
4993 int delalloc)
4994{
Sudip Mukherjee89771cc2016-02-16 13:32:47 +05304995 struct btrfs_block_group_cache *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02004996
Miao Xiee570fd22014-06-19 10:42:50 +08004997 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02004998 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 Boe321f8a2016-11-30 16:11:04 -08005016 /* We should only have one-level nested. */
5017 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02005018
5019 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08005020 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 Xiee570fd22014-06-19 10:42:50 +08005026}
5027
5028static inline void
5029btrfs_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 Bacik817d52f2009-07-13 21:29:25 -04005037/*
Qu Wenruob4bd7452018-11-02 09:39:47 +08005038 * Structure used internally for find_free_extent() function. Wraps needed
5039 * parameters.
5040 */
5041struct 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 Wenruoe72d79d2018-11-02 09:39:50 +08005061 /*
5062 * Current loop number, check find_free_extent_update_loop() for details
5063 */
Qu Wenruob4bd7452018-11-02 09:39:47 +08005064 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 Wenruod06e3bb2018-11-02 09:39:48 +08005091
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 */
5100static 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 Wenruod06e3bb2018-11-02 09:39:48 +08005105 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
5131release_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
5155refill_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 Sterba2ceeae22019-03-20 13:53:49 +01005164 ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start,
5165 ffe_ctl->num_bytes, aligned_cluster);
Qu Wenruod06e3bb2018-11-02 09:39:48 +08005166 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 Bacik676f1f72019-06-20 15:37:48 -04005185 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
Qu Wenruod06e3bb2018-11-02 09:39:48 +08005186 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 Wenruob4bd7452018-11-02 09:39:47 +08005199/*
Qu Wenruoe1a41842018-11-02 09:39:49 +08005200 * 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 */
5204static 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 Bacik676f1f72019-06-20 15:37:48 -04005252 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
5253 ffe_ctl->empty_size);
Qu Wenruoe1a41842018-11-02 09:39:49 +08005254 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 Wenruoe72d79d2018-11-02 09:39:50 +08005264 * 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 */
5268static 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 Bacikfc471cb2019-06-18 16:09:17 -04005336 ret = btrfs_chunk_alloc(trans, ffe_ctl->flags,
5337 CHUNK_ALLOC_FORCE);
Qu Wenruoe72d79d2018-11-02 09:39:50 +08005338
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 Masonfec577f2007-02-26 10:40:21 -05005375 * 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 Xiea4820392013-09-09 13:19:42 +08005377 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04005378 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08005379 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05005380 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08005381 *
5382 * If there is no suitable free space, we will record the max size of
5383 * the free space extent currently.
Qu Wenruoe72d79d2018-11-02 09:39:50 +08005384 *
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 Masonfec577f2007-02-26 10:40:21 -05005398 */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05005399static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08005400 u64 ram_bytes, u64 num_bytes, u64 empty_size,
5401 u64 hint_byte, struct btrfs_key *ins,
5402 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05005403{
Josef Bacik80eb2342008-10-29 14:49:05 -04005404 int ret = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005405 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04005406 struct btrfs_block_group_cache *block_group = NULL;
Qu Wenruob4bd7452018-11-02 09:39:47 +08005407 struct find_free_extent_ctl ffe_ctl = {0};
Josef Bacik80eb2342008-10-29 14:49:05 -04005408 struct btrfs_space_info *space_info;
Josef Bacik67377732010-09-16 16:19:09 -04005409 bool use_cluster = true;
Josef Bacika5e681d2015-10-01 14:54:10 -04005410 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05005411
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005412 WARN_ON(num_bytes < fs_info->sectorsize);
Qu Wenruob4bd7452018-11-02 09:39:47 +08005413
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 Sterba962a2982014-06-04 18:41:45 +02005427 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04005428 ins->objectid = 0;
5429 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04005430
Jeff Mahoney71ff6432016-09-06 16:00:42 -04005431 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05005432
Josef Bacik280c29082019-06-18 16:09:19 -04005433 space_info = btrfs_find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005434 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005435 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00005436 return -ENOSPC;
5437 }
Josef Bacik2552d172009-04-03 10:14:19 -04005438
Josef Bacik67377732010-09-16 16:19:09 -04005439 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04005440 * 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 Bacik67377732010-09-16 16:19:09 -04005448 */
Josef Bacik4f4db212015-09-29 11:40:47 -04005449 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 Mason239b14b2008-03-24 15:02:07 -04005460 }
5461
Qu Wenruob4bd7452018-11-02 09:39:47 +08005462 last_ptr = fetch_cluster_info(fs_info, space_info,
5463 &ffe_ctl.empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04005464 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005465 spin_lock(&last_ptr->lock);
5466 if (last_ptr->block_group)
5467 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04005468 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 Masonfa9c0d792009-04-03 09:47:43 -04005477 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04005478 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005479
Qu Wenruob4bd7452018-11-02 09:39:47 +08005480 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 Bacik817d52f2009-07-13 21:29:25 -04005486 /*
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 Bacikccf0e722009-11-10 21:23:48 -05005489 *
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 Bacik817d52f2009-07-13 21:29:25 -04005492 */
David Sterbab6919a52013-04-29 13:39:40 +00005493 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05005494 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04005495 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04005496 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 Bacikccf0e722009-11-10 21:23:48 -05005506 } else {
Qu Wenruob4bd7452018-11-02 09:39:47 +08005507 ffe_ctl.index = btrfs_bg_flags_to_raid_index(
Qu Wenruo3e72ee82018-01-30 18:20:45 +08005508 block_group->flags);
Miao Xiee570fd22014-06-19 10:42:50 +08005509 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04005510 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05005511 }
Josef Bacik2552d172009-04-03 10:14:19 -04005512 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04005513 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04005514 }
Chris Mason42e70e72008-11-07 18:17:11 -05005515 }
Josef Bacik2552d172009-04-03 10:14:19 -04005516search:
Qu Wenruob4bd7452018-11-02 09:39:47 +08005517 ffe_ctl.have_caching_bg = false;
5518 if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) ||
5519 ffe_ctl.index == 0)
Josef Bacika5e681d2015-10-01 14:54:10 -04005520 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04005521 down_read(&space_info->groups_sem);
Qu Wenruob4bd7452018-11-02 09:39:47 +08005522 list_for_each_entry(block_group,
5523 &space_info->block_groups[ffe_ctl.index], list) {
Jeff Mahoney14443932017-07-19 23:25:51 -04005524 /* If the block group is read-only, we can skip it entirely. */
5525 if (unlikely(block_group->ro))
5526 continue;
5527
Miao Xiee570fd22014-06-19 10:42:50 +08005528 btrfs_grab_block_group(block_group, delalloc);
Qu Wenruob4bd7452018-11-02 09:39:47 +08005529 ffe_ctl.search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05005530
Chris Mason83a50de2010-12-13 15:06:46 -05005531 /*
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 Sterbab6919a52013-04-29 13:39:40 +00005536 if (!block_group_bits(block_group, flags)) {
Bart Van Asschebece2e82018-06-20 10:03:31 -07005537 u64 extra = BTRFS_BLOCK_GROUP_DUP |
David Sterbac7369b32019-05-31 15:39:31 +02005538 BTRFS_BLOCK_GROUP_RAID1_MASK |
David Sterbaa07e8a42019-05-31 16:54:26 +02005539 BTRFS_BLOCK_GROUP_RAID56_MASK |
Chris Mason83a50de2010-12-13 15:06:46 -05005540 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 Sterbab6919a52013-04-29 13:39:40 +00005547 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05005548 goto loop;
Qu Wenruo2a284682019-07-16 17:00:33 +08005549
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 Mason83a50de2010-12-13 15:06:46 -05005557 }
5558
Josef Bacik2552d172009-04-03 10:14:19 -04005559have_block_group:
Josef Bacik676f1f72019-06-20 15:37:48 -04005560 ffe_ctl.cached = btrfs_block_group_cache_done(block_group);
Qu Wenruob4bd7452018-11-02 09:39:47 +08005561 if (unlikely(!ffe_ctl.cached)) {
5562 ffe_ctl.have_caching_bg = true;
Josef Bacik676f1f72019-06-20 15:37:48 -04005563 ret = btrfs_cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04005564 BUG_ON(ret < 0);
5565 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05005566 }
5567
Josef Bacik36cce922013-08-05 11:15:21 -04005568 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
5569 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005570
Josef Bacik0a243252009-09-11 16:11:20 -04005571 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05005572 * Ok we want to try and use the cluster allocator, so
5573 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04005574 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04005575 if (last_ptr && use_cluster) {
Qu Wenruod06e3bb2018-11-02 09:39:48 +08005576 struct btrfs_block_group_cache *cluster_bg = NULL;
Chris Mason44fb5512009-06-04 15:34:51 -04005577
Qu Wenruod06e3bb2018-11-02 09:39:48 +08005578 ret = find_free_extent_clustered(block_group, last_ptr,
5579 &ffe_ctl, &cluster_bg);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05005580
Qu Wenruod06e3bb2018-11-02 09:39:48 +08005581 if (ret == 0) {
5582 if (cluster_bg && cluster_bg != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08005583 btrfs_release_block_group(block_group,
5584 delalloc);
Qu Wenruod06e3bb2018-11-02 09:39:48 +08005585 block_group = cluster_bg;
Miao Xie215a63d2014-01-15 20:00:56 +08005586 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005587 goto checks;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08005588 } else if (ret == -EAGAIN) {
Josef Bacik817d52f2009-07-13 21:29:25 -04005589 goto have_block_group;
Qu Wenruod06e3bb2018-11-02 09:39:48 +08005590 } else if (ret > 0) {
5591 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005592 }
Qu Wenruod06e3bb2018-11-02 09:39:48 +08005593 /* ret == -ENOENT case falls through */
Chris Masonfa9c0d792009-04-03 09:47:43 -04005594 }
5595
Qu Wenruoe1a41842018-11-02 09:39:49 +08005596 ret = find_free_extent_unclustered(block_group, last_ptr,
5597 &ffe_ctl);
5598 if (ret == -EAGAIN)
Josef Bacik817d52f2009-07-13 21:29:25 -04005599 goto have_block_group;
Qu Wenruoe1a41842018-11-02 09:39:49 +08005600 else if (ret > 0)
Josef Bacik1cdda9b2009-10-06 10:04:28 -04005601 goto loop;
Qu Wenruoe1a41842018-11-02 09:39:49 +08005602 /* ret == 0 case falls through */
Chris Masonfa9c0d792009-04-03 09:47:43 -04005603checks:
Qu Wenruob4bd7452018-11-02 09:39:47 +08005604 ffe_ctl.search_start = round_up(ffe_ctl.found_offset,
5605 fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04005606
Josef Bacik2552d172009-04-03 10:14:19 -04005607 /* move on to the next group */
Qu Wenruob4bd7452018-11-02 09:39:47 +08005608 if (ffe_ctl.search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08005609 block_group->key.objectid + block_group->key.offset) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08005610 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
5611 num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04005612 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04005613 }
Josef Bacik80eb2342008-10-29 14:49:05 -04005614
Qu Wenruob4bd7452018-11-02 09:39:47 +08005615 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 Bacik6226cb02009-04-03 10:14:18 -04005618
Wang Xiaoguang18513092016-07-25 15:51:40 +08005619 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
5620 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005621 if (ret == -EAGAIN) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08005622 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
5623 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005624 goto loop;
5625 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01005626 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04005627
Josef Bacik2552d172009-04-03 10:14:19 -04005628 /* we are all good, lets return */
Qu Wenruob4bd7452018-11-02 09:39:47 +08005629 ins->objectid = ffe_ctl.search_start;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005630 ins->offset = num_bytes;
5631
Qu Wenruob4bd7452018-11-02 09:39:47 +08005632 trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start,
5633 num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08005634 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04005635 break;
5636loop:
Qu Wenruob4bd7452018-11-02 09:39:47 +08005637 ffe_ctl.retry_clustered = false;
5638 ffe_ctl.retry_unclustered = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08005639 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
Qu Wenruob4bd7452018-11-02 09:39:47 +08005640 ffe_ctl.index);
Miao Xiee570fd22014-06-19 10:42:50 +08005641 btrfs_release_block_group(block_group, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04005642 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04005643 }
5644 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05005645
Qu Wenruoe72d79d2018-11-02 09:39:50 +08005646 ret = find_free_extent_update_loop(fs_info, last_ptr, ins, &ffe_ctl,
5647 full_search, use_cluster);
5648 if (ret > 0)
Miao Xie60d2adb2011-09-09 17:34:35 +08005649 goto search;
5650
Josef Bacik4f4db212015-09-29 11:40:47 -04005651 if (ret == -ENOSPC) {
Qu Wenruob4bd7452018-11-02 09:39:47 +08005652 /*
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 Bacik4f4db212015-09-29 11:40:47 -04005658 spin_lock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08005659 space_info->max_extent_size = ffe_ctl.max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04005660 spin_unlock(&space_info->lock);
Qu Wenruob4bd7452018-11-02 09:39:47 +08005661 ins->offset = ffe_ctl.max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04005662 }
Chris Mason0f70abe2007-02-28 16:46:22 -05005663 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05005664}
Chris Masonec44a352008-04-28 15:29:52 -04005665
Nikolay Borisov6f47c702018-03-13 12:22:32 +02005666/*
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 Xiaoguang18513092016-07-25 15:51:40 +08005711int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04005712 u64 num_bytes, u64 min_alloc_size,
5713 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08005714 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05005715{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005716 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04005717 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00005718 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05005719 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04005720
Jeff Mahoney1b868262017-05-17 11:38:35 -04005721 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04005722again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005723 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05005724 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08005725 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01005726 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005727 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01005728 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08005729 if (!final_tried && ins->offset) {
5730 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04005731 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005732 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05005733 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08005734 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05005735 if (num_bytes == min_alloc_size)
5736 final_tried = true;
5737 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005738 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05005739 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005740
Josef Bacik280c29082019-06-18 16:09:19 -04005741 sinfo = btrfs_find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005742 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005743 "allocation failed flags %llu, wanted %llu",
5744 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01005745 if (sinfo)
Josef Bacik5da6afe2019-06-18 16:09:24 -04005746 btrfs_dump_space_info(fs_info, sinfo,
5747 num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05005748 }
Chris Mason925baed2008-06-25 16:01:30 -04005749 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005750
5751 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005752}
5753
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005754static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08005755 u64 start, u64 len,
5756 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04005757{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005758 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005759 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005760
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005761 cache = btrfs_lookup_block_group(fs_info, start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005762 if (!cache) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005763 btrfs_err(fs_info, "Unable to find block group for %llu",
5764 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005765 return -ENOSPC;
5766 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05005767
Chris Masone688b7252011-10-31 20:52:39 -04005768 if (pin)
David Sterbafdf08602019-03-20 12:12:32 +01005769 pin_down_extent(cache, start, len, 1);
Chris Masone688b7252011-10-31 20:52:39 -04005770 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005771 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005772 ret = btrfs_discard_extent(fs_info, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04005773 btrfs_add_free_space(cache, start, len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08005774 btrfs_free_reserved_bytes(cache, len, delalloc);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04005775 trace_btrfs_reserved_extent_free(fs_info, start, len);
Chris Masone688b7252011-10-31 20:52:39 -04005776 }
Dongsheng Yang31193212014-12-12 16:44:35 +08005777
Chris Masonfa9c0d792009-04-03 09:47:43 -04005778 btrfs_put_block_group(cache);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005779 return ret;
5780}
5781
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005782int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08005783 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04005784{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005785 return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04005786}
5787
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005788int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04005789 u64 start, u64 len)
5790{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005791 return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04005792}
5793
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005794static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005795 u64 parent, u64 root_objectid,
5796 u64 flags, u64 owner, u64 offset,
5797 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005798{
Nikolay Borisovef89b822018-06-20 15:48:58 +03005799 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005800 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005801 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005802 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005803 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005804 struct extent_buffer *leaf;
5805 int type;
5806 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04005807
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005808 if (parent > 0)
5809 type = BTRFS_SHARED_DATA_REF_KEY;
5810 else
5811 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04005812
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005813 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05005814
5815 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005816 if (!path)
5817 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05005818
Chris Masonb9473432009-03-13 11:00:37 -04005819 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005820 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5821 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005822 if (ret) {
5823 btrfs_free_path(path);
5824 return ret;
5825 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04005826
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005827 leaf = path->nodes[0];
5828 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05005829 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005830 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 Mason47e4bb92008-02-01 14:51:59 -05005834
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005835 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 Mason47e4bb92008-02-01 14:51:59 -05005850
5851 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05005852 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04005853
Nikolay Borisov25a356d2018-05-10 15:44:54 +03005854 ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07005855 if (ret)
5856 return ret;
5857
David Sterba6b279402019-03-20 12:10:15 +01005858 ret = update_block_group(trans, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005859 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005860 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005861 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05005862 BUG();
5863 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04005864 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005865 return ret;
5866}
5867
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005868static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03005869 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03005870 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005871{
Nikolay Borisov9dcdbe02018-05-21 12:27:20 +03005872 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005873 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005874 struct btrfs_extent_item *extent_item;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03005875 struct btrfs_key extent_key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005876 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 Borisov4e6bd4e2018-05-21 12:27:21 +03005880 struct btrfs_delayed_tree_ref *ref;
Josef Bacik3173a182013-03-07 14:22:04 -05005881 u32 size = sizeof(*extent_item) + sizeof(*iref);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03005882 u64 num_bytes;
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03005883 u64 flags = extent_op->flags_to_set;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005884 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05005885
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03005886 ref = btrfs_delayed_node_to_tree_ref(node);
5887
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03005888 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 Bacik3173a182013-03-07 14:22:04 -05005896 size += sizeof(*block_info);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03005897 num_bytes = node->num_bytes;
5898 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005899
5900 path = btrfs_alloc_path();
Josef Bacik80ee54b2018-10-11 15:54:22 -04005901 if (!path)
Mark Fashehd8926bb2011-07-13 10:38:47 -07005902 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005903
5904 path->leave_spinning = 1;
5905 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03005906 &extent_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005907 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07005908 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005909 return ret;
5910 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005911
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 Zheng5d4f98a2009-06-10 10:45:14 -04005919
Josef Bacik3173a182013-03-07 14:22:04 -05005920 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 Borisov21ebfbe2018-05-21 12:27:22 +03005924 btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03005925 btrfs_set_tree_block_level(leaf, block_info, ref->level);
Josef Bacik3173a182013-03-07 14:22:04 -05005926 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5927 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005928
Nikolay Borisovd4b20732018-05-21 12:27:23 +03005929 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005930 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5931 btrfs_set_extent_inline_ref_type(leaf, iref,
5932 BTRFS_SHARED_BLOCK_REF_KEY);
Nikolay Borisovd4b20732018-05-21 12:27:23 +03005933 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005934 } else {
5935 btrfs_set_extent_inline_ref_type(leaf, iref,
5936 BTRFS_TREE_BLOCK_REF_KEY);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03005937 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005938 }
5939
5940 btrfs_mark_buffer_dirty(leaf);
5941 btrfs_free_path(path);
5942
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03005943 ret = remove_from_free_space_tree(trans, extent_key.objectid,
5944 num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07005945 if (ret)
5946 return ret;
5947
David Sterba6b279402019-03-20 12:10:15 +01005948 ret = update_block_group(trans, extent_key.objectid,
Jeff Mahoney6202df62016-06-22 18:54:22 -04005949 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005950 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005951 btrfs_err(fs_info, "update block group failed for %llu %llu",
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03005952 extent_key.objectid, extent_key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005953 BUG();
5954 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04005955
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03005956 trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005957 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005958 return ret;
5959}
5960
5961int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04005962 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08005963 u64 offset, u64 ram_bytes,
5964 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005965{
Qu Wenruo76675592019-04-04 14:45:32 +08005966 struct btrfs_ref generic_ref = { 0 };
Chris Masone6dcd2d2008-07-17 12:53:50 -04005967 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04005968
Josef Bacik84f7d8e2017-09-29 15:43:49 -04005969 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04005970
Qu Wenruo76675592019-04-04 14:45:32 +08005971 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 Wenruo8a5040f2019-04-04 14:45:33 +08005974 btrfs_ref_tree_mod(root->fs_info, &generic_ref);
Qu Wenruo76675592019-04-04 14:45:32 +08005975 ret = btrfs_add_delayed_data_ref(trans, &generic_ref,
5976 ram_bytes, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005977 return ret;
5978}
Chris Masone02119d2008-09-05 16:13:11 -04005979
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 Zheng5d4f98a2009-06-10 10:45:14 -04005985int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005986 u64 root_objectid, u64 owner, u64 offset,
5987 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04005988{
Nikolay Borisov61da2ab2018-06-20 15:49:13 +03005989 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04005990 int ret;
5991 struct btrfs_block_group_cache *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08005992 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04005993
5994 /*
5995 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04005996 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04005997 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005998 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005999 ret = __exclude_logged_extent(fs_info, ins->objectid,
6000 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006001 if (ret)
6002 return ret;
6003 }
Chris Masone02119d2008-09-05 16:13:11 -04006004
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006005 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006006 if (!block_group)
6007 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04006008
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08006009 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 Borisovef89b822018-06-20 15:48:58 +03006017 ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
6018 offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006019 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006020 return ret;
6021}
6022
Eric Sandeen48a3b632013-04-25 20:41:01 +00006023static struct extent_buffer *
6024btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03006025 u64 bytenr, int level, u64 owner)
Chris Mason65b51a02008-08-01 15:11:20 -04006026{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006027 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04006028 struct extent_buffer *buf;
6029
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006030 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07006031 if (IS_ERR(buf))
6032 return buf;
6033
Qu Wenruob72c3ab2018-08-21 09:53:47 +08006034 /*
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 Mason85d4e462011-07-26 16:11:19 -04006047 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006048 btrfs_tree_lock(buf);
David Sterba6a884d7d2019-03-20 14:30:02 +01006049 btrfs_clean_tree_block(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006050 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006051
David Sterba8bead252018-04-04 02:03:48 +02006052 btrfs_set_lock_blocking_write(buf);
David Sterba4db8c522015-12-03 13:06:46 +01006053 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006054
Nikolay Borisovbc877d22018-06-18 14:13:19 +03006055 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 Borisovde37aa52018-10-30 16:43:24 +02006061 write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03006062 write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
Chris Masond0c803c2008-09-11 16:17:57 -04006063 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01006064 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006065 /*
6066 * we allow two log transactions at a time, use different
Andrea Gelmini52042d82018-11-28 12:05:13 +01006067 * EXTENT bit to differentiate dirty pages.
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006068 */
Filipe Manana656f30d2014-09-26 12:25:56 +01006069 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006070 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 Sterba3744dbe2016-04-26 23:54:39 +02006074 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04006075 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01006076 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04006077 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6078 buf->start + buf->len - 1, GFP_NOFS);
6079 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04006080 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006081 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006082 return buf;
6083}
6084
Chris Masonfec577f2007-02-26 10:40:21 -05006085/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006086 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08006087 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05006088 */
David Sterba4d75f8a2014-06-15 01:54:12 +02006089struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08006090 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 Masonfec577f2007-02-26 10:40:21 -05006095{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006096 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04006097 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006098 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006099 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08006100 struct btrfs_delayed_extent_op *extent_op;
Qu Wenruoed4f2552019-04-04 14:45:31 +08006101 struct btrfs_ref generic_ref = { 0 };
Yan, Zhengf0486c62010-05-16 10:46:25 -04006102 u64 flags = 0;
6103 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006104 u32 blocksize = fs_info->nodesize;
6105 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006106
David Sterba05653ef2016-07-15 15:23:37 +02006107#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006108 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04006109 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
Nikolay Borisovbc877d22018-06-18 14:13:19 +03006110 level, root_objectid);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04006111 if (!IS_ERR(buf))
6112 root->alloc_bytenr += blocksize;
6113 return buf;
6114 }
David Sterba05653ef2016-07-15 15:23:37 +02006115#endif
David Sterbafccb84c2014-09-29 23:53:21 +02006116
Josef Bacik67f9c222019-06-19 13:47:23 -04006117 block_rsv = btrfs_use_block_rsv(trans, root, blocksize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006118 if (IS_ERR(block_rsv))
6119 return ERR_CAST(block_rsv);
6120
Wang Xiaoguang18513092016-07-25 15:51:40 +08006121 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08006122 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08006123 if (ret)
6124 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05006125
Nikolay Borisovbc877d22018-06-18 14:13:19 +03006126 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level,
6127 root_objectid);
Omar Sandoval67b78592015-02-24 02:47:04 -08006128 if (IS_ERR(buf)) {
6129 ret = PTR_ERR(buf);
6130 goto out_free_reserved;
6131 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006132
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 Xie78a61842012-11-21 02:21:28 +00006141 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08006142 if (!extent_op) {
6143 ret = -ENOMEM;
6144 goto out_free_buf;
6145 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04006146 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 Sterba35b3ad52015-11-30 16:51:29 +01006151 extent_op->update_key = skinny_metadata ? false : true;
6152 extent_op->update_flags = true;
6153 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04006154 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006155
Qu Wenruoed4f2552019-04-04 14:45:31 +08006156 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 Wenruo8a5040f2019-04-04 14:45:33 +08006160 btrfs_ref_tree_mod(fs_info, &generic_ref);
Qu Wenruoed4f2552019-04-04 14:45:31 +08006161 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref,
Omar Sandoval7be07912017-06-06 16:45:30 -07006162 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08006163 if (ret)
6164 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006165 }
Chris Masonfec577f2007-02-26 10:40:21 -05006166 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08006167
6168out_free_delayed:
6169 btrfs_free_delayed_extent_op(extent_op);
6170out_free_buf:
6171 free_extent_buffer(buf);
6172out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006173 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08006174out_unuse:
Josef Bacik67f9c222019-06-19 13:47:23 -04006175 btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08006176 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006177}
Chris Masona28ec192007-03-06 20:08:01 -05006178
Yan Zheng2c47e6052009-06-27 21:07:35 -04006179struct walk_control {
6180 u64 refs[BTRFS_MAX_LEVEL];
6181 u64 flags[BTRFS_MAX_LEVEL];
6182 struct btrfs_key update_progress;
Josef Bacikaea6f022019-02-06 15:46:15 -05006183 struct btrfs_key drop_progress;
6184 int drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006185 int stage;
6186 int level;
6187 int shared_level;
6188 int update_ref;
6189 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006190 int reada_slot;
6191 int reada_count;
Josef Bacik78c52d92019-02-06 15:46:14 -05006192 int restarted;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006193};
6194
6195#define DROP_REFERENCE 1
6196#define UPDATE_BACKREF 2
6197
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006198static 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 Mahoney0b246af2016-06-22 18:54:23 -04006203 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006204 u64 bytenr;
6205 u64 generation;
6206 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006207 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006208 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006209 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 Mahoney0b246af2016-06-22 18:54:23 -04006221 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006222 }
6223
6224 eb = path->nodes[wc->level];
6225 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006226
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, Zheng94fcca92009-10-09 09:25:16 -04006242 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006243 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05006244 wc->level - 1, 1, &refs,
6245 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006246 /* We don't care about errors in readahead. */
6247 if (ret < 0)
6248 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006249 BUG_ON(refs == 0);
6250
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006251 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006252 if (refs == 1)
6253 goto reada;
6254
Yan, Zheng94fcca92009-10-09 09:25:16 -04006255 if (wc->level == 1 &&
6256 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6257 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006258 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, Zheng94fcca92009-10-09 09:25:16 -04006266 } else {
6267 if (wc->level == 1 &&
6268 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6269 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006270 }
6271reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006272 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006273 nread++;
6274 }
6275 wc->reada_slot = slot;
6276}
6277
Chris Mason9aca1d52007-03-13 11:09:37 -04006278/*
Liu Bo2c016dc2012-12-26 15:32:17 +08006279 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04006280 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04006281 * 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 Zhengf82d02d2008-10-29 14:49:05 -04006285 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006286static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6287 struct btrfs_root *root,
6288 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006289 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04006290{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006291 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006292 int level = wc->level;
6293 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04006294 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, Zheng94fcca92009-10-09 09:25:16 -04006305 if (lookup_info &&
6306 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6307 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006308 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006309 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05006310 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006311 &wc->refs[level],
6312 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006313 BUG_ON(ret == -ENOMEM);
6314 if (ret)
6315 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006316 BUG_ON(wc->refs[level] == 0);
6317 }
6318
Yan Zheng2c47e6052009-06-27 21:07:35 -04006319 if (wc->stage == DROP_REFERENCE) {
6320 if (wc->refs[level] > 1)
6321 return 1;
6322
6323 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04006324 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006325 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 Bacike339a6b2014-07-02 10:54:25 -07006333 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006334 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07006335 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006336 BUG_ON(ret); /* -ENOMEM */
David Sterbaf5c8daa2019-03-20 11:43:36 +01006337 ret = btrfs_set_disk_extent_flags(trans, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04006338 eb->len, flag,
6339 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006340 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006341 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 Masonbd681512011-07-16 15:23:14 -04006349 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006350 path->locks[level] = 0;
6351 }
6352 return 0;
6353}
6354
6355/*
Josef Bacik78c52d92019-02-06 15:46:14 -05006356 * 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 */
6359static 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 Bo2c016dc2012-12-26 15:32:17 +08006383 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006384 *
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 */
6395static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6396 struct btrfs_root *root,
6397 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006398 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006399{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006400 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006401 u64 bytenr;
6402 u64 generation;
6403 u64 parent;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006404 struct btrfs_key key;
Qu Wenruo581c1762018-03-29 09:08:11 +08006405 struct btrfs_key first_key;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08006406 struct btrfs_ref ref = { 0 };
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006407 struct extent_buffer *next;
6408 int level = wc->level;
6409 int reada = 0;
6410 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07006411 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006412
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, Zheng94fcca92009-10-09 09:25:16 -04006421 generation <= root->root_key.offset) {
6422 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006423 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006424 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006425
6426 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08006427 btrfs_node_key_to_cpu(path->nodes[level], &first_key,
6428 path->slots[level]);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006429
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006430 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006431 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006432 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07006433 if (IS_ERR(next))
6434 return PTR_ERR(next);
6435
Josef Bacikb2aaaa32013-07-05 17:05:38 -04006436 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
6437 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006438 reada = 1;
6439 }
6440 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02006441 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006442
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006443 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04006444 &wc->refs[level - 1],
6445 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02006446 if (ret < 0)
6447 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006448
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006449 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006450 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02006451 ret = -EIO;
6452 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006453 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04006454 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006455
Yan, Zheng94fcca92009-10-09 09:25:16 -04006456 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006457 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07006458 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006459 if (level == 1 &&
6460 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6461 goto skip;
6462
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006463 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, Zheng94fcca92009-10-09 09:25:16 -04006476 } else {
6477 if (level == 1 &&
6478 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6479 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006480 }
6481
Chris Masonb9fab912012-05-06 07:23:47 -04006482 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006483 btrfs_tree_unlock(next);
6484 free_extent_buffer(next);
6485 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006486 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006487 }
6488
6489 if (!next) {
6490 if (reada && level == 1)
6491 reada_walk_down(trans, root, wc, path);
Qu Wenruo581c1762018-03-29 09:08:11 +08006492 next = read_tree_block(fs_info, bytenr, generation, level - 1,
6493 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08006494 if (IS_ERR(next)) {
6495 return PTR_ERR(next);
6496 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04006497 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00006498 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04006499 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006500 btrfs_tree_lock(next);
David Sterba8bead252018-04-04 02:03:48 +02006501 btrfs_set_lock_blocking_write(next);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006502 }
6503
6504 level--;
Josef Bacik48672682016-09-23 13:23:28 +02006505 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, Zheng1c4850e2009-09-21 15:55:59 -04006511 path->nodes[level] = next;
6512 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006513 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006514 wc->level = level;
6515 if (wc->level == 1)
6516 wc->reada_slot = 0;
6517 return 0;
6518skip:
6519 wc->refs[level - 1] = 0;
6520 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006521 if (wc->stage == DROP_REFERENCE) {
6522 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6523 parent = path->nodes[level]->start;
6524 } else {
Josef Bacik48672682016-09-23 13:23:28 +02006525 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04006526 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02006527 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, Zheng94fcca92009-10-09 09:25:16 -04006534 parent = 0;
6535 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006536
Qu Wenruo2cd86d32018-09-27 14:42:33 +08006537 /*
Josef Bacik78c52d92019-02-06 15:46:14 -05006538 * 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 Wenruo2cd86d32018-09-27 14:42:33 +08006555 * 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 Fengqideb40622018-07-18 14:45:38 +08006561 ret = btrfs_qgroup_trace_subtree(trans, next,
Qu Wenruo33d1f052016-10-18 09:31:28 +08006562 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07006563 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006564 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006565 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
6566 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07006567 }
6568 }
Josef Bacikaea6f022019-02-06 15:46:15 -05006569
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 Wenruoffd4bb22019-04-04 14:45:36 +08006579 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 Bacik48672682016-09-23 13:23:28 +02006583 if (ret)
6584 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006585 }
Josef Bacik78c52d92019-02-06 15:46:14 -05006586no_delete:
Josef Bacik48672682016-09-23 13:23:28 +02006587 *lookup_info = 1;
6588 ret = 1;
6589
6590out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006591 btrfs_tree_unlock(next);
6592 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02006593
6594 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006595}
6596
6597/*
Liu Bo2c016dc2012-12-26 15:32:17 +08006598 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04006599 *
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 */
6609static 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 Mahoney0b246af2016-06-22 18:54:23 -04006614 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006615 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006616 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 Zheng2c47e6052009-06-27 21:07:35 -04006625 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 Sterba8bead252018-04-04 02:03:48 +02006641 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006642 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006643
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006644 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05006645 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006646 &wc->refs[level],
6647 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006648 if (ret < 0) {
6649 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00006650 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006651 return ret;
6652 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006653 BUG_ON(wc->refs[level] == 0);
6654 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04006655 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00006656 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006657 return 1;
6658 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006659 }
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 Bacike339a6b2014-07-02 10:54:25 -07006668 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006669 else
Josef Bacike339a6b2014-07-02 10:54:25 -07006670 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006671 BUG_ON(ret); /* -ENOMEM */
Qu Wenruoc4140cb2019-04-04 14:45:37 +08006672 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 Mahoney5d163e02016-09-20 10:05:00 -04006677 ret);
Qu Wenruoc4140cb2019-04-04 14:45:37 +08006678 }
Mark Fasheh11526512014-07-17 12:39:01 -07006679 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006680 }
David Sterba6a884d7d2019-03-20 14:30:02 +01006681 /* make block locked assertion in btrfs_clean_tree_block happy */
Yan Zheng2c47e6052009-06-27 21:07:35 -04006682 if (!path->locks[level] &&
6683 btrfs_header_generation(eb) == trans->transid) {
6684 btrfs_tree_lock(eb);
David Sterba8bead252018-04-04 02:03:48 +02006685 btrfs_set_lock_blocking_write(eb);
Chris Masonbd681512011-07-16 15:23:14 -04006686 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006687 }
David Sterba6a884d7d2019-03-20 14:30:02 +01006688 btrfs_clean_tree_block(eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006689 }
6690
6691 if (eb == root->node) {
6692 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6693 parent = eb->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08006694 else if (root->root_key.objectid != btrfs_header_owner(eb))
6695 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006696 } else {
6697 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6698 parent = path->nodes[level + 1]->start;
Qu Wenruo65c6e822018-08-21 09:42:03 +08006699 else if (root->root_key.objectid !=
6700 btrfs_header_owner(path->nodes[level + 1]))
6701 goto owner_mismatch;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006702 }
6703
Jan Schmidt5581a512012-05-16 17:04:52 +02006704 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006705out:
6706 wc->refs[level] = 0;
6707 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006708 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08006709
6710owner_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 Zheng2c47e6052009-06-27 21:07:35 -04006714}
6715
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006716static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6717 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006718 struct btrfs_path *path,
6719 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006720{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006721 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04006722 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006723 int ret;
6724
Yan Zheng2c47e6052009-06-27 21:07:35 -04006725 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04006726 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006727 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04006728 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006729
Yan Zheng2c47e6052009-06-27 21:07:35 -04006730 if (level == 0)
6731 break;
6732
Yan, Zheng7a7965f2010-02-01 02:41:17 +00006733 if (path->slots[level] >=
6734 btrfs_header_nritems(path->nodes[level]))
6735 break;
6736
Yan, Zheng94fcca92009-10-09 09:25:16 -04006737 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006738 if (ret > 0) {
6739 path->slots[level]++;
6740 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00006741 } else if (ret < 0)
6742 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006743 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006744 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04006745 return 0;
6746}
6747
Chris Masond3977122009-01-05 21:25:51 -05006748static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006749 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04006750 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006751 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05006752{
Yan Zheng2c47e6052009-06-27 21:07:35 -04006753 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05006754 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04006755
Yan Zheng2c47e6052009-06-27 21:07:35 -04006756 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 Mason20524f02007-03-10 06:35:47 -05006762 return 0;
6763 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006764 ret = walk_up_proc(trans, root, path, wc);
6765 if (ret > 0)
6766 return 0;
Qu Wenruo65c6e822018-08-21 09:42:03 +08006767 if (ret < 0)
6768 return ret;
Chris Masonbd56b302009-02-04 09:27:02 -05006769
Yan Zheng2c47e6052009-06-27 21:07:35 -04006770 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04006771 btrfs_tree_unlock_rw(path->nodes[level],
6772 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006773 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04006774 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006775 free_extent_buffer(path->nodes[level]);
6776 path->nodes[level] = NULL;
6777 level++;
Chris Mason20524f02007-03-10 06:35:47 -05006778 }
6779 }
6780 return 1;
6781}
6782
Chris Mason9aca1d52007-03-13 11:09:37 -04006783/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04006784 * 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 Sterba9d1a2a32013-03-12 15:13:28 +00006793 *
6794 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04006795 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04006796int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006797 struct btrfs_block_rsv *block_rsv, int update_ref,
6798 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05006799{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006800 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04006801 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006802 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006803 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04006804 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006805 struct walk_control *wc;
6806 struct btrfs_key key;
6807 int err = 0;
6808 int ret;
6809 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04006810 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05006811
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006812 btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07006813
Chris Mason5caf2a02007-04-02 11:20:42 -04006814 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006815 if (!path) {
6816 err = -ENOMEM;
6817 goto out;
6818 }
Chris Mason20524f02007-03-10 06:35:47 -05006819
Yan Zheng2c47e6052009-06-27 21:07:35 -04006820 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07006821 if (!wc) {
6822 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00006823 err = -ENOMEM;
6824 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07006825 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006826
Yan, Zhenga22285a2010-05-16 10:48:46 -04006827 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006828 if (IS_ERR(trans)) {
6829 err = PTR_ERR(trans);
6830 goto out_free;
6831 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00006832
Josef Bacik0568e822018-11-30 11:52:14 -05006833 err = btrfs_run_delayed_items(trans);
6834 if (err)
6835 goto out_end_trans;
6836
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006837 if (block_rsv)
6838 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006839
Josef Bacik83354f02018-11-30 11:52:13 -05006840 /*
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 Mason9f3a7422007-08-07 15:52:19 -04006849 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006850 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006851 path->nodes[level] = btrfs_lock_root_node(root);
David Sterba8bead252018-04-04 02:03:48 +02006852 btrfs_set_lock_blocking_write(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04006853 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04006854 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006855 memset(&wc->update_progress, 0,
6856 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04006857 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04006858 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006859 memcpy(&wc->update_progress, &key,
6860 sizeof(wc->update_progress));
6861
Chris Mason6702ed42007-08-07 16:15:09 -04006862 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006863 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04006864 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006865 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6866 path->lowest_level = 0;
6867 if (ret < 0) {
6868 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006869 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04006870 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006871 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006872
Chris Mason7d9eb122008-07-08 14:19:17 -04006873 /*
6874 * unlock our path, this is safe because only this
6875 * function is allowed to delete this snapshot
6876 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006877 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04006878
Yan Zheng2c47e6052009-06-27 21:07:35 -04006879 level = btrfs_header_level(root->node);
6880 while (1) {
6881 btrfs_tree_lock(path->nodes[level]);
David Sterba8bead252018-04-04 02:03:48 +02006882 btrfs_set_lock_blocking_write(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04006883 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006884
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006885 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04006886 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05006887 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04006888 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006889 if (ret < 0) {
6890 err = ret;
6891 goto out_end_trans;
6892 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006893 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 Bacikfec386a2013-07-15 12:41:42 -04006899 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006900 WARN_ON(wc->refs[level] != 1);
6901 level--;
6902 }
6903 }
6904
Josef Bacik78c52d92019-02-06 15:46:14 -05006905 wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006906 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 Mahoney0b246af2016-06-22 18:54:23 -04006911 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04006912
6913 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00006914
Yan Zheng2c47e6052009-06-27 21:07:35 -04006915 ret = walk_down_tree(trans, root, path, wc);
6916 if (ret < 0) {
6917 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04006918 break;
6919 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006920
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 Bacikaea6f022019-02-06 15:46:15 -05006933 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 Zheng2c47e6052009-06-27 21:07:35 -04006937 }
Josef Bacikaea6f022019-02-06 15:46:15 -05006938 btrfs_cpu_key_to_disk(&root_item->drop_progress,
6939 &wc->drop_progress);
6940 root_item->drop_level = wc->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006941
6942 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006943 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006944 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04006945 ret = btrfs_update_root(trans, tree_root,
6946 &root->root_key,
6947 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006948 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006949 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006950 err = ret;
6951 goto out_end_trans;
6952 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006953
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006954 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006955 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006956 btrfs_debug(fs_info,
6957 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04006958 err = -EAGAIN;
6959 goto out_free;
6960 }
6961
Yan, Zhenga22285a2010-05-16 10:48:46 -04006962 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006963 if (IS_ERR(trans)) {
6964 err = PTR_ERR(trans);
6965 goto out_free;
6966 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04006967 if (block_rsv)
6968 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04006969 }
Chris Mason20524f02007-03-10 06:35:47 -05006970 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006971 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006972 if (err)
6973 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006974
Lu Fengqiab9ce7d2018-08-01 11:32:27 +08006975 ret = btrfs_del_root(trans, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006976 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006977 btrfs_abort_transaction(trans, ret);
Jeff Mahoneye19182c2017-12-04 13:11:45 -05006978 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006979 goto out_end_trans;
6980 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04006981
Yan, Zheng76dda932009-09-21 16:00:26 -04006982 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00006983 ret = btrfs_find_root(tree_root, &root->root_key, path,
6984 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006985 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006986 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006987 err = ret;
6988 goto out_end_trans;
6989 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05006990 /* 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, Zheng76dda932009-09-21 16:00:26 -04006997 }
6998 }
6999
Miao Xie27cdeb72014-04-02 19:51:05 +08007000 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04007001 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007002 } else {
7003 free_extent_buffer(root->node);
7004 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00007005 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007006 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04007007 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007008out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007009 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007010out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007011 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007012 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007013out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04007014 /*
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 Meyer897ca812017-10-07 16:02:21 +02007021 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04007022 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08007023 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007024 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007025 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007026}
Chris Mason9078a3e2007-04-26 16:46:15 -04007027
Yan Zheng2c47e6052009-06-27 21:07:35 -04007028/*
7029 * drop subtree rooted at tree block 'node'.
7030 *
7031 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007032 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007033 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007034int 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 Mahoney0b246af2016-06-22 18:54:23 -04007039 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007040 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007041 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007042 int level;
7043 int parent_level;
7044 int ret = 0;
7045 int wret;
7046
Yan Zheng2c47e6052009-06-27 21:07:35 -04007047 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7048
Yan Zhengf82d02d2008-10-29 14:49:05 -04007049 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007050 if (!path)
7051 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007052
Yan Zheng2c47e6052009-06-27 21:07:35 -04007053 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007054 if (!wc) {
7055 btrfs_free_path(path);
7056 return -ENOMEM;
7057 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007058
Chris Masonb9447ef82009-03-09 11:45:38 -04007059 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007060 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 Masonb9447ef82009-03-09 11:45:38 -04007065 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007066 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007067 path->nodes[level] = node;
7068 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007069 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007070
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 Mahoney0b246af2016-06-22 18:54:23 -04007078 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007079
7080 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007081 wret = walk_down_tree(trans, root, path, wc);
7082 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007083 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007084 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007085 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007086
Yan Zheng2c47e6052009-06-27 21:07:35 -04007087 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007088 if (wret < 0)
7089 ret = wret;
7090 if (wret != 0)
7091 break;
7092 }
7093
Yan Zheng2c47e6052009-06-27 21:07:35 -04007094 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007095 btrfs_free_path(path);
7096 return ret;
7097}
7098
Jeff Mahoney6202df62016-06-22 18:54:22 -04007099static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04007100{
7101 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007102 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007103
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007104 /*
7105 * if restripe for this chunk_type is on pick target profile and
7106 * return, otherwise do the usual balance
7107 */
Jeff Mahoney6202df62016-06-22 18:54:22 -04007108 stripped = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007109 if (stripped)
7110 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007111
Jeff Mahoney6202df62016-06-22 18:54:22 -04007112 num_devices = fs_info->fs_devices->rw_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05007113
David Sterbaa07e8a42019-05-31 16:54:26 +02007114 stripped = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID56_MASK |
David Sterbac7369b32019-05-31 15:39:31 +02007115 BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007116
Chris Masonec44a352008-04-28 15:29:52 -04007117 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 Sterbac7369b32019-05-31 15:39:31 +02007126 if (flags & (BTRFS_BLOCK_GROUP_RAID1_MASK |
Chris Masonec44a352008-04-28 15:29:52 -04007127 BTRFS_BLOCK_GROUP_RAID10))
7128 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007129 } else {
7130 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007131 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 Dryomove3176ca2012-03-27 17:09:16 +03007141 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007142 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007143
Chris Masonec44a352008-04-28 15:29:52 -04007144 return flags;
7145}
7146
Qu Wenruoe9138142019-07-19 14:51:43 +08007147/*
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 */
Zhaolei868f4012015-08-05 16:43:27 +08007160static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007161{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007162 struct btrfs_space_info *sinfo = cache->space_info;
7163 u64 num_bytes;
Qu Wenruo3ece54e2019-01-30 13:07:51 +08007164 u64 sinfo_used;
Miao Xie199c36e2011-07-15 10:34:36 +00007165 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007166 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007167
Miao Xie199c36e2011-07-15 10:34:36 +00007168 /*
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 Leeee221842015-12-15 01:42:10 +09007176 min_allocable_bytes = SZ_1M;
Miao Xie199c36e2011-07-15 10:34:36 +00007177 else
7178 min_allocable_bytes = 0;
7179
Yan, Zhengf0486c62010-05-16 10:46:25 -04007180 spin_lock(&sinfo->lock);
7181 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007182
7183 if (cache->ro) {
Zhaolei868f4012015-08-05 16:43:27 +08007184 cache->ro++;
WuBo61cfea92011-07-26 03:30:11 +00007185 ret = 0;
7186 goto out;
7187 }
7188
Yan, Zhengf0486c62010-05-16 10:46:25 -04007189 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7190 cache->bytes_super - btrfs_block_group_used(&cache->item);
Qu Wenruo3ece54e2019-01-30 13:07:51 +08007191 sinfo_used = btrfs_space_info_used(sinfo, true);
Chris Mason7d9eb122008-07-08 14:19:17 -04007192
Qu Wenruoe9138142019-07-19 14:51:43 +08007193 /*
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 Wenruo3ece54e2019-01-30 13:07:51 +08007199 if (sinfo_used + num_bytes + min_allocable_bytes <=
7200 sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007201 sinfo->bytes_readonly += num_bytes;
Zhaolei868f4012015-08-05 16:43:27 +08007202 cache->ro++;
Josef Bacik633c0aa2014-10-31 09:49:34 -04007203 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007204 ret = 0;
7205 }
WuBo61cfea92011-07-26 03:30:11 +00007206out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007207 spin_unlock(&cache->lock);
7208 spin_unlock(&sinfo->lock);
Qu Wenruo3ece54e2019-01-30 13:07:51 +08007209 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 Bacik5da6afe2019-06-18 16:09:24 -04007216 btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0);
Qu Wenruo3ece54e2019-01-30 13:07:51 +08007217 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007218 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007219}
7220
Nikolay Borisovc83488a2018-06-20 15:49:14 +03007221int btrfs_inc_block_group_ro(struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007222
7223{
Nikolay Borisovc83488a2018-06-20 15:49:14 +03007224 struct btrfs_fs_info *fs_info = cache->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007225 struct btrfs_trans_handle *trans;
7226 u64 alloc_flags;
7227 int ret;
7228
Chris Mason1bbc6212015-04-06 12:46:08 -07007229again:
Jeff Mahoney5e00f192017-02-15 16:28:29 -05007230 trans = btrfs_join_transaction(fs_info->extent_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007231 if (IS_ERR(trans))
7232 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007233
Chris Mason1bbc6212015-04-06 12:46:08 -07007234 /*
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 Mahoney0b246af2016-06-22 18:54:23 -04007239 mutex_lock(&fs_info->ro_block_group_mutex);
Josef Bacik3204d332015-09-24 10:46:10 -04007240 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
Chris Mason1bbc6212015-04-06 12:46:08 -07007241 u64 transid = trans->transid;
7242
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007243 mutex_unlock(&fs_info->ro_block_group_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007244 btrfs_end_transaction(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07007245
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007246 ret = btrfs_wait_for_commit(fs_info, transid);
Chris Mason1bbc6212015-04-06 12:46:08 -07007247 if (ret)
7248 return ret;
7249 goto again;
7250 }
7251
Chris Mason153c35b2015-05-19 18:54:41 -07007252 /*
7253 * if we are changing raid levels, try to allocate a corresponding
7254 * block group with the new raid level.
7255 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007256 alloc_flags = update_block_group_flags(fs_info, cache->flags);
Chris Mason153c35b2015-05-19 18:54:41 -07007257 if (alloc_flags != cache->flags) {
Josef Bacikfc471cb2019-06-18 16:09:17 -04007258 ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
Chris Mason153c35b2015-05-19 18:54:41 -07007259 /*
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 Mason1bbc6212015-04-06 12:46:08 -07007269
Zhaolei868f4012015-08-05 16:43:27 +08007270 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007271 if (!ret)
7272 goto out;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007273 alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags);
Josef Bacikfc471cb2019-06-18 16:09:17 -04007274 ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007275 if (ret < 0)
7276 goto out;
Zhaolei868f4012015-08-05 16:43:27 +08007277 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007278out:
Shaohua Li2f081082015-01-09 10:40:15 -08007279 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007280 alloc_flags = update_block_group_flags(fs_info, cache->flags);
David Sterba34441362016-10-04 19:34:27 +02007281 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03007282 check_system_chunk(trans, alloc_flags);
David Sterba34441362016-10-04 19:34:27 +02007283 mutex_unlock(&fs_info->chunk_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08007284 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007285 mutex_unlock(&fs_info->ro_block_group_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08007286
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007287 btrfs_end_transaction(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007288 return ret;
7289}
7290
Nikolay Borisov43a7e992018-06-20 15:49:15 +03007291int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type)
Chris Masonc87f08c2011-02-16 13:57:04 -05007292{
Nikolay Borisov43a7e992018-06-20 15:49:15 +03007293 u64 alloc_flags = get_alloc_profile(trans->fs_info, type);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007294
Josef Bacikfc471cb2019-06-18 16:09:17 -04007295 return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007296}
7297
Miao Xie6d07bce2011-01-05 10:07:31 +00007298/*
7299 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04007300 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00007301 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04007302u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00007303{
7304 struct btrfs_block_group_cache *block_group;
7305 u64 free_bytes = 0;
7306 int factor;
7307
Nicholas D Steeves01327612016-05-19 21:18:45 -04007308 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04007309 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 Xie6d07bce2011-01-05 10:07:31 +00007314 spin_lock(&block_group->lock);
7315
7316 if (!block_group->ro) {
7317 spin_unlock(&block_group->lock);
7318 continue;
7319 }
7320
David Sterba46df06b2018-07-13 20:46:30 +02007321 factor = btrfs_bg_type_to_factor(block_group->flags);
Miao Xie6d07bce2011-01-05 10:07:31 +00007322 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 Xie6d07bce2011-01-05 10:07:31 +00007328 spin_unlock(&sinfo->lock);
7329
7330 return free_bytes;
7331}
7332
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007333void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007334{
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);
Zhaolei868f4012015-08-05 16:43:27 +08007342 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, Zhengf0486c62010-05-16 10:46:25 -04007349 spin_unlock(&cache->lock);
7350 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007351}
7352
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007353static int find_first_block_group(struct btrfs_fs_info *fs_info,
7354 struct btrfs_path *path,
7355 struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04007356{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007357 struct btrfs_root *root = fs_info->extent_root;
Chris Mason925baed2008-06-25 16:01:30 -04007358 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007359 struct btrfs_key found_key;
7360 struct extent_buffer *leaf;
Qu Wenruo514c7dc2018-08-01 10:37:16 +08007361 struct btrfs_block_group_item bg;
7362 u64 flags;
Chris Mason0b86a832008-03-24 15:01:56 -04007363 int slot;
7364
7365 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7366 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04007367 goto out;
7368
Chris Masond3977122009-01-05 21:25:51 -05007369 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007370 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 Mason925baed2008-06-25 16:01:30 -04007377 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04007378 break;
7379 }
7380 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7381
7382 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04007383 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
Liu Bo6fb37b72016-06-22 18:31:27 -07007384 struct extent_map_tree *em_tree;
7385 struct extent_map *em;
7386
David Sterbac8bf1b62019-05-17 11:43:17 +02007387 em_tree = &root->fs_info->mapping_tree;
Liu Bo6fb37b72016-06-22 18:31:27 -07007388 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 Mahoney0b246af2016-06-22 18:54:23 -04007393 btrfs_err(fs_info,
Liu Bo6fb37b72016-06-22 18:31:27 -07007394 "logical %llu len %llu found bg but no related chunk",
7395 found_key.objectid, found_key.offset);
7396 ret = -ENOENT;
Qu Wenruo514c7dc2018-08-01 10:37:16 +08007397 } 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 Bo6fb37b72016-06-22 18:31:27 -07007404 } else {
Qu Wenruo514c7dc2018-08-01 10:37:16 +08007405 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 Bo6fb37b72016-06-22 18:31:27 -07007423 }
Josef Bacik187ee582016-08-18 15:30:06 -04007424 free_extent_map(em);
Chris Mason925baed2008-06-25 16:01:30 -04007425 goto out;
7426 }
Chris Mason0b86a832008-03-24 15:01:56 -04007427 path->slots[0]++;
7428 }
Chris Mason925baed2008-06-25 16:01:30 -04007429out:
Chris Mason0b86a832008-03-24 15:01:56 -04007430 return ret;
7431}
7432
Josef Bacik0af3d002010-06-21 14:48:16 -04007433void 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 Bacik676f1f72019-06-20 15:37:48 -04007443 btrfs_wait_block_group_cache_done(block_group);
Josef Bacik0af3d002010-06-21 14:48:16 -04007444 spin_lock(&block_group->lock);
7445 if (block_group->iref)
7446 break;
7447 spin_unlock(&block_group->lock);
Josef Bacik2e405ad2019-06-20 15:37:45 -04007448 block_group = btrfs_next_block_group(block_group);
Josef Bacik0af3d002010-06-21 14:48:16 -04007449 }
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 Bof3bca802016-07-20 17:33:44 -07007461 ASSERT(block_group->io_ctl.inode == NULL);
Josef Bacik0af3d002010-06-21 14:48:16 -04007462 iput(inode);
7463 last = block_group->key.objectid + block_group->key.offset;
7464 btrfs_put_block_group(block_group);
7465 }
7466}
7467
Filipe Manana5cdd7db2017-02-01 22:39:50 +00007468/*
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 Yan1a40e232008-09-26 10:09:34 -04007473int btrfs_free_block_groups(struct btrfs_fs_info *info)
7474{
7475 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04007476 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04007477 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04007478 struct rb_node *n;
7479
Josef Bacik9e351cc2014-03-13 15:42:13 -04007480 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04007481 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 Bacike3cb3392019-06-20 15:37:50 -04007485 btrfs_put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04007486 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04007487 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04007488
Josef Bacik47ab2a62014-09-18 11:20:02 -04007489 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 Yan1a40e232008-09-26 10:09:34 -04007499 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 Yan1a40e232008-09-26 10:09:34 -04007503 rb_erase(&block_group->cache_node,
7504 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00007505 RB_CLEAR_NODE(&block_group->cache_node);
Yan Zhengd899e052008-10-30 14:25:28 -04007506 spin_unlock(&info->block_group_cache_lock);
7507
Josef Bacik80eb2342008-10-29 14:49:05 -04007508 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04007509 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04007510 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05007511
Josef Bacik3c148742011-02-02 15:53:47 +00007512 /*
7513 * We haven't cached this block group, which means we could
7514 * possibly have excluded extents on this block group.
7515 */
Josef Bacik36cce922013-08-05 11:15:21 -04007516 if (block_group->cached == BTRFS_CACHE_NO ||
7517 block_group->cached == BTRFS_CACHE_ERROR)
Josef Bacik6f410d12019-06-20 15:37:49 -04007518 btrfs_free_excluded_extents(block_group);
Josef Bacik3c148742011-02-02 15:53:47 +00007519
Josef Bacik817d52f2009-07-13 21:29:25 -04007520 btrfs_remove_free_space_cache(block_group);
Filipe Manana5cdd7db2017-02-01 22:39:50 +00007521 ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
Liu Bof3bca802016-07-20 17:33:44 -07007522 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 Bacik11dfe352009-11-13 20:12:59 +00007526 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04007527
7528 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04007529 }
7530 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04007531
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 Bacik67f9c222019-06-19 13:47:23 -04007540 btrfs_release_global_block_rsv(info);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04007541
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307542 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007543 int i;
7544
Chris Mason4184ea72009-03-10 12:39:20 -04007545 space_info = list_entry(info->space_info.next,
7546 struct btrfs_space_info,
7547 list);
Josef Bacikd555b6c2016-03-25 13:25:51 -04007548
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 Sterbab069e0c2013-02-08 21:28:17 +00007554 space_info->bytes_reserved > 0 ||
Josef Bacikd555b6c2016-03-25 13:25:51 -04007555 space_info->bytes_may_use > 0))
Josef Bacik5da6afe2019-06-18 16:09:24 -04007556 btrfs_dump_space_info(info, space_info, 0, 0);
Chris Mason4184ea72009-03-10 12:39:20 -04007557 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007558 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
7559 struct kobject *kobj;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04007560 kobj = space_info->block_group_kobjs[i];
7561 space_info->block_group_kobjs[i] = NULL;
7562 if (kobj) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007563 kobject_del(kobj);
7564 kobject_put(kobj);
7565 }
7566 }
7567 kobject_del(&space_info->kobj);
7568 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04007569 }
Zheng Yan1a40e232008-09-26 10:09:34 -04007570 return 0;
7571}
7572
Nikolay Borisovc434d212017-08-21 12:43:50 +03007573static void link_block_group(struct btrfs_block_group_cache *cache)
Yan, Zhengb742bb822010-05-16 10:46:24 -04007574{
Nikolay Borisovc434d212017-08-21 12:43:50 +03007575 struct btrfs_space_info *space_info = cache->space_info;
Jeff Mahoney75cb3792018-03-20 15:25:26 -04007576 struct btrfs_fs_info *fs_info = cache->fs_info;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007577 int index = btrfs_bg_flags_to_raid_index(cache->flags);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04007578 bool first = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04007579
7580 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04007581 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 Mananad7cd4dd2019-08-07 11:21:46 +01007587 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 Mahoney75cb3792018-03-20 15:25:26 -04007600 if (!rkobj) {
Filipe Mananad7cd4dd2019-08-07 11:21:46 +01007601 memalloc_nofs_restore(nofs_flag);
Jeff Mahoney75cb3792018-03-20 15:25:26 -04007602 btrfs_warn(cache->fs_info,
7603 "couldn't alloc memory for raid level kobject");
7604 return;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007605 }
Jeff Mahoney75cb3792018-03-20 15:25:26 -04007606 rkobj->flags = cache->flags;
7607 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
Filipe Mananad7cd4dd2019-08-07 11:21:46 +01007608 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 Mahoneyc1895442014-05-27 12:59:57 -04007617 space_info->block_group_kobjs[index] = &rkobj->kobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007618 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04007619}
7620
Miao Xie920e4a52014-01-15 20:00:55 +08007621static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007622btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info,
7623 u64 start, u64 size)
Miao Xie920e4a52014-01-15 20:00:55 +08007624{
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 Mahoney0b246af2016-06-22 18:54:23 -04007642 cache->fs_info = fs_info;
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03007643 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007644 set_free_space_tree_thresholds(cache);
7645
Miao Xie920e4a52014-01-15 20:00:55 +08007646 atomic_set(&cache->count, 1);
7647 spin_lock_init(&cache->lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007648 init_rwsem(&cache->data_rwsem);
Miao Xie920e4a52014-01-15 20:00:55 +08007649 INIT_LIST_HEAD(&cache->list);
7650 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -04007651 INIT_LIST_HEAD(&cache->bg_list);
Josef Bacik633c0aa2014-10-31 09:49:34 -04007652 INIT_LIST_HEAD(&cache->ro_list);
Josef Bacikce93ec52014-11-17 15:45:48 -05007653 INIT_LIST_HEAD(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07007654 INIT_LIST_HEAD(&cache->io_list);
Miao Xie920e4a52014-01-15 20:00:55 +08007655 btrfs_init_free_space_ctl(cache);
Filipe Manana04216822014-11-27 21:14:15 +00007656 atomic_set(&cache->trimming, 0);
Omar Sandovala5ed9182015-09-29 20:50:35 -07007657 mutex_init(&cache->free_space_lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +08007658 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
Miao Xie920e4a52014-01-15 20:00:55 +08007659
7660 return cache;
7661}
7662
Qu Wenruo7ef49512018-08-01 10:37:17 +08007663
7664/*
7665 * Iterate all chunks and verify that each of them has the corresponding block
7666 * group
7667 */
7668static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
7669{
David Sterbac8bf1b62019-05-17 11:43:17 +02007670 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Qu Wenruo7ef49512018-08-01 10:37:17 +08007671 struct extent_map *em;
7672 struct btrfs_block_group_cache *bg;
7673 u64 start = 0;
7674 int ret = 0;
7675
7676 while (1) {
David Sterbac8bf1b62019-05-17 11:43:17 +02007677 read_lock(&map_tree->lock);
Qu Wenruo7ef49512018-08-01 10:37:17 +08007678 /*
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 Sterbac8bf1b62019-05-17 11:43:17 +02007683 em = lookup_extent_mapping(map_tree, start, 1);
7684 read_unlock(&map_tree->lock);
Qu Wenruo7ef49512018-08-01 10:37:17 +08007685 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 Mahoney5b4aace2016-06-21 10:40:19 -04007719int btrfs_read_block_groups(struct btrfs_fs_info *info)
Chris Mason9078a3e2007-04-26 16:46:15 -04007720{
7721 struct btrfs_path *path;
7722 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007723 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04007724 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04007725 struct btrfs_key key;
7726 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04007727 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04007728 int need_clear = 0;
7729 u64 cache_gen;
Liu Bo49303382016-08-25 18:08:27 -07007730 u64 feature;
7731 int mixed;
7732
7733 feature = btrfs_super_incompat_flags(info->super_copy);
7734 mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS);
Chris Mason96b51792007-10-15 16:15:19 -04007735
Chris Mason9078a3e2007-04-26 16:46:15 -04007736 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007737 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02007738 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Chris Mason9078a3e2007-04-26 16:46:15 -04007739 path = btrfs_alloc_path();
7740 if (!path)
7741 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01007742 path->reada = READA_FORWARD;
Chris Mason9078a3e2007-04-26 16:46:15 -04007743
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007744 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 Bacik0af3d002010-06-21 14:48:16 -04007747 need_clear = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007748 if (btrfs_test_opt(info, CLEAR_CACHE))
Josef Bacik88c2ba32010-09-21 14:21:34 -04007749 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04007750
Chris Masond3977122009-01-05 21:25:51 -05007751 while (1) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007752 ret = find_first_block_group(info, path, &key);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007753 if (ret > 0)
7754 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007755 if (ret != 0)
7756 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +08007757
Chris Mason5f39d392007-10-15 16:14:19 -04007758 leaf = path->nodes[0];
7759 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +08007760
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007761 cache = btrfs_create_block_group_cache(info, found_key.objectid,
Miao Xie920e4a52014-01-15 20:00:55 +08007762 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04007763 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04007764 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007765 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04007766 }
Josef Bacik96303082009-07-13 21:29:25 -04007767
Liu Bocf7c1ef2012-07-06 03:31:34 -06007768 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 Mahoney0b246af2016-06-22 18:54:23 -04007779 if (btrfs_test_opt(info, SPACE_CACHE))
Josef Bacikce93ec52014-11-17 15:45:48 -05007780 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06007781 }
Josef Bacik0af3d002010-06-21 14:48:16 -04007782
Chris Mason5f39d392007-10-15 16:14:19 -04007783 read_extent_buffer(leaf, &cache->item,
7784 btrfs_item_ptr_offset(leaf, path->slots[0]),
7785 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +08007786 cache->flags = btrfs_block_group_flags(&cache->item);
Liu Bo49303382016-08-25 18:08:27 -07007787 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 Mason0b86a832008-03-24 15:01:56 -04007796
Chris Mason9078a3e2007-04-26 16:46:15 -04007797 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02007798 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +08007799
Josef Bacik817d52f2009-07-13 21:29:25 -04007800 /*
Josef Bacik3c148742011-02-02 15:53:47 +00007801 * 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 Borisov3c4da652018-06-20 15:49:09 +03007805 ret = exclude_super_stripes(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04007806 if (ret) {
7807 /*
7808 * We may have excluded something, so call this just in
7809 * case.
7810 */
Josef Bacik6f410d12019-06-20 15:37:49 -04007811 btrfs_free_excluded_extents(cache);
Miao Xie920e4a52014-01-15 20:00:55 +08007812 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04007813 goto error;
7814 }
Josef Bacik3c148742011-02-02 15:53:47 +00007815
7816 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04007817 * 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 Gelmini52042d82018-11-28 12:05:13 +01007820 * the space in and be done with it. This saves us _a_lot_ of
Josef Bacik817d52f2009-07-13 21:29:25 -04007821 * time, particularly in the full case.
7822 */
7823 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04007824 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007825 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik6f410d12019-06-20 15:37:49 -04007826 btrfs_free_excluded_extents(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007827 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04007828 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007829 cache->cached = BTRFS_CACHE_FINISHED;
Nikolay Borisov4457c1c2018-05-10 15:44:45 +03007830 add_new_free_space(cache, found_key.objectid,
Josef Bacik817d52f2009-07-13 21:29:25 -04007831 found_key.objectid +
7832 found_key.offset);
Josef Bacik6f410d12019-06-20 15:37:49 -04007833 btrfs_free_excluded_extents(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007834 }
Chris Mason96b51792007-10-15 16:15:19 -04007835
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007836 ret = btrfs_add_block_group_cache(info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -04007837 if (ret) {
7838 btrfs_remove_free_space_cache(cache);
7839 btrfs_put_block_group(cache);
7840 goto error;
7841 }
7842
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007843 trace_btrfs_add_block_group(info, cache, 0);
Josef Bacik280c29082019-06-18 16:09:19 -04007844 btrfs_update_space_info(info, cache->flags, found_key.offset,
7845 btrfs_block_group_used(&cache->item),
7846 cache->bytes_super, &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04007847
Chris Mason6324fbf2008-03-24 15:01:59 -04007848 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04007849
Nikolay Borisovc434d212017-08-21 12:43:50 +03007850 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007851
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007852 set_avail_alloc_bits(info, cache->flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007853 if (btrfs_chunk_readonly(info, cache->key.objectid)) {
Zhaolei868f4012015-08-05 16:43:27 +08007854 inc_block_group_ro(cache, 1);
Josef Bacik47ab2a62014-09-18 11:20:02 -04007855 } else if (btrfs_block_group_used(&cache->item) == 0) {
Qu Wenruo031f24d2018-05-22 16:43:47 +08007856 ASSERT(list_empty(&cache->bg_list));
7857 btrfs_mark_bg_unused(cache);
Josef Bacik47ab2a62014-09-18 11:20:02 -04007858 }
Chris Mason9078a3e2007-04-26 16:46:15 -04007859 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04007860
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007861 list_for_each_entry_rcu(space_info, &info->space_info, list) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007862 if (!(get_alloc_profile(info, space_info->flags) &
Yan, Zhengb742bb822010-05-16 10:46:24 -04007863 (BTRFS_BLOCK_GROUP_RAID10 |
David Sterbac7369b32019-05-31 15:39:31 +02007864 BTRFS_BLOCK_GROUP_RAID1_MASK |
David Sterbaa07e8a42019-05-31 16:54:26 +02007865 BTRFS_BLOCK_GROUP_RAID56_MASK |
Yan, Zhengb742bb822010-05-16 10:46:24 -04007866 BTRFS_BLOCK_GROUP_DUP)))
7867 continue;
7868 /*
7869 * avoid allocating from un-mirrored block group if there are
7870 * mirrored block groups.
7871 */
chandan1095cc02013-07-16 12:28:56 +05307872 list_for_each_entry(cache,
7873 &space_info->block_groups[BTRFS_RAID_RAID0],
7874 list)
Zhaolei868f4012015-08-05 16:43:27 +08007875 inc_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +05307876 list_for_each_entry(cache,
7877 &space_info->block_groups[BTRFS_RAID_SINGLE],
7878 list)
Zhaolei868f4012015-08-05 16:43:27 +08007879 inc_block_group_ro(cache, 1);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007880 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007881
Josef Bacik67f9c222019-06-19 13:47:23 -04007882 btrfs_init_global_block_rsv(info);
Qu Wenruo7ef49512018-08-01 10:37:17 +08007883 ret = check_chunk_block_group_mappings(info);
Chris Mason0b86a832008-03-24 15:01:56 -04007884error:
Chris Mason9078a3e2007-04-26 16:46:15 -04007885 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007886 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04007887}
Chris Mason6324fbf2008-03-24 15:01:59 -04007888
Nikolay Borisov6c686b32018-02-07 17:55:40 +02007889void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
Josef Bacikea658ba2012-09-11 16:57:25 -04007890{
Nikolay Borisov6c686b32018-02-07 17:55:40 +02007891 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik545e3362018-09-28 07:18:02 -04007892 struct btrfs_block_group_cache *block_group;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007893 struct btrfs_root *extent_root = fs_info->extent_root;
Josef Bacikea658ba2012-09-11 16:57:25 -04007894 struct btrfs_block_group_item item;
7895 struct btrfs_key key;
7896 int ret = 0;
7897
Filipe Manana5ce55552018-10-12 10:03:55 +01007898 if (!trans->can_flush_pending_bgs)
7899 return;
7900
Josef Bacik545e3362018-09-28 07:18:02 -04007901 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 Bacikea658ba2012-09-11 16:57:25 -04007905 if (ret)
Filipe Mananac92f6be2014-11-26 15:28:55 +00007906 goto next;
Josef Bacikea658ba2012-09-11 16:57:25 -04007907
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 Mahoney66642832016-06-10 18:19:25 -04007916 btrfs_abort_transaction(trans, ret);
Nikolay Borisov97aff912018-07-20 19:37:53 +03007917 ret = btrfs_finish_chunk_alloc(trans, key.objectid, key.offset);
Josef Bacik6df9a952013-06-27 13:22:46 -04007918 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04007919 btrfs_abort_transaction(trans, ret);
Nikolay Borisove4e07112018-05-10 15:44:41 +03007920 add_block_group_free_space(trans, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007921 /* already aborted the transaction if it failed. */
Filipe Mananac92f6be2014-11-26 15:28:55 +00007922next:
Josef Bacikba2c4d42018-12-03 10:20:33 -05007923 btrfs_delayed_refs_rsv_release(fs_info, 1);
Filipe Mananac92f6be2014-11-26 15:28:55 +00007924 list_del_init(&block_group->bg_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04007925 }
Filipe Manana5ce55552018-10-12 10:03:55 +01007926 btrfs_trans_release_chunk_metadata(trans);
Josef Bacikea658ba2012-09-11 16:57:25 -04007927}
7928
Nikolay Borisove7e02092018-06-20 15:48:55 +03007929int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used,
Nikolay Borisov01744842017-07-27 14:22:11 +03007930 u64 type, u64 chunk_offset, u64 size)
Chris Mason6324fbf2008-03-24 15:01:59 -04007931{
Nikolay Borisove7e02092018-06-20 15:48:55 +03007932 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04007933 struct btrfs_block_group_cache *cache;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007934 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04007935
David Sterba90787762019-03-20 13:28:05 +01007936 btrfs_set_log_full_commit(trans);
Chris Masone02119d2008-09-05 16:13:11 -04007937
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007938 cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007939 if (!cache)
7940 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08007941
Chris Mason6324fbf2008-03-24 15:01:59 -04007942 btrfs_set_block_group_used(&cache->item, bytes_used);
Nikolay Borisov01744842017-07-27 14:22:11 +03007943 btrfs_set_block_group_chunk_objectid(&cache->item,
7944 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Mason6324fbf2008-03-24 15:01:59 -04007945 btrfs_set_block_group_flags(&cache->item, type);
7946
Miao Xie920e4a52014-01-15 20:00:55 +08007947 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -04007948 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04007949 cache->cached = BTRFS_CACHE_FINISHED;
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007950 cache->needs_free_space = 1;
Nikolay Borisov3c4da652018-06-20 15:49:09 +03007951 ret = exclude_super_stripes(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04007952 if (ret) {
7953 /*
7954 * We may have excluded something, so call this just in
7955 * case.
7956 */
Josef Bacik6f410d12019-06-20 15:37:49 -04007957 btrfs_free_excluded_extents(cache);
Miao Xie920e4a52014-01-15 20:00:55 +08007958 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04007959 return ret;
7960 }
Josef Bacik96303082009-07-13 21:29:25 -04007961
Nikolay Borisov4457c1c2018-05-10 15:44:45 +03007962 add_new_free_space(cache, chunk_offset, chunk_offset + size);
Josef Bacik817d52f2009-07-13 21:29:25 -04007963
Josef Bacik6f410d12019-06-20 15:37:49 -04007964 btrfs_free_excluded_extents(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04007965
Josef Bacikd0bd4562015-09-23 14:54:14 -04007966#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007967 if (btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -04007968 u64 new_bytes_used = size - bytes_used;
7969
7970 bytes_used += new_bytes_used >> 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007971 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -04007972 }
7973#endif
Filipe Manana2e6e5182015-05-12 00:28:11 +01007974 /*
Nikolay Borisov2be12ef2017-05-22 09:35:49 +03007975 * 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 Manana2e6e5182015-05-12 00:28:11 +01007978 */
Josef Bacik280c29082019-06-18 16:09:19 -04007979 cache->space_info = btrfs_find_space_info(fs_info, cache->flags);
Jeff Mahoneydc2d3002018-03-20 15:25:25 -04007980 ASSERT(cache->space_info);
Filipe Manana2e6e5182015-05-12 00:28:11 +01007981
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007982 ret = btrfs_add_block_group_cache(fs_info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -04007983 if (ret) {
7984 btrfs_remove_free_space_cache(cache);
7985 btrfs_put_block_group(cache);
7986 return ret;
7987 }
7988
Filipe Manana2e6e5182015-05-12 00:28:11 +01007989 /*
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 Mahoney0b246af2016-06-22 18:54:23 -04007993 trace_btrfs_add_block_group(fs_info, cache, 1);
Josef Bacik280c29082019-06-18 16:09:19 -04007994 btrfs_update_space_info(fs_info, cache->flags, size, bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -04007995 cache->bytes_super, &cache->space_info);
Josef Bacik67f9c222019-06-19 13:47:23 -04007996 btrfs_update_global_block_rsv(fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04007997
Nikolay Borisovc434d212017-08-21 12:43:50 +03007998 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04007999
Josef Bacik47ab2a62014-09-18 11:20:02 -04008000 list_add_tail(&cache->bg_list, &trans->new_bgs);
Josef Bacikba2c4d42018-12-03 10:20:33 -05008001 trans->delayed_ref_updates++;
8002 btrfs_update_delayed_refs_rsv(trans);
Chris Mason6324fbf2008-03-24 15:01:59 -04008003
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008004 set_avail_alloc_bits(fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -04008005 return 0;
8006}
Zheng Yan1a40e232008-09-26 10:09:34 -04008007
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008008static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8009{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008010 u64 extra_flags = chunk_to_extended(flags) &
8011 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008012
Miao Xiede98ced2013-01-29 10:13:12 +00008013 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008014 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 Xiede98ced2013-01-29 10:13:12 +00008020 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008021}
8022
David Sterba6d58a552019-06-11 14:31:01 +02008023/*
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 */
8029static 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 Yan1a40e232008-09-26 10:09:34 -04008052int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03008053 u64 group_start, struct extent_map *em)
Zheng Yan1a40e232008-09-26 10:09:34 -04008054{
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03008055 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04008056 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008057 struct btrfs_path *path;
8058 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008059 struct btrfs_free_cluster *cluster;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008060 struct btrfs_root *tree_root = fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008061 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008062 struct inode *inode;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04008063 struct kobject *kobj = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04008064 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008065 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008066 int factor;
Filipe Manana4f69cb92014-11-26 15:28:51 +00008067 struct btrfs_caching_control *caching_ctl = NULL;
Filipe Manana04216822014-11-27 21:14:15 +00008068 bool remove_em;
Josef Bacikba2c4d42018-12-03 10:20:33 -05008069 bool remove_rsv = false;
Zheng Yan1a40e232008-09-26 10:09:34 -04008070
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04008071 block_group = btrfs_lookup_block_group(fs_info, group_start);
Zheng Yan1a40e232008-09-26 10:09:34 -04008072 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008073 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008074
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +08008075 trace_btrfs_remove_block_group(block_group);
liubo9f7c43c2011-03-07 02:13:33 +00008076 /*
8077 * Free the reserved super bytes from this block group before
8078 * remove it.
8079 */
Josef Bacik6f410d12019-06-20 15:37:49 -04008080 btrfs_free_excluded_extents(block_group);
Josef Bacikfd708b82017-09-29 15:43:50 -04008081 btrfs_free_ref_tree_range(fs_info, block_group->key.objectid,
8082 block_group->key.offset);
liubo9f7c43c2011-03-07 02:13:33 +00008083
Zheng Yan1a40e232008-09-26 10:09:34 -04008084 memcpy(&key, &block_group->key, sizeof(key));
Qu Wenruo3e72ee82018-01-30 18:20:45 +08008085 index = btrfs_bg_flags_to_raid_index(block_group->flags);
David Sterba46df06b2018-07-13 20:46:30 +02008086 factor = btrfs_bg_type_to_factor(block_group->flags);
Zheng Yan1a40e232008-09-26 10:09:34 -04008087
Chris Mason44fb5512009-06-04 15:34:51 -04008088 /* make sure this block group isn't part of an allocation cluster */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008089 cluster = &fs_info->data_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04008090 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 Mahoney0b246af2016-06-22 18:54:23 -04008098 cluster = &fs_info->meta_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04008099 spin_lock(&cluster->refill_lock);
8100 btrfs_return_cluster_to_free_space(block_group, cluster);
8101 spin_unlock(&cluster->refill_lock);
8102
Zheng Yan1a40e232008-09-26 10:09:34 -04008103 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008104 if (!path) {
8105 ret = -ENOMEM;
8106 goto out;
8107 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008108
Chris Mason1bbc6212015-04-06 12:46:08 -07008109 /*
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 Sterba7949f332019-03-20 13:40:19 +01008113 inode = lookup_free_space_inode(block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07008114
8115 mutex_lock(&trans->transaction->cache_write_mutex);
8116 /*
Andrea Gelmini52042d82018-11-28 12:05:13 +01008117 * Make sure our free space cache IO is done before removing the
Chris Mason1bbc6212015-04-06 12:46:08 -07008118 * 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 Mahoneyafdb5712016-09-09 12:09:35 -04008127 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07008128 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 Bacikba2c4d42018-12-03 10:20:33 -05008134 remove_rsv = true;
Chris Mason1bbc6212015-04-06 12:46:08 -07008135 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 Bacik0af3d002010-06-21 14:48:16 -04008140 if (!IS_ERR(inode)) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02008141 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008142 if (ret) {
8143 btrfs_add_delayed_iput(inode);
8144 goto out;
8145 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008146 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 Bacik455757c2011-09-19 12:26:24 -04008158 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008159 }
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 Sterbab3b4aa72011-04-21 01:20:15 +02008169 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008170 if (ret == 0) {
8171 ret = btrfs_del_item(trans, tree_root, path);
8172 if (ret)
8173 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008174 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008175 }
8176
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008177 spin_lock(&fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008178 rb_erase(&block_group->cache_node,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008179 &fs_info->block_group_cache_tree);
Filipe Manana292cbd52014-11-26 15:28:50 +00008180 RB_CLEAR_NODE(&block_group->cache_node);
Liu Boa1897fd2012-12-27 09:01:23 +00008181
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008182 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 Bacik817d52f2009-07-13 21:29:25 -04008185
Josef Bacik80eb2342008-10-29 14:49:05 -04008186 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008187 /*
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 Mahoney6ab0a202013-11-01 13:07:04 -04008192 if (list_empty(&block_group->space_info->block_groups[index])) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -04008193 kobj = block_group->space_info->block_group_kobjs[index];
8194 block_group->space_info->block_group_kobjs[index] = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008195 clear_avail_alloc_bits(fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008196 }
Josef Bacik80eb2342008-10-29 14:49:05 -04008197 up_write(&block_group->space_info->groups_sem);
David Sterba6d58a552019-06-11 14:31:01 +02008198 clear_incompat_bg_bits(fs_info, block_group->flags);
Jeff Mahoneyc1895442014-05-27 12:59:57 -04008199 if (kobj) {
8200 kobject_del(kobj);
8201 kobject_put(kobj);
8202 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008203
Filipe Manana4f69cb92014-11-26 15:28:51 +00008204 if (block_group->has_caching_ctl)
Josef Bacike3cb3392019-06-20 15:37:50 -04008205 caching_ctl = btrfs_get_caching_control(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008206 if (block_group->cached == BTRFS_CACHE_STARTED)
Josef Bacik676f1f72019-06-20 15:37:48 -04008207 btrfs_wait_block_group_cache_done(block_group);
Filipe Manana4f69cb92014-11-26 15:28:51 +00008208 if (block_group->has_caching_ctl) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008209 down_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +00008210 if (!caching_ctl) {
8211 struct btrfs_caching_control *ctl;
8212
8213 list_for_each_entry(ctl,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008214 &fs_info->caching_block_groups, list)
Filipe Manana4f69cb92014-11-26 15:28:51 +00008215 if (ctl->block_group == block_group) {
8216 caching_ctl = ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +02008217 refcount_inc(&caching_ctl->count);
Filipe Manana4f69cb92014-11-26 15:28:51 +00008218 break;
8219 }
8220 }
8221 if (caching_ctl)
8222 list_del_init(&caching_ctl->list);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008223 up_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +00008224 if (caching_ctl) {
8225 /* Once for the caching bgs list and once for us. */
Josef Bacike3cb3392019-06-20 15:37:50 -04008226 btrfs_put_caching_control(caching_ctl);
8227 btrfs_put_caching_control(caching_ctl);
Filipe Manana4f69cb92014-11-26 15:28:51 +00008228 }
8229 }
Josef Bacik817d52f2009-07-13 21:29:25 -04008230
Josef Bacikce93ec52014-11-17 15:45:48 -05008231 spin_lock(&trans->transaction->dirty_bgs_lock);
Nikolay Borisov9a0ec832019-01-30 16:50:49 +02008232 WARN_ON(!list_empty(&block_group->dirty_list));
8233 WARN_ON(!list_empty(&block_group->io_list));
Josef Bacikce93ec52014-11-17 15:45:48 -05008234 spin_unlock(&trans->transaction->dirty_bgs_lock);
Nikolay Borisov9a0ec832019-01-30 16:50:49 +02008235
Josef Bacik817d52f2009-07-13 21:29:25 -04008236 btrfs_remove_free_space_cache(block_group);
8237
Yan Zhengc146afa2008-11-12 14:34:12 -05008238 spin_lock(&block_group->space_info->lock);
Filipe Manana75c68e92015-01-16 13:24:40 +00008239 list_del_init(&block_group->ro_list);
Zhao Lei18d018a2015-02-24 20:07:44 +08008240
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008241 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Zhao Lei18d018a2015-02-24 20:07:44 +08008242 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 Zhengc146afa2008-11-12 14:34:12 -05008249 block_group->space_info->total_bytes -= block_group->key.offset;
8250 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008251 block_group->space_info->disk_total -= block_group->key.offset * factor;
Zhao Lei18d018a2015-02-24 20:07:44 +08008252
Yan Zhengc146afa2008-11-12 14:34:12 -05008253 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008254
Josef Bacik0af3d002010-06-21 14:48:16 -04008255 memcpy(&key, &block_group->key, sizeof(key));
8256
David Sterba34441362016-10-04 19:34:27 +02008257 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana04216822014-11-27 21:14:15 +00008258 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 Mahoneye33e17e2015-06-15 09:41:19 -04008277 *
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 Manana04216822014-11-27 21:14:15 +00008282 */
8283 remove_em = (atomic_read(&block_group->trimming) == 0);
Filipe Manana04216822014-11-27 21:14:15 +00008284 spin_unlock(&block_group->lock);
Filipe Manana04216822014-11-27 21:14:15 +00008285
David Sterba34441362016-10-04 19:34:27 +02008286 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana8dbcd102014-12-02 18:07:49 +00008287
Nikolay Borisovf3f72772018-05-10 15:44:46 +03008288 ret = remove_block_group_free_space(trans, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008289 if (ret)
8290 goto out;
8291
Chris Masonfa9c0d792009-04-03 09:47:43 -04008292 btrfs_put_block_group(block_group);
8293 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008294
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 Manana8eaf40c2019-06-12 11:05:42 +01008302 if (ret)
8303 goto out;
8304
8305 if (remove_em) {
8306 struct extent_map_tree *em_tree;
8307
David Sterbac8bf1b62019-05-17 11:43:17 +02008308 em_tree = &fs_info->mapping_tree;
Filipe Manana8eaf40c2019-06-12 11:05:42 +01008309 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 Yan1a40e232008-09-26 10:09:34 -04008315out:
Josef Bacikba2c4d42018-12-03 10:20:33 -05008316 if (remove_rsv)
8317 btrfs_delayed_refs_rsv_release(fs_info, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04008318 btrfs_free_path(path);
8319 return ret;
8320}
liuboacce9522011-01-06 19:30:25 +08008321
Filipe Manana8eab77f2015-11-13 23:57:16 +00008322struct btrfs_trans_handle *
Filipe Manana7fd01182015-11-13 23:57:17 +00008323btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info,
8324 const u64 chunk_offset)
Filipe Manana8eab77f2015-11-13 23:57:16 +00008325{
David Sterbac8bf1b62019-05-17 11:43:17 +02008326 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Filipe Manana7fd01182015-11-13 23:57:17 +00008327 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 Manana8eab77f2015-11-13 23:57:16 +00008336 /*
Filipe Manana7fd01182015-11-13 23:57:17 +00008337 * 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 Manana8eab77f2015-11-13 23:57:16 +00008341 * 1 unit for adding the free space inode's orphan (located in the tree
8342 * of tree roots).
Filipe Manana7fd01182015-11-13 23:57:17 +00008343 * 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 Manana8eab77f2015-11-13 23:57:16 +00008354 */
Jeff Mahoney95617d62015-06-03 10:55:48 -04008355 map = em->map_lookup;
Filipe Manana7fd01182015-11-13 23:57:17 +00008356 num_items = 3 + map->num_stripes;
8357 free_extent_map(em);
8358
Filipe Manana8eab77f2015-11-13 23:57:16 +00008359 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
Filipe Manana7fd01182015-11-13 23:57:17 +00008360 num_items, 1);
Filipe Manana8eab77f2015-11-13 23:57:16 +00008361}
8362
Josef Bacik47ab2a62014-09-18 11:20:02 -04008363/*
8364 * Process the unused_bgs list and remove any that don't have any allocated
8365 * space inside of them.
8366 */
8367void 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 Bacik47ab2a62014-09-18 11:20:02 -04008371 struct btrfs_trans_handle *trans;
8372 int ret = 0;
8373
Josef Bacikafcdd122016-09-02 15:40:02 -04008374 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
Josef Bacik47ab2a62014-09-18 11:20:02 -04008375 return;
8376
8377 spin_lock(&fs_info->unused_bgs_lock);
8378 while (!list_empty(&fs_info->unused_bgs)) {
8379 u64 start, end;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04008380 int trimming;
Josef Bacik47ab2a62014-09-18 11:20:02 -04008381
8382 block_group = list_first_entry(&fs_info->unused_bgs,
8383 struct btrfs_block_group_cache,
8384 bg_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -04008385 list_del_init(&block_group->bg_list);
Zhao Leiaefbe9a2015-09-29 21:03:54 +08008386
8387 space_info = block_group->space_info;
8388
Josef Bacik47ab2a62014-09-18 11:20:02 -04008389 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 Leid5f2e332015-10-08 18:46:44 +08008395 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01008396
Josef Bacik47ab2a62014-09-18 11:20:02 -04008397 /* 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 Wenruo43794442018-06-22 12:35:00 +08008400 if (block_group->reserved || block_group->pinned ||
Josef Bacik47ab2a62014-09-18 11:20:02 -04008401 btrfs_block_group_used(&block_group->item) ||
Chris Mason19c4d2f2016-10-10 13:43:31 -07008402 block_group->ro ||
Zhao Leiaefbe9a2015-09-29 21:03:54 +08008403 list_is_singular(&block_group->list)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04008404 /*
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 Wenruo4ed0a7a2018-04-26 17:17:20 +08008410 trace_btrfs_skip_unused_block_group(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -04008411 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. */
Zhaolei868f4012015-08-05 16:43:27 +08008418 ret = inc_block_group_ro(block_group, 0);
Josef Bacik47ab2a62014-09-18 11:20:02 -04008419 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 Manana7fd01182015-11-13 23:57:17 +00008429 trans = btrfs_start_trans_remove_block_group(fs_info,
8430 block_group->key.objectid);
Josef Bacik47ab2a62014-09-18 11:20:02 -04008431 if (IS_ERR(trans)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008432 btrfs_dec_block_group_ro(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -04008433 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 Mananad4b450c2015-01-29 19:18:25 +00008443 /*
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 Manana758eb512014-11-03 14:08:39 +00008455 ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
David Sterba91166212016-04-26 23:54:39 +02008456 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +00008457 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +00008458 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008459 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +00008460 goto end_trans;
8461 }
8462 ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
David Sterba91166212016-04-26 23:54:39 +02008463 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +00008464 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +00008465 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008466 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +00008467 goto end_trans;
8468 }
Filipe Mananad4b450c2015-01-29 19:18:25 +00008469 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04008470
8471 /* Reset pinned so btrfs_put_block_group doesn't complain */
Zhao Leic30666d2015-02-25 14:17:20 +08008472 spin_lock(&space_info->lock);
8473 spin_lock(&block_group->lock);
8474
Josef Bacikbb96c4e2019-06-18 16:09:21 -04008475 btrfs_space_info_update_bytes_pinned(fs_info, space_info,
8476 -block_group->pinned);
Zhao Leic30666d2015-02-25 14:17:20 +08008477 space_info->bytes_readonly += block_group->pinned;
Ethan Liendec59fa2018-07-13 16:50:42 +08008478 percpu_counter_add_batch(&space_info->total_bytes_pinned,
8479 -block_group->pinned,
8480 BTRFS_TOTAL_BYTES_PINNED_BATCH);
Josef Bacik47ab2a62014-09-18 11:20:02 -04008481 block_group->pinned = 0;
8482
Zhao Leic30666d2015-02-25 14:17:20 +08008483 spin_unlock(&block_group->lock);
8484 spin_unlock(&space_info->lock);
8485
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04008486 /* DISCARD can flip during remount */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008487 trimming = btrfs_test_opt(fs_info, DISCARD);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04008488
8489 /* Implicit trim during transaction commit. */
8490 if (trimming)
8491 btrfs_get_block_group_trimming(block_group);
8492
Josef Bacik47ab2a62014-09-18 11:20:02 -04008493 /*
8494 * Btrfs_remove_chunk will abort the transaction if things go
8495 * horribly wrong.
8496 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03008497 ret = btrfs_remove_chunk(trans, block_group->key.objectid);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04008498
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 Manana348a0012015-11-27 12:16:16 +00008511 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 Mahoneye33e17e2015-06-15 09:41:19 -04008517 list_move(&block_group->bg_list,
8518 &trans->transaction->deleted_bgs);
Filipe Manana348a0012015-11-27 12:16:16 +00008519 spin_unlock(&fs_info->unused_bgs_lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04008520 btrfs_get_block_group(block_group);
8521 }
Filipe Manana758eb512014-11-03 14:08:39 +00008522end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008523 btrfs_end_transaction(trans);
Josef Bacik47ab2a62014-09-18 11:20:02 -04008524next:
Zhao Leid5f2e332015-10-08 18:46:44 +08008525 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04008526 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 Mahoney2ff7e612016-06-22 18:54:24 -04008532int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
8533 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +08008534{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008535 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +08008536}
8537
Jeff Mahoney499f3772015-06-15 09:41:17 -04008538/*
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 Mahoneyfee7acc2018-09-06 17:18:16 -04008547 * to ensure that we're not discarding chunks that were released or
8548 * allocated in the current transaction.
Jeff Mahoney499f3772015-06-15 09:41:17 -04008549 *
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 Mahoneyfee7acc2018-09-06 17:18:16 -04008554 * 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 Mahoney499f3772015-06-15 09:41:17 -04008557 */
Nikolay Borisov8103d102019-06-03 13:06:00 +03008558static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed)
Jeff Mahoney499f3772015-06-15 09:41:17 -04008559{
Nikolay Borisov8103d102019-06-03 13:06:00 +03008560 u64 start = SZ_1M, len = 0, end = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04008561 int ret;
8562
8563 *trimmed = 0;
8564
Jeff Mahoney0be88e32018-09-06 17:18:15 -04008565 /* Discard not supported = nothing to do. */
8566 if (!blk_queue_discard(bdev_get_queue(device->bdev)))
8567 return 0;
8568
Andrea Gelmini52042d82018-11-28 12:05:13 +01008569 /* Not writable = nothing to do. */
Anand Jainebbede42017-12-04 12:54:52 +08008570 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoney499f3772015-06-15 09:41:17 -04008571 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 Mahoneyfb456252016-06-22 18:54:56 -04008580 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -04008581 u64 bytes;
8582
8583 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
8584 if (ret)
Jeff Mahoneyfee7acc2018-09-06 17:18:16 -04008585 break;
Jeff Mahoney499f3772015-06-15 09:41:17 -04008586
Nikolay Borisov929be172019-03-27 14:24:18 +02008587 find_first_clear_extent_bit(&device->alloc_state, start,
8588 &start, &end,
8589 CHUNK_TRIMMED | CHUNK_ALLOCATED);
Nikolay Borisov53460a42019-06-03 13:06:01 +03008590
8591 /* Ensure we skip the reserved area in the first 1M */
8592 start = max_t(u64, start, SZ_1M);
8593
Nikolay Borisov929be172019-03-27 14:24:18 +02008594 /*
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 Borisov53460a42019-06-03 13:06:01 +03008600
Nikolay Borisov929be172019-03-27 14:24:18 +02008601 len = end - start + 1;
Jeff Mahoney499f3772015-06-15 09:41:17 -04008602
Nikolay Borisov929be172019-03-27 14:24:18 +02008603 /* We didn't find any extents */
8604 if (!len) {
Jeff Mahoney499f3772015-06-15 09:41:17 -04008605 mutex_unlock(&fs_info->chunk_mutex);
Nikolay Borisov929be172019-03-27 14:24:18 +02008606 ret = 0;
Jeff Mahoney499f3772015-06-15 09:41:17 -04008607 break;
8608 }
8609
Nikolay Borisov929be172019-03-27 14:24:18 +02008610 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 Mahoney499f3772015-06-15 09:41:17 -04008616 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 Wenruo93bba242018-09-07 14:16:23 +08008635/*
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 Mahoney2ff7e612016-06-22 18:54:24 -04008644int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +00008645{
Li Dongyangf7039b12011-03-24 10:24:28 +00008646 struct btrfs_block_group_cache *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -04008647 struct btrfs_device *device;
8648 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +00008649 u64 group_trimmed;
Qu Wenruo07301df2019-05-28 16:21:54 +08008650 u64 range_end = U64_MAX;
Li Dongyangf7039b12011-03-24 10:24:28 +00008651 u64 start;
8652 u64 end;
8653 u64 trimmed = 0;
Qu Wenruo93bba242018-09-07 14:16:23 +08008654 u64 bg_failed = 0;
8655 u64 dev_failed = 0;
8656 int bg_ret = 0;
8657 int dev_ret = 0;
Li Dongyangf7039b12011-03-24 10:24:28 +00008658 int ret = 0;
8659
Qu Wenruo07301df2019-05-28 16:21:54 +08008660 /*
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 Wenruo6ba9fc82018-09-07 14:16:24 +08008668 cache = btrfs_lookup_first_block_group(fs_info, range->start);
Josef Bacik2e405ad2019-06-20 15:37:45 -04008669 for (; cache; cache = btrfs_next_block_group(cache)) {
Qu Wenruo07301df2019-05-28 16:21:54 +08008670 if (cache->key.objectid >= range_end) {
Li Dongyangf7039b12011-03-24 10:24:28 +00008671 btrfs_put_block_group(cache);
8672 break;
8673 }
8674
8675 start = max(range->start, cache->key.objectid);
Qu Wenruo07301df2019-05-28 16:21:54 +08008676 end = min(range_end, cache->key.objectid + cache->key.offset);
Li Dongyangf7039b12011-03-24 10:24:28 +00008677
8678 if (end - start >= range->minlen) {
Josef Bacik676f1f72019-06-20 15:37:48 -04008679 if (!btrfs_block_group_cache_done(cache)) {
8680 ret = btrfs_cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04008681 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08008682 bg_failed++;
8683 bg_ret = ret;
8684 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04008685 }
Josef Bacik676f1f72019-06-20 15:37:48 -04008686 ret = btrfs_wait_block_group_cache_done(cache);
Josef Bacik1be41b72013-06-12 13:56:06 -04008687 if (ret) {
Qu Wenruo93bba242018-09-07 14:16:23 +08008688 bg_failed++;
8689 bg_ret = ret;
8690 continue;
Josef Bacik1be41b72013-06-12 13:56:06 -04008691 }
Li Dongyangf7039b12011-03-24 10:24:28 +00008692 }
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 Wenruo93bba242018-09-07 14:16:23 +08008701 bg_failed++;
8702 bg_ret = ret;
8703 continue;
Li Dongyangf7039b12011-03-24 10:24:28 +00008704 }
8705 }
Li Dongyangf7039b12011-03-24 10:24:28 +00008706 }
8707
Qu Wenruo93bba242018-09-07 14:16:23 +08008708 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 Mahoney0b246af2016-06-22 18:54:23 -04008712 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoneyd4e329d2018-09-06 17:18:14 -04008713 devices = &fs_info->fs_devices->devices;
8714 list_for_each_entry(device, devices, dev_list) {
Nikolay Borisov8103d102019-06-03 13:06:00 +03008715 ret = btrfs_trim_free_extents(device, &group_trimmed);
Qu Wenruo93bba242018-09-07 14:16:23 +08008716 if (ret) {
8717 dev_failed++;
8718 dev_ret = ret;
Jeff Mahoney499f3772015-06-15 09:41:17 -04008719 break;
Qu Wenruo93bba242018-09-07 14:16:23 +08008720 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04008721
8722 trimmed += group_trimmed;
8723 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008724 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -04008725
Qu Wenruo93bba242018-09-07 14:16:23 +08008726 if (dev_failed)
8727 btrfs_warn(fs_info,
8728 "failed to trim %llu device(s), last error %d",
8729 dev_failed, dev_ret);
Li Dongyangf7039b12011-03-24 10:24:28 +00008730 range->len = trimmed;
Qu Wenruo93bba242018-09-07 14:16:23 +08008731 if (bg_ret)
8732 return bg_ret;
8733 return dev_ret;
Li Dongyangf7039b12011-03-24 10:24:28 +00008734}
Miao Xie8257b2d2014-03-06 13:38:19 +08008735
8736/*
David Sterbaea14b57f2017-06-22 02:19:11 +02008737 * btrfs_{start,end}_write_no_snapshotting() are similar to
Filipe Manana9ea24bb2014-10-29 11:57:59 +00008738 * 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 Sterbaea14b57f2017-06-22 02:19:11 +02008741 * operations while snapshotting is ongoing and that cause the snapshot to be
Filipe Manana9ea24bb2014-10-29 11:57:59 +00008742 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +08008743 */
David Sterbaea14b57f2017-06-22 02:19:11 +02008744void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +08008745{
8746 percpu_counter_dec(&root->subv_writers->counter);
David Sterba093258e2018-02-26 16:15:17 +01008747 cond_wake_up(&root->subv_writers->wait);
Miao Xie8257b2d2014-03-06 13:38:19 +08008748}
8749
David Sterbaea14b57f2017-06-22 02:19:11 +02008750int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +08008751{
David Sterbaea14b57f2017-06-22 02:19:11 +02008752 if (atomic_read(&root->will_be_snapshotted))
Miao Xie8257b2d2014-03-06 13:38:19 +08008753 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 Sterbaea14b57f2017-06-22 02:19:11 +02008760 if (atomic_read(&root->will_be_snapshotted)) {
8761 btrfs_end_write_no_snapshotting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +08008762 return 0;
8763 }
8764 return 1;
8765}
Zhao Lei0bc19f902016-01-06 18:56:36 +08008766
Zhao Lei0bc19f902016-01-06 18:56:36 +08008767void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
8768{
8769 while (true) {
8770 int ret;
8771
David Sterbaea14b57f2017-06-22 02:19:11 +02008772 ret = btrfs_start_write_no_snapshotting(root);
Zhao Lei0bc19f902016-01-06 18:56:36 +08008773 if (ret)
8774 break;
Peter Zijlstra46259562018-03-15 11:43:08 +01008775 wait_var_event(&root->will_be_snapshotted,
8776 !atomic_read(&root->will_be_snapshotted));
Zhao Lei0bc19f902016-01-06 18:56:36 +08008777 }
8778}
Qu Wenruo031f24d2018-05-22 16:43:47 +08008779
8780void 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}