blob: 0b1f01dd02de6d3973744025828c9f0682967d53 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +010019#include <linux/sched/signal.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050020#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040021#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010022#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050023#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040024#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040025#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
David Sterbadff51cd2011-06-14 12:52:17 +020027#include <linux/ratelimit.h>
Josef Bacikb150a4f2013-06-19 15:00:04 -040028#include <linux/percpu_counter.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040029#include <linux/lockdep.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020030#include <linux/crc32c.h>
Miao Xie995946d2014-04-02 19:51:06 +080031#include "tree-log.h"
Chris Masonfec577f2007-02-26 10:40:21 -050032#include "disk-io.h"
33#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040034#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050035#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040036#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040037#include "free-space-cache.h"
Omar Sandoval1e144fb2015-09-29 20:50:37 -070038#include "free-space-tree.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060039#include "math.h"
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040040#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070041#include "qgroup.h"
Josef Bacikfd708b82017-09-29 15:43:50 -040042#include "ref-verify.h"
Chris Masonfec577f2007-02-26 10:40:21 -050043
Arne Jansen709c0482011-09-12 12:22:57 +020044#undef SCRAMBLE_DELAYED_REFS
45
Miao Xie9e622d62012-01-26 15:01:12 -050046/*
47 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040048 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
49 * if we really need one.
50 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040051 * CHUNK_ALLOC_LIMITED means to only try and allocate one
52 * if we have very few chunks already allocated. This is
53 * used as part of the clustering code to help make sure
54 * we have a good pool of storage to cluster in, without
55 * filling the FS with empty chunks
56 *
Miao Xie9e622d62012-01-26 15:01:12 -050057 * CHUNK_ALLOC_FORCE means it must try to allocate one
58 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040059 */
60enum {
61 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050062 CHUNK_ALLOC_LIMITED = 1,
63 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040064};
65
Yan Zheng5d4f98a2009-06-10 10:45:14 -040066static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040067 struct btrfs_fs_info *fs_info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080068 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040069 u64 root_objectid, u64 owner_objectid,
70 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080071 struct btrfs_delayed_extent_op *extra_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040072static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
73 struct extent_buffer *leaf,
74 struct btrfs_extent_item *ei);
75static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040076 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040077 u64 parent, u64 root_objectid,
78 u64 flags, u64 owner, u64 offset,
79 struct btrfs_key *ins, int ref_mod);
80static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040081 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040082 u64 parent, u64 root_objectid,
83 u64 flags, struct btrfs_disk_key *key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +010084 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050085static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040086 struct btrfs_fs_info *fs_info, u64 flags,
Josef Bacik698d0082012-09-12 14:08:47 -040087 int force);
Yan Zheng11833d62009-09-11 16:11:19 -040088static int find_next_key(struct btrfs_path *path, int level,
89 struct btrfs_key *key);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -040090static void dump_space_info(struct btrfs_fs_info *fs_info,
91 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -040092 int dump_block_groups);
Josef Bacik5d803662013-02-07 16:06:02 -050093static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
94 u64 num_bytes);
Josef Bacik957780e2016-05-17 13:30:55 -040095static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
96 struct btrfs_space_info *space_info,
97 u64 num_bytes);
98static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
99 struct btrfs_space_info *space_info,
100 u64 num_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -0500101
Josef Bacik817d52f2009-07-13 21:29:25 -0400102static noinline int
103block_group_cache_done(struct btrfs_block_group_cache *cache)
104{
105 smp_mb();
Josef Bacik36cce922013-08-05 11:15:21 -0400106 return cache->cached == BTRFS_CACHE_FINISHED ||
107 cache->cached == BTRFS_CACHE_ERROR;
Josef Bacik817d52f2009-07-13 21:29:25 -0400108}
109
Josef Bacik0f9dd462008-09-23 13:14:11 -0400110static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
111{
112 return (cache->flags & bits) == bits;
113}
114
Filipe Manana758f2df2015-11-19 11:45:48 +0000115void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000116{
117 atomic_inc(&cache->count);
118}
119
120void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
121{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400122 if (atomic_dec_and_test(&cache->count)) {
123 WARN_ON(cache->pinned > 0);
124 WARN_ON(cache->reserved > 0);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800125
126 /*
127 * If not empty, someone is still holding mutex of
128 * full_stripe_lock, which can only be released by caller.
129 * And it will definitely cause use-after-free when caller
130 * tries to release full stripe lock.
131 *
132 * No better way to resolve, but only to warn.
133 */
134 WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
Li Zefan34d52cb2011-03-29 13:46:06 +0800135 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000136 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400137 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000138}
139
Josef Bacik0f9dd462008-09-23 13:14:11 -0400140/*
141 * this adds the block group to the fs_info rb tree for the block group
142 * cache
143 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500144static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400145 struct btrfs_block_group_cache *block_group)
146{
147 struct rb_node **p;
148 struct rb_node *parent = NULL;
149 struct btrfs_block_group_cache *cache;
150
151 spin_lock(&info->block_group_cache_lock);
152 p = &info->block_group_cache_tree.rb_node;
153
154 while (*p) {
155 parent = *p;
156 cache = rb_entry(parent, struct btrfs_block_group_cache,
157 cache_node);
158 if (block_group->key.objectid < cache->key.objectid) {
159 p = &(*p)->rb_left;
160 } else if (block_group->key.objectid > cache->key.objectid) {
161 p = &(*p)->rb_right;
162 } else {
163 spin_unlock(&info->block_group_cache_lock);
164 return -EEXIST;
165 }
166 }
167
168 rb_link_node(&block_group->cache_node, parent, p);
169 rb_insert_color(&block_group->cache_node,
170 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000171
172 if (info->first_logical_byte > block_group->key.objectid)
173 info->first_logical_byte = block_group->key.objectid;
174
Josef Bacik0f9dd462008-09-23 13:14:11 -0400175 spin_unlock(&info->block_group_cache_lock);
176
177 return 0;
178}
179
180/*
181 * This will return the block group at or after bytenr if contains is 0, else
182 * it will return the block group that contains the bytenr
183 */
184static struct btrfs_block_group_cache *
185block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
186 int contains)
187{
188 struct btrfs_block_group_cache *cache, *ret = NULL;
189 struct rb_node *n;
190 u64 end, start;
191
192 spin_lock(&info->block_group_cache_lock);
193 n = info->block_group_cache_tree.rb_node;
194
195 while (n) {
196 cache = rb_entry(n, struct btrfs_block_group_cache,
197 cache_node);
198 end = cache->key.objectid + cache->key.offset - 1;
199 start = cache->key.objectid;
200
201 if (bytenr < start) {
202 if (!contains && (!ret || start < ret->key.objectid))
203 ret = cache;
204 n = n->rb_left;
205 } else if (bytenr > start) {
206 if (contains && bytenr <= end) {
207 ret = cache;
208 break;
209 }
210 n = n->rb_right;
211 } else {
212 ret = cache;
213 break;
214 }
215 }
Liu Boa1897fd2012-12-27 09:01:23 +0000216 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000217 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000218 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
219 info->first_logical_byte = ret->key.objectid;
220 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400221 spin_unlock(&info->block_group_cache_lock);
222
223 return ret;
224}
225
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400226static int add_excluded_extent(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400227 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400228{
Yan Zheng11833d62009-09-11 16:11:19 -0400229 u64 end = start + num_bytes - 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400230 set_extent_bits(&fs_info->freed_extents[0],
David Sterbaceeb0ae2016-04-26 23:54:39 +0200231 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400232 set_extent_bits(&fs_info->freed_extents[1],
David Sterbaceeb0ae2016-04-26 23:54:39 +0200233 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -0400234 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400235}
236
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400237static void free_excluded_extents(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400238 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400239{
Yan Zheng11833d62009-09-11 16:11:19 -0400240 u64 start, end;
241
242 start = cache->key.objectid;
243 end = start + cache->key.offset - 1;
244
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400245 clear_extent_bits(&fs_info->freed_extents[0],
David Sterba91166212016-04-26 23:54:39 +0200246 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400247 clear_extent_bits(&fs_info->freed_extents[1],
David Sterba91166212016-04-26 23:54:39 +0200248 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -0400249}
250
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400251static int exclude_super_stripes(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400252 struct btrfs_block_group_cache *cache)
253{
Josef Bacik817d52f2009-07-13 21:29:25 -0400254 u64 bytenr;
255 u64 *logical;
256 int stripe_len;
257 int i, nr, ret;
258
Yan, Zheng06b23312009-11-26 09:31:11 +0000259 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
260 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
261 cache->bytes_super += stripe_len;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400262 ret = add_excluded_extent(fs_info, cache->key.objectid,
Yan, Zheng06b23312009-11-26 09:31:11 +0000263 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400264 if (ret)
265 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000266 }
267
Josef Bacik817d52f2009-07-13 21:29:25 -0400268 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
269 bytenr = btrfs_sb_offset(i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400270 ret = btrfs_rmap_block(fs_info, cache->key.objectid,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -0400271 bytenr, 0, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400272 if (ret)
273 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400274
Josef Bacik817d52f2009-07-13 21:29:25 -0400275 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400276 u64 start, len;
277
278 if (logical[nr] > cache->key.objectid +
279 cache->key.offset)
280 continue;
281
282 if (logical[nr] + stripe_len <= cache->key.objectid)
283 continue;
284
285 start = logical[nr];
286 if (start < cache->key.objectid) {
287 start = cache->key.objectid;
288 len = (logical[nr] + stripe_len) - start;
289 } else {
290 len = min_t(u64, stripe_len,
291 cache->key.objectid +
292 cache->key.offset - start);
293 }
294
295 cache->bytes_super += len;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400296 ret = add_excluded_extent(fs_info, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400297 if (ret) {
298 kfree(logical);
299 return ret;
300 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400301 }
Yan Zheng11833d62009-09-11 16:11:19 -0400302
Josef Bacik817d52f2009-07-13 21:29:25 -0400303 kfree(logical);
304 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400305 return 0;
306}
307
Yan Zheng11833d62009-09-11 16:11:19 -0400308static struct btrfs_caching_control *
309get_caching_control(struct btrfs_block_group_cache *cache)
310{
311 struct btrfs_caching_control *ctl;
312
313 spin_lock(&cache->lock);
Josef Bacikdde5abe2010-09-16 16:17:03 -0400314 if (!cache->caching_ctl) {
315 spin_unlock(&cache->lock);
316 return NULL;
317 }
318
Yan Zheng11833d62009-09-11 16:11:19 -0400319 ctl = cache->caching_ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200320 refcount_inc(&ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400321 spin_unlock(&cache->lock);
322 return ctl;
323}
324
325static void put_caching_control(struct btrfs_caching_control *ctl)
326{
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200327 if (refcount_dec_and_test(&ctl->count))
Yan Zheng11833d62009-09-11 16:11:19 -0400328 kfree(ctl);
329}
330
Josef Bacikd0bd4562015-09-23 14:54:14 -0400331#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400332static void fragment_free_space(struct btrfs_block_group_cache *block_group)
Josef Bacikd0bd4562015-09-23 14:54:14 -0400333{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400334 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400335 u64 start = block_group->key.objectid;
336 u64 len = block_group->key.offset;
337 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400338 fs_info->nodesize : fs_info->sectorsize;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400339 u64 step = chunk << 1;
340
341 while (len > chunk) {
342 btrfs_remove_free_space(block_group, start, chunk);
343 start += step;
344 if (len < step)
345 len = 0;
346 else
347 len -= step;
348 }
349}
350#endif
351
Josef Bacik0f9dd462008-09-23 13:14:11 -0400352/*
353 * this is only called by cache_block_group, since we could have freed extents
354 * we need to check the pinned_extents for any extents that can't be used yet
355 * since their free space will be released as soon as the transaction commits.
356 */
Omar Sandovala5ed9182015-09-29 20:50:35 -0700357u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
358 struct btrfs_fs_info *info, u64 start, u64 end)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400359{
Josef Bacik817d52f2009-07-13 21:29:25 -0400360 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400361 int ret;
362
363 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400364 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400365 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400366 EXTENT_DIRTY | EXTENT_UPTODATE,
367 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400368 if (ret)
369 break;
370
Yan, Zheng06b23312009-11-26 09:31:11 +0000371 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400372 start = extent_end + 1;
373 } else if (extent_start > start && extent_start < end) {
374 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400375 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500376 ret = btrfs_add_free_space(block_group, start,
377 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100378 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400379 start = extent_end + 1;
380 } else {
381 break;
382 }
383 }
384
385 if (start < end) {
386 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400387 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500388 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100389 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400390 }
391
Josef Bacik817d52f2009-07-13 21:29:25 -0400392 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400393}
394
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700395static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
Chris Masone37c9e62007-05-09 20:13:14 -0400396{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400397 struct btrfs_block_group_cache *block_group = caching_ctl->block_group;
398 struct btrfs_fs_info *fs_info = block_group->fs_info;
399 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400400 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400401 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400402 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400403 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400404 u64 last = 0;
405 u32 nritems;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700406 int ret;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400407 bool wakeup = true;
Chris Masonf510cfe2007-10-15 16:14:48 -0400408
Chris Masone37c9e62007-05-09 20:13:14 -0400409 path = btrfs_alloc_path();
410 if (!path)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700411 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400412
Josef Bacik817d52f2009-07-13 21:29:25 -0400413 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400414
Josef Bacikd0bd4562015-09-23 14:54:14 -0400415#ifdef CONFIG_BTRFS_DEBUG
416 /*
417 * If we're fragmenting we don't want to make anybody think we can
418 * allocate from this block group until we've had a chance to fragment
419 * the free space.
420 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400421 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -0400422 wakeup = false;
423#endif
Chris Mason5cd57b22008-06-25 16:01:30 -0400424 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400425 * We don't want to deadlock with somebody trying to allocate a new
426 * extent for the extent root while also trying to search the extent
427 * root to add free space. So we skip locking and search the commit
428 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400429 */
430 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400431 path->search_commit_root = 1;
David Sterbae4058b52015-11-27 16:31:35 +0100432 path->reada = READA_FORWARD;
Josef Bacik817d52f2009-07-13 21:29:25 -0400433
Yan Zhenge4404d62008-12-12 10:03:26 -0500434 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400435 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400436 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400437
Liu Bo52ee28d2013-07-11 17:51:15 +0800438next:
Yan Zheng11833d62009-09-11 16:11:19 -0400439 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400440 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700441 goto out;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500442
Yan Zheng11833d62009-09-11 16:11:19 -0400443 leaf = path->nodes[0];
444 nritems = btrfs_header_nritems(leaf);
445
Chris Masond3977122009-01-05 21:25:51 -0500446 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200447 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400448 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400449 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400450 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400451
Yan Zheng11833d62009-09-11 16:11:19 -0400452 if (path->slots[0] < nritems) {
453 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
454 } else {
455 ret = find_next_key(path, 0, &key);
456 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400457 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400458
Josef Bacikc9ea7b22013-09-19 10:02:11 -0400459 if (need_resched() ||
Josef Bacik9e351cc2014-03-13 15:42:13 -0400460 rwsem_is_contended(&fs_info->commit_root_sem)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400461 if (wakeup)
462 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400463 btrfs_release_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400464 up_read(&fs_info->commit_root_sem);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400465 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400466 cond_resched();
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700467 mutex_lock(&caching_ctl->mutex);
468 down_read(&fs_info->commit_root_sem);
469 goto next;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400470 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400471
472 ret = btrfs_next_leaf(extent_root, path);
473 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700474 goto out;
Josef Bacik0a3896d2013-04-19 14:37:26 -0400475 if (ret)
476 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400477 leaf = path->nodes[0];
478 nritems = btrfs_header_nritems(leaf);
479 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400480 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400481
Liu Bo52ee28d2013-07-11 17:51:15 +0800482 if (key.objectid < last) {
483 key.objectid = last;
484 key.offset = 0;
485 key.type = BTRFS_EXTENT_ITEM_KEY;
486
Josef Bacikd0bd4562015-09-23 14:54:14 -0400487 if (wakeup)
488 caching_ctl->progress = last;
Liu Bo52ee28d2013-07-11 17:51:15 +0800489 btrfs_release_path(path);
490 goto next;
491 }
492
Yan Zheng11833d62009-09-11 16:11:19 -0400493 if (key.objectid < block_group->key.objectid) {
494 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400495 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400496 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400497
Chris Masone37c9e62007-05-09 20:13:14 -0400498 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400499 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400500 break;
Yan7d7d6062007-09-14 16:15:28 -0400501
Josef Bacik3173a182013-03-07 14:22:04 -0500502 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
503 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400504 total_found += add_new_free_space(block_group,
505 fs_info, last,
506 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500507 if (key.type == BTRFS_METADATA_ITEM_KEY)
508 last = key.objectid +
Jeff Mahoneyda170662016-06-15 09:22:56 -0400509 fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500510 else
511 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400512
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700513 if (total_found > CACHING_CTL_WAKE_UP) {
Yan Zheng11833d62009-09-11 16:11:19 -0400514 total_found = 0;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400515 if (wakeup)
516 wake_up(&caching_ctl->wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400517 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400518 }
Chris Masone37c9e62007-05-09 20:13:14 -0400519 path->slots[0]++;
520 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400521 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400522
523 total_found += add_new_free_space(block_group, fs_info, last,
524 block_group->key.objectid +
525 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400526 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400527
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700528out:
529 btrfs_free_path(path);
530 return ret;
531}
532
533static noinline void caching_thread(struct btrfs_work *work)
534{
535 struct btrfs_block_group_cache *block_group;
536 struct btrfs_fs_info *fs_info;
537 struct btrfs_caching_control *caching_ctl;
538 int ret;
539
540 caching_ctl = container_of(work, struct btrfs_caching_control, work);
541 block_group = caching_ctl->block_group;
542 fs_info = block_group->fs_info;
543
544 mutex_lock(&caching_ctl->mutex);
545 down_read(&fs_info->commit_root_sem);
546
Omar Sandoval1e144fb2015-09-29 20:50:37 -0700547 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
548 ret = load_free_space_tree(caching_ctl);
549 else
550 ret = load_extent_tree_free(caching_ctl);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700551
Josef Bacik817d52f2009-07-13 21:29:25 -0400552 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400553 block_group->caching_ctl = NULL;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700554 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
Josef Bacik817d52f2009-07-13 21:29:25 -0400555 spin_unlock(&block_group->lock);
556
Josef Bacikd0bd4562015-09-23 14:54:14 -0400557#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400558 if (btrfs_should_fragment_free_space(block_group)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400559 u64 bytes_used;
560
561 spin_lock(&block_group->space_info->lock);
562 spin_lock(&block_group->lock);
563 bytes_used = block_group->key.offset -
564 btrfs_block_group_used(&block_group->item);
565 block_group->space_info->bytes_used += bytes_used >> 1;
566 spin_unlock(&block_group->lock);
567 spin_unlock(&block_group->space_info->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400568 fragment_free_space(block_group);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400569 }
570#endif
571
572 caching_ctl->progress = (u64)-1;
Chris Masonf7d3d2f2015-12-18 11:11:10 -0800573
Josef Bacik9e351cc2014-03-13 15:42:13 -0400574 up_read(&fs_info->commit_root_sem);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400575 free_excluded_extents(fs_info, block_group);
Yan Zheng11833d62009-09-11 16:11:19 -0400576 mutex_unlock(&caching_ctl->mutex);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700577
Yan Zheng11833d62009-09-11 16:11:19 -0400578 wake_up(&caching_ctl->wait);
579
580 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000581 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400582}
583
Josef Bacik9d66e232010-08-25 16:54:15 -0400584static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400585 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400586{
Josef Bacik291c7d22011-11-14 13:52:14 -0500587 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400588 struct btrfs_fs_info *fs_info = cache->fs_info;
589 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400590 int ret = 0;
591
Josef Bacik291c7d22011-11-14 13:52:14 -0500592 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100593 if (!caching_ctl)
594 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500595
596 INIT_LIST_HEAD(&caching_ctl->list);
597 mutex_init(&caching_ctl->mutex);
598 init_waitqueue_head(&caching_ctl->wait);
599 caching_ctl->block_group = cache;
600 caching_ctl->progress = cache->key.objectid;
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200601 refcount_set(&caching_ctl->count, 1);
Liu Bo9e0af232014-08-15 23:36:53 +0800602 btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
603 caching_thread, NULL, NULL);
Josef Bacik291c7d22011-11-14 13:52:14 -0500604
605 spin_lock(&cache->lock);
606 /*
607 * This should be a rare occasion, but this could happen I think in the
608 * case where one thread starts to load the space cache info, and then
609 * some other thread starts a transaction commit which tries to do an
610 * allocation while the other thread is still loading the space cache
611 * info. The previous loop should have kept us from choosing this block
612 * group, but if we've moved to the state where we will wait on caching
613 * block groups we need to first check if we're doing a fast load here,
614 * so we can wait for it to finish, otherwise we could end up allocating
615 * from a block group who's cache gets evicted for one reason or
616 * another.
617 */
618 while (cache->cached == BTRFS_CACHE_FAST) {
619 struct btrfs_caching_control *ctl;
620
621 ctl = cache->caching_ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200622 refcount_inc(&ctl->count);
Josef Bacik291c7d22011-11-14 13:52:14 -0500623 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
624 spin_unlock(&cache->lock);
625
626 schedule();
627
628 finish_wait(&ctl->wait, &wait);
629 put_caching_control(ctl);
630 spin_lock(&cache->lock);
631 }
632
633 if (cache->cached != BTRFS_CACHE_NO) {
634 spin_unlock(&cache->lock);
635 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400636 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500637 }
638 WARN_ON(cache->caching_ctl);
639 cache->caching_ctl = caching_ctl;
640 cache->cached = BTRFS_CACHE_FAST;
641 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400642
Satoru Takeuchid8953d62017-09-12 20:08:08 +0900643 if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500644 mutex_lock(&caching_ctl->mutex);
Josef Bacik9d66e232010-08-25 16:54:15 -0400645 ret = load_free_space_cache(fs_info, cache);
646
647 spin_lock(&cache->lock);
648 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500649 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400650 cache->cached = BTRFS_CACHE_FINISHED;
651 cache->last_byte_to_unpin = (u64)-1;
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500652 caching_ctl->progress = (u64)-1;
Josef Bacik9d66e232010-08-25 16:54:15 -0400653 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500654 if (load_cache_only) {
655 cache->caching_ctl = NULL;
656 cache->cached = BTRFS_CACHE_NO;
657 } else {
658 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000659 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500660 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400661 }
662 spin_unlock(&cache->lock);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400663#ifdef CONFIG_BTRFS_DEBUG
664 if (ret == 1 &&
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400665 btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400666 u64 bytes_used;
667
668 spin_lock(&cache->space_info->lock);
669 spin_lock(&cache->lock);
670 bytes_used = cache->key.offset -
671 btrfs_block_group_used(&cache->item);
672 cache->space_info->bytes_used += bytes_used >> 1;
673 spin_unlock(&cache->lock);
674 spin_unlock(&cache->space_info->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400675 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400676 }
677#endif
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500678 mutex_unlock(&caching_ctl->mutex);
679
Josef Bacik291c7d22011-11-14 13:52:14 -0500680 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000681 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500682 put_caching_control(caching_ctl);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400683 free_excluded_extents(fs_info, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400684 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000685 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500686 } else {
687 /*
Omar Sandoval1e144fb2015-09-29 20:50:37 -0700688 * We're either using the free space tree or no caching at all.
689 * Set cached to the appropriate value and wakeup any waiters.
Josef Bacik291c7d22011-11-14 13:52:14 -0500690 */
691 spin_lock(&cache->lock);
692 if (load_cache_only) {
693 cache->caching_ctl = NULL;
694 cache->cached = BTRFS_CACHE_NO;
695 } else {
696 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000697 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500698 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400699 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500700 wake_up(&caching_ctl->wait);
701 }
702
703 if (load_cache_only) {
704 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400705 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400706 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400707
Josef Bacik9e351cc2014-03-13 15:42:13 -0400708 down_write(&fs_info->commit_root_sem);
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200709 refcount_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400710 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400711 up_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -0400712
Josef Bacik11dfe352009-11-13 20:12:59 +0000713 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400714
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800715 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400716
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400717 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400718}
719
Josef Bacik0f9dd462008-09-23 13:14:11 -0400720/*
721 * return the block group that starts at or after bytenr
722 */
Chris Masond3977122009-01-05 21:25:51 -0500723static struct btrfs_block_group_cache *
724btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400725{
Masahiro Yamadae2c89902016-09-13 04:35:52 +0900726 return block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400727}
728
Josef Bacik0f9dd462008-09-23 13:14:11 -0400729/*
Sankar P9f556842009-05-14 13:52:22 -0400730 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400731 */
Chris Masond3977122009-01-05 21:25:51 -0500732struct btrfs_block_group_cache *btrfs_lookup_block_group(
733 struct btrfs_fs_info *info,
734 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400735{
Masahiro Yamadae2c89902016-09-13 04:35:52 +0900736 return block_group_cache_tree_search(info, bytenr, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400737}
Chris Mason0b86a832008-03-24 15:01:56 -0400738
Josef Bacik0f9dd462008-09-23 13:14:11 -0400739static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
740 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400741{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400742 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400743 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400744
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200745 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400746
Chris Mason4184ea72009-03-10 12:39:20 -0400747 rcu_read_lock();
748 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400749 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400750 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400751 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400752 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400753 }
Chris Mason4184ea72009-03-10 12:39:20 -0400754 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400755 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400756}
757
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700758static void add_pinned_bytes(struct btrfs_fs_info *fs_info, s64 num_bytes,
759 u64 owner, u64 root_objectid)
760{
761 struct btrfs_space_info *space_info;
762 u64 flags;
763
764 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
765 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
766 flags = BTRFS_BLOCK_GROUP_SYSTEM;
767 else
768 flags = BTRFS_BLOCK_GROUP_METADATA;
769 } else {
770 flags = BTRFS_BLOCK_GROUP_DATA;
771 }
772
773 space_info = __find_space_info(fs_info, flags);
Omar Sandoval55e81962017-06-06 16:45:27 -0700774 ASSERT(space_info);
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700775 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
776}
777
Chris Mason4184ea72009-03-10 12:39:20 -0400778/*
779 * after adding space to the filesystem, we need to clear the full flags
780 * on all the space infos.
781 */
782void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
783{
784 struct list_head *head = &info->space_info;
785 struct btrfs_space_info *found;
786
787 rcu_read_lock();
788 list_for_each_entry_rcu(found, head, list)
789 found->full = 0;
790 rcu_read_unlock();
791}
792
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000793/* simple helper to search for an existing data extent at a given offset */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400794int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400795{
796 int ret;
797 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400798 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400799
Zheng Yan31840ae2008-09-23 13:14:14 -0400800 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700801 if (!path)
802 return -ENOMEM;
803
Chris Masone02119d2008-09-05 16:13:11 -0400804 key.objectid = start;
805 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500806 key.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400807 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400808 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500809 return ret;
810}
811
Chris Masond8d5f3e2007-12-11 12:42:00 -0500812/*
Josef Bacik3173a182013-03-07 14:22:04 -0500813 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400814 *
815 * the head node for delayed ref is used to store the sum of all the
816 * reference count modifications queued up in the rbtree. the head
817 * node may also store the extent flags to set. This way you can check
818 * to see what the reference count and extent flags would be if all of
819 * the delayed refs are not processed.
820 */
821int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400822 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500823 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400824{
825 struct btrfs_delayed_ref_head *head;
826 struct btrfs_delayed_ref_root *delayed_refs;
827 struct btrfs_path *path;
828 struct btrfs_extent_item *ei;
829 struct extent_buffer *leaf;
830 struct btrfs_key key;
831 u32 item_size;
832 u64 num_refs;
833 u64 extent_flags;
834 int ret;
835
Josef Bacik3173a182013-03-07 14:22:04 -0500836 /*
837 * If we don't have skinny metadata, don't bother doing anything
838 * different
839 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400840 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
841 offset = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500842 metadata = 0;
843 }
844
Yan, Zhenga22285a2010-05-16 10:48:46 -0400845 path = btrfs_alloc_path();
846 if (!path)
847 return -ENOMEM;
848
Yan, Zhenga22285a2010-05-16 10:48:46 -0400849 if (!trans) {
850 path->skip_locking = 1;
851 path->search_commit_root = 1;
852 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000853
854search_again:
855 key.objectid = bytenr;
856 key.offset = offset;
857 if (metadata)
858 key.type = BTRFS_METADATA_ITEM_KEY;
859 else
860 key.type = BTRFS_EXTENT_ITEM_KEY;
861
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400862 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400863 if (ret < 0)
864 goto out_free;
865
Josef Bacik3173a182013-03-07 14:22:04 -0500866 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100867 if (path->slots[0]) {
868 path->slots[0]--;
869 btrfs_item_key_to_cpu(path->nodes[0], &key,
870 path->slots[0]);
871 if (key.objectid == bytenr &&
872 key.type == BTRFS_EXTENT_ITEM_KEY &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400873 key.offset == fs_info->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100874 ret = 0;
875 }
Josef Bacik3173a182013-03-07 14:22:04 -0500876 }
877
Yan, Zhenga22285a2010-05-16 10:48:46 -0400878 if (ret == 0) {
879 leaf = path->nodes[0];
880 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
881 if (item_size >= sizeof(*ei)) {
882 ei = btrfs_item_ptr(leaf, path->slots[0],
883 struct btrfs_extent_item);
884 num_refs = btrfs_extent_refs(leaf, ei);
885 extent_flags = btrfs_extent_flags(leaf, ei);
886 } else {
887#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
888 struct btrfs_extent_item_v0 *ei0;
889 BUG_ON(item_size != sizeof(*ei0));
890 ei0 = btrfs_item_ptr(leaf, path->slots[0],
891 struct btrfs_extent_item_v0);
892 num_refs = btrfs_extent_refs_v0(leaf, ei0);
893 /* FIXME: this isn't correct for data */
894 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
895#else
896 BUG();
897#endif
898 }
899 BUG_ON(num_refs == 0);
900 } else {
901 num_refs = 0;
902 extent_flags = 0;
903 ret = 0;
904 }
905
906 if (!trans)
907 goto out;
908
909 delayed_refs = &trans->transaction->delayed_refs;
910 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -0800911 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400912 if (head) {
913 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -0400914 refcount_inc(&head->refs);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400915 spin_unlock(&delayed_refs->lock);
916
David Sterbab3b4aa72011-04-21 01:20:15 +0200917 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400918
David Sterba8cc33e52011-05-02 15:29:25 +0200919 /*
920 * Mutex was contended, block until it's released and try
921 * again
922 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400923 mutex_lock(&head->mutex);
924 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -0400925 btrfs_put_delayed_ref_head(head);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000926 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400927 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500928 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400929 if (head->extent_op && head->extent_op->update_flags)
930 extent_flags |= head->extent_op->flags_to_set;
931 else
932 BUG_ON(num_refs == 0);
933
Josef Bacikd2788502017-09-29 15:43:57 -0400934 num_refs += head->ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500935 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400936 mutex_unlock(&head->mutex);
937 }
938 spin_unlock(&delayed_refs->lock);
939out:
940 WARN_ON(num_refs == 0);
941 if (refs)
942 *refs = num_refs;
943 if (flags)
944 *flags = extent_flags;
945out_free:
946 btrfs_free_path(path);
947 return ret;
948}
949
950/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500951 * Back reference rules. Back refs have three main goals:
952 *
953 * 1) differentiate between all holders of references to an extent so that
954 * when a reference is dropped we can make sure it was a valid reference
955 * before freeing the extent.
956 *
957 * 2) Provide enough information to quickly find the holders of an extent
958 * if we notice a given block is corrupted or bad.
959 *
960 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
961 * maintenance. This is actually the same as #2, but with a slightly
962 * different use case.
963 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400964 * There are two kinds of back refs. The implicit back refs is optimized
965 * for pointers in non-shared tree blocks. For a given pointer in a block,
966 * back refs of this kind provide information about the block's owner tree
967 * and the pointer's key. These information allow us to find the block by
968 * b-tree searching. The full back refs is for pointers in tree blocks not
969 * referenced by their owner trees. The location of tree block is recorded
970 * in the back refs. Actually the full back refs is generic, and can be
971 * used in all cases the implicit back refs is used. The major shortcoming
972 * of the full back refs is its overhead. Every time a tree block gets
973 * COWed, we have to update back refs entry for all pointers in it.
974 *
975 * For a newly allocated tree block, we use implicit back refs for
976 * pointers in it. This means most tree related operations only involve
977 * implicit back refs. For a tree block created in old transaction, the
978 * only way to drop a reference to it is COW it. So we can detect the
979 * event that tree block loses its owner tree's reference and do the
980 * back refs conversion.
981 *
Nicholas D Steeves01327612016-05-19 21:18:45 -0400982 * When a tree block is COWed through a tree, there are four cases:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400983 *
984 * The reference count of the block is one and the tree is the block's
985 * owner tree. Nothing to do in this case.
986 *
987 * The reference count of the block is one and the tree is not the
988 * block's owner tree. In this case, full back refs is used for pointers
989 * in the block. Remove these full back refs, add implicit back refs for
990 * every pointers in the new block.
991 *
992 * The reference count of the block is greater than one and the tree is
993 * the block's owner tree. In this case, implicit back refs is used for
994 * pointers in the block. Add full back refs for every pointers in the
995 * block, increase lower level extents' reference counts. The original
996 * implicit back refs are entailed to the new block.
997 *
998 * The reference count of the block is greater than one and the tree is
999 * not the block's owner tree. Add implicit back refs for every pointer in
1000 * the new block, increase lower level extents' reference count.
1001 *
1002 * Back Reference Key composing:
1003 *
1004 * The key objectid corresponds to the first byte in the extent,
1005 * The key type is used to differentiate between types of back refs.
1006 * There are different meanings of the key offset for different types
1007 * of back refs.
1008 *
Chris Masond8d5f3e2007-12-11 12:42:00 -05001009 * File extents can be referenced by:
1010 *
1011 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -04001012 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -05001013 * - different offsets inside a file (bookend extents in file.c)
1014 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001015 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001016 *
1017 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -05001018 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001019 * - original offset in the file
1020 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -04001021 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001022 * The key offset for the implicit back refs is hash of the first
1023 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001024 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001025 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001026 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001027 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001028 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001029 * The key offset for the implicit back refs is the first byte of
1030 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001031 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001032 * When a file extent is allocated, The implicit back refs is used.
1033 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001034 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001035 * (root_key.objectid, inode objectid, offset in file, 1)
1036 *
1037 * When a file extent is removed file truncation, we find the
1038 * corresponding implicit back refs and check the following fields:
1039 *
1040 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -05001041 *
1042 * Btree extents can be referenced by:
1043 *
1044 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -05001045 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001046 * Both the implicit back refs and the full back refs for tree blocks
1047 * only consist of key. The key offset for the implicit back refs is
1048 * objectid of block's owner tree. The key offset for the full back refs
1049 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001050 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001051 * When implicit back refs is used, information about the lowest key and
1052 * level of the tree block are required. These information are stored in
1053 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001054 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001055
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001056#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1057static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001058 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001059 struct btrfs_path *path,
1060 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -05001061{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001062 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001063 struct btrfs_extent_item *item;
1064 struct btrfs_extent_item_v0 *ei0;
1065 struct btrfs_extent_ref_v0 *ref0;
1066 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -04001067 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001068 struct btrfs_key key;
1069 struct btrfs_key found_key;
1070 u32 new_size = sizeof(*item);
1071 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -05001072 int ret;
1073
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001074 leaf = path->nodes[0];
1075 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -05001076
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001077 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1078 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1079 struct btrfs_extent_item_v0);
1080 refs = btrfs_extent_refs_v0(leaf, ei0);
1081
1082 if (owner == (u64)-1) {
1083 while (1) {
1084 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1085 ret = btrfs_next_leaf(root, path);
1086 if (ret < 0)
1087 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001088 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001089 leaf = path->nodes[0];
1090 }
1091 btrfs_item_key_to_cpu(leaf, &found_key,
1092 path->slots[0]);
1093 BUG_ON(key.objectid != found_key.objectid);
1094 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1095 path->slots[0]++;
1096 continue;
1097 }
1098 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1099 struct btrfs_extent_ref_v0);
1100 owner = btrfs_ref_objectid_v0(leaf, ref0);
1101 break;
1102 }
1103 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001104 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001105
1106 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1107 new_size += sizeof(*bi);
1108
1109 new_size -= sizeof(*ei0);
1110 ret = btrfs_search_slot(trans, root, &key, path,
1111 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001112 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001113 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001114 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001115
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001116 btrfs_extend_item(fs_info, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001117
1118 leaf = path->nodes[0];
1119 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1120 btrfs_set_extent_refs(leaf, item, refs);
1121 /* FIXME: get real generation */
1122 btrfs_set_extent_generation(leaf, item, 0);
1123 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1124 btrfs_set_extent_flags(leaf, item,
1125 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1126 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1127 bi = (struct btrfs_tree_block_info *)(item + 1);
1128 /* FIXME: get first key of the block */
David Sterbab159fa22016-11-08 18:09:03 +01001129 memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001130 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1131 } else {
1132 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1133 }
1134 btrfs_mark_buffer_dirty(leaf);
1135 return 0;
1136}
1137#endif
1138
Liu Bo167ce952017-08-18 15:15:18 -06001139/*
1140 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
1141 * is_data == BTRFS_REF_TYPE_DATA, data type is requried,
1142 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
1143 */
1144int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
1145 struct btrfs_extent_inline_ref *iref,
1146 enum btrfs_inline_ref_type is_data)
1147{
1148 int type = btrfs_extent_inline_ref_type(eb, iref);
Liu Bo64ecdb62017-08-18 15:15:24 -06001149 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
Liu Bo167ce952017-08-18 15:15:18 -06001150
1151 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1152 type == BTRFS_SHARED_BLOCK_REF_KEY ||
1153 type == BTRFS_SHARED_DATA_REF_KEY ||
1154 type == BTRFS_EXTENT_DATA_REF_KEY) {
1155 if (is_data == BTRFS_REF_TYPE_BLOCK) {
Liu Bo64ecdb62017-08-18 15:15:24 -06001156 if (type == BTRFS_TREE_BLOCK_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -06001157 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -06001158 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1159 ASSERT(eb->fs_info);
1160 /*
1161 * Every shared one has parent tree
1162 * block, which must be aligned to
1163 * nodesize.
1164 */
1165 if (offset &&
1166 IS_ALIGNED(offset, eb->fs_info->nodesize))
1167 return type;
1168 }
Liu Bo167ce952017-08-18 15:15:18 -06001169 } else if (is_data == BTRFS_REF_TYPE_DATA) {
Liu Bo64ecdb62017-08-18 15:15:24 -06001170 if (type == BTRFS_EXTENT_DATA_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -06001171 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -06001172 if (type == BTRFS_SHARED_DATA_REF_KEY) {
1173 ASSERT(eb->fs_info);
1174 /*
1175 * Every shared one has parent tree
1176 * block, which must be aligned to
1177 * nodesize.
1178 */
1179 if (offset &&
1180 IS_ALIGNED(offset, eb->fs_info->nodesize))
1181 return type;
1182 }
Liu Bo167ce952017-08-18 15:15:18 -06001183 } else {
1184 ASSERT(is_data == BTRFS_REF_TYPE_ANY);
1185 return type;
1186 }
1187 }
1188
1189 btrfs_print_leaf((struct extent_buffer *)eb);
1190 btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d",
1191 eb->start, type);
1192 WARN_ON(1);
1193
1194 return BTRFS_REF_TYPE_INVALID;
1195}
1196
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001197static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1198{
1199 u32 high_crc = ~(u32)0;
1200 u32 low_crc = ~(u32)0;
1201 __le64 lenum;
1202
1203 lenum = cpu_to_le64(root_objectid);
Nikolay Borisov9678c542018-01-08 11:45:05 +02001204 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001205 lenum = cpu_to_le64(owner);
Nikolay Borisov9678c542018-01-08 11:45:05 +02001206 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001207 lenum = cpu_to_le64(offset);
Nikolay Borisov9678c542018-01-08 11:45:05 +02001208 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001209
1210 return ((u64)high_crc << 31) ^ (u64)low_crc;
1211}
1212
1213static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1214 struct btrfs_extent_data_ref *ref)
1215{
1216 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1217 btrfs_extent_data_ref_objectid(leaf, ref),
1218 btrfs_extent_data_ref_offset(leaf, ref));
1219}
1220
1221static int match_extent_data_ref(struct extent_buffer *leaf,
1222 struct btrfs_extent_data_ref *ref,
1223 u64 root_objectid, u64 owner, u64 offset)
1224{
1225 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1226 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1227 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1228 return 0;
1229 return 1;
1230}
1231
1232static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001233 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001234 struct btrfs_path *path,
1235 u64 bytenr, u64 parent,
1236 u64 root_objectid,
1237 u64 owner, u64 offset)
1238{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001239 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001240 struct btrfs_key key;
1241 struct btrfs_extent_data_ref *ref;
1242 struct extent_buffer *leaf;
1243 u32 nritems;
1244 int ret;
1245 int recow;
1246 int err = -ENOENT;
1247
1248 key.objectid = bytenr;
1249 if (parent) {
1250 key.type = BTRFS_SHARED_DATA_REF_KEY;
1251 key.offset = parent;
1252 } else {
1253 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1254 key.offset = hash_extent_data_ref(root_objectid,
1255 owner, offset);
1256 }
1257again:
1258 recow = 0;
1259 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1260 if (ret < 0) {
1261 err = ret;
1262 goto fail;
1263 }
1264
1265 if (parent) {
1266 if (!ret)
1267 return 0;
1268#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1269 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001270 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001271 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1272 if (ret < 0) {
1273 err = ret;
1274 goto fail;
1275 }
1276 if (!ret)
1277 return 0;
1278#endif
1279 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001280 }
1281
1282 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001283 nritems = btrfs_header_nritems(leaf);
1284 while (1) {
1285 if (path->slots[0] >= nritems) {
1286 ret = btrfs_next_leaf(root, path);
1287 if (ret < 0)
1288 err = ret;
1289 if (ret)
1290 goto fail;
1291
1292 leaf = path->nodes[0];
1293 nritems = btrfs_header_nritems(leaf);
1294 recow = 1;
1295 }
1296
1297 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1298 if (key.objectid != bytenr ||
1299 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1300 goto fail;
1301
1302 ref = btrfs_item_ptr(leaf, path->slots[0],
1303 struct btrfs_extent_data_ref);
1304
1305 if (match_extent_data_ref(leaf, ref, root_objectid,
1306 owner, offset)) {
1307 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001308 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001309 goto again;
1310 }
1311 err = 0;
1312 break;
1313 }
1314 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001315 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001316fail:
1317 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001318}
1319
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001320static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001321 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001322 struct btrfs_path *path,
1323 u64 bytenr, u64 parent,
1324 u64 root_objectid, u64 owner,
1325 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001326{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001327 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04001328 struct btrfs_key key;
1329 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001330 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001331 u32 num_refs;
1332 int ret;
1333
1334 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001335 if (parent) {
1336 key.type = BTRFS_SHARED_DATA_REF_KEY;
1337 key.offset = parent;
1338 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001339 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001340 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1341 key.offset = hash_extent_data_ref(root_objectid,
1342 owner, offset);
1343 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001344 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001345
1346 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1347 if (ret && ret != -EEXIST)
1348 goto fail;
1349
1350 leaf = path->nodes[0];
1351 if (parent) {
1352 struct btrfs_shared_data_ref *ref;
1353 ref = btrfs_item_ptr(leaf, path->slots[0],
1354 struct btrfs_shared_data_ref);
1355 if (ret == 0) {
1356 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1357 } else {
1358 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1359 num_refs += refs_to_add;
1360 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1361 }
1362 } else {
1363 struct btrfs_extent_data_ref *ref;
1364 while (ret == -EEXIST) {
1365 ref = btrfs_item_ptr(leaf, path->slots[0],
1366 struct btrfs_extent_data_ref);
1367 if (match_extent_data_ref(leaf, ref, root_objectid,
1368 owner, offset))
1369 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001370 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001371 key.offset++;
1372 ret = btrfs_insert_empty_item(trans, root, path, &key,
1373 size);
1374 if (ret && ret != -EEXIST)
1375 goto fail;
1376
1377 leaf = path->nodes[0];
1378 }
1379 ref = btrfs_item_ptr(leaf, path->slots[0],
1380 struct btrfs_extent_data_ref);
1381 if (ret == 0) {
1382 btrfs_set_extent_data_ref_root(leaf, ref,
1383 root_objectid);
1384 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1385 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1386 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1387 } else {
1388 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1389 num_refs += refs_to_add;
1390 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1391 }
1392 }
1393 btrfs_mark_buffer_dirty(leaf);
1394 ret = 0;
1395fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001396 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001397 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001398}
1399
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001400static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001401 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001402 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -07001403 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001404{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001405 struct btrfs_key key;
1406 struct btrfs_extent_data_ref *ref1 = NULL;
1407 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001408 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001409 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001410 int ret = 0;
1411
1412 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001413 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1414
1415 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1416 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1417 struct btrfs_extent_data_ref);
1418 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1419 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1420 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1421 struct btrfs_shared_data_ref);
1422 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1423#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1424 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1425 struct btrfs_extent_ref_v0 *ref0;
1426 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1427 struct btrfs_extent_ref_v0);
1428 num_refs = btrfs_ref_count_v0(leaf, ref0);
1429#endif
1430 } else {
1431 BUG();
1432 }
1433
Chris Mason56bec292009-03-13 10:10:06 -04001434 BUG_ON(num_refs < refs_to_drop);
1435 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001436
Zheng Yan31840ae2008-09-23 13:14:14 -04001437 if (num_refs == 0) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001438 ret = btrfs_del_item(trans, fs_info->extent_root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -07001439 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001440 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001441 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1442 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1443 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1444 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1445#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1446 else {
1447 struct btrfs_extent_ref_v0 *ref0;
1448 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1449 struct btrfs_extent_ref_v0);
1450 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1451 }
1452#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001453 btrfs_mark_buffer_dirty(leaf);
1454 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001455 return ret;
1456}
1457
Zhaolei9ed0dea2015-08-06 22:16:24 +08001458static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001459 struct btrfs_extent_inline_ref *iref)
1460{
1461 struct btrfs_key key;
1462 struct extent_buffer *leaf;
1463 struct btrfs_extent_data_ref *ref1;
1464 struct btrfs_shared_data_ref *ref2;
1465 u32 num_refs = 0;
Liu Bo3de28d52017-08-18 15:15:19 -06001466 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001467
1468 leaf = path->nodes[0];
1469 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1470 if (iref) {
Liu Bo3de28d52017-08-18 15:15:19 -06001471 /*
1472 * If type is invalid, we should have bailed out earlier than
1473 * this call.
1474 */
1475 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
1476 ASSERT(type != BTRFS_REF_TYPE_INVALID);
1477 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001478 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1479 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1480 } else {
1481 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1482 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1483 }
1484 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1485 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1486 struct btrfs_extent_data_ref);
1487 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1488 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1489 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1490 struct btrfs_shared_data_ref);
1491 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1492#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1493 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1494 struct btrfs_extent_ref_v0 *ref0;
1495 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1496 struct btrfs_extent_ref_v0);
1497 num_refs = btrfs_ref_count_v0(leaf, ref0);
1498#endif
1499 } else {
1500 WARN_ON(1);
1501 }
1502 return num_refs;
1503}
1504
1505static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001506 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001507 struct btrfs_path *path,
1508 u64 bytenr, u64 parent,
1509 u64 root_objectid)
1510{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001511 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001512 struct btrfs_key key;
1513 int ret;
1514
1515 key.objectid = bytenr;
1516 if (parent) {
1517 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1518 key.offset = parent;
1519 } else {
1520 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1521 key.offset = root_objectid;
1522 }
1523
1524 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1525 if (ret > 0)
1526 ret = -ENOENT;
1527#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1528 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001529 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001530 key.type = BTRFS_EXTENT_REF_V0_KEY;
1531 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1532 if (ret > 0)
1533 ret = -ENOENT;
1534 }
1535#endif
1536 return ret;
1537}
1538
1539static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001540 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001541 struct btrfs_path *path,
1542 u64 bytenr, u64 parent,
1543 u64 root_objectid)
1544{
1545 struct btrfs_key key;
1546 int ret;
1547
1548 key.objectid = bytenr;
1549 if (parent) {
1550 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1551 key.offset = parent;
1552 } else {
1553 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1554 key.offset = root_objectid;
1555 }
1556
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001557 ret = btrfs_insert_empty_item(trans, fs_info->extent_root,
1558 path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001559 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001560 return ret;
1561}
1562
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001563static inline int extent_ref_type(u64 parent, u64 owner)
1564{
1565 int type;
1566 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1567 if (parent > 0)
1568 type = BTRFS_SHARED_BLOCK_REF_KEY;
1569 else
1570 type = BTRFS_TREE_BLOCK_REF_KEY;
1571 } else {
1572 if (parent > 0)
1573 type = BTRFS_SHARED_DATA_REF_KEY;
1574 else
1575 type = BTRFS_EXTENT_DATA_REF_KEY;
1576 }
1577 return type;
1578}
1579
Yan Zheng2c47e6052009-06-27 21:07:35 -04001580static int find_next_key(struct btrfs_path *path, int level,
1581 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001582
1583{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001584 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001585 if (!path->nodes[level])
1586 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001587 if (path->slots[level] + 1 >=
1588 btrfs_header_nritems(path->nodes[level]))
1589 continue;
1590 if (level == 0)
1591 btrfs_item_key_to_cpu(path->nodes[level], key,
1592 path->slots[level] + 1);
1593 else
1594 btrfs_node_key_to_cpu(path->nodes[level], key,
1595 path->slots[level] + 1);
1596 return 0;
1597 }
1598 return 1;
1599}
1600
1601/*
1602 * look for inline back ref. if back ref is found, *ref_ret is set
1603 * to the address of inline back ref, and 0 is returned.
1604 *
1605 * if back ref isn't found, *ref_ret is set to the address where it
1606 * should be inserted, and -ENOENT is returned.
1607 *
1608 * if insert is true and there are too many inline back refs, the path
1609 * points to the extent item, and -EAGAIN is returned.
1610 *
1611 * NOTE: inline back refs are ordered in the same way that back ref
1612 * items in the tree are ordered.
1613 */
1614static noinline_for_stack
1615int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001616 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001617 struct btrfs_path *path,
1618 struct btrfs_extent_inline_ref **ref_ret,
1619 u64 bytenr, u64 num_bytes,
1620 u64 parent, u64 root_objectid,
1621 u64 owner, u64 offset, int insert)
1622{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001623 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001624 struct btrfs_key key;
1625 struct extent_buffer *leaf;
1626 struct btrfs_extent_item *ei;
1627 struct btrfs_extent_inline_ref *iref;
1628 u64 flags;
1629 u64 item_size;
1630 unsigned long ptr;
1631 unsigned long end;
1632 int extra_size;
1633 int type;
1634 int want;
1635 int ret;
1636 int err = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001637 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Liu Bo3de28d52017-08-18 15:15:19 -06001638 int needed;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001639
1640 key.objectid = bytenr;
1641 key.type = BTRFS_EXTENT_ITEM_KEY;
1642 key.offset = num_bytes;
1643
1644 want = extent_ref_type(parent, owner);
1645 if (insert) {
1646 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001647 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001648 } else
1649 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001650
1651 /*
1652 * Owner is our parent level, so we can just add one to get the level
1653 * for the block we are interested in.
1654 */
1655 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1656 key.type = BTRFS_METADATA_ITEM_KEY;
1657 key.offset = owner;
1658 }
1659
1660again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001661 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1662 if (ret < 0) {
1663 err = ret;
1664 goto out;
1665 }
Josef Bacik3173a182013-03-07 14:22:04 -05001666
1667 /*
1668 * We may be a newly converted file system which still has the old fat
1669 * extent entries for metadata, so try and see if we have one of those.
1670 */
1671 if (ret > 0 && skinny_metadata) {
1672 skinny_metadata = false;
1673 if (path->slots[0]) {
1674 path->slots[0]--;
1675 btrfs_item_key_to_cpu(path->nodes[0], &key,
1676 path->slots[0]);
1677 if (key.objectid == bytenr &&
1678 key.type == BTRFS_EXTENT_ITEM_KEY &&
1679 key.offset == num_bytes)
1680 ret = 0;
1681 }
1682 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +01001683 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05001684 key.type = BTRFS_EXTENT_ITEM_KEY;
1685 key.offset = num_bytes;
1686 btrfs_release_path(path);
1687 goto again;
1688 }
1689 }
1690
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001691 if (ret && !insert) {
1692 err = -ENOENT;
1693 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301694 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -05001695 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -05001696 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001697 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001698
1699 leaf = path->nodes[0];
1700 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1701#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1702 if (item_size < sizeof(*ei)) {
1703 if (!insert) {
1704 err = -ENOENT;
1705 goto out;
1706 }
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001707 ret = convert_extent_item_v0(trans, fs_info, path, owner,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001708 extra_size);
1709 if (ret < 0) {
1710 err = ret;
1711 goto out;
1712 }
1713 leaf = path->nodes[0];
1714 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1715 }
1716#endif
1717 BUG_ON(item_size < sizeof(*ei));
1718
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001719 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1720 flags = btrfs_extent_flags(leaf, ei);
1721
1722 ptr = (unsigned long)(ei + 1);
1723 end = (unsigned long)ei + item_size;
1724
Josef Bacik3173a182013-03-07 14:22:04 -05001725 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001726 ptr += sizeof(struct btrfs_tree_block_info);
1727 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001728 }
1729
Liu Bo3de28d52017-08-18 15:15:19 -06001730 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
1731 needed = BTRFS_REF_TYPE_DATA;
1732 else
1733 needed = BTRFS_REF_TYPE_BLOCK;
1734
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001735 err = -ENOENT;
1736 while (1) {
1737 if (ptr >= end) {
1738 WARN_ON(ptr > end);
1739 break;
1740 }
1741 iref = (struct btrfs_extent_inline_ref *)ptr;
Liu Bo3de28d52017-08-18 15:15:19 -06001742 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
1743 if (type == BTRFS_REF_TYPE_INVALID) {
1744 err = -EINVAL;
1745 goto out;
1746 }
1747
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001748 if (want < type)
1749 break;
1750 if (want > type) {
1751 ptr += btrfs_extent_inline_ref_size(type);
1752 continue;
1753 }
1754
1755 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1756 struct btrfs_extent_data_ref *dref;
1757 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1758 if (match_extent_data_ref(leaf, dref, root_objectid,
1759 owner, offset)) {
1760 err = 0;
1761 break;
1762 }
1763 if (hash_extent_data_ref_item(leaf, dref) <
1764 hash_extent_data_ref(root_objectid, owner, offset))
1765 break;
1766 } else {
1767 u64 ref_offset;
1768 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1769 if (parent > 0) {
1770 if (parent == ref_offset) {
1771 err = 0;
1772 break;
1773 }
1774 if (ref_offset < parent)
1775 break;
1776 } else {
1777 if (root_objectid == ref_offset) {
1778 err = 0;
1779 break;
1780 }
1781 if (ref_offset < root_objectid)
1782 break;
1783 }
1784 }
1785 ptr += btrfs_extent_inline_ref_size(type);
1786 }
1787 if (err == -ENOENT && insert) {
1788 if (item_size + extra_size >=
1789 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1790 err = -EAGAIN;
1791 goto out;
1792 }
1793 /*
1794 * To add new inline back ref, we have to make sure
1795 * there is no corresponding back ref item.
1796 * For simplicity, we just do not add new inline back
1797 * ref if there is any kind of item for this block
1798 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001799 if (find_next_key(path, 0, &key) == 0 &&
1800 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001801 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001802 err = -EAGAIN;
1803 goto out;
1804 }
1805 }
1806 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1807out:
Yan Zheng85d41982009-06-11 08:51:10 -04001808 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001809 path->keep_locks = 0;
1810 btrfs_unlock_up_safe(path, 1);
1811 }
1812 return err;
1813}
1814
1815/*
1816 * helper to add new inline back ref
1817 */
1818static noinline_for_stack
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001819void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001820 struct btrfs_path *path,
1821 struct btrfs_extent_inline_ref *iref,
1822 u64 parent, u64 root_objectid,
1823 u64 owner, u64 offset, int refs_to_add,
1824 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001825{
1826 struct extent_buffer *leaf;
1827 struct btrfs_extent_item *ei;
1828 unsigned long ptr;
1829 unsigned long end;
1830 unsigned long item_offset;
1831 u64 refs;
1832 int size;
1833 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001834
1835 leaf = path->nodes[0];
1836 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1837 item_offset = (unsigned long)iref - (unsigned long)ei;
1838
1839 type = extent_ref_type(parent, owner);
1840 size = btrfs_extent_inline_ref_size(type);
1841
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001842 btrfs_extend_item(fs_info, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001843
1844 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1845 refs = btrfs_extent_refs(leaf, ei);
1846 refs += refs_to_add;
1847 btrfs_set_extent_refs(leaf, ei, refs);
1848 if (extent_op)
1849 __run_delayed_extent_op(extent_op, leaf, ei);
1850
1851 ptr = (unsigned long)ei + item_offset;
1852 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1853 if (ptr < end - size)
1854 memmove_extent_buffer(leaf, ptr + size, ptr,
1855 end - size - ptr);
1856
1857 iref = (struct btrfs_extent_inline_ref *)ptr;
1858 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1859 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1860 struct btrfs_extent_data_ref *dref;
1861 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1862 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1863 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1864 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1865 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1866 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1867 struct btrfs_shared_data_ref *sref;
1868 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1869 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1870 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1871 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1872 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1873 } else {
1874 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1875 }
1876 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001877}
1878
1879static int lookup_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001880 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001881 struct btrfs_path *path,
1882 struct btrfs_extent_inline_ref **ref_ret,
1883 u64 bytenr, u64 num_bytes, u64 parent,
1884 u64 root_objectid, u64 owner, u64 offset)
1885{
1886 int ret;
1887
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001888 ret = lookup_inline_extent_backref(trans, fs_info, path, ref_ret,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001889 bytenr, num_bytes, parent,
1890 root_objectid, owner, offset, 0);
1891 if (ret != -ENOENT)
1892 return ret;
1893
David Sterbab3b4aa72011-04-21 01:20:15 +02001894 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001895 *ref_ret = NULL;
1896
1897 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001898 ret = lookup_tree_block_ref(trans, fs_info, path, bytenr,
1899 parent, root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001900 } else {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001901 ret = lookup_extent_data_ref(trans, fs_info, path, bytenr,
1902 parent, root_objectid, owner,
1903 offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001904 }
1905 return ret;
1906}
1907
1908/*
1909 * helper to update/remove inline back ref
1910 */
1911static noinline_for_stack
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001912void update_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001913 struct btrfs_path *path,
1914 struct btrfs_extent_inline_ref *iref,
1915 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001916 struct btrfs_delayed_extent_op *extent_op,
1917 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001918{
1919 struct extent_buffer *leaf;
1920 struct btrfs_extent_item *ei;
1921 struct btrfs_extent_data_ref *dref = NULL;
1922 struct btrfs_shared_data_ref *sref = NULL;
1923 unsigned long ptr;
1924 unsigned long end;
1925 u32 item_size;
1926 int size;
1927 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001928 u64 refs;
1929
1930 leaf = path->nodes[0];
1931 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1932 refs = btrfs_extent_refs(leaf, ei);
1933 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1934 refs += refs_to_mod;
1935 btrfs_set_extent_refs(leaf, ei, refs);
1936 if (extent_op)
1937 __run_delayed_extent_op(extent_op, leaf, ei);
1938
Liu Bo3de28d52017-08-18 15:15:19 -06001939 /*
1940 * If type is invalid, we should have bailed out after
1941 * lookup_inline_extent_backref().
1942 */
1943 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1944 ASSERT(type != BTRFS_REF_TYPE_INVALID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001945
1946 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1947 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1948 refs = btrfs_extent_data_ref_count(leaf, dref);
1949 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1950 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1951 refs = btrfs_shared_data_ref_count(leaf, sref);
1952 } else {
1953 refs = 1;
1954 BUG_ON(refs_to_mod != -1);
1955 }
1956
1957 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1958 refs += refs_to_mod;
1959
1960 if (refs > 0) {
1961 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1962 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1963 else
1964 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1965 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001966 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001967 size = btrfs_extent_inline_ref_size(type);
1968 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1969 ptr = (unsigned long)iref;
1970 end = (unsigned long)ei + item_size;
1971 if (ptr + size < end)
1972 memmove_extent_buffer(leaf, ptr, ptr + size,
1973 end - ptr - size);
1974 item_size -= size;
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001975 btrfs_truncate_item(fs_info, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001976 }
1977 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001978}
1979
1980static noinline_for_stack
1981int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001982 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001983 struct btrfs_path *path,
1984 u64 bytenr, u64 num_bytes, u64 parent,
1985 u64 root_objectid, u64 owner,
1986 u64 offset, int refs_to_add,
1987 struct btrfs_delayed_extent_op *extent_op)
1988{
1989 struct btrfs_extent_inline_ref *iref;
1990 int ret;
1991
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001992 ret = lookup_inline_extent_backref(trans, fs_info, path, &iref,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001993 bytenr, num_bytes, parent,
1994 root_objectid, owner, offset, 1);
1995 if (ret == 0) {
1996 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001997 update_inline_extent_backref(fs_info, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001998 refs_to_add, extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001999 } else if (ret == -ENOENT) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002000 setup_inline_extent_backref(fs_info, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002001 root_objectid, owner, offset,
2002 refs_to_add, extent_op);
2003 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002004 }
2005 return ret;
2006}
2007
2008static int insert_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002009 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002010 struct btrfs_path *path,
2011 u64 bytenr, u64 parent, u64 root_objectid,
2012 u64 owner, u64 offset, int refs_to_add)
2013{
2014 int ret;
2015 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
2016 BUG_ON(refs_to_add != 1);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002017 ret = insert_tree_block_ref(trans, fs_info, path, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002018 parent, root_objectid);
2019 } else {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002020 ret = insert_extent_data_ref(trans, fs_info, path, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002021 parent, root_objectid,
2022 owner, offset, refs_to_add);
2023 }
2024 return ret;
2025}
2026
2027static int remove_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002028 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002029 struct btrfs_path *path,
2030 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07002031 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002032{
Jeff Mahoney143bede2012-03-01 14:56:26 +01002033 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002034
2035 BUG_ON(!is_data && refs_to_drop != 1);
2036 if (iref) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002037 update_inline_extent_backref(fs_info, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07002038 -refs_to_drop, NULL, last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002039 } else if (is_data) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002040 ret = remove_extent_data_ref(trans, fs_info, path, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07002041 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002042 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07002043 *last_ref = 1;
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002044 ret = btrfs_del_item(trans, fs_info->extent_root, path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002045 }
2046 return ret;
2047}
2048
Jeff Mahoney86557862015-06-15 09:41:16 -04002049#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002050static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
2051 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05002052{
Jeff Mahoney86557862015-06-15 09:41:16 -04002053 int j, ret = 0;
2054 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002055 u64 aligned_start = ALIGN(start, 1 << 9);
2056
2057 if (WARN_ON(start != aligned_start)) {
2058 len -= aligned_start - start;
2059 len = round_down(len, 1 << 9);
2060 start = aligned_start;
2061 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002062
2063 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04002064
2065 if (!len)
2066 return 0;
2067
2068 end = start + len;
2069 bytes_left = len;
2070
2071 /* Skip any superblocks on this device. */
2072 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
2073 u64 sb_start = btrfs_sb_offset(j);
2074 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
2075 u64 size = sb_start - start;
2076
2077 if (!in_range(sb_start, start, bytes_left) &&
2078 !in_range(sb_end, start, bytes_left) &&
2079 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
2080 continue;
2081
2082 /*
2083 * Superblock spans beginning of range. Adjust start and
2084 * try again.
2085 */
2086 if (sb_start <= start) {
2087 start += sb_end - start;
2088 if (start > end) {
2089 bytes_left = 0;
2090 break;
2091 }
2092 bytes_left = end - start;
2093 continue;
2094 }
2095
2096 if (size) {
2097 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
2098 GFP_NOFS, 0);
2099 if (!ret)
2100 *discarded_bytes += size;
2101 else if (ret != -EOPNOTSUPP)
2102 return ret;
2103 }
2104
2105 start = sb_end;
2106 if (start > end) {
2107 bytes_left = 0;
2108 break;
2109 }
2110 bytes_left = end - start;
2111 }
2112
2113 if (bytes_left) {
2114 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002115 GFP_NOFS, 0);
2116 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04002117 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002118 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002119 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05002120}
Chris Mason15916de2008-11-19 21:17:22 -05002121
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002122int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00002123 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05002124{
Liu Hui1f3c79a2009-01-05 15:57:51 -05002125 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00002126 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02002127 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002128
Christoph Hellwige244a0a2009-10-14 09:24:59 -04002129
Filipe Manana29992412016-05-27 17:42:05 +01002130 /*
2131 * Avoid races with device replace and make sure our bbio has devices
2132 * associated to its stripes that don't go away while we are discarding.
2133 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002134 btrfs_bio_counter_inc_blocked(fs_info);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002135 /* Tell the block device(s) that the sectors can be discarded */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002136 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
2137 &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002138 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05002139 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02002140 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002141 int i;
2142
Liu Hui1f3c79a2009-01-05 15:57:51 -05002143
Jan Schmidta1d3c472011-08-04 17:15:33 +02002144 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002145 u64 bytes;
Anand Jain38b5f682017-11-29 18:53:43 +08002146 struct request_queue *req_q;
2147
Filipe Manana627e0872018-01-30 18:40:22 +00002148 if (!stripe->dev->bdev) {
2149 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
2150 continue;
2151 }
Anand Jain38b5f682017-11-29 18:53:43 +08002152 req_q = bdev_get_queue(stripe->dev->bdev);
2153 if (!blk_queue_discard(req_q))
Josef Bacikd5e20032011-08-04 14:52:27 +00002154 continue;
2155
Li Dongyang5378e602011-03-24 10:24:27 +00002156 ret = btrfs_issue_discard(stripe->dev->bdev,
2157 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002158 stripe->length,
2159 &bytes);
Li Dongyang5378e602011-03-24 10:24:27 +00002160 if (!ret)
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002161 discarded_bytes += bytes;
Li Dongyang5378e602011-03-24 10:24:27 +00002162 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002163 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00002164
2165 /*
2166 * Just in case we get back EOPNOTSUPP for some reason,
2167 * just ignore the return value so we don't screw up
2168 * people calling discard_extent.
2169 */
2170 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002171 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08002172 btrfs_put_bbio(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002173 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002174 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00002175
2176 if (actual_bytes)
2177 *actual_bytes = discarded_bytes;
2178
Liu Hui1f3c79a2009-01-05 15:57:51 -05002179
David Woodhouse53b381b2013-01-29 18:40:14 -05002180 if (ret == -EOPNOTSUPP)
2181 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002182 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002183}
2184
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002185/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002186int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002187 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002188 u64 bytenr, u64 num_bytes, u64 parent,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002189 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04002190{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002191 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07002192 int old_ref_mod, new_ref_mod;
Zheng Yan31840ae2008-09-23 13:14:14 -04002193 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002194
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002195 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
2196 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04002197
Josef Bacikfd708b82017-09-29 15:43:50 -04002198 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent, root_objectid,
2199 owner, offset, BTRFS_ADD_DELAYED_REF);
2200
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002201 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002202 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07002203 num_bytes, parent,
2204 root_objectid, (int)owner,
2205 BTRFS_ADD_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07002206 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002207 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002208 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07002209 num_bytes, parent,
2210 root_objectid, owner, offset,
Omar Sandovald7eae342017-06-06 16:45:31 -07002211 0, BTRFS_ADD_DELAYED_REF,
2212 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002213 }
Omar Sandovald7eae342017-06-06 16:45:31 -07002214
2215 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0)
2216 add_pinned_bytes(fs_info, -num_bytes, owner, root_objectid);
2217
Zheng Yan31840ae2008-09-23 13:14:14 -04002218 return ret;
2219}
2220
Chris Mason925baed2008-06-25 16:01:30 -04002221static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002222 struct btrfs_fs_info *fs_info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002223 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002224 u64 parent, u64 root_objectid,
2225 u64 owner, u64 offset, int refs_to_add,
2226 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04002227{
Chris Mason5caf2a02007-04-02 11:20:42 -04002228 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002229 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04002230 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07002231 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002232 u64 bytenr = node->bytenr;
2233 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002234 u64 refs;
2235 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05002236
Chris Mason5caf2a02007-04-02 11:20:42 -04002237 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002238 if (!path)
2239 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04002240
David Sterbae4058b52015-11-27 16:31:35 +01002241 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002242 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002243 /* this will setup the path even if it fails to insert the back ref */
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002244 ret = insert_inline_extent_backref(trans, fs_info, path, bytenr,
2245 num_bytes, parent, root_objectid,
2246 owner, offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002247 refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08002248 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002249 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07002250
2251 /*
2252 * Ok we had -EAGAIN which means we didn't have space to insert and
2253 * inline extent ref, so just update the reference count and add a
2254 * normal backref.
2255 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002256 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07002257 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002258 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2259 refs = btrfs_extent_refs(leaf, item);
2260 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2261 if (extent_op)
2262 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002263
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002264 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002265 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002266
David Sterbae4058b52015-11-27 16:31:35 +01002267 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002268 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04002269 /* now insert the actual backref */
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002270 ret = insert_extent_backref(trans, fs_info, path, bytenr, parent,
2271 root_objectid, owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002272 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04002273 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002274out:
Chris Mason74493f72007-12-11 09:25:06 -05002275 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08002276 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002277}
2278
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002279static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002280 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002281 struct btrfs_delayed_ref_node *node,
2282 struct btrfs_delayed_extent_op *extent_op,
2283 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002284{
Chris Mason56bec292009-03-13 10:10:06 -04002285 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002286 struct btrfs_delayed_data_ref *ref;
2287 struct btrfs_key ins;
2288 u64 parent = 0;
2289 u64 ref_root = 0;
2290 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002291
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002292 ins.objectid = node->bytenr;
2293 ins.offset = node->num_bytes;
2294 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002295
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002296 ref = btrfs_delayed_node_to_data_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002297 trace_run_delayed_data_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002298
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002299 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2300 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002301 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002302
2303 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002304 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002305 flags |= extent_op->flags_to_set;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002306 ret = alloc_reserved_file_extent(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002307 parent, ref_root, flags,
2308 ref->objectid, ref->offset,
2309 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002310 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002311 ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002312 ref_root, ref->objectid,
2313 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002314 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002315 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002316 ret = __btrfs_free_extent(trans, fs_info, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002317 ref_root, ref->objectid,
2318 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002319 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002320 } else {
2321 BUG();
2322 }
Chris Mason56bec292009-03-13 10:10:06 -04002323 return ret;
2324}
2325
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002326static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2327 struct extent_buffer *leaf,
2328 struct btrfs_extent_item *ei)
2329{
2330 u64 flags = btrfs_extent_flags(leaf, ei);
2331 if (extent_op->update_flags) {
2332 flags |= extent_op->flags_to_set;
2333 btrfs_set_extent_flags(leaf, ei, flags);
2334 }
2335
2336 if (extent_op->update_key) {
2337 struct btrfs_tree_block_info *bi;
2338 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2339 bi = (struct btrfs_tree_block_info *)(ei + 1);
2340 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2341 }
2342}
2343
2344static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002345 struct btrfs_fs_info *fs_info,
Josef Bacikd2788502017-09-29 15:43:57 -04002346 struct btrfs_delayed_ref_head *head,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002347 struct btrfs_delayed_extent_op *extent_op)
2348{
2349 struct btrfs_key key;
2350 struct btrfs_path *path;
2351 struct btrfs_extent_item *ei;
2352 struct extent_buffer *leaf;
2353 u32 item_size;
2354 int ret;
2355 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002356 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002357
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002358 if (trans->aborted)
2359 return 0;
2360
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002361 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05002362 metadata = 0;
2363
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002364 path = btrfs_alloc_path();
2365 if (!path)
2366 return -ENOMEM;
2367
Josef Bacikd2788502017-09-29 15:43:57 -04002368 key.objectid = head->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002369
Josef Bacik3173a182013-03-07 14:22:04 -05002370 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002371 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002372 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002373 } else {
2374 key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacikd2788502017-09-29 15:43:57 -04002375 key.offset = head->num_bytes;
Josef Bacik3173a182013-03-07 14:22:04 -05002376 }
2377
2378again:
David Sterbae4058b52015-11-27 16:31:35 +01002379 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002380 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002381 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002382 if (ret < 0) {
2383 err = ret;
2384 goto out;
2385 }
2386 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002387 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002388 if (path->slots[0] > 0) {
2389 path->slots[0]--;
2390 btrfs_item_key_to_cpu(path->nodes[0], &key,
2391 path->slots[0]);
Josef Bacikd2788502017-09-29 15:43:57 -04002392 if (key.objectid == head->bytenr &&
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002393 key.type == BTRFS_EXTENT_ITEM_KEY &&
Josef Bacikd2788502017-09-29 15:43:57 -04002394 key.offset == head->num_bytes)
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002395 ret = 0;
2396 }
2397 if (ret > 0) {
2398 btrfs_release_path(path);
2399 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002400
Josef Bacikd2788502017-09-29 15:43:57 -04002401 key.objectid = head->bytenr;
2402 key.offset = head->num_bytes;
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002403 key.type = BTRFS_EXTENT_ITEM_KEY;
2404 goto again;
2405 }
2406 } else {
2407 err = -EIO;
2408 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002409 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002410 }
2411
2412 leaf = path->nodes[0];
2413 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2414#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2415 if (item_size < sizeof(*ei)) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002416 ret = convert_extent_item_v0(trans, fs_info, path, (u64)-1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002417 if (ret < 0) {
2418 err = ret;
2419 goto out;
2420 }
2421 leaf = path->nodes[0];
2422 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2423 }
2424#endif
2425 BUG_ON(item_size < sizeof(*ei));
2426 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2427 __run_delayed_extent_op(extent_op, leaf, ei);
2428
2429 btrfs_mark_buffer_dirty(leaf);
2430out:
2431 btrfs_free_path(path);
2432 return err;
2433}
2434
2435static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002436 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002437 struct btrfs_delayed_ref_node *node,
2438 struct btrfs_delayed_extent_op *extent_op,
2439 int insert_reserved)
2440{
2441 int ret = 0;
2442 struct btrfs_delayed_tree_ref *ref;
2443 struct btrfs_key ins;
2444 u64 parent = 0;
2445 u64 ref_root = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002446 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002447
2448 ref = btrfs_delayed_node_to_tree_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002449 trace_run_delayed_tree_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002450
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002451 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2452 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002453 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002454
Josef Bacik3173a182013-03-07 14:22:04 -05002455 ins.objectid = node->bytenr;
2456 if (skinny_metadata) {
2457 ins.offset = ref->level;
2458 ins.type = BTRFS_METADATA_ITEM_KEY;
2459 } else {
2460 ins.offset = node->num_bytes;
2461 ins.type = BTRFS_EXTENT_ITEM_KEY;
2462 }
2463
Liu Bo02794222016-09-14 19:19:05 -07002464 if (node->ref_mod != 1) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002465 btrfs_err(fs_info,
Liu Bo02794222016-09-14 19:19:05 -07002466 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
2467 node->bytenr, node->ref_mod, node->action, ref_root,
2468 parent);
2469 return -EIO;
2470 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002471 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002472 BUG_ON(!extent_op || !extent_op->update_flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002473 ret = alloc_reserved_tree_block(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002474 parent, ref_root,
2475 extent_op->flags_to_set,
2476 &extent_op->key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002477 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002478 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002479 ret = __btrfs_inc_extent_ref(trans, fs_info, node,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002480 parent, ref_root,
2481 ref->level, 0, 1,
Josef Bacikfcebe452014-05-13 17:30:47 -07002482 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002483 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002484 ret = __btrfs_free_extent(trans, fs_info, node,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002485 parent, ref_root,
2486 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002487 } else {
2488 BUG();
2489 }
2490 return ret;
2491}
2492
Chris Mason56bec292009-03-13 10:10:06 -04002493/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002494static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002495 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002496 struct btrfs_delayed_ref_node *node,
2497 struct btrfs_delayed_extent_op *extent_op,
2498 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002499{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002500 int ret = 0;
2501
Josef Bacik857cc2f2013-10-07 15:21:08 -04002502 if (trans->aborted) {
2503 if (insert_reserved)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002504 btrfs_pin_extent(fs_info, node->bytenr,
Josef Bacik857cc2f2013-10-07 15:21:08 -04002505 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002506 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002507 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002508
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002509 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2510 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002511 ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002512 insert_reserved);
2513 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2514 node->type == BTRFS_SHARED_DATA_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002515 ret = run_delayed_data_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002516 insert_reserved);
2517 else
2518 BUG();
2519 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002520}
2521
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002522static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04002523select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002524{
Filipe Mananacffc3372015-07-09 13:13:44 +01002525 struct btrfs_delayed_ref_node *ref;
2526
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002527 if (RB_EMPTY_ROOT(&head->ref_tree))
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002528 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002529
Filipe Mananacffc3372015-07-09 13:13:44 +01002530 /*
2531 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2532 * This is to prevent a ref count from going down to zero, which deletes
2533 * the extent item from the extent tree, when there still are references
2534 * to add, which would fail because they would not find the extent item.
2535 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002536 if (!list_empty(&head->ref_add_list))
2537 return list_first_entry(&head->ref_add_list,
2538 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01002539
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002540 ref = rb_entry(rb_first(&head->ref_tree),
2541 struct btrfs_delayed_ref_node, ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002542 ASSERT(list_empty(&ref->add_list));
2543 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04002544}
2545
Josef Bacik2eadaa22017-09-29 15:43:52 -04002546static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
2547 struct btrfs_delayed_ref_head *head)
2548{
2549 spin_lock(&delayed_refs->lock);
2550 head->processing = 0;
2551 delayed_refs->num_heads_ready++;
2552 spin_unlock(&delayed_refs->lock);
2553 btrfs_delayed_ref_unlock(head);
2554}
2555
Josef Bacikb00e6252017-09-29 15:43:53 -04002556static int cleanup_extent_op(struct btrfs_trans_handle *trans,
2557 struct btrfs_fs_info *fs_info,
2558 struct btrfs_delayed_ref_head *head)
2559{
2560 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
2561 int ret;
2562
2563 if (!extent_op)
2564 return 0;
2565 head->extent_op = NULL;
2566 if (head->must_insert_reserved) {
2567 btrfs_free_delayed_extent_op(extent_op);
2568 return 0;
2569 }
2570 spin_unlock(&head->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002571 ret = run_delayed_extent_op(trans, fs_info, head, extent_op);
Josef Bacikb00e6252017-09-29 15:43:53 -04002572 btrfs_free_delayed_extent_op(extent_op);
2573 return ret ? ret : 1;
2574}
2575
Josef Bacik194ab0b2017-09-29 15:43:54 -04002576static int cleanup_ref_head(struct btrfs_trans_handle *trans,
2577 struct btrfs_fs_info *fs_info,
2578 struct btrfs_delayed_ref_head *head)
2579{
2580 struct btrfs_delayed_ref_root *delayed_refs;
2581 int ret;
2582
2583 delayed_refs = &trans->transaction->delayed_refs;
2584
2585 ret = cleanup_extent_op(trans, fs_info, head);
2586 if (ret < 0) {
2587 unselect_delayed_ref_head(delayed_refs, head);
2588 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
2589 return ret;
2590 } else if (ret) {
2591 return ret;
2592 }
2593
2594 /*
2595 * Need to drop our head ref lock and re-acquire the delayed ref lock
2596 * and then re-check to make sure nobody got added.
2597 */
2598 spin_unlock(&head->lock);
2599 spin_lock(&delayed_refs->lock);
2600 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002601 if (!RB_EMPTY_ROOT(&head->ref_tree) || head->extent_op) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04002602 spin_unlock(&head->lock);
2603 spin_unlock(&delayed_refs->lock);
2604 return 1;
2605 }
Josef Bacik194ab0b2017-09-29 15:43:54 -04002606 delayed_refs->num_heads--;
2607 rb_erase(&head->href_node, &delayed_refs->href_root);
Josef Bacikd2788502017-09-29 15:43:57 -04002608 RB_CLEAR_NODE(&head->href_node);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002609 spin_unlock(&delayed_refs->lock);
Josef Bacikc1103f72017-09-29 15:43:56 -04002610 spin_unlock(&head->lock);
2611 atomic_dec(&delayed_refs->num_entries);
2612
Josef Bacikd2788502017-09-29 15:43:57 -04002613 trace_run_delayed_ref_head(fs_info, head, 0);
Josef Bacikc1103f72017-09-29 15:43:56 -04002614
2615 if (head->total_ref_mod < 0) {
2616 struct btrfs_block_group_cache *cache;
2617
Josef Bacikd2788502017-09-29 15:43:57 -04002618 cache = btrfs_lookup_block_group(fs_info, head->bytenr);
Josef Bacikc1103f72017-09-29 15:43:56 -04002619 ASSERT(cache);
2620 percpu_counter_add(&cache->space_info->total_bytes_pinned,
Josef Bacikd2788502017-09-29 15:43:57 -04002621 -head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04002622 btrfs_put_block_group(cache);
2623
2624 if (head->is_data) {
2625 spin_lock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002626 delayed_refs->pending_csums -= head->num_bytes;
Josef Bacikc1103f72017-09-29 15:43:56 -04002627 spin_unlock(&delayed_refs->lock);
2628 }
2629 }
2630
2631 if (head->must_insert_reserved) {
Josef Bacikd2788502017-09-29 15:43:57 -04002632 btrfs_pin_extent(fs_info, head->bytenr,
2633 head->num_bytes, 1);
Josef Bacikc1103f72017-09-29 15:43:56 -04002634 if (head->is_data) {
Josef Bacikd2788502017-09-29 15:43:57 -04002635 ret = btrfs_del_csums(trans, fs_info, head->bytenr,
2636 head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04002637 }
2638 }
2639
2640 /* Also free its reserved qgroup space */
2641 btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
2642 head->qgroup_reserved);
2643 btrfs_delayed_ref_unlock(head);
Josef Bacikd2788502017-09-29 15:43:57 -04002644 btrfs_put_delayed_ref_head(head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002645 return 0;
2646}
2647
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002648/*
2649 * Returns 0 on success or if called with an already aborted transaction.
2650 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2651 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002652static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacikd7df2c72014-01-23 09:21:38 -05002653 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002654{
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002655 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002656 struct btrfs_delayed_ref_root *delayed_refs;
2657 struct btrfs_delayed_ref_node *ref;
2658 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002659 struct btrfs_delayed_extent_op *extent_op;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002660 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002661 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002662 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002663 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002664 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002665
2666 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002667 while (1) {
2668 if (!locked_ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002669 if (count >= nr)
Chris Mason56bec292009-03-13 10:10:06 -04002670 break;
Chris Mason56bec292009-03-13 10:10:06 -04002671
Josef Bacikd7df2c72014-01-23 09:21:38 -05002672 spin_lock(&delayed_refs->lock);
2673 locked_ref = btrfs_select_ref_head(trans);
2674 if (!locked_ref) {
2675 spin_unlock(&delayed_refs->lock);
2676 break;
2677 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002678
2679 /* grab the lock that says we are going to process
2680 * all the refs for this head */
2681 ret = btrfs_delayed_ref_lock(trans, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002682 spin_unlock(&delayed_refs->lock);
Chris Masonc3e69d52009-03-13 10:17:05 -04002683 /*
2684 * we may have dropped the spin lock to get the head
2685 * mutex lock, and that might have given someone else
2686 * time to free the head. If that's true, it has been
2687 * removed from our list and we can move on.
2688 */
2689 if (ret == -EAGAIN) {
2690 locked_ref = NULL;
2691 count++;
2692 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002693 }
2694 }
2695
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002696 /*
2697 * We need to try and merge add/drops of the same ref since we
2698 * can run into issues with relocate dropping the implicit ref
2699 * and then it being added back again before the drop can
2700 * finish. If we merged anything we need to re-loop so we can
2701 * get a good ref.
2702 * Or we can get node references of the same type that weren't
2703 * merged when created due to bumps in the tree mod seq, and
2704 * we need to merge them to prevent adding an inline extent
2705 * backref before dropping it (triggering a BUG_ON at
2706 * insert_inline_extent_backref()).
2707 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002708 spin_lock(&locked_ref->lock);
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002709 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2710 locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002711
2712 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002713 * locked_ref is the head node, so we have to go one
2714 * node back for any delayed ref updates
2715 */
2716 ref = select_delayed_ref(locked_ref);
2717
2718 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002719 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002720 spin_unlock(&locked_ref->lock);
Josef Bacik2eadaa22017-09-29 15:43:52 -04002721 unselect_delayed_ref_head(delayed_refs, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002722 locked_ref = NULL;
Arne Jansend1270cd2011-09-13 15:16:43 +02002723 cond_resched();
Josef Bacik27a377d2014-02-07 13:57:59 -05002724 count++;
Arne Jansend1270cd2011-09-13 15:16:43 +02002725 continue;
2726 }
2727
Josef Bacikc1103f72017-09-29 15:43:56 -04002728 /*
2729 * We're done processing refs in this ref_head, clean everything
2730 * up and move on to the next ref_head.
2731 */
Chris Mason56bec292009-03-13 10:10:06 -04002732 if (!ref) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04002733 ret = cleanup_ref_head(trans, fs_info, locked_ref);
2734 if (ret > 0 ) {
Josef Bacikb00e6252017-09-29 15:43:53 -04002735 /* We dropped our lock, we need to loop. */
2736 ret = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002737 continue;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002738 } else if (ret) {
2739 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002740 }
Josef Bacikc1103f72017-09-29 15:43:56 -04002741 locked_ref = NULL;
2742 count++;
2743 continue;
2744 }
Chris Mason56bec292009-03-13 10:10:06 -04002745
Josef Bacikc1103f72017-09-29 15:43:56 -04002746 actual_count++;
2747 ref->in_tree = 0;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002748 rb_erase(&ref->ref_node, &locked_ref->ref_tree);
2749 RB_CLEAR_NODE(&ref->ref_node);
Josef Bacikc1103f72017-09-29 15:43:56 -04002750 if (!list_empty(&ref->add_list))
2751 list_del(&ref->add_list);
2752 /*
2753 * When we play the delayed ref, also correct the ref_mod on
2754 * head
2755 */
2756 switch (ref->action) {
2757 case BTRFS_ADD_DELAYED_REF:
2758 case BTRFS_ADD_DELAYED_EXTENT:
Josef Bacikd2788502017-09-29 15:43:57 -04002759 locked_ref->ref_mod -= ref->ref_mod;
Josef Bacikc1103f72017-09-29 15:43:56 -04002760 break;
2761 case BTRFS_DROP_DELAYED_REF:
Josef Bacikd2788502017-09-29 15:43:57 -04002762 locked_ref->ref_mod += ref->ref_mod;
Josef Bacikc1103f72017-09-29 15:43:56 -04002763 break;
2764 default:
2765 WARN_ON(1);
Arne Jansen22cd2e72012-08-09 00:16:53 -06002766 }
Josef Bacik1ce7a5e2017-09-29 15:43:55 -04002767 atomic_dec(&delayed_refs->num_entries);
2768
Josef Bacikb00e6252017-09-29 15:43:53 -04002769 /*
2770 * Record the must-insert_reserved flag before we drop the spin
2771 * lock.
2772 */
2773 must_insert_reserved = locked_ref->must_insert_reserved;
2774 locked_ref->must_insert_reserved = 0;
2775
2776 extent_op = locked_ref->extent_op;
2777 locked_ref->extent_op = NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002778 spin_unlock(&locked_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002779
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002780 ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002781 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002782
Miao Xie78a61842012-11-21 02:21:28 +00002783 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002784 if (ret) {
Josef Bacik2eadaa22017-09-29 15:43:52 -04002785 unselect_delayed_ref_head(delayed_refs, locked_ref);
Miao Xie093486c2012-12-19 08:10:10 +00002786 btrfs_put_delayed_ref(ref);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002787 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
2788 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002789 return ret;
2790 }
2791
Miao Xie093486c2012-12-19 08:10:10 +00002792 btrfs_put_delayed_ref(ref);
2793 count++;
Chris Mason1887be62009-03-13 10:11:24 -04002794 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002795 }
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002796
2797 /*
2798 * We don't want to include ref heads since we can have empty ref heads
2799 * and those will drastically skew our runtime down since we just do
2800 * accounting, no actual extent tree updates.
2801 */
2802 if (actual_count > 0) {
2803 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2804 u64 avg;
2805
2806 /*
2807 * We weigh the current average higher than our current runtime
2808 * to avoid large swings in the average.
2809 */
2810 spin_lock(&delayed_refs->lock);
2811 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002812 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002813 spin_unlock(&delayed_refs->lock);
2814 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002815 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002816}
2817
Arne Jansen709c0482011-09-12 12:22:57 +02002818#ifdef SCRAMBLE_DELAYED_REFS
2819/*
2820 * Normally delayed refs get processed in ascending bytenr order. This
2821 * correlates in most cases to the order added. To expose dependencies on this
2822 * order, we start to process the tree in the middle instead of the beginning
2823 */
2824static u64 find_middle(struct rb_root *root)
2825{
2826 struct rb_node *n = root->rb_node;
2827 struct btrfs_delayed_ref_node *entry;
2828 int alt = 1;
2829 u64 middle;
2830 u64 first = 0, last = 0;
2831
2832 n = rb_first(root);
2833 if (n) {
2834 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2835 first = entry->bytenr;
2836 }
2837 n = rb_last(root);
2838 if (n) {
2839 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2840 last = entry->bytenr;
2841 }
2842 n = root->rb_node;
2843
2844 while (n) {
2845 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2846 WARN_ON(!entry->in_tree);
2847
2848 middle = entry->bytenr;
2849
2850 if (alt)
2851 n = n->rb_left;
2852 else
2853 n = n->rb_right;
2854
2855 alt = 1 - alt;
2856 }
2857 return middle;
2858}
2859#endif
2860
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002861static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002862{
2863 u64 num_bytes;
2864
2865 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2866 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002867 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002868 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2869
2870 /*
2871 * 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 -04002872 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002873 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002874 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002875}
2876
Josef Bacik12621332015-02-03 07:50:16 -08002877/*
2878 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2879 * would require to store the csums for that many bytes.
2880 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002881u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002882{
2883 u64 csum_size;
2884 u64 num_csums_per_leaf;
2885 u64 num_csums;
2886
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002887 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002888 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002889 (u64)btrfs_super_csum_size(fs_info->super_copy));
2890 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002891 num_csums += num_csums_per_leaf - 1;
2892 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2893 return num_csums;
2894}
2895
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002896int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002897 struct btrfs_fs_info *fs_info)
Josef Bacik1be41b72013-06-12 13:56:06 -04002898{
2899 struct btrfs_block_rsv *global_rsv;
2900 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
Josef Bacik12621332015-02-03 07:50:16 -08002901 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
David Sterba165c8b02017-11-08 02:12:57 +01002902 unsigned int num_dirty_bgs = trans->transaction->num_dirty_bgs;
Josef Bacikcb723e42015-02-18 08:06:57 -08002903 u64 num_bytes, num_dirty_bgs_bytes;
Josef Bacik1be41b72013-06-12 13:56:06 -04002904 int ret = 0;
2905
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002906 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002907 num_heads = heads_to_leaves(fs_info, num_heads);
Josef Bacik1be41b72013-06-12 13:56:06 -04002908 if (num_heads > 1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002909 num_bytes += (num_heads - 1) * fs_info->nodesize;
Josef Bacik1be41b72013-06-12 13:56:06 -04002910 num_bytes <<= 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002911 num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
2912 fs_info->nodesize;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002913 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
Josef Bacikcb723e42015-02-18 08:06:57 -08002914 num_dirty_bgs);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002915 global_rsv = &fs_info->global_block_rsv;
Josef Bacik1be41b72013-06-12 13:56:06 -04002916
2917 /*
2918 * If we can't allocate any more chunks lets make sure we have _lots_ of
2919 * wiggle room since running delayed refs can create more delayed refs.
2920 */
Josef Bacikcb723e42015-02-18 08:06:57 -08002921 if (global_rsv->space_info->full) {
2922 num_dirty_bgs_bytes <<= 1;
Josef Bacik1be41b72013-06-12 13:56:06 -04002923 num_bytes <<= 1;
Josef Bacikcb723e42015-02-18 08:06:57 -08002924 }
Josef Bacik1be41b72013-06-12 13:56:06 -04002925
2926 spin_lock(&global_rsv->lock);
Josef Bacikcb723e42015-02-18 08:06:57 -08002927 if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
Josef Bacik1be41b72013-06-12 13:56:06 -04002928 ret = 1;
2929 spin_unlock(&global_rsv->lock);
2930 return ret;
2931}
2932
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002933int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002934 struct btrfs_fs_info *fs_info)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002935{
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002936 u64 num_entries =
2937 atomic_read(&trans->transaction->delayed_refs.num_entries);
2938 u64 avg_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002939 u64 val;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002940
2941 smp_mb();
2942 avg_runtime = fs_info->avg_delayed_ref_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002943 val = num_entries * avg_runtime;
Wang Xiaoguangdc1a90c2016-10-26 15:23:01 +08002944 if (val >= NSEC_PER_SEC)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002945 return 1;
Chris Masona79b7d42014-05-22 16:18:52 -07002946 if (val >= NSEC_PER_SEC / 2)
2947 return 2;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002948
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002949 return btrfs_check_space_for_delayed_refs(trans, fs_info);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002950}
2951
Chris Masona79b7d42014-05-22 16:18:52 -07002952struct async_delayed_refs {
2953 struct btrfs_root *root;
Josef Bacik31b96552016-04-11 17:37:40 -04002954 u64 transid;
Chris Masona79b7d42014-05-22 16:18:52 -07002955 int count;
2956 int error;
2957 int sync;
2958 struct completion wait;
2959 struct btrfs_work work;
2960};
2961
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002962static inline struct async_delayed_refs *
2963to_async_delayed_refs(struct btrfs_work *work)
2964{
2965 return container_of(work, struct async_delayed_refs, work);
2966}
2967
Chris Masona79b7d42014-05-22 16:18:52 -07002968static void delayed_ref_async_start(struct btrfs_work *work)
2969{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002970 struct async_delayed_refs *async = to_async_delayed_refs(work);
Chris Masona79b7d42014-05-22 16:18:52 -07002971 struct btrfs_trans_handle *trans;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002972 struct btrfs_fs_info *fs_info = async->root->fs_info;
Chris Masona79b7d42014-05-22 16:18:52 -07002973 int ret;
2974
Chris Mason0f873ec2016-04-27 09:59:38 -04002975 /* if the commit is already started, we don't need to wait here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002976 if (btrfs_transaction_blocked(fs_info))
Josef Bacik31b96552016-04-11 17:37:40 -04002977 goto done;
Josef Bacik31b96552016-04-11 17:37:40 -04002978
Chris Mason0f873ec2016-04-27 09:59:38 -04002979 trans = btrfs_join_transaction(async->root);
2980 if (IS_ERR(trans)) {
2981 async->error = PTR_ERR(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07002982 goto done;
2983 }
2984
2985 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002986 * trans->sync means that when we call end_transaction, we won't
Chris Masona79b7d42014-05-22 16:18:52 -07002987 * wait on delayed refs
2988 */
2989 trans->sync = true;
Chris Mason0f873ec2016-04-27 09:59:38 -04002990
2991 /* Don't bother flushing if we got into a different transaction */
2992 if (trans->transid > async->transid)
2993 goto end;
2994
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002995 ret = btrfs_run_delayed_refs(trans, async->count);
Chris Masona79b7d42014-05-22 16:18:52 -07002996 if (ret)
2997 async->error = ret;
Chris Mason0f873ec2016-04-27 09:59:38 -04002998end:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002999 ret = btrfs_end_transaction(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07003000 if (ret && !async->error)
3001 async->error = ret;
3002done:
3003 if (async->sync)
3004 complete(&async->wait);
3005 else
3006 kfree(async);
3007}
3008
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003009int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
Josef Bacik31b96552016-04-11 17:37:40 -04003010 unsigned long count, u64 transid, int wait)
Chris Masona79b7d42014-05-22 16:18:52 -07003011{
3012 struct async_delayed_refs *async;
3013 int ret;
3014
3015 async = kmalloc(sizeof(*async), GFP_NOFS);
3016 if (!async)
3017 return -ENOMEM;
3018
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003019 async->root = fs_info->tree_root;
Chris Masona79b7d42014-05-22 16:18:52 -07003020 async->count = count;
3021 async->error = 0;
Josef Bacik31b96552016-04-11 17:37:40 -04003022 async->transid = transid;
Chris Masona79b7d42014-05-22 16:18:52 -07003023 if (wait)
3024 async->sync = 1;
3025 else
3026 async->sync = 0;
3027 init_completion(&async->wait);
3028
Liu Bo9e0af232014-08-15 23:36:53 +08003029 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
3030 delayed_ref_async_start, NULL, NULL);
Chris Masona79b7d42014-05-22 16:18:52 -07003031
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003032 btrfs_queue_work(fs_info->extent_workers, &async->work);
Chris Masona79b7d42014-05-22 16:18:52 -07003033
3034 if (wait) {
3035 wait_for_completion(&async->wait);
3036 ret = async->error;
3037 kfree(async);
3038 return ret;
3039 }
3040 return 0;
3041}
3042
Chris Masonc3e69d52009-03-13 10:17:05 -04003043/*
3044 * this starts processing the delayed reference count updates and
3045 * extent insertions we have queued up so far. count can be
3046 * 0, which means to process everything in the tree at the start
3047 * of the run (but not newly added entries), or it can be some target
3048 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 *
3050 * Returns 0 on success or if called with an aborted transaction
3051 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04003052 */
3053int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003054 unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04003055{
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003056 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -04003057 struct rb_node *node;
3058 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08003059 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04003060 int ret;
3061 int run_all = count == (unsigned long)-1;
Filipe Mananad9a05402015-10-03 13:13:13 +01003062 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Chris Masonc3e69d52009-03-13 10:17:05 -04003063
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003064 /* We'll clean this up in btrfs_cleanup_transaction */
3065 if (trans->aborted)
3066 return 0;
3067
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003068 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08003069 return 0;
3070
Chris Masonc3e69d52009-03-13 10:17:05 -04003071 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08003072 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05003073 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05003074
Chris Masonc3e69d52009-03-13 10:17:05 -04003075again:
Arne Jansen709c0482011-09-12 12:22:57 +02003076#ifdef SCRAMBLE_DELAYED_REFS
3077 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
3078#endif
Filipe Mananad9a05402015-10-03 13:13:13 +01003079 trans->can_flush_pending_bgs = false;
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02003080 ret = __btrfs_run_delayed_refs(trans, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003081 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003082 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003083 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04003084 }
3085
Chris Mason56bec292009-03-13 10:10:06 -04003086 if (run_all) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05003087 if (!list_empty(&trans->new_bgs))
Nikolay Borisov6c686b32018-02-07 17:55:40 +02003088 btrfs_create_pending_block_groups(trans);
Josef Bacikea658ba2012-09-11 16:57:25 -04003089
Josef Bacikd7df2c72014-01-23 09:21:38 -05003090 spin_lock(&delayed_refs->lock);
Liu Boc46effa2013-10-14 12:59:45 +08003091 node = rb_first(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003092 if (!node) {
3093 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04003094 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05003095 }
Josef Bacikd2788502017-09-29 15:43:57 -04003096 head = rb_entry(node, struct btrfs_delayed_ref_head,
3097 href_node);
3098 refcount_inc(&head->refs);
Chris Mason56bec292009-03-13 10:10:06 -04003099 spin_unlock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04003100
3101 /* Mutex was contended, block until it's released and retry. */
3102 mutex_lock(&head->mutex);
3103 mutex_unlock(&head->mutex);
3104
3105 btrfs_put_delayed_ref_head(head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003106 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04003107 goto again;
3108 }
Chris Mason54aa1f42007-06-22 14:16:25 -04003109out:
Filipe Mananad9a05402015-10-03 13:13:13 +01003110 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Chris Masona28ec192007-03-06 20:08:01 -05003111 return 0;
3112}
3113
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003114int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003115 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003116 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04003117 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003118{
3119 struct btrfs_delayed_extent_op *extent_op;
3120 int ret;
3121
Miao Xie78a61842012-11-21 02:21:28 +00003122 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003123 if (!extent_op)
3124 return -ENOMEM;
3125
3126 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01003127 extent_op->update_flags = true;
3128 extent_op->update_key = false;
3129 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04003130 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003131
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003132 ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003133 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003134 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00003135 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003136 return ret;
3137}
3138
Liu Boe4c3b2d2017-01-30 12:25:28 -08003139static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003140 struct btrfs_path *path,
3141 u64 objectid, u64 offset, u64 bytenr)
3142{
3143 struct btrfs_delayed_ref_head *head;
3144 struct btrfs_delayed_ref_node *ref;
3145 struct btrfs_delayed_data_ref *data_ref;
3146 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08003147 struct btrfs_transaction *cur_trans;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04003148 struct rb_node *node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003149 int ret = 0;
3150
Liu Boe4c3b2d2017-01-30 12:25:28 -08003151 cur_trans = root->fs_info->running_transaction;
3152 if (!cur_trans)
3153 return 0;
3154
3155 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003156 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003157 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003158 if (!head) {
3159 spin_unlock(&delayed_refs->lock);
3160 return 0;
3161 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003162
3163 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04003164 refcount_inc(&head->refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003165 spin_unlock(&delayed_refs->lock);
3166
David Sterbab3b4aa72011-04-21 01:20:15 +02003167 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003168
David Sterba8cc33e52011-05-02 15:29:25 +02003169 /*
3170 * Mutex was contended, block until it's released and let
3171 * caller try again
3172 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003173 mutex_lock(&head->mutex);
3174 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04003175 btrfs_put_delayed_ref_head(head);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003176 return -EAGAIN;
3177 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003178 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003179
3180 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04003181 /*
3182 * XXX: We should replace this with a proper search function in the
3183 * future.
3184 */
3185 for (node = rb_first(&head->ref_tree); node; node = rb_next(node)) {
3186 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003187 /* If it's a shared ref we know a cross reference exists */
3188 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
3189 ret = 1;
3190 break;
3191 }
3192
3193 data_ref = btrfs_delayed_node_to_data_ref(ref);
3194
3195 /*
3196 * If our ref doesn't match the one we're currently looking at
3197 * then we have a cross reference.
3198 */
3199 if (data_ref->root != root->root_key.objectid ||
3200 data_ref->objectid != objectid ||
3201 data_ref->offset != offset) {
3202 ret = 1;
3203 break;
3204 }
3205 }
3206 spin_unlock(&head->lock);
3207 mutex_unlock(&head->mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003208 return ret;
3209}
3210
Liu Boe4c3b2d2017-01-30 12:25:28 -08003211static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003212 struct btrfs_path *path,
3213 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05003214{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003215 struct btrfs_fs_info *fs_info = root->fs_info;
3216 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04003217 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003218 struct btrfs_extent_data_ref *ref;
3219 struct btrfs_extent_inline_ref *iref;
3220 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05003221 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003222 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06003223 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04003224 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05003225
Chris Masonbe20aa92007-12-17 20:14:01 -05003226 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04003227 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04003228 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05003229
Chris Masonbe20aa92007-12-17 20:14:01 -05003230 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3231 if (ret < 0)
3232 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003233 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04003234
3235 ret = -ENOENT;
3236 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04003237 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003238
Zheng Yan31840ae2008-09-23 13:14:14 -04003239 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04003240 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003241 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05003242
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003243 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05003244 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003245
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003246 ret = 1;
3247 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3248#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3249 if (item_size < sizeof(*ei)) {
3250 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3251 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003252 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003253#endif
3254 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
3255
3256 if (item_size != sizeof(*ei) +
3257 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
3258 goto out;
3259
3260 if (btrfs_extent_generation(leaf, ei) <=
3261 btrfs_root_last_snapshot(&root->root_item))
3262 goto out;
3263
3264 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
Liu Bo3de28d52017-08-18 15:15:19 -06003265
3266 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
3267 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003268 goto out;
3269
3270 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3271 if (btrfs_extent_refs(leaf, ei) !=
3272 btrfs_extent_data_ref_count(leaf, ref) ||
3273 btrfs_extent_data_ref_root(leaf, ref) !=
3274 root->root_key.objectid ||
3275 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3276 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3277 goto out;
3278
Yan Zhengf321e492008-07-30 09:26:11 -04003279 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05003280out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003281 return ret;
3282}
3283
Liu Boe4c3b2d2017-01-30 12:25:28 -08003284int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
3285 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003286{
3287 struct btrfs_path *path;
3288 int ret;
3289 int ret2;
3290
3291 path = btrfs_alloc_path();
3292 if (!path)
3293 return -ENOENT;
3294
3295 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08003296 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003297 offset, bytenr);
3298 if (ret && ret != -ENOENT)
3299 goto out;
3300
Liu Boe4c3b2d2017-01-30 12:25:28 -08003301 ret2 = check_delayed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003302 offset, bytenr);
3303 } while (ret2 == -EAGAIN);
3304
3305 if (ret2 && ret2 != -ENOENT) {
3306 ret = ret2;
3307 goto out;
3308 }
3309
3310 if (ret != -ENOENT || ret2 != -ENOENT)
3311 ret = 0;
3312out:
Yan Zhengf321e492008-07-30 09:26:11 -04003313 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003314 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3315 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04003316 return ret;
3317}
3318
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003319static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05003320 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003321 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07003322 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04003323{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003324 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04003325 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003326 u64 num_bytes;
3327 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003328 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04003329 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003330 struct btrfs_key key;
3331 struct btrfs_file_extent_item *fi;
3332 int i;
3333 int level;
3334 int ret = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003335 int (*process_func)(struct btrfs_trans_handle *,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003336 struct btrfs_root *,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003337 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04003338
David Sterbafccb84c2014-09-29 23:53:21 +02003339
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003340 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003341 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003342
Zheng Yan31840ae2008-09-23 13:14:14 -04003343 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003344 nritems = btrfs_header_nritems(buf);
3345 level = btrfs_header_level(buf);
3346
Miao Xie27cdeb72014-04-02 19:51:05 +08003347 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003348 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003349
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003350 if (inc)
3351 process_func = btrfs_inc_extent_ref;
3352 else
3353 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003354
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003355 if (full_backref)
3356 parent = buf->start;
3357 else
3358 parent = 0;
3359
Zheng Yan31840ae2008-09-23 13:14:14 -04003360 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003361 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003362 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02003363 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003364 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003365 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003366 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003367 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003368 BTRFS_FILE_EXTENT_INLINE)
3369 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003370 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3371 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003372 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003373
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003374 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3375 key.offset -= btrfs_file_extent_offset(buf, fi);
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003376 ret = process_func(trans, root, bytenr, num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003377 parent, ref_root, key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003378 key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003379 if (ret)
3380 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003381 } else {
3382 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003383 num_bytes = fs_info->nodesize;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003384 ret = process_func(trans, root, bytenr, num_bytes,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003385 parent, ref_root, level - 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003386 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003387 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003388 }
3389 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003390 return 0;
3391fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003392 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003393}
3394
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003395int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003396 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04003397{
Josef Bacike339a6b2014-07-02 10:54:25 -07003398 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003399}
Zheng Yan31840ae2008-09-23 13:14:14 -04003400
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003401int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003402 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003403{
Josef Bacike339a6b2014-07-02 10:54:25 -07003404 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003405}
3406
Chris Mason9078a3e2007-04-26 16:46:15 -04003407static int write_one_cache_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003408 struct btrfs_fs_info *fs_info,
Chris Mason9078a3e2007-04-26 16:46:15 -04003409 struct btrfs_path *path,
3410 struct btrfs_block_group_cache *cache)
3411{
3412 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003413 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003414 unsigned long bi;
3415 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003416
Chris Mason9078a3e2007-04-26 16:46:15 -04003417 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003418 if (ret) {
3419 if (ret > 0)
3420 ret = -ENOENT;
Chris Mason54aa1f42007-06-22 14:16:25 -04003421 goto fail;
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003422 }
Chris Mason5f39d392007-10-15 16:14:19 -04003423
3424 leaf = path->nodes[0];
3425 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3426 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3427 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -04003428fail:
Filipe Manana24b89d02015-04-25 18:31:05 +01003429 btrfs_release_path(path);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003430 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003431
3432}
3433
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003434static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003435next_block_group(struct btrfs_fs_info *fs_info,
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003436 struct btrfs_block_group_cache *cache)
3437{
3438 struct rb_node *node;
Filipe Manana292cbd52014-11-26 15:28:50 +00003439
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003440 spin_lock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003441
3442 /* If our block group was removed, we need a full search. */
3443 if (RB_EMPTY_NODE(&cache->cache_node)) {
3444 const u64 next_bytenr = cache->key.objectid + cache->key.offset;
3445
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003446 spin_unlock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003447 btrfs_put_block_group(cache);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003448 cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
Filipe Manana292cbd52014-11-26 15:28:50 +00003449 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003450 node = rb_next(&cache->cache_node);
3451 btrfs_put_block_group(cache);
3452 if (node) {
3453 cache = rb_entry(node, struct btrfs_block_group_cache,
3454 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003455 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003456 } else
3457 cache = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003458 spin_unlock(&fs_info->block_group_cache_lock);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003459 return cache;
3460}
3461
Josef Bacik0af3d002010-06-21 14:48:16 -04003462static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3463 struct btrfs_trans_handle *trans,
3464 struct btrfs_path *path)
3465{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003466 struct btrfs_fs_info *fs_info = block_group->fs_info;
3467 struct btrfs_root *root = fs_info->tree_root;
Josef Bacik0af3d002010-06-21 14:48:16 -04003468 struct inode *inode = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08003469 struct extent_changeset *data_reserved = NULL;
Josef Bacik0af3d002010-06-21 14:48:16 -04003470 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003471 int dcs = BTRFS_DC_ERROR;
David Sterbaf8c269d2015-01-16 17:21:12 +01003472 u64 num_pages = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003473 int retries = 0;
3474 int ret = 0;
3475
3476 /*
3477 * If this block group is smaller than 100 megs don't bother caching the
3478 * block group.
3479 */
Byongho Leeee221842015-12-15 01:42:10 +09003480 if (block_group->key.offset < (100 * SZ_1M)) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003481 spin_lock(&block_group->lock);
3482 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3483 spin_unlock(&block_group->lock);
3484 return 0;
3485 }
3486
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003487 if (trans->aborted)
3488 return 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003489again:
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003490 inode = lookup_free_space_inode(fs_info, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003491 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3492 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003493 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003494 goto out;
3495 }
3496
3497 if (IS_ERR(inode)) {
3498 BUG_ON(retries);
3499 retries++;
3500
3501 if (block_group->ro)
3502 goto out_free;
3503
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003504 ret = create_free_space_inode(fs_info, trans, block_group,
3505 path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003506 if (ret)
3507 goto out_free;
3508 goto again;
3509 }
3510
3511 /*
3512 * We want to set the generation to 0, that way if anything goes wrong
3513 * from here on out we know not to trust this cache when we load up next
3514 * time.
3515 */
3516 BTRFS_I(inode)->generation = 0;
3517 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003518 if (ret) {
3519 /*
3520 * So theoretically we could recover from this, simply set the
3521 * super cache generation to 0 so we know to invalidate the
3522 * cache, but then we'd have to keep track of the block groups
3523 * that fail this way so we know we _have_ to reset this cache
3524 * before the next commit or risk reading stale cache. So to
3525 * limit our exposure to horrible edge cases lets just abort the
3526 * transaction, this only happens in really bad situations
3527 * anyway.
3528 */
Jeff Mahoney66642832016-06-10 18:19:25 -04003529 btrfs_abort_transaction(trans, ret);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003530 goto out_put;
3531 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003532 WARN_ON(ret);
3533
Josef Bacik8e138e02017-11-17 14:50:46 -05003534 /* We've already setup this transaction, go ahead and exit */
3535 if (block_group->cache_generation == trans->transid &&
3536 i_size_read(inode)) {
3537 dcs = BTRFS_DC_SETUP;
3538 goto out_put;
3539 }
3540
Josef Bacik0af3d002010-06-21 14:48:16 -04003541 if (i_size_read(inode) > 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003542 ret = btrfs_check_trunc_cache_free_space(fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003543 &fs_info->global_block_rsv);
Miao Xie7b61cd92013-05-13 13:55:09 +00003544 if (ret)
3545 goto out_put;
3546
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003547 ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003548 if (ret)
3549 goto out_put;
3550 }
3551
3552 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003553 if (block_group->cached != BTRFS_CACHE_FINISHED ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003554 !btrfs_test_opt(fs_info, SPACE_CACHE)) {
Liu Bocf7c1ef2012-07-06 03:31:34 -06003555 /*
3556 * don't bother trying to write stuff out _if_
3557 * a) we're not cached,
Liu Bo1a79c1f2017-03-06 13:49:02 -08003558 * b) we're with nospace_cache mount option,
3559 * c) we're with v2 space_cache (FREE_SPACE_TREE).
Liu Bocf7c1ef2012-07-06 03:31:34 -06003560 */
Josef Bacik2b209822010-12-03 13:17:53 -05003561 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003562 spin_unlock(&block_group->lock);
3563 goto out_put;
3564 }
3565 spin_unlock(&block_group->lock);
3566
Josef Bacik6fc823b2012-08-06 13:46:38 -06003567 /*
Josef Bacik2968b1f2015-10-01 12:55:18 -04003568 * We hit an ENOSPC when setting up the cache in this transaction, just
3569 * skip doing the setup, we've already cleared the cache so we're safe.
3570 */
3571 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
3572 ret = -ENOSPC;
3573 goto out_put;
3574 }
3575
3576 /*
Josef Bacik6fc823b2012-08-06 13:46:38 -06003577 * Try to preallocate enough space based on how big the block group is.
3578 * Keep in mind this has to include any pinned space which could end up
3579 * taking up quite a bit since it's not folded into the other space
3580 * cache.
3581 */
Byongho Leeee221842015-12-15 01:42:10 +09003582 num_pages = div_u64(block_group->key.offset, SZ_256M);
Josef Bacik0af3d002010-06-21 14:48:16 -04003583 if (!num_pages)
3584 num_pages = 1;
3585
Josef Bacik0af3d002010-06-21 14:48:16 -04003586 num_pages *= 16;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003587 num_pages *= PAGE_SIZE;
Josef Bacik0af3d002010-06-21 14:48:16 -04003588
Qu Wenruo364ecf32017-02-27 15:10:38 +08003589 ret = btrfs_check_data_free_space(inode, &data_reserved, 0, num_pages);
Josef Bacik0af3d002010-06-21 14:48:16 -04003590 if (ret)
3591 goto out_put;
3592
3593 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3594 num_pages, num_pages,
3595 &alloc_hint);
Josef Bacik2968b1f2015-10-01 12:55:18 -04003596 /*
3597 * Our cache requires contiguous chunks so that we don't modify a bunch
3598 * of metadata or split extents when writing the cache out, which means
3599 * we can enospc if we are heavily fragmented in addition to just normal
3600 * out of space conditions. So if we hit this just skip setting up any
3601 * other block groups for this transaction, maybe we'll unpin enough
3602 * space the next time around.
3603 */
Josef Bacik2b209822010-12-03 13:17:53 -05003604 if (!ret)
3605 dcs = BTRFS_DC_SETUP;
Josef Bacik2968b1f2015-10-01 12:55:18 -04003606 else if (ret == -ENOSPC)
3607 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
Josef Bacikc09544e2011-08-30 10:19:10 -04003608
Josef Bacik0af3d002010-06-21 14:48:16 -04003609out_put:
3610 iput(inode);
3611out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003612 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003613out:
3614 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003615 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003616 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003617 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003618 spin_unlock(&block_group->lock);
3619
Qu Wenruo364ecf32017-02-27 15:10:38 +08003620 extent_changeset_free(data_reserved);
Josef Bacik0af3d002010-06-21 14:48:16 -04003621 return ret;
3622}
3623
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003624int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003625 struct btrfs_fs_info *fs_info)
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003626{
3627 struct btrfs_block_group_cache *cache, *tmp;
3628 struct btrfs_transaction *cur_trans = trans->transaction;
3629 struct btrfs_path *path;
3630
3631 if (list_empty(&cur_trans->dirty_bgs) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003632 !btrfs_test_opt(fs_info, SPACE_CACHE))
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003633 return 0;
3634
3635 path = btrfs_alloc_path();
3636 if (!path)
3637 return -ENOMEM;
3638
3639 /* Could add new block groups, use _safe just in case */
3640 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
3641 dirty_list) {
3642 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3643 cache_save_setup(cache, trans, path);
3644 }
3645
3646 btrfs_free_path(path);
3647 return 0;
3648}
3649
Chris Mason1bbc6212015-04-06 12:46:08 -07003650/*
3651 * transaction commit does final block group cache writeback during a
3652 * critical section where nothing is allowed to change the FS. This is
3653 * required in order for the cache to actually match the block group,
3654 * but can introduce a lot of latency into the commit.
3655 *
3656 * So, btrfs_start_dirty_block_groups is here to kick off block group
3657 * cache IO. There's a chance we'll have to redo some of it if the
3658 * block group changes again during the commit, but it greatly reduces
3659 * the commit latency by getting rid of the easy block groups while
3660 * we're still allowing others to join the commit.
3661 */
Nikolay Borisov21217052018-02-07 17:55:41 +02003662int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
Chris Mason1bbc6212015-04-06 12:46:08 -07003663{
Nikolay Borisov21217052018-02-07 17:55:41 +02003664 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason1bbc6212015-04-06 12:46:08 -07003665 struct btrfs_block_group_cache *cache;
3666 struct btrfs_transaction *cur_trans = trans->transaction;
3667 int ret = 0;
3668 int should_put;
3669 struct btrfs_path *path = NULL;
3670 LIST_HEAD(dirty);
3671 struct list_head *io = &cur_trans->io_bgs;
3672 int num_started = 0;
3673 int loops = 0;
3674
3675 spin_lock(&cur_trans->dirty_bgs_lock);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003676 if (list_empty(&cur_trans->dirty_bgs)) {
3677 spin_unlock(&cur_trans->dirty_bgs_lock);
3678 return 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003679 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003680 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Chris Mason1bbc6212015-04-06 12:46:08 -07003681 spin_unlock(&cur_trans->dirty_bgs_lock);
3682
3683again:
Chris Mason1bbc6212015-04-06 12:46:08 -07003684 /*
3685 * make sure all the block groups on our dirty list actually
3686 * exist
3687 */
Nikolay Borisov6c686b32018-02-07 17:55:40 +02003688 btrfs_create_pending_block_groups(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07003689
3690 if (!path) {
3691 path = btrfs_alloc_path();
3692 if (!path)
3693 return -ENOMEM;
3694 }
3695
Filipe Mananab58d1a92015-04-25 18:29:16 +01003696 /*
3697 * cache_write_mutex is here only to save us from balance or automatic
3698 * removal of empty block groups deleting this block group while we are
3699 * writing out the cache
3700 */
3701 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003702 while (!list_empty(&dirty)) {
3703 cache = list_first_entry(&dirty,
3704 struct btrfs_block_group_cache,
3705 dirty_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07003706 /*
3707 * this can happen if something re-dirties a block
3708 * group that is already under IO. Just wait for it to
3709 * finish and then do it all again
3710 */
3711 if (!list_empty(&cache->io_list)) {
3712 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003713 btrfs_wait_cache_io(trans, cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003714 btrfs_put_block_group(cache);
3715 }
3716
3717
3718 /*
3719 * btrfs_wait_cache_io uses the cache->dirty_list to decide
3720 * if it should update the cache_state. Don't delete
3721 * until after we wait.
3722 *
3723 * Since we're not running in the commit critical section
3724 * we need the dirty_bgs_lock to protect from update_block_group
3725 */
3726 spin_lock(&cur_trans->dirty_bgs_lock);
3727 list_del_init(&cache->dirty_list);
3728 spin_unlock(&cur_trans->dirty_bgs_lock);
3729
3730 should_put = 1;
3731
3732 cache_save_setup(cache, trans, path);
3733
3734 if (cache->disk_cache_state == BTRFS_DC_SETUP) {
3735 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003736 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003737 cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003738 if (ret == 0 && cache->io_ctl.inode) {
3739 num_started++;
3740 should_put = 0;
3741
3742 /*
Nikolay Borisov45ae2c12018-02-08 18:25:18 +02003743 * The cache_write_mutex is protecting the
3744 * io_list, also refer to the definition of
3745 * btrfs_transaction::io_bgs for more details
Chris Mason1bbc6212015-04-06 12:46:08 -07003746 */
3747 list_add_tail(&cache->io_list, io);
3748 } else {
3749 /*
3750 * if we failed to write the cache, the
3751 * generation will be bad and life goes on
3752 */
3753 ret = 0;
3754 }
3755 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003756 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003757 ret = write_one_cache_group(trans, fs_info,
3758 path, cache);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003759 /*
3760 * Our block group might still be attached to the list
3761 * of new block groups in the transaction handle of some
3762 * other task (struct btrfs_trans_handle->new_bgs). This
3763 * means its block group item isn't yet in the extent
3764 * tree. If this happens ignore the error, as we will
3765 * try again later in the critical section of the
3766 * transaction commit.
3767 */
3768 if (ret == -ENOENT) {
3769 ret = 0;
3770 spin_lock(&cur_trans->dirty_bgs_lock);
3771 if (list_empty(&cache->dirty_list)) {
3772 list_add_tail(&cache->dirty_list,
3773 &cur_trans->dirty_bgs);
3774 btrfs_get_block_group(cache);
3775 }
3776 spin_unlock(&cur_trans->dirty_bgs_lock);
3777 } else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003778 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003779 }
3780 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003781
3782 /* if its not on the io list, we need to put the block group */
3783 if (should_put)
3784 btrfs_put_block_group(cache);
3785
3786 if (ret)
3787 break;
Filipe Mananab58d1a92015-04-25 18:29:16 +01003788
3789 /*
3790 * Avoid blocking other tasks for too long. It might even save
3791 * us from writing caches for block groups that are going to be
3792 * removed.
3793 */
3794 mutex_unlock(&trans->transaction->cache_write_mutex);
3795 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003796 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003797 mutex_unlock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003798
3799 /*
3800 * go through delayed refs for all the stuff we've just kicked off
3801 * and then loop back (just once)
3802 */
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003803 ret = btrfs_run_delayed_refs(trans, 0);
Chris Mason1bbc6212015-04-06 12:46:08 -07003804 if (!ret && loops == 0) {
3805 loops++;
3806 spin_lock(&cur_trans->dirty_bgs_lock);
3807 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003808 /*
3809 * dirty_bgs_lock protects us from concurrent block group
3810 * deletes too (not just cache_write_mutex).
3811 */
3812 if (!list_empty(&dirty)) {
3813 spin_unlock(&cur_trans->dirty_bgs_lock);
3814 goto again;
3815 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003816 spin_unlock(&cur_trans->dirty_bgs_lock);
Liu Boc79a1752016-07-20 17:44:12 -07003817 } else if (ret < 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003818 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
Chris Mason1bbc6212015-04-06 12:46:08 -07003819 }
3820
3821 btrfs_free_path(path);
3822 return ret;
3823}
3824
Chris Mason96b51792007-10-15 16:15:19 -04003825int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003826 struct btrfs_fs_info *fs_info)
Chris Mason9078a3e2007-04-26 16:46:15 -04003827{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003828 struct btrfs_block_group_cache *cache;
Josef Bacikce93ec52014-11-17 15:45:48 -05003829 struct btrfs_transaction *cur_trans = trans->transaction;
3830 int ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003831 int should_put;
Chris Mason9078a3e2007-04-26 16:46:15 -04003832 struct btrfs_path *path;
Chris Mason1bbc6212015-04-06 12:46:08 -07003833 struct list_head *io = &cur_trans->io_bgs;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003834 int num_started = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003835
3836 path = btrfs_alloc_path();
3837 if (!path)
3838 return -ENOMEM;
3839
Josef Bacikce93ec52014-11-17 15:45:48 -05003840 /*
Filipe Mananae44081e2015-12-18 03:02:48 +00003841 * Even though we are in the critical section of the transaction commit,
3842 * we can still have concurrent tasks adding elements to this
3843 * transaction's list of dirty block groups. These tasks correspond to
3844 * endio free space workers started when writeback finishes for a
3845 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
3846 * allocate new block groups as a result of COWing nodes of the root
3847 * tree when updating the free space inode. The writeback for the space
3848 * caches is triggered by an earlier call to
3849 * btrfs_start_dirty_block_groups() and iterations of the following
3850 * loop.
3851 * Also we want to do the cache_save_setup first and then run the
Josef Bacikce93ec52014-11-17 15:45:48 -05003852 * delayed refs to make sure we have the best chance at doing this all
3853 * in one shot.
3854 */
Filipe Mananae44081e2015-12-18 03:02:48 +00003855 spin_lock(&cur_trans->dirty_bgs_lock);
Josef Bacikce93ec52014-11-17 15:45:48 -05003856 while (!list_empty(&cur_trans->dirty_bgs)) {
3857 cache = list_first_entry(&cur_trans->dirty_bgs,
3858 struct btrfs_block_group_cache,
3859 dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003860
3861 /*
3862 * this can happen if cache_save_setup re-dirties a block
3863 * group that is already under IO. Just wait for it to
3864 * finish and then do it all again
3865 */
3866 if (!list_empty(&cache->io_list)) {
Filipe Mananae44081e2015-12-18 03:02:48 +00003867 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003868 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003869 btrfs_wait_cache_io(trans, cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003870 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003871 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003872 }
3873
Chris Mason1bbc6212015-04-06 12:46:08 -07003874 /*
3875 * don't remove from the dirty list until after we've waited
3876 * on any pending IO
3877 */
Josef Bacikce93ec52014-11-17 15:45:48 -05003878 list_del_init(&cache->dirty_list);
Filipe Mananae44081e2015-12-18 03:02:48 +00003879 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003880 should_put = 1;
3881
Chris Mason1bbc6212015-04-06 12:46:08 -07003882 cache_save_setup(cache, trans, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003883
Josef Bacikce93ec52014-11-17 15:45:48 -05003884 if (!ret)
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003885 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003886 (unsigned long) -1);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003887
3888 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3889 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003890 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003891 cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003892 if (ret == 0 && cache->io_ctl.inode) {
3893 num_started++;
3894 should_put = 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003895 list_add_tail(&cache->io_list, io);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003896 } else {
3897 /*
3898 * if we failed to write the cache, the
3899 * generation will be bad and life goes on
3900 */
3901 ret = 0;
3902 }
3903 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003904 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003905 ret = write_one_cache_group(trans, fs_info,
3906 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003907 /*
3908 * One of the free space endio workers might have
3909 * created a new block group while updating a free space
3910 * cache's inode (at inode.c:btrfs_finish_ordered_io())
3911 * and hasn't released its transaction handle yet, in
3912 * which case the new block group is still attached to
3913 * its transaction handle and its creation has not
3914 * finished yet (no block group item in the extent tree
3915 * yet, etc). If this is the case, wait for all free
3916 * space endio workers to finish and retry. This is a
3917 * a very rare case so no need for a more efficient and
3918 * complex approach.
3919 */
3920 if (ret == -ENOENT) {
3921 wait_event(cur_trans->writer_wait,
3922 atomic_read(&cur_trans->num_writers) == 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003923 ret = write_one_cache_group(trans, fs_info,
3924 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003925 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003926 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003927 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003928 }
Chris Masonc9dc4c62015-04-04 17:14:42 -07003929
3930 /* if its not on the io list, we need to put the block group */
3931 if (should_put)
3932 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003933 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003934 }
Filipe Mananae44081e2015-12-18 03:02:48 +00003935 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003936
Nikolay Borisov45ae2c12018-02-08 18:25:18 +02003937 /*
3938 * Refer to the definition of io_bgs member for details why it's safe
3939 * to use it without any locking
3940 */
Chris Mason1bbc6212015-04-06 12:46:08 -07003941 while (!list_empty(io)) {
3942 cache = list_first_entry(io, struct btrfs_block_group_cache,
Chris Masonc9dc4c62015-04-04 17:14:42 -07003943 io_list);
3944 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003945 btrfs_wait_cache_io(trans, cache, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003946 btrfs_put_block_group(cache);
3947 }
3948
Chris Mason9078a3e2007-04-26 16:46:15 -04003949 btrfs_free_path(path);
Josef Bacikce93ec52014-11-17 15:45:48 -05003950 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003951}
3952
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003953int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05003954{
3955 struct btrfs_block_group_cache *block_group;
3956 int readonly = 0;
3957
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003958 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003959 if (!block_group || block_group->ro)
3960 readonly = 1;
3961 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003962 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003963 return readonly;
3964}
3965
Filipe Mananaf78c4362016-05-09 13:15:41 +01003966bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3967{
3968 struct btrfs_block_group_cache *bg;
3969 bool ret = true;
3970
3971 bg = btrfs_lookup_block_group(fs_info, bytenr);
3972 if (!bg)
3973 return false;
3974
3975 spin_lock(&bg->lock);
3976 if (bg->ro)
3977 ret = false;
3978 else
3979 atomic_inc(&bg->nocow_writers);
3980 spin_unlock(&bg->lock);
3981
3982 /* no put on block group, done by btrfs_dec_nocow_writers */
3983 if (!ret)
3984 btrfs_put_block_group(bg);
3985
3986 return ret;
3987
3988}
3989
3990void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3991{
3992 struct btrfs_block_group_cache *bg;
3993
3994 bg = btrfs_lookup_block_group(fs_info, bytenr);
3995 ASSERT(bg);
3996 if (atomic_dec_and_test(&bg->nocow_writers))
3997 wake_up_atomic_t(&bg->nocow_writers);
3998 /*
3999 * Once for our lookup and once for the lookup done by a previous call
4000 * to btrfs_inc_nocow_writers()
4001 */
4002 btrfs_put_block_group(bg);
4003 btrfs_put_block_group(bg);
4004}
4005
Filipe Mananaf78c4362016-05-09 13:15:41 +01004006void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg)
4007{
David Howells5e4def22017-11-02 15:27:44 +00004008 wait_on_atomic_t(&bg->nocow_writers, atomic_t_wait,
Filipe Mananaf78c4362016-05-09 13:15:41 +01004009 TASK_UNINTERRUPTIBLE);
4010}
4011
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04004012static const char *alloc_name(u64 flags)
4013{
4014 switch (flags) {
4015 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
4016 return "mixed";
4017 case BTRFS_BLOCK_GROUP_METADATA:
4018 return "metadata";
4019 case BTRFS_BLOCK_GROUP_DATA:
4020 return "data";
4021 case BTRFS_BLOCK_GROUP_SYSTEM:
4022 return "system";
4023 default:
4024 WARN_ON(1);
4025 return "invalid-combination";
4026 };
4027}
4028
Nikolay Borisov2be12ef2017-05-22 09:35:49 +03004029static int create_space_info(struct btrfs_fs_info *info, u64 flags,
4030 struct btrfs_space_info **new)
4031{
4032
4033 struct btrfs_space_info *space_info;
4034 int i;
4035 int ret;
4036
4037 space_info = kzalloc(sizeof(*space_info), GFP_NOFS);
4038 if (!space_info)
4039 return -ENOMEM;
4040
4041 ret = percpu_counter_init(&space_info->total_bytes_pinned, 0,
4042 GFP_KERNEL);
4043 if (ret) {
4044 kfree(space_info);
4045 return ret;
4046 }
4047
4048 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
4049 INIT_LIST_HEAD(&space_info->block_groups[i]);
4050 init_rwsem(&space_info->groups_sem);
4051 spin_lock_init(&space_info->lock);
4052 space_info->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
4053 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
4054 init_waitqueue_head(&space_info->wait);
4055 INIT_LIST_HEAD(&space_info->ro_bgs);
4056 INIT_LIST_HEAD(&space_info->tickets);
4057 INIT_LIST_HEAD(&space_info->priority_tickets);
4058
4059 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
4060 info->space_info_kobj, "%s",
4061 alloc_name(space_info->flags));
4062 if (ret) {
4063 percpu_counter_destroy(&space_info->total_bytes_pinned);
4064 kfree(space_info);
4065 return ret;
4066 }
4067
4068 *new = space_info;
4069 list_add_rcu(&space_info->list, &info->space_info);
4070 if (flags & BTRFS_BLOCK_GROUP_DATA)
4071 info->data_sinfo = space_info;
4072
4073 return ret;
4074}
4075
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +03004076static void update_space_info(struct btrfs_fs_info *info, u64 flags,
Chris Mason593060d2008-03-25 16:50:33 -04004077 u64 total_bytes, u64 bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -04004078 u64 bytes_readonly,
Chris Mason593060d2008-03-25 16:50:33 -04004079 struct btrfs_space_info **space_info)
4080{
4081 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004082 int factor;
4083
4084 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
4085 BTRFS_BLOCK_GROUP_RAID10))
4086 factor = 2;
4087 else
4088 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04004089
4090 found = __find_space_info(info, flags);
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +03004091 ASSERT(found);
4092 spin_lock(&found->lock);
4093 found->total_bytes += total_bytes;
4094 found->disk_total += total_bytes * factor;
4095 found->bytes_used += bytes_used;
4096 found->disk_used += bytes_used * factor;
4097 found->bytes_readonly += bytes_readonly;
4098 if (total_bytes > 0)
4099 found->full = 0;
4100 space_info_add_new_bytes(info, found, total_bytes -
4101 bytes_used - bytes_readonly);
4102 spin_unlock(&found->lock);
4103 *space_info = found;
Chris Mason593060d2008-03-25 16:50:33 -04004104}
4105
Chris Mason8790d502008-04-03 16:29:03 -04004106static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
4107{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03004108 u64 extra_flags = chunk_to_extended(flags) &
4109 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004110
Miao Xiede98ced2013-01-29 10:13:12 +00004111 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004112 if (flags & BTRFS_BLOCK_GROUP_DATA)
4113 fs_info->avail_data_alloc_bits |= extra_flags;
4114 if (flags & BTRFS_BLOCK_GROUP_METADATA)
4115 fs_info->avail_metadata_alloc_bits |= extra_flags;
4116 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
4117 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004118 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04004119}
Chris Mason593060d2008-03-25 16:50:33 -04004120
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004121/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004122 * returns target flags in extended format or 0 if restripe for this
4123 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04004124 *
4125 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004126 */
4127static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
4128{
4129 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4130 u64 target = 0;
4131
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004132 if (!bctl)
4133 return 0;
4134
4135 if (flags & BTRFS_BLOCK_GROUP_DATA &&
4136 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4137 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
4138 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
4139 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4140 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
4141 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
4142 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4143 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
4144 }
4145
4146 return target;
4147}
4148
4149/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004150 * @flags: available profiles in extended format (see ctree.h)
4151 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004152 * Returns reduced profile in chunk format. If profile changing is in
4153 * progress (either running or paused) picks the target profile (if it's
4154 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004155 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004156static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04004157{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004158 u64 num_devices = fs_info->fs_devices->rw_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004159 u64 target;
Zhao Lei9c170b22015-09-15 21:08:08 +08004160 u64 raid_type;
4161 u64 allowed = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004162
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004163 /*
4164 * see if restripe for this chunk_type is in progress, if so
4165 * try to reduce to the target profile
4166 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004167 spin_lock(&fs_info->balance_lock);
4168 target = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004169 if (target) {
4170 /* pick target profile only if it's already available */
4171 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004172 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004173 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004174 }
4175 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004176 spin_unlock(&fs_info->balance_lock);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004177
David Woodhouse53b381b2013-01-29 18:40:14 -05004178 /* First, mask out the RAID levels which aren't possible */
Zhao Lei9c170b22015-09-15 21:08:08 +08004179 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
4180 if (num_devices >= btrfs_raid_array[raid_type].devs_min)
4181 allowed |= btrfs_raid_group[raid_type];
4182 }
4183 allowed &= flags;
Chris Masona061fc82008-05-07 11:43:44 -04004184
Zhao Lei9c170b22015-09-15 21:08:08 +08004185 if (allowed & BTRFS_BLOCK_GROUP_RAID6)
4186 allowed = BTRFS_BLOCK_GROUP_RAID6;
4187 else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
4188 allowed = BTRFS_BLOCK_GROUP_RAID5;
4189 else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
4190 allowed = BTRFS_BLOCK_GROUP_RAID10;
4191 else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
4192 allowed = BTRFS_BLOCK_GROUP_RAID1;
4193 else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
4194 allowed = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04004195
Zhao Lei9c170b22015-09-15 21:08:08 +08004196 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
Chris Masonec44a352008-04-28 15:29:52 -04004197
Zhao Lei9c170b22015-09-15 21:08:08 +08004198 return extended_to_chunk(flags | allowed);
Chris Masonec44a352008-04-28 15:29:52 -04004199}
4200
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004201static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05004202{
Miao Xiede98ced2013-01-29 10:13:12 +00004203 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004204 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004205
4206 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004207 flags = orig_flags;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004208 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00004209
4210 if (flags & BTRFS_BLOCK_GROUP_DATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004211 flags |= fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004212 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004213 flags |= fs_info->avail_system_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004214 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004215 flags |= fs_info->avail_metadata_alloc_bits;
4216 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02004217
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004218 return btrfs_reduce_alloc_profile(fs_info, flags);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004219}
Josef Bacik6a632092009-02-20 11:00:09 -05004220
Jeff Mahoney1b868262017-05-17 11:38:35 -04004221static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04004222{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004223 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004224 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05004225 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004226
Yan, Zhengb742bb82010-05-16 10:46:24 -04004227 if (data)
4228 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004229 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb82010-05-16 10:46:24 -04004230 flags = BTRFS_BLOCK_GROUP_SYSTEM;
4231 else
4232 flags = BTRFS_BLOCK_GROUP_METADATA;
4233
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004234 ret = get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05004235 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004236}
4237
Jeff Mahoney1b868262017-05-17 11:38:35 -04004238u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info)
4239{
4240 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_DATA);
4241}
4242
4243u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info)
4244{
4245 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4246}
4247
4248u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info)
4249{
4250 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4251}
4252
Liu Bo41361352017-02-13 15:42:21 -08004253static u64 btrfs_space_info_used(struct btrfs_space_info *s_info,
4254 bool may_use_included)
4255{
4256 ASSERT(s_info);
4257 return s_info->bytes_used + s_info->bytes_reserved +
4258 s_info->bytes_pinned + s_info->bytes_readonly +
4259 (may_use_included ? s_info->bytes_may_use : 0);
4260}
4261
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004262int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05004263{
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004264 struct btrfs_root *root = inode->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004265 struct btrfs_fs_info *fs_info = root->fs_info;
Nikolay Borisov1174cad2017-07-11 13:47:50 +03004266 struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
Josef Bacikab6e24102010-03-19 14:38:13 +00004267 u64 used;
Zhao Lei94b947b2015-02-14 13:23:45 +08004268 int ret = 0;
Zhao Leic99f1b02015-03-02 19:32:20 +08004269 int need_commit = 2;
4270 int have_pinned_space;
Josef Bacik6a632092009-02-20 11:00:09 -05004271
4272 /* make sure bytes are sectorsize aligned */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004273 bytes = ALIGN(bytes, fs_info->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05004274
Miao Xie9dced182013-10-25 17:33:36 +08004275 if (btrfs_is_free_space_inode(inode)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004276 need_commit = 0;
Miao Xie9dced182013-10-25 17:33:36 +08004277 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04004278 }
4279
Josef Bacik6a632092009-02-20 11:00:09 -05004280again:
4281 /* make sure we have enough space to handle the data first */
4282 spin_lock(&data_sinfo->lock);
Liu Bo41361352017-02-13 15:42:21 -08004283 used = btrfs_space_info_used(data_sinfo, true);
Josef Bacikab6e24102010-03-19 14:38:13 +00004284
4285 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004286 struct btrfs_trans_handle *trans;
4287
Josef Bacik6a632092009-02-20 11:00:09 -05004288 /*
4289 * if we don't have enough free bytes in this space then we need
4290 * to alloc a new chunk.
4291 */
Zhao Leib9fd47c2015-02-09 14:40:20 +08004292 if (!data_sinfo->full) {
Josef Bacik6a632092009-02-20 11:00:09 -05004293 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05004294
Chris Mason0e4f8f82011-04-15 16:05:44 -04004295 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05004296 spin_unlock(&data_sinfo->lock);
Nikolay Borisov1174cad2017-07-11 13:47:50 +03004297
Jeff Mahoney1b868262017-05-17 11:38:35 -04004298 alloc_target = btrfs_data_alloc_profile(fs_info);
Miao Xie9dced182013-10-25 17:33:36 +08004299 /*
4300 * It is ugly that we don't call nolock join
4301 * transaction for the free space inode case here.
4302 * But it is safe because we only do the data space
4303 * reservation for the free space cache in the
4304 * transaction context, the common join transaction
4305 * just increase the counter of the current transaction
4306 * handler, doesn't try to acquire the trans_lock of
4307 * the fs.
4308 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004309 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004310 if (IS_ERR(trans))
4311 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05004312
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004313 ret = do_chunk_alloc(trans, fs_info, alloc_target,
Chris Mason0e4f8f82011-04-15 16:05:44 -04004314 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004315 btrfs_end_transaction(trans);
Miao Xied52a5b52011-01-05 10:07:18 +00004316 if (ret < 0) {
4317 if (ret != -ENOSPC)
4318 return ret;
Zhao Leic99f1b02015-03-02 19:32:20 +08004319 else {
4320 have_pinned_space = 1;
Miao Xied52a5b52011-01-05 10:07:18 +00004321 goto commit_trans;
Zhao Leic99f1b02015-03-02 19:32:20 +08004322 }
Miao Xied52a5b52011-01-05 10:07:18 +00004323 }
Chris Mason33b4d472009-09-22 14:45:50 -04004324
Josef Bacik6a632092009-02-20 11:00:09 -05004325 goto again;
4326 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004327
4328 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04004329 * If we don't have enough pinned space to deal with this
Zhao Lei94b947b2015-02-14 13:23:45 +08004330 * allocation, and no removed chunk in current transaction,
4331 * don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004332 */
Zhao Leic99f1b02015-03-02 19:32:20 +08004333 have_pinned_space = percpu_counter_compare(
4334 &data_sinfo->total_bytes_pinned,
4335 used + bytes - data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05004336 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004337
4338 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00004339commit_trans:
Nikolay Borisov92e2f7e2018-02-05 10:41:16 +02004340 if (need_commit) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004341 need_commit--;
Josef Bacikb150a4f2013-06-19 15:00:04 -04004342
Zhao Leie1746e82015-12-01 18:39:40 +08004343 if (need_commit > 0) {
4344 btrfs_start_delalloc_roots(fs_info, 0, -1);
Chris Mason6374e57a2017-06-23 09:48:21 -07004345 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004346 (u64)-1);
Zhao Leie1746e82015-12-01 18:39:40 +08004347 }
Zhao Lei9a4e7272015-04-09 12:34:43 +08004348
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004349 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004350 if (IS_ERR(trans))
4351 return PTR_ERR(trans);
Zhao Leic99f1b02015-03-02 19:32:20 +08004352 if (have_pinned_space >= 0 ||
Josef Bacik3204d332015-09-24 10:46:10 -04004353 test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
4354 &trans->transaction->flags) ||
Zhao Leic99f1b02015-03-02 19:32:20 +08004355 need_commit > 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004356 ret = btrfs_commit_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004357 if (ret)
4358 return ret;
Zhao Leid7c15172015-02-26 10:49:20 +08004359 /*
Filipe Mananac2d6cb12016-01-15 11:05:12 +00004360 * The cleaner kthread might still be doing iput
4361 * operations. Wait for it to finish so that
4362 * more space is released.
Zhao Leid7c15172015-02-26 10:49:20 +08004363 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004364 mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
4365 mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
Zhao Lei94b947b2015-02-14 13:23:45 +08004366 goto again;
4367 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004368 btrfs_end_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004369 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004370 }
4371
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004372 trace_btrfs_space_reservation(fs_info,
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004373 "space_info:enospc",
4374 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004375 return -ENOSPC;
4376 }
4377 data_sinfo->bytes_may_use += bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004378 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004379 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004380 spin_unlock(&data_sinfo->lock);
4381
Dongsheng Yang237c0e92014-12-29 06:23:05 -05004382 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004383}
4384
Qu Wenruo364ecf32017-02-27 15:10:38 +08004385int btrfs_check_data_free_space(struct inode *inode,
4386 struct extent_changeset **reserved, u64 start, u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004387{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004388 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004389 int ret;
4390
4391 /* align the range */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004392 len = round_up(start + len, fs_info->sectorsize) -
4393 round_down(start, fs_info->sectorsize);
4394 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004395
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004396 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004397 if (ret < 0)
4398 return ret;
4399
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004400 /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
Qu Wenruo364ecf32017-02-27 15:10:38 +08004401 ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
Qu Wenruo7bc329c2017-02-27 15:10:36 +08004402 if (ret < 0)
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004403 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004404 else
4405 ret = 0;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004406 return ret;
4407}
4408
4409/*
Qu Wenruo4ceff072015-09-08 17:22:42 +08004410 * Called if we need to clear a data reservation for this inode
4411 * Normally in a error case.
4412 *
Qu Wenruo51773be2015-10-08 18:19:37 +08004413 * This one will *NOT* use accurate qgroup reserved space API, just for case
4414 * which we can't sleep and is sure it won't affect qgroup reserved space.
4415 * Like clear_bit_hook().
Qu Wenruo4ceff072015-09-08 17:22:42 +08004416 */
Qu Wenruo51773be2015-10-08 18:19:37 +08004417void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
4418 u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004419{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004420 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004421 struct btrfs_space_info *data_sinfo;
4422
4423 /* Make sure the range is aligned to sectorsize */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004424 len = round_up(start + len, fs_info->sectorsize) -
4425 round_down(start, fs_info->sectorsize);
4426 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004427
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004428 data_sinfo = fs_info->data_sinfo;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004429 spin_lock(&data_sinfo->lock);
4430 if (WARN_ON(data_sinfo->bytes_may_use < len))
4431 data_sinfo->bytes_may_use = 0;
4432 else
4433 data_sinfo->bytes_may_use -= len;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004434 trace_btrfs_space_reservation(fs_info, "space_info",
Qu Wenruo4ceff072015-09-08 17:22:42 +08004435 data_sinfo->flags, len, 0);
4436 spin_unlock(&data_sinfo->lock);
4437}
4438
Qu Wenruo51773be2015-10-08 18:19:37 +08004439/*
4440 * Called if we need to clear a data reservation for this inode
4441 * Normally in a error case.
4442 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04004443 * This one will handle the per-inode data rsv map for accurate reserved
Qu Wenruo51773be2015-10-08 18:19:37 +08004444 * space framework.
4445 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08004446void btrfs_free_reserved_data_space(struct inode *inode,
4447 struct extent_changeset *reserved, u64 start, u64 len)
Qu Wenruo51773be2015-10-08 18:19:37 +08004448{
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004449 struct btrfs_root *root = BTRFS_I(inode)->root;
4450
4451 /* Make sure the range is aligned to sectorsize */
Jeff Mahoneyda170662016-06-15 09:22:56 -04004452 len = round_up(start + len, root->fs_info->sectorsize) -
4453 round_down(start, root->fs_info->sectorsize);
4454 start = round_down(start, root->fs_info->sectorsize);
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004455
Qu Wenruo51773be2015-10-08 18:19:37 +08004456 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruobc42bda2017-02-27 15:10:39 +08004457 btrfs_qgroup_free_data(inode, reserved, start, len);
Qu Wenruo51773be2015-10-08 18:19:37 +08004458}
4459
Josef Bacik97e728d2009-04-21 17:40:57 -04004460static void force_metadata_allocation(struct btrfs_fs_info *info)
4461{
4462 struct list_head *head = &info->space_info;
4463 struct btrfs_space_info *found;
4464
4465 rcu_read_lock();
4466 list_for_each_entry_rcu(found, head, list) {
4467 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004468 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04004469 }
4470 rcu_read_unlock();
4471}
4472
Miao Xie3c76cd82013-04-25 10:12:38 +00004473static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
4474{
4475 return (global->size << 1);
4476}
4477
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004478static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
Josef Bacik698d0082012-09-12 14:08:47 -04004479 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04004480{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004481 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004482 u64 bytes_used = btrfs_space_info_used(sinfo, false);
Chris Masone5bc2452010-10-26 13:37:56 -04004483 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04004484
Chris Mason0e4f8f82011-04-15 16:05:44 -04004485 if (force == CHUNK_ALLOC_FORCE)
4486 return 1;
4487
4488 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04004489 * We need to take into account the global rsv because for all intents
4490 * and purposes it's used space. Don't worry about locking the
4491 * global_rsv, it doesn't change except when the transaction commits.
4492 */
Josef Bacik54338b52012-08-14 16:20:52 -04004493 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004494 bytes_used += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04004495
4496 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04004497 * in limited mode, we want to have some free space up to
4498 * about 1% of the FS size.
4499 */
4500 if (force == CHUNK_ALLOC_LIMITED) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004501 thresh = btrfs_super_total_bytes(fs_info->super_copy);
Byongho Leeee221842015-12-15 01:42:10 +09004502 thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
Chris Mason0e4f8f82011-04-15 16:05:44 -04004503
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004504 if (sinfo->total_bytes - bytes_used < thresh)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004505 return 1;
4506 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004507
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004508 if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04004509 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04004510 return 1;
4511}
4512
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004513static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004514{
4515 u64 num_dev;
4516
David Woodhouse53b381b2013-01-29 18:40:14 -05004517 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
4518 BTRFS_BLOCK_GROUP_RAID0 |
4519 BTRFS_BLOCK_GROUP_RAID5 |
4520 BTRFS_BLOCK_GROUP_RAID6))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004521 num_dev = fs_info->fs_devices->rw_devices;
Liu Bo15d1ff82012-03-29 09:57:44 -04004522 else if (type & BTRFS_BLOCK_GROUP_RAID1)
4523 num_dev = 2;
4524 else
4525 num_dev = 1; /* DUP or single */
4526
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004527 return num_dev;
Liu Bo15d1ff82012-03-29 09:57:44 -04004528}
4529
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004530/*
4531 * If @is_allocation is true, reserve space in the system space info necessary
4532 * for allocating a chunk, otherwise if it's false, reserve space necessary for
4533 * removing a chunk.
4534 */
4535void check_system_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004536 struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004537{
4538 struct btrfs_space_info *info;
4539 u64 left;
4540 u64 thresh;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004541 int ret = 0;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004542 u64 num_devs;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004543
4544 /*
4545 * Needed because we can end up allocating a system chunk and for an
4546 * atomic and race free space reservation in the chunk block reserve.
4547 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004548 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
Liu Bo15d1ff82012-03-29 09:57:44 -04004549
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004550 info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
Liu Bo15d1ff82012-03-29 09:57:44 -04004551 spin_lock(&info->lock);
Liu Bo41361352017-02-13 15:42:21 -08004552 left = info->total_bytes - btrfs_space_info_used(info, true);
Liu Bo15d1ff82012-03-29 09:57:44 -04004553 spin_unlock(&info->lock);
4554
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004555 num_devs = get_profile_num_devs(fs_info, type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004556
4557 /* num_devs device items to update and 1 chunk item to add or remove */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004558 thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) +
4559 btrfs_calc_trans_metadata_size(fs_info, 1);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004560
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004561 if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
4562 btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
4563 left, thresh, type);
4564 dump_space_info(fs_info, info, 0, 0);
Liu Bo15d1ff82012-03-29 09:57:44 -04004565 }
4566
4567 if (left < thresh) {
Jeff Mahoney1b868262017-05-17 11:38:35 -04004568 u64 flags = btrfs_system_alloc_profile(fs_info);
Liu Bo15d1ff82012-03-29 09:57:44 -04004569
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004570 /*
4571 * Ignore failure to create system chunk. We might end up not
4572 * needing it, as we might not need to COW all nodes/leafs from
4573 * the paths we visit in the chunk tree (they were already COWed
4574 * or created in the current transaction for example).
4575 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004576 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004577 }
4578
4579 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004580 ret = btrfs_block_rsv_add(fs_info->chunk_root,
4581 &fs_info->chunk_block_rsv,
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004582 thresh, BTRFS_RESERVE_NO_FLUSH);
4583 if (!ret)
4584 trans->chunk_bytes_reserved += thresh;
Liu Bo15d1ff82012-03-29 09:57:44 -04004585 }
4586}
4587
Liu Bo28b737f2016-07-29 11:09:50 -07004588/*
4589 * If force is CHUNK_ALLOC_FORCE:
4590 * - return 1 if it successfully allocates a chunk,
4591 * - return errors including -ENOSPC otherwise.
4592 * If force is NOT CHUNK_ALLOC_FORCE:
4593 * - return 0 if it doesn't need to allocate a new chunk,
4594 * - return 1 if it successfully allocates a chunk,
4595 * - return errors including -ENOSPC otherwise.
4596 */
Chris Mason6324fbf2008-03-24 15:01:59 -04004597static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004598 struct btrfs_fs_info *fs_info, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04004599{
4600 struct btrfs_space_info *space_info;
Josef Bacik6d741192011-04-11 20:20:11 -04004601 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05004602 int ret = 0;
4603
Josef Bacikc6b305a2012-12-18 09:16:16 -05004604 /* Don't re-enter if we're already allocating a chunk */
4605 if (trans->allocating_chunk)
4606 return -ENOSPC;
4607
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004608 space_info = __find_space_info(fs_info, flags);
Jeff Mahoneydc2d3002018-03-20 15:25:25 -04004609 ASSERT(space_info);
Chris Mason6324fbf2008-03-24 15:01:59 -04004610
Josef Bacik6d741192011-04-11 20:20:11 -04004611again:
Josef Bacik25179202008-10-29 14:49:05 -04004612 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05004613 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004614 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04004615 if (space_info->full) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004616 if (should_alloc_chunk(fs_info, space_info, force))
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004617 ret = -ENOSPC;
4618 else
4619 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04004620 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004621 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04004622 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004623
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004624 if (!should_alloc_chunk(fs_info, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04004625 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04004626 return 0;
4627 } else if (space_info->chunk_alloc) {
4628 wait_for_alloc = 1;
4629 } else {
4630 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04004631 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004632
Josef Bacik25179202008-10-29 14:49:05 -04004633 spin_unlock(&space_info->lock);
4634
Josef Bacik6d741192011-04-11 20:20:11 -04004635 mutex_lock(&fs_info->chunk_mutex);
4636
4637 /*
4638 * The chunk_mutex is held throughout the entirety of a chunk
4639 * allocation, so once we've acquired the chunk_mutex we know that the
4640 * other guy is done and we need to recheck and see if we should
4641 * allocate.
4642 */
4643 if (wait_for_alloc) {
4644 mutex_unlock(&fs_info->chunk_mutex);
4645 wait_for_alloc = 0;
4646 goto again;
4647 }
4648
Josef Bacikc6b305a2012-12-18 09:16:16 -05004649 trans->allocating_chunk = true;
4650
Josef Bacik97e728d2009-04-21 17:40:57 -04004651 /*
Josef Bacik67377732010-09-16 16:19:09 -04004652 * If we have mixed data/metadata chunks we want to make sure we keep
4653 * allocating mixed chunks instead of individual chunks.
4654 */
4655 if (btrfs_mixed_space_info(space_info))
4656 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
4657
4658 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04004659 * if we're doing a data chunk, go ahead and make sure that
4660 * we keep a reasonable number of metadata chunks allocated in the
4661 * FS as well.
4662 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04004663 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04004664 fs_info->data_chunk_allocations++;
4665 if (!(fs_info->data_chunk_allocations %
4666 fs_info->metadata_ratio))
4667 force_metadata_allocation(fs_info);
4668 }
4669
Liu Bo15d1ff82012-03-29 09:57:44 -04004670 /*
4671 * Check if we have enough space in SYSTEM chunk because we may need
4672 * to update devices.
4673 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004674 check_system_chunk(trans, fs_info, flags);
Liu Bo15d1ff82012-03-29 09:57:44 -04004675
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004676 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05004677 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004678
Josef Bacik9ed74f22009-09-11 16:12:44 -04004679 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004680 if (ret < 0 && ret != -ENOSPC)
4681 goto out;
Chris Masond3977122009-01-05 21:25:51 -05004682 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04004683 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04004684 else
4685 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04004686
Chris Mason0e4f8f82011-04-15 16:05:44 -04004687 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004688out:
Josef Bacik6d741192011-04-11 20:20:11 -04004689 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004690 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03004691 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana00d80e32015-07-20 14:56:20 +01004692 /*
4693 * When we allocate a new chunk we reserve space in the chunk block
4694 * reserve to make sure we can COW nodes/leafs in the chunk tree or
4695 * add new nodes/leafs to it if we end up needing to do it when
4696 * inserting the chunk item and updating device items as part of the
4697 * second phase of chunk allocation, performed by
4698 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
4699 * large number of new block groups to create in our transaction
4700 * handle's new_bgs list to avoid exhausting the chunk block reserve
4701 * in extreme cases - like having a single transaction create many new
4702 * block groups when starting to write out the free space caches of all
4703 * the block groups that were made dirty during the lifetime of the
4704 * transaction.
4705 */
Filipe Mananad9a05402015-10-03 13:13:13 +01004706 if (trans->can_flush_pending_bgs &&
Byongho Leeee221842015-12-15 01:42:10 +09004707 trans->chunk_bytes_reserved >= (u64)SZ_2M) {
Nikolay Borisov6c686b32018-02-07 17:55:40 +02004708 btrfs_create_pending_block_groups(trans);
Filipe Manana00d80e32015-07-20 14:56:20 +01004709 btrfs_trans_release_chunk_metadata(trans);
4710 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004711 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04004712}
4713
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004714static int can_overcommit(struct btrfs_fs_info *fs_info,
Josef Bacika80c8dc2012-09-06 16:59:33 -04004715 struct btrfs_space_info *space_info, u64 bytes,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004716 enum btrfs_reserve_flush_enum flush,
4717 bool system_chunk)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004718{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004719 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04004720 u64 profile;
Miao Xie3c76cd82013-04-25 10:12:38 +00004721 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004722 u64 avail;
4723 u64 used;
4724
Josef Bacik957780e2016-05-17 13:30:55 -04004725 /* Don't overcommit when in mixed mode. */
4726 if (space_info->flags & BTRFS_BLOCK_GROUP_DATA)
4727 return 0;
4728
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004729 if (system_chunk)
4730 profile = btrfs_system_alloc_profile(fs_info);
4731 else
4732 profile = btrfs_metadata_alloc_profile(fs_info);
4733
Liu Bo41361352017-02-13 15:42:21 -08004734 used = btrfs_space_info_used(space_info, false);
Josef Bacik96f1bb52013-01-30 17:02:51 -05004735
Josef Bacik96f1bb52013-01-30 17:02:51 -05004736 /*
4737 * We only want to allow over committing if we have lots of actual space
4738 * free, but if we don't have enough space to handle the global reserve
4739 * space then we could end up having a real enospc problem when trying
4740 * to allocate a chunk or some other such important allocation.
4741 */
Miao Xie3c76cd82013-04-25 10:12:38 +00004742 spin_lock(&global_rsv->lock);
4743 space_size = calc_global_rsv_need_space(global_rsv);
4744 spin_unlock(&global_rsv->lock);
4745 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05004746 return 0;
4747
4748 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004749
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004750 avail = atomic64_read(&fs_info->free_chunk_space);
Josef Bacika80c8dc2012-09-06 16:59:33 -04004751
4752 /*
4753 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05004754 * space is actually useable. For raid56, the space info used
4755 * doesn't include the parity drive, so we don't have to
4756 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04004757 */
4758 if (profile & (BTRFS_BLOCK_GROUP_DUP |
4759 BTRFS_BLOCK_GROUP_RAID1 |
4760 BTRFS_BLOCK_GROUP_RAID10))
4761 avail >>= 1;
4762
4763 /*
Miao Xie561c2942012-10-16 11:32:18 +00004764 * If we aren't flushing all things, let us overcommit up to
4765 * 1/2th of the space. If we can flush, don't let us overcommit
4766 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04004767 */
Miao Xie08e007d2012-10-16 11:33:38 +00004768 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04004769 avail >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004770 else
Josef Bacik14575ae2013-09-17 10:48:00 -04004771 avail >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004772
Josef Bacik14575ae2013-09-17 10:48:00 -04004773 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004774 return 1;
4775 return 0;
4776}
4777
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004778static void btrfs_writeback_inodes_sb_nr(struct btrfs_fs_info *fs_info,
Miao Xie6c255e62014-03-06 13:55:01 +08004779 unsigned long nr_pages, int nr_items)
Miao Xieda633a42012-12-20 11:19:09 +00004780{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004781 struct super_block *sb = fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00004782
Josef Bacik925a6ef2013-06-20 12:31:27 -04004783 if (down_read_trylock(&sb->s_umount)) {
4784 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4785 up_read(&sb->s_umount);
4786 } else {
Miao Xieda633a42012-12-20 11:19:09 +00004787 /*
4788 * We needn't worry the filesystem going from r/w to r/o though
4789 * we don't acquire ->s_umount mutex, because the filesystem
4790 * should guarantee the delalloc inodes list be empty after
4791 * the filesystem is readonly(all dirty pages are written to
4792 * the disk).
4793 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004794 btrfs_start_delalloc_roots(fs_info, 0, nr_items);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004795 if (!current->journal_info)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004796 btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1);
Miao Xieda633a42012-12-20 11:19:09 +00004797 }
4798}
4799
Chris Mason6374e57a2017-06-23 09:48:21 -07004800static inline u64 calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004801 u64 to_reclaim)
Miao Xie18cd8ea2013-11-04 23:13:22 +08004802{
4803 u64 bytes;
Chris Mason6374e57a2017-06-23 09:48:21 -07004804 u64 nr;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004805
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004806 bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Chris Mason6374e57a2017-06-23 09:48:21 -07004807 nr = div64_u64(to_reclaim, bytes);
Miao Xie18cd8ea2013-11-04 23:13:22 +08004808 if (!nr)
4809 nr = 1;
4810 return nr;
4811}
4812
Byongho Leeee221842015-12-15 01:42:10 +09004813#define EXTENT_SIZE_PER_ITEM SZ_256K
Miao Xiec61a16a2013-11-04 23:13:23 +08004814
Yan, Zheng5da9d012010-05-16 10:46:25 -04004815/*
4816 * shrink metadata reservation for delalloc
4817 */
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004818static void shrink_delalloc(struct btrfs_fs_info *fs_info, u64 to_reclaim,
4819 u64 orig, bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004820{
Josef Bacik0019f102010-10-15 15:18:40 -04004821 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004822 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004823 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004824 u64 max_reclaim;
Chris Mason6374e57a2017-06-23 09:48:21 -07004825 u64 items;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004826 long time_left;
Miao Xied3ee29e32013-11-04 23:13:20 +08004827 unsigned long nr_pages;
4828 int loops;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004829
Miao Xiec61a16a2013-11-04 23:13:23 +08004830 /* Calc the number of the pages we need flush for space reservation */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004831 items = calc_reclaim_items_nr(fs_info, to_reclaim);
Chris Mason6374e57a2017-06-23 09:48:21 -07004832 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004833
Josef Bacik663350a2011-11-03 22:54:25 -04004834 trans = (struct btrfs_trans_handle *)current->journal_info;
Josef Bacik69fe2d72017-10-19 14:15:57 -04004835 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Chris Masonbf9022e2010-10-26 13:40:45 -04004836
Miao Xie963d6782013-01-29 10:10:51 +00004837 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004838 &fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004839 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004840 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004841 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004842 if (wait_ordered)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004843 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004844 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004845 }
4846
Miao Xied3ee29e32013-11-04 23:13:20 +08004847 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004848 while (delalloc_bytes && loops < 3) {
4849 max_reclaim = min(delalloc_bytes, to_reclaim);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004850 nr_pages = max_reclaim >> PAGE_SHIFT;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004851 btrfs_writeback_inodes_sb_nr(fs_info, nr_pages, items);
Josef Bacikdea31f52012-09-06 16:47:00 -04004852 /*
4853 * We need to wait for the async pages to actually start before
4854 * we do anything.
4855 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004856 max_reclaim = atomic_read(&fs_info->async_delalloc_pages);
Miao Xie9f3a0742013-11-04 23:13:24 +08004857 if (!max_reclaim)
4858 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004859
Miao Xie9f3a0742013-11-04 23:13:24 +08004860 if (max_reclaim <= nr_pages)
4861 max_reclaim = 0;
4862 else
4863 max_reclaim -= nr_pages;
4864
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004865 wait_event(fs_info->async_submit_wait,
4866 atomic_read(&fs_info->async_delalloc_pages) <=
Miao Xie9f3a0742013-11-04 23:13:24 +08004867 (int)max_reclaim);
4868skip_async:
Josef Bacik0019f102010-10-15 15:18:40 -04004869 spin_lock(&space_info->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04004870 if (list_empty(&space_info->tickets) &&
4871 list_empty(&space_info->priority_tickets)) {
4872 spin_unlock(&space_info->lock);
4873 break;
4874 }
Josef Bacik0019f102010-10-15 15:18:40 -04004875 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004876
Chris Mason36e39c42011-03-12 07:08:42 -05004877 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004878 if (wait_ordered && !trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004879 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf104d042011-10-14 13:56:58 -04004880 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004881 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004882 if (time_left)
4883 break;
4884 }
Miao Xie963d6782013-01-29 10:10:51 +00004885 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004886 &fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004887 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004888}
4889
Josef Bacik996478c2017-08-22 16:00:39 -04004890struct reserve_ticket {
4891 u64 bytes;
4892 int error;
4893 struct list_head list;
4894 wait_queue_head_t wait;
4895};
4896
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004897/**
Josef Bacik663350a2011-11-03 22:54:25 -04004898 * maybe_commit_transaction - possibly commit the transaction if its ok to
4899 * @root - the root we're allocating for
4900 * @bytes - the number of bytes we want to reserve
4901 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004902 *
Josef Bacik663350a2011-11-03 22:54:25 -04004903 * This will check to make sure that committing the transaction will actually
4904 * get us somewhere and then commit the transaction if it does. Otherwise it
4905 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004906 */
Jeff Mahoney0c9ab342017-02-15 16:28:28 -05004907static int may_commit_transaction(struct btrfs_fs_info *fs_info,
Josef Bacik996478c2017-08-22 16:00:39 -04004908 struct btrfs_space_info *space_info)
Josef Bacik663350a2011-11-03 22:54:25 -04004909{
Josef Bacik996478c2017-08-22 16:00:39 -04004910 struct reserve_ticket *ticket = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004911 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;
Josef Bacik663350a2011-11-03 22:54:25 -04004912 struct btrfs_trans_handle *trans;
Josef Bacik996478c2017-08-22 16:00:39 -04004913 u64 bytes;
Josef Bacik663350a2011-11-03 22:54:25 -04004914
4915 trans = (struct btrfs_trans_handle *)current->journal_info;
4916 if (trans)
4917 return -EAGAIN;
4918
Josef Bacik996478c2017-08-22 16:00:39 -04004919 spin_lock(&space_info->lock);
4920 if (!list_empty(&space_info->priority_tickets))
4921 ticket = list_first_entry(&space_info->priority_tickets,
4922 struct reserve_ticket, list);
4923 else if (!list_empty(&space_info->tickets))
4924 ticket = list_first_entry(&space_info->tickets,
4925 struct reserve_ticket, list);
4926 bytes = (ticket) ? ticket->bytes : 0;
4927 spin_unlock(&space_info->lock);
4928
4929 if (!bytes)
4930 return 0;
Josef Bacik663350a2011-11-03 22:54:25 -04004931
4932 /* See if there is enough pinned space to make this reservation */
Josef Bacikb150a4f2013-06-19 15:00:04 -04004933 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Miao Xie0424c542014-03-06 13:54:59 +08004934 bytes) >= 0)
Josef Bacik663350a2011-11-03 22:54:25 -04004935 goto commit;
Josef Bacik663350a2011-11-03 22:54:25 -04004936
4937 /*
4938 * See if there is some space in the delayed insertion reservation for
4939 * this reservation.
4940 */
4941 if (space_info != delayed_rsv->space_info)
4942 return -ENOSPC;
4943
4944 spin_lock(&delayed_rsv->lock);
Josef Bacik996478c2017-08-22 16:00:39 -04004945 if (delayed_rsv->size > bytes)
4946 bytes = 0;
4947 else
4948 bytes -= delayed_rsv->size;
Nikolay Borisov057aac32017-11-07 11:22:54 +02004949 spin_unlock(&delayed_rsv->lock);
4950
Josef Bacikb150a4f2013-06-19 15:00:04 -04004951 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Josef Bacik996478c2017-08-22 16:00:39 -04004952 bytes) < 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004953 return -ENOSPC;
4954 }
Josef Bacik663350a2011-11-03 22:54:25 -04004955
4956commit:
Jeff Mahoneya9b33112017-05-17 11:38:34 -04004957 trans = btrfs_join_transaction(fs_info->extent_root);
Josef Bacik663350a2011-11-03 22:54:25 -04004958 if (IS_ERR(trans))
4959 return -ENOSPC;
4960
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004961 return btrfs_commit_transaction(trans);
Josef Bacik663350a2011-11-03 22:54:25 -04004962}
4963
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03004964/*
4965 * Try to flush some data based on policy set by @state. This is only advisory
4966 * and may fail for various reasons. The caller is supposed to examine the
4967 * state of @space_info to detect the outcome.
4968 */
4969static void flush_space(struct btrfs_fs_info *fs_info,
Josef Bacik96c3f432012-06-21 14:05:49 -04004970 struct btrfs_space_info *space_info, u64 num_bytes,
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03004971 int state)
Josef Bacik96c3f432012-06-21 14:05:49 -04004972{
Jeff Mahoneya9b33112017-05-17 11:38:34 -04004973 struct btrfs_root *root = fs_info->extent_root;
Josef Bacik96c3f432012-06-21 14:05:49 -04004974 struct btrfs_trans_handle *trans;
4975 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004976 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004977
4978 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004979 case FLUSH_DELAYED_ITEMS_NR:
4980 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08004981 if (state == FLUSH_DELAYED_ITEMS_NR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004982 nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004983 else
Josef Bacik96c3f432012-06-21 14:05:49 -04004984 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004985
Josef Bacik96c3f432012-06-21 14:05:49 -04004986 trans = btrfs_join_transaction(root);
4987 if (IS_ERR(trans)) {
4988 ret = PTR_ERR(trans);
4989 break;
4990 }
Nikolay Borisove5c304e62018-02-07 17:55:43 +02004991 ret = btrfs_run_delayed_items_nr(trans, nr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004992 btrfs_end_transaction(trans);
Josef Bacik96c3f432012-06-21 14:05:49 -04004993 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004994 case FLUSH_DELALLOC:
4995 case FLUSH_DELALLOC_WAIT:
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03004996 shrink_delalloc(fs_info, num_bytes * 2, num_bytes,
Josef Bacik67b0fd62012-09-24 13:42:00 -04004997 state == FLUSH_DELALLOC_WAIT);
4998 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004999 case ALLOC_CHUNK:
5000 trans = btrfs_join_transaction(root);
5001 if (IS_ERR(trans)) {
5002 ret = PTR_ERR(trans);
5003 break;
5004 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005005 ret = do_chunk_alloc(trans, fs_info,
Jeff Mahoney1b868262017-05-17 11:38:35 -04005006 btrfs_metadata_alloc_profile(fs_info),
Josef Bacikea658ba2012-09-11 16:57:25 -04005007 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005008 btrfs_end_transaction(trans);
Alex Lyakaseecba892015-12-06 12:32:31 +02005009 if (ret > 0 || ret == -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04005010 ret = 0;
5011 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04005012 case COMMIT_TRANS:
Josef Bacik996478c2017-08-22 16:00:39 -04005013 ret = may_commit_transaction(fs_info, space_info);
Josef Bacik96c3f432012-06-21 14:05:49 -04005014 break;
5015 default:
5016 ret = -ENOSPC;
5017 break;
5018 }
5019
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005020 trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, state,
5021 ret);
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03005022 return;
Josef Bacik96c3f432012-06-21 14:05:49 -04005023}
Miao Xie21c7e752014-05-13 17:29:04 -07005024
5025static inline u64
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005026btrfs_calc_reclaim_metadata_size(struct btrfs_fs_info *fs_info,
5027 struct btrfs_space_info *space_info,
5028 bool system_chunk)
Miao Xie21c7e752014-05-13 17:29:04 -07005029{
Josef Bacik957780e2016-05-17 13:30:55 -04005030 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07005031 u64 used;
5032 u64 expected;
Josef Bacik957780e2016-05-17 13:30:55 -04005033 u64 to_reclaim = 0;
Miao Xie21c7e752014-05-13 17:29:04 -07005034
Josef Bacik957780e2016-05-17 13:30:55 -04005035 list_for_each_entry(ticket, &space_info->tickets, list)
5036 to_reclaim += ticket->bytes;
5037 list_for_each_entry(ticket, &space_info->priority_tickets, list)
5038 to_reclaim += ticket->bytes;
5039 if (to_reclaim)
5040 return to_reclaim;
Miao Xie21c7e752014-05-13 17:29:04 -07005041
Wang Xiaoguange0af2482016-08-31 19:46:16 +08005042 to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005043 if (can_overcommit(fs_info, space_info, to_reclaim,
5044 BTRFS_RESERVE_FLUSH_ALL, system_chunk))
Wang Xiaoguange0af2482016-08-31 19:46:16 +08005045 return 0;
5046
Nikolay Borisov0eee8a42017-06-14 11:35:34 +03005047 used = btrfs_space_info_used(space_info, true);
5048
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005049 if (can_overcommit(fs_info, space_info, SZ_1M,
5050 BTRFS_RESERVE_FLUSH_ALL, system_chunk))
Miao Xie21c7e752014-05-13 17:29:04 -07005051 expected = div_factor_fine(space_info->total_bytes, 95);
5052 else
5053 expected = div_factor_fine(space_info->total_bytes, 90);
5054
5055 if (used > expected)
5056 to_reclaim = used - expected;
5057 else
5058 to_reclaim = 0;
5059 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
5060 space_info->bytes_reserved);
Miao Xie21c7e752014-05-13 17:29:04 -07005061 return to_reclaim;
5062}
5063
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005064static inline int need_do_async_reclaim(struct btrfs_fs_info *fs_info,
5065 struct btrfs_space_info *space_info,
5066 u64 used, bool system_chunk)
Miao Xie21c7e752014-05-13 17:29:04 -07005067{
Josef Bacik365c5312015-02-18 13:58:15 -08005068 u64 thresh = div_factor_fine(space_info->total_bytes, 98);
5069
5070 /* If we're just plain full then async reclaim just slows us down. */
Josef Bacikbaee8792016-01-26 09:35:38 -05005071 if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh)
Josef Bacik365c5312015-02-18 13:58:15 -08005072 return 0;
5073
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005074 if (!btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5075 system_chunk))
Liu Bo25ce4592014-09-10 12:58:50 +08005076 return 0;
Liu Bo25ce4592014-09-10 12:58:50 +08005077
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005078 return (used >= thresh && !btrfs_fs_closing(fs_info) &&
5079 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
Miao Xie21c7e752014-05-13 17:29:04 -07005080}
5081
Josef Bacik957780e2016-05-17 13:30:55 -04005082static void wake_all_tickets(struct list_head *head)
Miao Xie21c7e752014-05-13 17:29:04 -07005083{
Josef Bacik957780e2016-05-17 13:30:55 -04005084 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07005085
Josef Bacik957780e2016-05-17 13:30:55 -04005086 while (!list_empty(head)) {
5087 ticket = list_first_entry(head, struct reserve_ticket, list);
5088 list_del_init(&ticket->list);
5089 ticket->error = -ENOSPC;
5090 wake_up(&ticket->wait);
Miao Xie21c7e752014-05-13 17:29:04 -07005091 }
Miao Xie21c7e752014-05-13 17:29:04 -07005092}
5093
Josef Bacik957780e2016-05-17 13:30:55 -04005094/*
5095 * This is for normal flushers, we can wait all goddamned day if we want to. We
5096 * will loop and continuously try to flush as long as we are making progress.
5097 * We count progress as clearing off tickets each time we have to loop.
5098 */
Miao Xie21c7e752014-05-13 17:29:04 -07005099static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
5100{
5101 struct btrfs_fs_info *fs_info;
5102 struct btrfs_space_info *space_info;
5103 u64 to_reclaim;
5104 int flush_state;
Josef Bacik957780e2016-05-17 13:30:55 -04005105 int commit_cycles = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005106 u64 last_tickets_id;
Miao Xie21c7e752014-05-13 17:29:04 -07005107
5108 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
5109 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
5110
Josef Bacik957780e2016-05-17 13:30:55 -04005111 spin_lock(&space_info->lock);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005112 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5113 false);
Josef Bacik957780e2016-05-17 13:30:55 -04005114 if (!to_reclaim) {
5115 space_info->flush = 0;
5116 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005117 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005118 }
Wang Xiaoguangce129652016-09-02 10:58:46 +08005119 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005120 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005121
5122 flush_state = FLUSH_DELAYED_ITEMS_NR;
5123 do {
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03005124 flush_space(fs_info, space_info, to_reclaim, flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005125 spin_lock(&space_info->lock);
5126 if (list_empty(&space_info->tickets)) {
5127 space_info->flush = 0;
5128 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005129 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005130 }
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005131 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info,
5132 space_info,
5133 false);
Wang Xiaoguangce129652016-09-02 10:58:46 +08005134 if (last_tickets_id == space_info->tickets_id) {
Josef Bacik957780e2016-05-17 13:30:55 -04005135 flush_state++;
5136 } else {
Wang Xiaoguangce129652016-09-02 10:58:46 +08005137 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005138 flush_state = FLUSH_DELAYED_ITEMS_NR;
5139 if (commit_cycles)
5140 commit_cycles--;
5141 }
5142
5143 if (flush_state > COMMIT_TRANS) {
5144 commit_cycles++;
5145 if (commit_cycles > 2) {
5146 wake_all_tickets(&space_info->tickets);
5147 space_info->flush = 0;
5148 } else {
5149 flush_state = FLUSH_DELAYED_ITEMS_NR;
5150 }
5151 }
5152 spin_unlock(&space_info->lock);
5153 } while (flush_state <= COMMIT_TRANS);
Miao Xie21c7e752014-05-13 17:29:04 -07005154}
5155
5156void btrfs_init_async_reclaim_work(struct work_struct *work)
5157{
5158 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
5159}
5160
Josef Bacik957780e2016-05-17 13:30:55 -04005161static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
5162 struct btrfs_space_info *space_info,
5163 struct reserve_ticket *ticket)
5164{
5165 u64 to_reclaim;
5166 int flush_state = FLUSH_DELAYED_ITEMS_NR;
5167
5168 spin_lock(&space_info->lock);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005169 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5170 false);
Josef Bacik957780e2016-05-17 13:30:55 -04005171 if (!to_reclaim) {
5172 spin_unlock(&space_info->lock);
5173 return;
5174 }
5175 spin_unlock(&space_info->lock);
5176
5177 do {
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005178 flush_space(fs_info, space_info, to_reclaim, flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005179 flush_state++;
5180 spin_lock(&space_info->lock);
5181 if (ticket->bytes == 0) {
5182 spin_unlock(&space_info->lock);
5183 return;
5184 }
5185 spin_unlock(&space_info->lock);
5186
5187 /*
5188 * Priority flushers can't wait on delalloc without
5189 * deadlocking.
5190 */
5191 if (flush_state == FLUSH_DELALLOC ||
5192 flush_state == FLUSH_DELALLOC_WAIT)
5193 flush_state = ALLOC_CHUNK;
5194 } while (flush_state < COMMIT_TRANS);
5195}
5196
5197static int wait_reserve_ticket(struct btrfs_fs_info *fs_info,
5198 struct btrfs_space_info *space_info,
5199 struct reserve_ticket *ticket, u64 orig_bytes)
5200
5201{
5202 DEFINE_WAIT(wait);
5203 int ret = 0;
5204
5205 spin_lock(&space_info->lock);
5206 while (ticket->bytes > 0 && ticket->error == 0) {
5207 ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE);
5208 if (ret) {
5209 ret = -EINTR;
5210 break;
5211 }
5212 spin_unlock(&space_info->lock);
5213
5214 schedule();
5215
5216 finish_wait(&ticket->wait, &wait);
5217 spin_lock(&space_info->lock);
5218 }
5219 if (!ret)
5220 ret = ticket->error;
5221 if (!list_empty(&ticket->list))
5222 list_del_init(&ticket->list);
5223 if (ticket->bytes && ticket->bytes < orig_bytes) {
5224 u64 num_bytes = orig_bytes - ticket->bytes;
5225 space_info->bytes_may_use -= num_bytes;
5226 trace_btrfs_space_reservation(fs_info, "space_info",
5227 space_info->flags, num_bytes, 0);
5228 }
5229 spin_unlock(&space_info->lock);
5230
5231 return ret;
5232}
5233
Josef Bacik663350a2011-11-03 22:54:25 -04005234/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005235 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5236 * @root - the root we're allocating for
Josef Bacik957780e2016-05-17 13:30:55 -04005237 * @space_info - the space info we want to allocate from
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005238 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04005239 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005240 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04005241 * This will reserve orig_bytes number of bytes from the space info associated
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005242 * with the block_rsv. If there is not enough space it will make an attempt to
5243 * flush out space to make room. It will do this by flushing delalloc if
5244 * possible or committing the transaction. If flush is 0 then no attempts to
5245 * regain reservations will be made and this will fail if there is not enough
5246 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005247 */
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005248static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
Josef Bacik957780e2016-05-17 13:30:55 -04005249 struct btrfs_space_info *space_info,
5250 u64 orig_bytes,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005251 enum btrfs_reserve_flush_enum flush,
5252 bool system_chunk)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005253{
Josef Bacik957780e2016-05-17 13:30:55 -04005254 struct reserve_ticket ticket;
Josef Bacik2bf64752011-09-26 17:12:22 -04005255 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005256 int ret = 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005257
Josef Bacik957780e2016-05-17 13:30:55 -04005258 ASSERT(orig_bytes);
Josef Bacik8ca17f02016-05-27 13:24:13 -04005259 ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL);
5260
Yan, Zhengf0486c62010-05-16 10:46:25 -04005261 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04005262 ret = -ENOSPC;
Liu Bo41361352017-02-13 15:42:21 -08005263 used = btrfs_space_info_used(space_info, true);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005264
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005265 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005266 * If we have enough space then hooray, make our reservation and carry
5267 * on. If not see if we can overcommit, and if we can, hooray carry on.
5268 * If not things get more complicated.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005269 */
Josef Bacik957780e2016-05-17 13:30:55 -04005270 if (used + orig_bytes <= space_info->total_bytes) {
5271 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005272 trace_btrfs_space_reservation(fs_info, "space_info",
5273 space_info->flags, orig_bytes, 1);
Josef Bacik957780e2016-05-17 13:30:55 -04005274 ret = 0;
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005275 } else if (can_overcommit(fs_info, space_info, orig_bytes, flush,
5276 system_chunk)) {
Josef Bacik44734ed2012-09-28 16:04:19 -04005277 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005278 trace_btrfs_space_reservation(fs_info, "space_info",
5279 space_info->flags, orig_bytes, 1);
Josef Bacik44734ed2012-09-28 16:04:19 -04005280 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04005281 }
5282
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005283 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005284 * If we couldn't make a reservation then setup our reservation ticket
5285 * and kick the async worker if it's not already running.
Miao Xie08e007d2012-10-16 11:33:38 +00005286 *
Josef Bacik957780e2016-05-17 13:30:55 -04005287 * If we are a priority flusher then we just need to add our ticket to
5288 * the list and we will do our own flushing further down.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005289 */
Josef Bacik72bcd992012-12-18 15:16:34 -05005290 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacik957780e2016-05-17 13:30:55 -04005291 ticket.bytes = orig_bytes;
5292 ticket.error = 0;
5293 init_waitqueue_head(&ticket.wait);
5294 if (flush == BTRFS_RESERVE_FLUSH_ALL) {
5295 list_add_tail(&ticket.list, &space_info->tickets);
5296 if (!space_info->flush) {
5297 space_info->flush = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005298 trace_btrfs_trigger_flush(fs_info,
Josef Bacikf376df22016-03-25 13:25:56 -04005299 space_info->flags,
5300 orig_bytes, flush,
5301 "enospc");
Josef Bacik957780e2016-05-17 13:30:55 -04005302 queue_work(system_unbound_wq,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005303 &fs_info->async_reclaim_work);
Josef Bacik957780e2016-05-17 13:30:55 -04005304 }
5305 } else {
5306 list_add_tail(&ticket.list,
5307 &space_info->priority_tickets);
5308 }
Miao Xie21c7e752014-05-13 17:29:04 -07005309 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
5310 used += orig_bytes;
Josef Bacikf6acfd52014-09-18 11:27:17 -04005311 /*
5312 * We will do the space reservation dance during log replay,
5313 * which means we won't have fs_info->fs_root set, so don't do
5314 * the async reclaim as we will panic.
5315 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005316 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) &&
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005317 need_do_async_reclaim(fs_info, space_info,
5318 used, system_chunk) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005319 !work_busy(&fs_info->async_reclaim_work)) {
5320 trace_btrfs_trigger_flush(fs_info, space_info->flags,
5321 orig_bytes, flush, "preempt");
Miao Xie21c7e752014-05-13 17:29:04 -07005322 queue_work(system_unbound_wq,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005323 &fs_info->async_reclaim_work);
Josef Bacikf376df22016-03-25 13:25:56 -04005324 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005325 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005326 spin_unlock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00005327 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik957780e2016-05-17 13:30:55 -04005328 return ret;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005329
Josef Bacik957780e2016-05-17 13:30:55 -04005330 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005331 return wait_reserve_ticket(fs_info, space_info, &ticket,
Josef Bacik957780e2016-05-17 13:30:55 -04005332 orig_bytes);
Miao Xie08e007d2012-10-16 11:33:38 +00005333
Josef Bacik957780e2016-05-17 13:30:55 -04005334 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005335 priority_reclaim_metadata_space(fs_info, space_info, &ticket);
Josef Bacik957780e2016-05-17 13:30:55 -04005336 spin_lock(&space_info->lock);
5337 if (ticket.bytes) {
5338 if (ticket.bytes < orig_bytes) {
5339 u64 num_bytes = orig_bytes - ticket.bytes;
5340 space_info->bytes_may_use -= num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005341 trace_btrfs_space_reservation(fs_info, "space_info",
5342 space_info->flags,
5343 num_bytes, 0);
Miao Xie08e007d2012-10-16 11:33:38 +00005344
Josef Bacik957780e2016-05-17 13:30:55 -04005345 }
5346 list_del_init(&ticket.list);
5347 ret = -ENOSPC;
5348 }
5349 spin_unlock(&space_info->lock);
5350 ASSERT(list_empty(&ticket.list));
5351 return ret;
5352}
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005353
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005354/**
5355 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5356 * @root - the root we're allocating for
5357 * @block_rsv - the block_rsv we're allocating for
5358 * @orig_bytes - the number of bytes we want
5359 * @flush - whether or not we can flush to make our reservation
5360 *
Josef Bacik957780e2016-05-17 13:30:55 -04005361 * This will reserve orgi_bytes number of bytes from the space info associated
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005362 * with the block_rsv. If there is not enough space it will make an attempt to
5363 * flush out space to make room. It will do this by flushing delalloc if
5364 * possible or committing the transaction. If flush is 0 then no attempts to
5365 * regain reservations will be made and this will fail if there is not enough
5366 * space already.
5367 */
5368static int reserve_metadata_bytes(struct btrfs_root *root,
5369 struct btrfs_block_rsv *block_rsv,
5370 u64 orig_bytes,
5371 enum btrfs_reserve_flush_enum flush)
5372{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005373 struct btrfs_fs_info *fs_info = root->fs_info;
5374 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04005375 int ret;
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005376 bool system_chunk = (root == fs_info->chunk_root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005377
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005378 ret = __reserve_metadata_bytes(fs_info, block_rsv->space_info,
5379 orig_bytes, flush, system_chunk);
Josef Bacik5d803662013-02-07 16:06:02 -05005380 if (ret == -ENOSPC &&
5381 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
Josef Bacik5d803662013-02-07 16:06:02 -05005382 if (block_rsv != global_rsv &&
5383 !block_rsv_use_bytes(global_rsv, orig_bytes))
5384 ret = 0;
5385 }
Nikolay Borisov9a3daff2017-12-15 12:05:37 +02005386 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005387 trace_btrfs_space_reservation(fs_info, "space_info:enospc",
Josef Bacik957780e2016-05-17 13:30:55 -04005388 block_rsv->space_info->flags,
5389 orig_bytes, 1);
Nikolay Borisov9a3daff2017-12-15 12:05:37 +02005390
5391 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
5392 dump_space_info(fs_info, block_rsv->space_info,
5393 orig_bytes, 0);
5394 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005395 return ret;
5396}
5397
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005398static struct btrfs_block_rsv *get_block_rsv(
5399 const struct btrfs_trans_handle *trans,
5400 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005401{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005402 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik4c13d752011-08-30 11:31:29 -04005403 struct btrfs_block_rsv *block_rsv = NULL;
5404
Alexandru Moisee9cf4392015-09-09 00:18:50 +00005405 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005406 (root == fs_info->csum_root && trans->adding_csums) ||
5407 (root == fs_info->uuid_root))
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02005408 block_rsv = trans->block_rsv;
5409
Josef Bacik4c13d752011-08-30 11:31:29 -04005410 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005411 block_rsv = root->block_rsv;
5412
5413 if (!block_rsv)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005414 block_rsv = &fs_info->empty_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005415
5416 return block_rsv;
5417}
5418
5419static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
5420 u64 num_bytes)
5421{
5422 int ret = -ENOSPC;
5423 spin_lock(&block_rsv->lock);
5424 if (block_rsv->reserved >= num_bytes) {
5425 block_rsv->reserved -= num_bytes;
5426 if (block_rsv->reserved < block_rsv->size)
5427 block_rsv->full = 0;
5428 ret = 0;
5429 }
5430 spin_unlock(&block_rsv->lock);
5431 return ret;
5432}
5433
5434static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
5435 u64 num_bytes, int update_size)
5436{
5437 spin_lock(&block_rsv->lock);
5438 block_rsv->reserved += num_bytes;
5439 if (update_size)
5440 block_rsv->size += num_bytes;
5441 else if (block_rsv->reserved >= block_rsv->size)
5442 block_rsv->full = 1;
5443 spin_unlock(&block_rsv->lock);
5444}
5445
Josef Bacikd52be812013-05-29 14:54:47 -04005446int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
5447 struct btrfs_block_rsv *dest, u64 num_bytes,
5448 int min_factor)
5449{
5450 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5451 u64 min_bytes;
5452
5453 if (global_rsv->space_info != dest->space_info)
5454 return -ENOSPC;
5455
5456 spin_lock(&global_rsv->lock);
5457 min_bytes = div_factor(global_rsv->size, min_factor);
5458 if (global_rsv->reserved < min_bytes + num_bytes) {
5459 spin_unlock(&global_rsv->lock);
5460 return -ENOSPC;
5461 }
5462 global_rsv->reserved -= num_bytes;
5463 if (global_rsv->reserved < global_rsv->size)
5464 global_rsv->full = 0;
5465 spin_unlock(&global_rsv->lock);
5466
5467 block_rsv_add_bytes(dest, num_bytes, 1);
5468 return 0;
5469}
5470
Josef Bacik957780e2016-05-17 13:30:55 -04005471/*
5472 * This is for space we already have accounted in space_info->bytes_may_use, so
5473 * basically when we're returning space from block_rsv's.
5474 */
5475static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
5476 struct btrfs_space_info *space_info,
5477 u64 num_bytes)
5478{
5479 struct reserve_ticket *ticket;
5480 struct list_head *head;
5481 u64 used;
5482 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH;
5483 bool check_overcommit = false;
5484
5485 spin_lock(&space_info->lock);
5486 head = &space_info->priority_tickets;
5487
5488 /*
5489 * If we are over our limit then we need to check and see if we can
5490 * overcommit, and if we can't then we just need to free up our space
5491 * and not satisfy any requests.
5492 */
Nikolay Borisov0eee8a42017-06-14 11:35:34 +03005493 used = btrfs_space_info_used(space_info, true);
Josef Bacik957780e2016-05-17 13:30:55 -04005494 if (used - num_bytes >= space_info->total_bytes)
5495 check_overcommit = true;
5496again:
5497 while (!list_empty(head) && num_bytes) {
5498 ticket = list_first_entry(head, struct reserve_ticket,
5499 list);
5500 /*
5501 * We use 0 bytes because this space is already reserved, so
5502 * adding the ticket space would be a double count.
5503 */
5504 if (check_overcommit &&
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005505 !can_overcommit(fs_info, space_info, 0, flush, false))
Josef Bacik957780e2016-05-17 13:30:55 -04005506 break;
5507 if (num_bytes >= ticket->bytes) {
5508 list_del_init(&ticket->list);
5509 num_bytes -= ticket->bytes;
5510 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005511 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005512 wake_up(&ticket->wait);
5513 } else {
5514 ticket->bytes -= num_bytes;
5515 num_bytes = 0;
5516 }
5517 }
5518
5519 if (num_bytes && head == &space_info->priority_tickets) {
5520 head = &space_info->tickets;
5521 flush = BTRFS_RESERVE_FLUSH_ALL;
5522 goto again;
5523 }
5524 space_info->bytes_may_use -= num_bytes;
5525 trace_btrfs_space_reservation(fs_info, "space_info",
5526 space_info->flags, num_bytes, 0);
5527 spin_unlock(&space_info->lock);
5528}
5529
5530/*
5531 * This is for newly allocated space that isn't accounted in
5532 * space_info->bytes_may_use yet. So if we allocate a chunk or unpin an extent
5533 * we use this helper.
5534 */
5535static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
5536 struct btrfs_space_info *space_info,
5537 u64 num_bytes)
5538{
5539 struct reserve_ticket *ticket;
5540 struct list_head *head = &space_info->priority_tickets;
5541
5542again:
5543 while (!list_empty(head) && num_bytes) {
5544 ticket = list_first_entry(head, struct reserve_ticket,
5545 list);
5546 if (num_bytes >= ticket->bytes) {
5547 trace_btrfs_space_reservation(fs_info, "space_info",
5548 space_info->flags,
5549 ticket->bytes, 1);
5550 list_del_init(&ticket->list);
5551 num_bytes -= ticket->bytes;
5552 space_info->bytes_may_use += ticket->bytes;
5553 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005554 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005555 wake_up(&ticket->wait);
5556 } else {
5557 trace_btrfs_space_reservation(fs_info, "space_info",
5558 space_info->flags,
5559 num_bytes, 1);
5560 space_info->bytes_may_use += num_bytes;
5561 ticket->bytes -= num_bytes;
5562 num_bytes = 0;
5563 }
5564 }
5565
5566 if (num_bytes && head == &space_info->priority_tickets) {
5567 head = &space_info->tickets;
5568 goto again;
5569 }
5570}
5571
Josef Bacik69fe2d72017-10-19 14:15:57 -04005572static u64 block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005573 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02005574 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005575{
5576 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005577 u64 ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005578
5579 spin_lock(&block_rsv->lock);
5580 if (num_bytes == (u64)-1)
5581 num_bytes = block_rsv->size;
5582 block_rsv->size -= num_bytes;
5583 if (block_rsv->reserved >= block_rsv->size) {
5584 num_bytes = block_rsv->reserved - block_rsv->size;
5585 block_rsv->reserved = block_rsv->size;
5586 block_rsv->full = 1;
5587 } else {
5588 num_bytes = 0;
5589 }
5590 spin_unlock(&block_rsv->lock);
5591
Josef Bacik69fe2d72017-10-19 14:15:57 -04005592 ret = num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005593 if (num_bytes > 0) {
5594 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00005595 spin_lock(&dest->lock);
5596 if (!dest->full) {
5597 u64 bytes_to_add;
5598
5599 bytes_to_add = dest->size - dest->reserved;
5600 bytes_to_add = min(num_bytes, bytes_to_add);
5601 dest->reserved += bytes_to_add;
5602 if (dest->reserved >= dest->size)
5603 dest->full = 1;
5604 num_bytes -= bytes_to_add;
5605 }
5606 spin_unlock(&dest->lock);
5607 }
Josef Bacik957780e2016-05-17 13:30:55 -04005608 if (num_bytes)
5609 space_info_add_old_bytes(fs_info, space_info,
5610 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005611 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04005612 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005613}
5614
Josef Bacik25d609f2016-03-25 13:25:48 -04005615int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
5616 struct btrfs_block_rsv *dst, u64 num_bytes,
5617 int update_size)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005618{
5619 int ret;
5620
5621 ret = block_rsv_use_bytes(src, num_bytes);
5622 if (ret)
5623 return ret;
5624
Josef Bacik25d609f2016-03-25 13:25:48 -04005625 block_rsv_add_bytes(dst, num_bytes, update_size);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005626 return 0;
5627}
5628
Miao Xie66d8f3d2012-09-06 04:02:28 -06005629void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005630{
5631 memset(rsv, 0, sizeof(*rsv));
5632 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06005633 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005634}
5635
Josef Bacik69fe2d72017-10-19 14:15:57 -04005636void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
5637 struct btrfs_block_rsv *rsv,
5638 unsigned short type)
5639{
5640 btrfs_init_block_rsv(rsv, type);
5641 rsv->space_info = __find_space_info(fs_info,
5642 BTRFS_BLOCK_GROUP_METADATA);
5643}
5644
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005645struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
Miao Xie66d8f3d2012-09-06 04:02:28 -06005646 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005647{
5648 struct btrfs_block_rsv *block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005649
5650 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
5651 if (!block_rsv)
5652 return NULL;
5653
Josef Bacik69fe2d72017-10-19 14:15:57 -04005654 btrfs_init_metadata_block_rsv(fs_info, block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005655 return block_rsv;
5656}
5657
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005658void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005659 struct btrfs_block_rsv *rsv)
5660{
Josef Bacik2aaa6652012-08-29 14:27:18 -04005661 if (!rsv)
5662 return;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005663 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Josef Bacikdabdb642011-08-08 12:50:18 -04005664 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005665}
5666
Chris Masoncdfb0802015-04-06 18:17:00 -07005667void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
5668{
5669 kfree(rsv);
5670}
5671
Miao Xie08e007d2012-10-16 11:33:38 +00005672int btrfs_block_rsv_add(struct btrfs_root *root,
5673 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
5674 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005675{
5676 int ret;
5677
5678 if (num_bytes == 0)
5679 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005680
Miao Xie61b520a2011-11-10 20:45:05 -05005681 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005682 if (!ret) {
5683 block_rsv_add_bytes(block_rsv, num_bytes, 1);
5684 return 0;
5685 }
5686
Yan, Zhengf0486c62010-05-16 10:46:25 -04005687 return ret;
5688}
5689
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005690int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005691{
5692 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005693 int ret = -ENOSPC;
5694
5695 if (!block_rsv)
5696 return 0;
5697
5698 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04005699 num_bytes = div_factor(block_rsv->size, min_factor);
5700 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005701 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005702 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005703
Josef Bacik36ba0222011-10-18 12:15:48 -04005704 return ret;
5705}
5706
Miao Xie08e007d2012-10-16 11:33:38 +00005707int btrfs_block_rsv_refill(struct btrfs_root *root,
5708 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
5709 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04005710{
5711 u64 num_bytes = 0;
5712 int ret = -ENOSPC;
5713
5714 if (!block_rsv)
5715 return 0;
5716
5717 spin_lock(&block_rsv->lock);
5718 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04005719 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005720 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04005721 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04005722 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005723 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04005724
Yan, Zhengf0486c62010-05-16 10:46:25 -04005725 if (!ret)
5726 return 0;
5727
Miao Xieaa38a712011-11-18 17:43:00 +08005728 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04005729 if (!ret) {
5730 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005731 return 0;
5732 }
5733
Josef Bacik13553e52011-08-08 13:33:21 -04005734 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005735}
5736
Josef Bacik69fe2d72017-10-19 14:15:57 -04005737/**
5738 * btrfs_inode_rsv_refill - refill the inode block rsv.
5739 * @inode - the inode we are refilling.
5740 * @flush - the flusing restriction.
5741 *
5742 * Essentially the same as btrfs_block_rsv_refill, except it uses the
5743 * block_rsv->size as the minimum size. We'll either refill the missing amount
5744 * or return if we already have enough space. This will also handle the resreve
5745 * tracepoint for the reserved amount.
5746 */
Qu Wenruo3f2dd7a2017-11-17 15:14:19 +08005747static int btrfs_inode_rsv_refill(struct btrfs_inode *inode,
5748 enum btrfs_reserve_flush_enum flush)
Josef Bacik69fe2d72017-10-19 14:15:57 -04005749{
5750 struct btrfs_root *root = inode->root;
5751 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
5752 u64 num_bytes = 0;
5753 int ret = -ENOSPC;
5754
5755 spin_lock(&block_rsv->lock);
5756 if (block_rsv->reserved < block_rsv->size)
5757 num_bytes = block_rsv->size - block_rsv->reserved;
5758 spin_unlock(&block_rsv->lock);
5759
5760 if (num_bytes == 0)
5761 return 0;
5762
5763 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
5764 if (!ret) {
5765 block_rsv_add_bytes(block_rsv, num_bytes, 0);
5766 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5767 btrfs_ino(inode), num_bytes, 1);
5768 }
5769 return ret;
5770}
5771
5772/**
5773 * btrfs_inode_rsv_release - release any excessive reservation.
5774 * @inode - the inode we need to release from.
5775 *
5776 * This is the same as btrfs_block_rsv_release, except that it handles the
5777 * tracepoint for the reservation.
5778 */
Nikolay Borisovd2560eb2018-01-11 15:46:44 +02005779static void btrfs_inode_rsv_release(struct btrfs_inode *inode)
Josef Bacik69fe2d72017-10-19 14:15:57 -04005780{
5781 struct btrfs_fs_info *fs_info = inode->root->fs_info;
5782 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5783 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
5784 u64 released = 0;
5785
5786 /*
5787 * Since we statically set the block_rsv->size we just want to say we
5788 * are releasing 0 bytes, and then we'll just get the reservation over
5789 * the size free'd.
5790 */
5791 released = block_rsv_release_bytes(fs_info, block_rsv, global_rsv, 0);
5792 if (released > 0)
5793 trace_btrfs_space_reservation(fs_info, "delalloc",
5794 btrfs_ino(inode), released, 0);
5795}
5796
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005797void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005798 struct btrfs_block_rsv *block_rsv,
5799 u64 num_bytes)
5800{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005801 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5802
Liu Bo17504582013-12-29 21:44:50 +08005803 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04005804 block_rsv->space_info != global_rsv->space_info)
5805 global_rsv = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005806 block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005807}
5808
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005809static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
5810{
5811 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
5812 struct btrfs_space_info *sinfo = block_rsv->space_info;
5813 u64 num_bytes;
5814
Josef Bacikae2e4722016-05-27 12:58:35 -04005815 /*
5816 * The global block rsv is based on the size of the extent tree, the
5817 * checksum tree and the root tree. If the fs is empty we want to set
5818 * it to a minimal amount for safety.
5819 */
5820 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) +
5821 btrfs_root_used(&fs_info->csum_root->root_item) +
5822 btrfs_root_used(&fs_info->tree_root->root_item);
5823 num_bytes = max_t(u64, num_bytes, SZ_16M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005824
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005825 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005826 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005827
Byongho Leeee221842015-12-15 01:42:10 +09005828 block_rsv->size = min_t(u64, num_bytes, SZ_512M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005829
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005830 if (block_rsv->reserved < block_rsv->size) {
Liu Bo41361352017-02-13 15:42:21 -08005831 num_bytes = btrfs_space_info_used(sinfo, true);
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005832 if (sinfo->total_bytes > num_bytes) {
5833 num_bytes = sinfo->total_bytes - num_bytes;
5834 num_bytes = min(num_bytes,
5835 block_rsv->size - block_rsv->reserved);
5836 block_rsv->reserved += num_bytes;
5837 sinfo->bytes_may_use += num_bytes;
5838 trace_btrfs_space_reservation(fs_info, "space_info",
5839 sinfo->flags, num_bytes,
5840 1);
5841 }
5842 } else if (block_rsv->reserved > block_rsv->size) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005843 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04005844 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005845 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005846 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005847 block_rsv->reserved = block_rsv->size;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005848 }
David Sterba182608c2011-05-05 13:13:16 +02005849
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005850 if (block_rsv->reserved == block_rsv->size)
5851 block_rsv->full = 1;
5852 else
5853 block_rsv->full = 0;
5854
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005855 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005856 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005857}
5858
Yan, Zhengf0486c62010-05-16 10:46:25 -04005859static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
5860{
5861 struct btrfs_space_info *space_info;
5862
5863 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
5864 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005865
5866 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005867 fs_info->global_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005868 fs_info->trans_block_rsv.space_info = space_info;
5869 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04005870 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005871
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005872 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
5873 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
5874 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
5875 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00005876 if (fs_info->quota_root)
5877 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005878 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005879
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005880 update_global_block_rsv(fs_info);
5881}
5882
5883static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
5884{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005885 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
5886 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005887 WARN_ON(fs_info->trans_block_rsv.size > 0);
5888 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
5889 WARN_ON(fs_info->chunk_block_rsv.size > 0);
5890 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04005891 WARN_ON(fs_info->delayed_block_rsv.size > 0);
5892 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04005893}
5894
Yan, Zhenga22285a2010-05-16 10:48:46 -04005895
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005896/*
5897 * To be called after all the new block groups attached to the transaction
5898 * handle have been created (btrfs_create_pending_block_groups()).
5899 */
5900void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
5901{
Jeff Mahoney64b63582016-06-20 17:23:41 -04005902 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005903
5904 if (!trans->chunk_bytes_reserved)
5905 return;
5906
5907 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
5908
5909 block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL,
5910 trans->chunk_bytes_reserved);
5911 trans->chunk_bytes_reserved = 0;
5912}
5913
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005914/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04005915int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005916 struct btrfs_inode *inode)
Yan, Zhengd68fc572010-05-16 10:49:58 -04005917{
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005918 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
5919 struct btrfs_root *root = inode->root;
Josef Bacik40acc3ee2016-05-27 13:01:08 -04005920 /*
5921 * We always use trans->block_rsv here as we will have reserved space
5922 * for our orphan when starting the transaction, using get_block_rsv()
5923 * here will sometimes make us choose the wrong block rsv as we could be
5924 * doing a reloc inode for a non refcounted root.
5925 */
5926 struct btrfs_block_rsv *src_rsv = trans->block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005927 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
5928
5929 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04005930 * We need to hold space in order to delete our orphan item once we've
5931 * added it, so this takes the reservation so we can release it later
5932 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04005933 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005934 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5935
Nikolay Borisov9678c542018-01-08 11:45:05 +02005936 trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode),
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005937 num_bytes, 1);
Josef Bacik25d609f2016-03-25 13:25:48 -04005938 return btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005939}
5940
Nikolay Borisov703b3912017-02-20 13:50:40 +02005941void btrfs_orphan_release_metadata(struct btrfs_inode *inode)
Yan, Zhengd68fc572010-05-16 10:49:58 -04005942{
Nikolay Borisov703b3912017-02-20 13:50:40 +02005943 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
5944 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005945 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5946
Nikolay Borisov703b3912017-02-20 13:50:40 +02005947 trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode),
5948 num_bytes, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005949 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, num_bytes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005950}
5951
Miao Xied5c12072013-02-28 10:04:33 +00005952/*
5953 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
5954 * root: the root of the parent directory
5955 * rsv: block reservation
5956 * items: the number of items that we need do reservation
5957 * qgroup_reserved: used to return the reserved size in qgroup
5958 *
5959 * This function is used to reserve the space for snapshot/subvolume
5960 * creation and deletion. Those operations are different with the
5961 * common file/directory operations, they change two fs/file trees
5962 * and root tree, the number of items that the qgroup reserves is
5963 * different with the free space reservation. So we can not use
Nicholas D Steeves01327612016-05-19 21:18:45 -04005964 * the space reservation mechanism in start_transaction().
Miao Xied5c12072013-02-28 10:04:33 +00005965 */
5966int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
5967 struct btrfs_block_rsv *rsv,
5968 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005969 u64 *qgroup_reserved,
5970 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005971{
Miao Xied5c12072013-02-28 10:04:33 +00005972 u64 num_bytes;
5973 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005974 struct btrfs_fs_info *fs_info = root->fs_info;
5975 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00005976
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005977 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Miao Xied5c12072013-02-28 10:04:33 +00005978 /* One for parent inode, two for dir entries */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005979 num_bytes = 3 * fs_info->nodesize;
Jeff Mahoney003d7c52017-01-25 09:50:33 -05005980 ret = btrfs_qgroup_reserve_meta(root, num_bytes, true);
Miao Xied5c12072013-02-28 10:04:33 +00005981 if (ret)
5982 return ret;
5983 } else {
5984 num_bytes = 0;
5985 }
5986
5987 *qgroup_reserved = num_bytes;
5988
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005989 num_bytes = btrfs_calc_trans_metadata_size(fs_info, items);
5990 rsv->space_info = __find_space_info(fs_info,
Miao Xied5c12072013-02-28 10:04:33 +00005991 BTRFS_BLOCK_GROUP_METADATA);
5992 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
5993 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005994
5995 if (ret == -ENOSPC && use_global_rsv)
Josef Bacik25d609f2016-03-25 13:25:48 -04005996 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005997
Qu Wenruo71741092015-09-08 17:22:41 +08005998 if (ret && *qgroup_reserved)
5999 btrfs_qgroup_free_meta(root, *qgroup_reserved);
Miao Xied5c12072013-02-28 10:04:33 +00006000
6001 return ret;
6002}
6003
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006004void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
David Sterba7775c812017-02-10 19:18:18 +01006005 struct btrfs_block_rsv *rsv)
Miao Xied5c12072013-02-28 10:04:33 +00006006{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006007 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006008}
6009
Josef Bacik69fe2d72017-10-19 14:15:57 -04006010static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
6011 struct btrfs_inode *inode)
Josef Bacik9e0baf62011-07-15 15:16:44 +00006012{
Josef Bacik69fe2d72017-10-19 14:15:57 -04006013 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
6014 u64 reserve_size = 0;
6015 u64 csum_leaves;
6016 unsigned outstanding_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006017
Josef Bacik69fe2d72017-10-19 14:15:57 -04006018 lockdep_assert_held(&inode->lock);
6019 outstanding_extents = inode->outstanding_extents;
6020 if (outstanding_extents)
6021 reserve_size = btrfs_calc_trans_metadata_size(fs_info,
6022 outstanding_extents + 1);
6023 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
6024 inode->csum_bytes);
6025 reserve_size += btrfs_calc_trans_metadata_size(fs_info,
6026 csum_leaves);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006027
Josef Bacik69fe2d72017-10-19 14:15:57 -04006028 spin_lock(&block_rsv->lock);
6029 block_rsv->size = reserve_size;
6030 spin_unlock(&block_rsv->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006031}
6032
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006033int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006034{
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006035 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
6036 struct btrfs_root *root = inode->root;
Josef Bacik69fe2d72017-10-19 14:15:57 -04006037 unsigned nr_extents;
Miao Xie08e007d2012-10-16 11:33:38 +00006038 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07006039 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006040 bool delalloc_lock = true;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006041
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006042 /* If we are a free space inode we need to not flush since we will be in
6043 * the middle of a transaction commit. We also don't need the delalloc
6044 * mutex since we won't race with anybody. We need this mostly to make
6045 * lockdep shut its filthy mouth.
Josef Bacikbac357dc2016-07-20 16:48:45 -07006046 *
6047 * If we have a transaction open (can happen if we call truncate_block
6048 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006049 */
6050 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00006051 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006052 delalloc_lock = false;
Nikolay Borisovda07d4a2018-01-12 16:21:05 +02006053 } else {
6054 if (current->journal_info)
6055 flush = BTRFS_RESERVE_FLUSH_LIMIT;
Josef Bacikc09544e2011-08-30 10:19:10 -04006056
Nikolay Borisovda07d4a2018-01-12 16:21:05 +02006057 if (btrfs_transaction_in_commit(fs_info))
6058 schedule_timeout(1);
6059 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006060
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006061 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006062 mutex_lock(&inode->delalloc_mutex);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006063
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006064 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006065
6066 /* Add our new extents and calculate the new rsv size. */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006067 spin_lock(&inode->lock);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006068 nr_extents = count_max_extents(num_bytes);
Josef Bacik8b62f872017-10-19 14:15:55 -04006069 btrfs_mod_outstanding_extents(inode, nr_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006070 inode->csum_bytes += num_bytes;
6071 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006072 spin_unlock(&inode->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05006073
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006074 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Qu Wenruo71741092015-09-08 17:22:41 +08006075 ret = btrfs_qgroup_reserve_meta(root,
Jeff Mahoney003d7c52017-01-25 09:50:33 -05006076 nr_extents * fs_info->nodesize, true);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006077 if (ret)
6078 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00006079 }
Arne Jansenc5567232011-09-14 15:44:05 +02006080
Josef Bacik69fe2d72017-10-19 14:15:57 -04006081 ret = btrfs_inode_rsv_refill(inode, flush);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006082 if (unlikely(ret)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04006083 btrfs_qgroup_free_meta(root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006084 nr_extents * fs_info->nodesize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006085 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006086 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006087
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006088 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006089 mutex_unlock(&inode->delalloc_mutex);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006090 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00006091
6092out_fail:
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006093 spin_lock(&inode->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04006094 nr_extents = count_max_extents(num_bytes);
6095 btrfs_mod_outstanding_extents(inode, -nr_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006096 inode->csum_bytes -= num_bytes;
6097 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006098 spin_unlock(&inode->lock);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006099
Josef Bacik69fe2d72017-10-19 14:15:57 -04006100 btrfs_inode_rsv_release(inode);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006101 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006102 mutex_unlock(&inode->delalloc_mutex);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006103 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006104}
6105
Josef Bacik7709cde2011-08-04 10:25:02 -04006106/**
6107 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
Josef Bacik8b62f872017-10-19 14:15:55 -04006108 * @inode: the inode to release the reservation for.
6109 * @num_bytes: the number of bytes we are releasing.
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 */
Nikolay Borisov691fa052017-02-20 13:50:42 +02006115void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006116{
Nikolay Borisov691fa052017-02-20 13:50:42 +02006117 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006118
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006119 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006120 spin_lock(&inode->lock);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006121 inode->csum_bytes -= num_bytes;
6122 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006123 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006124
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006125 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04006126 return;
6127
Josef Bacik69fe2d72017-10-19 14:15:57 -04006128 btrfs_inode_rsv_release(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006129}
6130
Josef Bacik7709cde2011-08-04 10:25:02 -04006131/**
Josef Bacik8b62f872017-10-19 14:15:55 -04006132 * btrfs_delalloc_release_extents - release our outstanding_extents
6133 * @inode: the inode to balance the reservation for.
6134 * @num_bytes: the number of bytes we originally reserved with
6135 *
6136 * When we reserve space we increase outstanding_extents for the extents we may
6137 * add. Once we've set the range as delalloc or created our ordered extents we
6138 * have outstanding_extents to track the real usage, so we use this to free our
6139 * temporarily tracked outstanding_extents. This _must_ be used in conjunction
6140 * with btrfs_delalloc_reserve_metadata.
6141 */
6142void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes)
6143{
6144 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
6145 unsigned num_extents;
Josef Bacik8b62f872017-10-19 14:15:55 -04006146
6147 spin_lock(&inode->lock);
6148 num_extents = count_max_extents(num_bytes);
6149 btrfs_mod_outstanding_extents(inode, -num_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006150 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Josef Bacik8b62f872017-10-19 14:15:55 -04006151 spin_unlock(&inode->lock);
6152
Josef Bacik8b62f872017-10-19 14:15:55 -04006153 if (btrfs_is_testing(fs_info))
6154 return;
6155
Josef Bacik69fe2d72017-10-19 14:15:57 -04006156 btrfs_inode_rsv_release(inode);
Josef Bacik8b62f872017-10-19 14:15:55 -04006157}
6158
6159/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006160 * btrfs_delalloc_reserve_space - reserve data and metadata space for
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006161 * delalloc
6162 * @inode: inode we're writing to
6163 * @start: start range we are writing to
6164 * @len: how long the range we are writing to
Qu Wenruo364ecf32017-02-27 15:10:38 +08006165 * @reserved: mandatory parameter, record actually reserved qgroup ranges of
6166 * current reservation.
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006167 *
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006168 * This will do the following things
6169 *
6170 * o reserve space in data space info for num bytes
6171 * and reserve precious corresponding qgroup space
6172 * (Done in check_data_free_space)
6173 *
6174 * o reserve space for metadata space, based on the number of outstanding
6175 * extents and how much csums will be needed
6176 * also reserve metadata space in a per root over-reserve method.
6177 * o add to the inodes->delalloc_bytes
6178 * o add it to the fs_info's delalloc inodes list.
6179 * (Above 3 all done in delalloc_reserve_metadata)
6180 *
6181 * Return 0 for success
6182 * Return <0 for error(-ENOSPC or -EQUOT)
6183 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08006184int btrfs_delalloc_reserve_space(struct inode *inode,
6185 struct extent_changeset **reserved, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006186{
6187 int ret;
6188
Qu Wenruo364ecf32017-02-27 15:10:38 +08006189 ret = btrfs_check_data_free_space(inode, reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006190 if (ret < 0)
6191 return ret;
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006192 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006193 if (ret < 0)
Qu Wenruobc42bda2017-02-27 15:10:39 +08006194 btrfs_free_reserved_data_space(inode, *reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006195 return ret;
6196}
6197
6198/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006199 * btrfs_delalloc_release_space - release data and metadata space for delalloc
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006200 * @inode: inode we're releasing space for
6201 * @start: start position of the space already reserved
6202 * @len: the len of the space already reserved
Josef Bacik8b62f872017-10-19 14:15:55 -04006203 * @release_bytes: the len of the space we consumed or didn't use
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006204 *
6205 * This function will release the metadata space that was not used and will
6206 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
6207 * list if there are no delalloc bytes left.
6208 * Also it will handle the qgroup reserved space.
6209 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08006210void btrfs_delalloc_release_space(struct inode *inode,
Josef Bacik8b62f872017-10-19 14:15:55 -04006211 struct extent_changeset *reserved,
6212 u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006213{
Nikolay Borisov691fa052017-02-20 13:50:42 +02006214 btrfs_delalloc_release_metadata(BTRFS_I(inode), len);
Qu Wenruobc42bda2017-02-27 15:10:39 +08006215 btrfs_free_reserved_data_space(inode, reserved, start, len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006216}
6217
Josef Bacikce93ec52014-11-17 15:45:48 -05006218static int update_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6202df62016-06-22 18:54:22 -04006219 struct btrfs_fs_info *info, u64 bytenr,
Josef Bacikce93ec52014-11-17 15:45:48 -05006220 u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04006221{
Josef Bacik0af3d002010-06-21 14:48:16 -04006222 struct btrfs_block_group_cache *cache = NULL;
Chris Masondb945352007-10-15 16:15:53 -04006223 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006224 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04006225 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04006226 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04006227
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006228 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00006229 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02006230 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006231 if (alloc)
6232 old_val += num_bytes;
6233 else
6234 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02006235 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00006236 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006237
Chris Masond3977122009-01-05 21:25:51 -05006238 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04006239 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006240 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006241 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006242 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
6243 BTRFS_BLOCK_GROUP_RAID1 |
6244 BTRFS_BLOCK_GROUP_RAID10))
6245 factor = 2;
6246 else
6247 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04006248 /*
6249 * If this block group has free space cache written out, we
6250 * need to make sure to load it if we are removing space. This
6251 * is because we need the unpinning stage to actually add the
6252 * space back to the block group, otherwise we will leak space.
6253 */
6254 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00006255 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04006256
Chris Masondb945352007-10-15 16:15:53 -04006257 byte_in_group = bytenr - cache->key.objectid;
6258 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04006259
Josef Bacik25179202008-10-29 14:49:05 -04006260 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006261 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04006262
Jeff Mahoney6202df62016-06-22 18:54:22 -04006263 if (btrfs_test_opt(info, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04006264 cache->disk_cache_state < BTRFS_DC_CLEAR)
6265 cache->disk_cache_state = BTRFS_DC_CLEAR;
6266
Chris Mason9078a3e2007-04-26 16:46:15 -04006267 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04006268 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04006269 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04006270 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006271 btrfs_set_block_group_used(&cache->item, old_val);
6272 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006273 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006274 cache->space_info->bytes_used += num_bytes;
6275 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04006276 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006277 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04006278 } else {
Chris Masondb945352007-10-15 16:15:53 -04006279 old_val -= num_bytes;
Filipe Mananaae0ab002014-11-26 15:28:52 +00006280 btrfs_set_block_group_used(&cache->item, old_val);
6281 cache->pinned += num_bytes;
6282 cache->space_info->bytes_pinned += num_bytes;
6283 cache->space_info->bytes_used -= num_bytes;
6284 cache->space_info->disk_used -= num_bytes * factor;
6285 spin_unlock(&cache->lock);
6286 spin_unlock(&cache->space_info->lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04006287
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006288 trace_btrfs_space_reservation(info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006289 cache->space_info->flags,
6290 num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07006291 percpu_counter_add(&cache->space_info->total_bytes_pinned,
6292 num_bytes);
Filipe Mananaae0ab002014-11-26 15:28:52 +00006293 set_extent_dirty(info->pinned_extents,
6294 bytenr, bytenr + num_bytes - 1,
6295 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04006296 }
Chris Mason1bbc6212015-04-06 12:46:08 -07006297
6298 spin_lock(&trans->transaction->dirty_bgs_lock);
6299 if (list_empty(&cache->dirty_list)) {
6300 list_add_tail(&cache->dirty_list,
6301 &trans->transaction->dirty_bgs);
6302 trans->transaction->num_dirty_bgs++;
6303 btrfs_get_block_group(cache);
6304 }
6305 spin_unlock(&trans->transaction->dirty_bgs_lock);
6306
Filipe Manana036a9342015-11-23 15:25:16 +00006307 /*
6308 * No longer have used bytes in this block group, queue it for
6309 * deletion. We do this after adding the block group to the
6310 * dirty list to avoid races between cleaner kthread and space
6311 * cache writeout.
6312 */
6313 if (!alloc && old_val == 0) {
6314 spin_lock(&info->unused_bgs_lock);
6315 if (list_empty(&cache->bg_list)) {
6316 btrfs_get_block_group(cache);
6317 list_add_tail(&cache->bg_list,
6318 &info->unused_bgs);
6319 }
6320 spin_unlock(&info->unused_bgs_lock);
6321 }
6322
Chris Masonfa9c0d792009-04-03 09:47:43 -04006323 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04006324 total -= num_bytes;
6325 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006326 }
6327 return 0;
6328}
Chris Mason6324fbf2008-03-24 15:01:59 -04006329
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006330static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04006331{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006332 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05006333 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006334
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006335 spin_lock(&fs_info->block_group_cache_lock);
6336 bytenr = fs_info->first_logical_byte;
6337 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00006338
6339 if (bytenr < (u64)-1)
6340 return bytenr;
6341
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006342 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006343 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04006344 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006345
Yan Zhengd2fb3432008-12-11 16:30:39 -05006346 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006347 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006348
6349 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04006350}
6351
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006352static int pin_down_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006353 struct btrfs_block_group_cache *cache,
6354 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05006355{
Yan Zheng11833d62009-09-11 16:11:19 -04006356 spin_lock(&cache->space_info->lock);
6357 spin_lock(&cache->lock);
6358 cache->pinned += num_bytes;
6359 cache->space_info->bytes_pinned += num_bytes;
6360 if (reserved) {
6361 cache->reserved -= num_bytes;
6362 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05006363 }
Yan Zheng11833d62009-09-11 16:11:19 -04006364 spin_unlock(&cache->lock);
6365 spin_unlock(&cache->space_info->lock);
6366
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006367 trace_btrfs_space_reservation(fs_info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006368 cache->space_info->flags, num_bytes, 1);
Omar Sandoval4da8b762017-06-06 16:45:28 -07006369 percpu_counter_add(&cache->space_info->total_bytes_pinned, num_bytes);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006370 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006371 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05006372 return 0;
6373}
Chris Mason9078a3e2007-04-26 16:46:15 -04006374
Yan, Zhengf0486c62010-05-16 10:46:25 -04006375/*
6376 * this function must be called within transaction
6377 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006378int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006379 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04006380{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006381 struct btrfs_block_group_cache *cache;
6382
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006383 cache = btrfs_lookup_block_group(fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006384 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006385
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006386 pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006387
6388 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04006389 return 0;
6390}
Zheng Yane8569812008-09-26 10:05:48 -04006391
Yan, Zhengf0486c62010-05-16 10:46:25 -04006392/*
Chris Masone688b7252011-10-31 20:52:39 -04006393 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04006394 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006395int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04006396 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006397{
Chris Masone688b7252011-10-31 20:52:39 -04006398 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04006399 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04006400
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006401 cache = btrfs_lookup_block_group(fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006402 if (!cache)
6403 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04006404
6405 /*
6406 * pull in the free space cache (if any) so that our pin
6407 * removes the free space from the cache. We have load_only set
6408 * to one because the slow code to read in the free extents does check
6409 * the pinned extents.
6410 */
Liu Bof6373bf2012-12-27 09:01:18 +00006411 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04006412
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006413 pin_down_extent(fs_info, cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04006414
6415 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04006416 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04006417 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006418 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04006419}
6420
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006421static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
6422 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006423{
6424 int ret;
6425 struct btrfs_block_group_cache *block_group;
6426 struct btrfs_caching_control *caching_ctl;
6427
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006428 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006429 if (!block_group)
6430 return -EINVAL;
6431
6432 cache_block_group(block_group, 0);
6433 caching_ctl = get_caching_control(block_group);
6434
6435 if (!caching_ctl) {
6436 /* Logic error */
6437 BUG_ON(!block_group_cache_done(block_group));
6438 ret = btrfs_remove_free_space(block_group, start, num_bytes);
6439 } else {
6440 mutex_lock(&caching_ctl->mutex);
6441
6442 if (start >= caching_ctl->progress) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006443 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006444 } else if (start + num_bytes <= caching_ctl->progress) {
6445 ret = btrfs_remove_free_space(block_group,
6446 start, num_bytes);
6447 } else {
6448 num_bytes = caching_ctl->progress - start;
6449 ret = btrfs_remove_free_space(block_group,
6450 start, num_bytes);
6451 if (ret)
6452 goto out_lock;
6453
6454 num_bytes = (start + num_bytes) -
6455 caching_ctl->progress;
6456 start = caching_ctl->progress;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006457 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006458 }
6459out_lock:
6460 mutex_unlock(&caching_ctl->mutex);
6461 put_caching_control(caching_ctl);
6462 }
6463 btrfs_put_block_group(block_group);
6464 return ret;
6465}
6466
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006467int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info,
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006468 struct extent_buffer *eb)
6469{
6470 struct btrfs_file_extent_item *item;
6471 struct btrfs_key key;
6472 int found_type;
6473 int i;
6474
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006475 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006476 return 0;
6477
6478 for (i = 0; i < btrfs_header_nritems(eb); i++) {
6479 btrfs_item_key_to_cpu(eb, &key, i);
6480 if (key.type != BTRFS_EXTENT_DATA_KEY)
6481 continue;
6482 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
6483 found_type = btrfs_file_extent_type(eb, item);
6484 if (found_type == BTRFS_FILE_EXTENT_INLINE)
6485 continue;
6486 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
6487 continue;
6488 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
6489 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006490 __exclude_logged_extent(fs_info, key.objectid, key.offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006491 }
6492
6493 return 0;
6494}
6495
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006496static void
6497btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg)
6498{
6499 atomic_inc(&bg->reservations);
6500}
6501
6502void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
6503 const u64 start)
6504{
6505 struct btrfs_block_group_cache *bg;
6506
6507 bg = btrfs_lookup_block_group(fs_info, start);
6508 ASSERT(bg);
6509 if (atomic_dec_and_test(&bg->reservations))
6510 wake_up_atomic_t(&bg->reservations);
6511 btrfs_put_block_group(bg);
6512}
6513
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006514void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6515{
6516 struct btrfs_space_info *space_info = bg->space_info;
6517
6518 ASSERT(bg->ro);
6519
6520 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
6521 return;
6522
6523 /*
6524 * Our block group is read only but before we set it to read only,
6525 * some task might have had allocated an extent from it already, but it
6526 * has not yet created a respective ordered extent (and added it to a
6527 * root's list of ordered extents).
6528 * Therefore wait for any task currently allocating extents, since the
6529 * block group's reservations counter is incremented while a read lock
6530 * on the groups' semaphore is held and decremented after releasing
6531 * the read access on that semaphore and creating the ordered extent.
6532 */
6533 down_write(&space_info->groups_sem);
6534 up_write(&space_info->groups_sem);
6535
David Howells5e4def22017-11-02 15:27:44 +00006536 wait_on_atomic_t(&bg->reservations, atomic_t_wait,
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006537 TASK_UNINTERRUPTIBLE);
6538}
6539
Josef Bacikfb25e912011-07-26 17:00:46 -04006540/**
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006541 * btrfs_add_reserved_bytes - update the block_group and space info counters
Josef Bacikfb25e912011-07-26 17:00:46 -04006542 * @cache: The cache we are manipulating
Wang Xiaoguang18513092016-07-25 15:51:40 +08006543 * @ram_bytes: The number of bytes of file content, and will be same to
6544 * @num_bytes except for the compress path.
Josef Bacikfb25e912011-07-26 17:00:46 -04006545 * @num_bytes: The number of bytes in question
Miao Xiee570fd22014-06-19 10:42:50 +08006546 * @delalloc: The blocks are allocated for the delalloc write
Josef Bacikfb25e912011-07-26 17:00:46 -04006547 *
Xiaoguang Wang745699e2016-09-23 12:38:50 +08006548 * This is called by the allocator when it reserves space. If this is a
6549 * reservation and the block group has become read only we cannot make the
6550 * reservation and return -EAGAIN, otherwise this function always succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04006551 */
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006552static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
Wang Xiaoguang18513092016-07-25 15:51:40 +08006553 u64 ram_bytes, u64 num_bytes, int delalloc)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006554{
Josef Bacikfb25e912011-07-26 17:00:46 -04006555 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006556 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006557
Josef Bacikfb25e912011-07-26 17:00:46 -04006558 spin_lock(&space_info->lock);
6559 spin_lock(&cache->lock);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006560 if (cache->ro) {
6561 ret = -EAGAIN;
Josef Bacikfb25e912011-07-26 17:00:46 -04006562 } else {
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006563 cache->reserved += num_bytes;
6564 space_info->bytes_reserved += num_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006565
Wang Xiaoguang18513092016-07-25 15:51:40 +08006566 trace_btrfs_space_reservation(cache->fs_info,
6567 "space_info", space_info->flags,
6568 ram_bytes, 0);
6569 space_info->bytes_may_use -= ram_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006570 if (delalloc)
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006571 cache->delalloc_bytes += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006572 }
Josef Bacikfb25e912011-07-26 17:00:46 -04006573 spin_unlock(&cache->lock);
6574 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006575 return ret;
6576}
6577
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006578/**
6579 * btrfs_free_reserved_bytes - update the block_group and space info counters
6580 * @cache: The cache we are manipulating
6581 * @num_bytes: The number of bytes in question
6582 * @delalloc: The blocks are allocated for the delalloc write
6583 *
6584 * This is called by somebody who is freeing space that was never actually used
6585 * on disk. For example if you reserve some space for a new leaf in transaction
6586 * A and before transaction A commits you free that leaf, you call this with
6587 * reserve set to 0 in order to clear the reservation.
6588 */
6589
6590static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
6591 u64 num_bytes, int delalloc)
6592{
6593 struct btrfs_space_info *space_info = cache->space_info;
6594 int ret = 0;
6595
6596 spin_lock(&space_info->lock);
6597 spin_lock(&cache->lock);
6598 if (cache->ro)
6599 space_info->bytes_readonly += num_bytes;
6600 cache->reserved -= num_bytes;
6601 space_info->bytes_reserved -= num_bytes;
6602
6603 if (delalloc)
6604 cache->delalloc_bytes -= num_bytes;
6605 spin_unlock(&cache->lock);
6606 spin_unlock(&space_info->lock);
6607 return ret;
6608}
David Sterba8b74c032017-02-10 19:20:56 +01006609void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04006610{
Yan Zheng11833d62009-09-11 16:11:19 -04006611 struct btrfs_caching_control *next;
6612 struct btrfs_caching_control *caching_ctl;
6613 struct btrfs_block_group_cache *cache;
6614
Josef Bacik9e351cc2014-03-13 15:42:13 -04006615 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04006616
6617 list_for_each_entry_safe(caching_ctl, next,
6618 &fs_info->caching_block_groups, list) {
6619 cache = caching_ctl->block_group;
6620 if (block_group_cache_done(cache)) {
6621 cache->last_byte_to_unpin = (u64)-1;
6622 list_del_init(&caching_ctl->list);
6623 put_caching_control(caching_ctl);
6624 } else {
6625 cache->last_byte_to_unpin = caching_ctl->progress;
6626 }
6627 }
6628
6629 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6630 fs_info->pinned_extents = &fs_info->freed_extents[1];
6631 else
6632 fs_info->pinned_extents = &fs_info->freed_extents[0];
6633
Josef Bacik9e351cc2014-03-13 15:42:13 -04006634 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006635
6636 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04006637}
6638
Josef Bacikc759c4e2015-10-02 15:25:10 -04006639/*
6640 * Returns the free cluster for the given space info and sets empty_cluster to
6641 * what it should be based on the mount options.
6642 */
6643static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006644fetch_cluster_info(struct btrfs_fs_info *fs_info,
6645 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04006646{
6647 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006648
6649 *empty_cluster = 0;
6650 if (btrfs_mixed_space_info(space_info))
6651 return ret;
6652
Josef Bacikc759c4e2015-10-02 15:25:10 -04006653 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006654 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006655 if (btrfs_test_opt(fs_info, SSD))
6656 *empty_cluster = SZ_2M;
6657 else
Byongho Leeee221842015-12-15 01:42:10 +09006658 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006659 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
6660 btrfs_test_opt(fs_info, SSD_SPREAD)) {
6661 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006662 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006663 }
6664
6665 return ret;
6666}
6667
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006668static int unpin_extent_range(struct btrfs_fs_info *fs_info,
6669 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00006670 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04006671{
Yan Zheng11833d62009-09-11 16:11:19 -04006672 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04006673 struct btrfs_space_info *space_info;
6674 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006675 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04006676 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006677 u64 total_unpinned = 0;
6678 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04006679 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04006680
6681 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04006682 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04006683 if (!cache ||
6684 start >= cache->key.objectid + cache->key.offset) {
6685 if (cache)
6686 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04006687 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006688 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006689 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04006690
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006691 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04006692 cache->space_info,
6693 &empty_cluster);
6694 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04006695 }
6696
6697 len = cache->key.objectid + cache->key.offset - start;
6698 len = min(len, end + 1 - start);
6699
6700 if (start < cache->last_byte_to_unpin) {
6701 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00006702 if (return_free_space)
6703 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04006704 }
Josef Bacik25179202008-10-29 14:49:05 -04006705
Yan, Zhengf0486c62010-05-16 10:46:25 -04006706 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006707 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006708 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006709
Josef Bacikc759c4e2015-10-02 15:25:10 -04006710 /*
6711 * If this space cluster has been marked as fragmented and we've
6712 * unpinned enough in this block group to potentially allow a
6713 * cluster to be created inside of it go ahead and clear the
6714 * fragmented check.
6715 */
6716 if (cluster && cluster->fragmented &&
6717 total_unpinned > empty_cluster) {
6718 spin_lock(&cluster->lock);
6719 cluster->fragmented = 0;
6720 spin_unlock(&cluster->lock);
6721 }
6722
Josef Bacik7b398f82012-10-22 15:52:28 -04006723 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006724 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006725 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006726 space_info->bytes_pinned -= len;
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006727
6728 trace_btrfs_space_reservation(fs_info, "pinned",
6729 space_info->flags, len, 0);
Josef Bacik4f4db212015-09-29 11:40:47 -04006730 space_info->max_extent_size = 0;
Liu Bod288db52014-07-02 16:58:01 +08006731 percpu_counter_add(&space_info->total_bytes_pinned, -len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006732 if (cache->ro) {
6733 space_info->bytes_readonly += len;
6734 readonly = true;
6735 }
Josef Bacik25179202008-10-29 14:49:05 -04006736 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006737 if (!readonly && return_free_space &&
6738 global_rsv->space_info == space_info) {
6739 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03006740
Josef Bacik7b398f82012-10-22 15:52:28 -04006741 spin_lock(&global_rsv->lock);
6742 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04006743 to_add = min(len, global_rsv->size -
6744 global_rsv->reserved);
6745 global_rsv->reserved += to_add;
6746 space_info->bytes_may_use += to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006747 if (global_rsv->reserved >= global_rsv->size)
6748 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04006749 trace_btrfs_space_reservation(fs_info,
6750 "space_info",
6751 space_info->flags,
6752 to_add, 1);
6753 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006754 }
6755 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006756 /* Add to any tickets we may have */
6757 if (len)
6758 space_info_add_new_bytes(fs_info, space_info,
6759 len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006760 }
6761 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006762 }
6763
6764 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006765 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04006766 return 0;
6767}
6768
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02006769int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
Chris Masona28ec192007-03-06 20:08:01 -05006770{
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02006771 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006772 struct btrfs_block_group_cache *block_group, *tmp;
6773 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04006774 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04006775 u64 start;
6776 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05006777 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05006778
Yan Zheng11833d62009-09-11 16:11:19 -04006779 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6780 unpin = &fs_info->freed_extents[1];
6781 else
6782 unpin = &fs_info->freed_extents[0];
6783
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006784 while (!trans->aborted) {
Filipe Mananad4b450c2015-01-29 19:18:25 +00006785 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04006786 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04006787 EXTENT_DIRTY, NULL);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006788 if (ret) {
6789 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05006790 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00006791 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006792
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006793 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006794 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00006795 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006796
David Sterbaaf6f8f62016-04-26 23:54:39 +02006797 clear_extent_dirty(unpin, start, end);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006798 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006799 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masonb9473432009-03-13 11:00:37 -04006800 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05006801 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006802
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006803 /*
6804 * Transaction is finished. We don't need the lock anymore. We
6805 * do need to clean up the block groups in case of a transaction
6806 * abort.
6807 */
6808 deleted_bgs = &trans->transaction->deleted_bgs;
6809 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
6810 u64 trimmed = 0;
6811
6812 ret = -EROFS;
6813 if (!trans->aborted)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006814 ret = btrfs_discard_extent(fs_info,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006815 block_group->key.objectid,
6816 block_group->key.offset,
6817 &trimmed);
6818
6819 list_del_init(&block_group->bg_list);
6820 btrfs_put_block_group_trimming(block_group);
6821 btrfs_put_block_group(block_group);
6822
6823 if (ret) {
6824 const char *errstr = btrfs_decode_error(ret);
6825 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02006826 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006827 ret, errstr);
6828 }
6829 }
6830
Chris Masone20d96d2007-03-22 12:13:20 -04006831 return 0;
6832}
6833
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006834static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006835 struct btrfs_fs_info *info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006836 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006837 u64 root_objectid, u64 owner_objectid,
6838 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006839 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05006840{
Chris Masone2fa7222007-03-12 16:22:34 -04006841 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006842 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04006843 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04006844 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006845 struct btrfs_extent_item *ei;
6846 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05006847 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006848 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05006849 int extent_slot = 0;
6850 int found_extent = 0;
6851 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006852 u32 item_size;
6853 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006854 u64 bytenr = node->bytenr;
6855 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07006856 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006857 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05006858
Chris Mason5caf2a02007-04-02 11:20:42 -04006859 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04006860 if (!path)
6861 return -ENOMEM;
6862
David Sterbae4058b52015-11-27 16:31:35 +01006863 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04006864 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006865
6866 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
6867 BUG_ON(!is_data && refs_to_drop != 1);
6868
Josef Bacik3173a182013-03-07 14:22:04 -05006869 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02006870 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05006871
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05006872 ret = lookup_extent_backref(trans, info, path, &iref,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006873 bytenr, num_bytes, parent,
6874 root_objectid, owner_objectid,
6875 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05006876 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05006877 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006878 while (extent_slot >= 0) {
6879 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05006880 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006881 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05006882 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006883 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
6884 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05006885 found_extent = 1;
6886 break;
6887 }
Josef Bacik3173a182013-03-07 14:22:04 -05006888 if (key.type == BTRFS_METADATA_ITEM_KEY &&
6889 key.offset == owner_objectid) {
6890 found_extent = 1;
6891 break;
6892 }
Chris Mason952fcca2008-02-18 16:33:44 -05006893 if (path->slots[0] - extent_slot > 5)
6894 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006895 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05006896 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006897#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6898 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
6899 if (found_extent && item_size < sizeof(*ei))
6900 found_extent = 0;
6901#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04006902 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006903 BUG_ON(iref);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05006904 ret = remove_extent_backref(trans, info, path, NULL,
6905 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07006906 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06006907 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006908 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006909 goto out;
6910 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006911 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04006912 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006913
6914 key.objectid = bytenr;
6915 key.type = BTRFS_EXTENT_ITEM_KEY;
6916 key.offset = num_bytes;
6917
Josef Bacik3173a182013-03-07 14:22:04 -05006918 if (!is_data && skinny_metadata) {
6919 key.type = BTRFS_METADATA_ITEM_KEY;
6920 key.offset = owner_objectid;
6921 }
6922
Zheng Yan31840ae2008-09-23 13:14:14 -04006923 ret = btrfs_search_slot(trans, extent_root,
6924 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05006925 if (ret > 0 && skinny_metadata && path->slots[0]) {
6926 /*
6927 * Couldn't find our skinny metadata item,
6928 * see if we have ye olde extent item.
6929 */
6930 path->slots[0]--;
6931 btrfs_item_key_to_cpu(path->nodes[0], &key,
6932 path->slots[0]);
6933 if (key.objectid == bytenr &&
6934 key.type == BTRFS_EXTENT_ITEM_KEY &&
6935 key.offset == num_bytes)
6936 ret = 0;
6937 }
6938
6939 if (ret > 0 && skinny_metadata) {
6940 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01006941 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05006942 key.type = BTRFS_EXTENT_ITEM_KEY;
6943 key.offset = num_bytes;
6944 btrfs_release_path(path);
6945 ret = btrfs_search_slot(trans, extent_root,
6946 &key, path, -1, 1);
6947 }
6948
Josef Bacikf3465ca2008-11-12 14:19:50 -05006949 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006950 btrfs_err(info,
6951 "umm, got %d back from search, was looking for %llu",
6952 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00006953 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02006954 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006955 }
David Sterba005d6422012-09-18 07:52:32 -06006956 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006957 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006958 goto out;
6959 }
Zheng Yan31840ae2008-09-23 13:14:14 -04006960 extent_slot = path->slots[0];
6961 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306962 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02006963 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006964 btrfs_err(info,
6965 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006966 bytenr, parent, root_objectid, owner_objectid,
6967 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04006968 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04006969 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006970 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04006971 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006972 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05006973 }
Chris Mason5f39d392007-10-15 16:14:19 -04006974
6975 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006976 item_size = btrfs_item_size_nr(leaf, extent_slot);
6977#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6978 if (item_size < sizeof(*ei)) {
6979 BUG_ON(found_extent || extent_slot != path->slots[0]);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05006980 ret = convert_extent_item_v0(trans, info, path, owner_objectid,
6981 0);
David Sterba005d6422012-09-18 07:52:32 -06006982 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006983 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006984 goto out;
6985 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006986
David Sterbab3b4aa72011-04-21 01:20:15 +02006987 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006988 path->leave_spinning = 1;
6989
6990 key.objectid = bytenr;
6991 key.type = BTRFS_EXTENT_ITEM_KEY;
6992 key.offset = num_bytes;
6993
6994 ret = btrfs_search_slot(trans, extent_root, &key, path,
6995 -1, 1);
6996 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006997 btrfs_err(info,
6998 "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006999 ret, bytenr);
David Sterbaa4f78752017-06-29 18:37:49 +02007000 btrfs_print_leaf(path->nodes[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007001 }
David Sterba005d6422012-09-18 07:52:32 -06007002 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007003 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007004 goto out;
7005 }
7006
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007007 extent_slot = path->slots[0];
7008 leaf = path->nodes[0];
7009 item_size = btrfs_item_size_nr(leaf, extent_slot);
7010 }
7011#endif
7012 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05007013 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04007014 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05007015 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
7016 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007017 struct btrfs_tree_block_info *bi;
7018 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
7019 bi = (struct btrfs_tree_block_info *)(ei + 1);
7020 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05007021 }
7022
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007023 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04007024 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007025 btrfs_err(info,
7026 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
7027 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04007028 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04007029 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04007030 goto out;
7031 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007032 refs -= refs_to_drop;
7033
7034 if (refs > 0) {
7035 if (extent_op)
7036 __run_delayed_extent_op(extent_op, leaf, ei);
7037 /*
7038 * In the case of inline back ref, reference count will
7039 * be updated by remove_extent_backref
7040 */
7041 if (iref) {
7042 BUG_ON(!found_extent);
7043 } else {
7044 btrfs_set_extent_refs(leaf, ei, refs);
7045 btrfs_mark_buffer_dirty(leaf);
7046 }
7047 if (found_extent) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05007048 ret = remove_extent_backref(trans, info, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007049 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07007050 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06007051 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007052 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007053 goto out;
7054 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007055 }
7056 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007057 if (found_extent) {
7058 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08007059 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007060 if (iref) {
7061 BUG_ON(path->slots[0] != extent_slot);
7062 } else {
7063 BUG_ON(path->slots[0] != extent_slot + 1);
7064 path->slots[0] = extent_slot;
7065 num_to_del = 2;
7066 }
Chris Mason78fae272007-03-25 11:35:08 -04007067 }
Chris Masonb9473432009-03-13 11:00:37 -04007068
Josef Bacikfcebe452014-05-13 17:30:47 -07007069 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05007070 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
7071 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06007072 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007073 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007074 goto out;
7075 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007076 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01007077
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007078 if (is_data) {
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007079 ret = btrfs_del_csums(trans, info, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06007080 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007081 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007082 goto out;
7083 }
Chris Mason459931e2008-12-10 09:10:46 -05007084 }
7085
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007086 ret = add_to_free_space_tree(trans, info, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007087 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007088 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007089 goto out;
7090 }
7091
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007092 ret = update_block_group(trans, info, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06007093 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007094 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007095 goto out;
7096 }
Chris Masona28ec192007-03-06 20:08:01 -05007097 }
Josef Bacikfcebe452014-05-13 17:30:47 -07007098 btrfs_release_path(path);
7099
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007100out:
Chris Mason5caf2a02007-04-02 11:20:42 -04007101 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05007102 return ret;
7103}
7104
7105/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007106 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04007107 * delayed ref for that extent as well. This searches the delayed ref tree for
7108 * a given extent, and if there are no other delayed refs to be processed, it
7109 * removes it from the tree.
7110 */
7111static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007112 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04007113{
7114 struct btrfs_delayed_ref_head *head;
7115 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007116 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04007117
7118 delayed_refs = &trans->transaction->delayed_refs;
7119 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08007120 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04007121 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08007122 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04007123
Josef Bacikd7df2c72014-01-23 09:21:38 -05007124 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04007125 if (!RB_EMPTY_ROOT(&head->ref_tree))
Chris Mason1887be62009-03-13 10:11:24 -04007126 goto out;
7127
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007128 if (head->extent_op) {
7129 if (!head->must_insert_reserved)
7130 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00007131 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007132 head->extent_op = NULL;
7133 }
7134
Chris Mason1887be62009-03-13 10:11:24 -04007135 /*
7136 * waiting for the lock here would deadlock. If someone else has it
7137 * locked they are already in the process of dropping it anyway
7138 */
7139 if (!mutex_trylock(&head->mutex))
7140 goto out;
7141
7142 /*
7143 * at this point we have a head with no other entries. Go
7144 * ahead and process it.
7145 */
Liu Boc46effa2013-10-14 12:59:45 +08007146 rb_erase(&head->href_node, &delayed_refs->href_root);
Josef Bacikd2788502017-09-29 15:43:57 -04007147 RB_CLEAR_NODE(&head->href_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05007148 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04007149
7150 /*
7151 * we don't take a ref on the node because we're removing it from the
7152 * tree, so we just steal the ref the tree was holding.
7153 */
Chris Masonc3e69d52009-03-13 10:17:05 -04007154 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007155 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04007156 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007157 head->processing = 0;
7158 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04007159 spin_unlock(&delayed_refs->lock);
7160
Yan, Zhengf0486c62010-05-16 10:46:25 -04007161 BUG_ON(head->extent_op);
7162 if (head->must_insert_reserved)
7163 ret = 1;
7164
7165 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04007166 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007167 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04007168out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05007169 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08007170
7171out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04007172 spin_unlock(&delayed_refs->lock);
7173 return 0;
7174}
7175
Yan, Zhengf0486c62010-05-16 10:46:25 -04007176void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7177 struct btrfs_root *root,
7178 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02007179 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007180{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007181 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb150a4f2013-06-19 15:00:04 -04007182 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007183 int ret;
7184
7185 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07007186 int old_ref_mod, new_ref_mod;
7187
Josef Bacikfd708b82017-09-29 15:43:50 -04007188 btrfs_ref_tree_mod(root, buf->start, buf->len, parent,
7189 root->root_key.objectid,
7190 btrfs_header_level(buf), 0,
7191 BTRFS_DROP_DELAYED_REF);
Omar Sandoval7be07912017-06-06 16:45:30 -07007192 ret = btrfs_add_delayed_tree_ref(fs_info, trans, buf->start,
7193 buf->len, parent,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007194 root->root_key.objectid,
7195 btrfs_header_level(buf),
Omar Sandoval7be07912017-06-06 16:45:30 -07007196 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007197 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007198 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07007199 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007200 }
7201
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007202 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
Filipe Manana62198722015-01-06 20:18:45 +00007203 struct btrfs_block_group_cache *cache;
7204
Yan, Zhengf0486c62010-05-16 10:46:25 -04007205 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007206 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007207 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04007208 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007209 }
7210
Omar Sandoval4da8b762017-06-06 16:45:28 -07007211 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007212 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00007213
Yan, Zhengf0486c62010-05-16 10:46:25 -04007214 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007215 pin_down_extent(fs_info, cache, buf->start,
7216 buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00007217 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04007218 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007219 }
7220
7221 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
7222
7223 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08007224 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00007225 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007226 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007227 }
7228out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04007229 if (pin)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007230 add_pinned_bytes(fs_info, buf->len, btrfs_header_level(buf),
Josef Bacikb150a4f2013-06-19 15:00:04 -04007231 root->root_key.objectid);
7232
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007233 if (last_ref) {
7234 /*
7235 * Deleting the buffer, clear the corrupt flag since it doesn't
7236 * matter anymore.
7237 */
7238 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
7239 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007240}
7241
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007242/* Can return -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007243int btrfs_free_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007244 struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007245 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01007246 u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04007247{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007248 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07007249 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04007250 int ret;
7251
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04007252 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007253 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02007254
Josef Bacikfd708b82017-09-29 15:43:50 -04007255 if (root_objectid != BTRFS_TREE_LOG_OBJECTID)
7256 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent,
7257 root_objectid, owner, offset,
7258 BTRFS_DROP_DELAYED_REF);
7259
Chris Mason56bec292009-03-13 10:10:06 -04007260 /*
7261 * tree log blocks never actually go into the extent allocation
7262 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04007263 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007264 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
7265 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04007266 /* unlocks the pinned mutex */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007267 btrfs_pin_extent(fs_info, bytenr, num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07007268 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04007269 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007270 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007271 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007272 num_bytes, parent,
7273 root_objectid, (int)owner,
7274 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007275 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007276 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007277 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007278 num_bytes, parent,
7279 root_objectid, owner, offset,
7280 0, BTRFS_DROP_DELAYED_REF,
Omar Sandovald7eae342017-06-06 16:45:31 -07007281 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04007282 }
Omar Sandovald7eae342017-06-06 16:45:31 -07007283
7284 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
7285 add_pinned_bytes(fs_info, num_bytes, owner, root_objectid);
7286
Chris Mason925baed2008-06-25 16:01:30 -04007287 return ret;
7288}
7289
Chris Masonfec577f2007-02-26 10:40:21 -05007290/*
Josef Bacik817d52f2009-07-13 21:29:25 -04007291 * when we wait for progress in the block group caching, its because
7292 * our allocation attempt failed at least once. So, we must sleep
7293 * and let some progress happen before we try again.
7294 *
7295 * This function will sleep at least once waiting for new free space to
7296 * show up, and then it will check the block group free space numbers
7297 * for our min num_bytes. Another option is to have it go ahead
7298 * and look in the rbtree for a free extent of a given size, but this
7299 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04007300 *
7301 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
7302 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04007303 */
Josef Bacik36cce922013-08-05 11:15:21 -04007304static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04007305wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
7306 u64 num_bytes)
7307{
Yan Zheng11833d62009-09-11 16:11:19 -04007308 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04007309
Yan Zheng11833d62009-09-11 16:11:19 -04007310 caching_ctl = get_caching_control(cache);
7311 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007312 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04007313
Yan Zheng11833d62009-09-11 16:11:19 -04007314 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08007315 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04007316
7317 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04007318}
7319
7320static noinline int
7321wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
7322{
7323 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04007324 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007325
7326 caching_ctl = get_caching_control(cache);
7327 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007328 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007329
7330 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04007331 if (cache->cached == BTRFS_CACHE_ERROR)
7332 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04007333 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04007334 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04007335}
7336
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007337static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
7338 [BTRFS_RAID_RAID10] = "raid10",
7339 [BTRFS_RAID_RAID1] = "raid1",
7340 [BTRFS_RAID_DUP] = "dup",
7341 [BTRFS_RAID_RAID0] = "raid0",
7342 [BTRFS_RAID_SINGLE] = "single",
7343 [BTRFS_RAID_RAID5] = "raid5",
7344 [BTRFS_RAID_RAID6] = "raid6",
7345};
7346
Jeff Mahoney1b8e5df2013-11-20 16:50:23 -05007347static const char *get_raid_name(enum btrfs_raid_types type)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007348{
7349 if (type >= BTRFS_NR_RAID_TYPES)
7350 return NULL;
7351
7352 return btrfs_raid_type_names[type];
7353}
7354
Josef Bacik817d52f2009-07-13 21:29:25 -04007355enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05007356 LOOP_CACHING_NOWAIT = 0,
7357 LOOP_CACHING_WAIT = 1,
7358 LOOP_ALLOC_CHUNK = 2,
7359 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04007360};
7361
Miao Xiee570fd22014-06-19 10:42:50 +08007362static inline void
7363btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
7364 int delalloc)
7365{
7366 if (delalloc)
7367 down_read(&cache->data_rwsem);
7368}
7369
7370static inline void
7371btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
7372 int delalloc)
7373{
7374 btrfs_get_block_group(cache);
7375 if (delalloc)
7376 down_read(&cache->data_rwsem);
7377}
7378
7379static struct btrfs_block_group_cache *
7380btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
7381 struct btrfs_free_cluster *cluster,
7382 int delalloc)
7383{
Sudip Mukherjee89771cc2016-02-16 13:32:47 +05307384 struct btrfs_block_group_cache *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007385
Miao Xiee570fd22014-06-19 10:42:50 +08007386 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007387 while (1) {
7388 used_bg = cluster->block_group;
7389 if (!used_bg)
7390 return NULL;
7391
7392 if (used_bg == block_group)
7393 return used_bg;
7394
7395 btrfs_get_block_group(used_bg);
7396
7397 if (!delalloc)
7398 return used_bg;
7399
7400 if (down_read_trylock(&used_bg->data_rwsem))
7401 return used_bg;
7402
7403 spin_unlock(&cluster->refill_lock);
7404
Liu Boe321f8a2016-11-30 16:11:04 -08007405 /* We should only have one-level nested. */
7406 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007407
7408 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007409 if (used_bg == cluster->block_group)
7410 return used_bg;
7411
7412 up_read(&used_bg->data_rwsem);
7413 btrfs_put_block_group(used_bg);
7414 }
Miao Xiee570fd22014-06-19 10:42:50 +08007415}
7416
7417static inline void
7418btrfs_release_block_group(struct btrfs_block_group_cache *cache,
7419 int delalloc)
7420{
7421 if (delalloc)
7422 up_read(&cache->data_rwsem);
7423 btrfs_put_block_group(cache);
7424}
7425
Josef Bacik817d52f2009-07-13 21:29:25 -04007426/*
Chris Masonfec577f2007-02-26 10:40:21 -05007427 * walks the btree of allocated extents and find a hole of a given size.
7428 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08007429 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04007430 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08007431 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05007432 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08007433 *
7434 * If there is no suitable free space, we will record the max size of
7435 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05007436 */
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05007437static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08007438 u64 ram_bytes, u64 num_bytes, u64 empty_size,
7439 u64 hint_byte, struct btrfs_key *ins,
7440 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007441{
Josef Bacik80eb2342008-10-29 14:49:05 -04007442 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007443 struct btrfs_root *root = fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007444 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04007445 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05007446 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08007447 u64 max_extent_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007448 u64 empty_cluster = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04007449 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007450 int loop = 0;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007451 int index = btrfs_bg_flags_to_raid_index(flags);
Josef Bacik0a243252009-09-11 16:11:20 -04007452 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007453 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04007454 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08007455 bool have_caching_bg = false;
chandan13a0db52015-11-02 13:59:46 +05307456 bool orig_have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007457 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05007458
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007459 WARN_ON(num_bytes < fs_info->sectorsize);
David Sterba962a2982014-06-04 18:41:45 +02007460 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04007461 ins->objectid = 0;
7462 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04007463
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007464 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05007465
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007466 space_info = __find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007467 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007468 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007469 return -ENOSPC;
7470 }
Josef Bacik2552d172009-04-03 10:14:19 -04007471
Josef Bacik67377732010-09-16 16:19:09 -04007472 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04007473 * If our free space is heavily fragmented we may not be able to make
7474 * big contiguous allocations, so instead of doing the expensive search
7475 * for free space, simply return ENOSPC with our max_extent_size so we
7476 * can go ahead and search for a more manageable chunk.
7477 *
7478 * If our max_extent_size is large enough for our allocation simply
7479 * disable clustering since we will likely not be able to find enough
7480 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04007481 */
Josef Bacik4f4db212015-09-29 11:40:47 -04007482 if (unlikely(space_info->max_extent_size)) {
7483 spin_lock(&space_info->lock);
7484 if (space_info->max_extent_size &&
7485 num_bytes > space_info->max_extent_size) {
7486 ins->offset = space_info->max_extent_size;
7487 spin_unlock(&space_info->lock);
7488 return -ENOSPC;
7489 } else if (space_info->max_extent_size) {
7490 use_cluster = false;
7491 }
7492 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007493 }
7494
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007495 last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04007496 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007497 spin_lock(&last_ptr->lock);
7498 if (last_ptr->block_group)
7499 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007500 if (last_ptr->fragmented) {
7501 /*
7502 * We still set window_start so we can keep track of the
7503 * last place we found an allocation to try and save
7504 * some time.
7505 */
7506 hint_byte = last_ptr->window_start;
7507 use_cluster = false;
7508 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007509 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007510 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007511
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007512 search_start = max(search_start, first_logical_byte(fs_info, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04007513 search_start = max(search_start, hint_byte);
Josef Bacik2552d172009-04-03 10:14:19 -04007514 if (search_start == hint_byte) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007515 block_group = btrfs_lookup_block_group(fs_info, search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04007516 /*
7517 * we don't want to use the block group if it doesn't match our
7518 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05007519 *
7520 * However if we are re-searching with an ideal block group
7521 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04007522 */
David Sterbab6919a52013-04-29 13:39:40 +00007523 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05007524 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04007525 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007526 if (list_empty(&block_group->list) ||
7527 block_group->ro) {
7528 /*
7529 * someone is removing this block group,
7530 * we can't jump into the have_block_group
7531 * target because our list pointers are not
7532 * valid
7533 */
7534 btrfs_put_block_group(block_group);
7535 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05007536 } else {
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007537 index = btrfs_bg_flags_to_raid_index(
7538 block_group->flags);
Miao Xiee570fd22014-06-19 10:42:50 +08007539 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04007540 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05007541 }
Josef Bacik2552d172009-04-03 10:14:19 -04007542 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007543 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04007544 }
Chris Mason42e70e72008-11-07 18:17:11 -05007545 }
Josef Bacik2552d172009-04-03 10:14:19 -04007546search:
Miao Xie60d2adb2011-09-09 17:34:35 +08007547 have_caching_bg = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007548 if (index == 0 || index == btrfs_bg_flags_to_raid_index(flags))
Josef Bacika5e681d2015-10-01 14:54:10 -04007549 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04007550 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007551 list_for_each_entry(block_group, &space_info->block_groups[index],
7552 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04007553 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04007554 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05007555
Jeff Mahoney14443932017-07-19 23:25:51 -04007556 /* If the block group is read-only, we can skip it entirely. */
7557 if (unlikely(block_group->ro))
7558 continue;
7559
Miao Xiee570fd22014-06-19 10:42:50 +08007560 btrfs_grab_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007561 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05007562
Chris Mason83a50de2010-12-13 15:06:46 -05007563 /*
7564 * this can happen if we end up cycling through all the
7565 * raid types, but we want to make sure we only allocate
7566 * for the proper type.
7567 */
David Sterbab6919a52013-04-29 13:39:40 +00007568 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05007569 u64 extra = BTRFS_BLOCK_GROUP_DUP |
7570 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007571 BTRFS_BLOCK_GROUP_RAID5 |
7572 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05007573 BTRFS_BLOCK_GROUP_RAID10;
7574
7575 /*
7576 * if they asked for extra copies and this block group
7577 * doesn't provide them, bail. This does allow us to
7578 * fill raid0 from raid1.
7579 */
David Sterbab6919a52013-04-29 13:39:40 +00007580 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05007581 goto loop;
7582 }
7583
Josef Bacik2552d172009-04-03 10:14:19 -04007584have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05007585 cached = block_group_cache_done(block_group);
7586 if (unlikely(!cached)) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007587 have_caching_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00007588 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04007589 BUG_ON(ret < 0);
7590 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05007591 }
7592
Josef Bacik36cce922013-08-05 11:15:21 -04007593 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
7594 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007595
Josef Bacik0a243252009-09-11 16:11:20 -04007596 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007597 * Ok we want to try and use the cluster allocator, so
7598 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04007599 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04007600 if (last_ptr && use_cluster) {
Miao Xie215a63d2014-01-15 20:00:56 +08007601 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05007602 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007603 /*
7604 * the refill lock keeps out other
7605 * people trying to start a new cluster
7606 */
Miao Xiee570fd22014-06-19 10:42:50 +08007607 used_block_group = btrfs_lock_cluster(block_group,
7608 last_ptr,
7609 delalloc);
7610 if (!used_block_group)
Chris Mason44fb5512009-06-04 15:34:51 -04007611 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04007612
Miao Xiee570fd22014-06-19 10:42:50 +08007613 if (used_block_group != block_group &&
7614 (used_block_group->ro ||
7615 !block_group_bits(used_block_group, flags)))
7616 goto release_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007617
7618 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007619 last_ptr,
7620 num_bytes,
7621 used_block_group->key.objectid,
7622 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007623 if (offset) {
7624 /* we have a block, we're done */
7625 spin_unlock(&last_ptr->refill_lock);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007626 trace_btrfs_reserve_extent_cluster(fs_info,
Miao Xie89d43462014-01-15 20:00:57 +08007627 used_block_group,
7628 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08007629 if (used_block_group != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08007630 btrfs_release_block_group(block_group,
7631 delalloc);
Miao Xie215a63d2014-01-15 20:00:56 +08007632 block_group = used_block_group;
7633 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007634 goto checks;
7635 }
7636
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007637 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007638release_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007639 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7640 * set up a new clusters, so lets just skip it
7641 * and let the allocator find whatever block
7642 * it can find. If we reach this point, we
7643 * will have tried the cluster allocator
7644 * plenty of times and not have found
7645 * anything, so we are likely way too
7646 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007647 * anything.
7648 *
7649 * However, if the cluster is taken from the
7650 * current block group, release the cluster
7651 * first, so that we stand a better chance of
7652 * succeeding in the unclustered
7653 * allocation. */
7654 if (loop >= LOOP_NO_EMPTY_SIZE &&
Miao Xiee570fd22014-06-19 10:42:50 +08007655 used_block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007656 spin_unlock(&last_ptr->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007657 btrfs_release_block_group(used_block_group,
7658 delalloc);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007659 goto unclustered_alloc;
7660 }
7661
Chris Masonfa9c0d792009-04-03 09:47:43 -04007662 /*
7663 * this cluster didn't work out, free it and
7664 * start over
7665 */
7666 btrfs_return_cluster_to_free_space(NULL, last_ptr);
7667
Miao Xiee570fd22014-06-19 10:42:50 +08007668 if (used_block_group != block_group)
7669 btrfs_release_block_group(used_block_group,
7670 delalloc);
7671refill_cluster:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007672 if (loop >= LOOP_NO_EMPTY_SIZE) {
7673 spin_unlock(&last_ptr->refill_lock);
7674 goto unclustered_alloc;
7675 }
7676
Chris Mason8de972b2013-01-04 15:39:43 -05007677 aligned_cluster = max_t(unsigned long,
7678 empty_cluster + empty_size,
7679 block_group->full_stripe_len);
7680
Chris Masonfa9c0d792009-04-03 09:47:43 -04007681 /* allocate a cluster in this block group */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007682 ret = btrfs_find_space_cluster(fs_info, block_group,
Josef Bacik00361582013-08-14 14:02:47 -04007683 last_ptr, search_start,
7684 num_bytes,
7685 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007686 if (ret == 0) {
7687 /*
7688 * now pull our allocation out of this
7689 * cluster
7690 */
7691 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007692 last_ptr,
7693 num_bytes,
7694 search_start,
7695 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007696 if (offset) {
7697 /* we found one, proceed */
7698 spin_unlock(&last_ptr->refill_lock);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007699 trace_btrfs_reserve_extent_cluster(fs_info,
Josef Bacik3f7de032011-11-10 08:29:20 -05007700 block_group, search_start,
7701 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007702 goto checks;
7703 }
Josef Bacik0a243252009-09-11 16:11:20 -04007704 } else if (!cached && loop > LOOP_CACHING_NOWAIT
7705 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007706 spin_unlock(&last_ptr->refill_lock);
7707
Josef Bacik0a243252009-09-11 16:11:20 -04007708 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007709 wait_block_group_cache_progress(block_group,
7710 num_bytes + empty_cluster + empty_size);
7711 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007712 }
Josef Bacik817d52f2009-07-13 21:29:25 -04007713
Chris Masonfa9c0d792009-04-03 09:47:43 -04007714 /*
7715 * at this point we either didn't find a cluster
7716 * or we weren't able to allocate a block from our
7717 * cluster. Free the cluster we've been trying
7718 * to use, and go to the next block group
7719 */
Josef Bacik0a243252009-09-11 16:11:20 -04007720 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007721 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04007722 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007723 }
7724
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007725unclustered_alloc:
Josef Bacikc759c4e2015-10-02 15:25:10 -04007726 /*
7727 * We are doing an unclustered alloc, set the fragmented flag so
7728 * we don't bother trying to setup a cluster again until we get
7729 * more space.
7730 */
7731 if (unlikely(last_ptr)) {
7732 spin_lock(&last_ptr->lock);
7733 last_ptr->fragmented = 1;
7734 spin_unlock(&last_ptr->lock);
7735 }
Liu Bo0c9b36e2017-02-13 15:42:30 -08007736 if (cached) {
7737 struct btrfs_free_space_ctl *ctl =
7738 block_group->free_space_ctl;
7739
7740 spin_lock(&ctl->tree_lock);
7741 if (ctl->free_space <
7742 num_bytes + empty_cluster + empty_size) {
7743 if (ctl->free_space > max_extent_size)
7744 max_extent_size = ctl->free_space;
7745 spin_unlock(&ctl->tree_lock);
7746 goto loop;
7747 }
7748 spin_unlock(&ctl->tree_lock);
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007749 }
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007750
Josef Bacik6226cb02009-04-03 10:14:18 -04007751 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08007752 num_bytes, empty_size,
7753 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007754 /*
7755 * If we didn't find a chunk, and we haven't failed on this
7756 * block group before, and this block group is in the middle of
7757 * caching and we are ok with waiting, then go ahead and wait
7758 * for progress to be made, and set failed_alloc to true.
7759 *
7760 * If failed_alloc is true then we've already waited on this
7761 * block group once and should move on to the next block group.
7762 */
7763 if (!offset && !failed_alloc && !cached &&
7764 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007765 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007766 num_bytes + empty_size);
7767 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007768 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007769 } else if (!offset) {
7770 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04007771 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007772checks:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007773 search_start = ALIGN(offset, fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04007774
Josef Bacik2552d172009-04-03 10:14:19 -04007775 /* move on to the next group */
7776 if (search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08007777 block_group->key.objectid + block_group->key.offset) {
7778 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04007779 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04007780 }
Josef Bacik80eb2342008-10-29 14:49:05 -04007781
Josef Bacik6226cb02009-04-03 10:14:18 -04007782 if (offset < search_start)
Miao Xie215a63d2014-01-15 20:00:56 +08007783 btrfs_add_free_space(block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04007784 search_start - offset);
7785 BUG_ON(offset > search_start);
7786
Wang Xiaoguang18513092016-07-25 15:51:40 +08007787 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
7788 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007789 if (ret == -EAGAIN) {
Miao Xie215a63d2014-01-15 20:00:56 +08007790 btrfs_add_free_space(block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007791 goto loop;
7792 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007793 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04007794
Josef Bacik2552d172009-04-03 10:14:19 -04007795 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007796 ins->objectid = search_start;
7797 ins->offset = num_bytes;
7798
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007799 trace_btrfs_reserve_extent(fs_info, block_group,
Josef Bacik3f7de032011-11-10 08:29:20 -05007800 search_start, num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08007801 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007802 break;
7803loop:
Josef Bacik0a243252009-09-11 16:11:20 -04007804 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007805 failed_alloc = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007806 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
7807 index);
Miao Xiee570fd22014-06-19 10:42:50 +08007808 btrfs_release_block_group(block_group, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04007809 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04007810 }
7811 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05007812
chandan13a0db52015-11-02 13:59:46 +05307813 if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg
7814 && !orig_have_caching_bg)
7815 orig_have_caching_bg = true;
7816
Miao Xie60d2adb2011-09-09 17:34:35 +08007817 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
7818 goto search;
7819
Yan, Zhengb742bb82010-05-16 10:46:24 -04007820 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
7821 goto search;
7822
Josef Bacik285ff5a2012-01-13 15:27:45 -05007823 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05007824 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
7825 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04007826 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
7827 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
7828 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
7829 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04007830 */
Josef Bacik723bda22011-05-27 16:11:38 -04007831 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04007832 index = 0;
Josef Bacika5e681d2015-10-01 14:54:10 -04007833 if (loop == LOOP_CACHING_NOWAIT) {
7834 /*
7835 * We want to skip the LOOP_CACHING_WAIT step if we
Nicholas D Steeves01327612016-05-19 21:18:45 -04007836 * don't have any uncached bgs and we've already done a
Josef Bacika5e681d2015-10-01 14:54:10 -04007837 * full search through.
7838 */
chandan13a0db52015-11-02 13:59:46 +05307839 if (orig_have_caching_bg || !full_search)
Josef Bacika5e681d2015-10-01 14:54:10 -04007840 loop = LOOP_CACHING_WAIT;
7841 else
7842 loop = LOOP_ALLOC_CHUNK;
7843 } else {
7844 loop++;
7845 }
7846
Josef Bacik817d52f2009-07-13 21:29:25 -04007847 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04007848 struct btrfs_trans_handle *trans;
Wang Shilongf017f152014-03-13 13:19:47 +08007849 int exist = 0;
Josef Bacik00361582013-08-14 14:02:47 -04007850
Wang Shilongf017f152014-03-13 13:19:47 +08007851 trans = current->journal_info;
7852 if (trans)
7853 exist = 1;
7854 else
7855 trans = btrfs_join_transaction(root);
7856
Josef Bacik00361582013-08-14 14:02:47 -04007857 if (IS_ERR(trans)) {
7858 ret = PTR_ERR(trans);
7859 goto out;
7860 }
7861
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007862 ret = do_chunk_alloc(trans, fs_info, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04007863 CHUNK_ALLOC_FORCE);
Josef Bacika5e681d2015-10-01 14:54:10 -04007864
7865 /*
7866 * If we can't allocate a new chunk we've already looped
7867 * through at least once, move on to the NO_EMPTY_SIZE
7868 * case.
7869 */
7870 if (ret == -ENOSPC)
7871 loop = LOOP_NO_EMPTY_SIZE;
7872
Josef Bacikea658ba2012-09-11 16:57:25 -04007873 /*
7874 * Do not bail out on ENOSPC since we
7875 * can do more things.
7876 */
Josef Bacik00361582013-08-14 14:02:47 -04007877 if (ret < 0 && ret != -ENOSPC)
Jeff Mahoney66642832016-06-10 18:19:25 -04007878 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -04007879 else
7880 ret = 0;
Wang Shilongf017f152014-03-13 13:19:47 +08007881 if (!exist)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007882 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04007883 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04007884 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04007885 }
7886
7887 if (loop == LOOP_NO_EMPTY_SIZE) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007888 /*
7889 * Don't loop again if we already have no empty_size and
7890 * no empty_cluster.
7891 */
7892 if (empty_size == 0 &&
7893 empty_cluster == 0) {
7894 ret = -ENOSPC;
7895 goto out;
7896 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007897 empty_size = 0;
7898 empty_cluster = 0;
7899 }
Chris Mason42e70e72008-11-07 18:17:11 -05007900
Josef Bacik723bda22011-05-27 16:11:38 -04007901 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04007902 } else if (!ins->objectid) {
7903 ret = -ENOSPC;
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007904 } else if (ins->objectid) {
Josef Bacikc759c4e2015-10-02 15:25:10 -04007905 if (!use_cluster && last_ptr) {
7906 spin_lock(&last_ptr->lock);
7907 last_ptr->window_start = ins->objectid;
7908 spin_unlock(&last_ptr->lock);
7909 }
Josef Bacik2552d172009-04-03 10:14:19 -04007910 ret = 0;
7911 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007912out:
Josef Bacik4f4db212015-09-29 11:40:47 -04007913 if (ret == -ENOSPC) {
7914 spin_lock(&space_info->lock);
7915 space_info->max_extent_size = max_extent_size;
7916 spin_unlock(&space_info->lock);
Miao Xiea4820392013-09-09 13:19:42 +08007917 ins->offset = max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04007918 }
Chris Mason0f70abe2007-02-28 16:46:22 -05007919 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05007920}
Chris Masonec44a352008-04-28 15:29:52 -04007921
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007922static void dump_space_info(struct btrfs_fs_info *fs_info,
7923 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007924 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04007925{
7926 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04007927 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007928
Josef Bacik9ed74f22009-09-11 16:12:44 -04007929 spin_lock(&info->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007930 btrfs_info(fs_info, "space_info %llu has %llu free, is %sfull",
7931 info->flags,
Liu Bo41361352017-02-13 15:42:21 -08007932 info->total_bytes - btrfs_space_info_used(info, true),
7933 info->full ? "" : "not ");
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007934 btrfs_info(fs_info,
7935 "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu",
7936 info->total_bytes, info->bytes_used, info->bytes_pinned,
7937 info->bytes_reserved, info->bytes_may_use,
7938 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007939 spin_unlock(&info->lock);
7940
7941 if (!dump_block_groups)
7942 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007943
Josef Bacik80eb2342008-10-29 14:49:05 -04007944 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007945again:
7946 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04007947 spin_lock(&cache->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007948 btrfs_info(fs_info,
7949 "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s",
7950 cache->key.objectid, cache->key.offset,
7951 btrfs_block_group_used(&cache->item), cache->pinned,
7952 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04007953 btrfs_dump_free_space(cache, bytes);
7954 spin_unlock(&cache->lock);
7955 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007956 if (++index < BTRFS_NR_RAID_TYPES)
7957 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04007958 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007959}
Zheng Yane8569812008-09-26 10:05:48 -04007960
Nikolay Borisov6f47c702018-03-13 12:22:32 +02007961/*
7962 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
7963 * hole that is at least as big as @num_bytes.
7964 *
7965 * @root - The root that will contain this extent
7966 *
7967 * @ram_bytes - The amount of space in ram that @num_bytes take. This
7968 * is used for accounting purposes. This value differs
7969 * from @num_bytes only in the case of compressed extents.
7970 *
7971 * @num_bytes - Number of bytes to allocate on-disk.
7972 *
7973 * @min_alloc_size - Indicates the minimum amount of space that the
7974 * allocator should try to satisfy. In some cases
7975 * @num_bytes may be larger than what is required and if
7976 * the filesystem is fragmented then allocation fails.
7977 * However, the presence of @min_alloc_size gives a
7978 * chance to try and satisfy the smaller allocation.
7979 *
7980 * @empty_size - A hint that you plan on doing more COW. This is the
7981 * size in bytes the allocator should try to find free
7982 * next to the block it returns. This is just a hint and
7983 * may be ignored by the allocator.
7984 *
7985 * @hint_byte - Hint to the allocator to start searching above the byte
7986 * address passed. It might be ignored.
7987 *
7988 * @ins - This key is modified to record the found hole. It will
7989 * have the following values:
7990 * ins->objectid == start position
7991 * ins->flags = BTRFS_EXTENT_ITEM_KEY
7992 * ins->offset == the size of the hole.
7993 *
7994 * @is_data - Boolean flag indicating whether an extent is
7995 * allocated for data (true) or metadata (false)
7996 *
7997 * @delalloc - Boolean flag indicating whether this allocation is for
7998 * delalloc or not. If 'true' data_rwsem of block groups
7999 * is going to be acquired.
8000 *
8001 *
8002 * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
8003 * case -ENOSPC is returned then @ins->offset will contain the size of the
8004 * largest available hole the allocator managed to find.
8005 */
Wang Xiaoguang18513092016-07-25 15:51:40 +08008006int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04008007 u64 num_bytes, u64 min_alloc_size,
8008 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08008009 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05008010{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008011 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04008012 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00008013 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05008014 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04008015
Jeff Mahoney1b868262017-05-17 11:38:35 -04008016 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04008017again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008018 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05008019 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08008020 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008021 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008022 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008023 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08008024 if (!final_tried && ins->offset) {
8025 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04008026 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008027 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05008028 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08008029 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05008030 if (num_bytes == min_alloc_size)
8031 final_tried = true;
8032 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008033 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05008034 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008035
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008036 sinfo = __find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008037 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008038 "allocation failed flags %llu, wanted %llu",
8039 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01008040 if (sinfo)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008041 dump_space_info(fs_info, sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05008042 }
Chris Mason925baed2008-06-25 16:01:30 -04008043 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008044
8045 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008046}
8047
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008048static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008049 u64 start, u64 len,
8050 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04008051{
Josef Bacik0f9dd462008-09-23 13:14:11 -04008052 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05008053 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008054
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008055 cache = btrfs_lookup_block_group(fs_info, start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008056 if (!cache) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008057 btrfs_err(fs_info, "Unable to find block group for %llu",
8058 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008059 return -ENOSPC;
8060 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05008061
Chris Masone688b7252011-10-31 20:52:39 -04008062 if (pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008063 pin_down_extent(fs_info, cache, start, len, 1);
Chris Masone688b7252011-10-31 20:52:39 -04008064 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008065 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008066 ret = btrfs_discard_extent(fs_info, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04008067 btrfs_add_free_space(cache, start, len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08008068 btrfs_free_reserved_bytes(cache, len, delalloc);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008069 trace_btrfs_reserved_extent_free(fs_info, start, len);
Chris Masone688b7252011-10-31 20:52:39 -04008070 }
Dongsheng Yang31193212014-12-12 16:44:35 +08008071
Chris Masonfa9c0d792009-04-03 09:47:43 -04008072 btrfs_put_block_group(cache);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008073 return ret;
8074}
8075
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008076int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008077 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04008078{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008079 return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04008080}
8081
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008082int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04008083 u64 start, u64 len)
8084{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008085 return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04008086}
8087
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008088static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008089 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008090 u64 parent, u64 root_objectid,
8091 u64 flags, u64 owner, u64 offset,
8092 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008093{
8094 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008095 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008096 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008097 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008098 struct extent_buffer *leaf;
8099 int type;
8100 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04008101
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008102 if (parent > 0)
8103 type = BTRFS_SHARED_DATA_REF_KEY;
8104 else
8105 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04008106
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008107 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05008108
8109 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00008110 if (!path)
8111 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05008112
Chris Masonb9473432009-03-13 11:00:37 -04008113 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008114 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8115 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008116 if (ret) {
8117 btrfs_free_path(path);
8118 return ret;
8119 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008120
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008121 leaf = path->nodes[0];
8122 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05008123 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008124 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
8125 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8126 btrfs_set_extent_flags(leaf, extent_item,
8127 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05008128
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008129 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
8130 btrfs_set_extent_inline_ref_type(leaf, iref, type);
8131 if (parent > 0) {
8132 struct btrfs_shared_data_ref *ref;
8133 ref = (struct btrfs_shared_data_ref *)(iref + 1);
8134 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8135 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
8136 } else {
8137 struct btrfs_extent_data_ref *ref;
8138 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
8139 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
8140 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
8141 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
8142 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
8143 }
Chris Mason47e4bb92008-02-01 14:51:59 -05008144
8145 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05008146 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04008147
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008148 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
8149 ins->offset);
8150 if (ret)
8151 return ret;
8152
Jeff Mahoney6202df62016-06-22 18:54:22 -04008153 ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008154 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008155 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008156 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05008157 BUG();
8158 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008159 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008160 return ret;
8161}
8162
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008163static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008164 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008165 u64 parent, u64 root_objectid,
8166 u64 flags, struct btrfs_disk_key *key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01008167 int level, struct btrfs_key *ins)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008168{
8169 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008170 struct btrfs_extent_item *extent_item;
8171 struct btrfs_tree_block_info *block_info;
8172 struct btrfs_extent_inline_ref *iref;
8173 struct btrfs_path *path;
8174 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05008175 u32 size = sizeof(*extent_item) + sizeof(*iref);
Josef Bacikfcebe452014-05-13 17:30:47 -07008176 u64 num_bytes = ins->offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008177 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05008178
8179 if (!skinny_metadata)
8180 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008181
8182 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04008183 if (!path) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008184 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008185 fs_info->nodesize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07008186 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04008187 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008188
8189 path->leave_spinning = 1;
8190 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8191 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008192 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07008193 btrfs_free_path(path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008194 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008195 fs_info->nodesize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008196 return ret;
8197 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008198
8199 leaf = path->nodes[0];
8200 extent_item = btrfs_item_ptr(leaf, path->slots[0],
8201 struct btrfs_extent_item);
8202 btrfs_set_extent_refs(leaf, extent_item, 1);
8203 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8204 btrfs_set_extent_flags(leaf, extent_item,
8205 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008206
Josef Bacik3173a182013-03-07 14:22:04 -05008207 if (skinny_metadata) {
8208 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008209 num_bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05008210 } else {
8211 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
8212 btrfs_set_tree_block_key(leaf, block_info, key);
8213 btrfs_set_tree_block_level(leaf, block_info, level);
8214 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
8215 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008216
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008217 if (parent > 0) {
8218 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
8219 btrfs_set_extent_inline_ref_type(leaf, iref,
8220 BTRFS_SHARED_BLOCK_REF_KEY);
8221 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8222 } else {
8223 btrfs_set_extent_inline_ref_type(leaf, iref,
8224 BTRFS_TREE_BLOCK_REF_KEY);
8225 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
8226 }
8227
8228 btrfs_mark_buffer_dirty(leaf);
8229 btrfs_free_path(path);
8230
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008231 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
8232 num_bytes);
8233 if (ret)
8234 return ret;
8235
Jeff Mahoney6202df62016-06-22 18:54:22 -04008236 ret = update_block_group(trans, fs_info, ins->objectid,
8237 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008238 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008239 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008240 ins->objectid, ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008241 BUG();
8242 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04008243
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008244 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008245 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008246 return ret;
8247}
8248
8249int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008250 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08008251 u64 offset, u64 ram_bytes,
8252 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008253{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008254 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008255 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04008256
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008257 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04008258
Josef Bacikfd708b82017-09-29 15:43:50 -04008259 btrfs_ref_tree_mod(root, ins->objectid, ins->offset, 0,
8260 root->root_key.objectid, owner, offset,
8261 BTRFS_ADD_DELAYED_EXTENT);
8262
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008263 ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008264 ins->offset, 0,
8265 root->root_key.objectid, owner,
Omar Sandoval7be07912017-06-06 16:45:30 -07008266 offset, ram_bytes,
8267 BTRFS_ADD_DELAYED_EXTENT, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008268 return ret;
8269}
Chris Masone02119d2008-09-05 16:13:11 -04008270
8271/*
8272 * this is used by the tree logging recovery code. It records that
8273 * an extent has been allocated and makes sure to clear the free
8274 * space cache bits as well
8275 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008276int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008277 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008278 u64 root_objectid, u64 owner, u64 offset,
8279 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04008280{
8281 int ret;
8282 struct btrfs_block_group_cache *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008283 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008284
8285 /*
8286 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04008287 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008288 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008289 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008290 ret = __exclude_logged_extent(fs_info, ins->objectid,
8291 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008292 if (ret)
8293 return ret;
8294 }
Chris Masone02119d2008-09-05 16:13:11 -04008295
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008296 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008297 if (!block_group)
8298 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04008299
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008300 space_info = block_group->space_info;
8301 spin_lock(&space_info->lock);
8302 spin_lock(&block_group->lock);
8303 space_info->bytes_reserved += ins->offset;
8304 block_group->reserved += ins->offset;
8305 spin_unlock(&block_group->lock);
8306 spin_unlock(&space_info->lock);
8307
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008308 ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008309 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04008310 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04008311 return ret;
8312}
8313
Eric Sandeen48a3b632013-04-25 20:41:01 +00008314static struct extent_buffer *
8315btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
David Sterbafe864572014-06-15 02:28:42 +02008316 u64 bytenr, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04008317{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008318 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04008319 struct extent_buffer *buf;
8320
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008321 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008322 if (IS_ERR(buf))
8323 return buf;
8324
Chris Mason65b51a02008-08-01 15:11:20 -04008325 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04008326 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04008327 btrfs_tree_lock(buf);
David Sterba7c302b42017-02-10 18:47:57 +01008328 clean_tree_block(fs_info, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05008329 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008330
8331 btrfs_set_lock_blocking(buf);
David Sterba4db8c522015-12-03 13:06:46 +01008332 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008333
Chris Masond0c803c2008-09-11 16:17:57 -04008334 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01008335 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008336 /*
8337 * we allow two log transactions at a time, use different
8338 * EXENT bit to differentiate dirty pages.
8339 */
Filipe Manana656f30d2014-09-26 12:25:56 +01008340 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008341 set_extent_dirty(&root->dirty_log_pages, buf->start,
8342 buf->start + buf->len - 1, GFP_NOFS);
8343 else
8344 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02008345 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04008346 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01008347 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04008348 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
8349 buf->start + buf->len - 1, GFP_NOFS);
8350 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04008351 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05008352 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04008353 return buf;
8354}
8355
Yan, Zhengf0486c62010-05-16 10:46:25 -04008356static struct btrfs_block_rsv *
8357use_block_rsv(struct btrfs_trans_handle *trans,
8358 struct btrfs_root *root, u32 blocksize)
8359{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008360 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008361 struct btrfs_block_rsv *block_rsv;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008362 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008363 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00008364 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008365
8366 block_rsv = get_block_rsv(trans, root);
8367
Miao Xieb586b322013-05-13 13:55:10 +00008368 if (unlikely(block_rsv->size == 0))
8369 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00008370again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04008371 ret = block_rsv_use_bytes(block_rsv, blocksize);
8372 if (!ret)
8373 return block_rsv;
8374
Miao Xieb586b322013-05-13 13:55:10 +00008375 if (block_rsv->failfast)
8376 return ERR_PTR(ret);
8377
Miao Xied88033d2013-05-13 13:55:12 +00008378 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
8379 global_updated = true;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008380 update_global_block_rsv(fs_info);
Miao Xied88033d2013-05-13 13:55:12 +00008381 goto again;
8382 }
8383
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008384 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xieb586b322013-05-13 13:55:10 +00008385 static DEFINE_RATELIMIT_STATE(_rs,
8386 DEFAULT_RATELIMIT_INTERVAL * 10,
8387 /*DEFAULT_RATELIMIT_BURST*/ 1);
8388 if (__ratelimit(&_rs))
8389 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05008390 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00008391 }
8392try_reserve:
8393 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
8394 BTRFS_RESERVE_NO_FLUSH);
8395 if (!ret)
8396 return block_rsv;
8397 /*
8398 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00008399 * the global reserve if its space type is the same as the global
8400 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00008401 */
Miao Xie5881cfc2013-05-13 13:55:11 +00008402 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
8403 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00008404 ret = block_rsv_use_bytes(global_rsv, blocksize);
8405 if (!ret)
8406 return global_rsv;
8407 }
8408 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008409}
8410
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008411static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
8412 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008413{
8414 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008415 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008416}
8417
Chris Masonfec577f2007-02-26 10:40:21 -05008418/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04008419 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08008420 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05008421 */
David Sterba4d75f8a2014-06-15 01:54:12 +02008422struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08008423 struct btrfs_root *root,
8424 u64 parent, u64 root_objectid,
8425 const struct btrfs_disk_key *key,
8426 int level, u64 hint,
8427 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05008428{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008429 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04008430 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008431 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04008432 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008433 struct btrfs_delayed_extent_op *extent_op;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008434 u64 flags = 0;
8435 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008436 u32 blocksize = fs_info->nodesize;
8437 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008438
David Sterba05653ef2016-07-15 15:23:37 +02008439#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008440 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008441 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
David Sterbafe864572014-06-15 02:28:42 +02008442 level);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008443 if (!IS_ERR(buf))
8444 root->alloc_bytenr += blocksize;
8445 return buf;
8446 }
David Sterba05653ef2016-07-15 15:23:37 +02008447#endif
David Sterbafccb84c2014-09-29 23:53:21 +02008448
Yan, Zhengf0486c62010-05-16 10:46:25 -04008449 block_rsv = use_block_rsv(trans, root, blocksize);
8450 if (IS_ERR(block_rsv))
8451 return ERR_CAST(block_rsv);
8452
Wang Xiaoguang18513092016-07-25 15:51:40 +08008453 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08008454 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008455 if (ret)
8456 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05008457
David Sterbafe864572014-06-15 02:28:42 +02008458 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
Omar Sandoval67b78592015-02-24 02:47:04 -08008459 if (IS_ERR(buf)) {
8460 ret = PTR_ERR(buf);
8461 goto out_free_reserved;
8462 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008463
8464 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
8465 if (parent == 0)
8466 parent = ins.objectid;
8467 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8468 } else
8469 BUG_ON(parent > 0);
8470
8471 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00008472 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08008473 if (!extent_op) {
8474 ret = -ENOMEM;
8475 goto out_free_buf;
8476 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008477 if (key)
8478 memcpy(&extent_op->key, key, sizeof(extent_op->key));
8479 else
8480 memset(&extent_op->key, 0, sizeof(extent_op->key));
8481 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01008482 extent_op->update_key = skinny_metadata ? false : true;
8483 extent_op->update_flags = true;
8484 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04008485 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008486
Josef Bacikfd708b82017-09-29 15:43:50 -04008487 btrfs_ref_tree_mod(root, ins.objectid, ins.offset, parent,
8488 root_objectid, level, 0,
8489 BTRFS_ADD_DELAYED_EXTENT);
Omar Sandoval7be07912017-06-06 16:45:30 -07008490 ret = btrfs_add_delayed_tree_ref(fs_info, trans, ins.objectid,
8491 ins.offset, parent,
8492 root_objectid, level,
Omar Sandoval67b78592015-02-24 02:47:04 -08008493 BTRFS_ADD_DELAYED_EXTENT,
Omar Sandoval7be07912017-06-06 16:45:30 -07008494 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08008495 if (ret)
8496 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008497 }
Chris Masonfec577f2007-02-26 10:40:21 -05008498 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008499
8500out_free_delayed:
8501 btrfs_free_delayed_extent_op(extent_op);
8502out_free_buf:
8503 free_extent_buffer(buf);
8504out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008505 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008506out_unuse:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008507 unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08008508 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05008509}
Chris Masona28ec192007-03-06 20:08:01 -05008510
Yan Zheng2c47e6052009-06-27 21:07:35 -04008511struct walk_control {
8512 u64 refs[BTRFS_MAX_LEVEL];
8513 u64 flags[BTRFS_MAX_LEVEL];
8514 struct btrfs_key update_progress;
8515 int stage;
8516 int level;
8517 int shared_level;
8518 int update_ref;
8519 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008520 int reada_slot;
8521 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008522 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008523};
8524
8525#define DROP_REFERENCE 1
8526#define UPDATE_BACKREF 2
8527
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008528static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8529 struct btrfs_root *root,
8530 struct walk_control *wc,
8531 struct btrfs_path *path)
8532{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008533 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008534 u64 bytenr;
8535 u64 generation;
8536 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008537 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008538 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008539 struct btrfs_key key;
8540 struct extent_buffer *eb;
8541 int ret;
8542 int slot;
8543 int nread = 0;
8544
8545 if (path->slots[wc->level] < wc->reada_slot) {
8546 wc->reada_count = wc->reada_count * 2 / 3;
8547 wc->reada_count = max(wc->reada_count, 2);
8548 } else {
8549 wc->reada_count = wc->reada_count * 3 / 2;
8550 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008551 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008552 }
8553
8554 eb = path->nodes[wc->level];
8555 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008556
8557 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
8558 if (nread >= wc->reada_count)
8559 break;
8560
8561 cond_resched();
8562 bytenr = btrfs_node_blockptr(eb, slot);
8563 generation = btrfs_node_ptr_generation(eb, slot);
8564
8565 if (slot == path->slots[wc->level])
8566 goto reada;
8567
8568 if (wc->stage == UPDATE_BACKREF &&
8569 generation <= root->root_key.offset)
8570 continue;
8571
Yan, Zheng94fcca92009-10-09 09:25:16 -04008572 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008573 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05008574 wc->level - 1, 1, &refs,
8575 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008576 /* We don't care about errors in readahead. */
8577 if (ret < 0)
8578 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008579 BUG_ON(refs == 0);
8580
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008581 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008582 if (refs == 1)
8583 goto reada;
8584
Yan, Zheng94fcca92009-10-09 09:25:16 -04008585 if (wc->level == 1 &&
8586 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8587 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008588 if (!wc->update_ref ||
8589 generation <= root->root_key.offset)
8590 continue;
8591 btrfs_node_key_to_cpu(eb, &key, slot);
8592 ret = btrfs_comp_cpu_keys(&key,
8593 &wc->update_progress);
8594 if (ret < 0)
8595 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008596 } else {
8597 if (wc->level == 1 &&
8598 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8599 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008600 }
8601reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008602 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008603 nread++;
8604 }
8605 wc->reada_slot = slot;
8606}
8607
Chris Mason9aca1d52007-03-13 11:09:37 -04008608/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008609 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008610 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04008611 * when wc->stage == UPDATE_BACKREF, this function updates
8612 * back refs for pointers in the block.
8613 *
8614 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04008615 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008616static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8617 struct btrfs_root *root,
8618 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008619 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04008620{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008621 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008622 int level = wc->level;
8623 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04008624 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8625 int ret;
8626
8627 if (wc->stage == UPDATE_BACKREF &&
8628 btrfs_header_owner(eb) != root->root_key.objectid)
8629 return 1;
8630
8631 /*
8632 * when reference count of tree block is 1, it won't increase
8633 * again. once full backref flag is set, we never clear it.
8634 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04008635 if (lookup_info &&
8636 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
8637 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008638 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008639 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008640 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008641 &wc->refs[level],
8642 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008643 BUG_ON(ret == -ENOMEM);
8644 if (ret)
8645 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008646 BUG_ON(wc->refs[level] == 0);
8647 }
8648
Yan Zheng2c47e6052009-06-27 21:07:35 -04008649 if (wc->stage == DROP_REFERENCE) {
8650 if (wc->refs[level] > 1)
8651 return 1;
8652
8653 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04008654 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008655 path->locks[level] = 0;
8656 }
8657 return 0;
8658 }
8659
8660 /* wc->stage == UPDATE_BACKREF */
8661 if (!(wc->flags[level] & flag)) {
8662 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07008663 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008664 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07008665 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008666 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008667 ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04008668 eb->len, flag,
8669 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008670 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008671 wc->flags[level] |= flag;
8672 }
8673
8674 /*
8675 * the block is shared by multiple trees, so it's not good to
8676 * keep the tree lock
8677 */
8678 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04008679 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008680 path->locks[level] = 0;
8681 }
8682 return 0;
8683}
8684
8685/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008686 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008687 *
8688 * when wc->stage == DROP_REFERENCE, this function checks
8689 * reference count of the block pointed to. if the block
8690 * is shared and we need update back refs for the subtree
8691 * rooted at the block, this function changes wc->stage to
8692 * UPDATE_BACKREF. if the block is shared and there is no
8693 * need to update back, this function drops the reference
8694 * to the block.
8695 *
8696 * NOTE: return value 1 means we should stop walking down.
8697 */
8698static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8699 struct btrfs_root *root,
8700 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008701 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008702{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008703 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008704 u64 bytenr;
8705 u64 generation;
8706 u64 parent;
8707 u32 blocksize;
8708 struct btrfs_key key;
8709 struct extent_buffer *next;
8710 int level = wc->level;
8711 int reada = 0;
8712 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07008713 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008714
8715 generation = btrfs_node_ptr_generation(path->nodes[level],
8716 path->slots[level]);
8717 /*
8718 * if the lower level block was created before the snapshot
8719 * was created, we know there is no need to update back refs
8720 * for the subtree
8721 */
8722 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04008723 generation <= root->root_key.offset) {
8724 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008725 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008726 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008727
8728 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008729 blocksize = fs_info->nodesize;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008730
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008731 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008732 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008733 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008734 if (IS_ERR(next))
8735 return PTR_ERR(next);
8736
Josef Bacikb2aaaa32013-07-05 17:05:38 -04008737 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
8738 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008739 reada = 1;
8740 }
8741 btrfs_tree_lock(next);
8742 btrfs_set_lock_blocking(next);
8743
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008744 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008745 &wc->refs[level - 1],
8746 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02008747 if (ret < 0)
8748 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008749
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008750 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008751 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02008752 ret = -EIO;
8753 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008754 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008755 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008756
Yan, Zheng94fcca92009-10-09 09:25:16 -04008757 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008758 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07008759 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008760 if (level == 1 &&
8761 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8762 goto skip;
8763
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008764 if (!wc->update_ref ||
8765 generation <= root->root_key.offset)
8766 goto skip;
8767
8768 btrfs_node_key_to_cpu(path->nodes[level], &key,
8769 path->slots[level]);
8770 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
8771 if (ret < 0)
8772 goto skip;
8773
8774 wc->stage = UPDATE_BACKREF;
8775 wc->shared_level = level - 1;
8776 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008777 } else {
8778 if (level == 1 &&
8779 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8780 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008781 }
8782
Chris Masonb9fab912012-05-06 07:23:47 -04008783 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008784 btrfs_tree_unlock(next);
8785 free_extent_buffer(next);
8786 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008787 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008788 }
8789
8790 if (!next) {
8791 if (reada && level == 1)
8792 reada_walk_down(trans, root, wc, path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008793 next = read_tree_block(fs_info, bytenr, generation);
Liu Bo64c043d2015-05-25 17:30:15 +08008794 if (IS_ERR(next)) {
8795 return PTR_ERR(next);
8796 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04008797 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008798 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04008799 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008800 btrfs_tree_lock(next);
8801 btrfs_set_lock_blocking(next);
8802 }
8803
8804 level--;
Josef Bacik48672682016-09-23 13:23:28 +02008805 ASSERT(level == btrfs_header_level(next));
8806 if (level != btrfs_header_level(next)) {
8807 btrfs_err(root->fs_info, "mismatched level");
8808 ret = -EIO;
8809 goto out_unlock;
8810 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008811 path->nodes[level] = next;
8812 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008813 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008814 wc->level = level;
8815 if (wc->level == 1)
8816 wc->reada_slot = 0;
8817 return 0;
8818skip:
8819 wc->refs[level - 1] = 0;
8820 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008821 if (wc->stage == DROP_REFERENCE) {
8822 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
8823 parent = path->nodes[level]->start;
8824 } else {
Josef Bacik48672682016-09-23 13:23:28 +02008825 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04008826 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02008827 if (root->root_key.objectid !=
8828 btrfs_header_owner(path->nodes[level])) {
8829 btrfs_err(root->fs_info,
8830 "mismatched block owner");
8831 ret = -EIO;
8832 goto out_unlock;
8833 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008834 parent = 0;
8835 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008836
Mark Fasheh11526512014-07-17 12:39:01 -07008837 if (need_account) {
Qu Wenruo33d1f052016-10-18 09:31:28 +08008838 ret = btrfs_qgroup_trace_subtree(trans, root, next,
8839 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07008840 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008841 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008842 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
8843 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008844 }
8845 }
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008846 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008847 parent, root->root_key.objectid,
8848 level - 1, 0);
Josef Bacik48672682016-09-23 13:23:28 +02008849 if (ret)
8850 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008851 }
Josef Bacik48672682016-09-23 13:23:28 +02008852
8853 *lookup_info = 1;
8854 ret = 1;
8855
8856out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008857 btrfs_tree_unlock(next);
8858 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02008859
8860 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008861}
8862
8863/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008864 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008865 *
8866 * when wc->stage == DROP_REFERENCE, this function drops
8867 * reference count on the block.
8868 *
8869 * when wc->stage == UPDATE_BACKREF, this function changes
8870 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8871 * to UPDATE_BACKREF previously while processing the block.
8872 *
8873 * NOTE: return value 1 means we should stop walking up.
8874 */
8875static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8876 struct btrfs_root *root,
8877 struct btrfs_path *path,
8878 struct walk_control *wc)
8879{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008880 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008881 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008882 int level = wc->level;
8883 struct extent_buffer *eb = path->nodes[level];
8884 u64 parent = 0;
8885
8886 if (wc->stage == UPDATE_BACKREF) {
8887 BUG_ON(wc->shared_level < level);
8888 if (level < wc->shared_level)
8889 goto out;
8890
Yan Zheng2c47e6052009-06-27 21:07:35 -04008891 ret = find_next_key(path, level + 1, &wc->update_progress);
8892 if (ret > 0)
8893 wc->update_ref = 0;
8894
8895 wc->stage = DROP_REFERENCE;
8896 wc->shared_level = -1;
8897 path->slots[level] = 0;
8898
8899 /*
8900 * check reference count again if the block isn't locked.
8901 * we should start walking down the tree again if reference
8902 * count is one.
8903 */
8904 if (!path->locks[level]) {
8905 BUG_ON(level == 0);
8906 btrfs_tree_lock(eb);
8907 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008908 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008909
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008910 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008911 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008912 &wc->refs[level],
8913 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008914 if (ret < 0) {
8915 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008916 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008917 return ret;
8918 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008919 BUG_ON(wc->refs[level] == 0);
8920 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04008921 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008922 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008923 return 1;
8924 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008925 }
8926 }
8927
8928 /* wc->stage == DROP_REFERENCE */
8929 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
8930
8931 if (wc->refs[level] == 1) {
8932 if (level == 0) {
8933 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07008934 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008935 else
Josef Bacike339a6b2014-07-02 10:54:25 -07008936 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008937 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008938 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb);
Mark Fasheh11526512014-07-17 12:39:01 -07008939 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008940 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008941 "error %d accounting leaf items. Quota is out of sync, rescan required.",
8942 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008943 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008944 }
8945 /* make block locked assertion in clean_tree_block happy */
8946 if (!path->locks[level] &&
8947 btrfs_header_generation(eb) == trans->transid) {
8948 btrfs_tree_lock(eb);
8949 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008950 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008951 }
David Sterba7c302b42017-02-10 18:47:57 +01008952 clean_tree_block(fs_info, eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008953 }
8954
8955 if (eb == root->node) {
8956 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8957 parent = eb->start;
8958 else
8959 BUG_ON(root->root_key.objectid !=
8960 btrfs_header_owner(eb));
8961 } else {
8962 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8963 parent = path->nodes[level + 1]->start;
8964 else
8965 BUG_ON(root->root_key.objectid !=
8966 btrfs_header_owner(path->nodes[level + 1]));
8967 }
8968
Jan Schmidt5581a512012-05-16 17:04:52 +02008969 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008970out:
8971 wc->refs[level] = 0;
8972 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008973 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008974}
8975
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008976static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
8977 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008978 struct btrfs_path *path,
8979 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008980{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008981 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008982 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008983 int ret;
8984
Yan Zheng2c47e6052009-06-27 21:07:35 -04008985 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04008986 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008987 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008988 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008989
Yan Zheng2c47e6052009-06-27 21:07:35 -04008990 if (level == 0)
8991 break;
8992
Yan, Zheng7a7965f2010-02-01 02:41:17 +00008993 if (path->slots[level] >=
8994 btrfs_header_nritems(path->nodes[level]))
8995 break;
8996
Yan, Zheng94fcca92009-10-09 09:25:16 -04008997 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008998 if (ret > 0) {
8999 path->slots[level]++;
9000 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00009001 } else if (ret < 0)
9002 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009003 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009004 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009005 return 0;
9006}
9007
Chris Masond3977122009-01-05 21:25:51 -05009008static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05009009 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04009010 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009011 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05009012{
Yan Zheng2c47e6052009-06-27 21:07:35 -04009013 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05009014 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04009015
Yan Zheng2c47e6052009-06-27 21:07:35 -04009016 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
9017 while (level < max_level && path->nodes[level]) {
9018 wc->level = level;
9019 if (path->slots[level] + 1 <
9020 btrfs_header_nritems(path->nodes[level])) {
9021 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05009022 return 0;
9023 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009024 ret = walk_up_proc(trans, root, path, wc);
9025 if (ret > 0)
9026 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05009027
Yan Zheng2c47e6052009-06-27 21:07:35 -04009028 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04009029 btrfs_tree_unlock_rw(path->nodes[level],
9030 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009031 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009032 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009033 free_extent_buffer(path->nodes[level]);
9034 path->nodes[level] = NULL;
9035 level++;
Chris Mason20524f02007-03-10 06:35:47 -05009036 }
9037 }
9038 return 1;
9039}
9040
Chris Mason9aca1d52007-03-13 11:09:37 -04009041/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04009042 * drop a subvolume tree.
9043 *
9044 * this function traverses the tree freeing any blocks that only
9045 * referenced by the tree.
9046 *
9047 * when a shared tree block is found. this function decreases its
9048 * reference count by one. if update_ref is true, this function
9049 * also make sure backrefs for the shared block and all lower level
9050 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00009051 *
9052 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04009053 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04009054int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009055 struct btrfs_block_rsv *block_rsv, int update_ref,
9056 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05009057{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009058 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04009059 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009060 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009061 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04009062 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009063 struct walk_control *wc;
9064 struct btrfs_key key;
9065 int err = 0;
9066 int ret;
9067 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04009068 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05009069
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009070 btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07009071
Chris Mason5caf2a02007-04-02 11:20:42 -04009072 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009073 if (!path) {
9074 err = -ENOMEM;
9075 goto out;
9076 }
Chris Mason20524f02007-03-10 06:35:47 -05009077
Yan Zheng2c47e6052009-06-27 21:07:35 -04009078 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07009079 if (!wc) {
9080 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009081 err = -ENOMEM;
9082 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07009083 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009084
Yan, Zhenga22285a2010-05-16 10:48:46 -04009085 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009086 if (IS_ERR(trans)) {
9087 err = PTR_ERR(trans);
9088 goto out_free;
9089 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00009090
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009091 if (block_rsv)
9092 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009093
Chris Mason9f3a7422007-08-07 15:52:19 -04009094 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009095 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009096 path->nodes[level] = btrfs_lock_root_node(root);
9097 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04009098 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009099 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009100 memset(&wc->update_progress, 0,
9101 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04009102 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04009103 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009104 memcpy(&wc->update_progress, &key,
9105 sizeof(wc->update_progress));
9106
Chris Mason6702ed42007-08-07 16:15:09 -04009107 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009108 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04009109 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009110 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
9111 path->lowest_level = 0;
9112 if (ret < 0) {
9113 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009114 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04009115 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009116 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009117
Chris Mason7d9eb122008-07-08 14:19:17 -04009118 /*
9119 * unlock our path, this is safe because only this
9120 * function is allowed to delete this snapshot
9121 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009122 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04009123
Yan Zheng2c47e6052009-06-27 21:07:35 -04009124 level = btrfs_header_level(root->node);
9125 while (1) {
9126 btrfs_tree_lock(path->nodes[level]);
9127 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009128 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009129
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009130 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009131 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05009132 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04009133 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009134 if (ret < 0) {
9135 err = ret;
9136 goto out_end_trans;
9137 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009138 BUG_ON(wc->refs[level] == 0);
9139
9140 if (level == root_item->drop_level)
9141 break;
9142
9143 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009144 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009145 WARN_ON(wc->refs[level] != 1);
9146 level--;
9147 }
9148 }
9149
9150 wc->level = level;
9151 wc->shared_level = -1;
9152 wc->stage = DROP_REFERENCE;
9153 wc->update_ref = update_ref;
9154 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009155 wc->for_reloc = for_reloc;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009156 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009157
9158 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00009159
Yan Zheng2c47e6052009-06-27 21:07:35 -04009160 ret = walk_down_tree(trans, root, path, wc);
9161 if (ret < 0) {
9162 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04009163 break;
9164 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009165
9166 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
9167 if (ret < 0) {
9168 err = ret;
9169 break;
9170 }
9171
9172 if (ret > 0) {
9173 BUG_ON(wc->stage != DROP_REFERENCE);
9174 break;
9175 }
9176
9177 if (wc->stage == DROP_REFERENCE) {
9178 level = wc->level;
9179 btrfs_node_key(path->nodes[level],
9180 &root_item->drop_progress,
9181 path->slots[level]);
9182 root_item->drop_level = level;
9183 }
9184
9185 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009186 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009187 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009188 ret = btrfs_update_root(trans, tree_root,
9189 &root->root_key,
9190 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009191 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009192 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009193 err = ret;
9194 goto out_end_trans;
9195 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009196
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009197 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009198 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009199 btrfs_debug(fs_info,
9200 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04009201 err = -EAGAIN;
9202 goto out_free;
9203 }
9204
Yan, Zhenga22285a2010-05-16 10:48:46 -04009205 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009206 if (IS_ERR(trans)) {
9207 err = PTR_ERR(trans);
9208 goto out_free;
9209 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009210 if (block_rsv)
9211 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04009212 }
Chris Mason20524f02007-03-10 06:35:47 -05009213 }
David Sterbab3b4aa72011-04-21 01:20:15 +02009214 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009215 if (err)
9216 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009217
Jeff Mahoney1cd54472017-08-17 10:25:11 -04009218 ret = btrfs_del_root(trans, fs_info, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009219 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009220 btrfs_abort_transaction(trans, ret);
Jeff Mahoneye19182c2017-12-04 13:11:45 -05009221 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009222 goto out_end_trans;
9223 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009224
Yan, Zheng76dda932009-09-21 16:00:26 -04009225 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00009226 ret = btrfs_find_root(tree_root, &root->root_key, path,
9227 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009228 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009229 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009230 err = ret;
9231 goto out_end_trans;
9232 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05009233 /* if we fail to delete the orphan item this time
9234 * around, it'll get picked up the next time.
9235 *
9236 * The most common failure here is just -ENOENT.
9237 */
9238 btrfs_del_orphan_item(trans, tree_root,
9239 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04009240 }
9241 }
9242
Miao Xie27cdeb72014-04-02 19:51:05 +08009243 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04009244 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009245 } else {
9246 free_extent_buffer(root->node);
9247 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00009248 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009249 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04009250 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009251out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009252 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009253out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04009254 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04009255 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009256out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04009257 /*
9258 * So if we need to stop dropping the snapshot for whatever reason we
9259 * need to make sure to add it back to the dead root list so that we
9260 * keep trying to do the work later. This also cleans up roots if we
9261 * don't have it in the radix (like when we recover after a power fail
9262 * or unmount) so we don't leak memory.
9263 */
Thomas Meyer897ca812017-10-07 16:02:21 +02009264 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04009265 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08009266 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009267 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04009268 return err;
Chris Mason20524f02007-03-10 06:35:47 -05009269}
Chris Mason9078a3e2007-04-26 16:46:15 -04009270
Yan Zheng2c47e6052009-06-27 21:07:35 -04009271/*
9272 * drop subtree rooted at tree block 'node'.
9273 *
9274 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009275 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04009276 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04009277int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
9278 struct btrfs_root *root,
9279 struct extent_buffer *node,
9280 struct extent_buffer *parent)
9281{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009282 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009283 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009284 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009285 int level;
9286 int parent_level;
9287 int ret = 0;
9288 int wret;
9289
Yan Zheng2c47e6052009-06-27 21:07:35 -04009290 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
9291
Yan Zhengf82d02d2008-10-29 14:49:05 -04009292 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009293 if (!path)
9294 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009295
Yan Zheng2c47e6052009-06-27 21:07:35 -04009296 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009297 if (!wc) {
9298 btrfs_free_path(path);
9299 return -ENOMEM;
9300 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009301
Chris Masonb9447ef2009-03-09 11:45:38 -04009302 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009303 parent_level = btrfs_header_level(parent);
9304 extent_buffer_get(parent);
9305 path->nodes[parent_level] = parent;
9306 path->slots[parent_level] = btrfs_header_nritems(parent);
9307
Chris Masonb9447ef2009-03-09 11:45:38 -04009308 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009309 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009310 path->nodes[level] = node;
9311 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009312 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009313
9314 wc->refs[parent_level] = 1;
9315 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
9316 wc->level = level;
9317 wc->shared_level = -1;
9318 wc->stage = DROP_REFERENCE;
9319 wc->update_ref = 0;
9320 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009321 wc->for_reloc = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009322 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009323
9324 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009325 wret = walk_down_tree(trans, root, path, wc);
9326 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04009327 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009328 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009329 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009330
Yan Zheng2c47e6052009-06-27 21:07:35 -04009331 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009332 if (wret < 0)
9333 ret = wret;
9334 if (wret != 0)
9335 break;
9336 }
9337
Yan Zheng2c47e6052009-06-27 21:07:35 -04009338 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009339 btrfs_free_path(path);
9340 return ret;
9341}
9342
Jeff Mahoney6202df62016-06-22 18:54:22 -04009343static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04009344{
9345 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009346 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04009347
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009348 /*
9349 * if restripe for this chunk_type is on pick target profile and
9350 * return, otherwise do the usual balance
9351 */
Jeff Mahoney6202df62016-06-22 18:54:22 -04009352 stripped = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009353 if (stripped)
9354 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02009355
Jeff Mahoney6202df62016-06-22 18:54:22 -04009356 num_devices = fs_info->fs_devices->rw_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05009357
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009358 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05009359 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009360 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
9361
Chris Masonec44a352008-04-28 15:29:52 -04009362 if (num_devices == 1) {
9363 stripped |= BTRFS_BLOCK_GROUP_DUP;
9364 stripped = flags & ~stripped;
9365
9366 /* turn raid0 into single device chunks */
9367 if (flags & BTRFS_BLOCK_GROUP_RAID0)
9368 return stripped;
9369
9370 /* turn mirroring into duplication */
9371 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
9372 BTRFS_BLOCK_GROUP_RAID10))
9373 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04009374 } else {
9375 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04009376 if (flags & stripped)
9377 return flags;
9378
9379 stripped |= BTRFS_BLOCK_GROUP_DUP;
9380 stripped = flags & ~stripped;
9381
9382 /* switch duplicated blocks with raid1 */
9383 if (flags & BTRFS_BLOCK_GROUP_DUP)
9384 return stripped | BTRFS_BLOCK_GROUP_RAID1;
9385
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009386 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04009387 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009388
Chris Masonec44a352008-04-28 15:29:52 -04009389 return flags;
9390}
9391
Zhaolei868f4012015-08-05 16:43:27 +08009392static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04009393{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009394 struct btrfs_space_info *sinfo = cache->space_info;
9395 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00009396 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009397 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04009398
Miao Xie199c36e2011-07-15 10:34:36 +00009399 /*
9400 * We need some metadata space and system metadata space for
9401 * allocating chunks in some corner cases until we force to set
9402 * it to be readonly.
9403 */
9404 if ((sinfo->flags &
9405 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
9406 !force)
Byongho Leeee221842015-12-15 01:42:10 +09009407 min_allocable_bytes = SZ_1M;
Miao Xie199c36e2011-07-15 10:34:36 +00009408 else
9409 min_allocable_bytes = 0;
9410
Yan, Zhengf0486c62010-05-16 10:46:25 -04009411 spin_lock(&sinfo->lock);
9412 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00009413
9414 if (cache->ro) {
Zhaolei868f4012015-08-05 16:43:27 +08009415 cache->ro++;
WuBo61cfea92011-07-26 03:30:11 +00009416 ret = 0;
9417 goto out;
9418 }
9419
Yan, Zhengf0486c62010-05-16 10:46:25 -04009420 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
9421 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04009422
Liu Bo41361352017-02-13 15:42:21 -08009423 if (btrfs_space_info_used(sinfo, true) + num_bytes +
Josef Bacik37be25b2011-08-05 10:25:38 -04009424 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04009425 sinfo->bytes_readonly += num_bytes;
Zhaolei868f4012015-08-05 16:43:27 +08009426 cache->ro++;
Josef Bacik633c0aa2014-10-31 09:49:34 -04009427 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009428 ret = 0;
9429 }
WuBo61cfea92011-07-26 03:30:11 +00009430out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04009431 spin_unlock(&cache->lock);
9432 spin_unlock(&sinfo->lock);
9433 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04009434}
9435
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009436int btrfs_inc_block_group_ro(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04009437 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009438
9439{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009440 struct btrfs_trans_handle *trans;
9441 u64 alloc_flags;
9442 int ret;
9443
Chris Mason1bbc6212015-04-06 12:46:08 -07009444again:
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009445 trans = btrfs_join_transaction(fs_info->extent_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009446 if (IS_ERR(trans))
9447 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009448
Chris Mason1bbc6212015-04-06 12:46:08 -07009449 /*
9450 * we're not allowed to set block groups readonly after the dirty
9451 * block groups cache has started writing. If it already started,
9452 * back off and let this transaction commit
9453 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009454 mutex_lock(&fs_info->ro_block_group_mutex);
Josef Bacik3204d332015-09-24 10:46:10 -04009455 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
Chris Mason1bbc6212015-04-06 12:46:08 -07009456 u64 transid = trans->transid;
9457
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009458 mutex_unlock(&fs_info->ro_block_group_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009459 btrfs_end_transaction(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07009460
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009461 ret = btrfs_wait_for_commit(fs_info, transid);
Chris Mason1bbc6212015-04-06 12:46:08 -07009462 if (ret)
9463 return ret;
9464 goto again;
9465 }
9466
Chris Mason153c35b2015-05-19 18:54:41 -07009467 /*
9468 * if we are changing raid levels, try to allocate a corresponding
9469 * block group with the new raid level.
9470 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009471 alloc_flags = update_block_group_flags(fs_info, cache->flags);
Chris Mason153c35b2015-05-19 18:54:41 -07009472 if (alloc_flags != cache->flags) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009473 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason153c35b2015-05-19 18:54:41 -07009474 CHUNK_ALLOC_FORCE);
9475 /*
9476 * ENOSPC is allowed here, we may have enough space
9477 * already allocated at the new raid level to
9478 * carry on
9479 */
9480 if (ret == -ENOSPC)
9481 ret = 0;
9482 if (ret < 0)
9483 goto out;
9484 }
Chris Mason1bbc6212015-04-06 12:46:08 -07009485
Zhaolei868f4012015-08-05 16:43:27 +08009486 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009487 if (!ret)
9488 goto out;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009489 alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags);
9490 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04009491 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009492 if (ret < 0)
9493 goto out;
Zhaolei868f4012015-08-05 16:43:27 +08009494 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009495out:
Shaohua Li2f081082015-01-09 10:40:15 -08009496 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009497 alloc_flags = update_block_group_flags(fs_info, cache->flags);
David Sterba34441362016-10-04 19:34:27 +02009498 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009499 check_system_chunk(trans, fs_info, alloc_flags);
David Sterba34441362016-10-04 19:34:27 +02009500 mutex_unlock(&fs_info->chunk_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009501 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009502 mutex_unlock(&fs_info->ro_block_group_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009503
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009504 btrfs_end_transaction(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009505 return ret;
9506}
9507
Chris Masonc87f08c2011-02-16 13:57:04 -05009508int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009509 struct btrfs_fs_info *fs_info, u64 type)
Chris Masonc87f08c2011-02-16 13:57:04 -05009510{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009511 u64 alloc_flags = get_alloc_profile(fs_info, type);
9512
9513 return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05009514}
9515
Miao Xie6d07bce2011-01-05 10:07:31 +00009516/*
9517 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04009518 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00009519 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009520u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00009521{
9522 struct btrfs_block_group_cache *block_group;
9523 u64 free_bytes = 0;
9524 int factor;
9525
Nicholas D Steeves01327612016-05-19 21:18:45 -04009526 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009527 if (list_empty(&sinfo->ro_bgs))
9528 return 0;
9529
9530 spin_lock(&sinfo->lock);
9531 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00009532 spin_lock(&block_group->lock);
9533
9534 if (!block_group->ro) {
9535 spin_unlock(&block_group->lock);
9536 continue;
9537 }
9538
9539 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
9540 BTRFS_BLOCK_GROUP_RAID10 |
9541 BTRFS_BLOCK_GROUP_DUP))
9542 factor = 2;
9543 else
9544 factor = 1;
9545
9546 free_bytes += (block_group->key.offset -
9547 btrfs_block_group_used(&block_group->item)) *
9548 factor;
9549
9550 spin_unlock(&block_group->lock);
9551 }
Miao Xie6d07bce2011-01-05 10:07:31 +00009552 spin_unlock(&sinfo->lock);
9553
9554 return free_bytes;
9555}
9556
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009557void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache)
Yan, Zhengf0486c62010-05-16 10:46:25 -04009558{
9559 struct btrfs_space_info *sinfo = cache->space_info;
9560 u64 num_bytes;
9561
9562 BUG_ON(!cache->ro);
9563
9564 spin_lock(&sinfo->lock);
9565 spin_lock(&cache->lock);
Zhaolei868f4012015-08-05 16:43:27 +08009566 if (!--cache->ro) {
9567 num_bytes = cache->key.offset - cache->reserved -
9568 cache->pinned - cache->bytes_super -
9569 btrfs_block_group_used(&cache->item);
9570 sinfo->bytes_readonly -= num_bytes;
9571 list_del_init(&cache->ro_list);
9572 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009573 spin_unlock(&cache->lock);
9574 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009575}
9576
Josef Bacikba1bf482009-09-11 16:11:19 -04009577/*
9578 * checks to see if its even possible to relocate this block group.
9579 *
9580 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9581 * ok to go ahead and try.
9582 */
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009583int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04009584{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009585 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04009586 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04009587 struct btrfs_space_info *space_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009588 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacikba1bf482009-09-11 16:11:19 -04009589 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04009590 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00009591 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04009592 u64 dev_min = 1;
9593 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009594 u64 target;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009595 int debug;
liubocdcb7252011-08-03 10:15:25 +00009596 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04009597 int full = 0;
9598 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05009599
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009600 debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG);
Qu Wenruo0305bc22016-03-23 11:38:17 +08009601
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009602 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04009603
Josef Bacikba1bf482009-09-11 16:11:19 -04009604 /* odd, couldn't find the block group, leave it alone */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009605 if (!block_group) {
9606 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009607 btrfs_warn(fs_info,
Qu Wenruo0305bc22016-03-23 11:38:17 +08009608 "can't find block group for bytenr %llu",
9609 bytenr);
Josef Bacikba1bf482009-09-11 16:11:19 -04009610 return -1;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009611 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009612
liubocdcb7252011-08-03 10:15:25 +00009613 min_free = btrfs_block_group_used(&block_group->item);
9614
Josef Bacikba1bf482009-09-11 16:11:19 -04009615 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00009616 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04009617 goto out;
Chris Mason323da792008-05-09 11:46:48 -04009618
Josef Bacikba1bf482009-09-11 16:11:19 -04009619 space_info = block_group->space_info;
9620 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04009621
Josef Bacikba1bf482009-09-11 16:11:19 -04009622 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04009623
Josef Bacikba1bf482009-09-11 16:11:19 -04009624 /*
9625 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04009626 * relocate it unless we're able to allocate a new chunk below.
9627 *
9628 * Otherwise, we need to make sure we have room in the space to handle
9629 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04009630 */
Chris Mason7ce618d2009-09-22 14:48:44 -04009631 if ((space_info->total_bytes != block_group->key.offset) &&
Liu Bo41361352017-02-13 15:42:21 -08009632 (btrfs_space_info_used(space_info, false) + min_free <
9633 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04009634 spin_unlock(&space_info->lock);
9635 goto out;
9636 }
9637 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009638
Josef Bacikba1bf482009-09-11 16:11:19 -04009639 /*
9640 * ok we don't have enough space, but maybe we have free space on our
9641 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009642 * alloc devices and guess if we have enough space. if this block
9643 * group is going to be restriped, run checks against the target
9644 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04009645 */
9646 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05009647
liubocdcb7252011-08-03 10:15:25 +00009648 /*
9649 * index:
9650 * 0: raid10
9651 * 1: raid1
9652 * 2: dup
9653 * 3: raid0
9654 * 4: single
9655 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009656 target = get_restripe_target(fs_info, block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009657 if (target) {
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009658 index = btrfs_bg_flags_to_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009659 } else {
9660 /*
9661 * this is just a balance, so if we were marked as full
9662 * we know there is no space for a new chunk
9663 */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009664 if (full) {
9665 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009666 btrfs_warn(fs_info,
9667 "no space to alloc new chunk for block group %llu",
9668 block_group->key.objectid);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009669 goto out;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009670 }
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009671
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009672 index = btrfs_bg_flags_to_raid_index(block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009673 }
9674
Miao Xiee6ec7162013-01-17 05:38:51 +00009675 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00009676 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04009677 /* Divide by 2 */
9678 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009679 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00009680 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00009681 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04009682 /* Multiply by 2 */
9683 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009684 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00009685 dev_min = fs_devices->rw_devices;
David Sterba47c57132015-02-20 18:43:47 +01009686 min_free = div64_u64(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00009687 }
9688
Josef Bacik6df9a952013-06-27 13:22:46 -04009689 /* We need to do this so that we can look at pending chunks */
9690 trans = btrfs_join_transaction(root);
9691 if (IS_ERR(trans)) {
9692 ret = PTR_ERR(trans);
9693 goto out;
9694 }
9695
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009696 mutex_lock(&fs_info->chunk_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04009697 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00009698 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04009699
Josef Bacikba1bf482009-09-11 16:11:19 -04009700 /*
9701 * check to make sure we can actually find a chunk with enough
9702 * space to fit our block group in.
9703 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01009704 if (device->total_bytes > device->bytes_used + min_free &&
Anand Jain401e29c2017-12-04 12:54:55 +08009705 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Josef Bacik6df9a952013-06-27 13:22:46 -04009706 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00009707 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04009708 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00009709 dev_nr++;
9710
9711 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05009712 break;
liubocdcb7252011-08-03 10:15:25 +00009713
Josef Bacikba1bf482009-09-11 16:11:19 -04009714 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05009715 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009716 }
Qu Wenruo0305bc22016-03-23 11:38:17 +08009717 if (debug && ret == -1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009718 btrfs_warn(fs_info,
9719 "no space to allocate a new chunk for block group %llu",
9720 block_group->key.objectid);
9721 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009722 btrfs_end_transaction(trans);
Chris Masonedbd8d42007-12-21 16:27:24 -05009723out:
Josef Bacikba1bf482009-09-11 16:11:19 -04009724 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05009725 return ret;
9726}
9727
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009728static int find_first_block_group(struct btrfs_fs_info *fs_info,
9729 struct btrfs_path *path,
9730 struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04009731{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009732 struct btrfs_root *root = fs_info->extent_root;
Chris Mason925baed2008-06-25 16:01:30 -04009733 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009734 struct btrfs_key found_key;
9735 struct extent_buffer *leaf;
9736 int slot;
9737
9738 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
9739 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009740 goto out;
9741
Chris Masond3977122009-01-05 21:25:51 -05009742 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009743 slot = path->slots[0];
9744 leaf = path->nodes[0];
9745 if (slot >= btrfs_header_nritems(leaf)) {
9746 ret = btrfs_next_leaf(root, path);
9747 if (ret == 0)
9748 continue;
9749 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009750 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04009751 break;
9752 }
9753 btrfs_item_key_to_cpu(leaf, &found_key, slot);
9754
9755 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04009756 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
Liu Bo6fb37b72016-06-22 18:31:27 -07009757 struct extent_map_tree *em_tree;
9758 struct extent_map *em;
9759
9760 em_tree = &root->fs_info->mapping_tree.map_tree;
9761 read_lock(&em_tree->lock);
9762 em = lookup_extent_mapping(em_tree, found_key.objectid,
9763 found_key.offset);
9764 read_unlock(&em_tree->lock);
9765 if (!em) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009766 btrfs_err(fs_info,
Liu Bo6fb37b72016-06-22 18:31:27 -07009767 "logical %llu len %llu found bg but no related chunk",
9768 found_key.objectid, found_key.offset);
9769 ret = -ENOENT;
9770 } else {
9771 ret = 0;
9772 }
Josef Bacik187ee582016-08-18 15:30:06 -04009773 free_extent_map(em);
Chris Mason925baed2008-06-25 16:01:30 -04009774 goto out;
9775 }
Chris Mason0b86a832008-03-24 15:01:56 -04009776 path->slots[0]++;
9777 }
Chris Mason925baed2008-06-25 16:01:30 -04009778out:
Chris Mason0b86a832008-03-24 15:01:56 -04009779 return ret;
9780}
9781
Josef Bacik0af3d002010-06-21 14:48:16 -04009782void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9783{
9784 struct btrfs_block_group_cache *block_group;
9785 u64 last = 0;
9786
9787 while (1) {
9788 struct inode *inode;
9789
9790 block_group = btrfs_lookup_first_block_group(info, last);
9791 while (block_group) {
9792 spin_lock(&block_group->lock);
9793 if (block_group->iref)
9794 break;
9795 spin_unlock(&block_group->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009796 block_group = next_block_group(info, block_group);
Josef Bacik0af3d002010-06-21 14:48:16 -04009797 }
9798 if (!block_group) {
9799 if (last == 0)
9800 break;
9801 last = 0;
9802 continue;
9803 }
9804
9805 inode = block_group->inode;
9806 block_group->iref = 0;
9807 block_group->inode = NULL;
9808 spin_unlock(&block_group->lock);
Liu Bof3bca802016-07-20 17:33:44 -07009809 ASSERT(block_group->io_ctl.inode == NULL);
Josef Bacik0af3d002010-06-21 14:48:16 -04009810 iput(inode);
9811 last = block_group->key.objectid + block_group->key.offset;
9812 btrfs_put_block_group(block_group);
9813 }
9814}
9815
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009816/*
9817 * Must be called only after stopping all workers, since we could have block
9818 * group caching kthreads running, and therefore they could race with us if we
9819 * freed the block groups before stopping them.
9820 */
Zheng Yan1a40e232008-09-26 10:09:34 -04009821int btrfs_free_block_groups(struct btrfs_fs_info *info)
9822{
9823 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04009824 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04009825 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04009826 struct rb_node *n;
9827
Josef Bacik9e351cc2014-03-13 15:42:13 -04009828 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009829 while (!list_empty(&info->caching_block_groups)) {
9830 caching_ctl = list_entry(info->caching_block_groups.next,
9831 struct btrfs_caching_control, list);
9832 list_del(&caching_ctl->list);
9833 put_caching_control(caching_ctl);
9834 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04009835 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009836
Josef Bacik47ab2a62014-09-18 11:20:02 -04009837 spin_lock(&info->unused_bgs_lock);
9838 while (!list_empty(&info->unused_bgs)) {
9839 block_group = list_first_entry(&info->unused_bgs,
9840 struct btrfs_block_group_cache,
9841 bg_list);
9842 list_del_init(&block_group->bg_list);
9843 btrfs_put_block_group(block_group);
9844 }
9845 spin_unlock(&info->unused_bgs_lock);
9846
Zheng Yan1a40e232008-09-26 10:09:34 -04009847 spin_lock(&info->block_group_cache_lock);
9848 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
9849 block_group = rb_entry(n, struct btrfs_block_group_cache,
9850 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04009851 rb_erase(&block_group->cache_node,
9852 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009853 RB_CLEAR_NODE(&block_group->cache_node);
Yan Zhengd899e052008-10-30 14:25:28 -04009854 spin_unlock(&info->block_group_cache_lock);
9855
Josef Bacik80eb2342008-10-29 14:49:05 -04009856 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04009857 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04009858 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05009859
Josef Bacik3c148742011-02-02 15:53:47 +00009860 /*
9861 * We haven't cached this block group, which means we could
9862 * possibly have excluded extents on this block group.
9863 */
Josef Bacik36cce922013-08-05 11:15:21 -04009864 if (block_group->cached == BTRFS_CACHE_NO ||
9865 block_group->cached == BTRFS_CACHE_ERROR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009866 free_excluded_extents(info, block_group);
Josef Bacik3c148742011-02-02 15:53:47 +00009867
Josef Bacik817d52f2009-07-13 21:29:25 -04009868 btrfs_remove_free_space_cache(block_group);
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009869 ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
Liu Bof3bca802016-07-20 17:33:44 -07009870 ASSERT(list_empty(&block_group->dirty_list));
9871 ASSERT(list_empty(&block_group->io_list));
9872 ASSERT(list_empty(&block_group->bg_list));
9873 ASSERT(atomic_read(&block_group->count) == 1);
Josef Bacik11dfe352009-11-13 20:12:59 +00009874 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04009875
9876 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009877 }
9878 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04009879
9880 /* now that all the block groups are freed, go through and
9881 * free all the space_info structs. This is only called during
9882 * the final stages of unmount, and so we know nobody is
9883 * using them. We call synchronize_rcu() once before we start,
9884 * just to be on the safe side.
9885 */
9886 synchronize_rcu();
9887
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04009888 release_global_block_rsv(info);
9889
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309890 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009891 int i;
9892
Chris Mason4184ea72009-03-10 12:39:20 -04009893 space_info = list_entry(info->space_info.next,
9894 struct btrfs_space_info,
9895 list);
Josef Bacikd555b6c2016-03-25 13:25:51 -04009896
9897 /*
9898 * Do not hide this behind enospc_debug, this is actually
9899 * important and indicates a real bug if this happens.
9900 */
9901 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00009902 space_info->bytes_reserved > 0 ||
Josef Bacikd555b6c2016-03-25 13:25:51 -04009903 space_info->bytes_may_use > 0))
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009904 dump_space_info(info, space_info, 0, 0);
Chris Mason4184ea72009-03-10 12:39:20 -04009905 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009906 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
9907 struct kobject *kobj;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009908 kobj = space_info->block_group_kobjs[i];
9909 space_info->block_group_kobjs[i] = NULL;
9910 if (kobj) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009911 kobject_del(kobj);
9912 kobject_put(kobj);
9913 }
9914 }
9915 kobject_del(&space_info->kobj);
9916 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04009917 }
Zheng Yan1a40e232008-09-26 10:09:34 -04009918 return 0;
9919}
9920
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009921/* link_block_group will queue up kobjects to add when we're reclaim-safe */
9922void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info)
9923{
9924 struct btrfs_space_info *space_info;
9925 struct raid_kobject *rkobj;
9926 LIST_HEAD(list);
9927 int index;
9928 int ret = 0;
9929
9930 spin_lock(&fs_info->pending_raid_kobjs_lock);
9931 list_splice_init(&fs_info->pending_raid_kobjs, &list);
9932 spin_unlock(&fs_info->pending_raid_kobjs_lock);
9933
9934 list_for_each_entry(rkobj, &list, list) {
9935 space_info = __find_space_info(fs_info, rkobj->flags);
9936 index = btrfs_bg_flags_to_raid_index(rkobj->flags);
9937
9938 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
9939 "%s", get_raid_name(index));
9940 if (ret) {
9941 kobject_put(&rkobj->kobj);
9942 break;
9943 }
9944 }
9945 if (ret)
9946 btrfs_warn(fs_info,
9947 "failed to add kobject for block cache, ignoring");
9948}
9949
Nikolay Borisovc434d212017-08-21 12:43:50 +03009950static void link_block_group(struct btrfs_block_group_cache *cache)
Yan, Zhengb742bb82010-05-16 10:46:24 -04009951{
Nikolay Borisovc434d212017-08-21 12:43:50 +03009952 struct btrfs_space_info *space_info = cache->space_info;
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009953 struct btrfs_fs_info *fs_info = cache->fs_info;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009954 int index = btrfs_bg_flags_to_raid_index(cache->flags);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009955 bool first = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04009956
9957 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009958 if (list_empty(&space_info->block_groups[index]))
9959 first = true;
9960 list_add_tail(&cache->list, &space_info->block_groups[index]);
9961 up_write(&space_info->groups_sem);
9962
9963 if (first) {
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009964 struct raid_kobject *rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
9965 if (!rkobj) {
9966 btrfs_warn(cache->fs_info,
9967 "couldn't alloc memory for raid level kobject");
9968 return;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009969 }
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009970 rkobj->flags = cache->flags;
9971 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
9972
9973 spin_lock(&fs_info->pending_raid_kobjs_lock);
9974 list_add_tail(&rkobj->list, &fs_info->pending_raid_kobjs);
9975 spin_unlock(&fs_info->pending_raid_kobjs_lock);
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009976 space_info->block_group_kobjs[index] = &rkobj->kobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009977 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04009978}
9979
Miao Xie920e4a52014-01-15 20:00:55 +08009980static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009981btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info,
9982 u64 start, u64 size)
Miao Xie920e4a52014-01-15 20:00:55 +08009983{
9984 struct btrfs_block_group_cache *cache;
9985
9986 cache = kzalloc(sizeof(*cache), GFP_NOFS);
9987 if (!cache)
9988 return NULL;
9989
9990 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
9991 GFP_NOFS);
9992 if (!cache->free_space_ctl) {
9993 kfree(cache);
9994 return NULL;
9995 }
9996
9997 cache->key.objectid = start;
9998 cache->key.offset = size;
9999 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
10000
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010001 cache->fs_info = fs_info;
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +030010002 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010003 set_free_space_tree_thresholds(cache);
10004
Miao Xie920e4a52014-01-15 20:00:55 +080010005 atomic_set(&cache->count, 1);
10006 spin_lock_init(&cache->lock);
Miao Xiee570fd22014-06-19 10:42:50 +080010007 init_rwsem(&cache->data_rwsem);
Miao Xie920e4a52014-01-15 20:00:55 +080010008 INIT_LIST_HEAD(&cache->list);
10009 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010010 INIT_LIST_HEAD(&cache->bg_list);
Josef Bacik633c0aa2014-10-31 09:49:34 -040010011 INIT_LIST_HEAD(&cache->ro_list);
Josef Bacikce93ec52014-11-17 15:45:48 -050010012 INIT_LIST_HEAD(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -070010013 INIT_LIST_HEAD(&cache->io_list);
Miao Xie920e4a52014-01-15 20:00:55 +080010014 btrfs_init_free_space_ctl(cache);
Filipe Manana04216822014-11-27 21:14:15 +000010015 atomic_set(&cache->trimming, 0);
Omar Sandovala5ed9182015-09-29 20:50:35 -070010016 mutex_init(&cache->free_space_lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +080010017 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
Miao Xie920e4a52014-01-15 20:00:55 +080010018
10019 return cache;
10020}
10021
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010022int btrfs_read_block_groups(struct btrfs_fs_info *info)
Chris Mason9078a3e2007-04-26 16:46:15 -040010023{
10024 struct btrfs_path *path;
10025 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010026 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -040010027 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -040010028 struct btrfs_key key;
10029 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -040010030 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -040010031 int need_clear = 0;
10032 u64 cache_gen;
Liu Bo49303382016-08-25 18:08:27 -070010033 u64 feature;
10034 int mixed;
10035
10036 feature = btrfs_super_incompat_flags(info->super_copy);
10037 mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS);
Chris Mason96b51792007-10-15 16:15:19 -040010038
Chris Mason9078a3e2007-04-26 16:46:15 -040010039 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -040010040 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010041 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Chris Mason9078a3e2007-04-26 16:46:15 -040010042 path = btrfs_alloc_path();
10043 if (!path)
10044 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +010010045 path->reada = READA_FORWARD;
Chris Mason9078a3e2007-04-26 16:46:15 -040010046
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010047 cache_gen = btrfs_super_cache_generation(info->super_copy);
10048 if (btrfs_test_opt(info, SPACE_CACHE) &&
10049 btrfs_super_generation(info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -040010050 need_clear = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010051 if (btrfs_test_opt(info, CLEAR_CACHE))
Josef Bacik88c2ba32010-09-21 14:21:34 -040010052 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -040010053
Chris Masond3977122009-01-05 21:25:51 -050010054 while (1) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010055 ret = find_first_block_group(info, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -040010056 if (ret > 0)
10057 break;
Chris Mason0b86a832008-03-24 15:01:56 -040010058 if (ret != 0)
10059 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +080010060
Chris Mason5f39d392007-10-15 16:14:19 -040010061 leaf = path->nodes[0];
10062 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +080010063
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010064 cache = btrfs_create_block_group_cache(info, found_key.objectid,
Miao Xie920e4a52014-01-15 20:00:55 +080010065 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -040010066 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -040010067 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -040010068 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -040010069 }
Josef Bacik96303082009-07-13 21:29:25 -040010070
Liu Bocf7c1ef2012-07-06 03:31:34 -060010071 if (need_clear) {
10072 /*
10073 * When we mount with old space cache, we need to
10074 * set BTRFS_DC_CLEAR and set dirty flag.
10075 *
10076 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
10077 * truncate the old free space cache inode and
10078 * setup a new one.
10079 * b) Setting 'dirty flag' makes sure that we flush
10080 * the new space cache info onto disk.
10081 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010082 if (btrfs_test_opt(info, SPACE_CACHE))
Josef Bacikce93ec52014-11-17 15:45:48 -050010083 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -060010084 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010085
Chris Mason5f39d392007-10-15 16:14:19 -040010086 read_extent_buffer(leaf, &cache->item,
10087 btrfs_item_ptr_offset(leaf, path->slots[0]),
10088 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +080010089 cache->flags = btrfs_block_group_flags(&cache->item);
Liu Bo49303382016-08-25 18:08:27 -070010090 if (!mixed &&
10091 ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
10092 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
10093 btrfs_err(info,
10094"bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
10095 cache->key.objectid);
10096 ret = -EINVAL;
10097 goto error;
10098 }
Chris Mason0b86a832008-03-24 15:01:56 -040010099
Chris Mason9078a3e2007-04-26 16:46:15 -040010100 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +020010101 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +080010102
Josef Bacik817d52f2009-07-13 21:29:25 -040010103 /*
Josef Bacik3c148742011-02-02 15:53:47 +000010104 * We need to exclude the super stripes now so that the space
10105 * info has super bytes accounted for, otherwise we'll think
10106 * we have more space than we actually do.
10107 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010108 ret = exclude_super_stripes(info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010109 if (ret) {
10110 /*
10111 * We may have excluded something, so call this just in
10112 * case.
10113 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010114 free_excluded_extents(info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010115 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010116 goto error;
10117 }
Josef Bacik3c148742011-02-02 15:53:47 +000010118
10119 /*
Josef Bacik817d52f2009-07-13 21:29:25 -040010120 * check for two cases, either we are full, and therefore
10121 * don't need to bother with the caching work since we won't
10122 * find any space, or we are empty, and we can just add all
10123 * the space in and be done with it. This saves us _alot_ of
10124 * time, particularly in the full case.
10125 */
10126 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -040010127 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010128 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010129 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010130 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -040010131 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010132 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010133 add_new_free_space(cache, info,
Josef Bacik817d52f2009-07-13 21:29:25 -040010134 found_key.objectid,
10135 found_key.objectid +
10136 found_key.offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010137 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010138 }
Chris Mason96b51792007-10-15 16:15:19 -040010139
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010140 ret = btrfs_add_block_group_cache(info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010141 if (ret) {
10142 btrfs_remove_free_space_cache(cache);
10143 btrfs_put_block_group(cache);
10144 goto error;
10145 }
10146
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010147 trace_btrfs_add_block_group(info, cache, 0);
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +030010148 update_space_info(info, cache->flags, found_key.offset,
10149 btrfs_block_group_used(&cache->item),
10150 cache->bytes_super, &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010151
Chris Mason6324fbf2008-03-24 15:01:59 -040010152 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -040010153
Nikolay Borisovc434d212017-08-21 12:43:50 +030010154 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010155
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010156 set_avail_alloc_bits(info, cache->flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010157 if (btrfs_chunk_readonly(info, cache->key.objectid)) {
Zhaolei868f4012015-08-05 16:43:27 +080010158 inc_block_group_ro(cache, 1);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010159 } else if (btrfs_block_group_used(&cache->item) == 0) {
10160 spin_lock(&info->unused_bgs_lock);
10161 /* Should always be true but just in case. */
10162 if (list_empty(&cache->bg_list)) {
10163 btrfs_get_block_group(cache);
10164 list_add_tail(&cache->bg_list,
10165 &info->unused_bgs);
10166 }
10167 spin_unlock(&info->unused_bgs_lock);
10168 }
Chris Mason9078a3e2007-04-26 16:46:15 -040010169 }
Yan, Zhengb742bb82010-05-16 10:46:24 -040010170
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010171 list_for_each_entry_rcu(space_info, &info->space_info, list) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010172 if (!(get_alloc_profile(info, space_info->flags) &
Yan, Zhengb742bb82010-05-16 10:46:24 -040010173 (BTRFS_BLOCK_GROUP_RAID10 |
10174 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -050010175 BTRFS_BLOCK_GROUP_RAID5 |
10176 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -040010177 BTRFS_BLOCK_GROUP_DUP)))
10178 continue;
10179 /*
10180 * avoid allocating from un-mirrored block group if there are
10181 * mirrored block groups.
10182 */
chandan1095cc02013-07-16 12:28:56 +053010183 list_for_each_entry(cache,
10184 &space_info->block_groups[BTRFS_RAID_RAID0],
10185 list)
Zhaolei868f4012015-08-05 16:43:27 +080010186 inc_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +053010187 list_for_each_entry(cache,
10188 &space_info->block_groups[BTRFS_RAID_SINGLE],
10189 list)
Zhaolei868f4012015-08-05 16:43:27 +080010190 inc_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -040010191 }
Yan, Zhengf0486c62010-05-16 10:46:25 -040010192
Jeff Mahoney75cb3792018-03-20 15:25:26 -040010193 btrfs_add_raid_kobjects(info);
Yan, Zhengf0486c62010-05-16 10:46:25 -040010194 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -040010195 ret = 0;
10196error:
Chris Mason9078a3e2007-04-26 16:46:15 -040010197 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -040010198 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010199}
Chris Mason6324fbf2008-03-24 15:01:59 -040010200
Nikolay Borisov6c686b32018-02-07 17:55:40 +020010201void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
Josef Bacikea658ba2012-09-11 16:57:25 -040010202{
Nikolay Borisov6c686b32018-02-07 17:55:40 +020010203 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacikea658ba2012-09-11 16:57:25 -040010204 struct btrfs_block_group_cache *block_group, *tmp;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010205 struct btrfs_root *extent_root = fs_info->extent_root;
Josef Bacikea658ba2012-09-11 16:57:25 -040010206 struct btrfs_block_group_item item;
10207 struct btrfs_key key;
10208 int ret = 0;
Filipe Mananad9a05402015-10-03 13:13:13 +010010209 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010210
Filipe Mananad9a05402015-10-03 13:13:13 +010010211 trans->can_flush_pending_bgs = false;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010212 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
Josef Bacikea658ba2012-09-11 16:57:25 -040010213 if (ret)
Filipe Mananac92f6be2014-11-26 15:28:55 +000010214 goto next;
Josef Bacikea658ba2012-09-11 16:57:25 -040010215
10216 spin_lock(&block_group->lock);
10217 memcpy(&item, &block_group->item, sizeof(item));
10218 memcpy(&key, &block_group->key, sizeof(key));
10219 spin_unlock(&block_group->lock);
10220
10221 ret = btrfs_insert_item(trans, extent_root, &key, &item,
10222 sizeof(item));
10223 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010224 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010225 ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid,
10226 key.offset);
Josef Bacik6df9a952013-06-27 13:22:46 -040010227 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010228 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010229 add_block_group_free_space(trans, fs_info, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010230 /* already aborted the transaction if it failed. */
Filipe Mananac92f6be2014-11-26 15:28:55 +000010231next:
10232 list_del_init(&block_group->bg_list);
Josef Bacikea658ba2012-09-11 16:57:25 -040010233 }
Filipe Mananad9a05402015-10-03 13:13:13 +010010234 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010235}
10236
Chris Mason6324fbf2008-03-24 15:01:59 -040010237int btrfs_make_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010238 struct btrfs_fs_info *fs_info, u64 bytes_used,
Nikolay Borisov01744842017-07-27 14:22:11 +030010239 u64 type, u64 chunk_offset, u64 size)
Chris Mason6324fbf2008-03-24 15:01:59 -040010240{
Chris Mason6324fbf2008-03-24 15:01:59 -040010241 struct btrfs_block_group_cache *cache;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010242 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -040010243
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010244 btrfs_set_log_full_commit(fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -040010245
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010246 cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -040010247 if (!cache)
10248 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +080010249
Chris Mason6324fbf2008-03-24 15:01:59 -040010250 btrfs_set_block_group_used(&cache->item, bytes_used);
Nikolay Borisov01744842017-07-27 14:22:11 +030010251 btrfs_set_block_group_chunk_objectid(&cache->item,
10252 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Mason6324fbf2008-03-24 15:01:59 -040010253 btrfs_set_block_group_flags(&cache->item, type);
10254
Miao Xie920e4a52014-01-15 20:00:55 +080010255 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -040010256 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010257 cache->cached = BTRFS_CACHE_FINISHED;
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010258 cache->needs_free_space = 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010259 ret = exclude_super_stripes(fs_info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010260 if (ret) {
10261 /*
10262 * We may have excluded something, so call this just in
10263 * case.
10264 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010265 free_excluded_extents(fs_info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010266 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010267 return ret;
10268 }
Josef Bacik96303082009-07-13 21:29:25 -040010269
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010270 add_new_free_space(cache, fs_info, chunk_offset, chunk_offset + size);
Josef Bacik817d52f2009-07-13 21:29:25 -040010271
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010272 free_excluded_extents(fs_info, cache);
Yan Zheng11833d62009-09-11 16:11:19 -040010273
Josef Bacikd0bd4562015-09-23 14:54:14 -040010274#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010275 if (btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -040010276 u64 new_bytes_used = size - bytes_used;
10277
10278 bytes_used += new_bytes_used >> 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010279 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -040010280 }
10281#endif
Filipe Manana2e6e5182015-05-12 00:28:11 +010010282 /*
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010283 * Ensure the corresponding space_info object is created and
10284 * assigned to our block group. We want our bg to be added to the rbtree
10285 * with its ->space_info set.
Filipe Manana2e6e5182015-05-12 00:28:11 +010010286 */
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010287 cache->space_info = __find_space_info(fs_info, cache->flags);
Jeff Mahoneydc2d3002018-03-20 15:25:25 -040010288 ASSERT(cache->space_info);
Filipe Manana2e6e5182015-05-12 00:28:11 +010010289
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010290 ret = btrfs_add_block_group_cache(fs_info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010291 if (ret) {
10292 btrfs_remove_free_space_cache(cache);
10293 btrfs_put_block_group(cache);
10294 return ret;
10295 }
10296
Filipe Manana2e6e5182015-05-12 00:28:11 +010010297 /*
10298 * Now that our block group has its ->space_info set and is inserted in
10299 * the rbtree, update the space info's counters.
10300 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010301 trace_btrfs_add_block_group(fs_info, cache, 1);
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +030010302 update_space_info(fs_info, cache->flags, size, bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -040010303 cache->bytes_super, &cache->space_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010304 update_global_block_rsv(fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -040010305
Nikolay Borisovc434d212017-08-21 12:43:50 +030010306 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010307
Josef Bacik47ab2a62014-09-18 11:20:02 -040010308 list_add_tail(&cache->bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -040010309
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010310 set_avail_alloc_bits(fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -040010311 return 0;
10312}
Zheng Yan1a40e232008-09-26 10:09:34 -040010313
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010314static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
10315{
Ilya Dryomov899c81e2012-03-27 17:09:16 +030010316 u64 extra_flags = chunk_to_extended(flags) &
10317 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010318
Miao Xiede98ced2013-01-29 10:13:12 +000010319 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010320 if (flags & BTRFS_BLOCK_GROUP_DATA)
10321 fs_info->avail_data_alloc_bits &= ~extra_flags;
10322 if (flags & BTRFS_BLOCK_GROUP_METADATA)
10323 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
10324 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
10325 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +000010326 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010327}
10328
Zheng Yan1a40e232008-09-26 10:09:34 -040010329int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010330 struct btrfs_fs_info *fs_info, u64 group_start,
Filipe Manana04216822014-11-27 21:14:15 +000010331 struct extent_map *em)
Zheng Yan1a40e232008-09-26 10:09:34 -040010332{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010333 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010334 struct btrfs_path *path;
10335 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -040010336 struct btrfs_free_cluster *cluster;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010337 struct btrfs_root *tree_root = fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010338 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -040010339 struct inode *inode;
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010340 struct kobject *kobj = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -040010341 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010342 int index;
Josef Bacik89a55892010-10-14 14:52:27 -040010343 int factor;
Filipe Manana4f69cb92014-11-26 15:28:51 +000010344 struct btrfs_caching_control *caching_ctl = NULL;
Filipe Manana04216822014-11-27 21:14:15 +000010345 bool remove_em;
Zheng Yan1a40e232008-09-26 10:09:34 -040010346
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010347 block_group = btrfs_lookup_block_group(fs_info, group_start);
Zheng Yan1a40e232008-09-26 10:09:34 -040010348 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -050010349 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -040010350
liubo9f7c43c2011-03-07 02:13:33 +000010351 /*
10352 * Free the reserved super bytes from this block group before
10353 * remove it.
10354 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010355 free_excluded_extents(fs_info, block_group);
Josef Bacikfd708b82017-09-29 15:43:50 -040010356 btrfs_free_ref_tree_range(fs_info, block_group->key.objectid,
10357 block_group->key.offset);
liubo9f7c43c2011-03-07 02:13:33 +000010358
Zheng Yan1a40e232008-09-26 10:09:34 -040010359 memcpy(&key, &block_group->key, sizeof(key));
Qu Wenruo3e72ee82018-01-30 18:20:45 +080010360 index = btrfs_bg_flags_to_raid_index(block_group->flags);
Josef Bacik89a55892010-10-14 14:52:27 -040010361 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
10362 BTRFS_BLOCK_GROUP_RAID1 |
10363 BTRFS_BLOCK_GROUP_RAID10))
10364 factor = 2;
10365 else
10366 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -040010367
Chris Mason44fb5512009-06-04 15:34:51 -040010368 /* make sure this block group isn't part of an allocation cluster */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010369 cluster = &fs_info->data_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010370 spin_lock(&cluster->refill_lock);
10371 btrfs_return_cluster_to_free_space(block_group, cluster);
10372 spin_unlock(&cluster->refill_lock);
10373
10374 /*
10375 * make sure this block group isn't part of a metadata
10376 * allocation cluster
10377 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010378 cluster = &fs_info->meta_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010379 spin_lock(&cluster->refill_lock);
10380 btrfs_return_cluster_to_free_space(block_group, cluster);
10381 spin_unlock(&cluster->refill_lock);
10382
Zheng Yan1a40e232008-09-26 10:09:34 -040010383 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010384 if (!path) {
10385 ret = -ENOMEM;
10386 goto out;
10387 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010388
Chris Mason1bbc6212015-04-06 12:46:08 -070010389 /*
10390 * get the inode first so any iput calls done for the io_list
10391 * aren't the final iput (no unlinks allowed now)
10392 */
Jeff Mahoney77ab86b2017-02-15 16:28:30 -050010393 inode = lookup_free_space_inode(fs_info, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010394
10395 mutex_lock(&trans->transaction->cache_write_mutex);
10396 /*
10397 * make sure our free spache cache IO is done before remove the
10398 * free space inode
10399 */
10400 spin_lock(&trans->transaction->dirty_bgs_lock);
10401 if (!list_empty(&block_group->io_list)) {
10402 list_del_init(&block_group->io_list);
10403
10404 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
10405
10406 spin_unlock(&trans->transaction->dirty_bgs_lock);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040010407 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010408 btrfs_put_block_group(block_group);
10409 spin_lock(&trans->transaction->dirty_bgs_lock);
10410 }
10411
10412 if (!list_empty(&block_group->dirty_list)) {
10413 list_del_init(&block_group->dirty_list);
10414 btrfs_put_block_group(block_group);
10415 }
10416 spin_unlock(&trans->transaction->dirty_bgs_lock);
10417 mutex_unlock(&trans->transaction->cache_write_mutex);
10418
Josef Bacik0af3d002010-06-21 14:48:16 -040010419 if (!IS_ERR(inode)) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010420 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010421 if (ret) {
10422 btrfs_add_delayed_iput(inode);
10423 goto out;
10424 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010425 clear_nlink(inode);
10426 /* One for the block groups ref */
10427 spin_lock(&block_group->lock);
10428 if (block_group->iref) {
10429 block_group->iref = 0;
10430 block_group->inode = NULL;
10431 spin_unlock(&block_group->lock);
10432 iput(inode);
10433 } else {
10434 spin_unlock(&block_group->lock);
10435 }
10436 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -040010437 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -040010438 }
10439
10440 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
10441 key.offset = block_group->key.objectid;
10442 key.type = 0;
10443
10444 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
10445 if (ret < 0)
10446 goto out;
10447 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +020010448 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010449 if (ret == 0) {
10450 ret = btrfs_del_item(trans, tree_root, path);
10451 if (ret)
10452 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +020010453 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010454 }
10455
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010456 spin_lock(&fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -040010457 rb_erase(&block_group->cache_node,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010458 &fs_info->block_group_cache_tree);
Filipe Manana292cbd52014-11-26 15:28:50 +000010459 RB_CLEAR_NODE(&block_group->cache_node);
Liu Boa1897fd2012-12-27 09:01:23 +000010460
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010461 if (fs_info->first_logical_byte == block_group->key.objectid)
10462 fs_info->first_logical_byte = (u64)-1;
10463 spin_unlock(&fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010464
Josef Bacik80eb2342008-10-29 14:49:05 -040010465 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -040010466 /*
10467 * we must use list_del_init so people can check to see if they
10468 * are still on the list after taking the semaphore
10469 */
10470 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010471 if (list_empty(&block_group->space_info->block_groups[index])) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010472 kobj = block_group->space_info->block_group_kobjs[index];
10473 block_group->space_info->block_group_kobjs[index] = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010474 clear_avail_alloc_bits(fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010475 }
Josef Bacik80eb2342008-10-29 14:49:05 -040010476 up_write(&block_group->space_info->groups_sem);
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010477 if (kobj) {
10478 kobject_del(kobj);
10479 kobject_put(kobj);
10480 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010481
Filipe Manana4f69cb92014-11-26 15:28:51 +000010482 if (block_group->has_caching_ctl)
10483 caching_ctl = get_caching_control(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -040010484 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -040010485 wait_block_group_cache_done(block_group);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010486 if (block_group->has_caching_ctl) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010487 down_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010488 if (!caching_ctl) {
10489 struct btrfs_caching_control *ctl;
10490
10491 list_for_each_entry(ctl,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010492 &fs_info->caching_block_groups, list)
Filipe Manana4f69cb92014-11-26 15:28:51 +000010493 if (ctl->block_group == block_group) {
10494 caching_ctl = ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +020010495 refcount_inc(&caching_ctl->count);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010496 break;
10497 }
10498 }
10499 if (caching_ctl)
10500 list_del_init(&caching_ctl->list);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010501 up_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010502 if (caching_ctl) {
10503 /* Once for the caching bgs list and once for us. */
10504 put_caching_control(caching_ctl);
10505 put_caching_control(caching_ctl);
10506 }
10507 }
Josef Bacik817d52f2009-07-13 21:29:25 -040010508
Josef Bacikce93ec52014-11-17 15:45:48 -050010509 spin_lock(&trans->transaction->dirty_bgs_lock);
10510 if (!list_empty(&block_group->dirty_list)) {
Chris Mason1bbc6212015-04-06 12:46:08 -070010511 WARN_ON(1);
10512 }
10513 if (!list_empty(&block_group->io_list)) {
10514 WARN_ON(1);
Josef Bacikce93ec52014-11-17 15:45:48 -050010515 }
10516 spin_unlock(&trans->transaction->dirty_bgs_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010517 btrfs_remove_free_space_cache(block_group);
10518
Yan Zhengc146afa2008-11-12 14:34:12 -050010519 spin_lock(&block_group->space_info->lock);
Filipe Manana75c68e92015-01-16 13:24:40 +000010520 list_del_init(&block_group->ro_list);
Zhao Lei18d018a2015-02-24 20:07:44 +080010521
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010522 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Zhao Lei18d018a2015-02-24 20:07:44 +080010523 WARN_ON(block_group->space_info->total_bytes
10524 < block_group->key.offset);
10525 WARN_ON(block_group->space_info->bytes_readonly
10526 < block_group->key.offset);
10527 WARN_ON(block_group->space_info->disk_total
10528 < block_group->key.offset * factor);
10529 }
Yan Zhengc146afa2008-11-12 14:34:12 -050010530 block_group->space_info->total_bytes -= block_group->key.offset;
10531 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -040010532 block_group->space_info->disk_total -= block_group->key.offset * factor;
Zhao Lei18d018a2015-02-24 20:07:44 +080010533
Yan Zhengc146afa2008-11-12 14:34:12 -050010534 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -040010535
Josef Bacik0af3d002010-06-21 14:48:16 -040010536 memcpy(&key, &block_group->key, sizeof(key));
10537
David Sterba34441362016-10-04 19:34:27 +020010538 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana495e64f2014-12-02 18:07:30 +000010539 if (!list_empty(&em->list)) {
10540 /* We're in the transaction->pending_chunks list. */
10541 free_extent_map(em);
10542 }
Filipe Manana04216822014-11-27 21:14:15 +000010543 spin_lock(&block_group->lock);
10544 block_group->removed = 1;
10545 /*
10546 * At this point trimming can't start on this block group, because we
10547 * removed the block group from the tree fs_info->block_group_cache_tree
10548 * so no one can't find it anymore and even if someone already got this
10549 * block group before we removed it from the rbtree, they have already
10550 * incremented block_group->trimming - if they didn't, they won't find
10551 * any free space entries because we already removed them all when we
10552 * called btrfs_remove_free_space_cache().
10553 *
10554 * And we must not remove the extent map from the fs_info->mapping_tree
10555 * to prevent the same logical address range and physical device space
10556 * ranges from being reused for a new block group. This is because our
10557 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
10558 * completely transactionless, so while it is trimming a range the
10559 * currently running transaction might finish and a new one start,
10560 * allowing for new block groups to be created that can reuse the same
10561 * physical device locations unless we take this special care.
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010562 *
10563 * There may also be an implicit trim operation if the file system
10564 * is mounted with -odiscard. The same protections must remain
10565 * in place until the extents have been discarded completely when
10566 * the transaction commit has completed.
Filipe Manana04216822014-11-27 21:14:15 +000010567 */
10568 remove_em = (atomic_read(&block_group->trimming) == 0);
10569 /*
10570 * Make sure a trimmer task always sees the em in the pinned_chunks list
10571 * if it sees block_group->removed == 1 (needs to lock block_group->lock
10572 * before checking block_group->removed).
10573 */
10574 if (!remove_em) {
10575 /*
10576 * Our em might be in trans->transaction->pending_chunks which
10577 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10578 * and so is the fs_info->pinned_chunks list.
10579 *
10580 * So at this point we must be holding the chunk_mutex to avoid
10581 * any races with chunk allocation (more specifically at
10582 * volumes.c:contains_pending_extent()), to ensure it always
10583 * sees the em, either in the pending_chunks list or in the
10584 * pinned_chunks list.
10585 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010586 list_move_tail(&em->list, &fs_info->pinned_chunks);
Filipe Manana04216822014-11-27 21:14:15 +000010587 }
10588 spin_unlock(&block_group->lock);
Filipe Manana04216822014-11-27 21:14:15 +000010589
10590 if (remove_em) {
10591 struct extent_map_tree *em_tree;
10592
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010593 em_tree = &fs_info->mapping_tree.map_tree;
Filipe Manana04216822014-11-27 21:14:15 +000010594 write_lock(&em_tree->lock);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010595 /*
10596 * The em might be in the pending_chunks list, so make sure the
10597 * chunk mutex is locked, since remove_extent_mapping() will
10598 * delete us from that list.
10599 */
Filipe Manana04216822014-11-27 21:14:15 +000010600 remove_extent_mapping(em_tree, em);
10601 write_unlock(&em_tree->lock);
10602 /* once for the tree */
10603 free_extent_map(em);
10604 }
10605
David Sterba34441362016-10-04 19:34:27 +020010606 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010607
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010608 ret = remove_block_group_free_space(trans, fs_info, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010609 if (ret)
10610 goto out;
10611
Chris Masonfa9c0d792009-04-03 09:47:43 -040010612 btrfs_put_block_group(block_group);
10613 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -040010614
10615 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
10616 if (ret > 0)
10617 ret = -EIO;
10618 if (ret < 0)
10619 goto out;
10620
10621 ret = btrfs_del_item(trans, root, path);
10622out:
10623 btrfs_free_path(path);
10624 return ret;
10625}
liuboacce9522011-01-06 19:30:25 +080010626
Filipe Manana8eab77f2015-11-13 23:57:16 +000010627struct btrfs_trans_handle *
Filipe Manana7fd01182015-11-13 23:57:17 +000010628btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info,
10629 const u64 chunk_offset)
Filipe Manana8eab77f2015-11-13 23:57:16 +000010630{
Filipe Manana7fd01182015-11-13 23:57:17 +000010631 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
10632 struct extent_map *em;
10633 struct map_lookup *map;
10634 unsigned int num_items;
10635
10636 read_lock(&em_tree->lock);
10637 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
10638 read_unlock(&em_tree->lock);
10639 ASSERT(em && em->start == chunk_offset);
10640
Filipe Manana8eab77f2015-11-13 23:57:16 +000010641 /*
Filipe Manana7fd01182015-11-13 23:57:17 +000010642 * We need to reserve 3 + N units from the metadata space info in order
10643 * to remove a block group (done at btrfs_remove_chunk() and at
10644 * btrfs_remove_block_group()), which are used for:
10645 *
Filipe Manana8eab77f2015-11-13 23:57:16 +000010646 * 1 unit for adding the free space inode's orphan (located in the tree
10647 * of tree roots).
Filipe Manana7fd01182015-11-13 23:57:17 +000010648 * 1 unit for deleting the block group item (located in the extent
10649 * tree).
10650 * 1 unit for deleting the free space item (located in tree of tree
10651 * roots).
10652 * N units for deleting N device extent items corresponding to each
10653 * stripe (located in the device tree).
10654 *
10655 * In order to remove a block group we also need to reserve units in the
10656 * system space info in order to update the chunk tree (update one or
10657 * more device items and remove one chunk item), but this is done at
10658 * btrfs_remove_chunk() through a call to check_system_chunk().
Filipe Manana8eab77f2015-11-13 23:57:16 +000010659 */
Jeff Mahoney95617d62015-06-03 10:55:48 -040010660 map = em->map_lookup;
Filipe Manana7fd01182015-11-13 23:57:17 +000010661 num_items = 3 + map->num_stripes;
10662 free_extent_map(em);
10663
Filipe Manana8eab77f2015-11-13 23:57:16 +000010664 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
Filipe Manana7fd01182015-11-13 23:57:17 +000010665 num_items, 1);
Filipe Manana8eab77f2015-11-13 23:57:16 +000010666}
10667
Josef Bacik47ab2a62014-09-18 11:20:02 -040010668/*
10669 * Process the unused_bgs list and remove any that don't have any allocated
10670 * space inside of them.
10671 */
10672void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10673{
10674 struct btrfs_block_group_cache *block_group;
10675 struct btrfs_space_info *space_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010676 struct btrfs_trans_handle *trans;
10677 int ret = 0;
10678
Josef Bacikafcdd122016-09-02 15:40:02 -040010679 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
Josef Bacik47ab2a62014-09-18 11:20:02 -040010680 return;
10681
10682 spin_lock(&fs_info->unused_bgs_lock);
10683 while (!list_empty(&fs_info->unused_bgs)) {
10684 u64 start, end;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010685 int trimming;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010686
10687 block_group = list_first_entry(&fs_info->unused_bgs,
10688 struct btrfs_block_group_cache,
10689 bg_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010690 list_del_init(&block_group->bg_list);
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010691
10692 space_info = block_group->space_info;
10693
Josef Bacik47ab2a62014-09-18 11:20:02 -040010694 if (ret || btrfs_mixed_space_info(space_info)) {
10695 btrfs_put_block_group(block_group);
10696 continue;
10697 }
10698 spin_unlock(&fs_info->unused_bgs_lock);
10699
Zhao Leid5f2e332015-10-08 18:46:44 +080010700 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +010010701
Josef Bacik47ab2a62014-09-18 11:20:02 -040010702 /* Don't want to race with allocators so take the groups_sem */
10703 down_write(&space_info->groups_sem);
10704 spin_lock(&block_group->lock);
10705 if (block_group->reserved ||
10706 btrfs_block_group_used(&block_group->item) ||
Chris Mason19c4d2f2016-10-10 13:43:31 -070010707 block_group->ro ||
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010708 list_is_singular(&block_group->list)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -040010709 /*
10710 * We want to bail if we made new allocations or have
10711 * outstanding allocations in this block group. We do
10712 * the ro check in case balance is currently acting on
10713 * this block group.
10714 */
10715 spin_unlock(&block_group->lock);
10716 up_write(&space_info->groups_sem);
10717 goto next;
10718 }
10719 spin_unlock(&block_group->lock);
10720
10721 /* We don't want to force the issue, only flip if it's ok. */
Zhaolei868f4012015-08-05 16:43:27 +080010722 ret = inc_block_group_ro(block_group, 0);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010723 up_write(&space_info->groups_sem);
10724 if (ret < 0) {
10725 ret = 0;
10726 goto next;
10727 }
10728
10729 /*
10730 * Want to do this before we do anything else so we can recover
10731 * properly if we fail to join the transaction.
10732 */
Filipe Manana7fd01182015-11-13 23:57:17 +000010733 trans = btrfs_start_trans_remove_block_group(fs_info,
10734 block_group->key.objectid);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010735 if (IS_ERR(trans)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010736 btrfs_dec_block_group_ro(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010737 ret = PTR_ERR(trans);
10738 goto next;
10739 }
10740
10741 /*
10742 * We could have pending pinned extents for this block group,
10743 * just delete them, we don't care about them anymore.
10744 */
10745 start = block_group->key.objectid;
10746 end = start + block_group->key.offset - 1;
Filipe Mananad4b450c2015-01-29 19:18:25 +000010747 /*
10748 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10749 * btrfs_finish_extent_commit(). If we are at transaction N,
10750 * another task might be running finish_extent_commit() for the
10751 * previous transaction N - 1, and have seen a range belonging
10752 * to the block group in freed_extents[] before we were able to
10753 * clear the whole block group range from freed_extents[]. This
10754 * means that task can lookup for the block group after we
10755 * unpinned it from freed_extents[] and removed it, leading to
10756 * a BUG_ON() at btrfs_unpin_extent_range().
10757 */
10758 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana758eb512014-11-03 14:08:39 +000010759 ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
David Sterba91166212016-04-26 23:54:39 +020010760 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010761 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010762 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010763 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010764 goto end_trans;
10765 }
10766 ret = clear_extent_bits(&fs_info->freed_extents[1], 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 }
Filipe Mananad4b450c2015-01-29 19:18:25 +000010773 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010774
10775 /* Reset pinned so btrfs_put_block_group doesn't complain */
Zhao Leic30666d2015-02-25 14:17:20 +080010776 spin_lock(&space_info->lock);
10777 spin_lock(&block_group->lock);
10778
10779 space_info->bytes_pinned -= block_group->pinned;
10780 space_info->bytes_readonly += block_group->pinned;
10781 percpu_counter_add(&space_info->total_bytes_pinned,
10782 -block_group->pinned);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010783 block_group->pinned = 0;
10784
Zhao Leic30666d2015-02-25 14:17:20 +080010785 spin_unlock(&block_group->lock);
10786 spin_unlock(&space_info->lock);
10787
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010788 /* DISCARD can flip during remount */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010789 trimming = btrfs_test_opt(fs_info, DISCARD);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010790
10791 /* Implicit trim during transaction commit. */
10792 if (trimming)
10793 btrfs_get_block_group_trimming(block_group);
10794
Josef Bacik47ab2a62014-09-18 11:20:02 -040010795 /*
10796 * Btrfs_remove_chunk will abort the transaction if things go
10797 * horribly wrong.
10798 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010799 ret = btrfs_remove_chunk(trans, fs_info,
Josef Bacik47ab2a62014-09-18 11:20:02 -040010800 block_group->key.objectid);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010801
10802 if (ret) {
10803 if (trimming)
10804 btrfs_put_block_group_trimming(block_group);
10805 goto end_trans;
10806 }
10807
10808 /*
10809 * If we're not mounted with -odiscard, we can just forget
10810 * about this block group. Otherwise we'll need to wait
10811 * until transaction commit to do the actual discard.
10812 */
10813 if (trimming) {
Filipe Manana348a0012015-11-27 12:16:16 +000010814 spin_lock(&fs_info->unused_bgs_lock);
10815 /*
10816 * A concurrent scrub might have added us to the list
10817 * fs_info->unused_bgs, so use a list_move operation
10818 * to add the block group to the deleted_bgs list.
10819 */
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010820 list_move(&block_group->bg_list,
10821 &trans->transaction->deleted_bgs);
Filipe Manana348a0012015-11-27 12:16:16 +000010822 spin_unlock(&fs_info->unused_bgs_lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010823 btrfs_get_block_group(block_group);
10824 }
Filipe Manana758eb512014-11-03 14:08:39 +000010825end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010826 btrfs_end_transaction(trans);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010827next:
Zhao Leid5f2e332015-10-08 18:46:44 +080010828 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010829 btrfs_put_block_group(block_group);
10830 spin_lock(&fs_info->unused_bgs_lock);
10831 }
10832 spin_unlock(&fs_info->unused_bgs_lock);
10833}
10834
liuboc59021f2011-03-07 02:13:14 +000010835int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
10836{
10837 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +000010838 struct btrfs_super_block *disk_super;
10839 u64 features;
10840 u64 flags;
10841 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +000010842 int ret;
10843
David Sterba6c417612011-04-13 15:41:04 +020010844 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +000010845 if (!btrfs_super_root(disk_super))
Dan Carpenter0dc924c52016-01-13 15:21:17 +030010846 return -EINVAL;
liuboc59021f2011-03-07 02:13:14 +000010847
liubo1aba86d2011-04-08 08:44:37 +000010848 features = btrfs_super_incompat_flags(disk_super);
10849 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
10850 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +000010851
liubo1aba86d2011-04-08 08:44:37 +000010852 flags = BTRFS_BLOCK_GROUP_SYSTEM;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010853 ret = create_space_info(fs_info, flags, &space_info);
liuboc59021f2011-03-07 02:13:14 +000010854 if (ret)
liubo1aba86d2011-04-08 08:44:37 +000010855 goto out;
liuboc59021f2011-03-07 02:13:14 +000010856
liubo1aba86d2011-04-08 08:44:37 +000010857 if (mixed) {
10858 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010859 ret = create_space_info(fs_info, flags, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010860 } else {
10861 flags = BTRFS_BLOCK_GROUP_METADATA;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010862 ret = create_space_info(fs_info, flags, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010863 if (ret)
10864 goto out;
10865
10866 flags = BTRFS_BLOCK_GROUP_DATA;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010867 ret = create_space_info(fs_info, flags, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010868 }
10869out:
liuboc59021f2011-03-07 02:13:14 +000010870 return ret;
10871}
10872
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010873int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
10874 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +080010875{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010876 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +080010877}
10878
Jeff Mahoney499f3772015-06-15 09:41:17 -040010879/*
10880 * It used to be that old block groups would be left around forever.
10881 * Iterating over them would be enough to trim unused space. Since we
10882 * now automatically remove them, we also need to iterate over unallocated
10883 * space.
10884 *
10885 * We don't want a transaction for this since the discard may take a
10886 * substantial amount of time. We don't require that a transaction be
10887 * running, but we do need to take a running transaction into account
10888 * to ensure that we're not discarding chunks that were released in
10889 * the current transaction.
10890 *
10891 * Holding the chunks lock will prevent other threads from allocating
10892 * or releasing chunks, but it won't prevent a running transaction
10893 * from committing and releasing the memory that the pending chunks
10894 * list head uses. For that, we need to take a reference to the
10895 * transaction.
10896 */
10897static int btrfs_trim_free_extents(struct btrfs_device *device,
10898 u64 minlen, u64 *trimmed)
10899{
10900 u64 start = 0, len = 0;
10901 int ret;
10902
10903 *trimmed = 0;
10904
10905 /* Not writeable = nothing to do. */
Anand Jainebbede42017-12-04 12:54:52 +080010906 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoney499f3772015-06-15 09:41:17 -040010907 return 0;
10908
10909 /* No free space = nothing to do. */
10910 if (device->total_bytes <= device->bytes_used)
10911 return 0;
10912
10913 ret = 0;
10914
10915 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -040010916 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010917 struct btrfs_transaction *trans;
10918 u64 bytes;
10919
10920 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
10921 if (ret)
10922 return ret;
10923
10924 down_read(&fs_info->commit_root_sem);
10925
10926 spin_lock(&fs_info->trans_lock);
10927 trans = fs_info->running_transaction;
10928 if (trans)
Elena Reshetova9b64f572017-03-03 10:55:11 +020010929 refcount_inc(&trans->use_count);
Jeff Mahoney499f3772015-06-15 09:41:17 -040010930 spin_unlock(&fs_info->trans_lock);
10931
10932 ret = find_free_dev_extent_start(trans, device, minlen, start,
10933 &start, &len);
10934 if (trans)
10935 btrfs_put_transaction(trans);
10936
10937 if (ret) {
10938 up_read(&fs_info->commit_root_sem);
10939 mutex_unlock(&fs_info->chunk_mutex);
10940 if (ret == -ENOSPC)
10941 ret = 0;
10942 break;
10943 }
10944
10945 ret = btrfs_issue_discard(device->bdev, start, len, &bytes);
10946 up_read(&fs_info->commit_root_sem);
10947 mutex_unlock(&fs_info->chunk_mutex);
10948
10949 if (ret)
10950 break;
10951
10952 start += len;
10953 *trimmed += bytes;
10954
10955 if (fatal_signal_pending(current)) {
10956 ret = -ERESTARTSYS;
10957 break;
10958 }
10959
10960 cond_resched();
10961 }
10962
10963 return ret;
10964}
10965
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010966int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +000010967{
Li Dongyangf7039b12011-03-24 10:24:28 +000010968 struct btrfs_block_group_cache *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010969 struct btrfs_device *device;
10970 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +000010971 u64 group_trimmed;
10972 u64 start;
10973 u64 end;
10974 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +080010975 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +000010976 int ret = 0;
10977
Liu Bo2cac13e2012-02-09 18:17:41 +080010978 /*
10979 * try to trim all FS space, our block group may start from non-zero.
10980 */
10981 if (range->len == total_bytes)
10982 cache = btrfs_lookup_first_block_group(fs_info, range->start);
10983 else
10984 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +000010985
10986 while (cache) {
10987 if (cache->key.objectid >= (range->start + range->len)) {
10988 btrfs_put_block_group(cache);
10989 break;
10990 }
10991
10992 start = max(range->start, cache->key.objectid);
10993 end = min(range->start + range->len,
10994 cache->key.objectid + cache->key.offset);
10995
10996 if (end - start >= range->minlen) {
10997 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +000010998 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -040010999 if (ret) {
11000 btrfs_put_block_group(cache);
11001 break;
11002 }
11003 ret = wait_block_group_cache_done(cache);
11004 if (ret) {
11005 btrfs_put_block_group(cache);
11006 break;
11007 }
Li Dongyangf7039b12011-03-24 10:24:28 +000011008 }
11009 ret = btrfs_trim_block_group(cache,
11010 &group_trimmed,
11011 start,
11012 end,
11013 range->minlen);
11014
11015 trimmed += group_trimmed;
11016 if (ret) {
11017 btrfs_put_block_group(cache);
11018 break;
11019 }
11020 }
11021
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040011022 cache = next_block_group(fs_info, cache);
Li Dongyangf7039b12011-03-24 10:24:28 +000011023 }
11024
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011025 mutex_lock(&fs_info->fs_devices->device_list_mutex);
11026 devices = &fs_info->fs_devices->alloc_list;
Jeff Mahoney499f3772015-06-15 09:41:17 -040011027 list_for_each_entry(device, devices, dev_alloc_list) {
11028 ret = btrfs_trim_free_extents(device, range->minlen,
11029 &group_trimmed);
11030 if (ret)
11031 break;
11032
11033 trimmed += group_trimmed;
11034 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011035 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -040011036
Li Dongyangf7039b12011-03-24 10:24:28 +000011037 range->len = trimmed;
11038 return ret;
11039}
Miao Xie8257b2d2014-03-06 13:38:19 +080011040
11041/*
David Sterbaea14b57f2017-06-22 02:19:11 +020011042 * btrfs_{start,end}_write_no_snapshotting() are similar to
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011043 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
11044 * data into the page cache through nocow before the subvolume is snapshoted,
11045 * but flush the data into disk after the snapshot creation, or to prevent
David Sterbaea14b57f2017-06-22 02:19:11 +020011046 * operations while snapshotting is ongoing and that cause the snapshot to be
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011047 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +080011048 */
David Sterbaea14b57f2017-06-22 02:19:11 +020011049void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011050{
11051 percpu_counter_dec(&root->subv_writers->counter);
11052 /*
David Sterbaa83342a2015-02-16 19:36:47 +010011053 * Make sure counter is updated before we wake up waiters.
Miao Xie8257b2d2014-03-06 13:38:19 +080011054 */
11055 smp_mb();
11056 if (waitqueue_active(&root->subv_writers->wait))
11057 wake_up(&root->subv_writers->wait);
11058}
11059
David Sterbaea14b57f2017-06-22 02:19:11 +020011060int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011061{
David Sterbaea14b57f2017-06-22 02:19:11 +020011062 if (atomic_read(&root->will_be_snapshotted))
Miao Xie8257b2d2014-03-06 13:38:19 +080011063 return 0;
11064
11065 percpu_counter_inc(&root->subv_writers->counter);
11066 /*
11067 * Make sure counter is updated before we check for snapshot creation.
11068 */
11069 smp_mb();
David Sterbaea14b57f2017-06-22 02:19:11 +020011070 if (atomic_read(&root->will_be_snapshotted)) {
11071 btrfs_end_write_no_snapshotting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +080011072 return 0;
11073 }
11074 return 1;
11075}
Zhao Lei0bc19f902016-01-06 18:56:36 +080011076
Zhao Lei0bc19f902016-01-06 18:56:36 +080011077void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
11078{
11079 while (true) {
11080 int ret;
11081
David Sterbaea14b57f2017-06-22 02:19:11 +020011082 ret = btrfs_start_write_no_snapshotting(root);
Zhao Lei0bc19f902016-01-06 18:56:36 +080011083 if (ret)
11084 break;
David Howells5e4def22017-11-02 15:27:44 +000011085 wait_on_atomic_t(&root->will_be_snapshotted, atomic_t_wait,
Zhao Lei0bc19f902016-01-06 18:56:36 +080011086 TASK_UNINTERRUPTIBLE);
11087 }
11088}