blob: 67a3aa8bc2835923233ded207e7116a6953bfff6 [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>
Ingo Molnarf361bf42017-02-03 23:47:37 +01007#include <linux/sched/signal.h>
Chris Masonedbd8d42007-12-21 16:27:24 -05008#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -04009#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010010#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050011#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040012#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040013#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Sterbadff51cd2011-06-14 12:52:17 +020015#include <linux/ratelimit.h>
Josef Bacikb150a4f2013-06-19 15:00:04 -040016#include <linux/percpu_counter.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040017#include <linux/lockdep.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020018#include <linux/crc32c.h>
Miao Xie995946d2014-04-02 19:51:06 +080019#include "tree-log.h"
Chris Masonfec577f2007-02-26 10:40:21 -050020#include "disk-io.h"
21#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040022#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050023#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040024#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040025#include "free-space-cache.h"
Omar Sandoval1e144fb2015-09-29 20:50:37 -070026#include "free-space-tree.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060027#include "math.h"
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040028#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070029#include "qgroup.h"
Josef Bacikfd708b82017-09-29 15:43:50 -040030#include "ref-verify.h"
Chris Masonfec577f2007-02-26 10:40:21 -050031
Arne Jansen709c0482011-09-12 12:22:57 +020032#undef SCRAMBLE_DELAYED_REFS
33
Miao Xie9e622d62012-01-26 15:01:12 -050034/*
35 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040036 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
37 * if we really need one.
38 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040039 * CHUNK_ALLOC_LIMITED means to only try and allocate one
40 * if we have very few chunks already allocated. This is
41 * used as part of the clustering code to help make sure
42 * we have a good pool of storage to cluster in, without
43 * filling the FS with empty chunks
44 *
Miao Xie9e622d62012-01-26 15:01:12 -050045 * CHUNK_ALLOC_FORCE means it must try to allocate one
46 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040047 */
48enum {
49 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050050 CHUNK_ALLOC_LIMITED = 1,
51 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040052};
53
Yan Zheng5d4f98a2009-06-10 10:45:14 -040054static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +030055 struct btrfs_delayed_ref_node *node, u64 parent,
56 u64 root_objectid, u64 owner_objectid,
57 u64 owner_offset, int refs_to_drop,
58 struct btrfs_delayed_extent_op *extra_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040059static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
60 struct extent_buffer *leaf,
61 struct btrfs_extent_item *ei);
62static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040063 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040064 u64 parent, u64 root_objectid,
65 u64 flags, u64 owner, u64 offset,
66 struct btrfs_key *ins, int ref_mod);
67static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +030068 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +030069 struct btrfs_delayed_extent_op *extent_op);
Josef Bacik6a632092009-02-20 11:00:09 -050070static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040071 struct btrfs_fs_info *fs_info, u64 flags,
Josef Bacik698d0082012-09-12 14:08:47 -040072 int force);
Yan Zheng11833d62009-09-11 16:11:19 -040073static int find_next_key(struct btrfs_path *path, int level,
74 struct btrfs_key *key);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -040075static void dump_space_info(struct btrfs_fs_info *fs_info,
76 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -040077 int dump_block_groups);
Josef Bacik5d803662013-02-07 16:06:02 -050078static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
79 u64 num_bytes);
Josef Bacik957780e2016-05-17 13:30:55 -040080static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
81 struct btrfs_space_info *space_info,
82 u64 num_bytes);
83static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
84 struct btrfs_space_info *space_info,
85 u64 num_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -050086
Josef Bacik817d52f2009-07-13 21:29:25 -040087static noinline int
88block_group_cache_done(struct btrfs_block_group_cache *cache)
89{
90 smp_mb();
Josef Bacik36cce922013-08-05 11:15:21 -040091 return cache->cached == BTRFS_CACHE_FINISHED ||
92 cache->cached == BTRFS_CACHE_ERROR;
Josef Bacik817d52f2009-07-13 21:29:25 -040093}
94
Josef Bacik0f9dd462008-09-23 13:14:11 -040095static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
96{
97 return (cache->flags & bits) == bits;
98}
99
Filipe Manana758f2df2015-11-19 11:45:48 +0000100void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000101{
102 atomic_inc(&cache->count);
103}
104
105void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
106{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400107 if (atomic_dec_and_test(&cache->count)) {
108 WARN_ON(cache->pinned > 0);
109 WARN_ON(cache->reserved > 0);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800110
111 /*
112 * If not empty, someone is still holding mutex of
113 * full_stripe_lock, which can only be released by caller.
114 * And it will definitely cause use-after-free when caller
115 * tries to release full stripe lock.
116 *
117 * No better way to resolve, but only to warn.
118 */
119 WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
Li Zefan34d52cb2011-03-29 13:46:06 +0800120 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000121 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400122 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000123}
124
Josef Bacik0f9dd462008-09-23 13:14:11 -0400125/*
126 * this adds the block group to the fs_info rb tree for the block group
127 * cache
128 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500129static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400130 struct btrfs_block_group_cache *block_group)
131{
132 struct rb_node **p;
133 struct rb_node *parent = NULL;
134 struct btrfs_block_group_cache *cache;
135
136 spin_lock(&info->block_group_cache_lock);
137 p = &info->block_group_cache_tree.rb_node;
138
139 while (*p) {
140 parent = *p;
141 cache = rb_entry(parent, struct btrfs_block_group_cache,
142 cache_node);
143 if (block_group->key.objectid < cache->key.objectid) {
144 p = &(*p)->rb_left;
145 } else if (block_group->key.objectid > cache->key.objectid) {
146 p = &(*p)->rb_right;
147 } else {
148 spin_unlock(&info->block_group_cache_lock);
149 return -EEXIST;
150 }
151 }
152
153 rb_link_node(&block_group->cache_node, parent, p);
154 rb_insert_color(&block_group->cache_node,
155 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000156
157 if (info->first_logical_byte > block_group->key.objectid)
158 info->first_logical_byte = block_group->key.objectid;
159
Josef Bacik0f9dd462008-09-23 13:14:11 -0400160 spin_unlock(&info->block_group_cache_lock);
161
162 return 0;
163}
164
165/*
166 * This will return the block group at or after bytenr if contains is 0, else
167 * it will return the block group that contains the bytenr
168 */
169static struct btrfs_block_group_cache *
170block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
171 int contains)
172{
173 struct btrfs_block_group_cache *cache, *ret = NULL;
174 struct rb_node *n;
175 u64 end, start;
176
177 spin_lock(&info->block_group_cache_lock);
178 n = info->block_group_cache_tree.rb_node;
179
180 while (n) {
181 cache = rb_entry(n, struct btrfs_block_group_cache,
182 cache_node);
183 end = cache->key.objectid + cache->key.offset - 1;
184 start = cache->key.objectid;
185
186 if (bytenr < start) {
187 if (!contains && (!ret || start < ret->key.objectid))
188 ret = cache;
189 n = n->rb_left;
190 } else if (bytenr > start) {
191 if (contains && bytenr <= end) {
192 ret = cache;
193 break;
194 }
195 n = n->rb_right;
196 } else {
197 ret = cache;
198 break;
199 }
200 }
Liu Boa1897fd2012-12-27 09:01:23 +0000201 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000202 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000203 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
204 info->first_logical_byte = ret->key.objectid;
205 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400206 spin_unlock(&info->block_group_cache_lock);
207
208 return ret;
209}
210
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400211static int add_excluded_extent(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400212 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400213{
Yan Zheng11833d62009-09-11 16:11:19 -0400214 u64 end = start + num_bytes - 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400215 set_extent_bits(&fs_info->freed_extents[0],
David Sterbaceeb0ae2016-04-26 23:54:39 +0200216 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400217 set_extent_bits(&fs_info->freed_extents[1],
David Sterbaceeb0ae2016-04-26 23:54:39 +0200218 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -0400219 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400220}
221
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400222static void free_excluded_extents(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400223 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400224{
Yan Zheng11833d62009-09-11 16:11:19 -0400225 u64 start, end;
226
227 start = cache->key.objectid;
228 end = start + cache->key.offset - 1;
229
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400230 clear_extent_bits(&fs_info->freed_extents[0],
David Sterba91166212016-04-26 23:54:39 +0200231 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400232 clear_extent_bits(&fs_info->freed_extents[1],
David Sterba91166212016-04-26 23:54:39 +0200233 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -0400234}
235
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400236static int exclude_super_stripes(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400237 struct btrfs_block_group_cache *cache)
238{
Josef Bacik817d52f2009-07-13 21:29:25 -0400239 u64 bytenr;
240 u64 *logical;
241 int stripe_len;
242 int i, nr, ret;
243
Yan, Zheng06b23312009-11-26 09:31:11 +0000244 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
245 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
246 cache->bytes_super += stripe_len;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400247 ret = add_excluded_extent(fs_info, cache->key.objectid,
Yan, Zheng06b23312009-11-26 09:31:11 +0000248 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400249 if (ret)
250 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000251 }
252
Josef Bacik817d52f2009-07-13 21:29:25 -0400253 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
254 bytenr = btrfs_sb_offset(i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400255 ret = btrfs_rmap_block(fs_info, cache->key.objectid,
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +0300256 bytenr, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400257 if (ret)
258 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400259
Josef Bacik817d52f2009-07-13 21:29:25 -0400260 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400261 u64 start, len;
262
263 if (logical[nr] > cache->key.objectid +
264 cache->key.offset)
265 continue;
266
267 if (logical[nr] + stripe_len <= cache->key.objectid)
268 continue;
269
270 start = logical[nr];
271 if (start < cache->key.objectid) {
272 start = cache->key.objectid;
273 len = (logical[nr] + stripe_len) - start;
274 } else {
275 len = min_t(u64, stripe_len,
276 cache->key.objectid +
277 cache->key.offset - start);
278 }
279
280 cache->bytes_super += len;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400281 ret = add_excluded_extent(fs_info, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400282 if (ret) {
283 kfree(logical);
284 return ret;
285 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400286 }
Yan Zheng11833d62009-09-11 16:11:19 -0400287
Josef Bacik817d52f2009-07-13 21:29:25 -0400288 kfree(logical);
289 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400290 return 0;
291}
292
Yan Zheng11833d62009-09-11 16:11:19 -0400293static struct btrfs_caching_control *
294get_caching_control(struct btrfs_block_group_cache *cache)
295{
296 struct btrfs_caching_control *ctl;
297
298 spin_lock(&cache->lock);
Josef Bacikdde5abe2010-09-16 16:17:03 -0400299 if (!cache->caching_ctl) {
300 spin_unlock(&cache->lock);
301 return NULL;
302 }
303
Yan Zheng11833d62009-09-11 16:11:19 -0400304 ctl = cache->caching_ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200305 refcount_inc(&ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400306 spin_unlock(&cache->lock);
307 return ctl;
308}
309
310static void put_caching_control(struct btrfs_caching_control *ctl)
311{
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200312 if (refcount_dec_and_test(&ctl->count))
Yan Zheng11833d62009-09-11 16:11:19 -0400313 kfree(ctl);
314}
315
Josef Bacikd0bd4562015-09-23 14:54:14 -0400316#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400317static void fragment_free_space(struct btrfs_block_group_cache *block_group)
Josef Bacikd0bd4562015-09-23 14:54:14 -0400318{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400319 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400320 u64 start = block_group->key.objectid;
321 u64 len = block_group->key.offset;
322 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400323 fs_info->nodesize : fs_info->sectorsize;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400324 u64 step = chunk << 1;
325
326 while (len > chunk) {
327 btrfs_remove_free_space(block_group, start, chunk);
328 start += step;
329 if (len < step)
330 len = 0;
331 else
332 len -= step;
333 }
334}
335#endif
336
Josef Bacik0f9dd462008-09-23 13:14:11 -0400337/*
338 * this is only called by cache_block_group, since we could have freed extents
339 * we need to check the pinned_extents for any extents that can't be used yet
340 * since their free space will be released as soon as the transaction commits.
341 */
Omar Sandovala5ed9182015-09-29 20:50:35 -0700342u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Nikolay Borisov4457c1c2018-05-10 15:44:45 +0300343 u64 start, u64 end)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400344{
Nikolay Borisov4457c1c2018-05-10 15:44:45 +0300345 struct btrfs_fs_info *info = block_group->fs_info;
Josef Bacik817d52f2009-07-13 21:29:25 -0400346 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400347 int ret;
348
349 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400350 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400351 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400352 EXTENT_DIRTY | EXTENT_UPTODATE,
353 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400354 if (ret)
355 break;
356
Yan, Zheng06b23312009-11-26 09:31:11 +0000357 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400358 start = extent_end + 1;
359 } else if (extent_start > start && extent_start < end) {
360 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400361 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500362 ret = btrfs_add_free_space(block_group, start,
363 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100364 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400365 start = extent_end + 1;
366 } else {
367 break;
368 }
369 }
370
371 if (start < end) {
372 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400373 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500374 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100375 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400376 }
377
Josef Bacik817d52f2009-07-13 21:29:25 -0400378 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400379}
380
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700381static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
Chris Masone37c9e62007-05-09 20:13:14 -0400382{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400383 struct btrfs_block_group_cache *block_group = caching_ctl->block_group;
384 struct btrfs_fs_info *fs_info = block_group->fs_info;
385 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400386 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400387 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400388 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400389 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400390 u64 last = 0;
391 u32 nritems;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700392 int ret;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400393 bool wakeup = true;
Chris Masonf510cfe2007-10-15 16:14:48 -0400394
Chris Masone37c9e62007-05-09 20:13:14 -0400395 path = btrfs_alloc_path();
396 if (!path)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700397 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400398
Josef Bacik817d52f2009-07-13 21:29:25 -0400399 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400400
Josef Bacikd0bd4562015-09-23 14:54:14 -0400401#ifdef CONFIG_BTRFS_DEBUG
402 /*
403 * If we're fragmenting we don't want to make anybody think we can
404 * allocate from this block group until we've had a chance to fragment
405 * the free space.
406 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400407 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -0400408 wakeup = false;
409#endif
Chris Mason5cd57b22008-06-25 16:01:30 -0400410 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400411 * We don't want to deadlock with somebody trying to allocate a new
412 * extent for the extent root while also trying to search the extent
413 * root to add free space. So we skip locking and search the commit
414 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400415 */
416 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400417 path->search_commit_root = 1;
David Sterbae4058b52015-11-27 16:31:35 +0100418 path->reada = READA_FORWARD;
Josef Bacik817d52f2009-07-13 21:29:25 -0400419
Yan Zhenge4404d62008-12-12 10:03:26 -0500420 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400421 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400422 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400423
Liu Bo52ee28d2013-07-11 17:51:15 +0800424next:
Yan Zheng11833d62009-09-11 16:11:19 -0400425 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400426 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700427 goto out;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500428
Yan Zheng11833d62009-09-11 16:11:19 -0400429 leaf = path->nodes[0];
430 nritems = btrfs_header_nritems(leaf);
431
Chris Masond3977122009-01-05 21:25:51 -0500432 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200433 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400434 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400435 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400436 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400437
Yan Zheng11833d62009-09-11 16:11:19 -0400438 if (path->slots[0] < nritems) {
439 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
440 } else {
441 ret = find_next_key(path, 0, &key);
442 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400443 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400444
Josef Bacikc9ea7b22013-09-19 10:02:11 -0400445 if (need_resched() ||
Josef Bacik9e351cc2014-03-13 15:42:13 -0400446 rwsem_is_contended(&fs_info->commit_root_sem)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400447 if (wakeup)
448 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400449 btrfs_release_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400450 up_read(&fs_info->commit_root_sem);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400451 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400452 cond_resched();
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700453 mutex_lock(&caching_ctl->mutex);
454 down_read(&fs_info->commit_root_sem);
455 goto next;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400456 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400457
458 ret = btrfs_next_leaf(extent_root, path);
459 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700460 goto out;
Josef Bacik0a3896d2013-04-19 14:37:26 -0400461 if (ret)
462 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400463 leaf = path->nodes[0];
464 nritems = btrfs_header_nritems(leaf);
465 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400466 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400467
Liu Bo52ee28d2013-07-11 17:51:15 +0800468 if (key.objectid < last) {
469 key.objectid = last;
470 key.offset = 0;
471 key.type = BTRFS_EXTENT_ITEM_KEY;
472
Josef Bacikd0bd4562015-09-23 14:54:14 -0400473 if (wakeup)
474 caching_ctl->progress = last;
Liu Bo52ee28d2013-07-11 17:51:15 +0800475 btrfs_release_path(path);
476 goto next;
477 }
478
Yan Zheng11833d62009-09-11 16:11:19 -0400479 if (key.objectid < block_group->key.objectid) {
480 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400481 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400482 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400483
Chris Masone37c9e62007-05-09 20:13:14 -0400484 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400485 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400486 break;
Yan7d7d6062007-09-14 16:15:28 -0400487
Josef Bacik3173a182013-03-07 14:22:04 -0500488 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
489 key.type == BTRFS_METADATA_ITEM_KEY) {
Nikolay Borisov4457c1c2018-05-10 15:44:45 +0300490 total_found += add_new_free_space(block_group, last,
Josef Bacik817d52f2009-07-13 21:29:25 -0400491 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500492 if (key.type == BTRFS_METADATA_ITEM_KEY)
493 last = key.objectid +
Jeff Mahoneyda170662016-06-15 09:22:56 -0400494 fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500495 else
496 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400497
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700498 if (total_found > CACHING_CTL_WAKE_UP) {
Yan Zheng11833d62009-09-11 16:11:19 -0400499 total_found = 0;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400500 if (wakeup)
501 wake_up(&caching_ctl->wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400502 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400503 }
Chris Masone37c9e62007-05-09 20:13:14 -0400504 path->slots[0]++;
505 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400506 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400507
Nikolay Borisov4457c1c2018-05-10 15:44:45 +0300508 total_found += add_new_free_space(block_group, last,
Josef Bacik817d52f2009-07-13 21:29:25 -0400509 block_group->key.objectid +
510 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400511 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400512
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700513out:
514 btrfs_free_path(path);
515 return ret;
516}
517
518static noinline void caching_thread(struct btrfs_work *work)
519{
520 struct btrfs_block_group_cache *block_group;
521 struct btrfs_fs_info *fs_info;
522 struct btrfs_caching_control *caching_ctl;
523 int ret;
524
525 caching_ctl = container_of(work, struct btrfs_caching_control, work);
526 block_group = caching_ctl->block_group;
527 fs_info = block_group->fs_info;
528
529 mutex_lock(&caching_ctl->mutex);
530 down_read(&fs_info->commit_root_sem);
531
Omar Sandoval1e144fb2015-09-29 20:50:37 -0700532 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
533 ret = load_free_space_tree(caching_ctl);
534 else
535 ret = load_extent_tree_free(caching_ctl);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700536
Josef Bacik817d52f2009-07-13 21:29:25 -0400537 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400538 block_group->caching_ctl = NULL;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700539 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
Josef Bacik817d52f2009-07-13 21:29:25 -0400540 spin_unlock(&block_group->lock);
541
Josef Bacikd0bd4562015-09-23 14:54:14 -0400542#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400543 if (btrfs_should_fragment_free_space(block_group)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400544 u64 bytes_used;
545
546 spin_lock(&block_group->space_info->lock);
547 spin_lock(&block_group->lock);
548 bytes_used = block_group->key.offset -
549 btrfs_block_group_used(&block_group->item);
550 block_group->space_info->bytes_used += bytes_used >> 1;
551 spin_unlock(&block_group->lock);
552 spin_unlock(&block_group->space_info->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400553 fragment_free_space(block_group);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400554 }
555#endif
556
557 caching_ctl->progress = (u64)-1;
Chris Masonf7d3d2f2015-12-18 11:11:10 -0800558
Josef Bacik9e351cc2014-03-13 15:42:13 -0400559 up_read(&fs_info->commit_root_sem);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400560 free_excluded_extents(fs_info, block_group);
Yan Zheng11833d62009-09-11 16:11:19 -0400561 mutex_unlock(&caching_ctl->mutex);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700562
Yan Zheng11833d62009-09-11 16:11:19 -0400563 wake_up(&caching_ctl->wait);
564
565 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000566 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400567}
568
Josef Bacik9d66e232010-08-25 16:54:15 -0400569static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400570 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400571{
Josef Bacik291c7d22011-11-14 13:52:14 -0500572 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400573 struct btrfs_fs_info *fs_info = cache->fs_info;
574 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400575 int ret = 0;
576
Josef Bacik291c7d22011-11-14 13:52:14 -0500577 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100578 if (!caching_ctl)
579 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500580
581 INIT_LIST_HEAD(&caching_ctl->list);
582 mutex_init(&caching_ctl->mutex);
583 init_waitqueue_head(&caching_ctl->wait);
584 caching_ctl->block_group = cache;
585 caching_ctl->progress = cache->key.objectid;
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200586 refcount_set(&caching_ctl->count, 1);
Liu Bo9e0af232014-08-15 23:36:53 +0800587 btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
588 caching_thread, NULL, NULL);
Josef Bacik291c7d22011-11-14 13:52:14 -0500589
590 spin_lock(&cache->lock);
591 /*
592 * This should be a rare occasion, but this could happen I think in the
593 * case where one thread starts to load the space cache info, and then
594 * some other thread starts a transaction commit which tries to do an
595 * allocation while the other thread is still loading the space cache
596 * info. The previous loop should have kept us from choosing this block
597 * group, but if we've moved to the state where we will wait on caching
598 * block groups we need to first check if we're doing a fast load here,
599 * so we can wait for it to finish, otherwise we could end up allocating
600 * from a block group who's cache gets evicted for one reason or
601 * another.
602 */
603 while (cache->cached == BTRFS_CACHE_FAST) {
604 struct btrfs_caching_control *ctl;
605
606 ctl = cache->caching_ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200607 refcount_inc(&ctl->count);
Josef Bacik291c7d22011-11-14 13:52:14 -0500608 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
609 spin_unlock(&cache->lock);
610
611 schedule();
612
613 finish_wait(&ctl->wait, &wait);
614 put_caching_control(ctl);
615 spin_lock(&cache->lock);
616 }
617
618 if (cache->cached != BTRFS_CACHE_NO) {
619 spin_unlock(&cache->lock);
620 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400621 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500622 }
623 WARN_ON(cache->caching_ctl);
624 cache->caching_ctl = caching_ctl;
625 cache->cached = BTRFS_CACHE_FAST;
626 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400627
Satoru Takeuchid8953d62017-09-12 20:08:08 +0900628 if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500629 mutex_lock(&caching_ctl->mutex);
Josef Bacik9d66e232010-08-25 16:54:15 -0400630 ret = load_free_space_cache(fs_info, cache);
631
632 spin_lock(&cache->lock);
633 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500634 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400635 cache->cached = BTRFS_CACHE_FINISHED;
636 cache->last_byte_to_unpin = (u64)-1;
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500637 caching_ctl->progress = (u64)-1;
Josef Bacik9d66e232010-08-25 16:54:15 -0400638 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500639 if (load_cache_only) {
640 cache->caching_ctl = NULL;
641 cache->cached = BTRFS_CACHE_NO;
642 } else {
643 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000644 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500645 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400646 }
647 spin_unlock(&cache->lock);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400648#ifdef CONFIG_BTRFS_DEBUG
649 if (ret == 1 &&
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400650 btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400651 u64 bytes_used;
652
653 spin_lock(&cache->space_info->lock);
654 spin_lock(&cache->lock);
655 bytes_used = cache->key.offset -
656 btrfs_block_group_used(&cache->item);
657 cache->space_info->bytes_used += bytes_used >> 1;
658 spin_unlock(&cache->lock);
659 spin_unlock(&cache->space_info->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400660 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400661 }
662#endif
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500663 mutex_unlock(&caching_ctl->mutex);
664
Josef Bacik291c7d22011-11-14 13:52:14 -0500665 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000666 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500667 put_caching_control(caching_ctl);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400668 free_excluded_extents(fs_info, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400669 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000670 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500671 } else {
672 /*
Omar Sandoval1e144fb2015-09-29 20:50:37 -0700673 * We're either using the free space tree or no caching at all.
674 * Set cached to the appropriate value and wakeup any waiters.
Josef Bacik291c7d22011-11-14 13:52:14 -0500675 */
676 spin_lock(&cache->lock);
677 if (load_cache_only) {
678 cache->caching_ctl = NULL;
679 cache->cached = BTRFS_CACHE_NO;
680 } else {
681 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000682 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500683 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400684 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500685 wake_up(&caching_ctl->wait);
686 }
687
688 if (load_cache_only) {
689 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400690 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400691 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400692
Josef Bacik9e351cc2014-03-13 15:42:13 -0400693 down_write(&fs_info->commit_root_sem);
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200694 refcount_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400695 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400696 up_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -0400697
Josef Bacik11dfe352009-11-13 20:12:59 +0000698 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400699
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800700 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400701
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400702 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400703}
704
Josef Bacik0f9dd462008-09-23 13:14:11 -0400705/*
706 * return the block group that starts at or after bytenr
707 */
Chris Masond3977122009-01-05 21:25:51 -0500708static struct btrfs_block_group_cache *
709btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400710{
Masahiro Yamadae2c89902016-09-13 04:35:52 +0900711 return block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400712}
713
Josef Bacik0f9dd462008-09-23 13:14:11 -0400714/*
Sankar P9f556842009-05-14 13:52:22 -0400715 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400716 */
Chris Masond3977122009-01-05 21:25:51 -0500717struct btrfs_block_group_cache *btrfs_lookup_block_group(
718 struct btrfs_fs_info *info,
719 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400720{
Masahiro Yamadae2c89902016-09-13 04:35:52 +0900721 return block_group_cache_tree_search(info, bytenr, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400722}
Chris Mason0b86a832008-03-24 15:01:56 -0400723
Josef Bacik0f9dd462008-09-23 13:14:11 -0400724static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
725 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400726{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400727 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400728 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400729
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200730 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400731
Chris Mason4184ea72009-03-10 12:39:20 -0400732 rcu_read_lock();
733 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400734 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400735 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400736 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400737 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400738 }
Chris Mason4184ea72009-03-10 12:39:20 -0400739 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400740 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400741}
742
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700743static void add_pinned_bytes(struct btrfs_fs_info *fs_info, s64 num_bytes,
Nikolay Borisov29d2b842018-03-30 12:58:47 +0300744 bool metadata, u64 root_objectid)
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700745{
746 struct btrfs_space_info *space_info;
747 u64 flags;
748
Nikolay Borisov29d2b842018-03-30 12:58:47 +0300749 if (metadata) {
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700750 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
751 flags = BTRFS_BLOCK_GROUP_SYSTEM;
752 else
753 flags = BTRFS_BLOCK_GROUP_METADATA;
754 } else {
755 flags = BTRFS_BLOCK_GROUP_DATA;
756 }
757
758 space_info = __find_space_info(fs_info, flags);
Omar Sandoval55e81962017-06-06 16:45:27 -0700759 ASSERT(space_info);
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700760 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
761}
762
Chris Mason4184ea72009-03-10 12:39:20 -0400763/*
764 * after adding space to the filesystem, we need to clear the full flags
765 * on all the space infos.
766 */
767void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
768{
769 struct list_head *head = &info->space_info;
770 struct btrfs_space_info *found;
771
772 rcu_read_lock();
773 list_for_each_entry_rcu(found, head, list)
774 found->full = 0;
775 rcu_read_unlock();
776}
777
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000778/* simple helper to search for an existing data extent at a given offset */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400779int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400780{
781 int ret;
782 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400783 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400784
Zheng Yan31840ae2008-09-23 13:14:14 -0400785 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700786 if (!path)
787 return -ENOMEM;
788
Chris Masone02119d2008-09-05 16:13:11 -0400789 key.objectid = start;
790 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500791 key.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400792 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400793 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500794 return ret;
795}
796
Chris Masond8d5f3e2007-12-11 12:42:00 -0500797/*
Josef Bacik3173a182013-03-07 14:22:04 -0500798 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400799 *
800 * the head node for delayed ref is used to store the sum of all the
801 * reference count modifications queued up in the rbtree. the head
802 * node may also store the extent flags to set. This way you can check
803 * to see what the reference count and extent flags would be if all of
804 * the delayed refs are not processed.
805 */
806int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400807 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500808 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400809{
810 struct btrfs_delayed_ref_head *head;
811 struct btrfs_delayed_ref_root *delayed_refs;
812 struct btrfs_path *path;
813 struct btrfs_extent_item *ei;
814 struct extent_buffer *leaf;
815 struct btrfs_key key;
816 u32 item_size;
817 u64 num_refs;
818 u64 extent_flags;
819 int ret;
820
Josef Bacik3173a182013-03-07 14:22:04 -0500821 /*
822 * If we don't have skinny metadata, don't bother doing anything
823 * different
824 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400825 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
826 offset = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500827 metadata = 0;
828 }
829
Yan, Zhenga22285a2010-05-16 10:48:46 -0400830 path = btrfs_alloc_path();
831 if (!path)
832 return -ENOMEM;
833
Yan, Zhenga22285a2010-05-16 10:48:46 -0400834 if (!trans) {
835 path->skip_locking = 1;
836 path->search_commit_root = 1;
837 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000838
839search_again:
840 key.objectid = bytenr;
841 key.offset = offset;
842 if (metadata)
843 key.type = BTRFS_METADATA_ITEM_KEY;
844 else
845 key.type = BTRFS_EXTENT_ITEM_KEY;
846
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400847 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400848 if (ret < 0)
849 goto out_free;
850
Josef Bacik3173a182013-03-07 14:22:04 -0500851 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100852 if (path->slots[0]) {
853 path->slots[0]--;
854 btrfs_item_key_to_cpu(path->nodes[0], &key,
855 path->slots[0]);
856 if (key.objectid == bytenr &&
857 key.type == BTRFS_EXTENT_ITEM_KEY &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400858 key.offset == fs_info->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100859 ret = 0;
860 }
Josef Bacik3173a182013-03-07 14:22:04 -0500861 }
862
Yan, Zhenga22285a2010-05-16 10:48:46 -0400863 if (ret == 0) {
864 leaf = path->nodes[0];
865 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
866 if (item_size >= sizeof(*ei)) {
867 ei = btrfs_item_ptr(leaf, path->slots[0],
868 struct btrfs_extent_item);
869 num_refs = btrfs_extent_refs(leaf, ei);
870 extent_flags = btrfs_extent_flags(leaf, ei);
871 } else {
872#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
873 struct btrfs_extent_item_v0 *ei0;
874 BUG_ON(item_size != sizeof(*ei0));
875 ei0 = btrfs_item_ptr(leaf, path->slots[0],
876 struct btrfs_extent_item_v0);
877 num_refs = btrfs_extent_refs_v0(leaf, ei0);
878 /* FIXME: this isn't correct for data */
879 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
880#else
881 BUG();
882#endif
883 }
884 BUG_ON(num_refs == 0);
885 } else {
886 num_refs = 0;
887 extent_flags = 0;
888 ret = 0;
889 }
890
891 if (!trans)
892 goto out;
893
894 delayed_refs = &trans->transaction->delayed_refs;
895 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -0800896 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400897 if (head) {
898 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -0400899 refcount_inc(&head->refs);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400900 spin_unlock(&delayed_refs->lock);
901
David Sterbab3b4aa72011-04-21 01:20:15 +0200902 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400903
David Sterba8cc33e52011-05-02 15:29:25 +0200904 /*
905 * Mutex was contended, block until it's released and try
906 * again
907 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400908 mutex_lock(&head->mutex);
909 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -0400910 btrfs_put_delayed_ref_head(head);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000911 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400912 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500913 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400914 if (head->extent_op && head->extent_op->update_flags)
915 extent_flags |= head->extent_op->flags_to_set;
916 else
917 BUG_ON(num_refs == 0);
918
Josef Bacikd2788502017-09-29 15:43:57 -0400919 num_refs += head->ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500920 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400921 mutex_unlock(&head->mutex);
922 }
923 spin_unlock(&delayed_refs->lock);
924out:
925 WARN_ON(num_refs == 0);
926 if (refs)
927 *refs = num_refs;
928 if (flags)
929 *flags = extent_flags;
930out_free:
931 btrfs_free_path(path);
932 return ret;
933}
934
935/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500936 * Back reference rules. Back refs have three main goals:
937 *
938 * 1) differentiate between all holders of references to an extent so that
939 * when a reference is dropped we can make sure it was a valid reference
940 * before freeing the extent.
941 *
942 * 2) Provide enough information to quickly find the holders of an extent
943 * if we notice a given block is corrupted or bad.
944 *
945 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
946 * maintenance. This is actually the same as #2, but with a slightly
947 * different use case.
948 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949 * There are two kinds of back refs. The implicit back refs is optimized
950 * for pointers in non-shared tree blocks. For a given pointer in a block,
951 * back refs of this kind provide information about the block's owner tree
952 * and the pointer's key. These information allow us to find the block by
953 * b-tree searching. The full back refs is for pointers in tree blocks not
954 * referenced by their owner trees. The location of tree block is recorded
955 * in the back refs. Actually the full back refs is generic, and can be
956 * used in all cases the implicit back refs is used. The major shortcoming
957 * of the full back refs is its overhead. Every time a tree block gets
958 * COWed, we have to update back refs entry for all pointers in it.
959 *
960 * For a newly allocated tree block, we use implicit back refs for
961 * pointers in it. This means most tree related operations only involve
962 * implicit back refs. For a tree block created in old transaction, the
963 * only way to drop a reference to it is COW it. So we can detect the
964 * event that tree block loses its owner tree's reference and do the
965 * back refs conversion.
966 *
Nicholas D Steeves01327612016-05-19 21:18:45 -0400967 * When a tree block is COWed through a tree, there are four cases:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400968 *
969 * The reference count of the block is one and the tree is the block's
970 * owner tree. Nothing to do in this case.
971 *
972 * The reference count of the block is one and the tree is not the
973 * block's owner tree. In this case, full back refs is used for pointers
974 * in the block. Remove these full back refs, add implicit back refs for
975 * every pointers in the new block.
976 *
977 * The reference count of the block is greater than one and the tree is
978 * the block's owner tree. In this case, implicit back refs is used for
979 * pointers in the block. Add full back refs for every pointers in the
980 * block, increase lower level extents' reference counts. The original
981 * implicit back refs are entailed to the new block.
982 *
983 * The reference count of the block is greater than one and the tree is
984 * not the block's owner tree. Add implicit back refs for every pointer in
985 * the new block, increase lower level extents' reference count.
986 *
987 * Back Reference Key composing:
988 *
989 * The key objectid corresponds to the first byte in the extent,
990 * The key type is used to differentiate between types of back refs.
991 * There are different meanings of the key offset for different types
992 * of back refs.
993 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500994 * File extents can be referenced by:
995 *
996 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400997 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500998 * - different offsets inside a file (bookend extents in file.c)
999 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001000 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001001 *
1002 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -05001003 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001004 * - original offset in the file
1005 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -04001006 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001007 * The key offset for the implicit back refs is hash of the first
1008 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001009 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001010 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001011 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001012 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001013 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001014 * The key offset for the implicit back refs is the first byte of
1015 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001016 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001017 * When a file extent is allocated, The implicit back refs is used.
1018 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001019 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001020 * (root_key.objectid, inode objectid, offset in file, 1)
1021 *
1022 * When a file extent is removed file truncation, we find the
1023 * corresponding implicit back refs and check the following fields:
1024 *
1025 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -05001026 *
1027 * Btree extents can be referenced by:
1028 *
1029 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -05001030 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001031 * Both the implicit back refs and the full back refs for tree blocks
1032 * only consist of key. The key offset for the implicit back refs is
1033 * objectid of block's owner tree. The key offset for the full back refs
1034 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001035 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001036 * When implicit back refs is used, information about the lowest key and
1037 * level of the tree block are required. These information are stored in
1038 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001039 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001040
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001041#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1042static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001043 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001044 struct btrfs_path *path,
1045 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -05001046{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001047 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001048 struct btrfs_extent_item *item;
1049 struct btrfs_extent_item_v0 *ei0;
1050 struct btrfs_extent_ref_v0 *ref0;
1051 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -04001052 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001053 struct btrfs_key key;
1054 struct btrfs_key found_key;
1055 u32 new_size = sizeof(*item);
1056 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -05001057 int ret;
1058
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001059 leaf = path->nodes[0];
1060 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -05001061
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001062 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1063 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1064 struct btrfs_extent_item_v0);
1065 refs = btrfs_extent_refs_v0(leaf, ei0);
1066
1067 if (owner == (u64)-1) {
1068 while (1) {
1069 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1070 ret = btrfs_next_leaf(root, path);
1071 if (ret < 0)
1072 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001073 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001074 leaf = path->nodes[0];
1075 }
1076 btrfs_item_key_to_cpu(leaf, &found_key,
1077 path->slots[0]);
1078 BUG_ON(key.objectid != found_key.objectid);
1079 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1080 path->slots[0]++;
1081 continue;
1082 }
1083 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1084 struct btrfs_extent_ref_v0);
1085 owner = btrfs_ref_objectid_v0(leaf, ref0);
1086 break;
1087 }
1088 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001089 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001090
1091 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1092 new_size += sizeof(*bi);
1093
1094 new_size -= sizeof(*ei0);
1095 ret = btrfs_search_slot(trans, root, &key, path,
1096 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001097 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001098 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001099 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001100
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001101 btrfs_extend_item(fs_info, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001102
1103 leaf = path->nodes[0];
1104 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1105 btrfs_set_extent_refs(leaf, item, refs);
1106 /* FIXME: get real generation */
1107 btrfs_set_extent_generation(leaf, item, 0);
1108 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1109 btrfs_set_extent_flags(leaf, item,
1110 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1111 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1112 bi = (struct btrfs_tree_block_info *)(item + 1);
1113 /* FIXME: get first key of the block */
David Sterbab159fa22016-11-08 18:09:03 +01001114 memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001115 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1116 } else {
1117 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1118 }
1119 btrfs_mark_buffer_dirty(leaf);
1120 return 0;
1121}
1122#endif
1123
Liu Bo167ce952017-08-18 15:15:18 -06001124/*
1125 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
1126 * is_data == BTRFS_REF_TYPE_DATA, data type is requried,
1127 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
1128 */
1129int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
1130 struct btrfs_extent_inline_ref *iref,
1131 enum btrfs_inline_ref_type is_data)
1132{
1133 int type = btrfs_extent_inline_ref_type(eb, iref);
Liu Bo64ecdb62017-08-18 15:15:24 -06001134 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
Liu Bo167ce952017-08-18 15:15:18 -06001135
1136 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1137 type == BTRFS_SHARED_BLOCK_REF_KEY ||
1138 type == BTRFS_SHARED_DATA_REF_KEY ||
1139 type == BTRFS_EXTENT_DATA_REF_KEY) {
1140 if (is_data == BTRFS_REF_TYPE_BLOCK) {
Liu Bo64ecdb62017-08-18 15:15:24 -06001141 if (type == BTRFS_TREE_BLOCK_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -06001142 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -06001143 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1144 ASSERT(eb->fs_info);
1145 /*
1146 * Every shared one has parent tree
1147 * block, which must be aligned to
1148 * nodesize.
1149 */
1150 if (offset &&
1151 IS_ALIGNED(offset, eb->fs_info->nodesize))
1152 return type;
1153 }
Liu Bo167ce952017-08-18 15:15:18 -06001154 } else if (is_data == BTRFS_REF_TYPE_DATA) {
Liu Bo64ecdb62017-08-18 15:15:24 -06001155 if (type == BTRFS_EXTENT_DATA_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -06001156 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -06001157 if (type == BTRFS_SHARED_DATA_REF_KEY) {
1158 ASSERT(eb->fs_info);
1159 /*
1160 * Every shared one has parent tree
1161 * block, which must be aligned to
1162 * nodesize.
1163 */
1164 if (offset &&
1165 IS_ALIGNED(offset, eb->fs_info->nodesize))
1166 return type;
1167 }
Liu Bo167ce952017-08-18 15:15:18 -06001168 } else {
1169 ASSERT(is_data == BTRFS_REF_TYPE_ANY);
1170 return type;
1171 }
1172 }
1173
1174 btrfs_print_leaf((struct extent_buffer *)eb);
1175 btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d",
1176 eb->start, type);
1177 WARN_ON(1);
1178
1179 return BTRFS_REF_TYPE_INVALID;
1180}
1181
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001182static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1183{
1184 u32 high_crc = ~(u32)0;
1185 u32 low_crc = ~(u32)0;
1186 __le64 lenum;
1187
1188 lenum = cpu_to_le64(root_objectid);
Nikolay Borisov9678c542018-01-08 11:45:05 +02001189 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001190 lenum = cpu_to_le64(owner);
Nikolay Borisov9678c542018-01-08 11:45:05 +02001191 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001192 lenum = cpu_to_le64(offset);
Nikolay Borisov9678c542018-01-08 11:45:05 +02001193 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001194
1195 return ((u64)high_crc << 31) ^ (u64)low_crc;
1196}
1197
1198static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1199 struct btrfs_extent_data_ref *ref)
1200{
1201 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1202 btrfs_extent_data_ref_objectid(leaf, ref),
1203 btrfs_extent_data_ref_offset(leaf, ref));
1204}
1205
1206static int match_extent_data_ref(struct extent_buffer *leaf,
1207 struct btrfs_extent_data_ref *ref,
1208 u64 root_objectid, u64 owner, u64 offset)
1209{
1210 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1211 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1212 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1213 return 0;
1214 return 1;
1215}
1216
1217static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001218 struct btrfs_path *path,
1219 u64 bytenr, u64 parent,
1220 u64 root_objectid,
1221 u64 owner, u64 offset)
1222{
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +03001223 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001224 struct btrfs_key key;
1225 struct btrfs_extent_data_ref *ref;
1226 struct extent_buffer *leaf;
1227 u32 nritems;
1228 int ret;
1229 int recow;
1230 int err = -ENOENT;
1231
1232 key.objectid = bytenr;
1233 if (parent) {
1234 key.type = BTRFS_SHARED_DATA_REF_KEY;
1235 key.offset = parent;
1236 } else {
1237 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1238 key.offset = hash_extent_data_ref(root_objectid,
1239 owner, offset);
1240 }
1241again:
1242 recow = 0;
1243 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1244 if (ret < 0) {
1245 err = ret;
1246 goto fail;
1247 }
1248
1249 if (parent) {
1250 if (!ret)
1251 return 0;
1252#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1253 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001254 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001255 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1256 if (ret < 0) {
1257 err = ret;
1258 goto fail;
1259 }
1260 if (!ret)
1261 return 0;
1262#endif
1263 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001264 }
1265
1266 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001267 nritems = btrfs_header_nritems(leaf);
1268 while (1) {
1269 if (path->slots[0] >= nritems) {
1270 ret = btrfs_next_leaf(root, path);
1271 if (ret < 0)
1272 err = ret;
1273 if (ret)
1274 goto fail;
1275
1276 leaf = path->nodes[0];
1277 nritems = btrfs_header_nritems(leaf);
1278 recow = 1;
1279 }
1280
1281 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1282 if (key.objectid != bytenr ||
1283 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1284 goto fail;
1285
1286 ref = btrfs_item_ptr(leaf, path->slots[0],
1287 struct btrfs_extent_data_ref);
1288
1289 if (match_extent_data_ref(leaf, ref, root_objectid,
1290 owner, offset)) {
1291 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001292 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001293 goto again;
1294 }
1295 err = 0;
1296 break;
1297 }
1298 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001299 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001300fail:
1301 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001302}
1303
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001304static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001305 struct btrfs_path *path,
1306 u64 bytenr, u64 parent,
1307 u64 root_objectid, u64 owner,
1308 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001309{
Nikolay Borisov62b895a2018-06-20 15:48:44 +03001310 struct btrfs_root *root = trans->fs_info->extent_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04001311 struct btrfs_key key;
1312 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001313 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001314 u32 num_refs;
1315 int ret;
1316
1317 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001318 if (parent) {
1319 key.type = BTRFS_SHARED_DATA_REF_KEY;
1320 key.offset = parent;
1321 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001322 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001323 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1324 key.offset = hash_extent_data_ref(root_objectid,
1325 owner, offset);
1326 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001327 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001328
1329 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1330 if (ret && ret != -EEXIST)
1331 goto fail;
1332
1333 leaf = path->nodes[0];
1334 if (parent) {
1335 struct btrfs_shared_data_ref *ref;
1336 ref = btrfs_item_ptr(leaf, path->slots[0],
1337 struct btrfs_shared_data_ref);
1338 if (ret == 0) {
1339 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1340 } else {
1341 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1342 num_refs += refs_to_add;
1343 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1344 }
1345 } else {
1346 struct btrfs_extent_data_ref *ref;
1347 while (ret == -EEXIST) {
1348 ref = btrfs_item_ptr(leaf, path->slots[0],
1349 struct btrfs_extent_data_ref);
1350 if (match_extent_data_ref(leaf, ref, root_objectid,
1351 owner, offset))
1352 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001353 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001354 key.offset++;
1355 ret = btrfs_insert_empty_item(trans, root, path, &key,
1356 size);
1357 if (ret && ret != -EEXIST)
1358 goto fail;
1359
1360 leaf = path->nodes[0];
1361 }
1362 ref = btrfs_item_ptr(leaf, path->slots[0],
1363 struct btrfs_extent_data_ref);
1364 if (ret == 0) {
1365 btrfs_set_extent_data_ref_root(leaf, ref,
1366 root_objectid);
1367 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1368 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1369 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1370 } else {
1371 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1372 num_refs += refs_to_add;
1373 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1374 }
1375 }
1376 btrfs_mark_buffer_dirty(leaf);
1377 ret = 0;
1378fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001379 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001380 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001381}
1382
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001383static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001384 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -07001385 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001386{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001387 struct btrfs_key key;
1388 struct btrfs_extent_data_ref *ref1 = NULL;
1389 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001390 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001391 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001392 int ret = 0;
1393
1394 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001395 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1396
1397 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1398 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1399 struct btrfs_extent_data_ref);
1400 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1401 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1402 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1403 struct btrfs_shared_data_ref);
1404 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1405#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1406 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1407 struct btrfs_extent_ref_v0 *ref0;
1408 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1409 struct btrfs_extent_ref_v0);
1410 num_refs = btrfs_ref_count_v0(leaf, ref0);
1411#endif
1412 } else {
1413 BUG();
1414 }
1415
Chris Mason56bec292009-03-13 10:10:06 -04001416 BUG_ON(num_refs < refs_to_drop);
1417 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001418
Zheng Yan31840ae2008-09-23 13:14:14 -04001419 if (num_refs == 0) {
Nikolay Borisove9f62902018-06-20 15:48:46 +03001420 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -07001421 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001422 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001423 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1424 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1425 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1426 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1427#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1428 else {
1429 struct btrfs_extent_ref_v0 *ref0;
1430 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1431 struct btrfs_extent_ref_v0);
1432 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1433 }
1434#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001435 btrfs_mark_buffer_dirty(leaf);
1436 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001437 return ret;
1438}
1439
Zhaolei9ed0dea2015-08-06 22:16:24 +08001440static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001441 struct btrfs_extent_inline_ref *iref)
1442{
1443 struct btrfs_key key;
1444 struct extent_buffer *leaf;
1445 struct btrfs_extent_data_ref *ref1;
1446 struct btrfs_shared_data_ref *ref2;
1447 u32 num_refs = 0;
Liu Bo3de28d52017-08-18 15:15:19 -06001448 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001449
1450 leaf = path->nodes[0];
1451 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1452 if (iref) {
Liu Bo3de28d52017-08-18 15:15:19 -06001453 /*
1454 * If type is invalid, we should have bailed out earlier than
1455 * this call.
1456 */
1457 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
1458 ASSERT(type != BTRFS_REF_TYPE_INVALID);
1459 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001460 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1461 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1462 } else {
1463 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1464 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1465 }
1466 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1467 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1468 struct btrfs_extent_data_ref);
1469 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1470 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1471 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1472 struct btrfs_shared_data_ref);
1473 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1474#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1475 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1476 struct btrfs_extent_ref_v0 *ref0;
1477 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1478 struct btrfs_extent_ref_v0);
1479 num_refs = btrfs_ref_count_v0(leaf, ref0);
1480#endif
1481 } else {
1482 WARN_ON(1);
1483 }
1484 return num_refs;
1485}
1486
1487static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001488 struct btrfs_path *path,
1489 u64 bytenr, u64 parent,
1490 u64 root_objectid)
1491{
Nikolay Borisovb8582ee2018-06-20 15:48:50 +03001492 struct btrfs_root *root = trans->fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001493 struct btrfs_key key;
1494 int ret;
1495
1496 key.objectid = bytenr;
1497 if (parent) {
1498 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1499 key.offset = parent;
1500 } else {
1501 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1502 key.offset = root_objectid;
1503 }
1504
1505 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1506 if (ret > 0)
1507 ret = -ENOENT;
1508#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1509 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001510 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001511 key.type = BTRFS_EXTENT_REF_V0_KEY;
1512 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1513 if (ret > 0)
1514 ret = -ENOENT;
1515 }
1516#endif
1517 return ret;
1518}
1519
1520static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001521 struct btrfs_path *path,
1522 u64 bytenr, u64 parent,
1523 u64 root_objectid)
1524{
1525 struct btrfs_key key;
1526 int ret;
1527
1528 key.objectid = bytenr;
1529 if (parent) {
1530 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1531 key.offset = parent;
1532 } else {
1533 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1534 key.offset = root_objectid;
1535 }
1536
Nikolay Borisov10728402018-06-20 15:48:43 +03001537 ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001538 path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001539 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001540 return ret;
1541}
1542
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001543static inline int extent_ref_type(u64 parent, u64 owner)
1544{
1545 int type;
1546 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1547 if (parent > 0)
1548 type = BTRFS_SHARED_BLOCK_REF_KEY;
1549 else
1550 type = BTRFS_TREE_BLOCK_REF_KEY;
1551 } else {
1552 if (parent > 0)
1553 type = BTRFS_SHARED_DATA_REF_KEY;
1554 else
1555 type = BTRFS_EXTENT_DATA_REF_KEY;
1556 }
1557 return type;
1558}
1559
Yan Zheng2c47e6052009-06-27 21:07:35 -04001560static int find_next_key(struct btrfs_path *path, int level,
1561 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001562
1563{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001564 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001565 if (!path->nodes[level])
1566 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001567 if (path->slots[level] + 1 >=
1568 btrfs_header_nritems(path->nodes[level]))
1569 continue;
1570 if (level == 0)
1571 btrfs_item_key_to_cpu(path->nodes[level], key,
1572 path->slots[level] + 1);
1573 else
1574 btrfs_node_key_to_cpu(path->nodes[level], key,
1575 path->slots[level] + 1);
1576 return 0;
1577 }
1578 return 1;
1579}
1580
1581/*
1582 * look for inline back ref. if back ref is found, *ref_ret is set
1583 * to the address of inline back ref, and 0 is returned.
1584 *
1585 * if back ref isn't found, *ref_ret is set to the address where it
1586 * should be inserted, and -ENOENT is returned.
1587 *
1588 * if insert is true and there are too many inline back refs, the path
1589 * points to the extent item, and -EAGAIN is returned.
1590 *
1591 * NOTE: inline back refs are ordered in the same way that back ref
1592 * items in the tree are ordered.
1593 */
1594static noinline_for_stack
1595int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001596 struct btrfs_path *path,
1597 struct btrfs_extent_inline_ref **ref_ret,
1598 u64 bytenr, u64 num_bytes,
1599 u64 parent, u64 root_objectid,
1600 u64 owner, u64 offset, int insert)
1601{
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001602 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001603 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001604 struct btrfs_key key;
1605 struct extent_buffer *leaf;
1606 struct btrfs_extent_item *ei;
1607 struct btrfs_extent_inline_ref *iref;
1608 u64 flags;
1609 u64 item_size;
1610 unsigned long ptr;
1611 unsigned long end;
1612 int extra_size;
1613 int type;
1614 int want;
1615 int ret;
1616 int err = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001617 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Liu Bo3de28d52017-08-18 15:15:19 -06001618 int needed;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001619
1620 key.objectid = bytenr;
1621 key.type = BTRFS_EXTENT_ITEM_KEY;
1622 key.offset = num_bytes;
1623
1624 want = extent_ref_type(parent, owner);
1625 if (insert) {
1626 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001627 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001628 } else
1629 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001630
1631 /*
Nikolay Borisov16d1c062018-06-18 14:59:26 +03001632 * Owner is our level, so we can just add one to get the level for the
1633 * block we are interested in.
Josef Bacik3173a182013-03-07 14:22:04 -05001634 */
1635 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1636 key.type = BTRFS_METADATA_ITEM_KEY;
1637 key.offset = owner;
1638 }
1639
1640again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001641 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1642 if (ret < 0) {
1643 err = ret;
1644 goto out;
1645 }
Josef Bacik3173a182013-03-07 14:22:04 -05001646
1647 /*
1648 * We may be a newly converted file system which still has the old fat
1649 * extent entries for metadata, so try and see if we have one of those.
1650 */
1651 if (ret > 0 && skinny_metadata) {
1652 skinny_metadata = false;
1653 if (path->slots[0]) {
1654 path->slots[0]--;
1655 btrfs_item_key_to_cpu(path->nodes[0], &key,
1656 path->slots[0]);
1657 if (key.objectid == bytenr &&
1658 key.type == BTRFS_EXTENT_ITEM_KEY &&
1659 key.offset == num_bytes)
1660 ret = 0;
1661 }
1662 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +01001663 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05001664 key.type = BTRFS_EXTENT_ITEM_KEY;
1665 key.offset = num_bytes;
1666 btrfs_release_path(path);
1667 goto again;
1668 }
1669 }
1670
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001671 if (ret && !insert) {
1672 err = -ENOENT;
1673 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301674 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -05001675 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -05001676 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001677 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001678
1679 leaf = path->nodes[0];
1680 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1681#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1682 if (item_size < sizeof(*ei)) {
1683 if (!insert) {
1684 err = -ENOENT;
1685 goto out;
1686 }
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001687 ret = convert_extent_item_v0(trans, fs_info, path, owner,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001688 extra_size);
1689 if (ret < 0) {
1690 err = ret;
1691 goto out;
1692 }
1693 leaf = path->nodes[0];
1694 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1695 }
1696#endif
1697 BUG_ON(item_size < sizeof(*ei));
1698
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001699 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1700 flags = btrfs_extent_flags(leaf, ei);
1701
1702 ptr = (unsigned long)(ei + 1);
1703 end = (unsigned long)ei + item_size;
1704
Josef Bacik3173a182013-03-07 14:22:04 -05001705 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001706 ptr += sizeof(struct btrfs_tree_block_info);
1707 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001708 }
1709
Liu Bo3de28d52017-08-18 15:15:19 -06001710 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
1711 needed = BTRFS_REF_TYPE_DATA;
1712 else
1713 needed = BTRFS_REF_TYPE_BLOCK;
1714
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001715 err = -ENOENT;
1716 while (1) {
1717 if (ptr >= end) {
1718 WARN_ON(ptr > end);
1719 break;
1720 }
1721 iref = (struct btrfs_extent_inline_ref *)ptr;
Liu Bo3de28d52017-08-18 15:15:19 -06001722 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
1723 if (type == BTRFS_REF_TYPE_INVALID) {
1724 err = -EINVAL;
1725 goto out;
1726 }
1727
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001728 if (want < type)
1729 break;
1730 if (want > type) {
1731 ptr += btrfs_extent_inline_ref_size(type);
1732 continue;
1733 }
1734
1735 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1736 struct btrfs_extent_data_ref *dref;
1737 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1738 if (match_extent_data_ref(leaf, dref, root_objectid,
1739 owner, offset)) {
1740 err = 0;
1741 break;
1742 }
1743 if (hash_extent_data_ref_item(leaf, dref) <
1744 hash_extent_data_ref(root_objectid, owner, offset))
1745 break;
1746 } else {
1747 u64 ref_offset;
1748 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1749 if (parent > 0) {
1750 if (parent == ref_offset) {
1751 err = 0;
1752 break;
1753 }
1754 if (ref_offset < parent)
1755 break;
1756 } else {
1757 if (root_objectid == ref_offset) {
1758 err = 0;
1759 break;
1760 }
1761 if (ref_offset < root_objectid)
1762 break;
1763 }
1764 }
1765 ptr += btrfs_extent_inline_ref_size(type);
1766 }
1767 if (err == -ENOENT && insert) {
1768 if (item_size + extra_size >=
1769 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1770 err = -EAGAIN;
1771 goto out;
1772 }
1773 /*
1774 * To add new inline back ref, we have to make sure
1775 * there is no corresponding back ref item.
1776 * For simplicity, we just do not add new inline back
1777 * ref if there is any kind of item for this block
1778 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001779 if (find_next_key(path, 0, &key) == 0 &&
1780 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001781 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001782 err = -EAGAIN;
1783 goto out;
1784 }
1785 }
1786 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1787out:
Yan Zheng85d41982009-06-11 08:51:10 -04001788 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001789 path->keep_locks = 0;
1790 btrfs_unlock_up_safe(path, 1);
1791 }
1792 return err;
1793}
1794
1795/*
1796 * helper to add new inline back ref
1797 */
1798static noinline_for_stack
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001799void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001800 struct btrfs_path *path,
1801 struct btrfs_extent_inline_ref *iref,
1802 u64 parent, u64 root_objectid,
1803 u64 owner, u64 offset, int refs_to_add,
1804 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001805{
1806 struct extent_buffer *leaf;
1807 struct btrfs_extent_item *ei;
1808 unsigned long ptr;
1809 unsigned long end;
1810 unsigned long item_offset;
1811 u64 refs;
1812 int size;
1813 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001814
1815 leaf = path->nodes[0];
1816 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1817 item_offset = (unsigned long)iref - (unsigned long)ei;
1818
1819 type = extent_ref_type(parent, owner);
1820 size = btrfs_extent_inline_ref_size(type);
1821
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001822 btrfs_extend_item(fs_info, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001823
1824 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1825 refs = btrfs_extent_refs(leaf, ei);
1826 refs += refs_to_add;
1827 btrfs_set_extent_refs(leaf, ei, refs);
1828 if (extent_op)
1829 __run_delayed_extent_op(extent_op, leaf, ei);
1830
1831 ptr = (unsigned long)ei + item_offset;
1832 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1833 if (ptr < end - size)
1834 memmove_extent_buffer(leaf, ptr + size, ptr,
1835 end - size - ptr);
1836
1837 iref = (struct btrfs_extent_inline_ref *)ptr;
1838 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1839 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1840 struct btrfs_extent_data_ref *dref;
1841 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1842 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1843 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1844 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1845 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1846 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1847 struct btrfs_shared_data_ref *sref;
1848 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1849 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1850 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1851 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1852 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1853 } else {
1854 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1855 }
1856 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001857}
1858
1859static int lookup_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001860 struct btrfs_path *path,
1861 struct btrfs_extent_inline_ref **ref_ret,
1862 u64 bytenr, u64 num_bytes, u64 parent,
1863 u64 root_objectid, u64 owner, u64 offset)
1864{
1865 int ret;
1866
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001867 ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
1868 num_bytes, parent, root_objectid,
1869 owner, offset, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870 if (ret != -ENOENT)
1871 return ret;
1872
David Sterbab3b4aa72011-04-21 01:20:15 +02001873 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001874 *ref_ret = NULL;
1875
1876 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Nikolay Borisovb8582ee2018-06-20 15:48:50 +03001877 ret = lookup_tree_block_ref(trans, path, bytenr, parent,
1878 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001879 } else {
Nikolay Borisovbd1d53e2018-06-20 15:48:51 +03001880 ret = lookup_extent_data_ref(trans, path, bytenr, parent,
1881 root_objectid, owner, offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001882 }
1883 return ret;
1884}
1885
1886/*
1887 * helper to update/remove inline back ref
1888 */
1889static noinline_for_stack
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001890void update_inline_extent_backref(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001891 struct btrfs_extent_inline_ref *iref,
1892 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001893 struct btrfs_delayed_extent_op *extent_op,
1894 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001895{
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001896 struct extent_buffer *leaf = path->nodes[0];
1897 struct btrfs_fs_info *fs_info = leaf->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001898 struct btrfs_extent_item *ei;
1899 struct btrfs_extent_data_ref *dref = NULL;
1900 struct btrfs_shared_data_ref *sref = NULL;
1901 unsigned long ptr;
1902 unsigned long end;
1903 u32 item_size;
1904 int size;
1905 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001906 u64 refs;
1907
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001908 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1909 refs = btrfs_extent_refs(leaf, ei);
1910 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1911 refs += refs_to_mod;
1912 btrfs_set_extent_refs(leaf, ei, refs);
1913 if (extent_op)
1914 __run_delayed_extent_op(extent_op, leaf, ei);
1915
Liu Bo3de28d52017-08-18 15:15:19 -06001916 /*
1917 * If type is invalid, we should have bailed out after
1918 * lookup_inline_extent_backref().
1919 */
1920 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1921 ASSERT(type != BTRFS_REF_TYPE_INVALID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001922
1923 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1924 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1925 refs = btrfs_extent_data_ref_count(leaf, dref);
1926 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1927 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1928 refs = btrfs_shared_data_ref_count(leaf, sref);
1929 } else {
1930 refs = 1;
1931 BUG_ON(refs_to_mod != -1);
1932 }
1933
1934 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1935 refs += refs_to_mod;
1936
1937 if (refs > 0) {
1938 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1939 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1940 else
1941 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1942 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001943 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001944 size = btrfs_extent_inline_ref_size(type);
1945 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1946 ptr = (unsigned long)iref;
1947 end = (unsigned long)ei + item_size;
1948 if (ptr + size < end)
1949 memmove_extent_buffer(leaf, ptr, ptr + size,
1950 end - ptr - size);
1951 item_size -= size;
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001952 btrfs_truncate_item(fs_info, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001953 }
1954 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001955}
1956
1957static noinline_for_stack
1958int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001959 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001960 struct btrfs_path *path,
1961 u64 bytenr, u64 num_bytes, u64 parent,
1962 u64 root_objectid, u64 owner,
1963 u64 offset, int refs_to_add,
1964 struct btrfs_delayed_extent_op *extent_op)
1965{
1966 struct btrfs_extent_inline_ref *iref;
1967 int ret;
1968
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001969 ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
1970 num_bytes, parent, root_objectid,
1971 owner, offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001972 if (ret == 0) {
1973 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001974 update_inline_extent_backref(path, iref, refs_to_add,
1975 extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001976 } else if (ret == -ENOENT) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001977 setup_inline_extent_backref(fs_info, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001978 root_objectid, owner, offset,
1979 refs_to_add, extent_op);
1980 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001981 }
1982 return ret;
1983}
1984
1985static int insert_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001986 struct btrfs_path *path,
1987 u64 bytenr, u64 parent, u64 root_objectid,
1988 u64 owner, u64 offset, int refs_to_add)
1989{
1990 int ret;
1991 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1992 BUG_ON(refs_to_add != 1);
Nikolay Borisov10728402018-06-20 15:48:43 +03001993 ret = insert_tree_block_ref(trans, path, bytenr, parent,
1994 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001995 } else {
Nikolay Borisov62b895a2018-06-20 15:48:44 +03001996 ret = insert_extent_data_ref(trans, path, bytenr, parent,
1997 root_objectid, owner, offset,
1998 refs_to_add);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001999 }
2000 return ret;
2001}
2002
2003static int remove_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002004 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002005 struct btrfs_path *path,
2006 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07002007 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002008{
Jeff Mahoney143bede2012-03-01 14:56:26 +01002009 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002010
2011 BUG_ON(!is_data && refs_to_drop != 1);
2012 if (iref) {
Nikolay Borisov61a18f12018-06-20 15:48:49 +03002013 update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
2014 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002015 } else if (is_data) {
Nikolay Borisove9f62902018-06-20 15:48:46 +03002016 ret = remove_extent_data_ref(trans, path, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07002017 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002018 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07002019 *last_ref = 1;
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002020 ret = btrfs_del_item(trans, fs_info->extent_root, path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002021 }
2022 return ret;
2023}
2024
Jeff Mahoney86557862015-06-15 09:41:16 -04002025#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002026static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
2027 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05002028{
Jeff Mahoney86557862015-06-15 09:41:16 -04002029 int j, ret = 0;
2030 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002031 u64 aligned_start = ALIGN(start, 1 << 9);
2032
2033 if (WARN_ON(start != aligned_start)) {
2034 len -= aligned_start - start;
2035 len = round_down(len, 1 << 9);
2036 start = aligned_start;
2037 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002038
2039 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04002040
2041 if (!len)
2042 return 0;
2043
2044 end = start + len;
2045 bytes_left = len;
2046
2047 /* Skip any superblocks on this device. */
2048 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
2049 u64 sb_start = btrfs_sb_offset(j);
2050 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
2051 u64 size = sb_start - start;
2052
2053 if (!in_range(sb_start, start, bytes_left) &&
2054 !in_range(sb_end, start, bytes_left) &&
2055 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
2056 continue;
2057
2058 /*
2059 * Superblock spans beginning of range. Adjust start and
2060 * try again.
2061 */
2062 if (sb_start <= start) {
2063 start += sb_end - start;
2064 if (start > end) {
2065 bytes_left = 0;
2066 break;
2067 }
2068 bytes_left = end - start;
2069 continue;
2070 }
2071
2072 if (size) {
2073 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
2074 GFP_NOFS, 0);
2075 if (!ret)
2076 *discarded_bytes += size;
2077 else if (ret != -EOPNOTSUPP)
2078 return ret;
2079 }
2080
2081 start = sb_end;
2082 if (start > end) {
2083 bytes_left = 0;
2084 break;
2085 }
2086 bytes_left = end - start;
2087 }
2088
2089 if (bytes_left) {
2090 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002091 GFP_NOFS, 0);
2092 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04002093 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002094 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002095 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05002096}
Chris Mason15916de2008-11-19 21:17:22 -05002097
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002098int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00002099 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05002100{
Liu Hui1f3c79a2009-01-05 15:57:51 -05002101 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00002102 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02002103 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002104
Christoph Hellwige244a0a2009-10-14 09:24:59 -04002105
Filipe Manana29992412016-05-27 17:42:05 +01002106 /*
2107 * Avoid races with device replace and make sure our bbio has devices
2108 * associated to its stripes that don't go away while we are discarding.
2109 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002110 btrfs_bio_counter_inc_blocked(fs_info);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002111 /* Tell the block device(s) that the sectors can be discarded */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002112 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
2113 &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002114 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05002115 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02002116 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002117 int i;
2118
Liu Hui1f3c79a2009-01-05 15:57:51 -05002119
Jan Schmidta1d3c472011-08-04 17:15:33 +02002120 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002121 u64 bytes;
Anand Jain38b5f682017-11-29 18:53:43 +08002122 struct request_queue *req_q;
2123
Filipe Manana627e0872018-01-30 18:40:22 +00002124 if (!stripe->dev->bdev) {
2125 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
2126 continue;
2127 }
Anand Jain38b5f682017-11-29 18:53:43 +08002128 req_q = bdev_get_queue(stripe->dev->bdev);
2129 if (!blk_queue_discard(req_q))
Josef Bacikd5e20032011-08-04 14:52:27 +00002130 continue;
2131
Li Dongyang5378e602011-03-24 10:24:27 +00002132 ret = btrfs_issue_discard(stripe->dev->bdev,
2133 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002134 stripe->length,
2135 &bytes);
Li Dongyang5378e602011-03-24 10:24:27 +00002136 if (!ret)
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002137 discarded_bytes += bytes;
Li Dongyang5378e602011-03-24 10:24:27 +00002138 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002139 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00002140
2141 /*
2142 * Just in case we get back EOPNOTSUPP for some reason,
2143 * just ignore the return value so we don't screw up
2144 * people calling discard_extent.
2145 */
2146 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002147 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08002148 btrfs_put_bbio(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002149 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002150 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00002151
2152 if (actual_bytes)
2153 *actual_bytes = discarded_bytes;
2154
Liu Hui1f3c79a2009-01-05 15:57:51 -05002155
David Woodhouse53b381b2013-01-29 18:40:14 -05002156 if (ret == -EOPNOTSUPP)
2157 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002158 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002159}
2160
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002161/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002162int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002163 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002164 u64 bytenr, u64 num_bytes, u64 parent,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002165 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04002166{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002167 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07002168 int old_ref_mod, new_ref_mod;
Zheng Yan31840ae2008-09-23 13:14:14 -04002169 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002170
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002171 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
2172 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04002173
Josef Bacikfd708b82017-09-29 15:43:50 -04002174 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent, root_objectid,
2175 owner, offset, BTRFS_ADD_DELAYED_REF);
2176
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002177 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Nikolay Borisov44e1c472018-06-20 15:48:53 +03002178 ret = btrfs_add_delayed_tree_ref(trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07002179 num_bytes, parent,
2180 root_objectid, (int)owner,
2181 BTRFS_ADD_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07002182 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002183 } else {
Nikolay Borisov88a979c2018-06-20 15:48:54 +03002184 ret = btrfs_add_delayed_data_ref(trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07002185 num_bytes, parent,
2186 root_objectid, owner, offset,
Omar Sandovald7eae342017-06-06 16:45:31 -07002187 0, BTRFS_ADD_DELAYED_REF,
2188 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002189 }
Omar Sandovald7eae342017-06-06 16:45:31 -07002190
Nikolay Borisov29d2b842018-03-30 12:58:47 +03002191 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0) {
2192 bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
2193
2194 add_pinned_bytes(fs_info, -num_bytes, metadata, root_objectid);
2195 }
Omar Sandovald7eae342017-06-06 16:45:31 -07002196
Zheng Yan31840ae2008-09-23 13:14:14 -04002197 return ret;
2198}
2199
Nikolay Borisovbd3c6852018-06-18 14:59:25 +03002200/*
2201 * __btrfs_inc_extent_ref - insert backreference for a given extent
2202 *
2203 * @trans: Handle of transaction
2204 *
2205 * @node: The delayed ref node used to get the bytenr/length for
2206 * extent whose references are incremented.
2207 *
2208 * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
2209 * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
2210 * bytenr of the parent block. Since new extents are always
2211 * created with indirect references, this will only be the case
2212 * when relocating a shared extent. In that case, root_objectid
2213 * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
2214 * be 0
2215 *
2216 * @root_objectid: The id of the root where this modification has originated,
2217 * this can be either one of the well-known metadata trees or
2218 * the subvolume id which references this extent.
2219 *
2220 * @owner: For data extents it is the inode number of the owning file.
2221 * For metadata extents this parameter holds the level in the
2222 * tree of the extent.
2223 *
2224 * @offset: For metadata extents the offset is ignored and is currently
2225 * always passed as 0. For data extents it is the fileoffset
2226 * this extent belongs to.
2227 *
2228 * @refs_to_add Number of references to add
2229 *
2230 * @extent_op Pointer to a structure, holding information necessary when
2231 * updating a tree block's flags
2232 *
2233 */
Chris Mason925baed2008-06-25 16:01:30 -04002234static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002235 struct btrfs_fs_info *fs_info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002236 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002237 u64 parent, u64 root_objectid,
2238 u64 owner, u64 offset, int refs_to_add,
2239 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04002240{
Chris Mason5caf2a02007-04-02 11:20:42 -04002241 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002242 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04002243 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07002244 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002245 u64 bytenr = node->bytenr;
2246 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002247 u64 refs;
2248 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05002249
Chris Mason5caf2a02007-04-02 11:20:42 -04002250 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002251 if (!path)
2252 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04002253
David Sterbae4058b52015-11-27 16:31:35 +01002254 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002255 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002256 /* this will setup the path even if it fails to insert the back ref */
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002257 ret = insert_inline_extent_backref(trans, fs_info, path, bytenr,
2258 num_bytes, parent, root_objectid,
2259 owner, offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002260 refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08002261 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002262 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07002263
2264 /*
2265 * Ok we had -EAGAIN which means we didn't have space to insert and
2266 * inline extent ref, so just update the reference count and add a
2267 * normal backref.
2268 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002269 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07002270 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002271 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2272 refs = btrfs_extent_refs(leaf, item);
2273 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2274 if (extent_op)
2275 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002276
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002277 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002278 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002279
David Sterbae4058b52015-11-27 16:31:35 +01002280 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002281 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04002282 /* now insert the actual backref */
Nikolay Borisov37593412018-06-20 15:48:45 +03002283 ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid,
2284 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002285 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04002286 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002287out:
Chris Mason74493f72007-12-11 09:25:06 -05002288 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08002289 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002290}
2291
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002292static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002293 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002294 struct btrfs_delayed_ref_node *node,
2295 struct btrfs_delayed_extent_op *extent_op,
2296 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002297{
Chris Mason56bec292009-03-13 10:10:06 -04002298 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002299 struct btrfs_delayed_data_ref *ref;
2300 struct btrfs_key ins;
2301 u64 parent = 0;
2302 u64 ref_root = 0;
2303 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002304
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002305 ins.objectid = node->bytenr;
2306 ins.offset = node->num_bytes;
2307 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002308
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002309 ref = btrfs_delayed_node_to_data_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002310 trace_run_delayed_data_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002311
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002312 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2313 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002314 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002315
2316 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002317 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002318 flags |= extent_op->flags_to_set;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002319 ret = alloc_reserved_file_extent(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002320 parent, ref_root, flags,
2321 ref->objectid, ref->offset,
2322 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002323 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002324 ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002325 ref_root, ref->objectid,
2326 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002327 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002328 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03002329 ret = __btrfs_free_extent(trans, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002330 ref_root, ref->objectid,
2331 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002332 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002333 } else {
2334 BUG();
2335 }
Chris Mason56bec292009-03-13 10:10:06 -04002336 return ret;
2337}
2338
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002339static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2340 struct extent_buffer *leaf,
2341 struct btrfs_extent_item *ei)
2342{
2343 u64 flags = btrfs_extent_flags(leaf, ei);
2344 if (extent_op->update_flags) {
2345 flags |= extent_op->flags_to_set;
2346 btrfs_set_extent_flags(leaf, ei, flags);
2347 }
2348
2349 if (extent_op->update_key) {
2350 struct btrfs_tree_block_info *bi;
2351 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2352 bi = (struct btrfs_tree_block_info *)(ei + 1);
2353 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2354 }
2355}
2356
2357static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002358 struct btrfs_fs_info *fs_info,
Josef Bacikd2788502017-09-29 15:43:57 -04002359 struct btrfs_delayed_ref_head *head,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002360 struct btrfs_delayed_extent_op *extent_op)
2361{
2362 struct btrfs_key key;
2363 struct btrfs_path *path;
2364 struct btrfs_extent_item *ei;
2365 struct extent_buffer *leaf;
2366 u32 item_size;
2367 int ret;
2368 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002369 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002370
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002371 if (trans->aborted)
2372 return 0;
2373
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002374 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05002375 metadata = 0;
2376
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002377 path = btrfs_alloc_path();
2378 if (!path)
2379 return -ENOMEM;
2380
Josef Bacikd2788502017-09-29 15:43:57 -04002381 key.objectid = head->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002382
Josef Bacik3173a182013-03-07 14:22:04 -05002383 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002384 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002385 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002386 } else {
2387 key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacikd2788502017-09-29 15:43:57 -04002388 key.offset = head->num_bytes;
Josef Bacik3173a182013-03-07 14:22:04 -05002389 }
2390
2391again:
David Sterbae4058b52015-11-27 16:31:35 +01002392 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002393 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002394 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002395 if (ret < 0) {
2396 err = ret;
2397 goto out;
2398 }
2399 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002400 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002401 if (path->slots[0] > 0) {
2402 path->slots[0]--;
2403 btrfs_item_key_to_cpu(path->nodes[0], &key,
2404 path->slots[0]);
Josef Bacikd2788502017-09-29 15:43:57 -04002405 if (key.objectid == head->bytenr &&
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002406 key.type == BTRFS_EXTENT_ITEM_KEY &&
Josef Bacikd2788502017-09-29 15:43:57 -04002407 key.offset == head->num_bytes)
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002408 ret = 0;
2409 }
2410 if (ret > 0) {
2411 btrfs_release_path(path);
2412 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002413
Josef Bacikd2788502017-09-29 15:43:57 -04002414 key.objectid = head->bytenr;
2415 key.offset = head->num_bytes;
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002416 key.type = BTRFS_EXTENT_ITEM_KEY;
2417 goto again;
2418 }
2419 } else {
2420 err = -EIO;
2421 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002422 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002423 }
2424
2425 leaf = path->nodes[0];
2426 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2427#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2428 if (item_size < sizeof(*ei)) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002429 ret = convert_extent_item_v0(trans, fs_info, path, (u64)-1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002430 if (ret < 0) {
2431 err = ret;
2432 goto out;
2433 }
2434 leaf = path->nodes[0];
2435 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2436 }
2437#endif
2438 BUG_ON(item_size < sizeof(*ei));
2439 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2440 __run_delayed_extent_op(extent_op, leaf, ei);
2441
2442 btrfs_mark_buffer_dirty(leaf);
2443out:
2444 btrfs_free_path(path);
2445 return err;
2446}
2447
2448static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002449 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002450 struct btrfs_delayed_ref_node *node,
2451 struct btrfs_delayed_extent_op *extent_op,
2452 int insert_reserved)
2453{
2454 int ret = 0;
2455 struct btrfs_delayed_tree_ref *ref;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002456 u64 parent = 0;
2457 u64 ref_root = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002458
2459 ref = btrfs_delayed_node_to_tree_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002460 trace_run_delayed_tree_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002461
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002462 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2463 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002464 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002465
Liu Bo02794222016-09-14 19:19:05 -07002466 if (node->ref_mod != 1) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002467 btrfs_err(fs_info,
Liu Bo02794222016-09-14 19:19:05 -07002468 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
2469 node->bytenr, node->ref_mod, node->action, ref_root,
2470 parent);
2471 return -EIO;
2472 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002473 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002474 BUG_ON(!extent_op || !extent_op->update_flags);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03002475 ret = alloc_reserved_tree_block(trans, node, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002476 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002477 ret = __btrfs_inc_extent_ref(trans, fs_info, node,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002478 parent, ref_root,
2479 ref->level, 0, 1,
Josef Bacikfcebe452014-05-13 17:30:47 -07002480 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002481 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Nikolay Borisove72cb922018-06-20 15:48:57 +03002482 ret = __btrfs_free_extent(trans, node, parent, ref_root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002483 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002484 } else {
2485 BUG();
2486 }
2487 return ret;
2488}
2489
Chris Mason56bec292009-03-13 10:10:06 -04002490/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002491static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002492 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002493 struct btrfs_delayed_ref_node *node,
2494 struct btrfs_delayed_extent_op *extent_op,
2495 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002496{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002497 int ret = 0;
2498
Josef Bacik857cc2f2013-10-07 15:21:08 -04002499 if (trans->aborted) {
2500 if (insert_reserved)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002501 btrfs_pin_extent(fs_info, node->bytenr,
Josef Bacik857cc2f2013-10-07 15:21:08 -04002502 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002503 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002504 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002505
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002506 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2507 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002508 ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002509 insert_reserved);
2510 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2511 node->type == BTRFS_SHARED_DATA_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002512 ret = run_delayed_data_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002513 insert_reserved);
2514 else
2515 BUG();
2516 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002517}
2518
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002519static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04002520select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002521{
Filipe Mananacffc3372015-07-09 13:13:44 +01002522 struct btrfs_delayed_ref_node *ref;
2523
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002524 if (RB_EMPTY_ROOT(&head->ref_tree))
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002525 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002526
Filipe Mananacffc3372015-07-09 13:13:44 +01002527 /*
2528 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2529 * This is to prevent a ref count from going down to zero, which deletes
2530 * the extent item from the extent tree, when there still are references
2531 * to add, which would fail because they would not find the extent item.
2532 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002533 if (!list_empty(&head->ref_add_list))
2534 return list_first_entry(&head->ref_add_list,
2535 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01002536
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002537 ref = rb_entry(rb_first(&head->ref_tree),
2538 struct btrfs_delayed_ref_node, ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002539 ASSERT(list_empty(&ref->add_list));
2540 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04002541}
2542
Josef Bacik2eadaa22017-09-29 15:43:52 -04002543static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
2544 struct btrfs_delayed_ref_head *head)
2545{
2546 spin_lock(&delayed_refs->lock);
2547 head->processing = 0;
2548 delayed_refs->num_heads_ready++;
2549 spin_unlock(&delayed_refs->lock);
2550 btrfs_delayed_ref_unlock(head);
2551}
2552
Josef Bacikb00e6252017-09-29 15:43:53 -04002553static int cleanup_extent_op(struct btrfs_trans_handle *trans,
2554 struct btrfs_fs_info *fs_info,
2555 struct btrfs_delayed_ref_head *head)
2556{
2557 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
2558 int ret;
2559
2560 if (!extent_op)
2561 return 0;
2562 head->extent_op = NULL;
2563 if (head->must_insert_reserved) {
2564 btrfs_free_delayed_extent_op(extent_op);
2565 return 0;
2566 }
2567 spin_unlock(&head->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002568 ret = run_delayed_extent_op(trans, fs_info, head, extent_op);
Josef Bacikb00e6252017-09-29 15:43:53 -04002569 btrfs_free_delayed_extent_op(extent_op);
2570 return ret ? ret : 1;
2571}
2572
Josef Bacik194ab0b2017-09-29 15:43:54 -04002573static int cleanup_ref_head(struct btrfs_trans_handle *trans,
2574 struct btrfs_fs_info *fs_info,
2575 struct btrfs_delayed_ref_head *head)
2576{
2577 struct btrfs_delayed_ref_root *delayed_refs;
2578 int ret;
2579
2580 delayed_refs = &trans->transaction->delayed_refs;
2581
2582 ret = cleanup_extent_op(trans, fs_info, head);
2583 if (ret < 0) {
2584 unselect_delayed_ref_head(delayed_refs, head);
2585 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
2586 return ret;
2587 } else if (ret) {
2588 return ret;
2589 }
2590
2591 /*
2592 * Need to drop our head ref lock and re-acquire the delayed ref lock
2593 * and then re-check to make sure nobody got added.
2594 */
2595 spin_unlock(&head->lock);
2596 spin_lock(&delayed_refs->lock);
2597 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002598 if (!RB_EMPTY_ROOT(&head->ref_tree) || head->extent_op) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04002599 spin_unlock(&head->lock);
2600 spin_unlock(&delayed_refs->lock);
2601 return 1;
2602 }
Josef Bacik194ab0b2017-09-29 15:43:54 -04002603 delayed_refs->num_heads--;
2604 rb_erase(&head->href_node, &delayed_refs->href_root);
Josef Bacikd2788502017-09-29 15:43:57 -04002605 RB_CLEAR_NODE(&head->href_node);
Josef Bacikc1103f72017-09-29 15:43:56 -04002606 spin_unlock(&head->lock);
Nikolay Borisov1e7a1422018-04-11 11:21:18 +03002607 spin_unlock(&delayed_refs->lock);
Josef Bacikc1103f72017-09-29 15:43:56 -04002608 atomic_dec(&delayed_refs->num_entries);
2609
Josef Bacikd2788502017-09-29 15:43:57 -04002610 trace_run_delayed_ref_head(fs_info, head, 0);
Josef Bacikc1103f72017-09-29 15:43:56 -04002611
2612 if (head->total_ref_mod < 0) {
Nikolay Borisov5e388e92018-04-18 09:41:54 +03002613 struct btrfs_space_info *space_info;
2614 u64 flags;
Josef Bacikc1103f72017-09-29 15:43:56 -04002615
Nikolay Borisov5e388e92018-04-18 09:41:54 +03002616 if (head->is_data)
2617 flags = BTRFS_BLOCK_GROUP_DATA;
2618 else if (head->is_system)
2619 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2620 else
2621 flags = BTRFS_BLOCK_GROUP_METADATA;
2622 space_info = __find_space_info(fs_info, flags);
2623 ASSERT(space_info);
2624 percpu_counter_add(&space_info->total_bytes_pinned,
Josef Bacikd2788502017-09-29 15:43:57 -04002625 -head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04002626
2627 if (head->is_data) {
2628 spin_lock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002629 delayed_refs->pending_csums -= head->num_bytes;
Josef Bacikc1103f72017-09-29 15:43:56 -04002630 spin_unlock(&delayed_refs->lock);
2631 }
2632 }
2633
2634 if (head->must_insert_reserved) {
Josef Bacikd2788502017-09-29 15:43:57 -04002635 btrfs_pin_extent(fs_info, head->bytenr,
2636 head->num_bytes, 1);
Josef Bacikc1103f72017-09-29 15:43:56 -04002637 if (head->is_data) {
Josef Bacikd2788502017-09-29 15:43:57 -04002638 ret = btrfs_del_csums(trans, fs_info, head->bytenr,
2639 head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04002640 }
2641 }
2642
2643 /* Also free its reserved qgroup space */
2644 btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
2645 head->qgroup_reserved);
2646 btrfs_delayed_ref_unlock(head);
Josef Bacikd2788502017-09-29 15:43:57 -04002647 btrfs_put_delayed_ref_head(head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002648 return 0;
2649}
2650
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002651/*
2652 * Returns 0 on success or if called with an already aborted transaction.
2653 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2654 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002655static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacikd7df2c72014-01-23 09:21:38 -05002656 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002657{
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002658 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002659 struct btrfs_delayed_ref_root *delayed_refs;
2660 struct btrfs_delayed_ref_node *ref;
2661 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002662 struct btrfs_delayed_extent_op *extent_op;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002663 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002664 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002665 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002666 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002667 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002668
2669 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002670 while (1) {
2671 if (!locked_ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002672 if (count >= nr)
Chris Mason56bec292009-03-13 10:10:06 -04002673 break;
Chris Mason56bec292009-03-13 10:10:06 -04002674
Josef Bacikd7df2c72014-01-23 09:21:38 -05002675 spin_lock(&delayed_refs->lock);
2676 locked_ref = btrfs_select_ref_head(trans);
2677 if (!locked_ref) {
2678 spin_unlock(&delayed_refs->lock);
2679 break;
2680 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002681
2682 /* grab the lock that says we are going to process
2683 * all the refs for this head */
2684 ret = btrfs_delayed_ref_lock(trans, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002685 spin_unlock(&delayed_refs->lock);
Chris Masonc3e69d52009-03-13 10:17:05 -04002686 /*
2687 * we may have dropped the spin lock to get the head
2688 * mutex lock, and that might have given someone else
2689 * time to free the head. If that's true, it has been
2690 * removed from our list and we can move on.
2691 */
2692 if (ret == -EAGAIN) {
2693 locked_ref = NULL;
2694 count++;
2695 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002696 }
2697 }
2698
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002699 /*
2700 * We need to try and merge add/drops of the same ref since we
2701 * can run into issues with relocate dropping the implicit ref
2702 * and then it being added back again before the drop can
2703 * finish. If we merged anything we need to re-loop so we can
2704 * get a good ref.
2705 * Or we can get node references of the same type that weren't
2706 * merged when created due to bumps in the tree mod seq, and
2707 * we need to merge them to prevent adding an inline extent
2708 * backref before dropping it (triggering a BUG_ON at
2709 * insert_inline_extent_backref()).
2710 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002711 spin_lock(&locked_ref->lock);
Nikolay Borisovbe97f132018-04-19 11:06:39 +03002712 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002713
Arne Jansend1270cd2011-09-13 15:16:43 +02002714 ref = select_delayed_ref(locked_ref);
2715
2716 if (ref && ref->seq &&
Nikolay Borisov41d0bd32018-04-04 15:57:42 +03002717 btrfs_check_delayed_seq(fs_info, ref->seq)) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002718 spin_unlock(&locked_ref->lock);
Josef Bacik2eadaa22017-09-29 15:43:52 -04002719 unselect_delayed_ref_head(delayed_refs, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002720 locked_ref = NULL;
Arne Jansend1270cd2011-09-13 15:16:43 +02002721 cond_resched();
Josef Bacik27a377d2014-02-07 13:57:59 -05002722 count++;
Arne Jansend1270cd2011-09-13 15:16:43 +02002723 continue;
2724 }
2725
Josef Bacikc1103f72017-09-29 15:43:56 -04002726 /*
2727 * We're done processing refs in this ref_head, clean everything
2728 * up and move on to the next ref_head.
2729 */
Chris Mason56bec292009-03-13 10:10:06 -04002730 if (!ref) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04002731 ret = cleanup_ref_head(trans, fs_info, locked_ref);
2732 if (ret > 0 ) {
Josef Bacikb00e6252017-09-29 15:43:53 -04002733 /* We dropped our lock, we need to loop. */
2734 ret = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002735 continue;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002736 } else if (ret) {
2737 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002738 }
Josef Bacikc1103f72017-09-29 15:43:56 -04002739 locked_ref = NULL;
2740 count++;
2741 continue;
2742 }
Chris Mason56bec292009-03-13 10:10:06 -04002743
Josef Bacikc1103f72017-09-29 15:43:56 -04002744 actual_count++;
2745 ref->in_tree = 0;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002746 rb_erase(&ref->ref_node, &locked_ref->ref_tree);
2747 RB_CLEAR_NODE(&ref->ref_node);
Josef Bacikc1103f72017-09-29 15:43:56 -04002748 if (!list_empty(&ref->add_list))
2749 list_del(&ref->add_list);
2750 /*
2751 * When we play the delayed ref, also correct the ref_mod on
2752 * head
2753 */
2754 switch (ref->action) {
2755 case BTRFS_ADD_DELAYED_REF:
2756 case BTRFS_ADD_DELAYED_EXTENT:
Josef Bacikd2788502017-09-29 15:43:57 -04002757 locked_ref->ref_mod -= ref->ref_mod;
Josef Bacikc1103f72017-09-29 15:43:56 -04002758 break;
2759 case BTRFS_DROP_DELAYED_REF:
Josef Bacikd2788502017-09-29 15:43:57 -04002760 locked_ref->ref_mod += ref->ref_mod;
Josef Bacikc1103f72017-09-29 15:43:56 -04002761 break;
2762 default:
2763 WARN_ON(1);
Arne Jansen22cd2e72012-08-09 00:16:53 -06002764 }
Josef Bacik1ce7a5e2017-09-29 15:43:55 -04002765 atomic_dec(&delayed_refs->num_entries);
2766
Josef Bacikb00e6252017-09-29 15:43:53 -04002767 /*
2768 * Record the must-insert_reserved flag before we drop the spin
2769 * lock.
2770 */
2771 must_insert_reserved = locked_ref->must_insert_reserved;
2772 locked_ref->must_insert_reserved = 0;
2773
2774 extent_op = locked_ref->extent_op;
2775 locked_ref->extent_op = NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002776 spin_unlock(&locked_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002777
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002778 ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002779 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002780
Miao Xie78a61842012-11-21 02:21:28 +00002781 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002782 if (ret) {
Josef Bacik2eadaa22017-09-29 15:43:52 -04002783 unselect_delayed_ref_head(delayed_refs, locked_ref);
Miao Xie093486c2012-12-19 08:10:10 +00002784 btrfs_put_delayed_ref(ref);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002785 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
2786 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002787 return ret;
2788 }
2789
Miao Xie093486c2012-12-19 08:10:10 +00002790 btrfs_put_delayed_ref(ref);
2791 count++;
Chris Mason1887be62009-03-13 10:11:24 -04002792 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002793 }
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002794
2795 /*
2796 * We don't want to include ref heads since we can have empty ref heads
2797 * and those will drastically skew our runtime down since we just do
2798 * accounting, no actual extent tree updates.
2799 */
2800 if (actual_count > 0) {
2801 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2802 u64 avg;
2803
2804 /*
2805 * We weigh the current average higher than our current runtime
2806 * to avoid large swings in the average.
2807 */
2808 spin_lock(&delayed_refs->lock);
2809 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002810 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002811 spin_unlock(&delayed_refs->lock);
2812 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002813 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002814}
2815
Arne Jansen709c0482011-09-12 12:22:57 +02002816#ifdef SCRAMBLE_DELAYED_REFS
2817/*
2818 * Normally delayed refs get processed in ascending bytenr order. This
2819 * correlates in most cases to the order added. To expose dependencies on this
2820 * order, we start to process the tree in the middle instead of the beginning
2821 */
2822static u64 find_middle(struct rb_root *root)
2823{
2824 struct rb_node *n = root->rb_node;
2825 struct btrfs_delayed_ref_node *entry;
2826 int alt = 1;
2827 u64 middle;
2828 u64 first = 0, last = 0;
2829
2830 n = rb_first(root);
2831 if (n) {
2832 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2833 first = entry->bytenr;
2834 }
2835 n = rb_last(root);
2836 if (n) {
2837 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2838 last = entry->bytenr;
2839 }
2840 n = root->rb_node;
2841
2842 while (n) {
2843 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2844 WARN_ON(!entry->in_tree);
2845
2846 middle = entry->bytenr;
2847
2848 if (alt)
2849 n = n->rb_left;
2850 else
2851 n = n->rb_right;
2852
2853 alt = 1 - alt;
2854 }
2855 return middle;
2856}
2857#endif
2858
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002859static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002860{
2861 u64 num_bytes;
2862
2863 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2864 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002865 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002866 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2867
2868 /*
2869 * 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 -04002870 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002871 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002872 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002873}
2874
Josef Bacik12621332015-02-03 07:50:16 -08002875/*
2876 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2877 * would require to store the csums for that many bytes.
2878 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002879u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002880{
2881 u64 csum_size;
2882 u64 num_csums_per_leaf;
2883 u64 num_csums;
2884
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002885 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002886 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002887 (u64)btrfs_super_csum_size(fs_info->super_copy));
2888 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002889 num_csums += num_csums_per_leaf - 1;
2890 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2891 return num_csums;
2892}
2893
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002894int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002895 struct btrfs_fs_info *fs_info)
Josef Bacik1be41b72013-06-12 13:56:06 -04002896{
2897 struct btrfs_block_rsv *global_rsv;
2898 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
Josef Bacik12621332015-02-03 07:50:16 -08002899 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
David Sterba165c8b02017-11-08 02:12:57 +01002900 unsigned int num_dirty_bgs = trans->transaction->num_dirty_bgs;
Josef Bacikcb723e42015-02-18 08:06:57 -08002901 u64 num_bytes, num_dirty_bgs_bytes;
Josef Bacik1be41b72013-06-12 13:56:06 -04002902 int ret = 0;
2903
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002904 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002905 num_heads = heads_to_leaves(fs_info, num_heads);
Josef Bacik1be41b72013-06-12 13:56:06 -04002906 if (num_heads > 1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002907 num_bytes += (num_heads - 1) * fs_info->nodesize;
Josef Bacik1be41b72013-06-12 13:56:06 -04002908 num_bytes <<= 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002909 num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
2910 fs_info->nodesize;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002911 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
Josef Bacikcb723e42015-02-18 08:06:57 -08002912 num_dirty_bgs);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002913 global_rsv = &fs_info->global_block_rsv;
Josef Bacik1be41b72013-06-12 13:56:06 -04002914
2915 /*
2916 * If we can't allocate any more chunks lets make sure we have _lots_ of
2917 * wiggle room since running delayed refs can create more delayed refs.
2918 */
Josef Bacikcb723e42015-02-18 08:06:57 -08002919 if (global_rsv->space_info->full) {
2920 num_dirty_bgs_bytes <<= 1;
Josef Bacik1be41b72013-06-12 13:56:06 -04002921 num_bytes <<= 1;
Josef Bacikcb723e42015-02-18 08:06:57 -08002922 }
Josef Bacik1be41b72013-06-12 13:56:06 -04002923
2924 spin_lock(&global_rsv->lock);
Josef Bacikcb723e42015-02-18 08:06:57 -08002925 if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
Josef Bacik1be41b72013-06-12 13:56:06 -04002926 ret = 1;
2927 spin_unlock(&global_rsv->lock);
2928 return ret;
2929}
2930
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002931int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002932 struct btrfs_fs_info *fs_info)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002933{
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002934 u64 num_entries =
2935 atomic_read(&trans->transaction->delayed_refs.num_entries);
2936 u64 avg_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002937 u64 val;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002938
2939 smp_mb();
2940 avg_runtime = fs_info->avg_delayed_ref_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002941 val = num_entries * avg_runtime;
Wang Xiaoguangdc1a90c2016-10-26 15:23:01 +08002942 if (val >= NSEC_PER_SEC)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002943 return 1;
Chris Masona79b7d42014-05-22 16:18:52 -07002944 if (val >= NSEC_PER_SEC / 2)
2945 return 2;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002946
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002947 return btrfs_check_space_for_delayed_refs(trans, fs_info);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002948}
2949
Chris Masona79b7d42014-05-22 16:18:52 -07002950struct async_delayed_refs {
2951 struct btrfs_root *root;
Josef Bacik31b96552016-04-11 17:37:40 -04002952 u64 transid;
Chris Masona79b7d42014-05-22 16:18:52 -07002953 int count;
2954 int error;
2955 int sync;
2956 struct completion wait;
2957 struct btrfs_work work;
2958};
2959
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002960static inline struct async_delayed_refs *
2961to_async_delayed_refs(struct btrfs_work *work)
2962{
2963 return container_of(work, struct async_delayed_refs, work);
2964}
2965
Chris Masona79b7d42014-05-22 16:18:52 -07002966static void delayed_ref_async_start(struct btrfs_work *work)
2967{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002968 struct async_delayed_refs *async = to_async_delayed_refs(work);
Chris Masona79b7d42014-05-22 16:18:52 -07002969 struct btrfs_trans_handle *trans;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002970 struct btrfs_fs_info *fs_info = async->root->fs_info;
Chris Masona79b7d42014-05-22 16:18:52 -07002971 int ret;
2972
Chris Mason0f873ec2016-04-27 09:59:38 -04002973 /* if the commit is already started, we don't need to wait here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002974 if (btrfs_transaction_blocked(fs_info))
Josef Bacik31b96552016-04-11 17:37:40 -04002975 goto done;
Josef Bacik31b96552016-04-11 17:37:40 -04002976
Chris Mason0f873ec2016-04-27 09:59:38 -04002977 trans = btrfs_join_transaction(async->root);
2978 if (IS_ERR(trans)) {
2979 async->error = PTR_ERR(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07002980 goto done;
2981 }
2982
2983 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002984 * trans->sync means that when we call end_transaction, we won't
Chris Masona79b7d42014-05-22 16:18:52 -07002985 * wait on delayed refs
2986 */
2987 trans->sync = true;
Chris Mason0f873ec2016-04-27 09:59:38 -04002988
2989 /* Don't bother flushing if we got into a different transaction */
2990 if (trans->transid > async->transid)
2991 goto end;
2992
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002993 ret = btrfs_run_delayed_refs(trans, async->count);
Chris Masona79b7d42014-05-22 16:18:52 -07002994 if (ret)
2995 async->error = ret;
Chris Mason0f873ec2016-04-27 09:59:38 -04002996end:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002997 ret = btrfs_end_transaction(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07002998 if (ret && !async->error)
2999 async->error = ret;
3000done:
3001 if (async->sync)
3002 complete(&async->wait);
3003 else
3004 kfree(async);
3005}
3006
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003007int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
Josef Bacik31b96552016-04-11 17:37:40 -04003008 unsigned long count, u64 transid, int wait)
Chris Masona79b7d42014-05-22 16:18:52 -07003009{
3010 struct async_delayed_refs *async;
3011 int ret;
3012
3013 async = kmalloc(sizeof(*async), GFP_NOFS);
3014 if (!async)
3015 return -ENOMEM;
3016
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003017 async->root = fs_info->tree_root;
Chris Masona79b7d42014-05-22 16:18:52 -07003018 async->count = count;
3019 async->error = 0;
Josef Bacik31b96552016-04-11 17:37:40 -04003020 async->transid = transid;
Chris Masona79b7d42014-05-22 16:18:52 -07003021 if (wait)
3022 async->sync = 1;
3023 else
3024 async->sync = 0;
3025 init_completion(&async->wait);
3026
Liu Bo9e0af232014-08-15 23:36:53 +08003027 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
3028 delayed_ref_async_start, NULL, NULL);
Chris Masona79b7d42014-05-22 16:18:52 -07003029
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003030 btrfs_queue_work(fs_info->extent_workers, &async->work);
Chris Masona79b7d42014-05-22 16:18:52 -07003031
3032 if (wait) {
3033 wait_for_completion(&async->wait);
3034 ret = async->error;
3035 kfree(async);
3036 return ret;
3037 }
3038 return 0;
3039}
3040
Chris Masonc3e69d52009-03-13 10:17:05 -04003041/*
3042 * this starts processing the delayed reference count updates and
3043 * extent insertions we have queued up so far. count can be
3044 * 0, which means to process everything in the tree at the start
3045 * of the run (but not newly added entries), or it can be some target
3046 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003047 *
3048 * Returns 0 on success or if called with an aborted transaction
3049 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04003050 */
3051int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003052 unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04003053{
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003054 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -04003055 struct rb_node *node;
3056 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08003057 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04003058 int ret;
3059 int run_all = count == (unsigned long)-1;
Filipe Mananad9a05402015-10-03 13:13:13 +01003060 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Chris Masonc3e69d52009-03-13 10:17:05 -04003061
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003062 /* We'll clean this up in btrfs_cleanup_transaction */
3063 if (trans->aborted)
3064 return 0;
3065
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003066 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08003067 return 0;
3068
Chris Masonc3e69d52009-03-13 10:17:05 -04003069 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08003070 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05003071 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05003072
Chris Masonc3e69d52009-03-13 10:17:05 -04003073again:
Arne Jansen709c0482011-09-12 12:22:57 +02003074#ifdef SCRAMBLE_DELAYED_REFS
3075 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
3076#endif
Filipe Mananad9a05402015-10-03 13:13:13 +01003077 trans->can_flush_pending_bgs = false;
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02003078 ret = __btrfs_run_delayed_refs(trans, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003079 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003080 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003081 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04003082 }
3083
Chris Mason56bec292009-03-13 10:10:06 -04003084 if (run_all) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05003085 if (!list_empty(&trans->new_bgs))
Nikolay Borisov6c686b32018-02-07 17:55:40 +02003086 btrfs_create_pending_block_groups(trans);
Josef Bacikea658ba2012-09-11 16:57:25 -04003087
Josef Bacikd7df2c72014-01-23 09:21:38 -05003088 spin_lock(&delayed_refs->lock);
Liu Boc46effa2013-10-14 12:59:45 +08003089 node = rb_first(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003090 if (!node) {
3091 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04003092 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05003093 }
Josef Bacikd2788502017-09-29 15:43:57 -04003094 head = rb_entry(node, struct btrfs_delayed_ref_head,
3095 href_node);
3096 refcount_inc(&head->refs);
Chris Mason56bec292009-03-13 10:10:06 -04003097 spin_unlock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04003098
3099 /* Mutex was contended, block until it's released and retry. */
3100 mutex_lock(&head->mutex);
3101 mutex_unlock(&head->mutex);
3102
3103 btrfs_put_delayed_ref_head(head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003104 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04003105 goto again;
3106 }
Chris Mason54aa1f42007-06-22 14:16:25 -04003107out:
Filipe Mananad9a05402015-10-03 13:13:13 +01003108 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Chris Masona28ec192007-03-06 20:08:01 -05003109 return 0;
3110}
3111
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003112int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003113 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003114 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04003115 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003116{
3117 struct btrfs_delayed_extent_op *extent_op;
3118 int ret;
3119
Miao Xie78a61842012-11-21 02:21:28 +00003120 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003121 if (!extent_op)
3122 return -ENOMEM;
3123
3124 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01003125 extent_op->update_flags = true;
3126 extent_op->update_key = false;
3127 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04003128 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003129
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003130 ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003131 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003132 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00003133 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003134 return ret;
3135}
3136
Liu Boe4c3b2d2017-01-30 12:25:28 -08003137static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003138 struct btrfs_path *path,
3139 u64 objectid, u64 offset, u64 bytenr)
3140{
3141 struct btrfs_delayed_ref_head *head;
3142 struct btrfs_delayed_ref_node *ref;
3143 struct btrfs_delayed_data_ref *data_ref;
3144 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08003145 struct btrfs_transaction *cur_trans;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04003146 struct rb_node *node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003147 int ret = 0;
3148
ethanwu998ac6d2018-04-29 15:59:42 +08003149 spin_lock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08003150 cur_trans = root->fs_info->running_transaction;
ethanwu998ac6d2018-04-29 15:59:42 +08003151 if (cur_trans)
3152 refcount_inc(&cur_trans->use_count);
3153 spin_unlock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08003154 if (!cur_trans)
3155 return 0;
3156
3157 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003158 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003159 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003160 if (!head) {
3161 spin_unlock(&delayed_refs->lock);
ethanwu998ac6d2018-04-29 15:59:42 +08003162 btrfs_put_transaction(cur_trans);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003163 return 0;
3164 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003165
3166 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04003167 refcount_inc(&head->refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003168 spin_unlock(&delayed_refs->lock);
3169
David Sterbab3b4aa72011-04-21 01:20:15 +02003170 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003171
David Sterba8cc33e52011-05-02 15:29:25 +02003172 /*
3173 * Mutex was contended, block until it's released and let
3174 * caller try again
3175 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003176 mutex_lock(&head->mutex);
3177 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04003178 btrfs_put_delayed_ref_head(head);
ethanwu998ac6d2018-04-29 15:59:42 +08003179 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003180 return -EAGAIN;
3181 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003182 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003183
3184 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04003185 /*
3186 * XXX: We should replace this with a proper search function in the
3187 * future.
3188 */
3189 for (node = rb_first(&head->ref_tree); node; node = rb_next(node)) {
3190 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003191 /* If it's a shared ref we know a cross reference exists */
3192 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
3193 ret = 1;
3194 break;
3195 }
3196
3197 data_ref = btrfs_delayed_node_to_data_ref(ref);
3198
3199 /*
3200 * If our ref doesn't match the one we're currently looking at
3201 * then we have a cross reference.
3202 */
3203 if (data_ref->root != root->root_key.objectid ||
3204 data_ref->objectid != objectid ||
3205 data_ref->offset != offset) {
3206 ret = 1;
3207 break;
3208 }
3209 }
3210 spin_unlock(&head->lock);
3211 mutex_unlock(&head->mutex);
ethanwu998ac6d2018-04-29 15:59:42 +08003212 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003213 return ret;
3214}
3215
Liu Boe4c3b2d2017-01-30 12:25:28 -08003216static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003217 struct btrfs_path *path,
3218 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05003219{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003220 struct btrfs_fs_info *fs_info = root->fs_info;
3221 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04003222 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003223 struct btrfs_extent_data_ref *ref;
3224 struct btrfs_extent_inline_ref *iref;
3225 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05003226 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003227 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06003228 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04003229 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05003230
Chris Masonbe20aa92007-12-17 20:14:01 -05003231 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04003232 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04003233 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05003234
Chris Masonbe20aa92007-12-17 20:14:01 -05003235 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3236 if (ret < 0)
3237 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003238 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04003239
3240 ret = -ENOENT;
3241 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04003242 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003243
Zheng Yan31840ae2008-09-23 13:14:14 -04003244 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04003245 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003246 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05003247
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003248 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05003249 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003250
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003251 ret = 1;
3252 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3253#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3254 if (item_size < sizeof(*ei)) {
3255 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3256 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003257 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003258#endif
3259 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
3260
3261 if (item_size != sizeof(*ei) +
3262 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
3263 goto out;
3264
3265 if (btrfs_extent_generation(leaf, ei) <=
3266 btrfs_root_last_snapshot(&root->root_item))
3267 goto out;
3268
3269 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
Liu Bo3de28d52017-08-18 15:15:19 -06003270
3271 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
3272 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003273 goto out;
3274
3275 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3276 if (btrfs_extent_refs(leaf, ei) !=
3277 btrfs_extent_data_ref_count(leaf, ref) ||
3278 btrfs_extent_data_ref_root(leaf, ref) !=
3279 root->root_key.objectid ||
3280 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3281 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3282 goto out;
3283
Yan Zhengf321e492008-07-30 09:26:11 -04003284 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05003285out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003286 return ret;
3287}
3288
Liu Boe4c3b2d2017-01-30 12:25:28 -08003289int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
3290 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003291{
3292 struct btrfs_path *path;
3293 int ret;
3294 int ret2;
3295
3296 path = btrfs_alloc_path();
3297 if (!path)
Su Yue9132c4f2018-05-30 14:49:10 +08003298 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003299
3300 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08003301 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003302 offset, bytenr);
3303 if (ret && ret != -ENOENT)
3304 goto out;
3305
Liu Boe4c3b2d2017-01-30 12:25:28 -08003306 ret2 = check_delayed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003307 offset, bytenr);
3308 } while (ret2 == -EAGAIN);
3309
3310 if (ret2 && ret2 != -ENOENT) {
3311 ret = ret2;
3312 goto out;
3313 }
3314
3315 if (ret != -ENOENT || ret2 != -ENOENT)
3316 ret = 0;
3317out:
Yan Zhengf321e492008-07-30 09:26:11 -04003318 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003319 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3320 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04003321 return ret;
3322}
3323
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003324static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05003325 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003326 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07003327 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04003328{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003329 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04003330 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003331 u64 num_bytes;
3332 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003333 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04003334 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003335 struct btrfs_key key;
3336 struct btrfs_file_extent_item *fi;
3337 int i;
3338 int level;
3339 int ret = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003340 int (*process_func)(struct btrfs_trans_handle *,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003341 struct btrfs_root *,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003342 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04003343
David Sterbafccb84c2014-09-29 23:53:21 +02003344
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003345 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003346 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003347
Zheng Yan31840ae2008-09-23 13:14:14 -04003348 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003349 nritems = btrfs_header_nritems(buf);
3350 level = btrfs_header_level(buf);
3351
Miao Xie27cdeb72014-04-02 19:51:05 +08003352 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003353 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003354
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003355 if (inc)
3356 process_func = btrfs_inc_extent_ref;
3357 else
3358 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003359
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003360 if (full_backref)
3361 parent = buf->start;
3362 else
3363 parent = 0;
3364
Zheng Yan31840ae2008-09-23 13:14:14 -04003365 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003366 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003367 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02003368 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003369 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003370 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003371 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003372 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003373 BTRFS_FILE_EXTENT_INLINE)
3374 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003375 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3376 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003377 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003378
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003379 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3380 key.offset -= btrfs_file_extent_offset(buf, fi);
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003381 ret = process_func(trans, root, bytenr, num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003382 parent, ref_root, key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003383 key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003384 if (ret)
3385 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003386 } else {
3387 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003388 num_bytes = fs_info->nodesize;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003389 ret = process_func(trans, root, bytenr, num_bytes,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003390 parent, ref_root, level - 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003391 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003392 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003393 }
3394 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003395 return 0;
3396fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003397 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003398}
3399
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003400int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003401 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04003402{
Josef Bacike339a6b2014-07-02 10:54:25 -07003403 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003404}
Zheng Yan31840ae2008-09-23 13:14:14 -04003405
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003406int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003407 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003408{
Josef Bacike339a6b2014-07-02 10:54:25 -07003409 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003410}
3411
Chris Mason9078a3e2007-04-26 16:46:15 -04003412static int write_one_cache_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003413 struct btrfs_fs_info *fs_info,
Chris Mason9078a3e2007-04-26 16:46:15 -04003414 struct btrfs_path *path,
3415 struct btrfs_block_group_cache *cache)
3416{
3417 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003418 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003419 unsigned long bi;
3420 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003421
Chris Mason9078a3e2007-04-26 16:46:15 -04003422 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003423 if (ret) {
3424 if (ret > 0)
3425 ret = -ENOENT;
Chris Mason54aa1f42007-06-22 14:16:25 -04003426 goto fail;
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003427 }
Chris Mason5f39d392007-10-15 16:14:19 -04003428
3429 leaf = path->nodes[0];
3430 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3431 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3432 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -04003433fail:
Filipe Manana24b89d02015-04-25 18:31:05 +01003434 btrfs_release_path(path);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003435 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003436
3437}
3438
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003439static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003440next_block_group(struct btrfs_fs_info *fs_info,
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003441 struct btrfs_block_group_cache *cache)
3442{
3443 struct rb_node *node;
Filipe Manana292cbd52014-11-26 15:28:50 +00003444
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003445 spin_lock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003446
3447 /* If our block group was removed, we need a full search. */
3448 if (RB_EMPTY_NODE(&cache->cache_node)) {
3449 const u64 next_bytenr = cache->key.objectid + cache->key.offset;
3450
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003451 spin_unlock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003452 btrfs_put_block_group(cache);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003453 cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
Filipe Manana292cbd52014-11-26 15:28:50 +00003454 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003455 node = rb_next(&cache->cache_node);
3456 btrfs_put_block_group(cache);
3457 if (node) {
3458 cache = rb_entry(node, struct btrfs_block_group_cache,
3459 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003460 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003461 } else
3462 cache = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003463 spin_unlock(&fs_info->block_group_cache_lock);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003464 return cache;
3465}
3466
Josef Bacik0af3d002010-06-21 14:48:16 -04003467static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3468 struct btrfs_trans_handle *trans,
3469 struct btrfs_path *path)
3470{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003471 struct btrfs_fs_info *fs_info = block_group->fs_info;
3472 struct btrfs_root *root = fs_info->tree_root;
Josef Bacik0af3d002010-06-21 14:48:16 -04003473 struct inode *inode = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08003474 struct extent_changeset *data_reserved = NULL;
Josef Bacik0af3d002010-06-21 14:48:16 -04003475 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003476 int dcs = BTRFS_DC_ERROR;
David Sterbaf8c269d2015-01-16 17:21:12 +01003477 u64 num_pages = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003478 int retries = 0;
3479 int ret = 0;
3480
3481 /*
3482 * If this block group is smaller than 100 megs don't bother caching the
3483 * block group.
3484 */
Byongho Leeee221842015-12-15 01:42:10 +09003485 if (block_group->key.offset < (100 * SZ_1M)) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003486 spin_lock(&block_group->lock);
3487 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3488 spin_unlock(&block_group->lock);
3489 return 0;
3490 }
3491
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003492 if (trans->aborted)
3493 return 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003494again:
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003495 inode = lookup_free_space_inode(fs_info, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003496 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3497 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003498 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003499 goto out;
3500 }
3501
3502 if (IS_ERR(inode)) {
3503 BUG_ON(retries);
3504 retries++;
3505
3506 if (block_group->ro)
3507 goto out_free;
3508
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003509 ret = create_free_space_inode(fs_info, trans, block_group,
3510 path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003511 if (ret)
3512 goto out_free;
3513 goto again;
3514 }
3515
3516 /*
3517 * We want to set the generation to 0, that way if anything goes wrong
3518 * from here on out we know not to trust this cache when we load up next
3519 * time.
3520 */
3521 BTRFS_I(inode)->generation = 0;
3522 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003523 if (ret) {
3524 /*
3525 * So theoretically we could recover from this, simply set the
3526 * super cache generation to 0 so we know to invalidate the
3527 * cache, but then we'd have to keep track of the block groups
3528 * that fail this way so we know we _have_ to reset this cache
3529 * before the next commit or risk reading stale cache. So to
3530 * limit our exposure to horrible edge cases lets just abort the
3531 * transaction, this only happens in really bad situations
3532 * anyway.
3533 */
Jeff Mahoney66642832016-06-10 18:19:25 -04003534 btrfs_abort_transaction(trans, ret);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003535 goto out_put;
3536 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003537 WARN_ON(ret);
3538
Josef Bacik8e138e02017-11-17 14:50:46 -05003539 /* We've already setup this transaction, go ahead and exit */
3540 if (block_group->cache_generation == trans->transid &&
3541 i_size_read(inode)) {
3542 dcs = BTRFS_DC_SETUP;
3543 goto out_put;
3544 }
3545
Josef Bacik0af3d002010-06-21 14:48:16 -04003546 if (i_size_read(inode) > 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003547 ret = btrfs_check_trunc_cache_free_space(fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003548 &fs_info->global_block_rsv);
Miao Xie7b61cd92013-05-13 13:55:09 +00003549 if (ret)
3550 goto out_put;
3551
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003552 ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003553 if (ret)
3554 goto out_put;
3555 }
3556
3557 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003558 if (block_group->cached != BTRFS_CACHE_FINISHED ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003559 !btrfs_test_opt(fs_info, SPACE_CACHE)) {
Liu Bocf7c1ef2012-07-06 03:31:34 -06003560 /*
3561 * don't bother trying to write stuff out _if_
3562 * a) we're not cached,
Liu Bo1a79c1f2017-03-06 13:49:02 -08003563 * b) we're with nospace_cache mount option,
3564 * c) we're with v2 space_cache (FREE_SPACE_TREE).
Liu Bocf7c1ef2012-07-06 03:31:34 -06003565 */
Josef Bacik2b209822010-12-03 13:17:53 -05003566 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003567 spin_unlock(&block_group->lock);
3568 goto out_put;
3569 }
3570 spin_unlock(&block_group->lock);
3571
Josef Bacik6fc823b2012-08-06 13:46:38 -06003572 /*
Josef Bacik2968b1f2015-10-01 12:55:18 -04003573 * We hit an ENOSPC when setting up the cache in this transaction, just
3574 * skip doing the setup, we've already cleared the cache so we're safe.
3575 */
3576 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
3577 ret = -ENOSPC;
3578 goto out_put;
3579 }
3580
3581 /*
Josef Bacik6fc823b2012-08-06 13:46:38 -06003582 * Try to preallocate enough space based on how big the block group is.
3583 * Keep in mind this has to include any pinned space which could end up
3584 * taking up quite a bit since it's not folded into the other space
3585 * cache.
3586 */
Byongho Leeee221842015-12-15 01:42:10 +09003587 num_pages = div_u64(block_group->key.offset, SZ_256M);
Josef Bacik0af3d002010-06-21 14:48:16 -04003588 if (!num_pages)
3589 num_pages = 1;
3590
Josef Bacik0af3d002010-06-21 14:48:16 -04003591 num_pages *= 16;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003592 num_pages *= PAGE_SIZE;
Josef Bacik0af3d002010-06-21 14:48:16 -04003593
Qu Wenruo364ecf32017-02-27 15:10:38 +08003594 ret = btrfs_check_data_free_space(inode, &data_reserved, 0, num_pages);
Josef Bacik0af3d002010-06-21 14:48:16 -04003595 if (ret)
3596 goto out_put;
3597
3598 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3599 num_pages, num_pages,
3600 &alloc_hint);
Josef Bacik2968b1f2015-10-01 12:55:18 -04003601 /*
3602 * Our cache requires contiguous chunks so that we don't modify a bunch
3603 * of metadata or split extents when writing the cache out, which means
3604 * we can enospc if we are heavily fragmented in addition to just normal
3605 * out of space conditions. So if we hit this just skip setting up any
3606 * other block groups for this transaction, maybe we'll unpin enough
3607 * space the next time around.
3608 */
Josef Bacik2b209822010-12-03 13:17:53 -05003609 if (!ret)
3610 dcs = BTRFS_DC_SETUP;
Josef Bacik2968b1f2015-10-01 12:55:18 -04003611 else if (ret == -ENOSPC)
3612 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
Josef Bacikc09544e2011-08-30 10:19:10 -04003613
Josef Bacik0af3d002010-06-21 14:48:16 -04003614out_put:
3615 iput(inode);
3616out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003617 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003618out:
3619 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003620 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003621 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003622 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003623 spin_unlock(&block_group->lock);
3624
Qu Wenruo364ecf32017-02-27 15:10:38 +08003625 extent_changeset_free(data_reserved);
Josef Bacik0af3d002010-06-21 14:48:16 -04003626 return ret;
3627}
3628
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003629int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003630 struct btrfs_fs_info *fs_info)
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003631{
3632 struct btrfs_block_group_cache *cache, *tmp;
3633 struct btrfs_transaction *cur_trans = trans->transaction;
3634 struct btrfs_path *path;
3635
3636 if (list_empty(&cur_trans->dirty_bgs) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003637 !btrfs_test_opt(fs_info, SPACE_CACHE))
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003638 return 0;
3639
3640 path = btrfs_alloc_path();
3641 if (!path)
3642 return -ENOMEM;
3643
3644 /* Could add new block groups, use _safe just in case */
3645 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
3646 dirty_list) {
3647 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3648 cache_save_setup(cache, trans, path);
3649 }
3650
3651 btrfs_free_path(path);
3652 return 0;
3653}
3654
Chris Mason1bbc6212015-04-06 12:46:08 -07003655/*
3656 * transaction commit does final block group cache writeback during a
3657 * critical section where nothing is allowed to change the FS. This is
3658 * required in order for the cache to actually match the block group,
3659 * but can introduce a lot of latency into the commit.
3660 *
3661 * So, btrfs_start_dirty_block_groups is here to kick off block group
3662 * cache IO. There's a chance we'll have to redo some of it if the
3663 * block group changes again during the commit, but it greatly reduces
3664 * the commit latency by getting rid of the easy block groups while
3665 * we're still allowing others to join the commit.
3666 */
Nikolay Borisov21217052018-02-07 17:55:41 +02003667int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
Chris Mason1bbc6212015-04-06 12:46:08 -07003668{
Nikolay Borisov21217052018-02-07 17:55:41 +02003669 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason1bbc6212015-04-06 12:46:08 -07003670 struct btrfs_block_group_cache *cache;
3671 struct btrfs_transaction *cur_trans = trans->transaction;
3672 int ret = 0;
3673 int should_put;
3674 struct btrfs_path *path = NULL;
3675 LIST_HEAD(dirty);
3676 struct list_head *io = &cur_trans->io_bgs;
3677 int num_started = 0;
3678 int loops = 0;
3679
3680 spin_lock(&cur_trans->dirty_bgs_lock);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003681 if (list_empty(&cur_trans->dirty_bgs)) {
3682 spin_unlock(&cur_trans->dirty_bgs_lock);
3683 return 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003684 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003685 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Chris Mason1bbc6212015-04-06 12:46:08 -07003686 spin_unlock(&cur_trans->dirty_bgs_lock);
3687
3688again:
Chris Mason1bbc6212015-04-06 12:46:08 -07003689 /*
3690 * make sure all the block groups on our dirty list actually
3691 * exist
3692 */
Nikolay Borisov6c686b32018-02-07 17:55:40 +02003693 btrfs_create_pending_block_groups(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07003694
3695 if (!path) {
3696 path = btrfs_alloc_path();
3697 if (!path)
3698 return -ENOMEM;
3699 }
3700
Filipe Mananab58d1a92015-04-25 18:29:16 +01003701 /*
3702 * cache_write_mutex is here only to save us from balance or automatic
3703 * removal of empty block groups deleting this block group while we are
3704 * writing out the cache
3705 */
3706 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003707 while (!list_empty(&dirty)) {
3708 cache = list_first_entry(&dirty,
3709 struct btrfs_block_group_cache,
3710 dirty_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07003711 /*
3712 * this can happen if something re-dirties a block
3713 * group that is already under IO. Just wait for it to
3714 * finish and then do it all again
3715 */
3716 if (!list_empty(&cache->io_list)) {
3717 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003718 btrfs_wait_cache_io(trans, cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003719 btrfs_put_block_group(cache);
3720 }
3721
3722
3723 /*
3724 * btrfs_wait_cache_io uses the cache->dirty_list to decide
3725 * if it should update the cache_state. Don't delete
3726 * until after we wait.
3727 *
3728 * Since we're not running in the commit critical section
3729 * we need the dirty_bgs_lock to protect from update_block_group
3730 */
3731 spin_lock(&cur_trans->dirty_bgs_lock);
3732 list_del_init(&cache->dirty_list);
3733 spin_unlock(&cur_trans->dirty_bgs_lock);
3734
3735 should_put = 1;
3736
3737 cache_save_setup(cache, trans, path);
3738
3739 if (cache->disk_cache_state == BTRFS_DC_SETUP) {
3740 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003741 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003742 cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003743 if (ret == 0 && cache->io_ctl.inode) {
3744 num_started++;
3745 should_put = 0;
3746
3747 /*
Nikolay Borisov45ae2c12018-02-08 18:25:18 +02003748 * The cache_write_mutex is protecting the
3749 * io_list, also refer to the definition of
3750 * btrfs_transaction::io_bgs for more details
Chris Mason1bbc6212015-04-06 12:46:08 -07003751 */
3752 list_add_tail(&cache->io_list, io);
3753 } else {
3754 /*
3755 * if we failed to write the cache, the
3756 * generation will be bad and life goes on
3757 */
3758 ret = 0;
3759 }
3760 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003761 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003762 ret = write_one_cache_group(trans, fs_info,
3763 path, cache);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003764 /*
3765 * Our block group might still be attached to the list
3766 * of new block groups in the transaction handle of some
3767 * other task (struct btrfs_trans_handle->new_bgs). This
3768 * means its block group item isn't yet in the extent
3769 * tree. If this happens ignore the error, as we will
3770 * try again later in the critical section of the
3771 * transaction commit.
3772 */
3773 if (ret == -ENOENT) {
3774 ret = 0;
3775 spin_lock(&cur_trans->dirty_bgs_lock);
3776 if (list_empty(&cache->dirty_list)) {
3777 list_add_tail(&cache->dirty_list,
3778 &cur_trans->dirty_bgs);
3779 btrfs_get_block_group(cache);
3780 }
3781 spin_unlock(&cur_trans->dirty_bgs_lock);
3782 } else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003783 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003784 }
3785 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003786
3787 /* if its not on the io list, we need to put the block group */
3788 if (should_put)
3789 btrfs_put_block_group(cache);
3790
3791 if (ret)
3792 break;
Filipe Mananab58d1a92015-04-25 18:29:16 +01003793
3794 /*
3795 * Avoid blocking other tasks for too long. It might even save
3796 * us from writing caches for block groups that are going to be
3797 * removed.
3798 */
3799 mutex_unlock(&trans->transaction->cache_write_mutex);
3800 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003801 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003802 mutex_unlock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003803
3804 /*
3805 * go through delayed refs for all the stuff we've just kicked off
3806 * and then loop back (just once)
3807 */
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003808 ret = btrfs_run_delayed_refs(trans, 0);
Chris Mason1bbc6212015-04-06 12:46:08 -07003809 if (!ret && loops == 0) {
3810 loops++;
3811 spin_lock(&cur_trans->dirty_bgs_lock);
3812 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003813 /*
3814 * dirty_bgs_lock protects us from concurrent block group
3815 * deletes too (not just cache_write_mutex).
3816 */
3817 if (!list_empty(&dirty)) {
3818 spin_unlock(&cur_trans->dirty_bgs_lock);
3819 goto again;
3820 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003821 spin_unlock(&cur_trans->dirty_bgs_lock);
Liu Boc79a1752016-07-20 17:44:12 -07003822 } else if (ret < 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003823 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
Chris Mason1bbc6212015-04-06 12:46:08 -07003824 }
3825
3826 btrfs_free_path(path);
3827 return ret;
3828}
3829
Chris Mason96b51792007-10-15 16:15:19 -04003830int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003831 struct btrfs_fs_info *fs_info)
Chris Mason9078a3e2007-04-26 16:46:15 -04003832{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003833 struct btrfs_block_group_cache *cache;
Josef Bacikce93ec52014-11-17 15:45:48 -05003834 struct btrfs_transaction *cur_trans = trans->transaction;
3835 int ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003836 int should_put;
Chris Mason9078a3e2007-04-26 16:46:15 -04003837 struct btrfs_path *path;
Chris Mason1bbc6212015-04-06 12:46:08 -07003838 struct list_head *io = &cur_trans->io_bgs;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003839 int num_started = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003840
3841 path = btrfs_alloc_path();
3842 if (!path)
3843 return -ENOMEM;
3844
Josef Bacikce93ec52014-11-17 15:45:48 -05003845 /*
Filipe Mananae44081e2015-12-18 03:02:48 +00003846 * Even though we are in the critical section of the transaction commit,
3847 * we can still have concurrent tasks adding elements to this
3848 * transaction's list of dirty block groups. These tasks correspond to
3849 * endio free space workers started when writeback finishes for a
3850 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
3851 * allocate new block groups as a result of COWing nodes of the root
3852 * tree when updating the free space inode. The writeback for the space
3853 * caches is triggered by an earlier call to
3854 * btrfs_start_dirty_block_groups() and iterations of the following
3855 * loop.
3856 * Also we want to do the cache_save_setup first and then run the
Josef Bacikce93ec52014-11-17 15:45:48 -05003857 * delayed refs to make sure we have the best chance at doing this all
3858 * in one shot.
3859 */
Filipe Mananae44081e2015-12-18 03:02:48 +00003860 spin_lock(&cur_trans->dirty_bgs_lock);
Josef Bacikce93ec52014-11-17 15:45:48 -05003861 while (!list_empty(&cur_trans->dirty_bgs)) {
3862 cache = list_first_entry(&cur_trans->dirty_bgs,
3863 struct btrfs_block_group_cache,
3864 dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003865
3866 /*
3867 * this can happen if cache_save_setup re-dirties a block
3868 * group that is already under IO. Just wait for it to
3869 * finish and then do it all again
3870 */
3871 if (!list_empty(&cache->io_list)) {
Filipe Mananae44081e2015-12-18 03:02:48 +00003872 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003873 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003874 btrfs_wait_cache_io(trans, cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003875 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003876 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003877 }
3878
Chris Mason1bbc6212015-04-06 12:46:08 -07003879 /*
3880 * don't remove from the dirty list until after we've waited
3881 * on any pending IO
3882 */
Josef Bacikce93ec52014-11-17 15:45:48 -05003883 list_del_init(&cache->dirty_list);
Filipe Mananae44081e2015-12-18 03:02:48 +00003884 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003885 should_put = 1;
3886
Chris Mason1bbc6212015-04-06 12:46:08 -07003887 cache_save_setup(cache, trans, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003888
Josef Bacikce93ec52014-11-17 15:45:48 -05003889 if (!ret)
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003890 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003891 (unsigned long) -1);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003892
3893 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3894 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003895 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003896 cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003897 if (ret == 0 && cache->io_ctl.inode) {
3898 num_started++;
3899 should_put = 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003900 list_add_tail(&cache->io_list, io);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003901 } else {
3902 /*
3903 * if we failed to write the cache, the
3904 * generation will be bad and life goes on
3905 */
3906 ret = 0;
3907 }
3908 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003909 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003910 ret = write_one_cache_group(trans, fs_info,
3911 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003912 /*
3913 * One of the free space endio workers might have
3914 * created a new block group while updating a free space
3915 * cache's inode (at inode.c:btrfs_finish_ordered_io())
3916 * and hasn't released its transaction handle yet, in
3917 * which case the new block group is still attached to
3918 * its transaction handle and its creation has not
3919 * finished yet (no block group item in the extent tree
3920 * yet, etc). If this is the case, wait for all free
3921 * space endio workers to finish and retry. This is a
3922 * a very rare case so no need for a more efficient and
3923 * complex approach.
3924 */
3925 if (ret == -ENOENT) {
3926 wait_event(cur_trans->writer_wait,
3927 atomic_read(&cur_trans->num_writers) == 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003928 ret = write_one_cache_group(trans, fs_info,
3929 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003930 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003931 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003932 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003933 }
Chris Masonc9dc4c62015-04-04 17:14:42 -07003934
3935 /* if its not on the io list, we need to put the block group */
3936 if (should_put)
3937 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003938 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003939 }
Filipe Mananae44081e2015-12-18 03:02:48 +00003940 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003941
Nikolay Borisov45ae2c12018-02-08 18:25:18 +02003942 /*
3943 * Refer to the definition of io_bgs member for details why it's safe
3944 * to use it without any locking
3945 */
Chris Mason1bbc6212015-04-06 12:46:08 -07003946 while (!list_empty(io)) {
3947 cache = list_first_entry(io, struct btrfs_block_group_cache,
Chris Masonc9dc4c62015-04-04 17:14:42 -07003948 io_list);
3949 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003950 btrfs_wait_cache_io(trans, cache, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003951 btrfs_put_block_group(cache);
3952 }
3953
Chris Mason9078a3e2007-04-26 16:46:15 -04003954 btrfs_free_path(path);
Josef Bacikce93ec52014-11-17 15:45:48 -05003955 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003956}
3957
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003958int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05003959{
3960 struct btrfs_block_group_cache *block_group;
3961 int readonly = 0;
3962
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003963 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003964 if (!block_group || block_group->ro)
3965 readonly = 1;
3966 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003967 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003968 return readonly;
3969}
3970
Filipe Mananaf78c4362016-05-09 13:15:41 +01003971bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3972{
3973 struct btrfs_block_group_cache *bg;
3974 bool ret = true;
3975
3976 bg = btrfs_lookup_block_group(fs_info, bytenr);
3977 if (!bg)
3978 return false;
3979
3980 spin_lock(&bg->lock);
3981 if (bg->ro)
3982 ret = false;
3983 else
3984 atomic_inc(&bg->nocow_writers);
3985 spin_unlock(&bg->lock);
3986
3987 /* no put on block group, done by btrfs_dec_nocow_writers */
3988 if (!ret)
3989 btrfs_put_block_group(bg);
3990
3991 return ret;
3992
3993}
3994
3995void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3996{
3997 struct btrfs_block_group_cache *bg;
3998
3999 bg = btrfs_lookup_block_group(fs_info, bytenr);
4000 ASSERT(bg);
4001 if (atomic_dec_and_test(&bg->nocow_writers))
Peter Zijlstra46259562018-03-15 11:43:08 +01004002 wake_up_var(&bg->nocow_writers);
Filipe Mananaf78c4362016-05-09 13:15:41 +01004003 /*
4004 * Once for our lookup and once for the lookup done by a previous call
4005 * to btrfs_inc_nocow_writers()
4006 */
4007 btrfs_put_block_group(bg);
4008 btrfs_put_block_group(bg);
4009}
4010
Filipe Mananaf78c4362016-05-09 13:15:41 +01004011void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg)
4012{
Peter Zijlstra46259562018-03-15 11:43:08 +01004013 wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers));
Filipe Mananaf78c4362016-05-09 13:15:41 +01004014}
4015
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04004016static const char *alloc_name(u64 flags)
4017{
4018 switch (flags) {
4019 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
4020 return "mixed";
4021 case BTRFS_BLOCK_GROUP_METADATA:
4022 return "metadata";
4023 case BTRFS_BLOCK_GROUP_DATA:
4024 return "data";
4025 case BTRFS_BLOCK_GROUP_SYSTEM:
4026 return "system";
4027 default:
4028 WARN_ON(1);
4029 return "invalid-combination";
4030 };
4031}
4032
Lu Fengqi4ca61682018-05-28 14:30:27 +08004033static int create_space_info(struct btrfs_fs_info *info, u64 flags)
Nikolay Borisov2be12ef2017-05-22 09:35:49 +03004034{
4035
4036 struct btrfs_space_info *space_info;
4037 int i;
4038 int ret;
4039
4040 space_info = kzalloc(sizeof(*space_info), GFP_NOFS);
4041 if (!space_info)
4042 return -ENOMEM;
4043
4044 ret = percpu_counter_init(&space_info->total_bytes_pinned, 0,
4045 GFP_KERNEL);
4046 if (ret) {
4047 kfree(space_info);
4048 return ret;
4049 }
4050
4051 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
4052 INIT_LIST_HEAD(&space_info->block_groups[i]);
4053 init_rwsem(&space_info->groups_sem);
4054 spin_lock_init(&space_info->lock);
4055 space_info->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
4056 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
4057 init_waitqueue_head(&space_info->wait);
4058 INIT_LIST_HEAD(&space_info->ro_bgs);
4059 INIT_LIST_HEAD(&space_info->tickets);
4060 INIT_LIST_HEAD(&space_info->priority_tickets);
4061
4062 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
4063 info->space_info_kobj, "%s",
4064 alloc_name(space_info->flags));
4065 if (ret) {
4066 percpu_counter_destroy(&space_info->total_bytes_pinned);
4067 kfree(space_info);
4068 return ret;
4069 }
4070
Nikolay Borisov2be12ef2017-05-22 09:35:49 +03004071 list_add_rcu(&space_info->list, &info->space_info);
4072 if (flags & BTRFS_BLOCK_GROUP_DATA)
4073 info->data_sinfo = space_info;
4074
4075 return ret;
4076}
4077
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +03004078static void update_space_info(struct btrfs_fs_info *info, u64 flags,
Chris Mason593060d2008-03-25 16:50:33 -04004079 u64 total_bytes, u64 bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -04004080 u64 bytes_readonly,
Chris Mason593060d2008-03-25 16:50:33 -04004081 struct btrfs_space_info **space_info)
4082{
4083 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004084 int factor;
4085
4086 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
4087 BTRFS_BLOCK_GROUP_RAID10))
4088 factor = 2;
4089 else
4090 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04004091
4092 found = __find_space_info(info, flags);
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +03004093 ASSERT(found);
4094 spin_lock(&found->lock);
4095 found->total_bytes += total_bytes;
4096 found->disk_total += total_bytes * factor;
4097 found->bytes_used += bytes_used;
4098 found->disk_used += bytes_used * factor;
4099 found->bytes_readonly += bytes_readonly;
4100 if (total_bytes > 0)
4101 found->full = 0;
4102 space_info_add_new_bytes(info, found, total_bytes -
4103 bytes_used - bytes_readonly);
4104 spin_unlock(&found->lock);
4105 *space_info = found;
Chris Mason593060d2008-03-25 16:50:33 -04004106}
4107
Chris Mason8790d502008-04-03 16:29:03 -04004108static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
4109{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03004110 u64 extra_flags = chunk_to_extended(flags) &
4111 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004112
Miao Xiede98ced2013-01-29 10:13:12 +00004113 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004114 if (flags & BTRFS_BLOCK_GROUP_DATA)
4115 fs_info->avail_data_alloc_bits |= extra_flags;
4116 if (flags & BTRFS_BLOCK_GROUP_METADATA)
4117 fs_info->avail_metadata_alloc_bits |= extra_flags;
4118 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
4119 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004120 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04004121}
Chris Mason593060d2008-03-25 16:50:33 -04004122
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004123/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004124 * returns target flags in extended format or 0 if restripe for this
4125 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04004126 *
David Sterbadccdb072018-03-21 00:20:05 +01004127 * should be called with balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004128 */
4129static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
4130{
4131 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4132 u64 target = 0;
4133
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004134 if (!bctl)
4135 return 0;
4136
4137 if (flags & BTRFS_BLOCK_GROUP_DATA &&
4138 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4139 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
4140 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
4141 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4142 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
4143 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
4144 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4145 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
4146 }
4147
4148 return target;
4149}
4150
4151/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004152 * @flags: available profiles in extended format (see ctree.h)
4153 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004154 * Returns reduced profile in chunk format. If profile changing is in
4155 * progress (either running or paused) picks the target profile (if it's
4156 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004157 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004158static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04004159{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004160 u64 num_devices = fs_info->fs_devices->rw_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004161 u64 target;
Zhao Lei9c170b22015-09-15 21:08:08 +08004162 u64 raid_type;
4163 u64 allowed = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004164
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004165 /*
4166 * see if restripe for this chunk_type is in progress, if so
4167 * try to reduce to the target profile
4168 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004169 spin_lock(&fs_info->balance_lock);
4170 target = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004171 if (target) {
4172 /* pick target profile only if it's already available */
4173 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004174 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004175 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004176 }
4177 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004178 spin_unlock(&fs_info->balance_lock);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004179
David Woodhouse53b381b2013-01-29 18:40:14 -05004180 /* First, mask out the RAID levels which aren't possible */
Zhao Lei9c170b22015-09-15 21:08:08 +08004181 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
4182 if (num_devices >= btrfs_raid_array[raid_type].devs_min)
Anand Jain41a6e892018-04-25 19:01:43 +08004183 allowed |= btrfs_raid_array[raid_type].bg_flag;
Zhao Lei9c170b22015-09-15 21:08:08 +08004184 }
4185 allowed &= flags;
Chris Masona061fc82008-05-07 11:43:44 -04004186
Zhao Lei9c170b22015-09-15 21:08:08 +08004187 if (allowed & BTRFS_BLOCK_GROUP_RAID6)
4188 allowed = BTRFS_BLOCK_GROUP_RAID6;
4189 else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
4190 allowed = BTRFS_BLOCK_GROUP_RAID5;
4191 else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
4192 allowed = BTRFS_BLOCK_GROUP_RAID10;
4193 else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
4194 allowed = BTRFS_BLOCK_GROUP_RAID1;
4195 else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
4196 allowed = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04004197
Zhao Lei9c170b22015-09-15 21:08:08 +08004198 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
Chris Masonec44a352008-04-28 15:29:52 -04004199
Zhao Lei9c170b22015-09-15 21:08:08 +08004200 return extended_to_chunk(flags | allowed);
Chris Masonec44a352008-04-28 15:29:52 -04004201}
4202
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004203static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05004204{
Miao Xiede98ced2013-01-29 10:13:12 +00004205 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004206 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004207
4208 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004209 flags = orig_flags;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004210 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00004211
4212 if (flags & BTRFS_BLOCK_GROUP_DATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004213 flags |= fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004214 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004215 flags |= fs_info->avail_system_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004216 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004217 flags |= fs_info->avail_metadata_alloc_bits;
4218 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02004219
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004220 return btrfs_reduce_alloc_profile(fs_info, flags);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004221}
Josef Bacik6a632092009-02-20 11:00:09 -05004222
Jeff Mahoney1b868262017-05-17 11:38:35 -04004223static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04004224{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004225 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004226 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05004227 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004228
Yan, Zhengb742bb82010-05-16 10:46:24 -04004229 if (data)
4230 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004231 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb82010-05-16 10:46:24 -04004232 flags = BTRFS_BLOCK_GROUP_SYSTEM;
4233 else
4234 flags = BTRFS_BLOCK_GROUP_METADATA;
4235
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004236 ret = get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05004237 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004238}
4239
Jeff Mahoney1b868262017-05-17 11:38:35 -04004240u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info)
4241{
4242 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_DATA);
4243}
4244
4245u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info)
4246{
4247 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4248}
4249
4250u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info)
4251{
4252 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4253}
4254
Liu Bo41361352017-02-13 15:42:21 -08004255static u64 btrfs_space_info_used(struct btrfs_space_info *s_info,
4256 bool may_use_included)
4257{
4258 ASSERT(s_info);
4259 return s_info->bytes_used + s_info->bytes_reserved +
4260 s_info->bytes_pinned + s_info->bytes_readonly +
4261 (may_use_included ? s_info->bytes_may_use : 0);
4262}
4263
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004264int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05004265{
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004266 struct btrfs_root *root = inode->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004267 struct btrfs_fs_info *fs_info = root->fs_info;
Nikolay Borisov1174cad2017-07-11 13:47:50 +03004268 struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
Josef Bacikab6e24102010-03-19 14:38:13 +00004269 u64 used;
Zhao Lei94b947b2015-02-14 13:23:45 +08004270 int ret = 0;
Zhao Leic99f1b02015-03-02 19:32:20 +08004271 int need_commit = 2;
4272 int have_pinned_space;
Josef Bacik6a632092009-02-20 11:00:09 -05004273
4274 /* make sure bytes are sectorsize aligned */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004275 bytes = ALIGN(bytes, fs_info->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05004276
Miao Xie9dced182013-10-25 17:33:36 +08004277 if (btrfs_is_free_space_inode(inode)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004278 need_commit = 0;
Miao Xie9dced182013-10-25 17:33:36 +08004279 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04004280 }
4281
Josef Bacik6a632092009-02-20 11:00:09 -05004282again:
4283 /* make sure we have enough space to handle the data first */
4284 spin_lock(&data_sinfo->lock);
Liu Bo41361352017-02-13 15:42:21 -08004285 used = btrfs_space_info_used(data_sinfo, true);
Josef Bacikab6e24102010-03-19 14:38:13 +00004286
4287 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004288 struct btrfs_trans_handle *trans;
4289
Josef Bacik6a632092009-02-20 11:00:09 -05004290 /*
4291 * if we don't have enough free bytes in this space then we need
4292 * to alloc a new chunk.
4293 */
Zhao Leib9fd47c2015-02-09 14:40:20 +08004294 if (!data_sinfo->full) {
Josef Bacik6a632092009-02-20 11:00:09 -05004295 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05004296
Chris Mason0e4f8f82011-04-15 16:05:44 -04004297 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05004298 spin_unlock(&data_sinfo->lock);
Nikolay Borisov1174cad2017-07-11 13:47:50 +03004299
Jeff Mahoney1b868262017-05-17 11:38:35 -04004300 alloc_target = btrfs_data_alloc_profile(fs_info);
Miao Xie9dced182013-10-25 17:33:36 +08004301 /*
4302 * It is ugly that we don't call nolock join
4303 * transaction for the free space inode case here.
4304 * But it is safe because we only do the data space
4305 * reservation for the free space cache in the
4306 * transaction context, the common join transaction
4307 * just increase the counter of the current transaction
4308 * handler, doesn't try to acquire the trans_lock of
4309 * the fs.
4310 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004311 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004312 if (IS_ERR(trans))
4313 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05004314
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004315 ret = do_chunk_alloc(trans, fs_info, alloc_target,
Chris Mason0e4f8f82011-04-15 16:05:44 -04004316 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004317 btrfs_end_transaction(trans);
Miao Xied52a5b52011-01-05 10:07:18 +00004318 if (ret < 0) {
4319 if (ret != -ENOSPC)
4320 return ret;
Zhao Leic99f1b02015-03-02 19:32:20 +08004321 else {
4322 have_pinned_space = 1;
Miao Xied52a5b52011-01-05 10:07:18 +00004323 goto commit_trans;
Zhao Leic99f1b02015-03-02 19:32:20 +08004324 }
Miao Xied52a5b52011-01-05 10:07:18 +00004325 }
Chris Mason33b4d472009-09-22 14:45:50 -04004326
Josef Bacik6a632092009-02-20 11:00:09 -05004327 goto again;
4328 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004329
4330 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04004331 * If we don't have enough pinned space to deal with this
Zhao Lei94b947b2015-02-14 13:23:45 +08004332 * allocation, and no removed chunk in current transaction,
4333 * don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004334 */
Zhao Leic99f1b02015-03-02 19:32:20 +08004335 have_pinned_space = percpu_counter_compare(
4336 &data_sinfo->total_bytes_pinned,
4337 used + bytes - data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05004338 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004339
4340 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00004341commit_trans:
Nikolay Borisov92e2f7e2018-02-05 10:41:16 +02004342 if (need_commit) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004343 need_commit--;
Josef Bacikb150a4f2013-06-19 15:00:04 -04004344
Zhao Leie1746e82015-12-01 18:39:40 +08004345 if (need_commit > 0) {
Nikolay Borisov82b3e532018-04-23 10:54:13 +03004346 btrfs_start_delalloc_roots(fs_info, -1);
Chris Mason6374e57a2017-06-23 09:48:21 -07004347 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004348 (u64)-1);
Zhao Leie1746e82015-12-01 18:39:40 +08004349 }
Zhao Lei9a4e7272015-04-09 12:34:43 +08004350
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004351 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004352 if (IS_ERR(trans))
4353 return PTR_ERR(trans);
Zhao Leic99f1b02015-03-02 19:32:20 +08004354 if (have_pinned_space >= 0 ||
Josef Bacik3204d332015-09-24 10:46:10 -04004355 test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
4356 &trans->transaction->flags) ||
Zhao Leic99f1b02015-03-02 19:32:20 +08004357 need_commit > 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004358 ret = btrfs_commit_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004359 if (ret)
4360 return ret;
Zhao Leid7c15172015-02-26 10:49:20 +08004361 /*
Filipe Mananac2d6cb12016-01-15 11:05:12 +00004362 * The cleaner kthread might still be doing iput
4363 * operations. Wait for it to finish so that
4364 * more space is released.
Zhao Leid7c15172015-02-26 10:49:20 +08004365 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004366 mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
4367 mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
Zhao Lei94b947b2015-02-14 13:23:45 +08004368 goto again;
4369 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004370 btrfs_end_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004371 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004372 }
4373
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004374 trace_btrfs_space_reservation(fs_info,
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004375 "space_info:enospc",
4376 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004377 return -ENOSPC;
4378 }
4379 data_sinfo->bytes_may_use += bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004380 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004381 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004382 spin_unlock(&data_sinfo->lock);
4383
Dongsheng Yang237c0e92014-12-29 06:23:05 -05004384 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004385}
4386
Qu Wenruo364ecf32017-02-27 15:10:38 +08004387int btrfs_check_data_free_space(struct inode *inode,
4388 struct extent_changeset **reserved, u64 start, u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004389{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004390 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004391 int ret;
4392
4393 /* align the range */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004394 len = round_up(start + len, fs_info->sectorsize) -
4395 round_down(start, fs_info->sectorsize);
4396 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004397
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004398 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004399 if (ret < 0)
4400 return ret;
4401
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004402 /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
Qu Wenruo364ecf32017-02-27 15:10:38 +08004403 ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
Qu Wenruo7bc329c2017-02-27 15:10:36 +08004404 if (ret < 0)
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004405 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004406 else
4407 ret = 0;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004408 return ret;
4409}
4410
4411/*
Qu Wenruo4ceff072015-09-08 17:22:42 +08004412 * Called if we need to clear a data reservation for this inode
4413 * Normally in a error case.
4414 *
Qu Wenruo51773be2015-10-08 18:19:37 +08004415 * This one will *NOT* use accurate qgroup reserved space API, just for case
4416 * which we can't sleep and is sure it won't affect qgroup reserved space.
4417 * Like clear_bit_hook().
Qu Wenruo4ceff072015-09-08 17:22:42 +08004418 */
Qu Wenruo51773be2015-10-08 18:19:37 +08004419void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
4420 u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004421{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004422 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004423 struct btrfs_space_info *data_sinfo;
4424
4425 /* Make sure the range is aligned to sectorsize */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004426 len = round_up(start + len, fs_info->sectorsize) -
4427 round_down(start, fs_info->sectorsize);
4428 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004429
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004430 data_sinfo = fs_info->data_sinfo;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004431 spin_lock(&data_sinfo->lock);
4432 if (WARN_ON(data_sinfo->bytes_may_use < len))
4433 data_sinfo->bytes_may_use = 0;
4434 else
4435 data_sinfo->bytes_may_use -= len;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004436 trace_btrfs_space_reservation(fs_info, "space_info",
Qu Wenruo4ceff072015-09-08 17:22:42 +08004437 data_sinfo->flags, len, 0);
4438 spin_unlock(&data_sinfo->lock);
4439}
4440
Qu Wenruo51773be2015-10-08 18:19:37 +08004441/*
4442 * Called if we need to clear a data reservation for this inode
4443 * Normally in a error case.
4444 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04004445 * This one will handle the per-inode data rsv map for accurate reserved
Qu Wenruo51773be2015-10-08 18:19:37 +08004446 * space framework.
4447 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08004448void btrfs_free_reserved_data_space(struct inode *inode,
4449 struct extent_changeset *reserved, u64 start, u64 len)
Qu Wenruo51773be2015-10-08 18:19:37 +08004450{
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004451 struct btrfs_root *root = BTRFS_I(inode)->root;
4452
4453 /* Make sure the range is aligned to sectorsize */
Jeff Mahoneyda170662016-06-15 09:22:56 -04004454 len = round_up(start + len, root->fs_info->sectorsize) -
4455 round_down(start, root->fs_info->sectorsize);
4456 start = round_down(start, root->fs_info->sectorsize);
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004457
Qu Wenruo51773be2015-10-08 18:19:37 +08004458 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruobc42bda2017-02-27 15:10:39 +08004459 btrfs_qgroup_free_data(inode, reserved, start, len);
Qu Wenruo51773be2015-10-08 18:19:37 +08004460}
4461
Josef Bacik97e728d2009-04-21 17:40:57 -04004462static void force_metadata_allocation(struct btrfs_fs_info *info)
4463{
4464 struct list_head *head = &info->space_info;
4465 struct btrfs_space_info *found;
4466
4467 rcu_read_lock();
4468 list_for_each_entry_rcu(found, head, list) {
4469 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004470 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04004471 }
4472 rcu_read_unlock();
4473}
4474
Miao Xie3c76cd82013-04-25 10:12:38 +00004475static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
4476{
4477 return (global->size << 1);
4478}
4479
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004480static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
Josef Bacik698d0082012-09-12 14:08:47 -04004481 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04004482{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004483 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004484 u64 bytes_used = btrfs_space_info_used(sinfo, false);
Chris Masone5bc2452010-10-26 13:37:56 -04004485 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04004486
Chris Mason0e4f8f82011-04-15 16:05:44 -04004487 if (force == CHUNK_ALLOC_FORCE)
4488 return 1;
4489
4490 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04004491 * We need to take into account the global rsv because for all intents
4492 * and purposes it's used space. Don't worry about locking the
4493 * global_rsv, it doesn't change except when the transaction commits.
4494 */
Josef Bacik54338b52012-08-14 16:20:52 -04004495 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004496 bytes_used += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04004497
4498 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04004499 * in limited mode, we want to have some free space up to
4500 * about 1% of the FS size.
4501 */
4502 if (force == CHUNK_ALLOC_LIMITED) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004503 thresh = btrfs_super_total_bytes(fs_info->super_copy);
Byongho Leeee221842015-12-15 01:42:10 +09004504 thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
Chris Mason0e4f8f82011-04-15 16:05:44 -04004505
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004506 if (sinfo->total_bytes - bytes_used < thresh)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004507 return 1;
4508 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004509
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004510 if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04004511 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04004512 return 1;
4513}
4514
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004515static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004516{
4517 u64 num_dev;
4518
David Woodhouse53b381b2013-01-29 18:40:14 -05004519 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
4520 BTRFS_BLOCK_GROUP_RAID0 |
4521 BTRFS_BLOCK_GROUP_RAID5 |
4522 BTRFS_BLOCK_GROUP_RAID6))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004523 num_dev = fs_info->fs_devices->rw_devices;
Liu Bo15d1ff82012-03-29 09:57:44 -04004524 else if (type & BTRFS_BLOCK_GROUP_RAID1)
4525 num_dev = 2;
4526 else
4527 num_dev = 1; /* DUP or single */
4528
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004529 return num_dev;
Liu Bo15d1ff82012-03-29 09:57:44 -04004530}
4531
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004532/*
4533 * If @is_allocation is true, reserve space in the system space info necessary
4534 * for allocating a chunk, otherwise if it's false, reserve space necessary for
4535 * removing a chunk.
4536 */
4537void check_system_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004538 struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004539{
4540 struct btrfs_space_info *info;
4541 u64 left;
4542 u64 thresh;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004543 int ret = 0;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004544 u64 num_devs;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004545
4546 /*
4547 * Needed because we can end up allocating a system chunk and for an
4548 * atomic and race free space reservation in the chunk block reserve.
4549 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01004550 lockdep_assert_held(&fs_info->chunk_mutex);
Liu Bo15d1ff82012-03-29 09:57:44 -04004551
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004552 info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
Liu Bo15d1ff82012-03-29 09:57:44 -04004553 spin_lock(&info->lock);
Liu Bo41361352017-02-13 15:42:21 -08004554 left = info->total_bytes - btrfs_space_info_used(info, true);
Liu Bo15d1ff82012-03-29 09:57:44 -04004555 spin_unlock(&info->lock);
4556
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004557 num_devs = get_profile_num_devs(fs_info, type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004558
4559 /* num_devs device items to update and 1 chunk item to add or remove */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004560 thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) +
4561 btrfs_calc_trans_metadata_size(fs_info, 1);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004562
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004563 if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
4564 btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
4565 left, thresh, type);
4566 dump_space_info(fs_info, info, 0, 0);
Liu Bo15d1ff82012-03-29 09:57:44 -04004567 }
4568
4569 if (left < thresh) {
Jeff Mahoney1b868262017-05-17 11:38:35 -04004570 u64 flags = btrfs_system_alloc_profile(fs_info);
Liu Bo15d1ff82012-03-29 09:57:44 -04004571
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004572 /*
4573 * Ignore failure to create system chunk. We might end up not
4574 * needing it, as we might not need to COW all nodes/leafs from
4575 * the paths we visit in the chunk tree (they were already COWed
4576 * or created in the current transaction for example).
4577 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004578 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004579 }
4580
4581 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004582 ret = btrfs_block_rsv_add(fs_info->chunk_root,
4583 &fs_info->chunk_block_rsv,
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004584 thresh, BTRFS_RESERVE_NO_FLUSH);
4585 if (!ret)
4586 trans->chunk_bytes_reserved += thresh;
Liu Bo15d1ff82012-03-29 09:57:44 -04004587 }
4588}
4589
Liu Bo28b737f2016-07-29 11:09:50 -07004590/*
4591 * If force is CHUNK_ALLOC_FORCE:
4592 * - return 1 if it successfully allocates a chunk,
4593 * - return errors including -ENOSPC otherwise.
4594 * If force is NOT CHUNK_ALLOC_FORCE:
4595 * - return 0 if it doesn't need to allocate a new chunk,
4596 * - return 1 if it successfully allocates a chunk,
4597 * - return errors including -ENOSPC otherwise.
4598 */
Chris Mason6324fbf2008-03-24 15:01:59 -04004599static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004600 struct btrfs_fs_info *fs_info, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04004601{
4602 struct btrfs_space_info *space_info;
Josef Bacik6d741192011-04-11 20:20:11 -04004603 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05004604 int ret = 0;
4605
Josef Bacikc6b305a2012-12-18 09:16:16 -05004606 /* Don't re-enter if we're already allocating a chunk */
4607 if (trans->allocating_chunk)
4608 return -ENOSPC;
4609
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004610 space_info = __find_space_info(fs_info, flags);
Jeff Mahoneydc2d3002018-03-20 15:25:25 -04004611 ASSERT(space_info);
Chris Mason6324fbf2008-03-24 15:01:59 -04004612
Josef Bacik6d741192011-04-11 20:20:11 -04004613again:
Josef Bacik25179202008-10-29 14:49:05 -04004614 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05004615 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004616 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04004617 if (space_info->full) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004618 if (should_alloc_chunk(fs_info, space_info, force))
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004619 ret = -ENOSPC;
4620 else
4621 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04004622 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004623 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04004624 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004625
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004626 if (!should_alloc_chunk(fs_info, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04004627 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04004628 return 0;
4629 } else if (space_info->chunk_alloc) {
4630 wait_for_alloc = 1;
4631 } else {
4632 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04004633 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004634
Josef Bacik25179202008-10-29 14:49:05 -04004635 spin_unlock(&space_info->lock);
4636
Josef Bacik6d741192011-04-11 20:20:11 -04004637 mutex_lock(&fs_info->chunk_mutex);
4638
4639 /*
4640 * The chunk_mutex is held throughout the entirety of a chunk
4641 * allocation, so once we've acquired the chunk_mutex we know that the
4642 * other guy is done and we need to recheck and see if we should
4643 * allocate.
4644 */
4645 if (wait_for_alloc) {
4646 mutex_unlock(&fs_info->chunk_mutex);
4647 wait_for_alloc = 0;
Nikolay Borisov1e1c50a2018-04-05 10:40:15 +03004648 cond_resched();
Josef Bacik6d741192011-04-11 20:20:11 -04004649 goto again;
4650 }
4651
Josef Bacikc6b305a2012-12-18 09:16:16 -05004652 trans->allocating_chunk = true;
4653
Josef Bacik97e728d2009-04-21 17:40:57 -04004654 /*
Josef Bacik67377732010-09-16 16:19:09 -04004655 * If we have mixed data/metadata chunks we want to make sure we keep
4656 * allocating mixed chunks instead of individual chunks.
4657 */
4658 if (btrfs_mixed_space_info(space_info))
4659 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
4660
4661 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04004662 * if we're doing a data chunk, go ahead and make sure that
4663 * we keep a reasonable number of metadata chunks allocated in the
4664 * FS as well.
4665 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04004666 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04004667 fs_info->data_chunk_allocations++;
4668 if (!(fs_info->data_chunk_allocations %
4669 fs_info->metadata_ratio))
4670 force_metadata_allocation(fs_info);
4671 }
4672
Liu Bo15d1ff82012-03-29 09:57:44 -04004673 /*
4674 * Check if we have enough space in SYSTEM chunk because we may need
4675 * to update devices.
4676 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004677 check_system_chunk(trans, fs_info, flags);
Liu Bo15d1ff82012-03-29 09:57:44 -04004678
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004679 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05004680 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004681
Josef Bacik9ed74f22009-09-11 16:12:44 -04004682 spin_lock(&space_info->lock);
Nikolay Borisov57f16422018-04-11 11:21:19 +03004683 if (ret < 0) {
4684 if (ret == -ENOSPC)
4685 space_info->full = 1;
4686 else
4687 goto out;
4688 } else {
Yan, Zheng424499d2010-05-16 10:46:25 -04004689 ret = 1;
Nikolay Borisov57f16422018-04-11 11:21:19 +03004690 }
Josef Bacik6d741192011-04-11 20:20:11 -04004691
Chris Mason0e4f8f82011-04-15 16:05:44 -04004692 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004693out:
Josef Bacik6d741192011-04-11 20:20:11 -04004694 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004695 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03004696 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana00d80e32015-07-20 14:56:20 +01004697 /*
4698 * When we allocate a new chunk we reserve space in the chunk block
4699 * reserve to make sure we can COW nodes/leafs in the chunk tree or
4700 * add new nodes/leafs to it if we end up needing to do it when
4701 * inserting the chunk item and updating device items as part of the
4702 * second phase of chunk allocation, performed by
4703 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
4704 * large number of new block groups to create in our transaction
4705 * handle's new_bgs list to avoid exhausting the chunk block reserve
4706 * in extreme cases - like having a single transaction create many new
4707 * block groups when starting to write out the free space caches of all
4708 * the block groups that were made dirty during the lifetime of the
4709 * transaction.
4710 */
Filipe Mananad9a05402015-10-03 13:13:13 +01004711 if (trans->can_flush_pending_bgs &&
Byongho Leeee221842015-12-15 01:42:10 +09004712 trans->chunk_bytes_reserved >= (u64)SZ_2M) {
Nikolay Borisov6c686b32018-02-07 17:55:40 +02004713 btrfs_create_pending_block_groups(trans);
Filipe Manana00d80e32015-07-20 14:56:20 +01004714 btrfs_trans_release_chunk_metadata(trans);
4715 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004716 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04004717}
4718
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004719static int can_overcommit(struct btrfs_fs_info *fs_info,
Josef Bacika80c8dc2012-09-06 16:59:33 -04004720 struct btrfs_space_info *space_info, u64 bytes,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004721 enum btrfs_reserve_flush_enum flush,
4722 bool system_chunk)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004723{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004724 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04004725 u64 profile;
Miao Xie3c76cd82013-04-25 10:12:38 +00004726 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004727 u64 avail;
4728 u64 used;
4729
Josef Bacik957780e2016-05-17 13:30:55 -04004730 /* Don't overcommit when in mixed mode. */
4731 if (space_info->flags & BTRFS_BLOCK_GROUP_DATA)
4732 return 0;
4733
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004734 if (system_chunk)
4735 profile = btrfs_system_alloc_profile(fs_info);
4736 else
4737 profile = btrfs_metadata_alloc_profile(fs_info);
4738
Liu Bo41361352017-02-13 15:42:21 -08004739 used = btrfs_space_info_used(space_info, false);
Josef Bacik96f1bb52013-01-30 17:02:51 -05004740
Josef Bacik96f1bb52013-01-30 17:02:51 -05004741 /*
4742 * We only want to allow over committing if we have lots of actual space
4743 * free, but if we don't have enough space to handle the global reserve
4744 * space then we could end up having a real enospc problem when trying
4745 * to allocate a chunk or some other such important allocation.
4746 */
Miao Xie3c76cd82013-04-25 10:12:38 +00004747 spin_lock(&global_rsv->lock);
4748 space_size = calc_global_rsv_need_space(global_rsv);
4749 spin_unlock(&global_rsv->lock);
4750 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05004751 return 0;
4752
4753 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004754
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004755 avail = atomic64_read(&fs_info->free_chunk_space);
Josef Bacika80c8dc2012-09-06 16:59:33 -04004756
4757 /*
4758 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05004759 * space is actually useable. For raid56, the space info used
4760 * doesn't include the parity drive, so we don't have to
4761 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04004762 */
4763 if (profile & (BTRFS_BLOCK_GROUP_DUP |
4764 BTRFS_BLOCK_GROUP_RAID1 |
4765 BTRFS_BLOCK_GROUP_RAID10))
4766 avail >>= 1;
4767
4768 /*
Miao Xie561c2942012-10-16 11:32:18 +00004769 * If we aren't flushing all things, let us overcommit up to
4770 * 1/2th of the space. If we can flush, don't let us overcommit
4771 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04004772 */
Miao Xie08e007d2012-10-16 11:33:38 +00004773 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04004774 avail >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004775 else
Josef Bacik14575ae2013-09-17 10:48:00 -04004776 avail >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004777
Josef Bacik14575ae2013-09-17 10:48:00 -04004778 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004779 return 1;
4780 return 0;
4781}
4782
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004783static void btrfs_writeback_inodes_sb_nr(struct btrfs_fs_info *fs_info,
Miao Xie6c255e62014-03-06 13:55:01 +08004784 unsigned long nr_pages, int nr_items)
Miao Xieda633a42012-12-20 11:19:09 +00004785{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004786 struct super_block *sb = fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00004787
Josef Bacik925a6ef2013-06-20 12:31:27 -04004788 if (down_read_trylock(&sb->s_umount)) {
4789 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4790 up_read(&sb->s_umount);
4791 } else {
Miao Xieda633a42012-12-20 11:19:09 +00004792 /*
4793 * We needn't worry the filesystem going from r/w to r/o though
4794 * we don't acquire ->s_umount mutex, because the filesystem
4795 * should guarantee the delalloc inodes list be empty after
4796 * the filesystem is readonly(all dirty pages are written to
4797 * the disk).
4798 */
Nikolay Borisov82b3e532018-04-23 10:54:13 +03004799 btrfs_start_delalloc_roots(fs_info, nr_items);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004800 if (!current->journal_info)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004801 btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1);
Miao Xieda633a42012-12-20 11:19:09 +00004802 }
4803}
4804
Chris Mason6374e57a2017-06-23 09:48:21 -07004805static inline u64 calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004806 u64 to_reclaim)
Miao Xie18cd8ea2013-11-04 23:13:22 +08004807{
4808 u64 bytes;
Chris Mason6374e57a2017-06-23 09:48:21 -07004809 u64 nr;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004810
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004811 bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Chris Mason6374e57a2017-06-23 09:48:21 -07004812 nr = div64_u64(to_reclaim, bytes);
Miao Xie18cd8ea2013-11-04 23:13:22 +08004813 if (!nr)
4814 nr = 1;
4815 return nr;
4816}
4817
Byongho Leeee221842015-12-15 01:42:10 +09004818#define EXTENT_SIZE_PER_ITEM SZ_256K
Miao Xiec61a16a2013-11-04 23:13:23 +08004819
Yan, Zheng5da9d012010-05-16 10:46:25 -04004820/*
4821 * shrink metadata reservation for delalloc
4822 */
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004823static void shrink_delalloc(struct btrfs_fs_info *fs_info, u64 to_reclaim,
4824 u64 orig, bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004825{
Josef Bacik0019f102010-10-15 15:18:40 -04004826 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004827 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004828 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004829 u64 max_reclaim;
Chris Mason6374e57a2017-06-23 09:48:21 -07004830 u64 items;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004831 long time_left;
Miao Xied3ee29e32013-11-04 23:13:20 +08004832 unsigned long nr_pages;
4833 int loops;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004834
Miao Xiec61a16a2013-11-04 23:13:23 +08004835 /* Calc the number of the pages we need flush for space reservation */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004836 items = calc_reclaim_items_nr(fs_info, to_reclaim);
Chris Mason6374e57a2017-06-23 09:48:21 -07004837 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004838
Josef Bacik663350a2011-11-03 22:54:25 -04004839 trans = (struct btrfs_trans_handle *)current->journal_info;
Josef Bacik69fe2d72017-10-19 14:15:57 -04004840 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Chris Masonbf9022e2010-10-26 13:40:45 -04004841
Miao Xie963d6782013-01-29 10:10:51 +00004842 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004843 &fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004844 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004845 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004846 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004847 if (wait_ordered)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004848 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004849 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004850 }
4851
Miao Xied3ee29e32013-11-04 23:13:20 +08004852 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004853 while (delalloc_bytes && loops < 3) {
4854 max_reclaim = min(delalloc_bytes, to_reclaim);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004855 nr_pages = max_reclaim >> PAGE_SHIFT;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004856 btrfs_writeback_inodes_sb_nr(fs_info, nr_pages, items);
Josef Bacikdea31f52012-09-06 16:47:00 -04004857 /*
4858 * We need to wait for the async pages to actually start before
4859 * we do anything.
4860 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004861 max_reclaim = atomic_read(&fs_info->async_delalloc_pages);
Miao Xie9f3a0742013-11-04 23:13:24 +08004862 if (!max_reclaim)
4863 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004864
Miao Xie9f3a0742013-11-04 23:13:24 +08004865 if (max_reclaim <= nr_pages)
4866 max_reclaim = 0;
4867 else
4868 max_reclaim -= nr_pages;
4869
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004870 wait_event(fs_info->async_submit_wait,
4871 atomic_read(&fs_info->async_delalloc_pages) <=
Miao Xie9f3a0742013-11-04 23:13:24 +08004872 (int)max_reclaim);
4873skip_async:
Josef Bacik0019f102010-10-15 15:18:40 -04004874 spin_lock(&space_info->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04004875 if (list_empty(&space_info->tickets) &&
4876 list_empty(&space_info->priority_tickets)) {
4877 spin_unlock(&space_info->lock);
4878 break;
4879 }
Josef Bacik0019f102010-10-15 15:18:40 -04004880 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004881
Chris Mason36e39c42011-03-12 07:08:42 -05004882 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004883 if (wait_ordered && !trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004884 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf104d042011-10-14 13:56:58 -04004885 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004886 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004887 if (time_left)
4888 break;
4889 }
Miao Xie963d6782013-01-29 10:10:51 +00004890 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004891 &fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004892 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004893}
4894
Josef Bacik996478c2017-08-22 16:00:39 -04004895struct reserve_ticket {
4896 u64 bytes;
4897 int error;
4898 struct list_head list;
4899 wait_queue_head_t wait;
4900};
4901
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004902/**
Josef Bacik663350a2011-11-03 22:54:25 -04004903 * maybe_commit_transaction - possibly commit the transaction if its ok to
4904 * @root - the root we're allocating for
4905 * @bytes - the number of bytes we want to reserve
4906 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004907 *
Josef Bacik663350a2011-11-03 22:54:25 -04004908 * This will check to make sure that committing the transaction will actually
4909 * get us somewhere and then commit the transaction if it does. Otherwise it
4910 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004911 */
Jeff Mahoney0c9ab342017-02-15 16:28:28 -05004912static int may_commit_transaction(struct btrfs_fs_info *fs_info,
Josef Bacik996478c2017-08-22 16:00:39 -04004913 struct btrfs_space_info *space_info)
Josef Bacik663350a2011-11-03 22:54:25 -04004914{
Josef Bacik996478c2017-08-22 16:00:39 -04004915 struct reserve_ticket *ticket = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004916 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;
Josef Bacik663350a2011-11-03 22:54:25 -04004917 struct btrfs_trans_handle *trans;
Josef Bacik996478c2017-08-22 16:00:39 -04004918 u64 bytes;
Josef Bacik663350a2011-11-03 22:54:25 -04004919
4920 trans = (struct btrfs_trans_handle *)current->journal_info;
4921 if (trans)
4922 return -EAGAIN;
4923
Josef Bacik996478c2017-08-22 16:00:39 -04004924 spin_lock(&space_info->lock);
4925 if (!list_empty(&space_info->priority_tickets))
4926 ticket = list_first_entry(&space_info->priority_tickets,
4927 struct reserve_ticket, list);
4928 else if (!list_empty(&space_info->tickets))
4929 ticket = list_first_entry(&space_info->tickets,
4930 struct reserve_ticket, list);
4931 bytes = (ticket) ? ticket->bytes : 0;
4932 spin_unlock(&space_info->lock);
4933
4934 if (!bytes)
4935 return 0;
Josef Bacik663350a2011-11-03 22:54:25 -04004936
4937 /* See if there is enough pinned space to make this reservation */
Josef Bacikb150a4f2013-06-19 15:00:04 -04004938 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Miao Xie0424c542014-03-06 13:54:59 +08004939 bytes) >= 0)
Josef Bacik663350a2011-11-03 22:54:25 -04004940 goto commit;
Josef Bacik663350a2011-11-03 22:54:25 -04004941
4942 /*
4943 * See if there is some space in the delayed insertion reservation for
4944 * this reservation.
4945 */
4946 if (space_info != delayed_rsv->space_info)
4947 return -ENOSPC;
4948
4949 spin_lock(&delayed_rsv->lock);
Josef Bacik996478c2017-08-22 16:00:39 -04004950 if (delayed_rsv->size > bytes)
4951 bytes = 0;
4952 else
4953 bytes -= delayed_rsv->size;
Nikolay Borisov057aac32017-11-07 11:22:54 +02004954 spin_unlock(&delayed_rsv->lock);
4955
Josef Bacikb150a4f2013-06-19 15:00:04 -04004956 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Josef Bacik996478c2017-08-22 16:00:39 -04004957 bytes) < 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004958 return -ENOSPC;
4959 }
Josef Bacik663350a2011-11-03 22:54:25 -04004960
4961commit:
Jeff Mahoneya9b33112017-05-17 11:38:34 -04004962 trans = btrfs_join_transaction(fs_info->extent_root);
Josef Bacik663350a2011-11-03 22:54:25 -04004963 if (IS_ERR(trans))
4964 return -ENOSPC;
4965
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004966 return btrfs_commit_transaction(trans);
Josef Bacik663350a2011-11-03 22:54:25 -04004967}
4968
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03004969/*
4970 * Try to flush some data based on policy set by @state. This is only advisory
4971 * and may fail for various reasons. The caller is supposed to examine the
4972 * state of @space_info to detect the outcome.
4973 */
4974static void flush_space(struct btrfs_fs_info *fs_info,
Josef Bacik96c3f432012-06-21 14:05:49 -04004975 struct btrfs_space_info *space_info, u64 num_bytes,
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03004976 int state)
Josef Bacik96c3f432012-06-21 14:05:49 -04004977{
Jeff Mahoneya9b33112017-05-17 11:38:34 -04004978 struct btrfs_root *root = fs_info->extent_root;
Josef Bacik96c3f432012-06-21 14:05:49 -04004979 struct btrfs_trans_handle *trans;
4980 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004981 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004982
4983 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004984 case FLUSH_DELAYED_ITEMS_NR:
4985 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08004986 if (state == FLUSH_DELAYED_ITEMS_NR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004987 nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004988 else
Josef Bacik96c3f432012-06-21 14:05:49 -04004989 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004990
Josef Bacik96c3f432012-06-21 14:05:49 -04004991 trans = btrfs_join_transaction(root);
4992 if (IS_ERR(trans)) {
4993 ret = PTR_ERR(trans);
4994 break;
4995 }
Nikolay Borisove5c304e62018-02-07 17:55:43 +02004996 ret = btrfs_run_delayed_items_nr(trans, nr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004997 btrfs_end_transaction(trans);
Josef Bacik96c3f432012-06-21 14:05:49 -04004998 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004999 case FLUSH_DELALLOC:
5000 case FLUSH_DELALLOC_WAIT:
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005001 shrink_delalloc(fs_info, num_bytes * 2, num_bytes,
Josef Bacik67b0fd62012-09-24 13:42:00 -04005002 state == FLUSH_DELALLOC_WAIT);
5003 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04005004 case ALLOC_CHUNK:
5005 trans = btrfs_join_transaction(root);
5006 if (IS_ERR(trans)) {
5007 ret = PTR_ERR(trans);
5008 break;
5009 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005010 ret = do_chunk_alloc(trans, fs_info,
Jeff Mahoney1b868262017-05-17 11:38:35 -04005011 btrfs_metadata_alloc_profile(fs_info),
Josef Bacikea658ba2012-09-11 16:57:25 -04005012 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005013 btrfs_end_transaction(trans);
Alex Lyakaseecba892015-12-06 12:32:31 +02005014 if (ret > 0 || ret == -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04005015 ret = 0;
5016 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04005017 case COMMIT_TRANS:
Josef Bacik996478c2017-08-22 16:00:39 -04005018 ret = may_commit_transaction(fs_info, space_info);
Josef Bacik96c3f432012-06-21 14:05:49 -04005019 break;
5020 default:
5021 ret = -ENOSPC;
5022 break;
5023 }
5024
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005025 trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, state,
5026 ret);
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03005027 return;
Josef Bacik96c3f432012-06-21 14:05:49 -04005028}
Miao Xie21c7e752014-05-13 17:29:04 -07005029
5030static inline u64
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005031btrfs_calc_reclaim_metadata_size(struct btrfs_fs_info *fs_info,
5032 struct btrfs_space_info *space_info,
5033 bool system_chunk)
Miao Xie21c7e752014-05-13 17:29:04 -07005034{
Josef Bacik957780e2016-05-17 13:30:55 -04005035 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07005036 u64 used;
5037 u64 expected;
Josef Bacik957780e2016-05-17 13:30:55 -04005038 u64 to_reclaim = 0;
Miao Xie21c7e752014-05-13 17:29:04 -07005039
Josef Bacik957780e2016-05-17 13:30:55 -04005040 list_for_each_entry(ticket, &space_info->tickets, list)
5041 to_reclaim += ticket->bytes;
5042 list_for_each_entry(ticket, &space_info->priority_tickets, list)
5043 to_reclaim += ticket->bytes;
5044 if (to_reclaim)
5045 return to_reclaim;
Miao Xie21c7e752014-05-13 17:29:04 -07005046
Wang Xiaoguange0af2482016-08-31 19:46:16 +08005047 to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005048 if (can_overcommit(fs_info, space_info, to_reclaim,
5049 BTRFS_RESERVE_FLUSH_ALL, system_chunk))
Wang Xiaoguange0af2482016-08-31 19:46:16 +08005050 return 0;
5051
Nikolay Borisov0eee8a42017-06-14 11:35:34 +03005052 used = btrfs_space_info_used(space_info, true);
5053
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005054 if (can_overcommit(fs_info, space_info, SZ_1M,
5055 BTRFS_RESERVE_FLUSH_ALL, system_chunk))
Miao Xie21c7e752014-05-13 17:29:04 -07005056 expected = div_factor_fine(space_info->total_bytes, 95);
5057 else
5058 expected = div_factor_fine(space_info->total_bytes, 90);
5059
5060 if (used > expected)
5061 to_reclaim = used - expected;
5062 else
5063 to_reclaim = 0;
5064 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
5065 space_info->bytes_reserved);
Miao Xie21c7e752014-05-13 17:29:04 -07005066 return to_reclaim;
5067}
5068
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005069static inline int need_do_async_reclaim(struct btrfs_fs_info *fs_info,
5070 struct btrfs_space_info *space_info,
5071 u64 used, bool system_chunk)
Miao Xie21c7e752014-05-13 17:29:04 -07005072{
Josef Bacik365c5312015-02-18 13:58:15 -08005073 u64 thresh = div_factor_fine(space_info->total_bytes, 98);
5074
5075 /* If we're just plain full then async reclaim just slows us down. */
Josef Bacikbaee8792016-01-26 09:35:38 -05005076 if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh)
Josef Bacik365c5312015-02-18 13:58:15 -08005077 return 0;
5078
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005079 if (!btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5080 system_chunk))
Liu Bo25ce4592014-09-10 12:58:50 +08005081 return 0;
Liu Bo25ce4592014-09-10 12:58:50 +08005082
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005083 return (used >= thresh && !btrfs_fs_closing(fs_info) &&
5084 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
Miao Xie21c7e752014-05-13 17:29:04 -07005085}
5086
Josef Bacik957780e2016-05-17 13:30:55 -04005087static void wake_all_tickets(struct list_head *head)
Miao Xie21c7e752014-05-13 17:29:04 -07005088{
Josef Bacik957780e2016-05-17 13:30:55 -04005089 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07005090
Josef Bacik957780e2016-05-17 13:30:55 -04005091 while (!list_empty(head)) {
5092 ticket = list_first_entry(head, struct reserve_ticket, list);
5093 list_del_init(&ticket->list);
5094 ticket->error = -ENOSPC;
5095 wake_up(&ticket->wait);
Miao Xie21c7e752014-05-13 17:29:04 -07005096 }
Miao Xie21c7e752014-05-13 17:29:04 -07005097}
5098
Josef Bacik957780e2016-05-17 13:30:55 -04005099/*
5100 * This is for normal flushers, we can wait all goddamned day if we want to. We
5101 * will loop and continuously try to flush as long as we are making progress.
5102 * We count progress as clearing off tickets each time we have to loop.
5103 */
Miao Xie21c7e752014-05-13 17:29:04 -07005104static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
5105{
5106 struct btrfs_fs_info *fs_info;
5107 struct btrfs_space_info *space_info;
5108 u64 to_reclaim;
5109 int flush_state;
Josef Bacik957780e2016-05-17 13:30:55 -04005110 int commit_cycles = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005111 u64 last_tickets_id;
Miao Xie21c7e752014-05-13 17:29:04 -07005112
5113 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
5114 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
5115
Josef Bacik957780e2016-05-17 13:30:55 -04005116 spin_lock(&space_info->lock);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005117 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5118 false);
Josef Bacik957780e2016-05-17 13:30:55 -04005119 if (!to_reclaim) {
5120 space_info->flush = 0;
5121 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005122 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005123 }
Wang Xiaoguangce129652016-09-02 10:58:46 +08005124 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005125 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005126
5127 flush_state = FLUSH_DELAYED_ITEMS_NR;
5128 do {
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03005129 flush_space(fs_info, space_info, to_reclaim, flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005130 spin_lock(&space_info->lock);
5131 if (list_empty(&space_info->tickets)) {
5132 space_info->flush = 0;
5133 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005134 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005135 }
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005136 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info,
5137 space_info,
5138 false);
Wang Xiaoguangce129652016-09-02 10:58:46 +08005139 if (last_tickets_id == space_info->tickets_id) {
Josef Bacik957780e2016-05-17 13:30:55 -04005140 flush_state++;
5141 } else {
Wang Xiaoguangce129652016-09-02 10:58:46 +08005142 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005143 flush_state = FLUSH_DELAYED_ITEMS_NR;
5144 if (commit_cycles)
5145 commit_cycles--;
5146 }
5147
5148 if (flush_state > COMMIT_TRANS) {
5149 commit_cycles++;
5150 if (commit_cycles > 2) {
5151 wake_all_tickets(&space_info->tickets);
5152 space_info->flush = 0;
5153 } else {
5154 flush_state = FLUSH_DELAYED_ITEMS_NR;
5155 }
5156 }
5157 spin_unlock(&space_info->lock);
5158 } while (flush_state <= COMMIT_TRANS);
Miao Xie21c7e752014-05-13 17:29:04 -07005159}
5160
5161void btrfs_init_async_reclaim_work(struct work_struct *work)
5162{
5163 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
5164}
5165
Josef Bacik957780e2016-05-17 13:30:55 -04005166static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
5167 struct btrfs_space_info *space_info,
5168 struct reserve_ticket *ticket)
5169{
5170 u64 to_reclaim;
5171 int flush_state = FLUSH_DELAYED_ITEMS_NR;
5172
5173 spin_lock(&space_info->lock);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005174 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5175 false);
Josef Bacik957780e2016-05-17 13:30:55 -04005176 if (!to_reclaim) {
5177 spin_unlock(&space_info->lock);
5178 return;
5179 }
5180 spin_unlock(&space_info->lock);
5181
5182 do {
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005183 flush_space(fs_info, space_info, to_reclaim, flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005184 flush_state++;
5185 spin_lock(&space_info->lock);
5186 if (ticket->bytes == 0) {
5187 spin_unlock(&space_info->lock);
5188 return;
5189 }
5190 spin_unlock(&space_info->lock);
5191
5192 /*
5193 * Priority flushers can't wait on delalloc without
5194 * deadlocking.
5195 */
5196 if (flush_state == FLUSH_DELALLOC ||
5197 flush_state == FLUSH_DELALLOC_WAIT)
5198 flush_state = ALLOC_CHUNK;
5199 } while (flush_state < COMMIT_TRANS);
5200}
5201
5202static int wait_reserve_ticket(struct btrfs_fs_info *fs_info,
5203 struct btrfs_space_info *space_info,
5204 struct reserve_ticket *ticket, u64 orig_bytes)
5205
5206{
5207 DEFINE_WAIT(wait);
5208 int ret = 0;
5209
5210 spin_lock(&space_info->lock);
5211 while (ticket->bytes > 0 && ticket->error == 0) {
5212 ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE);
5213 if (ret) {
5214 ret = -EINTR;
5215 break;
5216 }
5217 spin_unlock(&space_info->lock);
5218
5219 schedule();
5220
5221 finish_wait(&ticket->wait, &wait);
5222 spin_lock(&space_info->lock);
5223 }
5224 if (!ret)
5225 ret = ticket->error;
5226 if (!list_empty(&ticket->list))
5227 list_del_init(&ticket->list);
5228 if (ticket->bytes && ticket->bytes < orig_bytes) {
5229 u64 num_bytes = orig_bytes - ticket->bytes;
5230 space_info->bytes_may_use -= num_bytes;
5231 trace_btrfs_space_reservation(fs_info, "space_info",
5232 space_info->flags, num_bytes, 0);
5233 }
5234 spin_unlock(&space_info->lock);
5235
5236 return ret;
5237}
5238
Josef Bacik663350a2011-11-03 22:54:25 -04005239/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005240 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5241 * @root - the root we're allocating for
Josef Bacik957780e2016-05-17 13:30:55 -04005242 * @space_info - the space info we want to allocate from
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005243 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04005244 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005245 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04005246 * This will reserve orig_bytes number of bytes from the space info associated
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005247 * with the block_rsv. If there is not enough space it will make an attempt to
5248 * flush out space to make room. It will do this by flushing delalloc if
5249 * possible or committing the transaction. If flush is 0 then no attempts to
5250 * regain reservations will be made and this will fail if there is not enough
5251 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005252 */
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005253static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
Josef Bacik957780e2016-05-17 13:30:55 -04005254 struct btrfs_space_info *space_info,
5255 u64 orig_bytes,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005256 enum btrfs_reserve_flush_enum flush,
5257 bool system_chunk)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005258{
Josef Bacik957780e2016-05-17 13:30:55 -04005259 struct reserve_ticket ticket;
Josef Bacik2bf64752011-09-26 17:12:22 -04005260 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005261 int ret = 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005262
Josef Bacik957780e2016-05-17 13:30:55 -04005263 ASSERT(orig_bytes);
Josef Bacik8ca17f02016-05-27 13:24:13 -04005264 ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL);
5265
Yan, Zhengf0486c62010-05-16 10:46:25 -04005266 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04005267 ret = -ENOSPC;
Liu Bo41361352017-02-13 15:42:21 -08005268 used = btrfs_space_info_used(space_info, true);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005269
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005270 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005271 * If we have enough space then hooray, make our reservation and carry
5272 * on. If not see if we can overcommit, and if we can, hooray carry on.
5273 * If not things get more complicated.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005274 */
Josef Bacik957780e2016-05-17 13:30:55 -04005275 if (used + orig_bytes <= space_info->total_bytes) {
5276 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005277 trace_btrfs_space_reservation(fs_info, "space_info",
5278 space_info->flags, orig_bytes, 1);
Josef Bacik957780e2016-05-17 13:30:55 -04005279 ret = 0;
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005280 } else if (can_overcommit(fs_info, space_info, orig_bytes, flush,
5281 system_chunk)) {
Josef Bacik44734ed2012-09-28 16:04:19 -04005282 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005283 trace_btrfs_space_reservation(fs_info, "space_info",
5284 space_info->flags, orig_bytes, 1);
Josef Bacik44734ed2012-09-28 16:04:19 -04005285 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04005286 }
5287
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005288 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005289 * If we couldn't make a reservation then setup our reservation ticket
5290 * and kick the async worker if it's not already running.
Miao Xie08e007d2012-10-16 11:33:38 +00005291 *
Josef Bacik957780e2016-05-17 13:30:55 -04005292 * If we are a priority flusher then we just need to add our ticket to
5293 * the list and we will do our own flushing further down.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005294 */
Josef Bacik72bcd992012-12-18 15:16:34 -05005295 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacik957780e2016-05-17 13:30:55 -04005296 ticket.bytes = orig_bytes;
5297 ticket.error = 0;
5298 init_waitqueue_head(&ticket.wait);
5299 if (flush == BTRFS_RESERVE_FLUSH_ALL) {
5300 list_add_tail(&ticket.list, &space_info->tickets);
5301 if (!space_info->flush) {
5302 space_info->flush = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005303 trace_btrfs_trigger_flush(fs_info,
Josef Bacikf376df22016-03-25 13:25:56 -04005304 space_info->flags,
5305 orig_bytes, flush,
5306 "enospc");
Josef Bacik957780e2016-05-17 13:30:55 -04005307 queue_work(system_unbound_wq,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005308 &fs_info->async_reclaim_work);
Josef Bacik957780e2016-05-17 13:30:55 -04005309 }
5310 } else {
5311 list_add_tail(&ticket.list,
5312 &space_info->priority_tickets);
5313 }
Miao Xie21c7e752014-05-13 17:29:04 -07005314 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
5315 used += orig_bytes;
Josef Bacikf6acfd52014-09-18 11:27:17 -04005316 /*
5317 * We will do the space reservation dance during log replay,
5318 * which means we won't have fs_info->fs_root set, so don't do
5319 * the async reclaim as we will panic.
5320 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005321 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) &&
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005322 need_do_async_reclaim(fs_info, space_info,
5323 used, system_chunk) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005324 !work_busy(&fs_info->async_reclaim_work)) {
5325 trace_btrfs_trigger_flush(fs_info, space_info->flags,
5326 orig_bytes, flush, "preempt");
Miao Xie21c7e752014-05-13 17:29:04 -07005327 queue_work(system_unbound_wq,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005328 &fs_info->async_reclaim_work);
Josef Bacikf376df22016-03-25 13:25:56 -04005329 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005330 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005331 spin_unlock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00005332 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik957780e2016-05-17 13:30:55 -04005333 return ret;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005334
Josef Bacik957780e2016-05-17 13:30:55 -04005335 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005336 return wait_reserve_ticket(fs_info, space_info, &ticket,
Josef Bacik957780e2016-05-17 13:30:55 -04005337 orig_bytes);
Miao Xie08e007d2012-10-16 11:33:38 +00005338
Josef Bacik957780e2016-05-17 13:30:55 -04005339 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005340 priority_reclaim_metadata_space(fs_info, space_info, &ticket);
Josef Bacik957780e2016-05-17 13:30:55 -04005341 spin_lock(&space_info->lock);
5342 if (ticket.bytes) {
5343 if (ticket.bytes < orig_bytes) {
5344 u64 num_bytes = orig_bytes - ticket.bytes;
5345 space_info->bytes_may_use -= num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005346 trace_btrfs_space_reservation(fs_info, "space_info",
5347 space_info->flags,
5348 num_bytes, 0);
Miao Xie08e007d2012-10-16 11:33:38 +00005349
Josef Bacik957780e2016-05-17 13:30:55 -04005350 }
5351 list_del_init(&ticket.list);
5352 ret = -ENOSPC;
5353 }
5354 spin_unlock(&space_info->lock);
5355 ASSERT(list_empty(&ticket.list));
5356 return ret;
5357}
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005358
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005359/**
5360 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5361 * @root - the root we're allocating for
5362 * @block_rsv - the block_rsv we're allocating for
5363 * @orig_bytes - the number of bytes we want
5364 * @flush - whether or not we can flush to make our reservation
5365 *
Josef Bacik957780e2016-05-17 13:30:55 -04005366 * This will reserve orgi_bytes number of bytes from the space info associated
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005367 * with the block_rsv. If there is not enough space it will make an attempt to
5368 * flush out space to make room. It will do this by flushing delalloc if
5369 * possible or committing the transaction. If flush is 0 then no attempts to
5370 * regain reservations will be made and this will fail if there is not enough
5371 * space already.
5372 */
5373static int reserve_metadata_bytes(struct btrfs_root *root,
5374 struct btrfs_block_rsv *block_rsv,
5375 u64 orig_bytes,
5376 enum btrfs_reserve_flush_enum flush)
5377{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005378 struct btrfs_fs_info *fs_info = root->fs_info;
5379 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04005380 int ret;
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005381 bool system_chunk = (root == fs_info->chunk_root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005382
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005383 ret = __reserve_metadata_bytes(fs_info, block_rsv->space_info,
5384 orig_bytes, flush, system_chunk);
Josef Bacik5d803662013-02-07 16:06:02 -05005385 if (ret == -ENOSPC &&
5386 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
Josef Bacik5d803662013-02-07 16:06:02 -05005387 if (block_rsv != global_rsv &&
5388 !block_rsv_use_bytes(global_rsv, orig_bytes))
5389 ret = 0;
5390 }
Nikolay Borisov9a3daff2017-12-15 12:05:37 +02005391 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005392 trace_btrfs_space_reservation(fs_info, "space_info:enospc",
Josef Bacik957780e2016-05-17 13:30:55 -04005393 block_rsv->space_info->flags,
5394 orig_bytes, 1);
Nikolay Borisov9a3daff2017-12-15 12:05:37 +02005395
5396 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
5397 dump_space_info(fs_info, block_rsv->space_info,
5398 orig_bytes, 0);
5399 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005400 return ret;
5401}
5402
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005403static struct btrfs_block_rsv *get_block_rsv(
5404 const struct btrfs_trans_handle *trans,
5405 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005406{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005407 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik4c13d752011-08-30 11:31:29 -04005408 struct btrfs_block_rsv *block_rsv = NULL;
5409
Alexandru Moisee9cf4392015-09-09 00:18:50 +00005410 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005411 (root == fs_info->csum_root && trans->adding_csums) ||
5412 (root == fs_info->uuid_root))
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02005413 block_rsv = trans->block_rsv;
5414
Josef Bacik4c13d752011-08-30 11:31:29 -04005415 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005416 block_rsv = root->block_rsv;
5417
5418 if (!block_rsv)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005419 block_rsv = &fs_info->empty_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005420
5421 return block_rsv;
5422}
5423
5424static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
5425 u64 num_bytes)
5426{
5427 int ret = -ENOSPC;
5428 spin_lock(&block_rsv->lock);
5429 if (block_rsv->reserved >= num_bytes) {
5430 block_rsv->reserved -= num_bytes;
5431 if (block_rsv->reserved < block_rsv->size)
5432 block_rsv->full = 0;
5433 ret = 0;
5434 }
5435 spin_unlock(&block_rsv->lock);
5436 return ret;
5437}
5438
5439static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
5440 u64 num_bytes, int update_size)
5441{
5442 spin_lock(&block_rsv->lock);
5443 block_rsv->reserved += num_bytes;
5444 if (update_size)
5445 block_rsv->size += num_bytes;
5446 else if (block_rsv->reserved >= block_rsv->size)
5447 block_rsv->full = 1;
5448 spin_unlock(&block_rsv->lock);
5449}
5450
Josef Bacikd52be812013-05-29 14:54:47 -04005451int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
5452 struct btrfs_block_rsv *dest, u64 num_bytes,
5453 int min_factor)
5454{
5455 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5456 u64 min_bytes;
5457
5458 if (global_rsv->space_info != dest->space_info)
5459 return -ENOSPC;
5460
5461 spin_lock(&global_rsv->lock);
5462 min_bytes = div_factor(global_rsv->size, min_factor);
5463 if (global_rsv->reserved < min_bytes + num_bytes) {
5464 spin_unlock(&global_rsv->lock);
5465 return -ENOSPC;
5466 }
5467 global_rsv->reserved -= num_bytes;
5468 if (global_rsv->reserved < global_rsv->size)
5469 global_rsv->full = 0;
5470 spin_unlock(&global_rsv->lock);
5471
5472 block_rsv_add_bytes(dest, num_bytes, 1);
5473 return 0;
5474}
5475
Josef Bacik957780e2016-05-17 13:30:55 -04005476/*
5477 * This is for space we already have accounted in space_info->bytes_may_use, so
5478 * basically when we're returning space from block_rsv's.
5479 */
5480static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
5481 struct btrfs_space_info *space_info,
5482 u64 num_bytes)
5483{
5484 struct reserve_ticket *ticket;
5485 struct list_head *head;
5486 u64 used;
5487 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH;
5488 bool check_overcommit = false;
5489
5490 spin_lock(&space_info->lock);
5491 head = &space_info->priority_tickets;
5492
5493 /*
5494 * If we are over our limit then we need to check and see if we can
5495 * overcommit, and if we can't then we just need to free up our space
5496 * and not satisfy any requests.
5497 */
Nikolay Borisov0eee8a42017-06-14 11:35:34 +03005498 used = btrfs_space_info_used(space_info, true);
Josef Bacik957780e2016-05-17 13:30:55 -04005499 if (used - num_bytes >= space_info->total_bytes)
5500 check_overcommit = true;
5501again:
5502 while (!list_empty(head) && num_bytes) {
5503 ticket = list_first_entry(head, struct reserve_ticket,
5504 list);
5505 /*
5506 * We use 0 bytes because this space is already reserved, so
5507 * adding the ticket space would be a double count.
5508 */
5509 if (check_overcommit &&
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005510 !can_overcommit(fs_info, space_info, 0, flush, false))
Josef Bacik957780e2016-05-17 13:30:55 -04005511 break;
5512 if (num_bytes >= ticket->bytes) {
5513 list_del_init(&ticket->list);
5514 num_bytes -= ticket->bytes;
5515 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005516 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005517 wake_up(&ticket->wait);
5518 } else {
5519 ticket->bytes -= num_bytes;
5520 num_bytes = 0;
5521 }
5522 }
5523
5524 if (num_bytes && head == &space_info->priority_tickets) {
5525 head = &space_info->tickets;
5526 flush = BTRFS_RESERVE_FLUSH_ALL;
5527 goto again;
5528 }
5529 space_info->bytes_may_use -= num_bytes;
5530 trace_btrfs_space_reservation(fs_info, "space_info",
5531 space_info->flags, num_bytes, 0);
5532 spin_unlock(&space_info->lock);
5533}
5534
5535/*
5536 * This is for newly allocated space that isn't accounted in
5537 * space_info->bytes_may_use yet. So if we allocate a chunk or unpin an extent
5538 * we use this helper.
5539 */
5540static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
5541 struct btrfs_space_info *space_info,
5542 u64 num_bytes)
5543{
5544 struct reserve_ticket *ticket;
5545 struct list_head *head = &space_info->priority_tickets;
5546
5547again:
5548 while (!list_empty(head) && num_bytes) {
5549 ticket = list_first_entry(head, struct reserve_ticket,
5550 list);
5551 if (num_bytes >= ticket->bytes) {
5552 trace_btrfs_space_reservation(fs_info, "space_info",
5553 space_info->flags,
5554 ticket->bytes, 1);
5555 list_del_init(&ticket->list);
5556 num_bytes -= ticket->bytes;
5557 space_info->bytes_may_use += ticket->bytes;
5558 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005559 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005560 wake_up(&ticket->wait);
5561 } else {
5562 trace_btrfs_space_reservation(fs_info, "space_info",
5563 space_info->flags,
5564 num_bytes, 1);
5565 space_info->bytes_may_use += num_bytes;
5566 ticket->bytes -= num_bytes;
5567 num_bytes = 0;
5568 }
5569 }
5570
5571 if (num_bytes && head == &space_info->priority_tickets) {
5572 head = &space_info->tickets;
5573 goto again;
5574 }
5575}
5576
Josef Bacik69fe2d72017-10-19 14:15:57 -04005577static u64 block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005578 struct btrfs_block_rsv *block_rsv,
Qu Wenruoff6bc372017-12-21 13:42:04 +08005579 struct btrfs_block_rsv *dest, u64 num_bytes,
5580 u64 *qgroup_to_release_ret)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005581{
5582 struct btrfs_space_info *space_info = block_rsv->space_info;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005583 u64 qgroup_to_release = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005584 u64 ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005585
5586 spin_lock(&block_rsv->lock);
Qu Wenruoff6bc372017-12-21 13:42:04 +08005587 if (num_bytes == (u64)-1) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005588 num_bytes = block_rsv->size;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005589 qgroup_to_release = block_rsv->qgroup_rsv_size;
5590 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005591 block_rsv->size -= num_bytes;
5592 if (block_rsv->reserved >= block_rsv->size) {
5593 num_bytes = block_rsv->reserved - block_rsv->size;
5594 block_rsv->reserved = block_rsv->size;
5595 block_rsv->full = 1;
5596 } else {
5597 num_bytes = 0;
5598 }
Qu Wenruoff6bc372017-12-21 13:42:04 +08005599 if (block_rsv->qgroup_rsv_reserved >= block_rsv->qgroup_rsv_size) {
5600 qgroup_to_release = block_rsv->qgroup_rsv_reserved -
5601 block_rsv->qgroup_rsv_size;
5602 block_rsv->qgroup_rsv_reserved = block_rsv->qgroup_rsv_size;
5603 } else {
5604 qgroup_to_release = 0;
5605 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005606 spin_unlock(&block_rsv->lock);
5607
Josef Bacik69fe2d72017-10-19 14:15:57 -04005608 ret = num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005609 if (num_bytes > 0) {
5610 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00005611 spin_lock(&dest->lock);
5612 if (!dest->full) {
5613 u64 bytes_to_add;
5614
5615 bytes_to_add = dest->size - dest->reserved;
5616 bytes_to_add = min(num_bytes, bytes_to_add);
5617 dest->reserved += bytes_to_add;
5618 if (dest->reserved >= dest->size)
5619 dest->full = 1;
5620 num_bytes -= bytes_to_add;
5621 }
5622 spin_unlock(&dest->lock);
5623 }
Josef Bacik957780e2016-05-17 13:30:55 -04005624 if (num_bytes)
5625 space_info_add_old_bytes(fs_info, space_info,
5626 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005627 }
Qu Wenruoff6bc372017-12-21 13:42:04 +08005628 if (qgroup_to_release_ret)
5629 *qgroup_to_release_ret = qgroup_to_release;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005630 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005631}
5632
Josef Bacik25d609f2016-03-25 13:25:48 -04005633int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
5634 struct btrfs_block_rsv *dst, u64 num_bytes,
5635 int update_size)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005636{
5637 int ret;
5638
5639 ret = block_rsv_use_bytes(src, num_bytes);
5640 if (ret)
5641 return ret;
5642
Josef Bacik25d609f2016-03-25 13:25:48 -04005643 block_rsv_add_bytes(dst, num_bytes, update_size);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005644 return 0;
5645}
5646
Miao Xie66d8f3d2012-09-06 04:02:28 -06005647void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005648{
5649 memset(rsv, 0, sizeof(*rsv));
5650 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06005651 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005652}
5653
Josef Bacik69fe2d72017-10-19 14:15:57 -04005654void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
5655 struct btrfs_block_rsv *rsv,
5656 unsigned short type)
5657{
5658 btrfs_init_block_rsv(rsv, type);
5659 rsv->space_info = __find_space_info(fs_info,
5660 BTRFS_BLOCK_GROUP_METADATA);
5661}
5662
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005663struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
Miao Xie66d8f3d2012-09-06 04:02:28 -06005664 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005665{
5666 struct btrfs_block_rsv *block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005667
5668 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
5669 if (!block_rsv)
5670 return NULL;
5671
Josef Bacik69fe2d72017-10-19 14:15:57 -04005672 btrfs_init_metadata_block_rsv(fs_info, block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005673 return block_rsv;
5674}
5675
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005676void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005677 struct btrfs_block_rsv *rsv)
5678{
Josef Bacik2aaa6652012-08-29 14:27:18 -04005679 if (!rsv)
5680 return;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005681 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Josef Bacikdabdb642011-08-08 12:50:18 -04005682 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005683}
5684
Chris Masoncdfb0802015-04-06 18:17:00 -07005685void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
5686{
5687 kfree(rsv);
5688}
5689
Miao Xie08e007d2012-10-16 11:33:38 +00005690int btrfs_block_rsv_add(struct btrfs_root *root,
5691 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
5692 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005693{
5694 int ret;
5695
5696 if (num_bytes == 0)
5697 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005698
Miao Xie61b520a2011-11-10 20:45:05 -05005699 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005700 if (!ret) {
5701 block_rsv_add_bytes(block_rsv, num_bytes, 1);
5702 return 0;
5703 }
5704
Yan, Zhengf0486c62010-05-16 10:46:25 -04005705 return ret;
5706}
5707
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005708int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005709{
5710 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005711 int ret = -ENOSPC;
5712
5713 if (!block_rsv)
5714 return 0;
5715
5716 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04005717 num_bytes = div_factor(block_rsv->size, min_factor);
5718 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005719 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005720 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005721
Josef Bacik36ba0222011-10-18 12:15:48 -04005722 return ret;
5723}
5724
Miao Xie08e007d2012-10-16 11:33:38 +00005725int btrfs_block_rsv_refill(struct btrfs_root *root,
5726 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
5727 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04005728{
5729 u64 num_bytes = 0;
5730 int ret = -ENOSPC;
5731
5732 if (!block_rsv)
5733 return 0;
5734
5735 spin_lock(&block_rsv->lock);
5736 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04005737 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005738 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04005739 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04005740 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005741 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04005742
Yan, Zhengf0486c62010-05-16 10:46:25 -04005743 if (!ret)
5744 return 0;
5745
Miao Xieaa38a712011-11-18 17:43:00 +08005746 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04005747 if (!ret) {
5748 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005749 return 0;
5750 }
5751
Josef Bacik13553e52011-08-08 13:33:21 -04005752 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005753}
5754
Josef Bacik69fe2d72017-10-19 14:15:57 -04005755/**
5756 * btrfs_inode_rsv_refill - refill the inode block rsv.
5757 * @inode - the inode we are refilling.
5758 * @flush - the flusing restriction.
5759 *
5760 * Essentially the same as btrfs_block_rsv_refill, except it uses the
5761 * block_rsv->size as the minimum size. We'll either refill the missing amount
5762 * or return if we already have enough space. This will also handle the resreve
5763 * tracepoint for the reserved amount.
5764 */
Qu Wenruo3f2dd7a2017-11-17 15:14:19 +08005765static int btrfs_inode_rsv_refill(struct btrfs_inode *inode,
5766 enum btrfs_reserve_flush_enum flush)
Josef Bacik69fe2d72017-10-19 14:15:57 -04005767{
5768 struct btrfs_root *root = inode->root;
5769 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
5770 u64 num_bytes = 0;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005771 u64 qgroup_num_bytes = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005772 int ret = -ENOSPC;
5773
5774 spin_lock(&block_rsv->lock);
5775 if (block_rsv->reserved < block_rsv->size)
5776 num_bytes = block_rsv->size - block_rsv->reserved;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005777 if (block_rsv->qgroup_rsv_reserved < block_rsv->qgroup_rsv_size)
5778 qgroup_num_bytes = block_rsv->qgroup_rsv_size -
5779 block_rsv->qgroup_rsv_reserved;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005780 spin_unlock(&block_rsv->lock);
5781
5782 if (num_bytes == 0)
5783 return 0;
5784
Qu Wenruoff6bc372017-12-21 13:42:04 +08005785 ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_num_bytes, true);
Qu Wenruo43b18592017-12-12 15:34:32 +08005786 if (ret)
5787 return ret;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005788 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
5789 if (!ret) {
5790 block_rsv_add_bytes(block_rsv, num_bytes, 0);
5791 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5792 btrfs_ino(inode), num_bytes, 1);
Qu Wenruoff6bc372017-12-21 13:42:04 +08005793
5794 /* Don't forget to increase qgroup_rsv_reserved */
5795 spin_lock(&block_rsv->lock);
5796 block_rsv->qgroup_rsv_reserved += qgroup_num_bytes;
5797 spin_unlock(&block_rsv->lock);
5798 } else
5799 btrfs_qgroup_free_meta_prealloc(root, qgroup_num_bytes);
Josef Bacik69fe2d72017-10-19 14:15:57 -04005800 return ret;
5801}
5802
5803/**
5804 * btrfs_inode_rsv_release - release any excessive reservation.
5805 * @inode - the inode we need to release from.
Qu Wenruo43b18592017-12-12 15:34:32 +08005806 * @qgroup_free - free or convert qgroup meta.
5807 * Unlike normal operation, qgroup meta reservation needs to know if we are
5808 * freeing qgroup reservation or just converting it into per-trans. Normally
5809 * @qgroup_free is true for error handling, and false for normal release.
Josef Bacik69fe2d72017-10-19 14:15:57 -04005810 *
5811 * This is the same as btrfs_block_rsv_release, except that it handles the
5812 * tracepoint for the reservation.
5813 */
Qu Wenruo43b18592017-12-12 15:34:32 +08005814static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
Josef Bacik69fe2d72017-10-19 14:15:57 -04005815{
5816 struct btrfs_fs_info *fs_info = inode->root->fs_info;
5817 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5818 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
5819 u64 released = 0;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005820 u64 qgroup_to_release = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005821
5822 /*
5823 * Since we statically set the block_rsv->size we just want to say we
5824 * are releasing 0 bytes, and then we'll just get the reservation over
5825 * the size free'd.
5826 */
Qu Wenruoff6bc372017-12-21 13:42:04 +08005827 released = block_rsv_release_bytes(fs_info, block_rsv, global_rsv, 0,
5828 &qgroup_to_release);
Josef Bacik69fe2d72017-10-19 14:15:57 -04005829 if (released > 0)
5830 trace_btrfs_space_reservation(fs_info, "delalloc",
5831 btrfs_ino(inode), released, 0);
Qu Wenruo43b18592017-12-12 15:34:32 +08005832 if (qgroup_free)
Qu Wenruoff6bc372017-12-21 13:42:04 +08005833 btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
Qu Wenruo43b18592017-12-12 15:34:32 +08005834 else
Qu Wenruoff6bc372017-12-21 13:42:04 +08005835 btrfs_qgroup_convert_reserved_meta(inode->root,
5836 qgroup_to_release);
Josef Bacik69fe2d72017-10-19 14:15:57 -04005837}
5838
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005839void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005840 struct btrfs_block_rsv *block_rsv,
5841 u64 num_bytes)
5842{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005843 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5844
Liu Bo17504582013-12-29 21:44:50 +08005845 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04005846 block_rsv->space_info != global_rsv->space_info)
5847 global_rsv = NULL;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005848 block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes, NULL);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005849}
5850
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005851static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
5852{
5853 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
5854 struct btrfs_space_info *sinfo = block_rsv->space_info;
5855 u64 num_bytes;
5856
Josef Bacikae2e4722016-05-27 12:58:35 -04005857 /*
5858 * The global block rsv is based on the size of the extent tree, the
5859 * checksum tree and the root tree. If the fs is empty we want to set
5860 * it to a minimal amount for safety.
5861 */
5862 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) +
5863 btrfs_root_used(&fs_info->csum_root->root_item) +
5864 btrfs_root_used(&fs_info->tree_root->root_item);
5865 num_bytes = max_t(u64, num_bytes, SZ_16M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005866
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005867 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005868 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005869
Byongho Leeee221842015-12-15 01:42:10 +09005870 block_rsv->size = min_t(u64, num_bytes, SZ_512M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005871
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005872 if (block_rsv->reserved < block_rsv->size) {
Liu Bo41361352017-02-13 15:42:21 -08005873 num_bytes = btrfs_space_info_used(sinfo, true);
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005874 if (sinfo->total_bytes > num_bytes) {
5875 num_bytes = sinfo->total_bytes - num_bytes;
5876 num_bytes = min(num_bytes,
5877 block_rsv->size - block_rsv->reserved);
5878 block_rsv->reserved += num_bytes;
5879 sinfo->bytes_may_use += num_bytes;
5880 trace_btrfs_space_reservation(fs_info, "space_info",
5881 sinfo->flags, num_bytes,
5882 1);
5883 }
5884 } else if (block_rsv->reserved > block_rsv->size) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005885 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04005886 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005887 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005888 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005889 block_rsv->reserved = block_rsv->size;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005890 }
David Sterba182608c2011-05-05 13:13:16 +02005891
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005892 if (block_rsv->reserved == block_rsv->size)
5893 block_rsv->full = 1;
5894 else
5895 block_rsv->full = 0;
5896
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005897 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005898 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005899}
5900
Yan, Zhengf0486c62010-05-16 10:46:25 -04005901static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
5902{
5903 struct btrfs_space_info *space_info;
5904
5905 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
5906 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005907
5908 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005909 fs_info->global_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005910 fs_info->trans_block_rsv.space_info = space_info;
5911 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04005912 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005913
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005914 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
5915 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
5916 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
5917 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00005918 if (fs_info->quota_root)
5919 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005920 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005921
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005922 update_global_block_rsv(fs_info);
5923}
5924
5925static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
5926{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005927 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
Qu Wenruoff6bc372017-12-21 13:42:04 +08005928 (u64)-1, NULL);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005929 WARN_ON(fs_info->trans_block_rsv.size > 0);
5930 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
5931 WARN_ON(fs_info->chunk_block_rsv.size > 0);
5932 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04005933 WARN_ON(fs_info->delayed_block_rsv.size > 0);
5934 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04005935}
5936
Yan, Zhenga22285a2010-05-16 10:48:46 -04005937
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005938/*
5939 * To be called after all the new block groups attached to the transaction
5940 * handle have been created (btrfs_create_pending_block_groups()).
5941 */
5942void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
5943{
Jeff Mahoney64b63582016-06-20 17:23:41 -04005944 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005945
5946 if (!trans->chunk_bytes_reserved)
5947 return;
5948
5949 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
5950
5951 block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL,
Qu Wenruoff6bc372017-12-21 13:42:04 +08005952 trans->chunk_bytes_reserved, NULL);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005953 trans->chunk_bytes_reserved = 0;
5954}
5955
Miao Xied5c12072013-02-28 10:04:33 +00005956/*
5957 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
5958 * root: the root of the parent directory
5959 * rsv: block reservation
5960 * items: the number of items that we need do reservation
5961 * qgroup_reserved: used to return the reserved size in qgroup
5962 *
5963 * This function is used to reserve the space for snapshot/subvolume
5964 * creation and deletion. Those operations are different with the
5965 * common file/directory operations, they change two fs/file trees
5966 * and root tree, the number of items that the qgroup reserves is
5967 * different with the free space reservation. So we can not use
Nicholas D Steeves01327612016-05-19 21:18:45 -04005968 * the space reservation mechanism in start_transaction().
Miao Xied5c12072013-02-28 10:04:33 +00005969 */
5970int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
5971 struct btrfs_block_rsv *rsv,
5972 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005973 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005974{
Miao Xied5c12072013-02-28 10:04:33 +00005975 u64 num_bytes;
5976 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005977 struct btrfs_fs_info *fs_info = root->fs_info;
5978 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00005979
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005980 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Miao Xied5c12072013-02-28 10:04:33 +00005981 /* One for parent inode, two for dir entries */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005982 num_bytes = 3 * fs_info->nodesize;
Qu Wenruo733e03a2017-12-12 15:34:29 +08005983 ret = btrfs_qgroup_reserve_meta_prealloc(root, num_bytes, true);
Miao Xied5c12072013-02-28 10:04:33 +00005984 if (ret)
5985 return ret;
5986 } else {
5987 num_bytes = 0;
5988 }
5989
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005990 num_bytes = btrfs_calc_trans_metadata_size(fs_info, items);
5991 rsv->space_info = __find_space_info(fs_info,
Miao Xied5c12072013-02-28 10:04:33 +00005992 BTRFS_BLOCK_GROUP_METADATA);
5993 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
5994 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005995
5996 if (ret == -ENOSPC && use_global_rsv)
Josef Bacik25d609f2016-03-25 13:25:48 -04005997 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005998
Gu JinXiangc4c129d2018-05-30 11:00:38 +08005999 if (ret && num_bytes)
6000 btrfs_qgroup_free_meta_prealloc(root, num_bytes);
Miao Xied5c12072013-02-28 10:04:33 +00006001
6002 return ret;
6003}
6004
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006005void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
David Sterba7775c812017-02-10 19:18:18 +01006006 struct btrfs_block_rsv *rsv)
Miao Xied5c12072013-02-28 10:04:33 +00006007{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006008 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006009}
6010
Josef Bacik69fe2d72017-10-19 14:15:57 -04006011static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
6012 struct btrfs_inode *inode)
Josef Bacik9e0baf62011-07-15 15:16:44 +00006013{
Josef Bacik69fe2d72017-10-19 14:15:57 -04006014 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
6015 u64 reserve_size = 0;
Qu Wenruoff6bc372017-12-21 13:42:04 +08006016 u64 qgroup_rsv_size = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04006017 u64 csum_leaves;
6018 unsigned outstanding_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006019
Josef Bacik69fe2d72017-10-19 14:15:57 -04006020 lockdep_assert_held(&inode->lock);
6021 outstanding_extents = inode->outstanding_extents;
6022 if (outstanding_extents)
6023 reserve_size = btrfs_calc_trans_metadata_size(fs_info,
6024 outstanding_extents + 1);
6025 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
6026 inode->csum_bytes);
6027 reserve_size += btrfs_calc_trans_metadata_size(fs_info,
6028 csum_leaves);
Qu Wenruoff6bc372017-12-21 13:42:04 +08006029 /*
6030 * For qgroup rsv, the calculation is very simple:
6031 * account one nodesize for each outstanding extent
6032 *
6033 * This is overestimating in most cases.
6034 */
6035 qgroup_rsv_size = outstanding_extents * fs_info->nodesize;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006036
Josef Bacik69fe2d72017-10-19 14:15:57 -04006037 spin_lock(&block_rsv->lock);
6038 block_rsv->size = reserve_size;
Qu Wenruoff6bc372017-12-21 13:42:04 +08006039 block_rsv->qgroup_rsv_size = qgroup_rsv_size;
Josef Bacik69fe2d72017-10-19 14:15:57 -04006040 spin_unlock(&block_rsv->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006041}
6042
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006043int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006044{
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006045 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006046 unsigned nr_extents;
Miao Xie08e007d2012-10-16 11:33:38 +00006047 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07006048 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006049 bool delalloc_lock = true;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006050
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006051 /* If we are a free space inode we need to not flush since we will be in
6052 * the middle of a transaction commit. We also don't need the delalloc
6053 * mutex since we won't race with anybody. We need this mostly to make
6054 * lockdep shut its filthy mouth.
Josef Bacikbac357dc2016-07-20 16:48:45 -07006055 *
6056 * If we have a transaction open (can happen if we call truncate_block
6057 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006058 */
6059 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00006060 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006061 delalloc_lock = false;
Nikolay Borisovda07d4a2018-01-12 16:21:05 +02006062 } else {
6063 if (current->journal_info)
6064 flush = BTRFS_RESERVE_FLUSH_LIMIT;
Josef Bacikc09544e2011-08-30 10:19:10 -04006065
Nikolay Borisovda07d4a2018-01-12 16:21:05 +02006066 if (btrfs_transaction_in_commit(fs_info))
6067 schedule_timeout(1);
6068 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006069
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006070 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006071 mutex_lock(&inode->delalloc_mutex);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006072
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006073 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006074
6075 /* Add our new extents and calculate the new rsv size. */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006076 spin_lock(&inode->lock);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006077 nr_extents = count_max_extents(num_bytes);
Josef Bacik8b62f872017-10-19 14:15:55 -04006078 btrfs_mod_outstanding_extents(inode, nr_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006079 inode->csum_bytes += num_bytes;
6080 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006081 spin_unlock(&inode->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05006082
Josef Bacik69fe2d72017-10-19 14:15:57 -04006083 ret = btrfs_inode_rsv_refill(inode, flush);
Qu Wenruo43b18592017-12-12 15:34:32 +08006084 if (unlikely(ret))
Wang Shilong88e081bf2013-03-01 11:36:01 +00006085 goto out_fail;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006086
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006087 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006088 mutex_unlock(&inode->delalloc_mutex);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006089 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00006090
6091out_fail:
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006092 spin_lock(&inode->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04006093 nr_extents = count_max_extents(num_bytes);
6094 btrfs_mod_outstanding_extents(inode, -nr_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006095 inode->csum_bytes -= num_bytes;
6096 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006097 spin_unlock(&inode->lock);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006098
Qu Wenruo43b18592017-12-12 15:34:32 +08006099 btrfs_inode_rsv_release(inode, true);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006100 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006101 mutex_unlock(&inode->delalloc_mutex);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006102 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006103}
6104
Josef Bacik7709cde2011-08-04 10:25:02 -04006105/**
6106 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
Josef Bacik8b62f872017-10-19 14:15:55 -04006107 * @inode: the inode to release the reservation for.
6108 * @num_bytes: the number of bytes we are releasing.
Qu Wenruo43b18592017-12-12 15:34:32 +08006109 * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
Josef Bacik7709cde2011-08-04 10:25:02 -04006110 *
6111 * This will release the metadata reservation for an inode. This can be called
6112 * once we complete IO for a given set of bytes to release their metadata
Josef Bacik8b62f872017-10-19 14:15:55 -04006113 * reservations, or on error for the same reason.
Josef Bacik7709cde2011-08-04 10:25:02 -04006114 */
Qu Wenruo43b18592017-12-12 15:34:32 +08006115void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
6116 bool qgroup_free)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006117{
Nikolay Borisov691fa052017-02-20 13:50:42 +02006118 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006119
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006120 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006121 spin_lock(&inode->lock);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006122 inode->csum_bytes -= num_bytes;
6123 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006124 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006125
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006126 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04006127 return;
6128
Qu Wenruo43b18592017-12-12 15:34:32 +08006129 btrfs_inode_rsv_release(inode, qgroup_free);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006130}
6131
Josef Bacik7709cde2011-08-04 10:25:02 -04006132/**
Josef Bacik8b62f872017-10-19 14:15:55 -04006133 * btrfs_delalloc_release_extents - release our outstanding_extents
6134 * @inode: the inode to balance the reservation for.
6135 * @num_bytes: the number of bytes we originally reserved with
Qu Wenruo43b18592017-12-12 15:34:32 +08006136 * @qgroup_free: do we need to free qgroup meta reservation or convert them.
Josef Bacik8b62f872017-10-19 14:15:55 -04006137 *
6138 * When we reserve space we increase outstanding_extents for the extents we may
6139 * add. Once we've set the range as delalloc or created our ordered extents we
6140 * have outstanding_extents to track the real usage, so we use this to free our
6141 * temporarily tracked outstanding_extents. This _must_ be used in conjunction
6142 * with btrfs_delalloc_reserve_metadata.
6143 */
Qu Wenruo43b18592017-12-12 15:34:32 +08006144void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes,
6145 bool qgroup_free)
Josef Bacik8b62f872017-10-19 14:15:55 -04006146{
6147 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
6148 unsigned num_extents;
Josef Bacik8b62f872017-10-19 14:15:55 -04006149
6150 spin_lock(&inode->lock);
6151 num_extents = count_max_extents(num_bytes);
6152 btrfs_mod_outstanding_extents(inode, -num_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006153 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Josef Bacik8b62f872017-10-19 14:15:55 -04006154 spin_unlock(&inode->lock);
6155
Josef Bacik8b62f872017-10-19 14:15:55 -04006156 if (btrfs_is_testing(fs_info))
6157 return;
6158
Qu Wenruo43b18592017-12-12 15:34:32 +08006159 btrfs_inode_rsv_release(inode, qgroup_free);
Josef Bacik8b62f872017-10-19 14:15:55 -04006160}
6161
6162/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006163 * btrfs_delalloc_reserve_space - reserve data and metadata space for
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006164 * delalloc
6165 * @inode: inode we're writing to
6166 * @start: start range we are writing to
6167 * @len: how long the range we are writing to
Qu Wenruo364ecf32017-02-27 15:10:38 +08006168 * @reserved: mandatory parameter, record actually reserved qgroup ranges of
6169 * current reservation.
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006170 *
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006171 * This will do the following things
6172 *
6173 * o reserve space in data space info for num bytes
6174 * and reserve precious corresponding qgroup space
6175 * (Done in check_data_free_space)
6176 *
6177 * o reserve space for metadata space, based on the number of outstanding
6178 * extents and how much csums will be needed
6179 * also reserve metadata space in a per root over-reserve method.
6180 * o add to the inodes->delalloc_bytes
6181 * o add it to the fs_info's delalloc inodes list.
6182 * (Above 3 all done in delalloc_reserve_metadata)
6183 *
6184 * Return 0 for success
6185 * Return <0 for error(-ENOSPC or -EQUOT)
6186 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08006187int btrfs_delalloc_reserve_space(struct inode *inode,
6188 struct extent_changeset **reserved, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006189{
6190 int ret;
6191
Qu Wenruo364ecf32017-02-27 15:10:38 +08006192 ret = btrfs_check_data_free_space(inode, reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006193 if (ret < 0)
6194 return ret;
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006195 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006196 if (ret < 0)
Qu Wenruobc42bda2017-02-27 15:10:39 +08006197 btrfs_free_reserved_data_space(inode, *reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006198 return ret;
6199}
6200
6201/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006202 * btrfs_delalloc_release_space - release data and metadata space for delalloc
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006203 * @inode: inode we're releasing space for
6204 * @start: start position of the space already reserved
6205 * @len: the len of the space already reserved
Josef Bacik8b62f872017-10-19 14:15:55 -04006206 * @release_bytes: the len of the space we consumed or didn't use
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006207 *
6208 * This function will release the metadata space that was not used and will
6209 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
6210 * list if there are no delalloc bytes left.
6211 * Also it will handle the qgroup reserved space.
6212 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08006213void btrfs_delalloc_release_space(struct inode *inode,
Josef Bacik8b62f872017-10-19 14:15:55 -04006214 struct extent_changeset *reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08006215 u64 start, u64 len, bool qgroup_free)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006216{
Qu Wenruo43b18592017-12-12 15:34:32 +08006217 btrfs_delalloc_release_metadata(BTRFS_I(inode), len, qgroup_free);
Qu Wenruobc42bda2017-02-27 15:10:39 +08006218 btrfs_free_reserved_data_space(inode, reserved, start, len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006219}
6220
Josef Bacikce93ec52014-11-17 15:45:48 -05006221static int update_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6202df62016-06-22 18:54:22 -04006222 struct btrfs_fs_info *info, u64 bytenr,
Josef Bacikce93ec52014-11-17 15:45:48 -05006223 u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04006224{
Josef Bacik0af3d002010-06-21 14:48:16 -04006225 struct btrfs_block_group_cache *cache = NULL;
Chris Masondb945352007-10-15 16:15:53 -04006226 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006227 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04006228 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04006229 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04006230
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006231 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00006232 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02006233 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006234 if (alloc)
6235 old_val += num_bytes;
6236 else
6237 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02006238 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00006239 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006240
Chris Masond3977122009-01-05 21:25:51 -05006241 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04006242 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006243 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006244 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006245 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
6246 BTRFS_BLOCK_GROUP_RAID1 |
6247 BTRFS_BLOCK_GROUP_RAID10))
6248 factor = 2;
6249 else
6250 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04006251 /*
6252 * If this block group has free space cache written out, we
6253 * need to make sure to load it if we are removing space. This
6254 * is because we need the unpinning stage to actually add the
6255 * space back to the block group, otherwise we will leak space.
6256 */
6257 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00006258 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04006259
Chris Masondb945352007-10-15 16:15:53 -04006260 byte_in_group = bytenr - cache->key.objectid;
6261 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04006262
Josef Bacik25179202008-10-29 14:49:05 -04006263 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006264 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04006265
Jeff Mahoney6202df62016-06-22 18:54:22 -04006266 if (btrfs_test_opt(info, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04006267 cache->disk_cache_state < BTRFS_DC_CLEAR)
6268 cache->disk_cache_state = BTRFS_DC_CLEAR;
6269
Chris Mason9078a3e2007-04-26 16:46:15 -04006270 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04006271 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04006272 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04006273 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006274 btrfs_set_block_group_used(&cache->item, old_val);
6275 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006276 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006277 cache->space_info->bytes_used += num_bytes;
6278 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04006279 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006280 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04006281 } else {
Chris Masondb945352007-10-15 16:15:53 -04006282 old_val -= num_bytes;
Filipe Mananaae0ab002014-11-26 15:28:52 +00006283 btrfs_set_block_group_used(&cache->item, old_val);
6284 cache->pinned += num_bytes;
6285 cache->space_info->bytes_pinned += num_bytes;
6286 cache->space_info->bytes_used -= num_bytes;
6287 cache->space_info->disk_used -= num_bytes * factor;
6288 spin_unlock(&cache->lock);
6289 spin_unlock(&cache->space_info->lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04006290
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006291 trace_btrfs_space_reservation(info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006292 cache->space_info->flags,
6293 num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07006294 percpu_counter_add(&cache->space_info->total_bytes_pinned,
6295 num_bytes);
Filipe Mananaae0ab002014-11-26 15:28:52 +00006296 set_extent_dirty(info->pinned_extents,
6297 bytenr, bytenr + num_bytes - 1,
6298 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04006299 }
Chris Mason1bbc6212015-04-06 12:46:08 -07006300
6301 spin_lock(&trans->transaction->dirty_bgs_lock);
6302 if (list_empty(&cache->dirty_list)) {
6303 list_add_tail(&cache->dirty_list,
6304 &trans->transaction->dirty_bgs);
Bart Van Asschebece2e82018-06-20 10:03:31 -07006305 trans->transaction->num_dirty_bgs++;
Chris Mason1bbc6212015-04-06 12:46:08 -07006306 btrfs_get_block_group(cache);
6307 }
6308 spin_unlock(&trans->transaction->dirty_bgs_lock);
6309
Filipe Manana036a9342015-11-23 15:25:16 +00006310 /*
6311 * No longer have used bytes in this block group, queue it for
6312 * deletion. We do this after adding the block group to the
6313 * dirty list to avoid races between cleaner kthread and space
6314 * cache writeout.
6315 */
6316 if (!alloc && old_val == 0) {
6317 spin_lock(&info->unused_bgs_lock);
6318 if (list_empty(&cache->bg_list)) {
6319 btrfs_get_block_group(cache);
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +08006320 trace_btrfs_add_unused_block_group(cache);
Filipe Manana036a9342015-11-23 15:25:16 +00006321 list_add_tail(&cache->bg_list,
6322 &info->unused_bgs);
6323 }
6324 spin_unlock(&info->unused_bgs_lock);
6325 }
6326
Chris Masonfa9c0d792009-04-03 09:47:43 -04006327 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04006328 total -= num_bytes;
6329 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006330 }
6331 return 0;
6332}
Chris Mason6324fbf2008-03-24 15:01:59 -04006333
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006334static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04006335{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006336 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05006337 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006338
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006339 spin_lock(&fs_info->block_group_cache_lock);
6340 bytenr = fs_info->first_logical_byte;
6341 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00006342
6343 if (bytenr < (u64)-1)
6344 return bytenr;
6345
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006346 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006347 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04006348 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006349
Yan Zhengd2fb3432008-12-11 16:30:39 -05006350 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006351 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006352
6353 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04006354}
6355
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006356static int pin_down_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006357 struct btrfs_block_group_cache *cache,
6358 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05006359{
Yan Zheng11833d62009-09-11 16:11:19 -04006360 spin_lock(&cache->space_info->lock);
6361 spin_lock(&cache->lock);
6362 cache->pinned += num_bytes;
6363 cache->space_info->bytes_pinned += num_bytes;
6364 if (reserved) {
6365 cache->reserved -= num_bytes;
6366 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05006367 }
Yan Zheng11833d62009-09-11 16:11:19 -04006368 spin_unlock(&cache->lock);
6369 spin_unlock(&cache->space_info->lock);
6370
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006371 trace_btrfs_space_reservation(fs_info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006372 cache->space_info->flags, num_bytes, 1);
Omar Sandoval4da8b762017-06-06 16:45:28 -07006373 percpu_counter_add(&cache->space_info->total_bytes_pinned, num_bytes);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006374 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006375 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05006376 return 0;
6377}
Chris Mason9078a3e2007-04-26 16:46:15 -04006378
Yan, Zhengf0486c62010-05-16 10:46:25 -04006379/*
6380 * this function must be called within transaction
6381 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006382int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006383 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04006384{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006385 struct btrfs_block_group_cache *cache;
6386
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006387 cache = btrfs_lookup_block_group(fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006388 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006389
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006390 pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006391
6392 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04006393 return 0;
6394}
Zheng Yane8569812008-09-26 10:05:48 -04006395
Yan, Zhengf0486c62010-05-16 10:46:25 -04006396/*
Chris Masone688b7252011-10-31 20:52:39 -04006397 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04006398 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006399int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04006400 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006401{
Chris Masone688b7252011-10-31 20:52:39 -04006402 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04006403 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04006404
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006405 cache = btrfs_lookup_block_group(fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006406 if (!cache)
6407 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04006408
6409 /*
6410 * pull in the free space cache (if any) so that our pin
6411 * removes the free space from the cache. We have load_only set
6412 * to one because the slow code to read in the free extents does check
6413 * the pinned extents.
6414 */
Liu Bof6373bf2012-12-27 09:01:18 +00006415 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04006416
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006417 pin_down_extent(fs_info, cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04006418
6419 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04006420 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04006421 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006422 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04006423}
6424
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006425static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
6426 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006427{
6428 int ret;
6429 struct btrfs_block_group_cache *block_group;
6430 struct btrfs_caching_control *caching_ctl;
6431
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006432 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006433 if (!block_group)
6434 return -EINVAL;
6435
6436 cache_block_group(block_group, 0);
6437 caching_ctl = get_caching_control(block_group);
6438
6439 if (!caching_ctl) {
6440 /* Logic error */
6441 BUG_ON(!block_group_cache_done(block_group));
6442 ret = btrfs_remove_free_space(block_group, start, num_bytes);
6443 } else {
6444 mutex_lock(&caching_ctl->mutex);
6445
6446 if (start >= caching_ctl->progress) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006447 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006448 } else if (start + num_bytes <= caching_ctl->progress) {
6449 ret = btrfs_remove_free_space(block_group,
6450 start, num_bytes);
6451 } else {
6452 num_bytes = caching_ctl->progress - start;
6453 ret = btrfs_remove_free_space(block_group,
6454 start, num_bytes);
6455 if (ret)
6456 goto out_lock;
6457
6458 num_bytes = (start + num_bytes) -
6459 caching_ctl->progress;
6460 start = caching_ctl->progress;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006461 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006462 }
6463out_lock:
6464 mutex_unlock(&caching_ctl->mutex);
6465 put_caching_control(caching_ctl);
6466 }
6467 btrfs_put_block_group(block_group);
6468 return ret;
6469}
6470
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006471int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info,
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006472 struct extent_buffer *eb)
6473{
6474 struct btrfs_file_extent_item *item;
6475 struct btrfs_key key;
6476 int found_type;
6477 int i;
Gu Jinxiangb89311e2018-05-22 17:46:51 +08006478 int ret = 0;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006479
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006480 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006481 return 0;
6482
6483 for (i = 0; i < btrfs_header_nritems(eb); i++) {
6484 btrfs_item_key_to_cpu(eb, &key, i);
6485 if (key.type != BTRFS_EXTENT_DATA_KEY)
6486 continue;
6487 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
6488 found_type = btrfs_file_extent_type(eb, item);
6489 if (found_type == BTRFS_FILE_EXTENT_INLINE)
6490 continue;
6491 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
6492 continue;
6493 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
6494 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Gu Jinxiangb89311e2018-05-22 17:46:51 +08006495 ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
6496 if (ret)
6497 break;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006498 }
6499
Gu Jinxiangb89311e2018-05-22 17:46:51 +08006500 return ret;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006501}
6502
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006503static void
6504btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg)
6505{
6506 atomic_inc(&bg->reservations);
6507}
6508
6509void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
6510 const u64 start)
6511{
6512 struct btrfs_block_group_cache *bg;
6513
6514 bg = btrfs_lookup_block_group(fs_info, start);
6515 ASSERT(bg);
6516 if (atomic_dec_and_test(&bg->reservations))
Peter Zijlstra46259562018-03-15 11:43:08 +01006517 wake_up_var(&bg->reservations);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006518 btrfs_put_block_group(bg);
6519}
6520
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006521void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6522{
6523 struct btrfs_space_info *space_info = bg->space_info;
6524
6525 ASSERT(bg->ro);
6526
6527 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
6528 return;
6529
6530 /*
6531 * Our block group is read only but before we set it to read only,
6532 * some task might have had allocated an extent from it already, but it
6533 * has not yet created a respective ordered extent (and added it to a
6534 * root's list of ordered extents).
6535 * Therefore wait for any task currently allocating extents, since the
6536 * block group's reservations counter is incremented while a read lock
6537 * on the groups' semaphore is held and decremented after releasing
6538 * the read access on that semaphore and creating the ordered extent.
6539 */
6540 down_write(&space_info->groups_sem);
6541 up_write(&space_info->groups_sem);
6542
Peter Zijlstra46259562018-03-15 11:43:08 +01006543 wait_var_event(&bg->reservations, !atomic_read(&bg->reservations));
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006544}
6545
Josef Bacikfb25e912011-07-26 17:00:46 -04006546/**
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006547 * btrfs_add_reserved_bytes - update the block_group and space info counters
Josef Bacikfb25e912011-07-26 17:00:46 -04006548 * @cache: The cache we are manipulating
Wang Xiaoguang18513092016-07-25 15:51:40 +08006549 * @ram_bytes: The number of bytes of file content, and will be same to
6550 * @num_bytes except for the compress path.
Josef Bacikfb25e912011-07-26 17:00:46 -04006551 * @num_bytes: The number of bytes in question
Miao Xiee570fd22014-06-19 10:42:50 +08006552 * @delalloc: The blocks are allocated for the delalloc write
Josef Bacikfb25e912011-07-26 17:00:46 -04006553 *
Xiaoguang Wang745699e2016-09-23 12:38:50 +08006554 * This is called by the allocator when it reserves space. If this is a
6555 * reservation and the block group has become read only we cannot make the
6556 * reservation and return -EAGAIN, otherwise this function always succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04006557 */
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006558static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
Wang Xiaoguang18513092016-07-25 15:51:40 +08006559 u64 ram_bytes, u64 num_bytes, int delalloc)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006560{
Josef Bacikfb25e912011-07-26 17:00:46 -04006561 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006562 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006563
Josef Bacikfb25e912011-07-26 17:00:46 -04006564 spin_lock(&space_info->lock);
6565 spin_lock(&cache->lock);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006566 if (cache->ro) {
6567 ret = -EAGAIN;
Josef Bacikfb25e912011-07-26 17:00:46 -04006568 } else {
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006569 cache->reserved += num_bytes;
6570 space_info->bytes_reserved += num_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006571
Wang Xiaoguang18513092016-07-25 15:51:40 +08006572 trace_btrfs_space_reservation(cache->fs_info,
6573 "space_info", space_info->flags,
6574 ram_bytes, 0);
6575 space_info->bytes_may_use -= ram_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006576 if (delalloc)
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006577 cache->delalloc_bytes += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006578 }
Josef Bacikfb25e912011-07-26 17:00:46 -04006579 spin_unlock(&cache->lock);
6580 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006581 return ret;
6582}
6583
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006584/**
6585 * btrfs_free_reserved_bytes - update the block_group and space info counters
6586 * @cache: The cache we are manipulating
6587 * @num_bytes: The number of bytes in question
6588 * @delalloc: The blocks are allocated for the delalloc write
6589 *
6590 * This is called by somebody who is freeing space that was never actually used
6591 * on disk. For example if you reserve some space for a new leaf in transaction
6592 * A and before transaction A commits you free that leaf, you call this with
6593 * reserve set to 0 in order to clear the reservation.
6594 */
6595
6596static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
6597 u64 num_bytes, int delalloc)
6598{
6599 struct btrfs_space_info *space_info = cache->space_info;
6600 int ret = 0;
6601
6602 spin_lock(&space_info->lock);
6603 spin_lock(&cache->lock);
6604 if (cache->ro)
6605 space_info->bytes_readonly += num_bytes;
6606 cache->reserved -= num_bytes;
6607 space_info->bytes_reserved -= num_bytes;
6608
6609 if (delalloc)
6610 cache->delalloc_bytes -= num_bytes;
6611 spin_unlock(&cache->lock);
6612 spin_unlock(&space_info->lock);
6613 return ret;
6614}
David Sterba8b74c032017-02-10 19:20:56 +01006615void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04006616{
Yan Zheng11833d62009-09-11 16:11:19 -04006617 struct btrfs_caching_control *next;
6618 struct btrfs_caching_control *caching_ctl;
6619 struct btrfs_block_group_cache *cache;
6620
Josef Bacik9e351cc2014-03-13 15:42:13 -04006621 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04006622
6623 list_for_each_entry_safe(caching_ctl, next,
6624 &fs_info->caching_block_groups, list) {
6625 cache = caching_ctl->block_group;
6626 if (block_group_cache_done(cache)) {
6627 cache->last_byte_to_unpin = (u64)-1;
6628 list_del_init(&caching_ctl->list);
6629 put_caching_control(caching_ctl);
6630 } else {
6631 cache->last_byte_to_unpin = caching_ctl->progress;
6632 }
6633 }
6634
6635 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6636 fs_info->pinned_extents = &fs_info->freed_extents[1];
6637 else
6638 fs_info->pinned_extents = &fs_info->freed_extents[0];
6639
Josef Bacik9e351cc2014-03-13 15:42:13 -04006640 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006641
6642 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04006643}
6644
Josef Bacikc759c4e2015-10-02 15:25:10 -04006645/*
6646 * Returns the free cluster for the given space info and sets empty_cluster to
6647 * what it should be based on the mount options.
6648 */
6649static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006650fetch_cluster_info(struct btrfs_fs_info *fs_info,
6651 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04006652{
6653 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006654
6655 *empty_cluster = 0;
6656 if (btrfs_mixed_space_info(space_info))
6657 return ret;
6658
Josef Bacikc759c4e2015-10-02 15:25:10 -04006659 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006660 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006661 if (btrfs_test_opt(fs_info, SSD))
6662 *empty_cluster = SZ_2M;
6663 else
Byongho Leeee221842015-12-15 01:42:10 +09006664 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006665 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
6666 btrfs_test_opt(fs_info, SSD_SPREAD)) {
6667 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006668 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006669 }
6670
6671 return ret;
6672}
6673
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006674static int unpin_extent_range(struct btrfs_fs_info *fs_info,
6675 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00006676 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04006677{
Yan Zheng11833d62009-09-11 16:11:19 -04006678 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04006679 struct btrfs_space_info *space_info;
6680 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006681 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04006682 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006683 u64 total_unpinned = 0;
6684 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04006685 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04006686
6687 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04006688 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04006689 if (!cache ||
6690 start >= cache->key.objectid + cache->key.offset) {
6691 if (cache)
6692 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04006693 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006694 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006695 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04006696
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006697 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04006698 cache->space_info,
6699 &empty_cluster);
6700 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04006701 }
6702
6703 len = cache->key.objectid + cache->key.offset - start;
6704 len = min(len, end + 1 - start);
6705
6706 if (start < cache->last_byte_to_unpin) {
6707 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00006708 if (return_free_space)
6709 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04006710 }
Josef Bacik25179202008-10-29 14:49:05 -04006711
Yan, Zhengf0486c62010-05-16 10:46:25 -04006712 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006713 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006714 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006715
Josef Bacikc759c4e2015-10-02 15:25:10 -04006716 /*
6717 * If this space cluster has been marked as fragmented and we've
6718 * unpinned enough in this block group to potentially allow a
6719 * cluster to be created inside of it go ahead and clear the
6720 * fragmented check.
6721 */
6722 if (cluster && cluster->fragmented &&
6723 total_unpinned > empty_cluster) {
6724 spin_lock(&cluster->lock);
6725 cluster->fragmented = 0;
6726 spin_unlock(&cluster->lock);
6727 }
6728
Josef Bacik7b398f82012-10-22 15:52:28 -04006729 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006730 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006731 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006732 space_info->bytes_pinned -= len;
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006733
6734 trace_btrfs_space_reservation(fs_info, "pinned",
6735 space_info->flags, len, 0);
Josef Bacik4f4db212015-09-29 11:40:47 -04006736 space_info->max_extent_size = 0;
Liu Bod288db52014-07-02 16:58:01 +08006737 percpu_counter_add(&space_info->total_bytes_pinned, -len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006738 if (cache->ro) {
6739 space_info->bytes_readonly += len;
6740 readonly = true;
6741 }
Josef Bacik25179202008-10-29 14:49:05 -04006742 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006743 if (!readonly && return_free_space &&
6744 global_rsv->space_info == space_info) {
6745 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03006746
Josef Bacik7b398f82012-10-22 15:52:28 -04006747 spin_lock(&global_rsv->lock);
6748 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04006749 to_add = min(len, global_rsv->size -
6750 global_rsv->reserved);
6751 global_rsv->reserved += to_add;
6752 space_info->bytes_may_use += to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006753 if (global_rsv->reserved >= global_rsv->size)
6754 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04006755 trace_btrfs_space_reservation(fs_info,
6756 "space_info",
6757 space_info->flags,
6758 to_add, 1);
6759 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006760 }
6761 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006762 /* Add to any tickets we may have */
6763 if (len)
6764 space_info_add_new_bytes(fs_info, space_info,
6765 len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006766 }
6767 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006768 }
6769
6770 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006771 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04006772 return 0;
6773}
6774
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02006775int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
Chris Masona28ec192007-03-06 20:08:01 -05006776{
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02006777 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006778 struct btrfs_block_group_cache *block_group, *tmp;
6779 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04006780 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04006781 u64 start;
6782 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05006783 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05006784
Yan Zheng11833d62009-09-11 16:11:19 -04006785 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6786 unpin = &fs_info->freed_extents[1];
6787 else
6788 unpin = &fs_info->freed_extents[0];
6789
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006790 while (!trans->aborted) {
Filipe Mananad4b450c2015-01-29 19:18:25 +00006791 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04006792 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04006793 EXTENT_DIRTY, NULL);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006794 if (ret) {
6795 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05006796 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00006797 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006798
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006799 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006800 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00006801 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006802
David Sterbaaf6f8f62016-04-26 23:54:39 +02006803 clear_extent_dirty(unpin, start, end);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006804 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006805 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masonb9473432009-03-13 11:00:37 -04006806 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05006807 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006808
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006809 /*
6810 * Transaction is finished. We don't need the lock anymore. We
6811 * do need to clean up the block groups in case of a transaction
6812 * abort.
6813 */
6814 deleted_bgs = &trans->transaction->deleted_bgs;
6815 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
6816 u64 trimmed = 0;
6817
6818 ret = -EROFS;
6819 if (!trans->aborted)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006820 ret = btrfs_discard_extent(fs_info,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006821 block_group->key.objectid,
6822 block_group->key.offset,
6823 &trimmed);
6824
6825 list_del_init(&block_group->bg_list);
6826 btrfs_put_block_group_trimming(block_group);
6827 btrfs_put_block_group(block_group);
6828
6829 if (ret) {
6830 const char *errstr = btrfs_decode_error(ret);
6831 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02006832 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006833 ret, errstr);
6834 }
6835 }
6836
Chris Masone20d96d2007-03-22 12:13:20 -04006837 return 0;
6838}
6839
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006840static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Nikolay Borisove72cb922018-06-20 15:48:57 +03006841 struct btrfs_delayed_ref_node *node, u64 parent,
6842 u64 root_objectid, u64 owner_objectid,
6843 u64 owner_offset, int refs_to_drop,
6844 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05006845{
Nikolay Borisove72cb922018-06-20 15:48:57 +03006846 struct btrfs_fs_info *info = trans->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04006847 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006848 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04006849 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04006850 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006851 struct btrfs_extent_item *ei;
6852 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05006853 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006854 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05006855 int extent_slot = 0;
6856 int found_extent = 0;
6857 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006858 u32 item_size;
6859 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006860 u64 bytenr = node->bytenr;
6861 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07006862 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006863 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05006864
Chris Mason5caf2a02007-04-02 11:20:42 -04006865 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04006866 if (!path)
6867 return -ENOMEM;
6868
David Sterbae4058b52015-11-27 16:31:35 +01006869 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04006870 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006871
6872 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
6873 BUG_ON(!is_data && refs_to_drop != 1);
6874
Josef Bacik3173a182013-03-07 14:22:04 -05006875 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02006876 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05006877
Nikolay Borisovfbe48012018-06-20 15:48:52 +03006878 ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
6879 parent, root_objectid, owner_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006880 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05006881 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05006882 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006883 while (extent_slot >= 0) {
6884 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05006885 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006886 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05006887 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006888 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
6889 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05006890 found_extent = 1;
6891 break;
6892 }
Josef Bacik3173a182013-03-07 14:22:04 -05006893 if (key.type == BTRFS_METADATA_ITEM_KEY &&
6894 key.offset == owner_objectid) {
6895 found_extent = 1;
6896 break;
6897 }
Chris Mason952fcca2008-02-18 16:33:44 -05006898 if (path->slots[0] - extent_slot > 5)
6899 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006900 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05006901 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006902#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6903 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
6904 if (found_extent && item_size < sizeof(*ei))
6905 found_extent = 0;
6906#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04006907 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006908 BUG_ON(iref);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05006909 ret = remove_extent_backref(trans, info, path, NULL,
6910 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07006911 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06006912 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006913 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006914 goto out;
6915 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006916 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04006917 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006918
6919 key.objectid = bytenr;
6920 key.type = BTRFS_EXTENT_ITEM_KEY;
6921 key.offset = num_bytes;
6922
Josef Bacik3173a182013-03-07 14:22:04 -05006923 if (!is_data && skinny_metadata) {
6924 key.type = BTRFS_METADATA_ITEM_KEY;
6925 key.offset = owner_objectid;
6926 }
6927
Zheng Yan31840ae2008-09-23 13:14:14 -04006928 ret = btrfs_search_slot(trans, extent_root,
6929 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05006930 if (ret > 0 && skinny_metadata && path->slots[0]) {
6931 /*
6932 * Couldn't find our skinny metadata item,
6933 * see if we have ye olde extent item.
6934 */
6935 path->slots[0]--;
6936 btrfs_item_key_to_cpu(path->nodes[0], &key,
6937 path->slots[0]);
6938 if (key.objectid == bytenr &&
6939 key.type == BTRFS_EXTENT_ITEM_KEY &&
6940 key.offset == num_bytes)
6941 ret = 0;
6942 }
6943
6944 if (ret > 0 && skinny_metadata) {
6945 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01006946 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05006947 key.type = BTRFS_EXTENT_ITEM_KEY;
6948 key.offset = num_bytes;
6949 btrfs_release_path(path);
6950 ret = btrfs_search_slot(trans, extent_root,
6951 &key, path, -1, 1);
6952 }
6953
Josef Bacikf3465ca2008-11-12 14:19:50 -05006954 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006955 btrfs_err(info,
6956 "umm, got %d back from search, was looking for %llu",
6957 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00006958 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02006959 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006960 }
David Sterba005d6422012-09-18 07:52:32 -06006961 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006962 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006963 goto out;
6964 }
Zheng Yan31840ae2008-09-23 13:14:14 -04006965 extent_slot = path->slots[0];
6966 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306967 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02006968 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006969 btrfs_err(info,
6970 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006971 bytenr, parent, root_objectid, owner_objectid,
6972 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04006973 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04006974 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006975 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04006976 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006977 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05006978 }
Chris Mason5f39d392007-10-15 16:14:19 -04006979
6980 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006981 item_size = btrfs_item_size_nr(leaf, extent_slot);
6982#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6983 if (item_size < sizeof(*ei)) {
6984 BUG_ON(found_extent || extent_slot != path->slots[0]);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05006985 ret = convert_extent_item_v0(trans, info, path, owner_objectid,
6986 0);
David Sterba005d6422012-09-18 07:52:32 -06006987 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006988 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006989 goto out;
6990 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006991
David Sterbab3b4aa72011-04-21 01:20:15 +02006992 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006993 path->leave_spinning = 1;
6994
6995 key.objectid = bytenr;
6996 key.type = BTRFS_EXTENT_ITEM_KEY;
6997 key.offset = num_bytes;
6998
6999 ret = btrfs_search_slot(trans, extent_root, &key, path,
7000 -1, 1);
7001 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007002 btrfs_err(info,
7003 "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007004 ret, bytenr);
David Sterbaa4f78752017-06-29 18:37:49 +02007005 btrfs_print_leaf(path->nodes[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007006 }
David Sterba005d6422012-09-18 07:52:32 -06007007 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007008 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007009 goto out;
7010 }
7011
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007012 extent_slot = path->slots[0];
7013 leaf = path->nodes[0];
7014 item_size = btrfs_item_size_nr(leaf, extent_slot);
7015 }
7016#endif
7017 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05007018 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04007019 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05007020 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
7021 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007022 struct btrfs_tree_block_info *bi;
7023 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
7024 bi = (struct btrfs_tree_block_info *)(ei + 1);
7025 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05007026 }
7027
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007028 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04007029 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007030 btrfs_err(info,
7031 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
7032 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04007033 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04007034 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04007035 goto out;
7036 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007037 refs -= refs_to_drop;
7038
7039 if (refs > 0) {
7040 if (extent_op)
7041 __run_delayed_extent_op(extent_op, leaf, ei);
7042 /*
7043 * In the case of inline back ref, reference count will
7044 * be updated by remove_extent_backref
7045 */
7046 if (iref) {
7047 BUG_ON(!found_extent);
7048 } else {
7049 btrfs_set_extent_refs(leaf, ei, refs);
7050 btrfs_mark_buffer_dirty(leaf);
7051 }
7052 if (found_extent) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05007053 ret = remove_extent_backref(trans, info, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007054 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07007055 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06007056 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007057 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007058 goto out;
7059 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007060 }
7061 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007062 if (found_extent) {
7063 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08007064 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007065 if (iref) {
7066 BUG_ON(path->slots[0] != extent_slot);
7067 } else {
7068 BUG_ON(path->slots[0] != extent_slot + 1);
7069 path->slots[0] = extent_slot;
7070 num_to_del = 2;
7071 }
Chris Mason78fae272007-03-25 11:35:08 -04007072 }
Chris Masonb9473432009-03-13 11:00:37 -04007073
Josef Bacikfcebe452014-05-13 17:30:47 -07007074 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05007075 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
7076 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06007077 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007078 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007079 goto out;
7080 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007081 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01007082
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007083 if (is_data) {
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007084 ret = btrfs_del_csums(trans, info, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06007085 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007086 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007087 goto out;
7088 }
Chris Mason459931e2008-12-10 09:10:46 -05007089 }
7090
Nikolay Borisove7355e52018-05-10 15:44:55 +03007091 ret = add_to_free_space_tree(trans, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007092 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007093 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007094 goto out;
7095 }
7096
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007097 ret = update_block_group(trans, info, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06007098 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007099 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007100 goto out;
7101 }
Chris Masona28ec192007-03-06 20:08:01 -05007102 }
Josef Bacikfcebe452014-05-13 17:30:47 -07007103 btrfs_release_path(path);
7104
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007105out:
Chris Mason5caf2a02007-04-02 11:20:42 -04007106 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05007107 return ret;
7108}
7109
7110/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007111 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04007112 * delayed ref for that extent as well. This searches the delayed ref tree for
7113 * a given extent, and if there are no other delayed refs to be processed, it
7114 * removes it from the tree.
7115 */
7116static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007117 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04007118{
7119 struct btrfs_delayed_ref_head *head;
7120 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007121 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04007122
7123 delayed_refs = &trans->transaction->delayed_refs;
7124 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08007125 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04007126 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08007127 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04007128
Josef Bacikd7df2c72014-01-23 09:21:38 -05007129 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04007130 if (!RB_EMPTY_ROOT(&head->ref_tree))
Chris Mason1887be62009-03-13 10:11:24 -04007131 goto out;
7132
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007133 if (head->extent_op) {
7134 if (!head->must_insert_reserved)
7135 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00007136 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007137 head->extent_op = NULL;
7138 }
7139
Chris Mason1887be62009-03-13 10:11:24 -04007140 /*
7141 * waiting for the lock here would deadlock. If someone else has it
7142 * locked they are already in the process of dropping it anyway
7143 */
7144 if (!mutex_trylock(&head->mutex))
7145 goto out;
7146
7147 /*
7148 * at this point we have a head with no other entries. Go
7149 * ahead and process it.
7150 */
Liu Boc46effa2013-10-14 12:59:45 +08007151 rb_erase(&head->href_node, &delayed_refs->href_root);
Josef Bacikd2788502017-09-29 15:43:57 -04007152 RB_CLEAR_NODE(&head->href_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05007153 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04007154
7155 /*
7156 * we don't take a ref on the node because we're removing it from the
7157 * tree, so we just steal the ref the tree was holding.
7158 */
Chris Masonc3e69d52009-03-13 10:17:05 -04007159 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007160 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04007161 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007162 head->processing = 0;
7163 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04007164 spin_unlock(&delayed_refs->lock);
7165
Yan, Zhengf0486c62010-05-16 10:46:25 -04007166 BUG_ON(head->extent_op);
7167 if (head->must_insert_reserved)
7168 ret = 1;
7169
7170 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04007171 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007172 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04007173out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05007174 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08007175
7176out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04007177 spin_unlock(&delayed_refs->lock);
7178 return 0;
7179}
7180
Yan, Zhengf0486c62010-05-16 10:46:25 -04007181void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7182 struct btrfs_root *root,
7183 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02007184 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007185{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007186 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb150a4f2013-06-19 15:00:04 -04007187 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007188 int ret;
7189
7190 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07007191 int old_ref_mod, new_ref_mod;
7192
Josef Bacikfd708b82017-09-29 15:43:50 -04007193 btrfs_ref_tree_mod(root, buf->start, buf->len, parent,
7194 root->root_key.objectid,
7195 btrfs_header_level(buf), 0,
7196 BTRFS_DROP_DELAYED_REF);
Nikolay Borisov44e1c472018-06-20 15:48:53 +03007197 ret = btrfs_add_delayed_tree_ref(trans, buf->start,
Omar Sandoval7be07912017-06-06 16:45:30 -07007198 buf->len, parent,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007199 root->root_key.objectid,
7200 btrfs_header_level(buf),
Omar Sandoval7be07912017-06-06 16:45:30 -07007201 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007202 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007203 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07007204 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007205 }
7206
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007207 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
Filipe Manana62198722015-01-06 20:18:45 +00007208 struct btrfs_block_group_cache *cache;
7209
Yan, Zhengf0486c62010-05-16 10:46:25 -04007210 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007211 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007212 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04007213 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007214 }
7215
Omar Sandoval4da8b762017-06-06 16:45:28 -07007216 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007217 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00007218
Yan, Zhengf0486c62010-05-16 10:46:25 -04007219 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007220 pin_down_extent(fs_info, cache, buf->start,
7221 buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00007222 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04007223 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007224 }
7225
7226 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
7227
7228 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08007229 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00007230 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007231 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007232 }
7233out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04007234 if (pin)
Nikolay Borisov29d2b842018-03-30 12:58:47 +03007235 add_pinned_bytes(fs_info, buf->len, true,
Josef Bacikb150a4f2013-06-19 15:00:04 -04007236 root->root_key.objectid);
7237
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007238 if (last_ref) {
7239 /*
7240 * Deleting the buffer, clear the corrupt flag since it doesn't
7241 * matter anymore.
7242 */
7243 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
7244 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007245}
7246
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007247/* Can return -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007248int btrfs_free_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007249 struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007250 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01007251 u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04007252{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007253 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07007254 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04007255 int ret;
7256
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04007257 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007258 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02007259
Josef Bacikfd708b82017-09-29 15:43:50 -04007260 if (root_objectid != BTRFS_TREE_LOG_OBJECTID)
7261 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent,
7262 root_objectid, owner, offset,
7263 BTRFS_DROP_DELAYED_REF);
7264
Chris Mason56bec292009-03-13 10:10:06 -04007265 /*
7266 * tree log blocks never actually go into the extent allocation
7267 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04007268 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007269 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
7270 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04007271 /* unlocks the pinned mutex */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007272 btrfs_pin_extent(fs_info, bytenr, num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07007273 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04007274 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007275 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Nikolay Borisov44e1c472018-06-20 15:48:53 +03007276 ret = btrfs_add_delayed_tree_ref(trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007277 num_bytes, parent,
7278 root_objectid, (int)owner,
7279 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007280 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007281 } else {
Nikolay Borisov88a979c2018-06-20 15:48:54 +03007282 ret = btrfs_add_delayed_data_ref(trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007283 num_bytes, parent,
7284 root_objectid, owner, offset,
7285 0, BTRFS_DROP_DELAYED_REF,
Omar Sandovald7eae342017-06-06 16:45:31 -07007286 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04007287 }
Omar Sandovald7eae342017-06-06 16:45:31 -07007288
Nikolay Borisov29d2b842018-03-30 12:58:47 +03007289 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0) {
7290 bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
7291
7292 add_pinned_bytes(fs_info, num_bytes, metadata, root_objectid);
7293 }
Omar Sandovald7eae342017-06-06 16:45:31 -07007294
Chris Mason925baed2008-06-25 16:01:30 -04007295 return ret;
7296}
7297
Chris Masonfec577f2007-02-26 10:40:21 -05007298/*
Josef Bacik817d52f2009-07-13 21:29:25 -04007299 * when we wait for progress in the block group caching, its because
7300 * our allocation attempt failed at least once. So, we must sleep
7301 * and let some progress happen before we try again.
7302 *
7303 * This function will sleep at least once waiting for new free space to
7304 * show up, and then it will check the block group free space numbers
7305 * for our min num_bytes. Another option is to have it go ahead
7306 * and look in the rbtree for a free extent of a given size, but this
7307 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04007308 *
7309 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
7310 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04007311 */
Josef Bacik36cce922013-08-05 11:15:21 -04007312static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04007313wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
7314 u64 num_bytes)
7315{
Yan Zheng11833d62009-09-11 16:11:19 -04007316 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04007317
Yan Zheng11833d62009-09-11 16:11:19 -04007318 caching_ctl = get_caching_control(cache);
7319 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007320 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04007321
Yan Zheng11833d62009-09-11 16:11:19 -04007322 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08007323 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04007324
7325 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04007326}
7327
7328static noinline int
7329wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
7330{
7331 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04007332 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007333
7334 caching_ctl = get_caching_control(cache);
7335 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007336 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007337
7338 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04007339 if (cache->cached == BTRFS_CACHE_ERROR)
7340 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04007341 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04007342 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04007343}
7344
7345enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05007346 LOOP_CACHING_NOWAIT = 0,
7347 LOOP_CACHING_WAIT = 1,
7348 LOOP_ALLOC_CHUNK = 2,
7349 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04007350};
7351
Miao Xiee570fd22014-06-19 10:42:50 +08007352static inline void
7353btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
7354 int delalloc)
7355{
7356 if (delalloc)
7357 down_read(&cache->data_rwsem);
7358}
7359
7360static inline void
7361btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
7362 int delalloc)
7363{
7364 btrfs_get_block_group(cache);
7365 if (delalloc)
7366 down_read(&cache->data_rwsem);
7367}
7368
7369static struct btrfs_block_group_cache *
7370btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
7371 struct btrfs_free_cluster *cluster,
7372 int delalloc)
7373{
Sudip Mukherjee89771cc2016-02-16 13:32:47 +05307374 struct btrfs_block_group_cache *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007375
Miao Xiee570fd22014-06-19 10:42:50 +08007376 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007377 while (1) {
7378 used_bg = cluster->block_group;
7379 if (!used_bg)
7380 return NULL;
7381
7382 if (used_bg == block_group)
7383 return used_bg;
7384
7385 btrfs_get_block_group(used_bg);
7386
7387 if (!delalloc)
7388 return used_bg;
7389
7390 if (down_read_trylock(&used_bg->data_rwsem))
7391 return used_bg;
7392
7393 spin_unlock(&cluster->refill_lock);
7394
Liu Boe321f8a2016-11-30 16:11:04 -08007395 /* We should only have one-level nested. */
7396 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007397
7398 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007399 if (used_bg == cluster->block_group)
7400 return used_bg;
7401
7402 up_read(&used_bg->data_rwsem);
7403 btrfs_put_block_group(used_bg);
7404 }
Miao Xiee570fd22014-06-19 10:42:50 +08007405}
7406
7407static inline void
7408btrfs_release_block_group(struct btrfs_block_group_cache *cache,
7409 int delalloc)
7410{
7411 if (delalloc)
7412 up_read(&cache->data_rwsem);
7413 btrfs_put_block_group(cache);
7414}
7415
Josef Bacik817d52f2009-07-13 21:29:25 -04007416/*
Chris Masonfec577f2007-02-26 10:40:21 -05007417 * walks the btree of allocated extents and find a hole of a given size.
7418 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08007419 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04007420 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08007421 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05007422 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08007423 *
7424 * If there is no suitable free space, we will record the max size of
7425 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05007426 */
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05007427static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08007428 u64 ram_bytes, u64 num_bytes, u64 empty_size,
7429 u64 hint_byte, struct btrfs_key *ins,
7430 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007431{
Josef Bacik80eb2342008-10-29 14:49:05 -04007432 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007433 struct btrfs_root *root = fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007434 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04007435 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05007436 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08007437 u64 max_extent_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007438 u64 empty_cluster = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04007439 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007440 int loop = 0;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007441 int index = btrfs_bg_flags_to_raid_index(flags);
Josef Bacik0a243252009-09-11 16:11:20 -04007442 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007443 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04007444 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08007445 bool have_caching_bg = false;
chandan13a0db52015-11-02 13:59:46 +05307446 bool orig_have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007447 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05007448
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007449 WARN_ON(num_bytes < fs_info->sectorsize);
David Sterba962a2982014-06-04 18:41:45 +02007450 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04007451 ins->objectid = 0;
7452 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04007453
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007454 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05007455
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007456 space_info = __find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007457 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007458 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007459 return -ENOSPC;
7460 }
Josef Bacik2552d172009-04-03 10:14:19 -04007461
Josef Bacik67377732010-09-16 16:19:09 -04007462 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04007463 * If our free space is heavily fragmented we may not be able to make
7464 * big contiguous allocations, so instead of doing the expensive search
7465 * for free space, simply return ENOSPC with our max_extent_size so we
7466 * can go ahead and search for a more manageable chunk.
7467 *
7468 * If our max_extent_size is large enough for our allocation simply
7469 * disable clustering since we will likely not be able to find enough
7470 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04007471 */
Josef Bacik4f4db212015-09-29 11:40:47 -04007472 if (unlikely(space_info->max_extent_size)) {
7473 spin_lock(&space_info->lock);
7474 if (space_info->max_extent_size &&
7475 num_bytes > space_info->max_extent_size) {
7476 ins->offset = space_info->max_extent_size;
7477 spin_unlock(&space_info->lock);
7478 return -ENOSPC;
7479 } else if (space_info->max_extent_size) {
7480 use_cluster = false;
7481 }
7482 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007483 }
7484
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007485 last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04007486 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007487 spin_lock(&last_ptr->lock);
7488 if (last_ptr->block_group)
7489 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007490 if (last_ptr->fragmented) {
7491 /*
7492 * We still set window_start so we can keep track of the
7493 * last place we found an allocation to try and save
7494 * some time.
7495 */
7496 hint_byte = last_ptr->window_start;
7497 use_cluster = false;
7498 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007499 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007500 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007501
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007502 search_start = max(search_start, first_logical_byte(fs_info, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04007503 search_start = max(search_start, hint_byte);
Josef Bacik2552d172009-04-03 10:14:19 -04007504 if (search_start == hint_byte) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007505 block_group = btrfs_lookup_block_group(fs_info, search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04007506 /*
7507 * we don't want to use the block group if it doesn't match our
7508 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05007509 *
7510 * However if we are re-searching with an ideal block group
7511 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04007512 */
David Sterbab6919a52013-04-29 13:39:40 +00007513 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05007514 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04007515 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007516 if (list_empty(&block_group->list) ||
7517 block_group->ro) {
7518 /*
7519 * someone is removing this block group,
7520 * we can't jump into the have_block_group
7521 * target because our list pointers are not
7522 * valid
7523 */
7524 btrfs_put_block_group(block_group);
7525 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05007526 } else {
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007527 index = btrfs_bg_flags_to_raid_index(
7528 block_group->flags);
Miao Xiee570fd22014-06-19 10:42:50 +08007529 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04007530 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05007531 }
Josef Bacik2552d172009-04-03 10:14:19 -04007532 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007533 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04007534 }
Chris Mason42e70e72008-11-07 18:17:11 -05007535 }
Josef Bacik2552d172009-04-03 10:14:19 -04007536search:
Miao Xie60d2adb2011-09-09 17:34:35 +08007537 have_caching_bg = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007538 if (index == 0 || index == btrfs_bg_flags_to_raid_index(flags))
Josef Bacika5e681d2015-10-01 14:54:10 -04007539 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04007540 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007541 list_for_each_entry(block_group, &space_info->block_groups[index],
7542 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04007543 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04007544 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05007545
Jeff Mahoney14443932017-07-19 23:25:51 -04007546 /* If the block group is read-only, we can skip it entirely. */
7547 if (unlikely(block_group->ro))
7548 continue;
7549
Miao Xiee570fd22014-06-19 10:42:50 +08007550 btrfs_grab_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007551 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05007552
Chris Mason83a50de2010-12-13 15:06:46 -05007553 /*
7554 * this can happen if we end up cycling through all the
7555 * raid types, but we want to make sure we only allocate
7556 * for the proper type.
7557 */
David Sterbab6919a52013-04-29 13:39:40 +00007558 if (!block_group_bits(block_group, flags)) {
Bart Van Asschebece2e82018-06-20 10:03:31 -07007559 u64 extra = BTRFS_BLOCK_GROUP_DUP |
Chris Mason83a50de2010-12-13 15:06:46 -05007560 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007561 BTRFS_BLOCK_GROUP_RAID5 |
7562 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05007563 BTRFS_BLOCK_GROUP_RAID10;
7564
7565 /*
7566 * if they asked for extra copies and this block group
7567 * doesn't provide them, bail. This does allow us to
7568 * fill raid0 from raid1.
7569 */
David Sterbab6919a52013-04-29 13:39:40 +00007570 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05007571 goto loop;
7572 }
7573
Josef Bacik2552d172009-04-03 10:14:19 -04007574have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05007575 cached = block_group_cache_done(block_group);
7576 if (unlikely(!cached)) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007577 have_caching_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00007578 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04007579 BUG_ON(ret < 0);
7580 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05007581 }
7582
Josef Bacik36cce922013-08-05 11:15:21 -04007583 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
7584 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007585
Josef Bacik0a243252009-09-11 16:11:20 -04007586 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007587 * Ok we want to try and use the cluster allocator, so
7588 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04007589 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04007590 if (last_ptr && use_cluster) {
Miao Xie215a63d2014-01-15 20:00:56 +08007591 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05007592 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007593 /*
7594 * the refill lock keeps out other
7595 * people trying to start a new cluster
7596 */
Miao Xiee570fd22014-06-19 10:42:50 +08007597 used_block_group = btrfs_lock_cluster(block_group,
7598 last_ptr,
7599 delalloc);
7600 if (!used_block_group)
Chris Mason44fb5512009-06-04 15:34:51 -04007601 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04007602
Miao Xiee570fd22014-06-19 10:42:50 +08007603 if (used_block_group != block_group &&
7604 (used_block_group->ro ||
7605 !block_group_bits(used_block_group, flags)))
7606 goto release_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007607
7608 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007609 last_ptr,
7610 num_bytes,
7611 used_block_group->key.objectid,
7612 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007613 if (offset) {
7614 /* we have a block, we're done */
7615 spin_unlock(&last_ptr->refill_lock);
Qu Wenruo3dca5c92018-04-26 14:24:25 +08007616 trace_btrfs_reserve_extent_cluster(
Miao Xie89d43462014-01-15 20:00:57 +08007617 used_block_group,
7618 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08007619 if (used_block_group != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08007620 btrfs_release_block_group(block_group,
7621 delalloc);
Miao Xie215a63d2014-01-15 20:00:56 +08007622 block_group = used_block_group;
7623 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007624 goto checks;
7625 }
7626
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007627 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007628release_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007629 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7630 * set up a new clusters, so lets just skip it
7631 * and let the allocator find whatever block
7632 * it can find. If we reach this point, we
7633 * will have tried the cluster allocator
7634 * plenty of times and not have found
7635 * anything, so we are likely way too
7636 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007637 * anything.
7638 *
7639 * However, if the cluster is taken from the
7640 * current block group, release the cluster
7641 * first, so that we stand a better chance of
7642 * succeeding in the unclustered
7643 * allocation. */
7644 if (loop >= LOOP_NO_EMPTY_SIZE &&
Miao Xiee570fd22014-06-19 10:42:50 +08007645 used_block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007646 spin_unlock(&last_ptr->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007647 btrfs_release_block_group(used_block_group,
7648 delalloc);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007649 goto unclustered_alloc;
7650 }
7651
Chris Masonfa9c0d792009-04-03 09:47:43 -04007652 /*
7653 * this cluster didn't work out, free it and
7654 * start over
7655 */
7656 btrfs_return_cluster_to_free_space(NULL, last_ptr);
7657
Miao Xiee570fd22014-06-19 10:42:50 +08007658 if (used_block_group != block_group)
7659 btrfs_release_block_group(used_block_group,
7660 delalloc);
7661refill_cluster:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007662 if (loop >= LOOP_NO_EMPTY_SIZE) {
7663 spin_unlock(&last_ptr->refill_lock);
7664 goto unclustered_alloc;
7665 }
7666
Chris Mason8de972b2013-01-04 15:39:43 -05007667 aligned_cluster = max_t(unsigned long,
7668 empty_cluster + empty_size,
7669 block_group->full_stripe_len);
7670
Chris Masonfa9c0d792009-04-03 09:47:43 -04007671 /* allocate a cluster in this block group */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007672 ret = btrfs_find_space_cluster(fs_info, block_group,
Josef Bacik00361582013-08-14 14:02:47 -04007673 last_ptr, search_start,
7674 num_bytes,
7675 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007676 if (ret == 0) {
7677 /*
7678 * now pull our allocation out of this
7679 * cluster
7680 */
7681 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007682 last_ptr,
7683 num_bytes,
7684 search_start,
7685 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007686 if (offset) {
7687 /* we found one, proceed */
7688 spin_unlock(&last_ptr->refill_lock);
Qu Wenruo3dca5c92018-04-26 14:24:25 +08007689 trace_btrfs_reserve_extent_cluster(
Josef Bacik3f7de032011-11-10 08:29:20 -05007690 block_group, search_start,
7691 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007692 goto checks;
7693 }
Josef Bacik0a243252009-09-11 16:11:20 -04007694 } else if (!cached && loop > LOOP_CACHING_NOWAIT
7695 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007696 spin_unlock(&last_ptr->refill_lock);
7697
Josef Bacik0a243252009-09-11 16:11:20 -04007698 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007699 wait_block_group_cache_progress(block_group,
7700 num_bytes + empty_cluster + empty_size);
7701 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007702 }
Josef Bacik817d52f2009-07-13 21:29:25 -04007703
Chris Masonfa9c0d792009-04-03 09:47:43 -04007704 /*
7705 * at this point we either didn't find a cluster
7706 * or we weren't able to allocate a block from our
7707 * cluster. Free the cluster we've been trying
7708 * to use, and go to the next block group
7709 */
Josef Bacik0a243252009-09-11 16:11:20 -04007710 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007711 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04007712 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007713 }
7714
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007715unclustered_alloc:
Josef Bacikc759c4e2015-10-02 15:25:10 -04007716 /*
7717 * We are doing an unclustered alloc, set the fragmented flag so
7718 * we don't bother trying to setup a cluster again until we get
7719 * more space.
7720 */
7721 if (unlikely(last_ptr)) {
7722 spin_lock(&last_ptr->lock);
7723 last_ptr->fragmented = 1;
7724 spin_unlock(&last_ptr->lock);
7725 }
Liu Bo0c9b36e2017-02-13 15:42:30 -08007726 if (cached) {
7727 struct btrfs_free_space_ctl *ctl =
7728 block_group->free_space_ctl;
7729
7730 spin_lock(&ctl->tree_lock);
7731 if (ctl->free_space <
7732 num_bytes + empty_cluster + empty_size) {
7733 if (ctl->free_space > max_extent_size)
7734 max_extent_size = ctl->free_space;
7735 spin_unlock(&ctl->tree_lock);
7736 goto loop;
7737 }
7738 spin_unlock(&ctl->tree_lock);
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007739 }
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007740
Josef Bacik6226cb02009-04-03 10:14:18 -04007741 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08007742 num_bytes, empty_size,
7743 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007744 /*
7745 * If we didn't find a chunk, and we haven't failed on this
7746 * block group before, and this block group is in the middle of
7747 * caching and we are ok with waiting, then go ahead and wait
7748 * for progress to be made, and set failed_alloc to true.
7749 *
7750 * If failed_alloc is true then we've already waited on this
7751 * block group once and should move on to the next block group.
7752 */
7753 if (!offset && !failed_alloc && !cached &&
7754 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007755 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007756 num_bytes + empty_size);
7757 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007758 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007759 } else if (!offset) {
7760 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04007761 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007762checks:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007763 search_start = ALIGN(offset, fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04007764
Josef Bacik2552d172009-04-03 10:14:19 -04007765 /* move on to the next group */
7766 if (search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08007767 block_group->key.objectid + block_group->key.offset) {
7768 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04007769 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04007770 }
Josef Bacik80eb2342008-10-29 14:49:05 -04007771
Josef Bacik6226cb02009-04-03 10:14:18 -04007772 if (offset < search_start)
Miao Xie215a63d2014-01-15 20:00:56 +08007773 btrfs_add_free_space(block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04007774 search_start - offset);
7775 BUG_ON(offset > search_start);
7776
Wang Xiaoguang18513092016-07-25 15:51:40 +08007777 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
7778 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007779 if (ret == -EAGAIN) {
Miao Xie215a63d2014-01-15 20:00:56 +08007780 btrfs_add_free_space(block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007781 goto loop;
7782 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007783 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04007784
Josef Bacik2552d172009-04-03 10:14:19 -04007785 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007786 ins->objectid = search_start;
7787 ins->offset = num_bytes;
7788
Qu Wenruo3dca5c92018-04-26 14:24:25 +08007789 trace_btrfs_reserve_extent(block_group, search_start, num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08007790 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007791 break;
7792loop:
Josef Bacik0a243252009-09-11 16:11:20 -04007793 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007794 failed_alloc = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007795 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
7796 index);
Miao Xiee570fd22014-06-19 10:42:50 +08007797 btrfs_release_block_group(block_group, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04007798 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04007799 }
7800 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05007801
chandan13a0db52015-11-02 13:59:46 +05307802 if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg
7803 && !orig_have_caching_bg)
7804 orig_have_caching_bg = true;
7805
Miao Xie60d2adb2011-09-09 17:34:35 +08007806 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
7807 goto search;
7808
Yan, Zhengb742bb82010-05-16 10:46:24 -04007809 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
7810 goto search;
7811
Josef Bacik285ff5a2012-01-13 15:27:45 -05007812 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05007813 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
7814 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04007815 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
7816 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
7817 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
7818 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04007819 */
Josef Bacik723bda22011-05-27 16:11:38 -04007820 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04007821 index = 0;
Josef Bacika5e681d2015-10-01 14:54:10 -04007822 if (loop == LOOP_CACHING_NOWAIT) {
7823 /*
7824 * We want to skip the LOOP_CACHING_WAIT step if we
Nicholas D Steeves01327612016-05-19 21:18:45 -04007825 * don't have any uncached bgs and we've already done a
Josef Bacika5e681d2015-10-01 14:54:10 -04007826 * full search through.
7827 */
chandan13a0db52015-11-02 13:59:46 +05307828 if (orig_have_caching_bg || !full_search)
Josef Bacika5e681d2015-10-01 14:54:10 -04007829 loop = LOOP_CACHING_WAIT;
7830 else
7831 loop = LOOP_ALLOC_CHUNK;
7832 } else {
7833 loop++;
7834 }
7835
Josef Bacik817d52f2009-07-13 21:29:25 -04007836 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04007837 struct btrfs_trans_handle *trans;
Wang Shilongf017f152014-03-13 13:19:47 +08007838 int exist = 0;
Josef Bacik00361582013-08-14 14:02:47 -04007839
Wang Shilongf017f152014-03-13 13:19:47 +08007840 trans = current->journal_info;
7841 if (trans)
7842 exist = 1;
7843 else
7844 trans = btrfs_join_transaction(root);
7845
Josef Bacik00361582013-08-14 14:02:47 -04007846 if (IS_ERR(trans)) {
7847 ret = PTR_ERR(trans);
7848 goto out;
7849 }
7850
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007851 ret = do_chunk_alloc(trans, fs_info, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04007852 CHUNK_ALLOC_FORCE);
Josef Bacika5e681d2015-10-01 14:54:10 -04007853
7854 /*
7855 * If we can't allocate a new chunk we've already looped
7856 * through at least once, move on to the NO_EMPTY_SIZE
7857 * case.
7858 */
7859 if (ret == -ENOSPC)
7860 loop = LOOP_NO_EMPTY_SIZE;
7861
Josef Bacikea658ba2012-09-11 16:57:25 -04007862 /*
7863 * Do not bail out on ENOSPC since we
7864 * can do more things.
7865 */
Josef Bacik00361582013-08-14 14:02:47 -04007866 if (ret < 0 && ret != -ENOSPC)
Jeff Mahoney66642832016-06-10 18:19:25 -04007867 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -04007868 else
7869 ret = 0;
Wang Shilongf017f152014-03-13 13:19:47 +08007870 if (!exist)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007871 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04007872 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04007873 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04007874 }
7875
7876 if (loop == LOOP_NO_EMPTY_SIZE) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007877 /*
7878 * Don't loop again if we already have no empty_size and
7879 * no empty_cluster.
7880 */
7881 if (empty_size == 0 &&
7882 empty_cluster == 0) {
7883 ret = -ENOSPC;
7884 goto out;
7885 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007886 empty_size = 0;
7887 empty_cluster = 0;
7888 }
Chris Mason42e70e72008-11-07 18:17:11 -05007889
Josef Bacik723bda22011-05-27 16:11:38 -04007890 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04007891 } else if (!ins->objectid) {
7892 ret = -ENOSPC;
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007893 } else if (ins->objectid) {
Josef Bacikc759c4e2015-10-02 15:25:10 -04007894 if (!use_cluster && last_ptr) {
7895 spin_lock(&last_ptr->lock);
7896 last_ptr->window_start = ins->objectid;
7897 spin_unlock(&last_ptr->lock);
7898 }
Josef Bacik2552d172009-04-03 10:14:19 -04007899 ret = 0;
7900 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007901out:
Josef Bacik4f4db212015-09-29 11:40:47 -04007902 if (ret == -ENOSPC) {
7903 spin_lock(&space_info->lock);
7904 space_info->max_extent_size = max_extent_size;
7905 spin_unlock(&space_info->lock);
Miao Xiea4820392013-09-09 13:19:42 +08007906 ins->offset = max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04007907 }
Chris Mason0f70abe2007-02-28 16:46:22 -05007908 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05007909}
Chris Masonec44a352008-04-28 15:29:52 -04007910
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007911static void dump_space_info(struct btrfs_fs_info *fs_info,
7912 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007913 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04007914{
7915 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04007916 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007917
Josef Bacik9ed74f22009-09-11 16:12:44 -04007918 spin_lock(&info->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007919 btrfs_info(fs_info, "space_info %llu has %llu free, is %sfull",
7920 info->flags,
Liu Bo41361352017-02-13 15:42:21 -08007921 info->total_bytes - btrfs_space_info_used(info, true),
7922 info->full ? "" : "not ");
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007923 btrfs_info(fs_info,
7924 "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu",
7925 info->total_bytes, info->bytes_used, info->bytes_pinned,
7926 info->bytes_reserved, info->bytes_may_use,
7927 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007928 spin_unlock(&info->lock);
7929
7930 if (!dump_block_groups)
7931 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007932
Josef Bacik80eb2342008-10-29 14:49:05 -04007933 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007934again:
7935 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04007936 spin_lock(&cache->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007937 btrfs_info(fs_info,
7938 "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s",
7939 cache->key.objectid, cache->key.offset,
7940 btrfs_block_group_used(&cache->item), cache->pinned,
7941 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04007942 btrfs_dump_free_space(cache, bytes);
7943 spin_unlock(&cache->lock);
7944 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007945 if (++index < BTRFS_NR_RAID_TYPES)
7946 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04007947 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007948}
Zheng Yane8569812008-09-26 10:05:48 -04007949
Nikolay Borisov6f47c702018-03-13 12:22:32 +02007950/*
7951 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
7952 * hole that is at least as big as @num_bytes.
7953 *
7954 * @root - The root that will contain this extent
7955 *
7956 * @ram_bytes - The amount of space in ram that @num_bytes take. This
7957 * is used for accounting purposes. This value differs
7958 * from @num_bytes only in the case of compressed extents.
7959 *
7960 * @num_bytes - Number of bytes to allocate on-disk.
7961 *
7962 * @min_alloc_size - Indicates the minimum amount of space that the
7963 * allocator should try to satisfy. In some cases
7964 * @num_bytes may be larger than what is required and if
7965 * the filesystem is fragmented then allocation fails.
7966 * However, the presence of @min_alloc_size gives a
7967 * chance to try and satisfy the smaller allocation.
7968 *
7969 * @empty_size - A hint that you plan on doing more COW. This is the
7970 * size in bytes the allocator should try to find free
7971 * next to the block it returns. This is just a hint and
7972 * may be ignored by the allocator.
7973 *
7974 * @hint_byte - Hint to the allocator to start searching above the byte
7975 * address passed. It might be ignored.
7976 *
7977 * @ins - This key is modified to record the found hole. It will
7978 * have the following values:
7979 * ins->objectid == start position
7980 * ins->flags = BTRFS_EXTENT_ITEM_KEY
7981 * ins->offset == the size of the hole.
7982 *
7983 * @is_data - Boolean flag indicating whether an extent is
7984 * allocated for data (true) or metadata (false)
7985 *
7986 * @delalloc - Boolean flag indicating whether this allocation is for
7987 * delalloc or not. If 'true' data_rwsem of block groups
7988 * is going to be acquired.
7989 *
7990 *
7991 * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
7992 * case -ENOSPC is returned then @ins->offset will contain the size of the
7993 * largest available hole the allocator managed to find.
7994 */
Wang Xiaoguang18513092016-07-25 15:51:40 +08007995int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04007996 u64 num_bytes, u64 min_alloc_size,
7997 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08007998 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007999{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008000 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04008001 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00008002 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05008003 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04008004
Jeff Mahoney1b868262017-05-17 11:38:35 -04008005 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04008006again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008007 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05008008 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08008009 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008010 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008011 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008012 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08008013 if (!final_tried && ins->offset) {
8014 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04008015 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008016 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05008017 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08008018 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05008019 if (num_bytes == min_alloc_size)
8020 final_tried = true;
8021 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008022 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05008023 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008024
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008025 sinfo = __find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008026 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008027 "allocation failed flags %llu, wanted %llu",
8028 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01008029 if (sinfo)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008030 dump_space_info(fs_info, sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05008031 }
Chris Mason925baed2008-06-25 16:01:30 -04008032 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008033
8034 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008035}
8036
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008037static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008038 u64 start, u64 len,
8039 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04008040{
Josef Bacik0f9dd462008-09-23 13:14:11 -04008041 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05008042 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008043
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008044 cache = btrfs_lookup_block_group(fs_info, start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008045 if (!cache) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008046 btrfs_err(fs_info, "Unable to find block group for %llu",
8047 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008048 return -ENOSPC;
8049 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05008050
Chris Masone688b7252011-10-31 20:52:39 -04008051 if (pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008052 pin_down_extent(fs_info, cache, start, len, 1);
Chris Masone688b7252011-10-31 20:52:39 -04008053 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008054 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008055 ret = btrfs_discard_extent(fs_info, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04008056 btrfs_add_free_space(cache, start, len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08008057 btrfs_free_reserved_bytes(cache, len, delalloc);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008058 trace_btrfs_reserved_extent_free(fs_info, start, len);
Chris Masone688b7252011-10-31 20:52:39 -04008059 }
Dongsheng Yang31193212014-12-12 16:44:35 +08008060
Chris Masonfa9c0d792009-04-03 09:47:43 -04008061 btrfs_put_block_group(cache);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008062 return ret;
8063}
8064
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008065int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008066 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04008067{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008068 return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04008069}
8070
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008071int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04008072 u64 start, u64 len)
8073{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008074 return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04008075}
8076
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008077static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008078 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008079 u64 parent, u64 root_objectid,
8080 u64 flags, u64 owner, u64 offset,
8081 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008082{
8083 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008084 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008085 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008086 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008087 struct extent_buffer *leaf;
8088 int type;
8089 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04008090
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008091 if (parent > 0)
8092 type = BTRFS_SHARED_DATA_REF_KEY;
8093 else
8094 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04008095
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008096 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05008097
8098 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00008099 if (!path)
8100 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05008101
Chris Masonb9473432009-03-13 11:00:37 -04008102 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008103 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8104 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008105 if (ret) {
8106 btrfs_free_path(path);
8107 return ret;
8108 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008109
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008110 leaf = path->nodes[0];
8111 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05008112 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008113 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
8114 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8115 btrfs_set_extent_flags(leaf, extent_item,
8116 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05008117
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008118 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
8119 btrfs_set_extent_inline_ref_type(leaf, iref, type);
8120 if (parent > 0) {
8121 struct btrfs_shared_data_ref *ref;
8122 ref = (struct btrfs_shared_data_ref *)(iref + 1);
8123 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8124 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
8125 } else {
8126 struct btrfs_extent_data_ref *ref;
8127 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
8128 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
8129 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
8130 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
8131 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
8132 }
Chris Mason47e4bb92008-02-01 14:51:59 -05008133
8134 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05008135 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04008136
Nikolay Borisov25a356d2018-05-10 15:44:54 +03008137 ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008138 if (ret)
8139 return ret;
8140
Jeff Mahoney6202df62016-06-22 18:54:22 -04008141 ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008142 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008143 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008144 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05008145 BUG();
8146 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008147 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008148 return ret;
8149}
8150
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008151static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008152 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03008153 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008154{
Nikolay Borisov9dcdbe02018-05-21 12:27:20 +03008155 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008156 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008157 struct btrfs_extent_item *extent_item;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008158 struct btrfs_key extent_key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008159 struct btrfs_tree_block_info *block_info;
8160 struct btrfs_extent_inline_ref *iref;
8161 struct btrfs_path *path;
8162 struct extent_buffer *leaf;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008163 struct btrfs_delayed_tree_ref *ref;
Josef Bacik3173a182013-03-07 14:22:04 -05008164 u32 size = sizeof(*extent_item) + sizeof(*iref);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008165 u64 num_bytes;
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03008166 u64 flags = extent_op->flags_to_set;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008167 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05008168
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008169 ref = btrfs_delayed_node_to_tree_ref(node);
8170
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008171 extent_key.objectid = node->bytenr;
8172 if (skinny_metadata) {
8173 extent_key.offset = ref->level;
8174 extent_key.type = BTRFS_METADATA_ITEM_KEY;
8175 num_bytes = fs_info->nodesize;
8176 } else {
8177 extent_key.offset = node->num_bytes;
8178 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik3173a182013-03-07 14:22:04 -05008179 size += sizeof(*block_info);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008180 num_bytes = node->num_bytes;
8181 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008182
8183 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04008184 if (!path) {
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008185 btrfs_free_and_pin_reserved_extent(fs_info,
8186 extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008187 fs_info->nodesize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07008188 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04008189 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008190
8191 path->leave_spinning = 1;
8192 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008193 &extent_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008194 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07008195 btrfs_free_path(path);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008196 btrfs_free_and_pin_reserved_extent(fs_info,
8197 extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008198 fs_info->nodesize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008199 return ret;
8200 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008201
8202 leaf = path->nodes[0];
8203 extent_item = btrfs_item_ptr(leaf, path->slots[0],
8204 struct btrfs_extent_item);
8205 btrfs_set_extent_refs(leaf, extent_item, 1);
8206 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8207 btrfs_set_extent_flags(leaf, extent_item,
8208 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008209
Josef Bacik3173a182013-03-07 14:22:04 -05008210 if (skinny_metadata) {
8211 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
8212 } else {
8213 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03008214 btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008215 btrfs_set_tree_block_level(leaf, block_info, ref->level);
Josef Bacik3173a182013-03-07 14:22:04 -05008216 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
8217 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008218
Nikolay Borisovd4b20732018-05-21 12:27:23 +03008219 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008220 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
8221 btrfs_set_extent_inline_ref_type(leaf, iref,
8222 BTRFS_SHARED_BLOCK_REF_KEY);
Nikolay Borisovd4b20732018-05-21 12:27:23 +03008223 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008224 } else {
8225 btrfs_set_extent_inline_ref_type(leaf, iref,
8226 BTRFS_TREE_BLOCK_REF_KEY);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008227 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008228 }
8229
8230 btrfs_mark_buffer_dirty(leaf);
8231 btrfs_free_path(path);
8232
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008233 ret = remove_from_free_space_tree(trans, extent_key.objectid,
8234 num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008235 if (ret)
8236 return ret;
8237
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008238 ret = update_block_group(trans, fs_info, extent_key.objectid,
Jeff Mahoney6202df62016-06-22 18:54:22 -04008239 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008240 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008241 btrfs_err(fs_info, "update block group failed for %llu %llu",
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008242 extent_key.objectid, extent_key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008243 BUG();
8244 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04008245
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008246 trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008247 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008248 return ret;
8249}
8250
8251int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008252 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08008253 u64 offset, u64 ram_bytes,
8254 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008255{
8256 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04008257
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008258 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04008259
Josef Bacikfd708b82017-09-29 15:43:50 -04008260 btrfs_ref_tree_mod(root, ins->objectid, ins->offset, 0,
8261 root->root_key.objectid, owner, offset,
8262 BTRFS_ADD_DELAYED_EXTENT);
8263
Nikolay Borisov88a979c2018-06-20 15:48:54 +03008264 ret = btrfs_add_delayed_data_ref(trans, ins->objectid,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008265 ins->offset, 0,
8266 root->root_key.objectid, owner,
Omar Sandoval7be07912017-06-06 16:45:30 -07008267 offset, ram_bytes,
8268 BTRFS_ADD_DELAYED_EXTENT, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008269 return ret;
8270}
Chris Masone02119d2008-09-05 16:13:11 -04008271
8272/*
8273 * this is used by the tree logging recovery code. It records that
8274 * an extent has been allocated and makes sure to clear the free
8275 * space cache bits as well
8276 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008277int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008278 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008279 u64 root_objectid, u64 owner, u64 offset,
8280 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04008281{
8282 int ret;
8283 struct btrfs_block_group_cache *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008284 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008285
8286 /*
8287 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04008288 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008289 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008290 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008291 ret = __exclude_logged_extent(fs_info, ins->objectid,
8292 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008293 if (ret)
8294 return ret;
8295 }
Chris Masone02119d2008-09-05 16:13:11 -04008296
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008297 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008298 if (!block_group)
8299 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04008300
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008301 space_info = block_group->space_info;
8302 spin_lock(&space_info->lock);
8303 spin_lock(&block_group->lock);
8304 space_info->bytes_reserved += ins->offset;
8305 block_group->reserved += ins->offset;
8306 spin_unlock(&block_group->lock);
8307 spin_unlock(&space_info->lock);
8308
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008309 ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008310 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04008311 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04008312 return ret;
8313}
8314
Eric Sandeen48a3b632013-04-25 20:41:01 +00008315static struct extent_buffer *
8316btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
David Sterbafe864572014-06-15 02:28:42 +02008317 u64 bytenr, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04008318{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008319 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04008320 struct extent_buffer *buf;
8321
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008322 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008323 if (IS_ERR(buf))
8324 return buf;
8325
Chris Mason65b51a02008-08-01 15:11:20 -04008326 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04008327 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04008328 btrfs_tree_lock(buf);
David Sterba7c302b42017-02-10 18:47:57 +01008329 clean_tree_block(fs_info, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05008330 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008331
8332 btrfs_set_lock_blocking(buf);
David Sterba4db8c522015-12-03 13:06:46 +01008333 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008334
Chris Masond0c803c2008-09-11 16:17:57 -04008335 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01008336 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008337 /*
8338 * we allow two log transactions at a time, use different
8339 * EXENT bit to differentiate dirty pages.
8340 */
Filipe Manana656f30d2014-09-26 12:25:56 +01008341 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008342 set_extent_dirty(&root->dirty_log_pages, buf->start,
8343 buf->start + buf->len - 1, GFP_NOFS);
8344 else
8345 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02008346 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04008347 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01008348 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04008349 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
8350 buf->start + buf->len - 1, GFP_NOFS);
8351 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04008352 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05008353 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04008354 return buf;
8355}
8356
Yan, Zhengf0486c62010-05-16 10:46:25 -04008357static struct btrfs_block_rsv *
8358use_block_rsv(struct btrfs_trans_handle *trans,
8359 struct btrfs_root *root, u32 blocksize)
8360{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008361 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008362 struct btrfs_block_rsv *block_rsv;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008363 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008364 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00008365 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008366
8367 block_rsv = get_block_rsv(trans, root);
8368
Miao Xieb586b322013-05-13 13:55:10 +00008369 if (unlikely(block_rsv->size == 0))
8370 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00008371again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04008372 ret = block_rsv_use_bytes(block_rsv, blocksize);
8373 if (!ret)
8374 return block_rsv;
8375
Miao Xieb586b322013-05-13 13:55:10 +00008376 if (block_rsv->failfast)
8377 return ERR_PTR(ret);
8378
Miao Xied88033d2013-05-13 13:55:12 +00008379 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
8380 global_updated = true;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008381 update_global_block_rsv(fs_info);
Miao Xied88033d2013-05-13 13:55:12 +00008382 goto again;
8383 }
8384
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008385 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xieb586b322013-05-13 13:55:10 +00008386 static DEFINE_RATELIMIT_STATE(_rs,
8387 DEFAULT_RATELIMIT_INTERVAL * 10,
8388 /*DEFAULT_RATELIMIT_BURST*/ 1);
8389 if (__ratelimit(&_rs))
8390 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05008391 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00008392 }
8393try_reserve:
8394 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
8395 BTRFS_RESERVE_NO_FLUSH);
8396 if (!ret)
8397 return block_rsv;
8398 /*
8399 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00008400 * the global reserve if its space type is the same as the global
8401 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00008402 */
Miao Xie5881cfc2013-05-13 13:55:11 +00008403 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
8404 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00008405 ret = block_rsv_use_bytes(global_rsv, blocksize);
8406 if (!ret)
8407 return global_rsv;
8408 }
8409 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008410}
8411
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008412static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
8413 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008414{
8415 block_rsv_add_bytes(block_rsv, blocksize, 0);
Qu Wenruoff6bc372017-12-21 13:42:04 +08008416 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0, NULL);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008417}
8418
Chris Masonfec577f2007-02-26 10:40:21 -05008419/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04008420 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08008421 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05008422 */
David Sterba4d75f8a2014-06-15 01:54:12 +02008423struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08008424 struct btrfs_root *root,
8425 u64 parent, u64 root_objectid,
8426 const struct btrfs_disk_key *key,
8427 int level, u64 hint,
8428 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05008429{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008430 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04008431 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008432 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04008433 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008434 struct btrfs_delayed_extent_op *extent_op;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008435 u64 flags = 0;
8436 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008437 u32 blocksize = fs_info->nodesize;
8438 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008439
David Sterba05653ef2016-07-15 15:23:37 +02008440#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008441 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008442 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
David Sterbafe864572014-06-15 02:28:42 +02008443 level);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008444 if (!IS_ERR(buf))
8445 root->alloc_bytenr += blocksize;
8446 return buf;
8447 }
David Sterba05653ef2016-07-15 15:23:37 +02008448#endif
David Sterbafccb84c2014-09-29 23:53:21 +02008449
Yan, Zhengf0486c62010-05-16 10:46:25 -04008450 block_rsv = use_block_rsv(trans, root, blocksize);
8451 if (IS_ERR(block_rsv))
8452 return ERR_CAST(block_rsv);
8453
Wang Xiaoguang18513092016-07-25 15:51:40 +08008454 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08008455 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008456 if (ret)
8457 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05008458
David Sterbafe864572014-06-15 02:28:42 +02008459 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
Omar Sandoval67b78592015-02-24 02:47:04 -08008460 if (IS_ERR(buf)) {
8461 ret = PTR_ERR(buf);
8462 goto out_free_reserved;
8463 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008464
8465 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
8466 if (parent == 0)
8467 parent = ins.objectid;
8468 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8469 } else
8470 BUG_ON(parent > 0);
8471
8472 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00008473 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08008474 if (!extent_op) {
8475 ret = -ENOMEM;
8476 goto out_free_buf;
8477 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008478 if (key)
8479 memcpy(&extent_op->key, key, sizeof(extent_op->key));
8480 else
8481 memset(&extent_op->key, 0, sizeof(extent_op->key));
8482 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01008483 extent_op->update_key = skinny_metadata ? false : true;
8484 extent_op->update_flags = true;
8485 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04008486 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008487
Josef Bacikfd708b82017-09-29 15:43:50 -04008488 btrfs_ref_tree_mod(root, ins.objectid, ins.offset, parent,
8489 root_objectid, level, 0,
8490 BTRFS_ADD_DELAYED_EXTENT);
Nikolay Borisov44e1c472018-06-20 15:48:53 +03008491 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
Omar Sandoval7be07912017-06-06 16:45:30 -07008492 ins.offset, parent,
8493 root_objectid, level,
Omar Sandoval67b78592015-02-24 02:47:04 -08008494 BTRFS_ADD_DELAYED_EXTENT,
Omar Sandoval7be07912017-06-06 16:45:30 -07008495 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08008496 if (ret)
8497 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008498 }
Chris Masonfec577f2007-02-26 10:40:21 -05008499 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008500
8501out_free_delayed:
8502 btrfs_free_delayed_extent_op(extent_op);
8503out_free_buf:
8504 free_extent_buffer(buf);
8505out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008506 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008507out_unuse:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008508 unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08008509 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05008510}
Chris Masona28ec192007-03-06 20:08:01 -05008511
Yan Zheng2c47e6052009-06-27 21:07:35 -04008512struct walk_control {
8513 u64 refs[BTRFS_MAX_LEVEL];
8514 u64 flags[BTRFS_MAX_LEVEL];
8515 struct btrfs_key update_progress;
8516 int stage;
8517 int level;
8518 int shared_level;
8519 int update_ref;
8520 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008521 int reada_slot;
8522 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008523 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008524};
8525
8526#define DROP_REFERENCE 1
8527#define UPDATE_BACKREF 2
8528
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008529static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8530 struct btrfs_root *root,
8531 struct walk_control *wc,
8532 struct btrfs_path *path)
8533{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008534 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008535 u64 bytenr;
8536 u64 generation;
8537 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008538 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008539 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008540 struct btrfs_key key;
8541 struct extent_buffer *eb;
8542 int ret;
8543 int slot;
8544 int nread = 0;
8545
8546 if (path->slots[wc->level] < wc->reada_slot) {
8547 wc->reada_count = wc->reada_count * 2 / 3;
8548 wc->reada_count = max(wc->reada_count, 2);
8549 } else {
8550 wc->reada_count = wc->reada_count * 3 / 2;
8551 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008552 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008553 }
8554
8555 eb = path->nodes[wc->level];
8556 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008557
8558 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
8559 if (nread >= wc->reada_count)
8560 break;
8561
8562 cond_resched();
8563 bytenr = btrfs_node_blockptr(eb, slot);
8564 generation = btrfs_node_ptr_generation(eb, slot);
8565
8566 if (slot == path->slots[wc->level])
8567 goto reada;
8568
8569 if (wc->stage == UPDATE_BACKREF &&
8570 generation <= root->root_key.offset)
8571 continue;
8572
Yan, Zheng94fcca92009-10-09 09:25:16 -04008573 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008574 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05008575 wc->level - 1, 1, &refs,
8576 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008577 /* We don't care about errors in readahead. */
8578 if (ret < 0)
8579 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008580 BUG_ON(refs == 0);
8581
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008582 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008583 if (refs == 1)
8584 goto reada;
8585
Yan, Zheng94fcca92009-10-09 09:25:16 -04008586 if (wc->level == 1 &&
8587 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8588 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008589 if (!wc->update_ref ||
8590 generation <= root->root_key.offset)
8591 continue;
8592 btrfs_node_key_to_cpu(eb, &key, slot);
8593 ret = btrfs_comp_cpu_keys(&key,
8594 &wc->update_progress);
8595 if (ret < 0)
8596 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008597 } else {
8598 if (wc->level == 1 &&
8599 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8600 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008601 }
8602reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008603 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008604 nread++;
8605 }
8606 wc->reada_slot = slot;
8607}
8608
Chris Mason9aca1d52007-03-13 11:09:37 -04008609/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008610 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008611 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04008612 * when wc->stage == UPDATE_BACKREF, this function updates
8613 * back refs for pointers in the block.
8614 *
8615 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04008616 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008617static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8618 struct btrfs_root *root,
8619 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008620 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04008621{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008622 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008623 int level = wc->level;
8624 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04008625 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8626 int ret;
8627
8628 if (wc->stage == UPDATE_BACKREF &&
8629 btrfs_header_owner(eb) != root->root_key.objectid)
8630 return 1;
8631
8632 /*
8633 * when reference count of tree block is 1, it won't increase
8634 * again. once full backref flag is set, we never clear it.
8635 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04008636 if (lookup_info &&
8637 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
8638 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008639 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008640 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008641 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008642 &wc->refs[level],
8643 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008644 BUG_ON(ret == -ENOMEM);
8645 if (ret)
8646 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008647 BUG_ON(wc->refs[level] == 0);
8648 }
8649
Yan Zheng2c47e6052009-06-27 21:07:35 -04008650 if (wc->stage == DROP_REFERENCE) {
8651 if (wc->refs[level] > 1)
8652 return 1;
8653
8654 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04008655 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008656 path->locks[level] = 0;
8657 }
8658 return 0;
8659 }
8660
8661 /* wc->stage == UPDATE_BACKREF */
8662 if (!(wc->flags[level] & flag)) {
8663 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07008664 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008665 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07008666 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008667 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008668 ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04008669 eb->len, flag,
8670 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008671 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008672 wc->flags[level] |= flag;
8673 }
8674
8675 /*
8676 * the block is shared by multiple trees, so it's not good to
8677 * keep the tree lock
8678 */
8679 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04008680 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008681 path->locks[level] = 0;
8682 }
8683 return 0;
8684}
8685
8686/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008687 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008688 *
8689 * when wc->stage == DROP_REFERENCE, this function checks
8690 * reference count of the block pointed to. if the block
8691 * is shared and we need update back refs for the subtree
8692 * rooted at the block, this function changes wc->stage to
8693 * UPDATE_BACKREF. if the block is shared and there is no
8694 * need to update back, this function drops the reference
8695 * to the block.
8696 *
8697 * NOTE: return value 1 means we should stop walking down.
8698 */
8699static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8700 struct btrfs_root *root,
8701 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008702 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008703{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008704 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008705 u64 bytenr;
8706 u64 generation;
8707 u64 parent;
8708 u32 blocksize;
8709 struct btrfs_key key;
Qu Wenruo581c1762018-03-29 09:08:11 +08008710 struct btrfs_key first_key;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008711 struct extent_buffer *next;
8712 int level = wc->level;
8713 int reada = 0;
8714 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07008715 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008716
8717 generation = btrfs_node_ptr_generation(path->nodes[level],
8718 path->slots[level]);
8719 /*
8720 * if the lower level block was created before the snapshot
8721 * was created, we know there is no need to update back refs
8722 * for the subtree
8723 */
8724 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04008725 generation <= root->root_key.offset) {
8726 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008727 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008728 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008729
8730 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08008731 btrfs_node_key_to_cpu(path->nodes[level], &first_key,
8732 path->slots[level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008733 blocksize = fs_info->nodesize;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008734
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008735 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008736 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008737 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008738 if (IS_ERR(next))
8739 return PTR_ERR(next);
8740
Josef Bacikb2aaaa32013-07-05 17:05:38 -04008741 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
8742 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008743 reada = 1;
8744 }
8745 btrfs_tree_lock(next);
8746 btrfs_set_lock_blocking(next);
8747
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008748 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008749 &wc->refs[level - 1],
8750 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02008751 if (ret < 0)
8752 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008753
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008754 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008755 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02008756 ret = -EIO;
8757 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008758 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008759 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008760
Yan, Zheng94fcca92009-10-09 09:25:16 -04008761 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008762 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07008763 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008764 if (level == 1 &&
8765 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8766 goto skip;
8767
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008768 if (!wc->update_ref ||
8769 generation <= root->root_key.offset)
8770 goto skip;
8771
8772 btrfs_node_key_to_cpu(path->nodes[level], &key,
8773 path->slots[level]);
8774 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
8775 if (ret < 0)
8776 goto skip;
8777
8778 wc->stage = UPDATE_BACKREF;
8779 wc->shared_level = level - 1;
8780 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008781 } else {
8782 if (level == 1 &&
8783 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8784 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008785 }
8786
Chris Masonb9fab912012-05-06 07:23:47 -04008787 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008788 btrfs_tree_unlock(next);
8789 free_extent_buffer(next);
8790 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008791 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008792 }
8793
8794 if (!next) {
8795 if (reada && level == 1)
8796 reada_walk_down(trans, root, wc, path);
Qu Wenruo581c1762018-03-29 09:08:11 +08008797 next = read_tree_block(fs_info, bytenr, generation, level - 1,
8798 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08008799 if (IS_ERR(next)) {
8800 return PTR_ERR(next);
8801 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04008802 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008803 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04008804 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008805 btrfs_tree_lock(next);
8806 btrfs_set_lock_blocking(next);
8807 }
8808
8809 level--;
Josef Bacik48672682016-09-23 13:23:28 +02008810 ASSERT(level == btrfs_header_level(next));
8811 if (level != btrfs_header_level(next)) {
8812 btrfs_err(root->fs_info, "mismatched level");
8813 ret = -EIO;
8814 goto out_unlock;
8815 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008816 path->nodes[level] = next;
8817 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008818 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008819 wc->level = level;
8820 if (wc->level == 1)
8821 wc->reada_slot = 0;
8822 return 0;
8823skip:
8824 wc->refs[level - 1] = 0;
8825 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008826 if (wc->stage == DROP_REFERENCE) {
8827 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
8828 parent = path->nodes[level]->start;
8829 } else {
Josef Bacik48672682016-09-23 13:23:28 +02008830 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04008831 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02008832 if (root->root_key.objectid !=
8833 btrfs_header_owner(path->nodes[level])) {
8834 btrfs_err(root->fs_info,
8835 "mismatched block owner");
8836 ret = -EIO;
8837 goto out_unlock;
8838 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008839 parent = 0;
8840 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008841
Mark Fasheh11526512014-07-17 12:39:01 -07008842 if (need_account) {
Qu Wenruo33d1f052016-10-18 09:31:28 +08008843 ret = btrfs_qgroup_trace_subtree(trans, root, next,
8844 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07008845 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008846 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008847 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
8848 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008849 }
8850 }
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008851 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008852 parent, root->root_key.objectid,
8853 level - 1, 0);
Josef Bacik48672682016-09-23 13:23:28 +02008854 if (ret)
8855 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008856 }
Josef Bacik48672682016-09-23 13:23:28 +02008857
8858 *lookup_info = 1;
8859 ret = 1;
8860
8861out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008862 btrfs_tree_unlock(next);
8863 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02008864
8865 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008866}
8867
8868/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008869 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008870 *
8871 * when wc->stage == DROP_REFERENCE, this function drops
8872 * reference count on the block.
8873 *
8874 * when wc->stage == UPDATE_BACKREF, this function changes
8875 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8876 * to UPDATE_BACKREF previously while processing the block.
8877 *
8878 * NOTE: return value 1 means we should stop walking up.
8879 */
8880static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8881 struct btrfs_root *root,
8882 struct btrfs_path *path,
8883 struct walk_control *wc)
8884{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008885 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008886 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008887 int level = wc->level;
8888 struct extent_buffer *eb = path->nodes[level];
8889 u64 parent = 0;
8890
8891 if (wc->stage == UPDATE_BACKREF) {
8892 BUG_ON(wc->shared_level < level);
8893 if (level < wc->shared_level)
8894 goto out;
8895
Yan Zheng2c47e6052009-06-27 21:07:35 -04008896 ret = find_next_key(path, level + 1, &wc->update_progress);
8897 if (ret > 0)
8898 wc->update_ref = 0;
8899
8900 wc->stage = DROP_REFERENCE;
8901 wc->shared_level = -1;
8902 path->slots[level] = 0;
8903
8904 /*
8905 * check reference count again if the block isn't locked.
8906 * we should start walking down the tree again if reference
8907 * count is one.
8908 */
8909 if (!path->locks[level]) {
8910 BUG_ON(level == 0);
8911 btrfs_tree_lock(eb);
8912 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008913 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008914
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008915 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008916 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008917 &wc->refs[level],
8918 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008919 if (ret < 0) {
8920 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008921 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008922 return ret;
8923 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008924 BUG_ON(wc->refs[level] == 0);
8925 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04008926 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008927 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008928 return 1;
8929 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008930 }
8931 }
8932
8933 /* wc->stage == DROP_REFERENCE */
8934 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
8935
8936 if (wc->refs[level] == 1) {
8937 if (level == 0) {
8938 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07008939 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008940 else
Josef Bacike339a6b2014-07-02 10:54:25 -07008941 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008942 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008943 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb);
Mark Fasheh11526512014-07-17 12:39:01 -07008944 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008945 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008946 "error %d accounting leaf items. Quota is out of sync, rescan required.",
8947 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008948 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008949 }
8950 /* make block locked assertion in clean_tree_block happy */
8951 if (!path->locks[level] &&
8952 btrfs_header_generation(eb) == trans->transid) {
8953 btrfs_tree_lock(eb);
8954 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008955 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008956 }
David Sterba7c302b42017-02-10 18:47:57 +01008957 clean_tree_block(fs_info, eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008958 }
8959
8960 if (eb == root->node) {
8961 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8962 parent = eb->start;
8963 else
8964 BUG_ON(root->root_key.objectid !=
8965 btrfs_header_owner(eb));
8966 } else {
8967 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8968 parent = path->nodes[level + 1]->start;
8969 else
8970 BUG_ON(root->root_key.objectid !=
8971 btrfs_header_owner(path->nodes[level + 1]));
8972 }
8973
Jan Schmidt5581a512012-05-16 17:04:52 +02008974 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008975out:
8976 wc->refs[level] = 0;
8977 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008978 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008979}
8980
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008981static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
8982 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008983 struct btrfs_path *path,
8984 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008985{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008986 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008987 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008988 int ret;
8989
Yan Zheng2c47e6052009-06-27 21:07:35 -04008990 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04008991 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008992 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008993 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008994
Yan Zheng2c47e6052009-06-27 21:07:35 -04008995 if (level == 0)
8996 break;
8997
Yan, Zheng7a7965f2010-02-01 02:41:17 +00008998 if (path->slots[level] >=
8999 btrfs_header_nritems(path->nodes[level]))
9000 break;
9001
Yan, Zheng94fcca92009-10-09 09:25:16 -04009002 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009003 if (ret > 0) {
9004 path->slots[level]++;
9005 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00009006 } else if (ret < 0)
9007 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009008 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009009 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009010 return 0;
9011}
9012
Chris Masond3977122009-01-05 21:25:51 -05009013static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05009014 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04009015 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009016 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05009017{
Yan Zheng2c47e6052009-06-27 21:07:35 -04009018 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05009019 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04009020
Yan Zheng2c47e6052009-06-27 21:07:35 -04009021 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
9022 while (level < max_level && path->nodes[level]) {
9023 wc->level = level;
9024 if (path->slots[level] + 1 <
9025 btrfs_header_nritems(path->nodes[level])) {
9026 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05009027 return 0;
9028 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009029 ret = walk_up_proc(trans, root, path, wc);
9030 if (ret > 0)
9031 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05009032
Yan Zheng2c47e6052009-06-27 21:07:35 -04009033 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04009034 btrfs_tree_unlock_rw(path->nodes[level],
9035 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009036 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009037 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009038 free_extent_buffer(path->nodes[level]);
9039 path->nodes[level] = NULL;
9040 level++;
Chris Mason20524f02007-03-10 06:35:47 -05009041 }
9042 }
9043 return 1;
9044}
9045
Chris Mason9aca1d52007-03-13 11:09:37 -04009046/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04009047 * drop a subvolume tree.
9048 *
9049 * this function traverses the tree freeing any blocks that only
9050 * referenced by the tree.
9051 *
9052 * when a shared tree block is found. this function decreases its
9053 * reference count by one. if update_ref is true, this function
9054 * also make sure backrefs for the shared block and all lower level
9055 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00009056 *
9057 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04009058 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04009059int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009060 struct btrfs_block_rsv *block_rsv, int update_ref,
9061 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05009062{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009063 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04009064 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009065 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009066 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04009067 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009068 struct walk_control *wc;
9069 struct btrfs_key key;
9070 int err = 0;
9071 int ret;
9072 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04009073 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05009074
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009075 btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07009076
Chris Mason5caf2a02007-04-02 11:20:42 -04009077 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009078 if (!path) {
9079 err = -ENOMEM;
9080 goto out;
9081 }
Chris Mason20524f02007-03-10 06:35:47 -05009082
Yan Zheng2c47e6052009-06-27 21:07:35 -04009083 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07009084 if (!wc) {
9085 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009086 err = -ENOMEM;
9087 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07009088 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009089
Yan, Zhenga22285a2010-05-16 10:48:46 -04009090 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009091 if (IS_ERR(trans)) {
9092 err = PTR_ERR(trans);
9093 goto out_free;
9094 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00009095
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009096 if (block_rsv)
9097 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009098
Chris Mason9f3a7422007-08-07 15:52:19 -04009099 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009100 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009101 path->nodes[level] = btrfs_lock_root_node(root);
9102 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04009103 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009104 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009105 memset(&wc->update_progress, 0,
9106 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04009107 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04009108 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009109 memcpy(&wc->update_progress, &key,
9110 sizeof(wc->update_progress));
9111
Chris Mason6702ed42007-08-07 16:15:09 -04009112 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009113 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04009114 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009115 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
9116 path->lowest_level = 0;
9117 if (ret < 0) {
9118 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009119 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04009120 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009121 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009122
Chris Mason7d9eb122008-07-08 14:19:17 -04009123 /*
9124 * unlock our path, this is safe because only this
9125 * function is allowed to delete this snapshot
9126 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009127 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04009128
Yan Zheng2c47e6052009-06-27 21:07:35 -04009129 level = btrfs_header_level(root->node);
9130 while (1) {
9131 btrfs_tree_lock(path->nodes[level]);
9132 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009133 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009134
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009135 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009136 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05009137 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04009138 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009139 if (ret < 0) {
9140 err = ret;
9141 goto out_end_trans;
9142 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009143 BUG_ON(wc->refs[level] == 0);
9144
9145 if (level == root_item->drop_level)
9146 break;
9147
9148 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009149 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009150 WARN_ON(wc->refs[level] != 1);
9151 level--;
9152 }
9153 }
9154
9155 wc->level = level;
9156 wc->shared_level = -1;
9157 wc->stage = DROP_REFERENCE;
9158 wc->update_ref = update_ref;
9159 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009160 wc->for_reloc = for_reloc;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009161 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009162
9163 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00009164
Yan Zheng2c47e6052009-06-27 21:07:35 -04009165 ret = walk_down_tree(trans, root, path, wc);
9166 if (ret < 0) {
9167 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04009168 break;
9169 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009170
9171 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
9172 if (ret < 0) {
9173 err = ret;
9174 break;
9175 }
9176
9177 if (ret > 0) {
9178 BUG_ON(wc->stage != DROP_REFERENCE);
9179 break;
9180 }
9181
9182 if (wc->stage == DROP_REFERENCE) {
9183 level = wc->level;
9184 btrfs_node_key(path->nodes[level],
9185 &root_item->drop_progress,
9186 path->slots[level]);
9187 root_item->drop_level = level;
9188 }
9189
9190 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009191 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009192 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009193 ret = btrfs_update_root(trans, tree_root,
9194 &root->root_key,
9195 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009196 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009197 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009198 err = ret;
9199 goto out_end_trans;
9200 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009201
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009202 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009203 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009204 btrfs_debug(fs_info,
9205 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04009206 err = -EAGAIN;
9207 goto out_free;
9208 }
9209
Yan, Zhenga22285a2010-05-16 10:48:46 -04009210 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009211 if (IS_ERR(trans)) {
9212 err = PTR_ERR(trans);
9213 goto out_free;
9214 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009215 if (block_rsv)
9216 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04009217 }
Chris Mason20524f02007-03-10 06:35:47 -05009218 }
David Sterbab3b4aa72011-04-21 01:20:15 +02009219 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009220 if (err)
9221 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009222
Jeff Mahoney1cd54472017-08-17 10:25:11 -04009223 ret = btrfs_del_root(trans, fs_info, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009224 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009225 btrfs_abort_transaction(trans, ret);
Jeff Mahoneye19182c2017-12-04 13:11:45 -05009226 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009227 goto out_end_trans;
9228 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009229
Yan, Zheng76dda932009-09-21 16:00:26 -04009230 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00009231 ret = btrfs_find_root(tree_root, &root->root_key, path,
9232 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009233 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009234 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009235 err = ret;
9236 goto out_end_trans;
9237 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05009238 /* if we fail to delete the orphan item this time
9239 * around, it'll get picked up the next time.
9240 *
9241 * The most common failure here is just -ENOENT.
9242 */
9243 btrfs_del_orphan_item(trans, tree_root,
9244 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04009245 }
9246 }
9247
Miao Xie27cdeb72014-04-02 19:51:05 +08009248 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04009249 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009250 } else {
9251 free_extent_buffer(root->node);
9252 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00009253 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009254 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04009255 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009256out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009257 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009258out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04009259 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04009260 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009261out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04009262 /*
9263 * So if we need to stop dropping the snapshot for whatever reason we
9264 * need to make sure to add it back to the dead root list so that we
9265 * keep trying to do the work later. This also cleans up roots if we
9266 * don't have it in the radix (like when we recover after a power fail
9267 * or unmount) so we don't leak memory.
9268 */
Thomas Meyer897ca812017-10-07 16:02:21 +02009269 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04009270 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08009271 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009272 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04009273 return err;
Chris Mason20524f02007-03-10 06:35:47 -05009274}
Chris Mason9078a3e2007-04-26 16:46:15 -04009275
Yan Zheng2c47e6052009-06-27 21:07:35 -04009276/*
9277 * drop subtree rooted at tree block 'node'.
9278 *
9279 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009280 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04009281 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04009282int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
9283 struct btrfs_root *root,
9284 struct extent_buffer *node,
9285 struct extent_buffer *parent)
9286{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009287 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009288 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009289 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009290 int level;
9291 int parent_level;
9292 int ret = 0;
9293 int wret;
9294
Yan Zheng2c47e6052009-06-27 21:07:35 -04009295 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
9296
Yan Zhengf82d02d2008-10-29 14:49:05 -04009297 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009298 if (!path)
9299 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009300
Yan Zheng2c47e6052009-06-27 21:07:35 -04009301 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009302 if (!wc) {
9303 btrfs_free_path(path);
9304 return -ENOMEM;
9305 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009306
Chris Masonb9447ef2009-03-09 11:45:38 -04009307 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009308 parent_level = btrfs_header_level(parent);
9309 extent_buffer_get(parent);
9310 path->nodes[parent_level] = parent;
9311 path->slots[parent_level] = btrfs_header_nritems(parent);
9312
Chris Masonb9447ef2009-03-09 11:45:38 -04009313 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009314 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009315 path->nodes[level] = node;
9316 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009317 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009318
9319 wc->refs[parent_level] = 1;
9320 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
9321 wc->level = level;
9322 wc->shared_level = -1;
9323 wc->stage = DROP_REFERENCE;
9324 wc->update_ref = 0;
9325 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009326 wc->for_reloc = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009327 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009328
9329 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009330 wret = walk_down_tree(trans, root, path, wc);
9331 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04009332 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009333 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009334 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009335
Yan Zheng2c47e6052009-06-27 21:07:35 -04009336 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009337 if (wret < 0)
9338 ret = wret;
9339 if (wret != 0)
9340 break;
9341 }
9342
Yan Zheng2c47e6052009-06-27 21:07:35 -04009343 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009344 btrfs_free_path(path);
9345 return ret;
9346}
9347
Jeff Mahoney6202df62016-06-22 18:54:22 -04009348static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04009349{
9350 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009351 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04009352
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009353 /*
9354 * if restripe for this chunk_type is on pick target profile and
9355 * return, otherwise do the usual balance
9356 */
Jeff Mahoney6202df62016-06-22 18:54:22 -04009357 stripped = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009358 if (stripped)
9359 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02009360
Jeff Mahoney6202df62016-06-22 18:54:22 -04009361 num_devices = fs_info->fs_devices->rw_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05009362
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009363 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05009364 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009365 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
9366
Chris Masonec44a352008-04-28 15:29:52 -04009367 if (num_devices == 1) {
9368 stripped |= BTRFS_BLOCK_GROUP_DUP;
9369 stripped = flags & ~stripped;
9370
9371 /* turn raid0 into single device chunks */
9372 if (flags & BTRFS_BLOCK_GROUP_RAID0)
9373 return stripped;
9374
9375 /* turn mirroring into duplication */
9376 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
9377 BTRFS_BLOCK_GROUP_RAID10))
9378 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04009379 } else {
9380 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04009381 if (flags & stripped)
9382 return flags;
9383
9384 stripped |= BTRFS_BLOCK_GROUP_DUP;
9385 stripped = flags & ~stripped;
9386
9387 /* switch duplicated blocks with raid1 */
9388 if (flags & BTRFS_BLOCK_GROUP_DUP)
9389 return stripped | BTRFS_BLOCK_GROUP_RAID1;
9390
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009391 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04009392 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009393
Chris Masonec44a352008-04-28 15:29:52 -04009394 return flags;
9395}
9396
Zhaolei868f4012015-08-05 16:43:27 +08009397static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04009398{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009399 struct btrfs_space_info *sinfo = cache->space_info;
9400 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00009401 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009402 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04009403
Miao Xie199c36e2011-07-15 10:34:36 +00009404 /*
9405 * We need some metadata space and system metadata space for
9406 * allocating chunks in some corner cases until we force to set
9407 * it to be readonly.
9408 */
9409 if ((sinfo->flags &
9410 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
9411 !force)
Byongho Leeee221842015-12-15 01:42:10 +09009412 min_allocable_bytes = SZ_1M;
Miao Xie199c36e2011-07-15 10:34:36 +00009413 else
9414 min_allocable_bytes = 0;
9415
Yan, Zhengf0486c62010-05-16 10:46:25 -04009416 spin_lock(&sinfo->lock);
9417 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00009418
9419 if (cache->ro) {
Zhaolei868f4012015-08-05 16:43:27 +08009420 cache->ro++;
WuBo61cfea92011-07-26 03:30:11 +00009421 ret = 0;
9422 goto out;
9423 }
9424
Yan, Zhengf0486c62010-05-16 10:46:25 -04009425 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
9426 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04009427
Liu Bo41361352017-02-13 15:42:21 -08009428 if (btrfs_space_info_used(sinfo, true) + num_bytes +
Josef Bacik37be25b2011-08-05 10:25:38 -04009429 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04009430 sinfo->bytes_readonly += num_bytes;
Zhaolei868f4012015-08-05 16:43:27 +08009431 cache->ro++;
Josef Bacik633c0aa2014-10-31 09:49:34 -04009432 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009433 ret = 0;
9434 }
WuBo61cfea92011-07-26 03:30:11 +00009435out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04009436 spin_unlock(&cache->lock);
9437 spin_unlock(&sinfo->lock);
9438 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04009439}
9440
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009441int btrfs_inc_block_group_ro(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04009442 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009443
9444{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009445 struct btrfs_trans_handle *trans;
9446 u64 alloc_flags;
9447 int ret;
9448
Chris Mason1bbc6212015-04-06 12:46:08 -07009449again:
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009450 trans = btrfs_join_transaction(fs_info->extent_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009451 if (IS_ERR(trans))
9452 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009453
Chris Mason1bbc6212015-04-06 12:46:08 -07009454 /*
9455 * we're not allowed to set block groups readonly after the dirty
9456 * block groups cache has started writing. If it already started,
9457 * back off and let this transaction commit
9458 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009459 mutex_lock(&fs_info->ro_block_group_mutex);
Josef Bacik3204d332015-09-24 10:46:10 -04009460 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
Chris Mason1bbc6212015-04-06 12:46:08 -07009461 u64 transid = trans->transid;
9462
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009463 mutex_unlock(&fs_info->ro_block_group_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009464 btrfs_end_transaction(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07009465
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009466 ret = btrfs_wait_for_commit(fs_info, transid);
Chris Mason1bbc6212015-04-06 12:46:08 -07009467 if (ret)
9468 return ret;
9469 goto again;
9470 }
9471
Chris Mason153c35b2015-05-19 18:54:41 -07009472 /*
9473 * if we are changing raid levels, try to allocate a corresponding
9474 * block group with the new raid level.
9475 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009476 alloc_flags = update_block_group_flags(fs_info, cache->flags);
Chris Mason153c35b2015-05-19 18:54:41 -07009477 if (alloc_flags != cache->flags) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009478 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason153c35b2015-05-19 18:54:41 -07009479 CHUNK_ALLOC_FORCE);
9480 /*
9481 * ENOSPC is allowed here, we may have enough space
9482 * already allocated at the new raid level to
9483 * carry on
9484 */
9485 if (ret == -ENOSPC)
9486 ret = 0;
9487 if (ret < 0)
9488 goto out;
9489 }
Chris Mason1bbc6212015-04-06 12:46:08 -07009490
Zhaolei868f4012015-08-05 16:43:27 +08009491 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009492 if (!ret)
9493 goto out;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009494 alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags);
9495 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04009496 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009497 if (ret < 0)
9498 goto out;
Zhaolei868f4012015-08-05 16:43:27 +08009499 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009500out:
Shaohua Li2f081082015-01-09 10:40:15 -08009501 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009502 alloc_flags = update_block_group_flags(fs_info, cache->flags);
David Sterba34441362016-10-04 19:34:27 +02009503 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009504 check_system_chunk(trans, fs_info, alloc_flags);
David Sterba34441362016-10-04 19:34:27 +02009505 mutex_unlock(&fs_info->chunk_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009506 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009507 mutex_unlock(&fs_info->ro_block_group_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009508
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009509 btrfs_end_transaction(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009510 return ret;
9511}
9512
Chris Masonc87f08c2011-02-16 13:57:04 -05009513int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009514 struct btrfs_fs_info *fs_info, u64 type)
Chris Masonc87f08c2011-02-16 13:57:04 -05009515{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009516 u64 alloc_flags = get_alloc_profile(fs_info, type);
9517
9518 return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05009519}
9520
Miao Xie6d07bce2011-01-05 10:07:31 +00009521/*
9522 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04009523 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00009524 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009525u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00009526{
9527 struct btrfs_block_group_cache *block_group;
9528 u64 free_bytes = 0;
9529 int factor;
9530
Nicholas D Steeves01327612016-05-19 21:18:45 -04009531 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009532 if (list_empty(&sinfo->ro_bgs))
9533 return 0;
9534
9535 spin_lock(&sinfo->lock);
9536 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00009537 spin_lock(&block_group->lock);
9538
9539 if (!block_group->ro) {
9540 spin_unlock(&block_group->lock);
9541 continue;
9542 }
9543
9544 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
9545 BTRFS_BLOCK_GROUP_RAID10 |
9546 BTRFS_BLOCK_GROUP_DUP))
9547 factor = 2;
9548 else
9549 factor = 1;
9550
9551 free_bytes += (block_group->key.offset -
9552 btrfs_block_group_used(&block_group->item)) *
9553 factor;
9554
9555 spin_unlock(&block_group->lock);
9556 }
Miao Xie6d07bce2011-01-05 10:07:31 +00009557 spin_unlock(&sinfo->lock);
9558
9559 return free_bytes;
9560}
9561
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009562void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache)
Yan, Zhengf0486c62010-05-16 10:46:25 -04009563{
9564 struct btrfs_space_info *sinfo = cache->space_info;
9565 u64 num_bytes;
9566
9567 BUG_ON(!cache->ro);
9568
9569 spin_lock(&sinfo->lock);
9570 spin_lock(&cache->lock);
Zhaolei868f4012015-08-05 16:43:27 +08009571 if (!--cache->ro) {
9572 num_bytes = cache->key.offset - cache->reserved -
9573 cache->pinned - cache->bytes_super -
9574 btrfs_block_group_used(&cache->item);
9575 sinfo->bytes_readonly -= num_bytes;
9576 list_del_init(&cache->ro_list);
9577 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009578 spin_unlock(&cache->lock);
9579 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009580}
9581
Josef Bacikba1bf482009-09-11 16:11:19 -04009582/*
9583 * checks to see if its even possible to relocate this block group.
9584 *
9585 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9586 * ok to go ahead and try.
9587 */
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009588int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04009589{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009590 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04009591 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04009592 struct btrfs_space_info *space_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009593 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacikba1bf482009-09-11 16:11:19 -04009594 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04009595 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00009596 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04009597 u64 dev_min = 1;
9598 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009599 u64 target;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009600 int debug;
liubocdcb7252011-08-03 10:15:25 +00009601 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04009602 int full = 0;
9603 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05009604
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009605 debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG);
Qu Wenruo0305bc22016-03-23 11:38:17 +08009606
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009607 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04009608
Josef Bacikba1bf482009-09-11 16:11:19 -04009609 /* odd, couldn't find the block group, leave it alone */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009610 if (!block_group) {
9611 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009612 btrfs_warn(fs_info,
Qu Wenruo0305bc22016-03-23 11:38:17 +08009613 "can't find block group for bytenr %llu",
9614 bytenr);
Josef Bacikba1bf482009-09-11 16:11:19 -04009615 return -1;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009616 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009617
liubocdcb7252011-08-03 10:15:25 +00009618 min_free = btrfs_block_group_used(&block_group->item);
9619
Josef Bacikba1bf482009-09-11 16:11:19 -04009620 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00009621 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04009622 goto out;
Chris Mason323da792008-05-09 11:46:48 -04009623
Josef Bacikba1bf482009-09-11 16:11:19 -04009624 space_info = block_group->space_info;
9625 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04009626
Josef Bacikba1bf482009-09-11 16:11:19 -04009627 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04009628
Josef Bacikba1bf482009-09-11 16:11:19 -04009629 /*
9630 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04009631 * relocate it unless we're able to allocate a new chunk below.
9632 *
9633 * Otherwise, we need to make sure we have room in the space to handle
9634 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04009635 */
Chris Mason7ce618d2009-09-22 14:48:44 -04009636 if ((space_info->total_bytes != block_group->key.offset) &&
Liu Bo41361352017-02-13 15:42:21 -08009637 (btrfs_space_info_used(space_info, false) + min_free <
9638 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04009639 spin_unlock(&space_info->lock);
9640 goto out;
9641 }
9642 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009643
Josef Bacikba1bf482009-09-11 16:11:19 -04009644 /*
9645 * ok we don't have enough space, but maybe we have free space on our
9646 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009647 * alloc devices and guess if we have enough space. if this block
9648 * group is going to be restriped, run checks against the target
9649 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04009650 */
9651 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05009652
liubocdcb7252011-08-03 10:15:25 +00009653 /*
9654 * index:
9655 * 0: raid10
9656 * 1: raid1
9657 * 2: dup
9658 * 3: raid0
9659 * 4: single
9660 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009661 target = get_restripe_target(fs_info, block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009662 if (target) {
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009663 index = btrfs_bg_flags_to_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009664 } else {
9665 /*
9666 * this is just a balance, so if we were marked as full
9667 * we know there is no space for a new chunk
9668 */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009669 if (full) {
9670 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009671 btrfs_warn(fs_info,
9672 "no space to alloc new chunk for block group %llu",
9673 block_group->key.objectid);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009674 goto out;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009675 }
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009676
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009677 index = btrfs_bg_flags_to_raid_index(block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009678 }
9679
Miao Xiee6ec7162013-01-17 05:38:51 +00009680 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00009681 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04009682 /* Divide by 2 */
9683 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009684 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00009685 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00009686 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04009687 /* Multiply by 2 */
9688 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009689 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00009690 dev_min = fs_devices->rw_devices;
David Sterba47c57132015-02-20 18:43:47 +01009691 min_free = div64_u64(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00009692 }
9693
Josef Bacik6df9a952013-06-27 13:22:46 -04009694 /* We need to do this so that we can look at pending chunks */
9695 trans = btrfs_join_transaction(root);
9696 if (IS_ERR(trans)) {
9697 ret = PTR_ERR(trans);
9698 goto out;
9699 }
9700
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009701 mutex_lock(&fs_info->chunk_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04009702 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00009703 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04009704
Josef Bacikba1bf482009-09-11 16:11:19 -04009705 /*
9706 * check to make sure we can actually find a chunk with enough
9707 * space to fit our block group in.
9708 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01009709 if (device->total_bytes > device->bytes_used + min_free &&
Anand Jain401e29c2017-12-04 12:54:55 +08009710 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Josef Bacik6df9a952013-06-27 13:22:46 -04009711 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00009712 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04009713 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00009714 dev_nr++;
9715
9716 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05009717 break;
liubocdcb7252011-08-03 10:15:25 +00009718
Josef Bacikba1bf482009-09-11 16:11:19 -04009719 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05009720 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009721 }
Qu Wenruo0305bc22016-03-23 11:38:17 +08009722 if (debug && ret == -1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009723 btrfs_warn(fs_info,
9724 "no space to allocate a new chunk for block group %llu",
9725 block_group->key.objectid);
9726 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009727 btrfs_end_transaction(trans);
Chris Masonedbd8d42007-12-21 16:27:24 -05009728out:
Josef Bacikba1bf482009-09-11 16:11:19 -04009729 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05009730 return ret;
9731}
9732
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009733static int find_first_block_group(struct btrfs_fs_info *fs_info,
9734 struct btrfs_path *path,
9735 struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04009736{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009737 struct btrfs_root *root = fs_info->extent_root;
Chris Mason925baed2008-06-25 16:01:30 -04009738 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009739 struct btrfs_key found_key;
9740 struct extent_buffer *leaf;
9741 int slot;
9742
9743 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
9744 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009745 goto out;
9746
Chris Masond3977122009-01-05 21:25:51 -05009747 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009748 slot = path->slots[0];
9749 leaf = path->nodes[0];
9750 if (slot >= btrfs_header_nritems(leaf)) {
9751 ret = btrfs_next_leaf(root, path);
9752 if (ret == 0)
9753 continue;
9754 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009755 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04009756 break;
9757 }
9758 btrfs_item_key_to_cpu(leaf, &found_key, slot);
9759
9760 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04009761 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
Liu Bo6fb37b72016-06-22 18:31:27 -07009762 struct extent_map_tree *em_tree;
9763 struct extent_map *em;
9764
9765 em_tree = &root->fs_info->mapping_tree.map_tree;
9766 read_lock(&em_tree->lock);
9767 em = lookup_extent_mapping(em_tree, found_key.objectid,
9768 found_key.offset);
9769 read_unlock(&em_tree->lock);
9770 if (!em) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009771 btrfs_err(fs_info,
Liu Bo6fb37b72016-06-22 18:31:27 -07009772 "logical %llu len %llu found bg but no related chunk",
9773 found_key.objectid, found_key.offset);
9774 ret = -ENOENT;
9775 } else {
9776 ret = 0;
9777 }
Josef Bacik187ee582016-08-18 15:30:06 -04009778 free_extent_map(em);
Chris Mason925baed2008-06-25 16:01:30 -04009779 goto out;
9780 }
Chris Mason0b86a832008-03-24 15:01:56 -04009781 path->slots[0]++;
9782 }
Chris Mason925baed2008-06-25 16:01:30 -04009783out:
Chris Mason0b86a832008-03-24 15:01:56 -04009784 return ret;
9785}
9786
Josef Bacik0af3d002010-06-21 14:48:16 -04009787void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9788{
9789 struct btrfs_block_group_cache *block_group;
9790 u64 last = 0;
9791
9792 while (1) {
9793 struct inode *inode;
9794
9795 block_group = btrfs_lookup_first_block_group(info, last);
9796 while (block_group) {
9797 spin_lock(&block_group->lock);
9798 if (block_group->iref)
9799 break;
9800 spin_unlock(&block_group->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009801 block_group = next_block_group(info, block_group);
Josef Bacik0af3d002010-06-21 14:48:16 -04009802 }
9803 if (!block_group) {
9804 if (last == 0)
9805 break;
9806 last = 0;
9807 continue;
9808 }
9809
9810 inode = block_group->inode;
9811 block_group->iref = 0;
9812 block_group->inode = NULL;
9813 spin_unlock(&block_group->lock);
Liu Bof3bca802016-07-20 17:33:44 -07009814 ASSERT(block_group->io_ctl.inode == NULL);
Josef Bacik0af3d002010-06-21 14:48:16 -04009815 iput(inode);
9816 last = block_group->key.objectid + block_group->key.offset;
9817 btrfs_put_block_group(block_group);
9818 }
9819}
9820
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009821/*
9822 * Must be called only after stopping all workers, since we could have block
9823 * group caching kthreads running, and therefore they could race with us if we
9824 * freed the block groups before stopping them.
9825 */
Zheng Yan1a40e232008-09-26 10:09:34 -04009826int btrfs_free_block_groups(struct btrfs_fs_info *info)
9827{
9828 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04009829 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04009830 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04009831 struct rb_node *n;
9832
Josef Bacik9e351cc2014-03-13 15:42:13 -04009833 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009834 while (!list_empty(&info->caching_block_groups)) {
9835 caching_ctl = list_entry(info->caching_block_groups.next,
9836 struct btrfs_caching_control, list);
9837 list_del(&caching_ctl->list);
9838 put_caching_control(caching_ctl);
9839 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04009840 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009841
Josef Bacik47ab2a62014-09-18 11:20:02 -04009842 spin_lock(&info->unused_bgs_lock);
9843 while (!list_empty(&info->unused_bgs)) {
9844 block_group = list_first_entry(&info->unused_bgs,
9845 struct btrfs_block_group_cache,
9846 bg_list);
9847 list_del_init(&block_group->bg_list);
9848 btrfs_put_block_group(block_group);
9849 }
9850 spin_unlock(&info->unused_bgs_lock);
9851
Zheng Yan1a40e232008-09-26 10:09:34 -04009852 spin_lock(&info->block_group_cache_lock);
9853 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
9854 block_group = rb_entry(n, struct btrfs_block_group_cache,
9855 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04009856 rb_erase(&block_group->cache_node,
9857 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009858 RB_CLEAR_NODE(&block_group->cache_node);
Yan Zhengd899e052008-10-30 14:25:28 -04009859 spin_unlock(&info->block_group_cache_lock);
9860
Josef Bacik80eb2342008-10-29 14:49:05 -04009861 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04009862 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04009863 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05009864
Josef Bacik3c148742011-02-02 15:53:47 +00009865 /*
9866 * We haven't cached this block group, which means we could
9867 * possibly have excluded extents on this block group.
9868 */
Josef Bacik36cce922013-08-05 11:15:21 -04009869 if (block_group->cached == BTRFS_CACHE_NO ||
9870 block_group->cached == BTRFS_CACHE_ERROR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009871 free_excluded_extents(info, block_group);
Josef Bacik3c148742011-02-02 15:53:47 +00009872
Josef Bacik817d52f2009-07-13 21:29:25 -04009873 btrfs_remove_free_space_cache(block_group);
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009874 ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
Liu Bof3bca802016-07-20 17:33:44 -07009875 ASSERT(list_empty(&block_group->dirty_list));
9876 ASSERT(list_empty(&block_group->io_list));
9877 ASSERT(list_empty(&block_group->bg_list));
9878 ASSERT(atomic_read(&block_group->count) == 1);
Josef Bacik11dfe352009-11-13 20:12:59 +00009879 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04009880
9881 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009882 }
9883 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04009884
9885 /* now that all the block groups are freed, go through and
9886 * free all the space_info structs. This is only called during
9887 * the final stages of unmount, and so we know nobody is
9888 * using them. We call synchronize_rcu() once before we start,
9889 * just to be on the safe side.
9890 */
9891 synchronize_rcu();
9892
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04009893 release_global_block_rsv(info);
9894
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309895 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009896 int i;
9897
Chris Mason4184ea72009-03-10 12:39:20 -04009898 space_info = list_entry(info->space_info.next,
9899 struct btrfs_space_info,
9900 list);
Josef Bacikd555b6c2016-03-25 13:25:51 -04009901
9902 /*
9903 * Do not hide this behind enospc_debug, this is actually
9904 * important and indicates a real bug if this happens.
9905 */
9906 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00009907 space_info->bytes_reserved > 0 ||
Josef Bacikd555b6c2016-03-25 13:25:51 -04009908 space_info->bytes_may_use > 0))
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009909 dump_space_info(info, space_info, 0, 0);
Chris Mason4184ea72009-03-10 12:39:20 -04009910 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009911 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
9912 struct kobject *kobj;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009913 kobj = space_info->block_group_kobjs[i];
9914 space_info->block_group_kobjs[i] = NULL;
9915 if (kobj) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009916 kobject_del(kobj);
9917 kobject_put(kobj);
9918 }
9919 }
9920 kobject_del(&space_info->kobj);
9921 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04009922 }
Zheng Yan1a40e232008-09-26 10:09:34 -04009923 return 0;
9924}
9925
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009926/* link_block_group will queue up kobjects to add when we're reclaim-safe */
9927void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info)
9928{
9929 struct btrfs_space_info *space_info;
9930 struct raid_kobject *rkobj;
9931 LIST_HEAD(list);
9932 int index;
9933 int ret = 0;
9934
9935 spin_lock(&fs_info->pending_raid_kobjs_lock);
9936 list_splice_init(&fs_info->pending_raid_kobjs, &list);
9937 spin_unlock(&fs_info->pending_raid_kobjs_lock);
9938
9939 list_for_each_entry(rkobj, &list, list) {
9940 space_info = __find_space_info(fs_info, rkobj->flags);
9941 index = btrfs_bg_flags_to_raid_index(rkobj->flags);
9942
9943 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
9944 "%s", get_raid_name(index));
9945 if (ret) {
9946 kobject_put(&rkobj->kobj);
9947 break;
9948 }
9949 }
9950 if (ret)
9951 btrfs_warn(fs_info,
9952 "failed to add kobject for block cache, ignoring");
9953}
9954
Nikolay Borisovc434d212017-08-21 12:43:50 +03009955static void link_block_group(struct btrfs_block_group_cache *cache)
Yan, Zhengb742bb82010-05-16 10:46:24 -04009956{
Nikolay Borisovc434d212017-08-21 12:43:50 +03009957 struct btrfs_space_info *space_info = cache->space_info;
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009958 struct btrfs_fs_info *fs_info = cache->fs_info;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009959 int index = btrfs_bg_flags_to_raid_index(cache->flags);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009960 bool first = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04009961
9962 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009963 if (list_empty(&space_info->block_groups[index]))
9964 first = true;
9965 list_add_tail(&cache->list, &space_info->block_groups[index]);
9966 up_write(&space_info->groups_sem);
9967
9968 if (first) {
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009969 struct raid_kobject *rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
9970 if (!rkobj) {
9971 btrfs_warn(cache->fs_info,
9972 "couldn't alloc memory for raid level kobject");
9973 return;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009974 }
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009975 rkobj->flags = cache->flags;
9976 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
9977
9978 spin_lock(&fs_info->pending_raid_kobjs_lock);
9979 list_add_tail(&rkobj->list, &fs_info->pending_raid_kobjs);
9980 spin_unlock(&fs_info->pending_raid_kobjs_lock);
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009981 space_info->block_group_kobjs[index] = &rkobj->kobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009982 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04009983}
9984
Miao Xie920e4a52014-01-15 20:00:55 +08009985static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009986btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info,
9987 u64 start, u64 size)
Miao Xie920e4a52014-01-15 20:00:55 +08009988{
9989 struct btrfs_block_group_cache *cache;
9990
9991 cache = kzalloc(sizeof(*cache), GFP_NOFS);
9992 if (!cache)
9993 return NULL;
9994
9995 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
9996 GFP_NOFS);
9997 if (!cache->free_space_ctl) {
9998 kfree(cache);
9999 return NULL;
10000 }
10001
10002 cache->key.objectid = start;
10003 cache->key.offset = size;
10004 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
10005
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010006 cache->fs_info = fs_info;
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +030010007 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010008 set_free_space_tree_thresholds(cache);
10009
Miao Xie920e4a52014-01-15 20:00:55 +080010010 atomic_set(&cache->count, 1);
10011 spin_lock_init(&cache->lock);
Miao Xiee570fd22014-06-19 10:42:50 +080010012 init_rwsem(&cache->data_rwsem);
Miao Xie920e4a52014-01-15 20:00:55 +080010013 INIT_LIST_HEAD(&cache->list);
10014 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010015 INIT_LIST_HEAD(&cache->bg_list);
Josef Bacik633c0aa2014-10-31 09:49:34 -040010016 INIT_LIST_HEAD(&cache->ro_list);
Josef Bacikce93ec52014-11-17 15:45:48 -050010017 INIT_LIST_HEAD(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -070010018 INIT_LIST_HEAD(&cache->io_list);
Miao Xie920e4a52014-01-15 20:00:55 +080010019 btrfs_init_free_space_ctl(cache);
Filipe Manana04216822014-11-27 21:14:15 +000010020 atomic_set(&cache->trimming, 0);
Omar Sandovala5ed9182015-09-29 20:50:35 -070010021 mutex_init(&cache->free_space_lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +080010022 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
Miao Xie920e4a52014-01-15 20:00:55 +080010023
10024 return cache;
10025}
10026
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010027int btrfs_read_block_groups(struct btrfs_fs_info *info)
Chris Mason9078a3e2007-04-26 16:46:15 -040010028{
10029 struct btrfs_path *path;
10030 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010031 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -040010032 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -040010033 struct btrfs_key key;
10034 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -040010035 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -040010036 int need_clear = 0;
10037 u64 cache_gen;
Liu Bo49303382016-08-25 18:08:27 -070010038 u64 feature;
10039 int mixed;
10040
10041 feature = btrfs_super_incompat_flags(info->super_copy);
10042 mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS);
Chris Mason96b51792007-10-15 16:15:19 -040010043
Chris Mason9078a3e2007-04-26 16:46:15 -040010044 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -040010045 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010046 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Chris Mason9078a3e2007-04-26 16:46:15 -040010047 path = btrfs_alloc_path();
10048 if (!path)
10049 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +010010050 path->reada = READA_FORWARD;
Chris Mason9078a3e2007-04-26 16:46:15 -040010051
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010052 cache_gen = btrfs_super_cache_generation(info->super_copy);
10053 if (btrfs_test_opt(info, SPACE_CACHE) &&
10054 btrfs_super_generation(info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -040010055 need_clear = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010056 if (btrfs_test_opt(info, CLEAR_CACHE))
Josef Bacik88c2ba32010-09-21 14:21:34 -040010057 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -040010058
Chris Masond3977122009-01-05 21:25:51 -050010059 while (1) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010060 ret = find_first_block_group(info, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -040010061 if (ret > 0)
10062 break;
Chris Mason0b86a832008-03-24 15:01:56 -040010063 if (ret != 0)
10064 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +080010065
Chris Mason5f39d392007-10-15 16:14:19 -040010066 leaf = path->nodes[0];
10067 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +080010068
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010069 cache = btrfs_create_block_group_cache(info, found_key.objectid,
Miao Xie920e4a52014-01-15 20:00:55 +080010070 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -040010071 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -040010072 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -040010073 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -040010074 }
Josef Bacik96303082009-07-13 21:29:25 -040010075
Liu Bocf7c1ef2012-07-06 03:31:34 -060010076 if (need_clear) {
10077 /*
10078 * When we mount with old space cache, we need to
10079 * set BTRFS_DC_CLEAR and set dirty flag.
10080 *
10081 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
10082 * truncate the old free space cache inode and
10083 * setup a new one.
10084 * b) Setting 'dirty flag' makes sure that we flush
10085 * the new space cache info onto disk.
10086 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010087 if (btrfs_test_opt(info, SPACE_CACHE))
Josef Bacikce93ec52014-11-17 15:45:48 -050010088 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -060010089 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010090
Chris Mason5f39d392007-10-15 16:14:19 -040010091 read_extent_buffer(leaf, &cache->item,
10092 btrfs_item_ptr_offset(leaf, path->slots[0]),
10093 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +080010094 cache->flags = btrfs_block_group_flags(&cache->item);
Liu Bo49303382016-08-25 18:08:27 -070010095 if (!mixed &&
10096 ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
10097 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
10098 btrfs_err(info,
10099"bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
10100 cache->key.objectid);
10101 ret = -EINVAL;
10102 goto error;
10103 }
Chris Mason0b86a832008-03-24 15:01:56 -040010104
Chris Mason9078a3e2007-04-26 16:46:15 -040010105 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +020010106 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +080010107
Josef Bacik817d52f2009-07-13 21:29:25 -040010108 /*
Josef Bacik3c148742011-02-02 15:53:47 +000010109 * We need to exclude the super stripes now so that the space
10110 * info has super bytes accounted for, otherwise we'll think
10111 * we have more space than we actually do.
10112 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010113 ret = exclude_super_stripes(info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010114 if (ret) {
10115 /*
10116 * We may have excluded something, so call this just in
10117 * case.
10118 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010119 free_excluded_extents(info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010120 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010121 goto error;
10122 }
Josef Bacik3c148742011-02-02 15:53:47 +000010123
10124 /*
Josef Bacik817d52f2009-07-13 21:29:25 -040010125 * check for two cases, either we are full, and therefore
10126 * don't need to bother with the caching work since we won't
10127 * find any space, or we are empty, and we can just add all
10128 * the space in and be done with it. This saves us _alot_ of
10129 * time, particularly in the full case.
10130 */
10131 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -040010132 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010133 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010134 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010135 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -040010136 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010137 cache->cached = BTRFS_CACHE_FINISHED;
Nikolay Borisov4457c1c2018-05-10 15:44:45 +030010138 add_new_free_space(cache, found_key.objectid,
Josef Bacik817d52f2009-07-13 21:29:25 -040010139 found_key.objectid +
10140 found_key.offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010141 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010142 }
Chris Mason96b51792007-10-15 16:15:19 -040010143
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010144 ret = btrfs_add_block_group_cache(info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010145 if (ret) {
10146 btrfs_remove_free_space_cache(cache);
10147 btrfs_put_block_group(cache);
10148 goto error;
10149 }
10150
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010151 trace_btrfs_add_block_group(info, cache, 0);
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +030010152 update_space_info(info, cache->flags, found_key.offset,
10153 btrfs_block_group_used(&cache->item),
10154 cache->bytes_super, &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010155
Chris Mason6324fbf2008-03-24 15:01:59 -040010156 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -040010157
Nikolay Borisovc434d212017-08-21 12:43:50 +030010158 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010159
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010160 set_avail_alloc_bits(info, cache->flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010161 if (btrfs_chunk_readonly(info, cache->key.objectid)) {
Zhaolei868f4012015-08-05 16:43:27 +080010162 inc_block_group_ro(cache, 1);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010163 } else if (btrfs_block_group_used(&cache->item) == 0) {
10164 spin_lock(&info->unused_bgs_lock);
10165 /* Should always be true but just in case. */
10166 if (list_empty(&cache->bg_list)) {
10167 btrfs_get_block_group(cache);
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +080010168 trace_btrfs_add_unused_block_group(cache);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010169 list_add_tail(&cache->bg_list,
10170 &info->unused_bgs);
10171 }
10172 spin_unlock(&info->unused_bgs_lock);
10173 }
Chris Mason9078a3e2007-04-26 16:46:15 -040010174 }
Yan, Zhengb742bb82010-05-16 10:46:24 -040010175
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010176 list_for_each_entry_rcu(space_info, &info->space_info, list) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010177 if (!(get_alloc_profile(info, space_info->flags) &
Yan, Zhengb742bb82010-05-16 10:46:24 -040010178 (BTRFS_BLOCK_GROUP_RAID10 |
10179 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -050010180 BTRFS_BLOCK_GROUP_RAID5 |
10181 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -040010182 BTRFS_BLOCK_GROUP_DUP)))
10183 continue;
10184 /*
10185 * avoid allocating from un-mirrored block group if there are
10186 * mirrored block groups.
10187 */
chandan1095cc02013-07-16 12:28:56 +053010188 list_for_each_entry(cache,
10189 &space_info->block_groups[BTRFS_RAID_RAID0],
10190 list)
Zhaolei868f4012015-08-05 16:43:27 +080010191 inc_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +053010192 list_for_each_entry(cache,
10193 &space_info->block_groups[BTRFS_RAID_SINGLE],
10194 list)
Zhaolei868f4012015-08-05 16:43:27 +080010195 inc_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -040010196 }
Yan, Zhengf0486c62010-05-16 10:46:25 -040010197
Jeff Mahoney75cb3792018-03-20 15:25:26 -040010198 btrfs_add_raid_kobjects(info);
Yan, Zhengf0486c62010-05-16 10:46:25 -040010199 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -040010200 ret = 0;
10201error:
Chris Mason9078a3e2007-04-26 16:46:15 -040010202 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -040010203 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010204}
Chris Mason6324fbf2008-03-24 15:01:59 -040010205
Nikolay Borisov6c686b32018-02-07 17:55:40 +020010206void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
Josef Bacikea658ba2012-09-11 16:57:25 -040010207{
Nikolay Borisov6c686b32018-02-07 17:55:40 +020010208 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacikea658ba2012-09-11 16:57:25 -040010209 struct btrfs_block_group_cache *block_group, *tmp;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010210 struct btrfs_root *extent_root = fs_info->extent_root;
Josef Bacikea658ba2012-09-11 16:57:25 -040010211 struct btrfs_block_group_item item;
10212 struct btrfs_key key;
10213 int ret = 0;
Filipe Mananad9a05402015-10-03 13:13:13 +010010214 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010215
Filipe Mananad9a05402015-10-03 13:13:13 +010010216 trans->can_flush_pending_bgs = false;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010217 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
Josef Bacikea658ba2012-09-11 16:57:25 -040010218 if (ret)
Filipe Mananac92f6be2014-11-26 15:28:55 +000010219 goto next;
Josef Bacikea658ba2012-09-11 16:57:25 -040010220
10221 spin_lock(&block_group->lock);
10222 memcpy(&item, &block_group->item, sizeof(item));
10223 memcpy(&key, &block_group->key, sizeof(key));
10224 spin_unlock(&block_group->lock);
10225
10226 ret = btrfs_insert_item(trans, extent_root, &key, &item,
10227 sizeof(item));
10228 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010229 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010230 ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid,
10231 key.offset);
Josef Bacik6df9a952013-06-27 13:22:46 -040010232 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010233 btrfs_abort_transaction(trans, ret);
Nikolay Borisove4e07112018-05-10 15:44:41 +030010234 add_block_group_free_space(trans, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010235 /* already aborted the transaction if it failed. */
Filipe Mananac92f6be2014-11-26 15:28:55 +000010236next:
10237 list_del_init(&block_group->bg_list);
Josef Bacikea658ba2012-09-11 16:57:25 -040010238 }
Filipe Mananad9a05402015-10-03 13:13:13 +010010239 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010240}
10241
Nikolay Borisove7e02092018-06-20 15:48:55 +030010242int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used,
Nikolay Borisov01744842017-07-27 14:22:11 +030010243 u64 type, u64 chunk_offset, u64 size)
Chris Mason6324fbf2008-03-24 15:01:59 -040010244{
Nikolay Borisove7e02092018-06-20 15:48:55 +030010245 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -040010246 struct btrfs_block_group_cache *cache;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010247 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -040010248
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010249 btrfs_set_log_full_commit(fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -040010250
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010251 cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -040010252 if (!cache)
10253 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +080010254
Chris Mason6324fbf2008-03-24 15:01:59 -040010255 btrfs_set_block_group_used(&cache->item, bytes_used);
Nikolay Borisov01744842017-07-27 14:22:11 +030010256 btrfs_set_block_group_chunk_objectid(&cache->item,
10257 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Mason6324fbf2008-03-24 15:01:59 -040010258 btrfs_set_block_group_flags(&cache->item, type);
10259
Miao Xie920e4a52014-01-15 20:00:55 +080010260 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -040010261 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010262 cache->cached = BTRFS_CACHE_FINISHED;
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010263 cache->needs_free_space = 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010264 ret = exclude_super_stripes(fs_info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010265 if (ret) {
10266 /*
10267 * We may have excluded something, so call this just in
10268 * case.
10269 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010270 free_excluded_extents(fs_info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010271 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010272 return ret;
10273 }
Josef Bacik96303082009-07-13 21:29:25 -040010274
Nikolay Borisov4457c1c2018-05-10 15:44:45 +030010275 add_new_free_space(cache, chunk_offset, chunk_offset + size);
Josef Bacik817d52f2009-07-13 21:29:25 -040010276
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010277 free_excluded_extents(fs_info, cache);
Yan Zheng11833d62009-09-11 16:11:19 -040010278
Josef Bacikd0bd4562015-09-23 14:54:14 -040010279#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010280 if (btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -040010281 u64 new_bytes_used = size - bytes_used;
10282
10283 bytes_used += new_bytes_used >> 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010284 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -040010285 }
10286#endif
Filipe Manana2e6e5182015-05-12 00:28:11 +010010287 /*
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010288 * Ensure the corresponding space_info object is created and
10289 * assigned to our block group. We want our bg to be added to the rbtree
10290 * with its ->space_info set.
Filipe Manana2e6e5182015-05-12 00:28:11 +010010291 */
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010292 cache->space_info = __find_space_info(fs_info, cache->flags);
Jeff Mahoneydc2d3002018-03-20 15:25:25 -040010293 ASSERT(cache->space_info);
Filipe Manana2e6e5182015-05-12 00:28:11 +010010294
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010295 ret = btrfs_add_block_group_cache(fs_info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010296 if (ret) {
10297 btrfs_remove_free_space_cache(cache);
10298 btrfs_put_block_group(cache);
10299 return ret;
10300 }
10301
Filipe Manana2e6e5182015-05-12 00:28:11 +010010302 /*
10303 * Now that our block group has its ->space_info set and is inserted in
10304 * the rbtree, update the space info's counters.
10305 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010306 trace_btrfs_add_block_group(fs_info, cache, 1);
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +030010307 update_space_info(fs_info, cache->flags, size, bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -040010308 cache->bytes_super, &cache->space_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010309 update_global_block_rsv(fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -040010310
Nikolay Borisovc434d212017-08-21 12:43:50 +030010311 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010312
Josef Bacik47ab2a62014-09-18 11:20:02 -040010313 list_add_tail(&cache->bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -040010314
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010315 set_avail_alloc_bits(fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -040010316 return 0;
10317}
Zheng Yan1a40e232008-09-26 10:09:34 -040010318
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010319static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
10320{
Ilya Dryomov899c81e2012-03-27 17:09:16 +030010321 u64 extra_flags = chunk_to_extended(flags) &
10322 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010323
Miao Xiede98ced2013-01-29 10:13:12 +000010324 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010325 if (flags & BTRFS_BLOCK_GROUP_DATA)
10326 fs_info->avail_data_alloc_bits &= ~extra_flags;
10327 if (flags & BTRFS_BLOCK_GROUP_METADATA)
10328 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
10329 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
10330 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +000010331 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010332}
10333
Zheng Yan1a40e232008-09-26 10:09:34 -040010334int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
Nikolay Borisov5a98ec02018-06-20 15:48:56 +030010335 u64 group_start, struct extent_map *em)
Zheng Yan1a40e232008-09-26 10:09:34 -040010336{
Nikolay Borisov5a98ec02018-06-20 15:48:56 +030010337 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010338 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010339 struct btrfs_path *path;
10340 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -040010341 struct btrfs_free_cluster *cluster;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010342 struct btrfs_root *tree_root = fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010343 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -040010344 struct inode *inode;
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010345 struct kobject *kobj = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -040010346 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010347 int index;
Josef Bacik89a55892010-10-14 14:52:27 -040010348 int factor;
Filipe Manana4f69cb92014-11-26 15:28:51 +000010349 struct btrfs_caching_control *caching_ctl = NULL;
Filipe Manana04216822014-11-27 21:14:15 +000010350 bool remove_em;
Zheng Yan1a40e232008-09-26 10:09:34 -040010351
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010352 block_group = btrfs_lookup_block_group(fs_info, group_start);
Zheng Yan1a40e232008-09-26 10:09:34 -040010353 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -050010354 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -040010355
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +080010356 trace_btrfs_remove_block_group(block_group);
liubo9f7c43c2011-03-07 02:13:33 +000010357 /*
10358 * Free the reserved super bytes from this block group before
10359 * remove it.
10360 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010361 free_excluded_extents(fs_info, block_group);
Josef Bacikfd708b82017-09-29 15:43:50 -040010362 btrfs_free_ref_tree_range(fs_info, block_group->key.objectid,
10363 block_group->key.offset);
liubo9f7c43c2011-03-07 02:13:33 +000010364
Zheng Yan1a40e232008-09-26 10:09:34 -040010365 memcpy(&key, &block_group->key, sizeof(key));
Qu Wenruo3e72ee82018-01-30 18:20:45 +080010366 index = btrfs_bg_flags_to_raid_index(block_group->flags);
Josef Bacik89a55892010-10-14 14:52:27 -040010367 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
10368 BTRFS_BLOCK_GROUP_RAID1 |
10369 BTRFS_BLOCK_GROUP_RAID10))
10370 factor = 2;
10371 else
10372 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -040010373
Chris Mason44fb5512009-06-04 15:34:51 -040010374 /* make sure this block group isn't part of an allocation cluster */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010375 cluster = &fs_info->data_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010376 spin_lock(&cluster->refill_lock);
10377 btrfs_return_cluster_to_free_space(block_group, cluster);
10378 spin_unlock(&cluster->refill_lock);
10379
10380 /*
10381 * make sure this block group isn't part of a metadata
10382 * allocation cluster
10383 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010384 cluster = &fs_info->meta_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010385 spin_lock(&cluster->refill_lock);
10386 btrfs_return_cluster_to_free_space(block_group, cluster);
10387 spin_unlock(&cluster->refill_lock);
10388
Zheng Yan1a40e232008-09-26 10:09:34 -040010389 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010390 if (!path) {
10391 ret = -ENOMEM;
10392 goto out;
10393 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010394
Chris Mason1bbc6212015-04-06 12:46:08 -070010395 /*
10396 * get the inode first so any iput calls done for the io_list
10397 * aren't the final iput (no unlinks allowed now)
10398 */
Jeff Mahoney77ab86b2017-02-15 16:28:30 -050010399 inode = lookup_free_space_inode(fs_info, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010400
10401 mutex_lock(&trans->transaction->cache_write_mutex);
10402 /*
10403 * make sure our free spache cache IO is done before remove the
10404 * free space inode
10405 */
10406 spin_lock(&trans->transaction->dirty_bgs_lock);
10407 if (!list_empty(&block_group->io_list)) {
10408 list_del_init(&block_group->io_list);
10409
10410 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
10411
10412 spin_unlock(&trans->transaction->dirty_bgs_lock);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040010413 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010414 btrfs_put_block_group(block_group);
10415 spin_lock(&trans->transaction->dirty_bgs_lock);
10416 }
10417
10418 if (!list_empty(&block_group->dirty_list)) {
10419 list_del_init(&block_group->dirty_list);
10420 btrfs_put_block_group(block_group);
10421 }
10422 spin_unlock(&trans->transaction->dirty_bgs_lock);
10423 mutex_unlock(&trans->transaction->cache_write_mutex);
10424
Josef Bacik0af3d002010-06-21 14:48:16 -040010425 if (!IS_ERR(inode)) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010426 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010427 if (ret) {
10428 btrfs_add_delayed_iput(inode);
10429 goto out;
10430 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010431 clear_nlink(inode);
10432 /* One for the block groups ref */
10433 spin_lock(&block_group->lock);
10434 if (block_group->iref) {
10435 block_group->iref = 0;
10436 block_group->inode = NULL;
10437 spin_unlock(&block_group->lock);
10438 iput(inode);
10439 } else {
10440 spin_unlock(&block_group->lock);
10441 }
10442 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -040010443 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -040010444 }
10445
10446 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
10447 key.offset = block_group->key.objectid;
10448 key.type = 0;
10449
10450 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
10451 if (ret < 0)
10452 goto out;
10453 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +020010454 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010455 if (ret == 0) {
10456 ret = btrfs_del_item(trans, tree_root, path);
10457 if (ret)
10458 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +020010459 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010460 }
10461
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010462 spin_lock(&fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -040010463 rb_erase(&block_group->cache_node,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010464 &fs_info->block_group_cache_tree);
Filipe Manana292cbd52014-11-26 15:28:50 +000010465 RB_CLEAR_NODE(&block_group->cache_node);
Liu Boa1897fd2012-12-27 09:01:23 +000010466
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010467 if (fs_info->first_logical_byte == block_group->key.objectid)
10468 fs_info->first_logical_byte = (u64)-1;
10469 spin_unlock(&fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010470
Josef Bacik80eb2342008-10-29 14:49:05 -040010471 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -040010472 /*
10473 * we must use list_del_init so people can check to see if they
10474 * are still on the list after taking the semaphore
10475 */
10476 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010477 if (list_empty(&block_group->space_info->block_groups[index])) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010478 kobj = block_group->space_info->block_group_kobjs[index];
10479 block_group->space_info->block_group_kobjs[index] = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010480 clear_avail_alloc_bits(fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010481 }
Josef Bacik80eb2342008-10-29 14:49:05 -040010482 up_write(&block_group->space_info->groups_sem);
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010483 if (kobj) {
10484 kobject_del(kobj);
10485 kobject_put(kobj);
10486 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010487
Filipe Manana4f69cb92014-11-26 15:28:51 +000010488 if (block_group->has_caching_ctl)
10489 caching_ctl = get_caching_control(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -040010490 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -040010491 wait_block_group_cache_done(block_group);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010492 if (block_group->has_caching_ctl) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010493 down_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010494 if (!caching_ctl) {
10495 struct btrfs_caching_control *ctl;
10496
10497 list_for_each_entry(ctl,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010498 &fs_info->caching_block_groups, list)
Filipe Manana4f69cb92014-11-26 15:28:51 +000010499 if (ctl->block_group == block_group) {
10500 caching_ctl = ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +020010501 refcount_inc(&caching_ctl->count);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010502 break;
10503 }
10504 }
10505 if (caching_ctl)
10506 list_del_init(&caching_ctl->list);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010507 up_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010508 if (caching_ctl) {
10509 /* Once for the caching bgs list and once for us. */
10510 put_caching_control(caching_ctl);
10511 put_caching_control(caching_ctl);
10512 }
10513 }
Josef Bacik817d52f2009-07-13 21:29:25 -040010514
Josef Bacikce93ec52014-11-17 15:45:48 -050010515 spin_lock(&trans->transaction->dirty_bgs_lock);
10516 if (!list_empty(&block_group->dirty_list)) {
Chris Mason1bbc6212015-04-06 12:46:08 -070010517 WARN_ON(1);
10518 }
10519 if (!list_empty(&block_group->io_list)) {
10520 WARN_ON(1);
Josef Bacikce93ec52014-11-17 15:45:48 -050010521 }
10522 spin_unlock(&trans->transaction->dirty_bgs_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010523 btrfs_remove_free_space_cache(block_group);
10524
Yan Zhengc146afa2008-11-12 14:34:12 -050010525 spin_lock(&block_group->space_info->lock);
Filipe Manana75c68e92015-01-16 13:24:40 +000010526 list_del_init(&block_group->ro_list);
Zhao Lei18d018a2015-02-24 20:07:44 +080010527
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010528 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Zhao Lei18d018a2015-02-24 20:07:44 +080010529 WARN_ON(block_group->space_info->total_bytes
10530 < block_group->key.offset);
10531 WARN_ON(block_group->space_info->bytes_readonly
10532 < block_group->key.offset);
10533 WARN_ON(block_group->space_info->disk_total
10534 < block_group->key.offset * factor);
10535 }
Yan Zhengc146afa2008-11-12 14:34:12 -050010536 block_group->space_info->total_bytes -= block_group->key.offset;
10537 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -040010538 block_group->space_info->disk_total -= block_group->key.offset * factor;
Zhao Lei18d018a2015-02-24 20:07:44 +080010539
Yan Zhengc146afa2008-11-12 14:34:12 -050010540 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -040010541
Josef Bacik0af3d002010-06-21 14:48:16 -040010542 memcpy(&key, &block_group->key, sizeof(key));
10543
David Sterba34441362016-10-04 19:34:27 +020010544 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana495e64f2014-12-02 18:07:30 +000010545 if (!list_empty(&em->list)) {
10546 /* We're in the transaction->pending_chunks list. */
10547 free_extent_map(em);
10548 }
Filipe Manana04216822014-11-27 21:14:15 +000010549 spin_lock(&block_group->lock);
10550 block_group->removed = 1;
10551 /*
10552 * At this point trimming can't start on this block group, because we
10553 * removed the block group from the tree fs_info->block_group_cache_tree
10554 * so no one can't find it anymore and even if someone already got this
10555 * block group before we removed it from the rbtree, they have already
10556 * incremented block_group->trimming - if they didn't, they won't find
10557 * any free space entries because we already removed them all when we
10558 * called btrfs_remove_free_space_cache().
10559 *
10560 * And we must not remove the extent map from the fs_info->mapping_tree
10561 * to prevent the same logical address range and physical device space
10562 * ranges from being reused for a new block group. This is because our
10563 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
10564 * completely transactionless, so while it is trimming a range the
10565 * currently running transaction might finish and a new one start,
10566 * allowing for new block groups to be created that can reuse the same
10567 * physical device locations unless we take this special care.
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010568 *
10569 * There may also be an implicit trim operation if the file system
10570 * is mounted with -odiscard. The same protections must remain
10571 * in place until the extents have been discarded completely when
10572 * the transaction commit has completed.
Filipe Manana04216822014-11-27 21:14:15 +000010573 */
10574 remove_em = (atomic_read(&block_group->trimming) == 0);
10575 /*
10576 * Make sure a trimmer task always sees the em in the pinned_chunks list
10577 * if it sees block_group->removed == 1 (needs to lock block_group->lock
10578 * before checking block_group->removed).
10579 */
10580 if (!remove_em) {
10581 /*
10582 * Our em might be in trans->transaction->pending_chunks which
10583 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10584 * and so is the fs_info->pinned_chunks list.
10585 *
10586 * So at this point we must be holding the chunk_mutex to avoid
10587 * any races with chunk allocation (more specifically at
10588 * volumes.c:contains_pending_extent()), to ensure it always
10589 * sees the em, either in the pending_chunks list or in the
10590 * pinned_chunks list.
10591 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010592 list_move_tail(&em->list, &fs_info->pinned_chunks);
Filipe Manana04216822014-11-27 21:14:15 +000010593 }
10594 spin_unlock(&block_group->lock);
Filipe Manana04216822014-11-27 21:14:15 +000010595
10596 if (remove_em) {
10597 struct extent_map_tree *em_tree;
10598
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010599 em_tree = &fs_info->mapping_tree.map_tree;
Filipe Manana04216822014-11-27 21:14:15 +000010600 write_lock(&em_tree->lock);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010601 /*
10602 * The em might be in the pending_chunks list, so make sure the
10603 * chunk mutex is locked, since remove_extent_mapping() will
10604 * delete us from that list.
10605 */
Filipe Manana04216822014-11-27 21:14:15 +000010606 remove_extent_mapping(em_tree, em);
10607 write_unlock(&em_tree->lock);
10608 /* once for the tree */
10609 free_extent_map(em);
10610 }
10611
David Sterba34441362016-10-04 19:34:27 +020010612 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010613
Nikolay Borisovf3f72772018-05-10 15:44:46 +030010614 ret = remove_block_group_free_space(trans, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010615 if (ret)
10616 goto out;
10617
Chris Masonfa9c0d792009-04-03 09:47:43 -040010618 btrfs_put_block_group(block_group);
10619 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -040010620
10621 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
10622 if (ret > 0)
10623 ret = -EIO;
10624 if (ret < 0)
10625 goto out;
10626
10627 ret = btrfs_del_item(trans, root, path);
10628out:
10629 btrfs_free_path(path);
10630 return ret;
10631}
liuboacce9522011-01-06 19:30:25 +080010632
Filipe Manana8eab77f2015-11-13 23:57:16 +000010633struct btrfs_trans_handle *
Filipe Manana7fd01182015-11-13 23:57:17 +000010634btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info,
10635 const u64 chunk_offset)
Filipe Manana8eab77f2015-11-13 23:57:16 +000010636{
Filipe Manana7fd01182015-11-13 23:57:17 +000010637 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
10638 struct extent_map *em;
10639 struct map_lookup *map;
10640 unsigned int num_items;
10641
10642 read_lock(&em_tree->lock);
10643 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
10644 read_unlock(&em_tree->lock);
10645 ASSERT(em && em->start == chunk_offset);
10646
Filipe Manana8eab77f2015-11-13 23:57:16 +000010647 /*
Filipe Manana7fd01182015-11-13 23:57:17 +000010648 * We need to reserve 3 + N units from the metadata space info in order
10649 * to remove a block group (done at btrfs_remove_chunk() and at
10650 * btrfs_remove_block_group()), which are used for:
10651 *
Filipe Manana8eab77f2015-11-13 23:57:16 +000010652 * 1 unit for adding the free space inode's orphan (located in the tree
10653 * of tree roots).
Filipe Manana7fd01182015-11-13 23:57:17 +000010654 * 1 unit for deleting the block group item (located in the extent
10655 * tree).
10656 * 1 unit for deleting the free space item (located in tree of tree
10657 * roots).
10658 * N units for deleting N device extent items corresponding to each
10659 * stripe (located in the device tree).
10660 *
10661 * In order to remove a block group we also need to reserve units in the
10662 * system space info in order to update the chunk tree (update one or
10663 * more device items and remove one chunk item), but this is done at
10664 * btrfs_remove_chunk() through a call to check_system_chunk().
Filipe Manana8eab77f2015-11-13 23:57:16 +000010665 */
Jeff Mahoney95617d62015-06-03 10:55:48 -040010666 map = em->map_lookup;
Filipe Manana7fd01182015-11-13 23:57:17 +000010667 num_items = 3 + map->num_stripes;
10668 free_extent_map(em);
10669
Filipe Manana8eab77f2015-11-13 23:57:16 +000010670 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
Filipe Manana7fd01182015-11-13 23:57:17 +000010671 num_items, 1);
Filipe Manana8eab77f2015-11-13 23:57:16 +000010672}
10673
Josef Bacik47ab2a62014-09-18 11:20:02 -040010674/*
10675 * Process the unused_bgs list and remove any that don't have any allocated
10676 * space inside of them.
10677 */
10678void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10679{
10680 struct btrfs_block_group_cache *block_group;
10681 struct btrfs_space_info *space_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010682 struct btrfs_trans_handle *trans;
10683 int ret = 0;
10684
Josef Bacikafcdd122016-09-02 15:40:02 -040010685 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
Josef Bacik47ab2a62014-09-18 11:20:02 -040010686 return;
10687
10688 spin_lock(&fs_info->unused_bgs_lock);
10689 while (!list_empty(&fs_info->unused_bgs)) {
10690 u64 start, end;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010691 int trimming;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010692
10693 block_group = list_first_entry(&fs_info->unused_bgs,
10694 struct btrfs_block_group_cache,
10695 bg_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010696 list_del_init(&block_group->bg_list);
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010697
10698 space_info = block_group->space_info;
10699
Josef Bacik47ab2a62014-09-18 11:20:02 -040010700 if (ret || btrfs_mixed_space_info(space_info)) {
10701 btrfs_put_block_group(block_group);
10702 continue;
10703 }
10704 spin_unlock(&fs_info->unused_bgs_lock);
10705
Zhao Leid5f2e332015-10-08 18:46:44 +080010706 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +010010707
Josef Bacik47ab2a62014-09-18 11:20:02 -040010708 /* Don't want to race with allocators so take the groups_sem */
10709 down_write(&space_info->groups_sem);
10710 spin_lock(&block_group->lock);
10711 if (block_group->reserved ||
10712 btrfs_block_group_used(&block_group->item) ||
Chris Mason19c4d2f2016-10-10 13:43:31 -070010713 block_group->ro ||
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010714 list_is_singular(&block_group->list)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -040010715 /*
10716 * We want to bail if we made new allocations or have
10717 * outstanding allocations in this block group. We do
10718 * the ro check in case balance is currently acting on
10719 * this block group.
10720 */
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +080010721 trace_btrfs_skip_unused_block_group(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010722 spin_unlock(&block_group->lock);
10723 up_write(&space_info->groups_sem);
10724 goto next;
10725 }
10726 spin_unlock(&block_group->lock);
10727
10728 /* We don't want to force the issue, only flip if it's ok. */
Zhaolei868f4012015-08-05 16:43:27 +080010729 ret = inc_block_group_ro(block_group, 0);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010730 up_write(&space_info->groups_sem);
10731 if (ret < 0) {
10732 ret = 0;
10733 goto next;
10734 }
10735
10736 /*
10737 * Want to do this before we do anything else so we can recover
10738 * properly if we fail to join the transaction.
10739 */
Filipe Manana7fd01182015-11-13 23:57:17 +000010740 trans = btrfs_start_trans_remove_block_group(fs_info,
10741 block_group->key.objectid);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010742 if (IS_ERR(trans)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010743 btrfs_dec_block_group_ro(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010744 ret = PTR_ERR(trans);
10745 goto next;
10746 }
10747
10748 /*
10749 * We could have pending pinned extents for this block group,
10750 * just delete them, we don't care about them anymore.
10751 */
10752 start = block_group->key.objectid;
10753 end = start + block_group->key.offset - 1;
Filipe Mananad4b450c2015-01-29 19:18:25 +000010754 /*
10755 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10756 * btrfs_finish_extent_commit(). If we are at transaction N,
10757 * another task might be running finish_extent_commit() for the
10758 * previous transaction N - 1, and have seen a range belonging
10759 * to the block group in freed_extents[] before we were able to
10760 * clear the whole block group range from freed_extents[]. This
10761 * means that task can lookup for the block group after we
10762 * unpinned it from freed_extents[] and removed it, leading to
10763 * a BUG_ON() at btrfs_unpin_extent_range().
10764 */
10765 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana758eb512014-11-03 14:08:39 +000010766 ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
David Sterba91166212016-04-26 23:54:39 +020010767 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010768 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010769 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010770 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010771 goto end_trans;
10772 }
10773 ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
David Sterba91166212016-04-26 23:54:39 +020010774 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010775 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010776 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010777 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010778 goto end_trans;
10779 }
Filipe Mananad4b450c2015-01-29 19:18:25 +000010780 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010781
10782 /* Reset pinned so btrfs_put_block_group doesn't complain */
Zhao Leic30666d2015-02-25 14:17:20 +080010783 spin_lock(&space_info->lock);
10784 spin_lock(&block_group->lock);
10785
10786 space_info->bytes_pinned -= block_group->pinned;
10787 space_info->bytes_readonly += block_group->pinned;
10788 percpu_counter_add(&space_info->total_bytes_pinned,
10789 -block_group->pinned);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010790 block_group->pinned = 0;
10791
Zhao Leic30666d2015-02-25 14:17:20 +080010792 spin_unlock(&block_group->lock);
10793 spin_unlock(&space_info->lock);
10794
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010795 /* DISCARD can flip during remount */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010796 trimming = btrfs_test_opt(fs_info, DISCARD);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010797
10798 /* Implicit trim during transaction commit. */
10799 if (trimming)
10800 btrfs_get_block_group_trimming(block_group);
10801
Josef Bacik47ab2a62014-09-18 11:20:02 -040010802 /*
10803 * Btrfs_remove_chunk will abort the transaction if things go
10804 * horribly wrong.
10805 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010806 ret = btrfs_remove_chunk(trans, fs_info,
Josef Bacik47ab2a62014-09-18 11:20:02 -040010807 block_group->key.objectid);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010808
10809 if (ret) {
10810 if (trimming)
10811 btrfs_put_block_group_trimming(block_group);
10812 goto end_trans;
10813 }
10814
10815 /*
10816 * If we're not mounted with -odiscard, we can just forget
10817 * about this block group. Otherwise we'll need to wait
10818 * until transaction commit to do the actual discard.
10819 */
10820 if (trimming) {
Filipe Manana348a0012015-11-27 12:16:16 +000010821 spin_lock(&fs_info->unused_bgs_lock);
10822 /*
10823 * A concurrent scrub might have added us to the list
10824 * fs_info->unused_bgs, so use a list_move operation
10825 * to add the block group to the deleted_bgs list.
10826 */
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010827 list_move(&block_group->bg_list,
10828 &trans->transaction->deleted_bgs);
Filipe Manana348a0012015-11-27 12:16:16 +000010829 spin_unlock(&fs_info->unused_bgs_lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010830 btrfs_get_block_group(block_group);
10831 }
Filipe Manana758eb512014-11-03 14:08:39 +000010832end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010833 btrfs_end_transaction(trans);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010834next:
Zhao Leid5f2e332015-10-08 18:46:44 +080010835 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010836 btrfs_put_block_group(block_group);
10837 spin_lock(&fs_info->unused_bgs_lock);
10838 }
10839 spin_unlock(&fs_info->unused_bgs_lock);
10840}
10841
liuboc59021f2011-03-07 02:13:14 +000010842int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
10843{
liubo1aba86d2011-04-08 08:44:37 +000010844 struct btrfs_super_block *disk_super;
10845 u64 features;
10846 u64 flags;
10847 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +000010848 int ret;
10849
David Sterba6c417612011-04-13 15:41:04 +020010850 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +000010851 if (!btrfs_super_root(disk_super))
Dan Carpenter0dc924c52016-01-13 15:21:17 +030010852 return -EINVAL;
liuboc59021f2011-03-07 02:13:14 +000010853
liubo1aba86d2011-04-08 08:44:37 +000010854 features = btrfs_super_incompat_flags(disk_super);
10855 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
10856 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +000010857
liubo1aba86d2011-04-08 08:44:37 +000010858 flags = BTRFS_BLOCK_GROUP_SYSTEM;
Lu Fengqi4ca61682018-05-28 14:30:27 +080010859 ret = create_space_info(fs_info, flags);
liuboc59021f2011-03-07 02:13:14 +000010860 if (ret)
liubo1aba86d2011-04-08 08:44:37 +000010861 goto out;
liuboc59021f2011-03-07 02:13:14 +000010862
liubo1aba86d2011-04-08 08:44:37 +000010863 if (mixed) {
10864 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
Lu Fengqi4ca61682018-05-28 14:30:27 +080010865 ret = create_space_info(fs_info, flags);
liubo1aba86d2011-04-08 08:44:37 +000010866 } else {
10867 flags = BTRFS_BLOCK_GROUP_METADATA;
Lu Fengqi4ca61682018-05-28 14:30:27 +080010868 ret = create_space_info(fs_info, flags);
liubo1aba86d2011-04-08 08:44:37 +000010869 if (ret)
10870 goto out;
10871
10872 flags = BTRFS_BLOCK_GROUP_DATA;
Lu Fengqi4ca61682018-05-28 14:30:27 +080010873 ret = create_space_info(fs_info, flags);
liubo1aba86d2011-04-08 08:44:37 +000010874 }
10875out:
liuboc59021f2011-03-07 02:13:14 +000010876 return ret;
10877}
10878
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010879int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
10880 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +080010881{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010882 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +080010883}
10884
Jeff Mahoney499f3772015-06-15 09:41:17 -040010885/*
10886 * It used to be that old block groups would be left around forever.
10887 * Iterating over them would be enough to trim unused space. Since we
10888 * now automatically remove them, we also need to iterate over unallocated
10889 * space.
10890 *
10891 * We don't want a transaction for this since the discard may take a
10892 * substantial amount of time. We don't require that a transaction be
10893 * running, but we do need to take a running transaction into account
10894 * to ensure that we're not discarding chunks that were released in
10895 * the current transaction.
10896 *
10897 * Holding the chunks lock will prevent other threads from allocating
10898 * or releasing chunks, but it won't prevent a running transaction
10899 * from committing and releasing the memory that the pending chunks
10900 * list head uses. For that, we need to take a reference to the
10901 * transaction.
10902 */
10903static int btrfs_trim_free_extents(struct btrfs_device *device,
10904 u64 minlen, u64 *trimmed)
10905{
10906 u64 start = 0, len = 0;
10907 int ret;
10908
10909 *trimmed = 0;
10910
10911 /* Not writeable = nothing to do. */
Anand Jainebbede42017-12-04 12:54:52 +080010912 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoney499f3772015-06-15 09:41:17 -040010913 return 0;
10914
10915 /* No free space = nothing to do. */
10916 if (device->total_bytes <= device->bytes_used)
10917 return 0;
10918
10919 ret = 0;
10920
10921 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -040010922 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010923 struct btrfs_transaction *trans;
10924 u64 bytes;
10925
10926 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
10927 if (ret)
10928 return ret;
10929
10930 down_read(&fs_info->commit_root_sem);
10931
10932 spin_lock(&fs_info->trans_lock);
10933 trans = fs_info->running_transaction;
10934 if (trans)
Elena Reshetova9b64f572017-03-03 10:55:11 +020010935 refcount_inc(&trans->use_count);
Jeff Mahoney499f3772015-06-15 09:41:17 -040010936 spin_unlock(&fs_info->trans_lock);
10937
10938 ret = find_free_dev_extent_start(trans, device, minlen, start,
10939 &start, &len);
10940 if (trans)
10941 btrfs_put_transaction(trans);
10942
10943 if (ret) {
10944 up_read(&fs_info->commit_root_sem);
10945 mutex_unlock(&fs_info->chunk_mutex);
10946 if (ret == -ENOSPC)
10947 ret = 0;
10948 break;
10949 }
10950
10951 ret = btrfs_issue_discard(device->bdev, start, len, &bytes);
10952 up_read(&fs_info->commit_root_sem);
10953 mutex_unlock(&fs_info->chunk_mutex);
10954
10955 if (ret)
10956 break;
10957
10958 start += len;
10959 *trimmed += bytes;
10960
10961 if (fatal_signal_pending(current)) {
10962 ret = -ERESTARTSYS;
10963 break;
10964 }
10965
10966 cond_resched();
10967 }
10968
10969 return ret;
10970}
10971
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010972int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +000010973{
Li Dongyangf7039b12011-03-24 10:24:28 +000010974 struct btrfs_block_group_cache *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010975 struct btrfs_device *device;
10976 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +000010977 u64 group_trimmed;
10978 u64 start;
10979 u64 end;
10980 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +080010981 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +000010982 int ret = 0;
10983
Liu Bo2cac13e2012-02-09 18:17:41 +080010984 /*
10985 * try to trim all FS space, our block group may start from non-zero.
10986 */
10987 if (range->len == total_bytes)
10988 cache = btrfs_lookup_first_block_group(fs_info, range->start);
10989 else
10990 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +000010991
10992 while (cache) {
10993 if (cache->key.objectid >= (range->start + range->len)) {
10994 btrfs_put_block_group(cache);
10995 break;
10996 }
10997
10998 start = max(range->start, cache->key.objectid);
10999 end = min(range->start + range->len,
11000 cache->key.objectid + cache->key.offset);
11001
11002 if (end - start >= range->minlen) {
11003 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +000011004 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -040011005 if (ret) {
11006 btrfs_put_block_group(cache);
11007 break;
11008 }
11009 ret = wait_block_group_cache_done(cache);
11010 if (ret) {
11011 btrfs_put_block_group(cache);
11012 break;
11013 }
Li Dongyangf7039b12011-03-24 10:24:28 +000011014 }
11015 ret = btrfs_trim_block_group(cache,
11016 &group_trimmed,
11017 start,
11018 end,
11019 range->minlen);
11020
11021 trimmed += group_trimmed;
11022 if (ret) {
11023 btrfs_put_block_group(cache);
11024 break;
11025 }
11026 }
11027
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040011028 cache = next_block_group(fs_info, cache);
Li Dongyangf7039b12011-03-24 10:24:28 +000011029 }
11030
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011031 mutex_lock(&fs_info->fs_devices->device_list_mutex);
11032 devices = &fs_info->fs_devices->alloc_list;
Jeff Mahoney499f3772015-06-15 09:41:17 -040011033 list_for_each_entry(device, devices, dev_alloc_list) {
11034 ret = btrfs_trim_free_extents(device, range->minlen,
11035 &group_trimmed);
11036 if (ret)
11037 break;
11038
11039 trimmed += group_trimmed;
11040 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011041 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -040011042
Li Dongyangf7039b12011-03-24 10:24:28 +000011043 range->len = trimmed;
11044 return ret;
11045}
Miao Xie8257b2d2014-03-06 13:38:19 +080011046
11047/*
David Sterbaea14b57f2017-06-22 02:19:11 +020011048 * btrfs_{start,end}_write_no_snapshotting() are similar to
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011049 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
11050 * data into the page cache through nocow before the subvolume is snapshoted,
11051 * but flush the data into disk after the snapshot creation, or to prevent
David Sterbaea14b57f2017-06-22 02:19:11 +020011052 * operations while snapshotting is ongoing and that cause the snapshot to be
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011053 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +080011054 */
David Sterbaea14b57f2017-06-22 02:19:11 +020011055void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011056{
11057 percpu_counter_dec(&root->subv_writers->counter);
David Sterba093258e2018-02-26 16:15:17 +010011058 cond_wake_up(&root->subv_writers->wait);
Miao Xie8257b2d2014-03-06 13:38:19 +080011059}
11060
David Sterbaea14b57f2017-06-22 02:19:11 +020011061int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011062{
David Sterbaea14b57f2017-06-22 02:19:11 +020011063 if (atomic_read(&root->will_be_snapshotted))
Miao Xie8257b2d2014-03-06 13:38:19 +080011064 return 0;
11065
11066 percpu_counter_inc(&root->subv_writers->counter);
11067 /*
11068 * Make sure counter is updated before we check for snapshot creation.
11069 */
11070 smp_mb();
David Sterbaea14b57f2017-06-22 02:19:11 +020011071 if (atomic_read(&root->will_be_snapshotted)) {
11072 btrfs_end_write_no_snapshotting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +080011073 return 0;
11074 }
11075 return 1;
11076}
Zhao Lei0bc19f902016-01-06 18:56:36 +080011077
Zhao Lei0bc19f902016-01-06 18:56:36 +080011078void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
11079{
11080 while (true) {
11081 int ret;
11082
David Sterbaea14b57f2017-06-22 02:19:11 +020011083 ret = btrfs_start_write_no_snapshotting(root);
Zhao Lei0bc19f902016-01-06 18:56:36 +080011084 if (ret)
11085 break;
Peter Zijlstra46259562018-03-15 11:43:08 +010011086 wait_var_event(&root->will_be_snapshotted,
11087 !atomic_read(&root->will_be_snapshotted));
Zhao Lei0bc19f902016-01-06 18:56:36 +080011088 }
11089}