blob: 6b07202385d3b620eac2ff223dfd1665a0fca6a4 [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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Borisovd2006e62017-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 Borisovd2006e62017-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
Qu Wenruo43b18592017-12-12 15:34:32 +08005763 ret = btrfs_qgroup_reserve_meta_prealloc(root, num_bytes, true);
5764 if (ret)
5765 return ret;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005766 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
5767 if (!ret) {
5768 block_rsv_add_bytes(block_rsv, num_bytes, 0);
5769 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5770 btrfs_ino(inode), num_bytes, 1);
5771 }
5772 return ret;
5773}
5774
5775/**
5776 * btrfs_inode_rsv_release - release any excessive reservation.
5777 * @inode - the inode we need to release from.
Qu Wenruo43b18592017-12-12 15:34:32 +08005778 * @qgroup_free - free or convert qgroup meta.
5779 * Unlike normal operation, qgroup meta reservation needs to know if we are
5780 * freeing qgroup reservation or just converting it into per-trans. Normally
5781 * @qgroup_free is true for error handling, and false for normal release.
Josef Bacik69fe2d72017-10-19 14:15:57 -04005782 *
5783 * This is the same as btrfs_block_rsv_release, except that it handles the
5784 * tracepoint for the reservation.
5785 */
Qu Wenruo43b18592017-12-12 15:34:32 +08005786static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
Josef Bacik69fe2d72017-10-19 14:15:57 -04005787{
5788 struct btrfs_fs_info *fs_info = inode->root->fs_info;
5789 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5790 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
5791 u64 released = 0;
5792
5793 /*
5794 * Since we statically set the block_rsv->size we just want to say we
5795 * are releasing 0 bytes, and then we'll just get the reservation over
5796 * the size free'd.
5797 */
5798 released = block_rsv_release_bytes(fs_info, block_rsv, global_rsv, 0);
5799 if (released > 0)
5800 trace_btrfs_space_reservation(fs_info, "delalloc",
5801 btrfs_ino(inode), released, 0);
Qu Wenruo43b18592017-12-12 15:34:32 +08005802 if (qgroup_free)
5803 btrfs_qgroup_free_meta_prealloc(inode->root, released);
5804 else
5805 btrfs_qgroup_convert_reserved_meta(inode->root, released);
Josef Bacik69fe2d72017-10-19 14:15:57 -04005806}
5807
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005808void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005809 struct btrfs_block_rsv *block_rsv,
5810 u64 num_bytes)
5811{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005812 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5813
Liu Bo17504582013-12-29 21:44:50 +08005814 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04005815 block_rsv->space_info != global_rsv->space_info)
5816 global_rsv = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005817 block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005818}
5819
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005820static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
5821{
5822 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
5823 struct btrfs_space_info *sinfo = block_rsv->space_info;
5824 u64 num_bytes;
5825
Josef Bacikae2e4722016-05-27 12:58:35 -04005826 /*
5827 * The global block rsv is based on the size of the extent tree, the
5828 * checksum tree and the root tree. If the fs is empty we want to set
5829 * it to a minimal amount for safety.
5830 */
5831 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) +
5832 btrfs_root_used(&fs_info->csum_root->root_item) +
5833 btrfs_root_used(&fs_info->tree_root->root_item);
5834 num_bytes = max_t(u64, num_bytes, SZ_16M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005835
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005836 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005837 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005838
Byongho Leeee221842015-12-15 01:42:10 +09005839 block_rsv->size = min_t(u64, num_bytes, SZ_512M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005840
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005841 if (block_rsv->reserved < block_rsv->size) {
Liu Bo41361352017-02-13 15:42:21 -08005842 num_bytes = btrfs_space_info_used(sinfo, true);
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005843 if (sinfo->total_bytes > num_bytes) {
5844 num_bytes = sinfo->total_bytes - num_bytes;
5845 num_bytes = min(num_bytes,
5846 block_rsv->size - block_rsv->reserved);
5847 block_rsv->reserved += num_bytes;
5848 sinfo->bytes_may_use += num_bytes;
5849 trace_btrfs_space_reservation(fs_info, "space_info",
5850 sinfo->flags, num_bytes,
5851 1);
5852 }
5853 } else if (block_rsv->reserved > block_rsv->size) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005854 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04005855 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005856 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005857 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005858 block_rsv->reserved = block_rsv->size;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005859 }
David Sterba182608c2011-05-05 13:13:16 +02005860
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005861 if (block_rsv->reserved == block_rsv->size)
5862 block_rsv->full = 1;
5863 else
5864 block_rsv->full = 0;
5865
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005866 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005867 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005868}
5869
Yan, Zhengf0486c62010-05-16 10:46:25 -04005870static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
5871{
5872 struct btrfs_space_info *space_info;
5873
5874 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
5875 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005876
5877 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005878 fs_info->global_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005879 fs_info->trans_block_rsv.space_info = space_info;
5880 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04005881 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005882
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005883 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
5884 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
5885 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
5886 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00005887 if (fs_info->quota_root)
5888 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005889 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005890
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005891 update_global_block_rsv(fs_info);
5892}
5893
5894static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
5895{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005896 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
5897 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005898 WARN_ON(fs_info->trans_block_rsv.size > 0);
5899 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
5900 WARN_ON(fs_info->chunk_block_rsv.size > 0);
5901 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04005902 WARN_ON(fs_info->delayed_block_rsv.size > 0);
5903 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04005904}
5905
Yan, Zhenga22285a2010-05-16 10:48:46 -04005906
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005907/*
5908 * To be called after all the new block groups attached to the transaction
5909 * handle have been created (btrfs_create_pending_block_groups()).
5910 */
5911void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
5912{
Jeff Mahoney64b63582016-06-20 17:23:41 -04005913 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005914
5915 if (!trans->chunk_bytes_reserved)
5916 return;
5917
5918 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
5919
5920 block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL,
5921 trans->chunk_bytes_reserved);
5922 trans->chunk_bytes_reserved = 0;
5923}
5924
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005925/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04005926int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005927 struct btrfs_inode *inode)
Yan, Zhengd68fc572010-05-16 10:49:58 -04005928{
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005929 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
5930 struct btrfs_root *root = inode->root;
Josef Bacik40acc3e2016-05-27 13:01:08 -04005931 /*
5932 * We always use trans->block_rsv here as we will have reserved space
5933 * for our orphan when starting the transaction, using get_block_rsv()
5934 * here will sometimes make us choose the wrong block rsv as we could be
5935 * doing a reloc inode for a non refcounted root.
5936 */
5937 struct btrfs_block_rsv *src_rsv = trans->block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005938 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
5939
5940 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04005941 * We need to hold space in order to delete our orphan item once we've
5942 * added it, so this takes the reservation so we can release it later
5943 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04005944 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005945 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5946
Nikolay Borisov9678c542018-01-08 11:45:05 +02005947 trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode),
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005948 num_bytes, 1);
Josef Bacik25d609f2016-03-25 13:25:48 -04005949 return btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005950}
5951
Nikolay Borisov703b3912017-02-20 13:50:40 +02005952void btrfs_orphan_release_metadata(struct btrfs_inode *inode)
Yan, Zhengd68fc572010-05-16 10:49:58 -04005953{
Nikolay Borisov703b3912017-02-20 13:50:40 +02005954 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
5955 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005956 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5957
Nikolay Borisov703b3912017-02-20 13:50:40 +02005958 trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode),
5959 num_bytes, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005960 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, num_bytes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005961}
5962
Miao Xied5c12072013-02-28 10:04:33 +00005963/*
5964 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
5965 * root: the root of the parent directory
5966 * rsv: block reservation
5967 * items: the number of items that we need do reservation
5968 * qgroup_reserved: used to return the reserved size in qgroup
5969 *
5970 * This function is used to reserve the space for snapshot/subvolume
5971 * creation and deletion. Those operations are different with the
5972 * common file/directory operations, they change two fs/file trees
5973 * and root tree, the number of items that the qgroup reserves is
5974 * different with the free space reservation. So we can not use
Nicholas D Steeves01327612016-05-19 21:18:45 -04005975 * the space reservation mechanism in start_transaction().
Miao Xied5c12072013-02-28 10:04:33 +00005976 */
5977int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
5978 struct btrfs_block_rsv *rsv,
5979 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005980 u64 *qgroup_reserved,
5981 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005982{
Miao Xied5c12072013-02-28 10:04:33 +00005983 u64 num_bytes;
5984 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005985 struct btrfs_fs_info *fs_info = root->fs_info;
5986 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00005987
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005988 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Miao Xied5c12072013-02-28 10:04:33 +00005989 /* One for parent inode, two for dir entries */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005990 num_bytes = 3 * fs_info->nodesize;
Qu Wenruo733e03a2017-12-12 15:34:29 +08005991 ret = btrfs_qgroup_reserve_meta_prealloc(root, num_bytes, true);
Miao Xied5c12072013-02-28 10:04:33 +00005992 if (ret)
5993 return ret;
5994 } else {
5995 num_bytes = 0;
5996 }
5997
5998 *qgroup_reserved = num_bytes;
5999
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006000 num_bytes = btrfs_calc_trans_metadata_size(fs_info, items);
6001 rsv->space_info = __find_space_info(fs_info,
Miao Xied5c12072013-02-28 10:04:33 +00006002 BTRFS_BLOCK_GROUP_METADATA);
6003 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
6004 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04006005
6006 if (ret == -ENOSPC && use_global_rsv)
Josef Bacik25d609f2016-03-25 13:25:48 -04006007 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04006008
Qu Wenruo71741092015-09-08 17:22:41 +08006009 if (ret && *qgroup_reserved)
Qu Wenruo733e03a2017-12-12 15:34:29 +08006010 btrfs_qgroup_free_meta_prealloc(root, *qgroup_reserved);
Miao Xied5c12072013-02-28 10:04:33 +00006011
6012 return ret;
6013}
6014
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006015void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
David Sterba7775c812017-02-10 19:18:18 +01006016 struct btrfs_block_rsv *rsv)
Miao Xied5c12072013-02-28 10:04:33 +00006017{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006018 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006019}
6020
Josef Bacik69fe2d72017-10-19 14:15:57 -04006021static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
6022 struct btrfs_inode *inode)
Josef Bacik9e0baf62011-07-15 15:16:44 +00006023{
Josef Bacik69fe2d72017-10-19 14:15:57 -04006024 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
6025 u64 reserve_size = 0;
6026 u64 csum_leaves;
6027 unsigned outstanding_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006028
Josef Bacik69fe2d72017-10-19 14:15:57 -04006029 lockdep_assert_held(&inode->lock);
6030 outstanding_extents = inode->outstanding_extents;
6031 if (outstanding_extents)
6032 reserve_size = btrfs_calc_trans_metadata_size(fs_info,
6033 outstanding_extents + 1);
6034 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
6035 inode->csum_bytes);
6036 reserve_size += btrfs_calc_trans_metadata_size(fs_info,
6037 csum_leaves);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006038
Josef Bacik69fe2d72017-10-19 14:15:57 -04006039 spin_lock(&block_rsv->lock);
6040 block_rsv->size = reserve_size;
6041 spin_unlock(&block_rsv->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006042}
6043
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006044int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006045{
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006046 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006047 unsigned nr_extents;
Miao Xie08e007d2012-10-16 11:33:38 +00006048 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07006049 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006050 bool delalloc_lock = true;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006051
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006052 /* If we are a free space inode we need to not flush since we will be in
6053 * the middle of a transaction commit. We also don't need the delalloc
6054 * mutex since we won't race with anybody. We need this mostly to make
6055 * lockdep shut its filthy mouth.
Josef Bacikbac357dc2016-07-20 16:48:45 -07006056 *
6057 * If we have a transaction open (can happen if we call truncate_block
6058 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006059 */
6060 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00006061 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006062 delalloc_lock = false;
Nikolay Borisovda07d4a2018-01-12 16:21:05 +02006063 } else {
6064 if (current->journal_info)
6065 flush = BTRFS_RESERVE_FLUSH_LIMIT;
Josef Bacikc09544e2011-08-30 10:19:10 -04006066
Nikolay Borisovda07d4a2018-01-12 16:21:05 +02006067 if (btrfs_transaction_in_commit(fs_info))
6068 schedule_timeout(1);
6069 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006070
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006071 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006072 mutex_lock(&inode->delalloc_mutex);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006073
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006074 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006075
6076 /* Add our new extents and calculate the new rsv size. */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006077 spin_lock(&inode->lock);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006078 nr_extents = count_max_extents(num_bytes);
Josef Bacik8b62f872017-10-19 14:15:55 -04006079 btrfs_mod_outstanding_extents(inode, nr_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006080 inode->csum_bytes += num_bytes;
6081 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006082 spin_unlock(&inode->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05006083
Josef Bacik69fe2d72017-10-19 14:15:57 -04006084 ret = btrfs_inode_rsv_refill(inode, flush);
Qu Wenruo43b18592017-12-12 15:34:32 +08006085 if (unlikely(ret))
Wang Shilong88e081bf2013-03-01 11:36:01 +00006086 goto out_fail;
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
Qu Wenruo43b18592017-12-12 15:34:32 +08006100 btrfs_inode_rsv_release(inode, true);
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.
Qu Wenruo43b18592017-12-12 15:34:32 +08006110 * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
Josef Bacik7709cde2011-08-04 10:25:02 -04006111 *
6112 * This will release the metadata reservation for an inode. This can be called
6113 * once we complete IO for a given set of bytes to release their metadata
Josef Bacik8b62f872017-10-19 14:15:55 -04006114 * reservations, or on error for the same reason.
Josef Bacik7709cde2011-08-04 10:25:02 -04006115 */
Qu Wenruo43b18592017-12-12 15:34:32 +08006116void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
6117 bool qgroup_free)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006118{
Nikolay Borisov691fa052017-02-20 13:50:42 +02006119 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006120
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006121 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006122 spin_lock(&inode->lock);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006123 inode->csum_bytes -= num_bytes;
6124 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006125 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006126
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006127 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04006128 return;
6129
Qu Wenruo43b18592017-12-12 15:34:32 +08006130 btrfs_inode_rsv_release(inode, qgroup_free);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006131}
6132
Josef Bacik7709cde2011-08-04 10:25:02 -04006133/**
Josef Bacik8b62f872017-10-19 14:15:55 -04006134 * btrfs_delalloc_release_extents - release our outstanding_extents
6135 * @inode: the inode to balance the reservation for.
6136 * @num_bytes: the number of bytes we originally reserved with
Qu Wenruo43b18592017-12-12 15:34:32 +08006137 * @qgroup_free: do we need to free qgroup meta reservation or convert them.
Josef Bacik8b62f872017-10-19 14:15:55 -04006138 *
6139 * When we reserve space we increase outstanding_extents for the extents we may
6140 * add. Once we've set the range as delalloc or created our ordered extents we
6141 * have outstanding_extents to track the real usage, so we use this to free our
6142 * temporarily tracked outstanding_extents. This _must_ be used in conjunction
6143 * with btrfs_delalloc_reserve_metadata.
6144 */
Qu Wenruo43b18592017-12-12 15:34:32 +08006145void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes,
6146 bool qgroup_free)
Josef Bacik8b62f872017-10-19 14:15:55 -04006147{
6148 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
6149 unsigned num_extents;
Josef Bacik8b62f872017-10-19 14:15:55 -04006150
6151 spin_lock(&inode->lock);
6152 num_extents = count_max_extents(num_bytes);
6153 btrfs_mod_outstanding_extents(inode, -num_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006154 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Josef Bacik8b62f872017-10-19 14:15:55 -04006155 spin_unlock(&inode->lock);
6156
Josef Bacik8b62f872017-10-19 14:15:55 -04006157 if (btrfs_is_testing(fs_info))
6158 return;
6159
Qu Wenruo43b18592017-12-12 15:34:32 +08006160 btrfs_inode_rsv_release(inode, qgroup_free);
Josef Bacik8b62f872017-10-19 14:15:55 -04006161}
6162
6163/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006164 * btrfs_delalloc_reserve_space - reserve data and metadata space for
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006165 * delalloc
6166 * @inode: inode we're writing to
6167 * @start: start range we are writing to
6168 * @len: how long the range we are writing to
Qu Wenruo364ecf32017-02-27 15:10:38 +08006169 * @reserved: mandatory parameter, record actually reserved qgroup ranges of
6170 * current reservation.
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006171 *
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006172 * This will do the following things
6173 *
6174 * o reserve space in data space info for num bytes
6175 * and reserve precious corresponding qgroup space
6176 * (Done in check_data_free_space)
6177 *
6178 * o reserve space for metadata space, based on the number of outstanding
6179 * extents and how much csums will be needed
6180 * also reserve metadata space in a per root over-reserve method.
6181 * o add to the inodes->delalloc_bytes
6182 * o add it to the fs_info's delalloc inodes list.
6183 * (Above 3 all done in delalloc_reserve_metadata)
6184 *
6185 * Return 0 for success
6186 * Return <0 for error(-ENOSPC or -EQUOT)
6187 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08006188int btrfs_delalloc_reserve_space(struct inode *inode,
6189 struct extent_changeset **reserved, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006190{
6191 int ret;
6192
Qu Wenruo364ecf32017-02-27 15:10:38 +08006193 ret = btrfs_check_data_free_space(inode, reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006194 if (ret < 0)
6195 return ret;
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006196 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006197 if (ret < 0)
Qu Wenruobc42bda2017-02-27 15:10:39 +08006198 btrfs_free_reserved_data_space(inode, *reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006199 return ret;
6200}
6201
6202/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006203 * btrfs_delalloc_release_space - release data and metadata space for delalloc
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006204 * @inode: inode we're releasing space for
6205 * @start: start position of the space already reserved
6206 * @len: the len of the space already reserved
Josef Bacik8b62f872017-10-19 14:15:55 -04006207 * @release_bytes: the len of the space we consumed or didn't use
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006208 *
6209 * This function will release the metadata space that was not used and will
6210 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
6211 * list if there are no delalloc bytes left.
6212 * Also it will handle the qgroup reserved space.
6213 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08006214void btrfs_delalloc_release_space(struct inode *inode,
Josef Bacik8b62f872017-10-19 14:15:55 -04006215 struct extent_changeset *reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08006216 u64 start, u64 len, bool qgroup_free)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006217{
Qu Wenruo43b18592017-12-12 15:34:32 +08006218 btrfs_delalloc_release_metadata(BTRFS_I(inode), len, qgroup_free);
Qu Wenruobc42bda2017-02-27 15:10:39 +08006219 btrfs_free_reserved_data_space(inode, reserved, start, len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006220}
6221
Josef Bacikce93ec52014-11-17 15:45:48 -05006222static int update_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6202df62016-06-22 18:54:22 -04006223 struct btrfs_fs_info *info, u64 bytenr,
Josef Bacikce93ec52014-11-17 15:45:48 -05006224 u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04006225{
Josef Bacik0af3d002010-06-21 14:48:16 -04006226 struct btrfs_block_group_cache *cache = NULL;
Chris Masondb945352007-10-15 16:15:53 -04006227 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006228 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04006229 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04006230 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04006231
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006232 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00006233 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02006234 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006235 if (alloc)
6236 old_val += num_bytes;
6237 else
6238 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02006239 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00006240 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006241
Chris Masond3977122009-01-05 21:25:51 -05006242 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04006243 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006244 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006245 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006246 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
6247 BTRFS_BLOCK_GROUP_RAID1 |
6248 BTRFS_BLOCK_GROUP_RAID10))
6249 factor = 2;
6250 else
6251 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04006252 /*
6253 * If this block group has free space cache written out, we
6254 * need to make sure to load it if we are removing space. This
6255 * is because we need the unpinning stage to actually add the
6256 * space back to the block group, otherwise we will leak space.
6257 */
6258 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00006259 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04006260
Chris Masondb945352007-10-15 16:15:53 -04006261 byte_in_group = bytenr - cache->key.objectid;
6262 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04006263
Josef Bacik25179202008-10-29 14:49:05 -04006264 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006265 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04006266
Jeff Mahoney6202df62016-06-22 18:54:22 -04006267 if (btrfs_test_opt(info, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04006268 cache->disk_cache_state < BTRFS_DC_CLEAR)
6269 cache->disk_cache_state = BTRFS_DC_CLEAR;
6270
Chris Mason9078a3e2007-04-26 16:46:15 -04006271 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04006272 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04006273 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04006274 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006275 btrfs_set_block_group_used(&cache->item, old_val);
6276 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006277 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006278 cache->space_info->bytes_used += num_bytes;
6279 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04006280 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006281 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04006282 } else {
Chris Masondb945352007-10-15 16:15:53 -04006283 old_val -= num_bytes;
Filipe Mananaae0ab002014-11-26 15:28:52 +00006284 btrfs_set_block_group_used(&cache->item, old_val);
6285 cache->pinned += num_bytes;
6286 cache->space_info->bytes_pinned += num_bytes;
6287 cache->space_info->bytes_used -= num_bytes;
6288 cache->space_info->disk_used -= num_bytes * factor;
6289 spin_unlock(&cache->lock);
6290 spin_unlock(&cache->space_info->lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04006291
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006292 trace_btrfs_space_reservation(info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006293 cache->space_info->flags,
6294 num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07006295 percpu_counter_add(&cache->space_info->total_bytes_pinned,
6296 num_bytes);
Filipe Mananaae0ab002014-11-26 15:28:52 +00006297 set_extent_dirty(info->pinned_extents,
6298 bytenr, bytenr + num_bytes - 1,
6299 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04006300 }
Chris Mason1bbc6212015-04-06 12:46:08 -07006301
6302 spin_lock(&trans->transaction->dirty_bgs_lock);
6303 if (list_empty(&cache->dirty_list)) {
6304 list_add_tail(&cache->dirty_list,
6305 &trans->transaction->dirty_bgs);
6306 trans->transaction->num_dirty_bgs++;
6307 btrfs_get_block_group(cache);
6308 }
6309 spin_unlock(&trans->transaction->dirty_bgs_lock);
6310
Filipe Manana036a9342015-11-23 15:25:16 +00006311 /*
6312 * No longer have used bytes in this block group, queue it for
6313 * deletion. We do this after adding the block group to the
6314 * dirty list to avoid races between cleaner kthread and space
6315 * cache writeout.
6316 */
6317 if (!alloc && old_val == 0) {
6318 spin_lock(&info->unused_bgs_lock);
6319 if (list_empty(&cache->bg_list)) {
6320 btrfs_get_block_group(cache);
6321 list_add_tail(&cache->bg_list,
6322 &info->unused_bgs);
6323 }
6324 spin_unlock(&info->unused_bgs_lock);
6325 }
6326
Chris Masonfa9c0d792009-04-03 09:47:43 -04006327 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04006328 total -= num_bytes;
6329 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006330 }
6331 return 0;
6332}
Chris Mason6324fbf2008-03-24 15:01:59 -04006333
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006334static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04006335{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006336 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05006337 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006338
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006339 spin_lock(&fs_info->block_group_cache_lock);
6340 bytenr = fs_info->first_logical_byte;
6341 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00006342
6343 if (bytenr < (u64)-1)
6344 return bytenr;
6345
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006346 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006347 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04006348 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006349
Yan Zhengd2fb3432008-12-11 16:30:39 -05006350 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006351 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006352
6353 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04006354}
6355
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006356static int pin_down_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006357 struct btrfs_block_group_cache *cache,
6358 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05006359{
Yan Zheng11833d62009-09-11 16:11:19 -04006360 spin_lock(&cache->space_info->lock);
6361 spin_lock(&cache->lock);
6362 cache->pinned += num_bytes;
6363 cache->space_info->bytes_pinned += num_bytes;
6364 if (reserved) {
6365 cache->reserved -= num_bytes;
6366 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05006367 }
Yan Zheng11833d62009-09-11 16:11:19 -04006368 spin_unlock(&cache->lock);
6369 spin_unlock(&cache->space_info->lock);
6370
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006371 trace_btrfs_space_reservation(fs_info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006372 cache->space_info->flags, num_bytes, 1);
Omar Sandoval4da8b762017-06-06 16:45:28 -07006373 percpu_counter_add(&cache->space_info->total_bytes_pinned, num_bytes);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006374 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006375 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05006376 return 0;
6377}
Chris Mason9078a3e2007-04-26 16:46:15 -04006378
Yan, Zhengf0486c62010-05-16 10:46:25 -04006379/*
6380 * this function must be called within transaction
6381 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006382int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006383 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04006384{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006385 struct btrfs_block_group_cache *cache;
6386
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006387 cache = btrfs_lookup_block_group(fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006388 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006389
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006390 pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006391
6392 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04006393 return 0;
6394}
Zheng Yane8569812008-09-26 10:05:48 -04006395
Yan, Zhengf0486c62010-05-16 10:46:25 -04006396/*
Chris Masone688b7252011-10-31 20:52:39 -04006397 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04006398 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006399int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04006400 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006401{
Chris Masone688b7252011-10-31 20:52:39 -04006402 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04006403 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04006404
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006405 cache = btrfs_lookup_block_group(fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006406 if (!cache)
6407 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04006408
6409 /*
6410 * pull in the free space cache (if any) so that our pin
6411 * removes the free space from the cache. We have load_only set
6412 * to one because the slow code to read in the free extents does check
6413 * the pinned extents.
6414 */
Liu Bof6373bf2012-12-27 09:01:18 +00006415 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04006416
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006417 pin_down_extent(fs_info, cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04006418
6419 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04006420 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04006421 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006422 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04006423}
6424
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006425static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
6426 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006427{
6428 int ret;
6429 struct btrfs_block_group_cache *block_group;
6430 struct btrfs_caching_control *caching_ctl;
6431
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006432 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006433 if (!block_group)
6434 return -EINVAL;
6435
6436 cache_block_group(block_group, 0);
6437 caching_ctl = get_caching_control(block_group);
6438
6439 if (!caching_ctl) {
6440 /* Logic error */
6441 BUG_ON(!block_group_cache_done(block_group));
6442 ret = btrfs_remove_free_space(block_group, start, num_bytes);
6443 } else {
6444 mutex_lock(&caching_ctl->mutex);
6445
6446 if (start >= caching_ctl->progress) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006447 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006448 } else if (start + num_bytes <= caching_ctl->progress) {
6449 ret = btrfs_remove_free_space(block_group,
6450 start, num_bytes);
6451 } else {
6452 num_bytes = caching_ctl->progress - start;
6453 ret = btrfs_remove_free_space(block_group,
6454 start, num_bytes);
6455 if (ret)
6456 goto out_lock;
6457
6458 num_bytes = (start + num_bytes) -
6459 caching_ctl->progress;
6460 start = caching_ctl->progress;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006461 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006462 }
6463out_lock:
6464 mutex_unlock(&caching_ctl->mutex);
6465 put_caching_control(caching_ctl);
6466 }
6467 btrfs_put_block_group(block_group);
6468 return ret;
6469}
6470
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006471int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info,
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006472 struct extent_buffer *eb)
6473{
6474 struct btrfs_file_extent_item *item;
6475 struct btrfs_key key;
6476 int found_type;
6477 int i;
6478
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006479 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006480 return 0;
6481
6482 for (i = 0; i < btrfs_header_nritems(eb); i++) {
6483 btrfs_item_key_to_cpu(eb, &key, i);
6484 if (key.type != BTRFS_EXTENT_DATA_KEY)
6485 continue;
6486 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
6487 found_type = btrfs_file_extent_type(eb, item);
6488 if (found_type == BTRFS_FILE_EXTENT_INLINE)
6489 continue;
6490 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
6491 continue;
6492 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
6493 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006494 __exclude_logged_extent(fs_info, key.objectid, key.offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006495 }
6496
6497 return 0;
6498}
6499
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006500static void
6501btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg)
6502{
6503 atomic_inc(&bg->reservations);
6504}
6505
6506void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
6507 const u64 start)
6508{
6509 struct btrfs_block_group_cache *bg;
6510
6511 bg = btrfs_lookup_block_group(fs_info, start);
6512 ASSERT(bg);
6513 if (atomic_dec_and_test(&bg->reservations))
6514 wake_up_atomic_t(&bg->reservations);
6515 btrfs_put_block_group(bg);
6516}
6517
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006518void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6519{
6520 struct btrfs_space_info *space_info = bg->space_info;
6521
6522 ASSERT(bg->ro);
6523
6524 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
6525 return;
6526
6527 /*
6528 * Our block group is read only but before we set it to read only,
6529 * some task might have had allocated an extent from it already, but it
6530 * has not yet created a respective ordered extent (and added it to a
6531 * root's list of ordered extents).
6532 * Therefore wait for any task currently allocating extents, since the
6533 * block group's reservations counter is incremented while a read lock
6534 * on the groups' semaphore is held and decremented after releasing
6535 * the read access on that semaphore and creating the ordered extent.
6536 */
6537 down_write(&space_info->groups_sem);
6538 up_write(&space_info->groups_sem);
6539
David Howells5e4def22017-11-02 15:27:44 +00006540 wait_on_atomic_t(&bg->reservations, atomic_t_wait,
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006541 TASK_UNINTERRUPTIBLE);
6542}
6543
Josef Bacikfb25e912011-07-26 17:00:46 -04006544/**
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006545 * btrfs_add_reserved_bytes - update the block_group and space info counters
Josef Bacikfb25e912011-07-26 17:00:46 -04006546 * @cache: The cache we are manipulating
Wang Xiaoguang18513092016-07-25 15:51:40 +08006547 * @ram_bytes: The number of bytes of file content, and will be same to
6548 * @num_bytes except for the compress path.
Josef Bacikfb25e912011-07-26 17:00:46 -04006549 * @num_bytes: The number of bytes in question
Miao Xiee570fd22014-06-19 10:42:50 +08006550 * @delalloc: The blocks are allocated for the delalloc write
Josef Bacikfb25e912011-07-26 17:00:46 -04006551 *
Xiaoguang Wang745699e2016-09-23 12:38:50 +08006552 * This is called by the allocator when it reserves space. If this is a
6553 * reservation and the block group has become read only we cannot make the
6554 * reservation and return -EAGAIN, otherwise this function always succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04006555 */
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006556static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
Wang Xiaoguang18513092016-07-25 15:51:40 +08006557 u64 ram_bytes, u64 num_bytes, int delalloc)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006558{
Josef Bacikfb25e912011-07-26 17:00:46 -04006559 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006560 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006561
Josef Bacikfb25e912011-07-26 17:00:46 -04006562 spin_lock(&space_info->lock);
6563 spin_lock(&cache->lock);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006564 if (cache->ro) {
6565 ret = -EAGAIN;
Josef Bacikfb25e912011-07-26 17:00:46 -04006566 } else {
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006567 cache->reserved += num_bytes;
6568 space_info->bytes_reserved += num_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006569
Wang Xiaoguang18513092016-07-25 15:51:40 +08006570 trace_btrfs_space_reservation(cache->fs_info,
6571 "space_info", space_info->flags,
6572 ram_bytes, 0);
6573 space_info->bytes_may_use -= ram_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006574 if (delalloc)
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006575 cache->delalloc_bytes += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006576 }
Josef Bacikfb25e912011-07-26 17:00:46 -04006577 spin_unlock(&cache->lock);
6578 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006579 return ret;
6580}
6581
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006582/**
6583 * btrfs_free_reserved_bytes - update the block_group and space info counters
6584 * @cache: The cache we are manipulating
6585 * @num_bytes: The number of bytes in question
6586 * @delalloc: The blocks are allocated for the delalloc write
6587 *
6588 * This is called by somebody who is freeing space that was never actually used
6589 * on disk. For example if you reserve some space for a new leaf in transaction
6590 * A and before transaction A commits you free that leaf, you call this with
6591 * reserve set to 0 in order to clear the reservation.
6592 */
6593
6594static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
6595 u64 num_bytes, int delalloc)
6596{
6597 struct btrfs_space_info *space_info = cache->space_info;
6598 int ret = 0;
6599
6600 spin_lock(&space_info->lock);
6601 spin_lock(&cache->lock);
6602 if (cache->ro)
6603 space_info->bytes_readonly += num_bytes;
6604 cache->reserved -= num_bytes;
6605 space_info->bytes_reserved -= num_bytes;
6606
6607 if (delalloc)
6608 cache->delalloc_bytes -= num_bytes;
6609 spin_unlock(&cache->lock);
6610 spin_unlock(&space_info->lock);
6611 return ret;
6612}
David Sterba8b74c032017-02-10 19:20:56 +01006613void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04006614{
Yan Zheng11833d62009-09-11 16:11:19 -04006615 struct btrfs_caching_control *next;
6616 struct btrfs_caching_control *caching_ctl;
6617 struct btrfs_block_group_cache *cache;
6618
Josef Bacik9e351cc2014-03-13 15:42:13 -04006619 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04006620
6621 list_for_each_entry_safe(caching_ctl, next,
6622 &fs_info->caching_block_groups, list) {
6623 cache = caching_ctl->block_group;
6624 if (block_group_cache_done(cache)) {
6625 cache->last_byte_to_unpin = (u64)-1;
6626 list_del_init(&caching_ctl->list);
6627 put_caching_control(caching_ctl);
6628 } else {
6629 cache->last_byte_to_unpin = caching_ctl->progress;
6630 }
6631 }
6632
6633 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6634 fs_info->pinned_extents = &fs_info->freed_extents[1];
6635 else
6636 fs_info->pinned_extents = &fs_info->freed_extents[0];
6637
Josef Bacik9e351cc2014-03-13 15:42:13 -04006638 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006639
6640 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04006641}
6642
Josef Bacikc759c4e2015-10-02 15:25:10 -04006643/*
6644 * Returns the free cluster for the given space info and sets empty_cluster to
6645 * what it should be based on the mount options.
6646 */
6647static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006648fetch_cluster_info(struct btrfs_fs_info *fs_info,
6649 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04006650{
6651 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006652
6653 *empty_cluster = 0;
6654 if (btrfs_mixed_space_info(space_info))
6655 return ret;
6656
Josef Bacikc759c4e2015-10-02 15:25:10 -04006657 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006658 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006659 if (btrfs_test_opt(fs_info, SSD))
6660 *empty_cluster = SZ_2M;
6661 else
Byongho Leeee221842015-12-15 01:42:10 +09006662 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006663 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
6664 btrfs_test_opt(fs_info, SSD_SPREAD)) {
6665 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006666 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006667 }
6668
6669 return ret;
6670}
6671
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006672static int unpin_extent_range(struct btrfs_fs_info *fs_info,
6673 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00006674 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04006675{
Yan Zheng11833d62009-09-11 16:11:19 -04006676 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04006677 struct btrfs_space_info *space_info;
6678 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006679 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04006680 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006681 u64 total_unpinned = 0;
6682 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04006683 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04006684
6685 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04006686 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04006687 if (!cache ||
6688 start >= cache->key.objectid + cache->key.offset) {
6689 if (cache)
6690 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04006691 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006692 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006693 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04006694
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006695 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04006696 cache->space_info,
6697 &empty_cluster);
6698 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04006699 }
6700
6701 len = cache->key.objectid + cache->key.offset - start;
6702 len = min(len, end + 1 - start);
6703
6704 if (start < cache->last_byte_to_unpin) {
6705 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00006706 if (return_free_space)
6707 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04006708 }
Josef Bacik25179202008-10-29 14:49:05 -04006709
Yan, Zhengf0486c62010-05-16 10:46:25 -04006710 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006711 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006712 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006713
Josef Bacikc759c4e2015-10-02 15:25:10 -04006714 /*
6715 * If this space cluster has been marked as fragmented and we've
6716 * unpinned enough in this block group to potentially allow a
6717 * cluster to be created inside of it go ahead and clear the
6718 * fragmented check.
6719 */
6720 if (cluster && cluster->fragmented &&
6721 total_unpinned > empty_cluster) {
6722 spin_lock(&cluster->lock);
6723 cluster->fragmented = 0;
6724 spin_unlock(&cluster->lock);
6725 }
6726
Josef Bacik7b398f82012-10-22 15:52:28 -04006727 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006728 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006729 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006730 space_info->bytes_pinned -= len;
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006731
6732 trace_btrfs_space_reservation(fs_info, "pinned",
6733 space_info->flags, len, 0);
Josef Bacik4f4db212015-09-29 11:40:47 -04006734 space_info->max_extent_size = 0;
Liu Bod288db52014-07-02 16:58:01 +08006735 percpu_counter_add(&space_info->total_bytes_pinned, -len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006736 if (cache->ro) {
6737 space_info->bytes_readonly += len;
6738 readonly = true;
6739 }
Josef Bacik25179202008-10-29 14:49:05 -04006740 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006741 if (!readonly && return_free_space &&
6742 global_rsv->space_info == space_info) {
6743 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03006744
Josef Bacik7b398f82012-10-22 15:52:28 -04006745 spin_lock(&global_rsv->lock);
6746 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04006747 to_add = min(len, global_rsv->size -
6748 global_rsv->reserved);
6749 global_rsv->reserved += to_add;
6750 space_info->bytes_may_use += to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006751 if (global_rsv->reserved >= global_rsv->size)
6752 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04006753 trace_btrfs_space_reservation(fs_info,
6754 "space_info",
6755 space_info->flags,
6756 to_add, 1);
6757 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006758 }
6759 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006760 /* Add to any tickets we may have */
6761 if (len)
6762 space_info_add_new_bytes(fs_info, space_info,
6763 len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006764 }
6765 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006766 }
6767
6768 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006769 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04006770 return 0;
6771}
6772
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02006773int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
Chris Masona28ec192007-03-06 20:08:01 -05006774{
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02006775 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006776 struct btrfs_block_group_cache *block_group, *tmp;
6777 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04006778 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04006779 u64 start;
6780 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05006781 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05006782
Yan Zheng11833d62009-09-11 16:11:19 -04006783 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6784 unpin = &fs_info->freed_extents[1];
6785 else
6786 unpin = &fs_info->freed_extents[0];
6787
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006788 while (!trans->aborted) {
Filipe Mananad4b450c2015-01-29 19:18:25 +00006789 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04006790 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04006791 EXTENT_DIRTY, NULL);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006792 if (ret) {
6793 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05006794 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00006795 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006796
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006797 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006798 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00006799 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006800
David Sterbaaf6f8f62016-04-26 23:54:39 +02006801 clear_extent_dirty(unpin, start, end);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006802 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006803 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masonb9473432009-03-13 11:00:37 -04006804 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05006805 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006806
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006807 /*
6808 * Transaction is finished. We don't need the lock anymore. We
6809 * do need to clean up the block groups in case of a transaction
6810 * abort.
6811 */
6812 deleted_bgs = &trans->transaction->deleted_bgs;
6813 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
6814 u64 trimmed = 0;
6815
6816 ret = -EROFS;
6817 if (!trans->aborted)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006818 ret = btrfs_discard_extent(fs_info,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006819 block_group->key.objectid,
6820 block_group->key.offset,
6821 &trimmed);
6822
6823 list_del_init(&block_group->bg_list);
6824 btrfs_put_block_group_trimming(block_group);
6825 btrfs_put_block_group(block_group);
6826
6827 if (ret) {
6828 const char *errstr = btrfs_decode_error(ret);
6829 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02006830 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006831 ret, errstr);
6832 }
6833 }
6834
Chris Masone20d96d2007-03-22 12:13:20 -04006835 return 0;
6836}
6837
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006838static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006839 struct btrfs_fs_info *info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006840 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006841 u64 root_objectid, u64 owner_objectid,
6842 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006843 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05006844{
Chris Masone2fa7222007-03-12 16:22:34 -04006845 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006846 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04006847 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04006848 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006849 struct btrfs_extent_item *ei;
6850 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05006851 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006852 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05006853 int extent_slot = 0;
6854 int found_extent = 0;
6855 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006856 u32 item_size;
6857 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006858 u64 bytenr = node->bytenr;
6859 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07006860 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006861 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05006862
Chris Mason5caf2a02007-04-02 11:20:42 -04006863 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04006864 if (!path)
6865 return -ENOMEM;
6866
David Sterbae4058b52015-11-27 16:31:35 +01006867 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04006868 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006869
6870 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
6871 BUG_ON(!is_data && refs_to_drop != 1);
6872
Josef Bacik3173a182013-03-07 14:22:04 -05006873 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02006874 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05006875
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05006876 ret = lookup_extent_backref(trans, info, path, &iref,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006877 bytenr, num_bytes, parent,
6878 root_objectid, owner_objectid,
6879 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05006880 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05006881 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006882 while (extent_slot >= 0) {
6883 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05006884 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006885 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05006886 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006887 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
6888 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05006889 found_extent = 1;
6890 break;
6891 }
Josef Bacik3173a182013-03-07 14:22:04 -05006892 if (key.type == BTRFS_METADATA_ITEM_KEY &&
6893 key.offset == owner_objectid) {
6894 found_extent = 1;
6895 break;
6896 }
Chris Mason952fcca2008-02-18 16:33:44 -05006897 if (path->slots[0] - extent_slot > 5)
6898 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006899 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05006900 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006901#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6902 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
6903 if (found_extent && item_size < sizeof(*ei))
6904 found_extent = 0;
6905#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04006906 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006907 BUG_ON(iref);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05006908 ret = remove_extent_backref(trans, info, path, NULL,
6909 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07006910 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06006911 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006912 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006913 goto out;
6914 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006915 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04006916 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006917
6918 key.objectid = bytenr;
6919 key.type = BTRFS_EXTENT_ITEM_KEY;
6920 key.offset = num_bytes;
6921
Josef Bacik3173a182013-03-07 14:22:04 -05006922 if (!is_data && skinny_metadata) {
6923 key.type = BTRFS_METADATA_ITEM_KEY;
6924 key.offset = owner_objectid;
6925 }
6926
Zheng Yan31840ae2008-09-23 13:14:14 -04006927 ret = btrfs_search_slot(trans, extent_root,
6928 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05006929 if (ret > 0 && skinny_metadata && path->slots[0]) {
6930 /*
6931 * Couldn't find our skinny metadata item,
6932 * see if we have ye olde extent item.
6933 */
6934 path->slots[0]--;
6935 btrfs_item_key_to_cpu(path->nodes[0], &key,
6936 path->slots[0]);
6937 if (key.objectid == bytenr &&
6938 key.type == BTRFS_EXTENT_ITEM_KEY &&
6939 key.offset == num_bytes)
6940 ret = 0;
6941 }
6942
6943 if (ret > 0 && skinny_metadata) {
6944 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01006945 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05006946 key.type = BTRFS_EXTENT_ITEM_KEY;
6947 key.offset = num_bytes;
6948 btrfs_release_path(path);
6949 ret = btrfs_search_slot(trans, extent_root,
6950 &key, path, -1, 1);
6951 }
6952
Josef Bacikf3465ca2008-11-12 14:19:50 -05006953 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006954 btrfs_err(info,
6955 "umm, got %d back from search, was looking for %llu",
6956 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00006957 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02006958 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006959 }
David Sterba005d6422012-09-18 07:52:32 -06006960 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006961 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006962 goto out;
6963 }
Zheng Yan31840ae2008-09-23 13:14:14 -04006964 extent_slot = path->slots[0];
6965 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306966 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02006967 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006968 btrfs_err(info,
6969 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006970 bytenr, parent, root_objectid, owner_objectid,
6971 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04006972 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04006973 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006974 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04006975 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006976 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05006977 }
Chris Mason5f39d392007-10-15 16:14:19 -04006978
6979 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006980 item_size = btrfs_item_size_nr(leaf, extent_slot);
6981#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6982 if (item_size < sizeof(*ei)) {
6983 BUG_ON(found_extent || extent_slot != path->slots[0]);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05006984 ret = convert_extent_item_v0(trans, info, path, owner_objectid,
6985 0);
David Sterba005d6422012-09-18 07:52:32 -06006986 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006987 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006988 goto out;
6989 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006990
David Sterbab3b4aa72011-04-21 01:20:15 +02006991 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006992 path->leave_spinning = 1;
6993
6994 key.objectid = bytenr;
6995 key.type = BTRFS_EXTENT_ITEM_KEY;
6996 key.offset = num_bytes;
6997
6998 ret = btrfs_search_slot(trans, extent_root, &key, path,
6999 -1, 1);
7000 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007001 btrfs_err(info,
7002 "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007003 ret, bytenr);
David Sterbaa4f78752017-06-29 18:37:49 +02007004 btrfs_print_leaf(path->nodes[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007005 }
David Sterba005d6422012-09-18 07:52:32 -06007006 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007007 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007008 goto out;
7009 }
7010
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007011 extent_slot = path->slots[0];
7012 leaf = path->nodes[0];
7013 item_size = btrfs_item_size_nr(leaf, extent_slot);
7014 }
7015#endif
7016 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05007017 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04007018 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05007019 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
7020 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007021 struct btrfs_tree_block_info *bi;
7022 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
7023 bi = (struct btrfs_tree_block_info *)(ei + 1);
7024 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05007025 }
7026
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007027 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04007028 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007029 btrfs_err(info,
7030 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
7031 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04007032 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04007033 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04007034 goto out;
7035 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007036 refs -= refs_to_drop;
7037
7038 if (refs > 0) {
7039 if (extent_op)
7040 __run_delayed_extent_op(extent_op, leaf, ei);
7041 /*
7042 * In the case of inline back ref, reference count will
7043 * be updated by remove_extent_backref
7044 */
7045 if (iref) {
7046 BUG_ON(!found_extent);
7047 } else {
7048 btrfs_set_extent_refs(leaf, ei, refs);
7049 btrfs_mark_buffer_dirty(leaf);
7050 }
7051 if (found_extent) {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05007052 ret = remove_extent_backref(trans, info, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007053 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07007054 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06007055 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007056 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007057 goto out;
7058 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007059 }
7060 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007061 if (found_extent) {
7062 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08007063 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007064 if (iref) {
7065 BUG_ON(path->slots[0] != extent_slot);
7066 } else {
7067 BUG_ON(path->slots[0] != extent_slot + 1);
7068 path->slots[0] = extent_slot;
7069 num_to_del = 2;
7070 }
Chris Mason78fae272007-03-25 11:35:08 -04007071 }
Chris Masonb9473432009-03-13 11:00:37 -04007072
Josef Bacikfcebe452014-05-13 17:30:47 -07007073 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05007074 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
7075 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06007076 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007077 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007078 goto out;
7079 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007080 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01007081
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007082 if (is_data) {
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007083 ret = btrfs_del_csums(trans, info, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06007084 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007085 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007086 goto out;
7087 }
Chris Mason459931e2008-12-10 09:10:46 -05007088 }
7089
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007090 ret = add_to_free_space_tree(trans, info, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007091 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007092 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007093 goto out;
7094 }
7095
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007096 ret = update_block_group(trans, info, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06007097 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007098 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007099 goto out;
7100 }
Chris Masona28ec192007-03-06 20:08:01 -05007101 }
Josef Bacikfcebe452014-05-13 17:30:47 -07007102 btrfs_release_path(path);
7103
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007104out:
Chris Mason5caf2a02007-04-02 11:20:42 -04007105 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05007106 return ret;
7107}
7108
7109/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007110 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04007111 * delayed ref for that extent as well. This searches the delayed ref tree for
7112 * a given extent, and if there are no other delayed refs to be processed, it
7113 * removes it from the tree.
7114 */
7115static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007116 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04007117{
7118 struct btrfs_delayed_ref_head *head;
7119 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007120 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04007121
7122 delayed_refs = &trans->transaction->delayed_refs;
7123 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08007124 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04007125 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08007126 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04007127
Josef Bacikd7df2c72014-01-23 09:21:38 -05007128 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04007129 if (!RB_EMPTY_ROOT(&head->ref_tree))
Chris Mason1887be62009-03-13 10:11:24 -04007130 goto out;
7131
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007132 if (head->extent_op) {
7133 if (!head->must_insert_reserved)
7134 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00007135 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007136 head->extent_op = NULL;
7137 }
7138
Chris Mason1887be62009-03-13 10:11:24 -04007139 /*
7140 * waiting for the lock here would deadlock. If someone else has it
7141 * locked they are already in the process of dropping it anyway
7142 */
7143 if (!mutex_trylock(&head->mutex))
7144 goto out;
7145
7146 /*
7147 * at this point we have a head with no other entries. Go
7148 * ahead and process it.
7149 */
Liu Boc46effa2013-10-14 12:59:45 +08007150 rb_erase(&head->href_node, &delayed_refs->href_root);
Josef Bacikd2788502017-09-29 15:43:57 -04007151 RB_CLEAR_NODE(&head->href_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05007152 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04007153
7154 /*
7155 * we don't take a ref on the node because we're removing it from the
7156 * tree, so we just steal the ref the tree was holding.
7157 */
Chris Masonc3e69d52009-03-13 10:17:05 -04007158 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007159 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04007160 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007161 head->processing = 0;
7162 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04007163 spin_unlock(&delayed_refs->lock);
7164
Yan, Zhengf0486c62010-05-16 10:46:25 -04007165 BUG_ON(head->extent_op);
7166 if (head->must_insert_reserved)
7167 ret = 1;
7168
7169 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04007170 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007171 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04007172out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05007173 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08007174
7175out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04007176 spin_unlock(&delayed_refs->lock);
7177 return 0;
7178}
7179
Yan, Zhengf0486c62010-05-16 10:46:25 -04007180void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7181 struct btrfs_root *root,
7182 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02007183 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007184{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007185 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb150a4f2013-06-19 15:00:04 -04007186 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007187 int ret;
7188
7189 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07007190 int old_ref_mod, new_ref_mod;
7191
Josef Bacikfd708b82017-09-29 15:43:50 -04007192 btrfs_ref_tree_mod(root, buf->start, buf->len, parent,
7193 root->root_key.objectid,
7194 btrfs_header_level(buf), 0,
7195 BTRFS_DROP_DELAYED_REF);
Omar Sandoval7be07912017-06-06 16:45:30 -07007196 ret = btrfs_add_delayed_tree_ref(fs_info, trans, buf->start,
7197 buf->len, parent,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007198 root->root_key.objectid,
7199 btrfs_header_level(buf),
Omar Sandoval7be07912017-06-06 16:45:30 -07007200 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007201 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007202 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07007203 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007204 }
7205
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007206 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
Filipe Manana62198722015-01-06 20:18:45 +00007207 struct btrfs_block_group_cache *cache;
7208
Yan, Zhengf0486c62010-05-16 10:46:25 -04007209 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007210 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007211 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04007212 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007213 }
7214
Omar Sandoval4da8b762017-06-06 16:45:28 -07007215 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007216 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00007217
Yan, Zhengf0486c62010-05-16 10:46:25 -04007218 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007219 pin_down_extent(fs_info, cache, buf->start,
7220 buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00007221 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04007222 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007223 }
7224
7225 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
7226
7227 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08007228 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00007229 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007230 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007231 }
7232out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04007233 if (pin)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007234 add_pinned_bytes(fs_info, buf->len, btrfs_header_level(buf),
Josef Bacikb150a4f2013-06-19 15:00:04 -04007235 root->root_key.objectid);
7236
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007237 if (last_ref) {
7238 /*
7239 * Deleting the buffer, clear the corrupt flag since it doesn't
7240 * matter anymore.
7241 */
7242 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
7243 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007244}
7245
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007246/* Can return -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007247int btrfs_free_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007248 struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007249 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01007250 u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04007251{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007252 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07007253 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04007254 int ret;
7255
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04007256 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007257 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02007258
Josef Bacikfd708b82017-09-29 15:43:50 -04007259 if (root_objectid != BTRFS_TREE_LOG_OBJECTID)
7260 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent,
7261 root_objectid, owner, offset,
7262 BTRFS_DROP_DELAYED_REF);
7263
Chris Mason56bec292009-03-13 10:10:06 -04007264 /*
7265 * tree log blocks never actually go into the extent allocation
7266 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04007267 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007268 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
7269 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04007270 /* unlocks the pinned mutex */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007271 btrfs_pin_extent(fs_info, bytenr, num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07007272 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04007273 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007274 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007275 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007276 num_bytes, parent,
7277 root_objectid, (int)owner,
7278 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007279 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007280 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007281 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007282 num_bytes, parent,
7283 root_objectid, owner, offset,
7284 0, BTRFS_DROP_DELAYED_REF,
Omar Sandovald7eae342017-06-06 16:45:31 -07007285 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04007286 }
Omar Sandovald7eae342017-06-06 16:45:31 -07007287
7288 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
7289 add_pinned_bytes(fs_info, num_bytes, owner, root_objectid);
7290
Chris Mason925baed2008-06-25 16:01:30 -04007291 return ret;
7292}
7293
Chris Masonfec577f2007-02-26 10:40:21 -05007294/*
Josef Bacik817d52f2009-07-13 21:29:25 -04007295 * when we wait for progress in the block group caching, its because
7296 * our allocation attempt failed at least once. So, we must sleep
7297 * and let some progress happen before we try again.
7298 *
7299 * This function will sleep at least once waiting for new free space to
7300 * show up, and then it will check the block group free space numbers
7301 * for our min num_bytes. Another option is to have it go ahead
7302 * and look in the rbtree for a free extent of a given size, but this
7303 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04007304 *
7305 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
7306 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04007307 */
Josef Bacik36cce922013-08-05 11:15:21 -04007308static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04007309wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
7310 u64 num_bytes)
7311{
Yan Zheng11833d62009-09-11 16:11:19 -04007312 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04007313
Yan Zheng11833d62009-09-11 16:11:19 -04007314 caching_ctl = get_caching_control(cache);
7315 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007316 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04007317
Yan Zheng11833d62009-09-11 16:11:19 -04007318 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08007319 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04007320
7321 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04007322}
7323
7324static noinline int
7325wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
7326{
7327 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04007328 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007329
7330 caching_ctl = get_caching_control(cache);
7331 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007332 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007333
7334 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04007335 if (cache->cached == BTRFS_CACHE_ERROR)
7336 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04007337 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04007338 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04007339}
7340
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007341static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
7342 [BTRFS_RAID_RAID10] = "raid10",
7343 [BTRFS_RAID_RAID1] = "raid1",
7344 [BTRFS_RAID_DUP] = "dup",
7345 [BTRFS_RAID_RAID0] = "raid0",
7346 [BTRFS_RAID_SINGLE] = "single",
7347 [BTRFS_RAID_RAID5] = "raid5",
7348 [BTRFS_RAID_RAID6] = "raid6",
7349};
7350
Jeff Mahoney1b8e5df2013-11-20 16:50:23 -05007351static const char *get_raid_name(enum btrfs_raid_types type)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007352{
7353 if (type >= BTRFS_NR_RAID_TYPES)
7354 return NULL;
7355
7356 return btrfs_raid_type_names[type];
7357}
7358
Josef Bacik817d52f2009-07-13 21:29:25 -04007359enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05007360 LOOP_CACHING_NOWAIT = 0,
7361 LOOP_CACHING_WAIT = 1,
7362 LOOP_ALLOC_CHUNK = 2,
7363 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04007364};
7365
Miao Xiee570fd22014-06-19 10:42:50 +08007366static inline void
7367btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
7368 int delalloc)
7369{
7370 if (delalloc)
7371 down_read(&cache->data_rwsem);
7372}
7373
7374static inline void
7375btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
7376 int delalloc)
7377{
7378 btrfs_get_block_group(cache);
7379 if (delalloc)
7380 down_read(&cache->data_rwsem);
7381}
7382
7383static struct btrfs_block_group_cache *
7384btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
7385 struct btrfs_free_cluster *cluster,
7386 int delalloc)
7387{
Sudip Mukherjee89771cc2016-02-16 13:32:47 +05307388 struct btrfs_block_group_cache *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007389
Miao Xiee570fd22014-06-19 10:42:50 +08007390 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007391 while (1) {
7392 used_bg = cluster->block_group;
7393 if (!used_bg)
7394 return NULL;
7395
7396 if (used_bg == block_group)
7397 return used_bg;
7398
7399 btrfs_get_block_group(used_bg);
7400
7401 if (!delalloc)
7402 return used_bg;
7403
7404 if (down_read_trylock(&used_bg->data_rwsem))
7405 return used_bg;
7406
7407 spin_unlock(&cluster->refill_lock);
7408
Liu Boe321f8a2016-11-30 16:11:04 -08007409 /* We should only have one-level nested. */
7410 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007411
7412 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007413 if (used_bg == cluster->block_group)
7414 return used_bg;
7415
7416 up_read(&used_bg->data_rwsem);
7417 btrfs_put_block_group(used_bg);
7418 }
Miao Xiee570fd22014-06-19 10:42:50 +08007419}
7420
7421static inline void
7422btrfs_release_block_group(struct btrfs_block_group_cache *cache,
7423 int delalloc)
7424{
7425 if (delalloc)
7426 up_read(&cache->data_rwsem);
7427 btrfs_put_block_group(cache);
7428}
7429
Josef Bacik817d52f2009-07-13 21:29:25 -04007430/*
Chris Masonfec577f2007-02-26 10:40:21 -05007431 * walks the btree of allocated extents and find a hole of a given size.
7432 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08007433 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04007434 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08007435 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05007436 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08007437 *
7438 * If there is no suitable free space, we will record the max size of
7439 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05007440 */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05007441static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08007442 u64 ram_bytes, u64 num_bytes, u64 empty_size,
7443 u64 hint_byte, struct btrfs_key *ins,
7444 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007445{
Josef Bacik80eb2342008-10-29 14:49:05 -04007446 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007447 struct btrfs_root *root = fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007448 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04007449 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05007450 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08007451 u64 max_extent_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007452 u64 empty_cluster = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04007453 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007454 int loop = 0;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007455 int index = btrfs_bg_flags_to_raid_index(flags);
Josef Bacik0a243252009-09-11 16:11:20 -04007456 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007457 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04007458 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08007459 bool have_caching_bg = false;
chandan13a0db52015-11-02 13:59:46 +05307460 bool orig_have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007461 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05007462
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007463 WARN_ON(num_bytes < fs_info->sectorsize);
David Sterba962a2982014-06-04 18:41:45 +02007464 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04007465 ins->objectid = 0;
7466 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04007467
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007468 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05007469
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007470 space_info = __find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007471 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007472 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007473 return -ENOSPC;
7474 }
Josef Bacik2552d172009-04-03 10:14:19 -04007475
Josef Bacik67377732010-09-16 16:19:09 -04007476 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04007477 * If our free space is heavily fragmented we may not be able to make
7478 * big contiguous allocations, so instead of doing the expensive search
7479 * for free space, simply return ENOSPC with our max_extent_size so we
7480 * can go ahead and search for a more manageable chunk.
7481 *
7482 * If our max_extent_size is large enough for our allocation simply
7483 * disable clustering since we will likely not be able to find enough
7484 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04007485 */
Josef Bacik4f4db212015-09-29 11:40:47 -04007486 if (unlikely(space_info->max_extent_size)) {
7487 spin_lock(&space_info->lock);
7488 if (space_info->max_extent_size &&
7489 num_bytes > space_info->max_extent_size) {
7490 ins->offset = space_info->max_extent_size;
7491 spin_unlock(&space_info->lock);
7492 return -ENOSPC;
7493 } else if (space_info->max_extent_size) {
7494 use_cluster = false;
7495 }
7496 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007497 }
7498
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007499 last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04007500 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007501 spin_lock(&last_ptr->lock);
7502 if (last_ptr->block_group)
7503 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007504 if (last_ptr->fragmented) {
7505 /*
7506 * We still set window_start so we can keep track of the
7507 * last place we found an allocation to try and save
7508 * some time.
7509 */
7510 hint_byte = last_ptr->window_start;
7511 use_cluster = false;
7512 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007513 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007514 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007515
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007516 search_start = max(search_start, first_logical_byte(fs_info, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04007517 search_start = max(search_start, hint_byte);
Josef Bacik2552d172009-04-03 10:14:19 -04007518 if (search_start == hint_byte) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007519 block_group = btrfs_lookup_block_group(fs_info, search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04007520 /*
7521 * we don't want to use the block group if it doesn't match our
7522 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05007523 *
7524 * However if we are re-searching with an ideal block group
7525 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04007526 */
David Sterbab6919a52013-04-29 13:39:40 +00007527 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05007528 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04007529 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007530 if (list_empty(&block_group->list) ||
7531 block_group->ro) {
7532 /*
7533 * someone is removing this block group,
7534 * we can't jump into the have_block_group
7535 * target because our list pointers are not
7536 * valid
7537 */
7538 btrfs_put_block_group(block_group);
7539 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05007540 } else {
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007541 index = btrfs_bg_flags_to_raid_index(
7542 block_group->flags);
Miao Xiee570fd22014-06-19 10:42:50 +08007543 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04007544 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05007545 }
Josef Bacik2552d172009-04-03 10:14:19 -04007546 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007547 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04007548 }
Chris Mason42e70e72008-11-07 18:17:11 -05007549 }
Josef Bacik2552d172009-04-03 10:14:19 -04007550search:
Miao Xie60d2adb2011-09-09 17:34:35 +08007551 have_caching_bg = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007552 if (index == 0 || index == btrfs_bg_flags_to_raid_index(flags))
Josef Bacika5e681d2015-10-01 14:54:10 -04007553 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04007554 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007555 list_for_each_entry(block_group, &space_info->block_groups[index],
7556 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04007557 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04007558 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05007559
Jeff Mahoney14443932017-07-19 23:25:51 -04007560 /* If the block group is read-only, we can skip it entirely. */
7561 if (unlikely(block_group->ro))
7562 continue;
7563
Miao Xiee570fd22014-06-19 10:42:50 +08007564 btrfs_grab_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007565 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05007566
Chris Mason83a50de2010-12-13 15:06:46 -05007567 /*
7568 * this can happen if we end up cycling through all the
7569 * raid types, but we want to make sure we only allocate
7570 * for the proper type.
7571 */
David Sterbab6919a52013-04-29 13:39:40 +00007572 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05007573 u64 extra = BTRFS_BLOCK_GROUP_DUP |
7574 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007575 BTRFS_BLOCK_GROUP_RAID5 |
7576 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05007577 BTRFS_BLOCK_GROUP_RAID10;
7578
7579 /*
7580 * if they asked for extra copies and this block group
7581 * doesn't provide them, bail. This does allow us to
7582 * fill raid0 from raid1.
7583 */
David Sterbab6919a52013-04-29 13:39:40 +00007584 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05007585 goto loop;
7586 }
7587
Josef Bacik2552d172009-04-03 10:14:19 -04007588have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05007589 cached = block_group_cache_done(block_group);
7590 if (unlikely(!cached)) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007591 have_caching_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00007592 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04007593 BUG_ON(ret < 0);
7594 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05007595 }
7596
Josef Bacik36cce922013-08-05 11:15:21 -04007597 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
7598 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007599
Josef Bacik0a243252009-09-11 16:11:20 -04007600 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007601 * Ok we want to try and use the cluster allocator, so
7602 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04007603 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04007604 if (last_ptr && use_cluster) {
Miao Xie215a63d2014-01-15 20:00:56 +08007605 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05007606 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007607 /*
7608 * the refill lock keeps out other
7609 * people trying to start a new cluster
7610 */
Miao Xiee570fd22014-06-19 10:42:50 +08007611 used_block_group = btrfs_lock_cluster(block_group,
7612 last_ptr,
7613 delalloc);
7614 if (!used_block_group)
Chris Mason44fb5512009-06-04 15:34:51 -04007615 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04007616
Miao Xiee570fd22014-06-19 10:42:50 +08007617 if (used_block_group != block_group &&
7618 (used_block_group->ro ||
7619 !block_group_bits(used_block_group, flags)))
7620 goto release_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007621
7622 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007623 last_ptr,
7624 num_bytes,
7625 used_block_group->key.objectid,
7626 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007627 if (offset) {
7628 /* we have a block, we're done */
7629 spin_unlock(&last_ptr->refill_lock);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007630 trace_btrfs_reserve_extent_cluster(fs_info,
Miao Xie89d43462014-01-15 20:00:57 +08007631 used_block_group,
7632 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08007633 if (used_block_group != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08007634 btrfs_release_block_group(block_group,
7635 delalloc);
Miao Xie215a63d2014-01-15 20:00:56 +08007636 block_group = used_block_group;
7637 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007638 goto checks;
7639 }
7640
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007641 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007642release_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007643 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7644 * set up a new clusters, so lets just skip it
7645 * and let the allocator find whatever block
7646 * it can find. If we reach this point, we
7647 * will have tried the cluster allocator
7648 * plenty of times and not have found
7649 * anything, so we are likely way too
7650 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007651 * anything.
7652 *
7653 * However, if the cluster is taken from the
7654 * current block group, release the cluster
7655 * first, so that we stand a better chance of
7656 * succeeding in the unclustered
7657 * allocation. */
7658 if (loop >= LOOP_NO_EMPTY_SIZE &&
Miao Xiee570fd22014-06-19 10:42:50 +08007659 used_block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007660 spin_unlock(&last_ptr->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007661 btrfs_release_block_group(used_block_group,
7662 delalloc);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007663 goto unclustered_alloc;
7664 }
7665
Chris Masonfa9c0d792009-04-03 09:47:43 -04007666 /*
7667 * this cluster didn't work out, free it and
7668 * start over
7669 */
7670 btrfs_return_cluster_to_free_space(NULL, last_ptr);
7671
Miao Xiee570fd22014-06-19 10:42:50 +08007672 if (used_block_group != block_group)
7673 btrfs_release_block_group(used_block_group,
7674 delalloc);
7675refill_cluster:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007676 if (loop >= LOOP_NO_EMPTY_SIZE) {
7677 spin_unlock(&last_ptr->refill_lock);
7678 goto unclustered_alloc;
7679 }
7680
Chris Mason8de972b2013-01-04 15:39:43 -05007681 aligned_cluster = max_t(unsigned long,
7682 empty_cluster + empty_size,
7683 block_group->full_stripe_len);
7684
Chris Masonfa9c0d792009-04-03 09:47:43 -04007685 /* allocate a cluster in this block group */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007686 ret = btrfs_find_space_cluster(fs_info, block_group,
Josef Bacik00361582013-08-14 14:02:47 -04007687 last_ptr, search_start,
7688 num_bytes,
7689 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007690 if (ret == 0) {
7691 /*
7692 * now pull our allocation out of this
7693 * cluster
7694 */
7695 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007696 last_ptr,
7697 num_bytes,
7698 search_start,
7699 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007700 if (offset) {
7701 /* we found one, proceed */
7702 spin_unlock(&last_ptr->refill_lock);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007703 trace_btrfs_reserve_extent_cluster(fs_info,
Josef Bacik3f7de032011-11-10 08:29:20 -05007704 block_group, search_start,
7705 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007706 goto checks;
7707 }
Josef Bacik0a243252009-09-11 16:11:20 -04007708 } else if (!cached && loop > LOOP_CACHING_NOWAIT
7709 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007710 spin_unlock(&last_ptr->refill_lock);
7711
Josef Bacik0a243252009-09-11 16:11:20 -04007712 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007713 wait_block_group_cache_progress(block_group,
7714 num_bytes + empty_cluster + empty_size);
7715 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007716 }
Josef Bacik817d52f2009-07-13 21:29:25 -04007717
Chris Masonfa9c0d792009-04-03 09:47:43 -04007718 /*
7719 * at this point we either didn't find a cluster
7720 * or we weren't able to allocate a block from our
7721 * cluster. Free the cluster we've been trying
7722 * to use, and go to the next block group
7723 */
Josef Bacik0a243252009-09-11 16:11:20 -04007724 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007725 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04007726 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007727 }
7728
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007729unclustered_alloc:
Josef Bacikc759c4e2015-10-02 15:25:10 -04007730 /*
7731 * We are doing an unclustered alloc, set the fragmented flag so
7732 * we don't bother trying to setup a cluster again until we get
7733 * more space.
7734 */
7735 if (unlikely(last_ptr)) {
7736 spin_lock(&last_ptr->lock);
7737 last_ptr->fragmented = 1;
7738 spin_unlock(&last_ptr->lock);
7739 }
Liu Bo0c9b36e2017-02-13 15:42:30 -08007740 if (cached) {
7741 struct btrfs_free_space_ctl *ctl =
7742 block_group->free_space_ctl;
7743
7744 spin_lock(&ctl->tree_lock);
7745 if (ctl->free_space <
7746 num_bytes + empty_cluster + empty_size) {
7747 if (ctl->free_space > max_extent_size)
7748 max_extent_size = ctl->free_space;
7749 spin_unlock(&ctl->tree_lock);
7750 goto loop;
7751 }
7752 spin_unlock(&ctl->tree_lock);
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007753 }
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007754
Josef Bacik6226cb02009-04-03 10:14:18 -04007755 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08007756 num_bytes, empty_size,
7757 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007758 /*
7759 * If we didn't find a chunk, and we haven't failed on this
7760 * block group before, and this block group is in the middle of
7761 * caching and we are ok with waiting, then go ahead and wait
7762 * for progress to be made, and set failed_alloc to true.
7763 *
7764 * If failed_alloc is true then we've already waited on this
7765 * block group once and should move on to the next block group.
7766 */
7767 if (!offset && !failed_alloc && !cached &&
7768 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007769 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007770 num_bytes + empty_size);
7771 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007772 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007773 } else if (!offset) {
7774 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04007775 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007776checks:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007777 search_start = ALIGN(offset, fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04007778
Josef Bacik2552d172009-04-03 10:14:19 -04007779 /* move on to the next group */
7780 if (search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08007781 block_group->key.objectid + block_group->key.offset) {
7782 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04007783 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04007784 }
Josef Bacik80eb2342008-10-29 14:49:05 -04007785
Josef Bacik6226cb02009-04-03 10:14:18 -04007786 if (offset < search_start)
Miao Xie215a63d2014-01-15 20:00:56 +08007787 btrfs_add_free_space(block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04007788 search_start - offset);
7789 BUG_ON(offset > search_start);
7790
Wang Xiaoguang18513092016-07-25 15:51:40 +08007791 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
7792 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007793 if (ret == -EAGAIN) {
Miao Xie215a63d2014-01-15 20:00:56 +08007794 btrfs_add_free_space(block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007795 goto loop;
7796 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007797 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04007798
Josef Bacik2552d172009-04-03 10:14:19 -04007799 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007800 ins->objectid = search_start;
7801 ins->offset = num_bytes;
7802
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007803 trace_btrfs_reserve_extent(fs_info, block_group,
Josef Bacik3f7de032011-11-10 08:29:20 -05007804 search_start, num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08007805 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007806 break;
7807loop:
Josef Bacik0a243252009-09-11 16:11:20 -04007808 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007809 failed_alloc = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007810 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
7811 index);
Miao Xiee570fd22014-06-19 10:42:50 +08007812 btrfs_release_block_group(block_group, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04007813 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04007814 }
7815 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05007816
chandan13a0db52015-11-02 13:59:46 +05307817 if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg
7818 && !orig_have_caching_bg)
7819 orig_have_caching_bg = true;
7820
Miao Xie60d2adb2011-09-09 17:34:35 +08007821 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
7822 goto search;
7823
Yan, Zhengb742bb82010-05-16 10:46:24 -04007824 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
7825 goto search;
7826
Josef Bacik285ff5a2012-01-13 15:27:45 -05007827 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05007828 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
7829 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04007830 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
7831 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
7832 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
7833 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04007834 */
Josef Bacik723bda22011-05-27 16:11:38 -04007835 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04007836 index = 0;
Josef Bacika5e681d2015-10-01 14:54:10 -04007837 if (loop == LOOP_CACHING_NOWAIT) {
7838 /*
7839 * We want to skip the LOOP_CACHING_WAIT step if we
Nicholas D Steeves01327612016-05-19 21:18:45 -04007840 * don't have any uncached bgs and we've already done a
Josef Bacika5e681d2015-10-01 14:54:10 -04007841 * full search through.
7842 */
chandan13a0db52015-11-02 13:59:46 +05307843 if (orig_have_caching_bg || !full_search)
Josef Bacika5e681d2015-10-01 14:54:10 -04007844 loop = LOOP_CACHING_WAIT;
7845 else
7846 loop = LOOP_ALLOC_CHUNK;
7847 } else {
7848 loop++;
7849 }
7850
Josef Bacik817d52f2009-07-13 21:29:25 -04007851 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04007852 struct btrfs_trans_handle *trans;
Wang Shilongf017f152014-03-13 13:19:47 +08007853 int exist = 0;
Josef Bacik00361582013-08-14 14:02:47 -04007854
Wang Shilongf017f152014-03-13 13:19:47 +08007855 trans = current->journal_info;
7856 if (trans)
7857 exist = 1;
7858 else
7859 trans = btrfs_join_transaction(root);
7860
Josef Bacik00361582013-08-14 14:02:47 -04007861 if (IS_ERR(trans)) {
7862 ret = PTR_ERR(trans);
7863 goto out;
7864 }
7865
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007866 ret = do_chunk_alloc(trans, fs_info, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04007867 CHUNK_ALLOC_FORCE);
Josef Bacika5e681d2015-10-01 14:54:10 -04007868
7869 /*
7870 * If we can't allocate a new chunk we've already looped
7871 * through at least once, move on to the NO_EMPTY_SIZE
7872 * case.
7873 */
7874 if (ret == -ENOSPC)
7875 loop = LOOP_NO_EMPTY_SIZE;
7876
Josef Bacikea658ba2012-09-11 16:57:25 -04007877 /*
7878 * Do not bail out on ENOSPC since we
7879 * can do more things.
7880 */
Josef Bacik00361582013-08-14 14:02:47 -04007881 if (ret < 0 && ret != -ENOSPC)
Jeff Mahoney66642832016-06-10 18:19:25 -04007882 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -04007883 else
7884 ret = 0;
Wang Shilongf017f152014-03-13 13:19:47 +08007885 if (!exist)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007886 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04007887 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04007888 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04007889 }
7890
7891 if (loop == LOOP_NO_EMPTY_SIZE) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007892 /*
7893 * Don't loop again if we already have no empty_size and
7894 * no empty_cluster.
7895 */
7896 if (empty_size == 0 &&
7897 empty_cluster == 0) {
7898 ret = -ENOSPC;
7899 goto out;
7900 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007901 empty_size = 0;
7902 empty_cluster = 0;
7903 }
Chris Mason42e70e72008-11-07 18:17:11 -05007904
Josef Bacik723bda22011-05-27 16:11:38 -04007905 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04007906 } else if (!ins->objectid) {
7907 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04007908 } else if (ins->objectid) {
Josef Bacikc759c4e2015-10-02 15:25:10 -04007909 if (!use_cluster && last_ptr) {
7910 spin_lock(&last_ptr->lock);
7911 last_ptr->window_start = ins->objectid;
7912 spin_unlock(&last_ptr->lock);
7913 }
Josef Bacik2552d172009-04-03 10:14:19 -04007914 ret = 0;
7915 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007916out:
Josef Bacik4f4db212015-09-29 11:40:47 -04007917 if (ret == -ENOSPC) {
7918 spin_lock(&space_info->lock);
7919 space_info->max_extent_size = max_extent_size;
7920 spin_unlock(&space_info->lock);
Miao Xiea4820392013-09-09 13:19:42 +08007921 ins->offset = max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04007922 }
Chris Mason0f70abe2007-02-28 16:46:22 -05007923 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05007924}
Chris Masonec44a352008-04-28 15:29:52 -04007925
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007926static void dump_space_info(struct btrfs_fs_info *fs_info,
7927 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007928 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04007929{
7930 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04007931 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007932
Josef Bacik9ed74f22009-09-11 16:12:44 -04007933 spin_lock(&info->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007934 btrfs_info(fs_info, "space_info %llu has %llu free, is %sfull",
7935 info->flags,
Liu Bo41361352017-02-13 15:42:21 -08007936 info->total_bytes - btrfs_space_info_used(info, true),
7937 info->full ? "" : "not ");
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007938 btrfs_info(fs_info,
7939 "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu",
7940 info->total_bytes, info->bytes_used, info->bytes_pinned,
7941 info->bytes_reserved, info->bytes_may_use,
7942 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007943 spin_unlock(&info->lock);
7944
7945 if (!dump_block_groups)
7946 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007947
Josef Bacik80eb2342008-10-29 14:49:05 -04007948 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007949again:
7950 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04007951 spin_lock(&cache->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007952 btrfs_info(fs_info,
7953 "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s",
7954 cache->key.objectid, cache->key.offset,
7955 btrfs_block_group_used(&cache->item), cache->pinned,
7956 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04007957 btrfs_dump_free_space(cache, bytes);
7958 spin_unlock(&cache->lock);
7959 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007960 if (++index < BTRFS_NR_RAID_TYPES)
7961 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04007962 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007963}
Zheng Yane8569812008-09-26 10:05:48 -04007964
Nikolay Borisov6f47c702018-03-13 12:22:32 +02007965/*
7966 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
7967 * hole that is at least as big as @num_bytes.
7968 *
7969 * @root - The root that will contain this extent
7970 *
7971 * @ram_bytes - The amount of space in ram that @num_bytes take. This
7972 * is used for accounting purposes. This value differs
7973 * from @num_bytes only in the case of compressed extents.
7974 *
7975 * @num_bytes - Number of bytes to allocate on-disk.
7976 *
7977 * @min_alloc_size - Indicates the minimum amount of space that the
7978 * allocator should try to satisfy. In some cases
7979 * @num_bytes may be larger than what is required and if
7980 * the filesystem is fragmented then allocation fails.
7981 * However, the presence of @min_alloc_size gives a
7982 * chance to try and satisfy the smaller allocation.
7983 *
7984 * @empty_size - A hint that you plan on doing more COW. This is the
7985 * size in bytes the allocator should try to find free
7986 * next to the block it returns. This is just a hint and
7987 * may be ignored by the allocator.
7988 *
7989 * @hint_byte - Hint to the allocator to start searching above the byte
7990 * address passed. It might be ignored.
7991 *
7992 * @ins - This key is modified to record the found hole. It will
7993 * have the following values:
7994 * ins->objectid == start position
7995 * ins->flags = BTRFS_EXTENT_ITEM_KEY
7996 * ins->offset == the size of the hole.
7997 *
7998 * @is_data - Boolean flag indicating whether an extent is
7999 * allocated for data (true) or metadata (false)
8000 *
8001 * @delalloc - Boolean flag indicating whether this allocation is for
8002 * delalloc or not. If 'true' data_rwsem of block groups
8003 * is going to be acquired.
8004 *
8005 *
8006 * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
8007 * case -ENOSPC is returned then @ins->offset will contain the size of the
8008 * largest available hole the allocator managed to find.
8009 */
Wang Xiaoguang18513092016-07-25 15:51:40 +08008010int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04008011 u64 num_bytes, u64 min_alloc_size,
8012 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08008013 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05008014{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008015 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04008016 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00008017 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05008018 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04008019
Jeff Mahoney1b868262017-05-17 11:38:35 -04008020 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04008021again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008022 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05008023 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08008024 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008025 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008026 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008027 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08008028 if (!final_tried && ins->offset) {
8029 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04008030 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008031 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05008032 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08008033 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05008034 if (num_bytes == min_alloc_size)
8035 final_tried = true;
8036 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008037 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05008038 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008039
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008040 sinfo = __find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008041 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008042 "allocation failed flags %llu, wanted %llu",
8043 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01008044 if (sinfo)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008045 dump_space_info(fs_info, sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05008046 }
Chris Mason925baed2008-06-25 16:01:30 -04008047 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008048
8049 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008050}
8051
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008052static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008053 u64 start, u64 len,
8054 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04008055{
Josef Bacik0f9dd462008-09-23 13:14:11 -04008056 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05008057 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008058
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008059 cache = btrfs_lookup_block_group(fs_info, start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008060 if (!cache) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008061 btrfs_err(fs_info, "Unable to find block group for %llu",
8062 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008063 return -ENOSPC;
8064 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05008065
Chris Masone688b7252011-10-31 20:52:39 -04008066 if (pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008067 pin_down_extent(fs_info, cache, start, len, 1);
Chris Masone688b7252011-10-31 20:52:39 -04008068 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008069 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008070 ret = btrfs_discard_extent(fs_info, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04008071 btrfs_add_free_space(cache, start, len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08008072 btrfs_free_reserved_bytes(cache, len, delalloc);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008073 trace_btrfs_reserved_extent_free(fs_info, start, len);
Chris Masone688b7252011-10-31 20:52:39 -04008074 }
Dongsheng Yang31193212014-12-12 16:44:35 +08008075
Chris Masonfa9c0d792009-04-03 09:47:43 -04008076 btrfs_put_block_group(cache);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008077 return ret;
8078}
8079
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008080int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008081 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04008082{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008083 return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04008084}
8085
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008086int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04008087 u64 start, u64 len)
8088{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008089 return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04008090}
8091
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008092static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008093 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008094 u64 parent, u64 root_objectid,
8095 u64 flags, u64 owner, u64 offset,
8096 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008097{
8098 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008099 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008100 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008101 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008102 struct extent_buffer *leaf;
8103 int type;
8104 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04008105
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008106 if (parent > 0)
8107 type = BTRFS_SHARED_DATA_REF_KEY;
8108 else
8109 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04008110
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008111 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05008112
8113 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00008114 if (!path)
8115 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05008116
Chris Masonb9473432009-03-13 11:00:37 -04008117 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008118 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8119 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008120 if (ret) {
8121 btrfs_free_path(path);
8122 return ret;
8123 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008124
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008125 leaf = path->nodes[0];
8126 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05008127 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008128 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
8129 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8130 btrfs_set_extent_flags(leaf, extent_item,
8131 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05008132
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008133 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
8134 btrfs_set_extent_inline_ref_type(leaf, iref, type);
8135 if (parent > 0) {
8136 struct btrfs_shared_data_ref *ref;
8137 ref = (struct btrfs_shared_data_ref *)(iref + 1);
8138 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8139 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
8140 } else {
8141 struct btrfs_extent_data_ref *ref;
8142 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
8143 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
8144 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
8145 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
8146 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
8147 }
Chris Mason47e4bb92008-02-01 14:51:59 -05008148
8149 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05008150 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04008151
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008152 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
8153 ins->offset);
8154 if (ret)
8155 return ret;
8156
Jeff Mahoney6202df62016-06-22 18:54:22 -04008157 ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008158 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008159 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008160 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05008161 BUG();
8162 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008163 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008164 return ret;
8165}
8166
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008167static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008168 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008169 u64 parent, u64 root_objectid,
8170 u64 flags, struct btrfs_disk_key *key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01008171 int level, struct btrfs_key *ins)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008172{
8173 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008174 struct btrfs_extent_item *extent_item;
8175 struct btrfs_tree_block_info *block_info;
8176 struct btrfs_extent_inline_ref *iref;
8177 struct btrfs_path *path;
8178 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05008179 u32 size = sizeof(*extent_item) + sizeof(*iref);
Josef Bacikfcebe452014-05-13 17:30:47 -07008180 u64 num_bytes = ins->offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008181 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05008182
8183 if (!skinny_metadata)
8184 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008185
8186 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04008187 if (!path) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008188 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008189 fs_info->nodesize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07008190 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04008191 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008192
8193 path->leave_spinning = 1;
8194 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8195 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008196 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07008197 btrfs_free_path(path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008198 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008199 fs_info->nodesize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008200 return ret;
8201 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008202
8203 leaf = path->nodes[0];
8204 extent_item = btrfs_item_ptr(leaf, path->slots[0],
8205 struct btrfs_extent_item);
8206 btrfs_set_extent_refs(leaf, extent_item, 1);
8207 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8208 btrfs_set_extent_flags(leaf, extent_item,
8209 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008210
Josef Bacik3173a182013-03-07 14:22:04 -05008211 if (skinny_metadata) {
8212 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008213 num_bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05008214 } else {
8215 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
8216 btrfs_set_tree_block_key(leaf, block_info, key);
8217 btrfs_set_tree_block_level(leaf, block_info, level);
8218 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
8219 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008220
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008221 if (parent > 0) {
8222 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
8223 btrfs_set_extent_inline_ref_type(leaf, iref,
8224 BTRFS_SHARED_BLOCK_REF_KEY);
8225 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8226 } else {
8227 btrfs_set_extent_inline_ref_type(leaf, iref,
8228 BTRFS_TREE_BLOCK_REF_KEY);
8229 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
8230 }
8231
8232 btrfs_mark_buffer_dirty(leaf);
8233 btrfs_free_path(path);
8234
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008235 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
8236 num_bytes);
8237 if (ret)
8238 return ret;
8239
Jeff Mahoney6202df62016-06-22 18:54:22 -04008240 ret = update_block_group(trans, fs_info, ins->objectid,
8241 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008242 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008243 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008244 ins->objectid, ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008245 BUG();
8246 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04008247
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008248 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008249 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008250 return ret;
8251}
8252
8253int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008254 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08008255 u64 offset, u64 ram_bytes,
8256 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008257{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008258 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008259 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04008260
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008261 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04008262
Josef Bacikfd708b82017-09-29 15:43:50 -04008263 btrfs_ref_tree_mod(root, ins->objectid, ins->offset, 0,
8264 root->root_key.objectid, owner, offset,
8265 BTRFS_ADD_DELAYED_EXTENT);
8266
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008267 ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008268 ins->offset, 0,
8269 root->root_key.objectid, owner,
Omar Sandoval7be07912017-06-06 16:45:30 -07008270 offset, ram_bytes,
8271 BTRFS_ADD_DELAYED_EXTENT, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008272 return ret;
8273}
Chris Masone02119d2008-09-05 16:13:11 -04008274
8275/*
8276 * this is used by the tree logging recovery code. It records that
8277 * an extent has been allocated and makes sure to clear the free
8278 * space cache bits as well
8279 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008280int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008281 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008282 u64 root_objectid, u64 owner, u64 offset,
8283 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04008284{
8285 int ret;
8286 struct btrfs_block_group_cache *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008287 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008288
8289 /*
8290 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04008291 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008292 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008293 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008294 ret = __exclude_logged_extent(fs_info, ins->objectid,
8295 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008296 if (ret)
8297 return ret;
8298 }
Chris Masone02119d2008-09-05 16:13:11 -04008299
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008300 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008301 if (!block_group)
8302 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04008303
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008304 space_info = block_group->space_info;
8305 spin_lock(&space_info->lock);
8306 spin_lock(&block_group->lock);
8307 space_info->bytes_reserved += ins->offset;
8308 block_group->reserved += ins->offset;
8309 spin_unlock(&block_group->lock);
8310 spin_unlock(&space_info->lock);
8311
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008312 ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008313 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04008314 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04008315 return ret;
8316}
8317
Eric Sandeen48a3b632013-04-25 20:41:01 +00008318static struct extent_buffer *
8319btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
David Sterbafe864572014-06-15 02:28:42 +02008320 u64 bytenr, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04008321{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008322 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04008323 struct extent_buffer *buf;
8324
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008325 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008326 if (IS_ERR(buf))
8327 return buf;
8328
Chris Mason65b51a02008-08-01 15:11:20 -04008329 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04008330 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04008331 btrfs_tree_lock(buf);
David Sterba7c302b42017-02-10 18:47:57 +01008332 clean_tree_block(fs_info, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05008333 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008334
8335 btrfs_set_lock_blocking(buf);
David Sterba4db8c522015-12-03 13:06:46 +01008336 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008337
Chris Masond0c803c2008-09-11 16:17:57 -04008338 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01008339 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008340 /*
8341 * we allow two log transactions at a time, use different
8342 * EXENT bit to differentiate dirty pages.
8343 */
Filipe Manana656f30d2014-09-26 12:25:56 +01008344 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008345 set_extent_dirty(&root->dirty_log_pages, buf->start,
8346 buf->start + buf->len - 1, GFP_NOFS);
8347 else
8348 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02008349 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04008350 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01008351 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04008352 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
8353 buf->start + buf->len - 1, GFP_NOFS);
8354 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04008355 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05008356 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04008357 return buf;
8358}
8359
Yan, Zhengf0486c62010-05-16 10:46:25 -04008360static struct btrfs_block_rsv *
8361use_block_rsv(struct btrfs_trans_handle *trans,
8362 struct btrfs_root *root, u32 blocksize)
8363{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008364 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008365 struct btrfs_block_rsv *block_rsv;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008366 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008367 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00008368 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008369
8370 block_rsv = get_block_rsv(trans, root);
8371
Miao Xieb586b322013-05-13 13:55:10 +00008372 if (unlikely(block_rsv->size == 0))
8373 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00008374again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04008375 ret = block_rsv_use_bytes(block_rsv, blocksize);
8376 if (!ret)
8377 return block_rsv;
8378
Miao Xieb586b322013-05-13 13:55:10 +00008379 if (block_rsv->failfast)
8380 return ERR_PTR(ret);
8381
Miao Xied88033d2013-05-13 13:55:12 +00008382 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
8383 global_updated = true;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008384 update_global_block_rsv(fs_info);
Miao Xied88033d2013-05-13 13:55:12 +00008385 goto again;
8386 }
8387
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008388 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xieb586b322013-05-13 13:55:10 +00008389 static DEFINE_RATELIMIT_STATE(_rs,
8390 DEFAULT_RATELIMIT_INTERVAL * 10,
8391 /*DEFAULT_RATELIMIT_BURST*/ 1);
8392 if (__ratelimit(&_rs))
8393 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05008394 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00008395 }
8396try_reserve:
8397 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
8398 BTRFS_RESERVE_NO_FLUSH);
8399 if (!ret)
8400 return block_rsv;
8401 /*
8402 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00008403 * the global reserve if its space type is the same as the global
8404 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00008405 */
Miao Xie5881cfc2013-05-13 13:55:11 +00008406 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
8407 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00008408 ret = block_rsv_use_bytes(global_rsv, blocksize);
8409 if (!ret)
8410 return global_rsv;
8411 }
8412 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008413}
8414
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008415static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
8416 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008417{
8418 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008419 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008420}
8421
Chris Masonfec577f2007-02-26 10:40:21 -05008422/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04008423 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08008424 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05008425 */
David Sterba4d75f8a2014-06-15 01:54:12 +02008426struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08008427 struct btrfs_root *root,
8428 u64 parent, u64 root_objectid,
8429 const struct btrfs_disk_key *key,
8430 int level, u64 hint,
8431 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05008432{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008433 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04008434 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008435 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04008436 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008437 struct btrfs_delayed_extent_op *extent_op;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008438 u64 flags = 0;
8439 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008440 u32 blocksize = fs_info->nodesize;
8441 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008442
David Sterba05653ef2016-07-15 15:23:37 +02008443#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008444 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008445 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
David Sterbafe864572014-06-15 02:28:42 +02008446 level);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008447 if (!IS_ERR(buf))
8448 root->alloc_bytenr += blocksize;
8449 return buf;
8450 }
David Sterba05653ef2016-07-15 15:23:37 +02008451#endif
David Sterbafccb84c2014-09-29 23:53:21 +02008452
Yan, Zhengf0486c62010-05-16 10:46:25 -04008453 block_rsv = use_block_rsv(trans, root, blocksize);
8454 if (IS_ERR(block_rsv))
8455 return ERR_CAST(block_rsv);
8456
Wang Xiaoguang18513092016-07-25 15:51:40 +08008457 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08008458 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008459 if (ret)
8460 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05008461
David Sterbafe864572014-06-15 02:28:42 +02008462 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
Omar Sandoval67b78592015-02-24 02:47:04 -08008463 if (IS_ERR(buf)) {
8464 ret = PTR_ERR(buf);
8465 goto out_free_reserved;
8466 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008467
8468 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
8469 if (parent == 0)
8470 parent = ins.objectid;
8471 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8472 } else
8473 BUG_ON(parent > 0);
8474
8475 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00008476 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08008477 if (!extent_op) {
8478 ret = -ENOMEM;
8479 goto out_free_buf;
8480 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008481 if (key)
8482 memcpy(&extent_op->key, key, sizeof(extent_op->key));
8483 else
8484 memset(&extent_op->key, 0, sizeof(extent_op->key));
8485 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01008486 extent_op->update_key = skinny_metadata ? false : true;
8487 extent_op->update_flags = true;
8488 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04008489 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008490
Josef Bacikfd708b82017-09-29 15:43:50 -04008491 btrfs_ref_tree_mod(root, ins.objectid, ins.offset, parent,
8492 root_objectid, level, 0,
8493 BTRFS_ADD_DELAYED_EXTENT);
Omar Sandoval7be07912017-06-06 16:45:30 -07008494 ret = btrfs_add_delayed_tree_ref(fs_info, trans, ins.objectid,
8495 ins.offset, parent,
8496 root_objectid, level,
Omar Sandoval67b78592015-02-24 02:47:04 -08008497 BTRFS_ADD_DELAYED_EXTENT,
Omar Sandoval7be07912017-06-06 16:45:30 -07008498 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08008499 if (ret)
8500 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008501 }
Chris Masonfec577f2007-02-26 10:40:21 -05008502 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008503
8504out_free_delayed:
8505 btrfs_free_delayed_extent_op(extent_op);
8506out_free_buf:
8507 free_extent_buffer(buf);
8508out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008509 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008510out_unuse:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008511 unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08008512 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05008513}
Chris Masona28ec192007-03-06 20:08:01 -05008514
Yan Zheng2c47e6052009-06-27 21:07:35 -04008515struct walk_control {
8516 u64 refs[BTRFS_MAX_LEVEL];
8517 u64 flags[BTRFS_MAX_LEVEL];
8518 struct btrfs_key update_progress;
8519 int stage;
8520 int level;
8521 int shared_level;
8522 int update_ref;
8523 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008524 int reada_slot;
8525 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008526 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008527};
8528
8529#define DROP_REFERENCE 1
8530#define UPDATE_BACKREF 2
8531
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008532static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8533 struct btrfs_root *root,
8534 struct walk_control *wc,
8535 struct btrfs_path *path)
8536{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008537 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008538 u64 bytenr;
8539 u64 generation;
8540 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008541 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008542 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008543 struct btrfs_key key;
8544 struct extent_buffer *eb;
8545 int ret;
8546 int slot;
8547 int nread = 0;
8548
8549 if (path->slots[wc->level] < wc->reada_slot) {
8550 wc->reada_count = wc->reada_count * 2 / 3;
8551 wc->reada_count = max(wc->reada_count, 2);
8552 } else {
8553 wc->reada_count = wc->reada_count * 3 / 2;
8554 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008555 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008556 }
8557
8558 eb = path->nodes[wc->level];
8559 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008560
8561 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
8562 if (nread >= wc->reada_count)
8563 break;
8564
8565 cond_resched();
8566 bytenr = btrfs_node_blockptr(eb, slot);
8567 generation = btrfs_node_ptr_generation(eb, slot);
8568
8569 if (slot == path->slots[wc->level])
8570 goto reada;
8571
8572 if (wc->stage == UPDATE_BACKREF &&
8573 generation <= root->root_key.offset)
8574 continue;
8575
Yan, Zheng94fcca92009-10-09 09:25:16 -04008576 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008577 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05008578 wc->level - 1, 1, &refs,
8579 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008580 /* We don't care about errors in readahead. */
8581 if (ret < 0)
8582 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008583 BUG_ON(refs == 0);
8584
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008585 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008586 if (refs == 1)
8587 goto reada;
8588
Yan, Zheng94fcca92009-10-09 09:25:16 -04008589 if (wc->level == 1 &&
8590 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8591 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008592 if (!wc->update_ref ||
8593 generation <= root->root_key.offset)
8594 continue;
8595 btrfs_node_key_to_cpu(eb, &key, slot);
8596 ret = btrfs_comp_cpu_keys(&key,
8597 &wc->update_progress);
8598 if (ret < 0)
8599 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008600 } else {
8601 if (wc->level == 1 &&
8602 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8603 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008604 }
8605reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008606 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008607 nread++;
8608 }
8609 wc->reada_slot = slot;
8610}
8611
Chris Mason9aca1d52007-03-13 11:09:37 -04008612/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008613 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008614 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04008615 * when wc->stage == UPDATE_BACKREF, this function updates
8616 * back refs for pointers in the block.
8617 *
8618 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04008619 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008620static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8621 struct btrfs_root *root,
8622 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008623 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04008624{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008625 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008626 int level = wc->level;
8627 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04008628 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8629 int ret;
8630
8631 if (wc->stage == UPDATE_BACKREF &&
8632 btrfs_header_owner(eb) != root->root_key.objectid)
8633 return 1;
8634
8635 /*
8636 * when reference count of tree block is 1, it won't increase
8637 * again. once full backref flag is set, we never clear it.
8638 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04008639 if (lookup_info &&
8640 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
8641 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008642 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008643 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008644 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008645 &wc->refs[level],
8646 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008647 BUG_ON(ret == -ENOMEM);
8648 if (ret)
8649 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008650 BUG_ON(wc->refs[level] == 0);
8651 }
8652
Yan Zheng2c47e6052009-06-27 21:07:35 -04008653 if (wc->stage == DROP_REFERENCE) {
8654 if (wc->refs[level] > 1)
8655 return 1;
8656
8657 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04008658 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008659 path->locks[level] = 0;
8660 }
8661 return 0;
8662 }
8663
8664 /* wc->stage == UPDATE_BACKREF */
8665 if (!(wc->flags[level] & flag)) {
8666 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07008667 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008668 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07008669 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008670 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008671 ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04008672 eb->len, flag,
8673 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008674 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008675 wc->flags[level] |= flag;
8676 }
8677
8678 /*
8679 * the block is shared by multiple trees, so it's not good to
8680 * keep the tree lock
8681 */
8682 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04008683 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008684 path->locks[level] = 0;
8685 }
8686 return 0;
8687}
8688
8689/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008690 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008691 *
8692 * when wc->stage == DROP_REFERENCE, this function checks
8693 * reference count of the block pointed to. if the block
8694 * is shared and we need update back refs for the subtree
8695 * rooted at the block, this function changes wc->stage to
8696 * UPDATE_BACKREF. if the block is shared and there is no
8697 * need to update back, this function drops the reference
8698 * to the block.
8699 *
8700 * NOTE: return value 1 means we should stop walking down.
8701 */
8702static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8703 struct btrfs_root *root,
8704 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008705 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008706{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008707 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008708 u64 bytenr;
8709 u64 generation;
8710 u64 parent;
8711 u32 blocksize;
8712 struct btrfs_key key;
8713 struct extent_buffer *next;
8714 int level = wc->level;
8715 int reada = 0;
8716 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07008717 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008718
8719 generation = btrfs_node_ptr_generation(path->nodes[level],
8720 path->slots[level]);
8721 /*
8722 * if the lower level block was created before the snapshot
8723 * was created, we know there is no need to update back refs
8724 * for the subtree
8725 */
8726 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04008727 generation <= root->root_key.offset) {
8728 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008729 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008730 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008731
8732 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008733 blocksize = fs_info->nodesize;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008734
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008735 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008736 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008737 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008738 if (IS_ERR(next))
8739 return PTR_ERR(next);
8740
Josef Bacikb2aaaa32013-07-05 17:05:38 -04008741 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
8742 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008743 reada = 1;
8744 }
8745 btrfs_tree_lock(next);
8746 btrfs_set_lock_blocking(next);
8747
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008748 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008749 &wc->refs[level - 1],
8750 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02008751 if (ret < 0)
8752 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008753
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008754 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008755 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02008756 ret = -EIO;
8757 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008758 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008759 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008760
Yan, Zheng94fcca92009-10-09 09:25:16 -04008761 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008762 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07008763 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008764 if (level == 1 &&
8765 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8766 goto skip;
8767
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008768 if (!wc->update_ref ||
8769 generation <= root->root_key.offset)
8770 goto skip;
8771
8772 btrfs_node_key_to_cpu(path->nodes[level], &key,
8773 path->slots[level]);
8774 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
8775 if (ret < 0)
8776 goto skip;
8777
8778 wc->stage = UPDATE_BACKREF;
8779 wc->shared_level = level - 1;
8780 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008781 } else {
8782 if (level == 1 &&
8783 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8784 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008785 }
8786
Chris Masonb9fab912012-05-06 07:23:47 -04008787 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008788 btrfs_tree_unlock(next);
8789 free_extent_buffer(next);
8790 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008791 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008792 }
8793
8794 if (!next) {
8795 if (reada && level == 1)
8796 reada_walk_down(trans, root, wc, path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008797 next = read_tree_block(fs_info, bytenr, generation);
Liu Bo64c043d2015-05-25 17:30:15 +08008798 if (IS_ERR(next)) {
8799 return PTR_ERR(next);
8800 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04008801 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008802 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04008803 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008804 btrfs_tree_lock(next);
8805 btrfs_set_lock_blocking(next);
8806 }
8807
8808 level--;
Josef Bacik48672682016-09-23 13:23:28 +02008809 ASSERT(level == btrfs_header_level(next));
8810 if (level != btrfs_header_level(next)) {
8811 btrfs_err(root->fs_info, "mismatched level");
8812 ret = -EIO;
8813 goto out_unlock;
8814 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008815 path->nodes[level] = next;
8816 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008817 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008818 wc->level = level;
8819 if (wc->level == 1)
8820 wc->reada_slot = 0;
8821 return 0;
8822skip:
8823 wc->refs[level - 1] = 0;
8824 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008825 if (wc->stage == DROP_REFERENCE) {
8826 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
8827 parent = path->nodes[level]->start;
8828 } else {
Josef Bacik48672682016-09-23 13:23:28 +02008829 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04008830 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02008831 if (root->root_key.objectid !=
8832 btrfs_header_owner(path->nodes[level])) {
8833 btrfs_err(root->fs_info,
8834 "mismatched block owner");
8835 ret = -EIO;
8836 goto out_unlock;
8837 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008838 parent = 0;
8839 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008840
Mark Fasheh11526512014-07-17 12:39:01 -07008841 if (need_account) {
Qu Wenruo33d1f052016-10-18 09:31:28 +08008842 ret = btrfs_qgroup_trace_subtree(trans, root, next,
8843 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07008844 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008845 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008846 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
8847 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008848 }
8849 }
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008850 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008851 parent, root->root_key.objectid,
8852 level - 1, 0);
Josef Bacik48672682016-09-23 13:23:28 +02008853 if (ret)
8854 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008855 }
Josef Bacik48672682016-09-23 13:23:28 +02008856
8857 *lookup_info = 1;
8858 ret = 1;
8859
8860out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008861 btrfs_tree_unlock(next);
8862 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02008863
8864 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008865}
8866
8867/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008868 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008869 *
8870 * when wc->stage == DROP_REFERENCE, this function drops
8871 * reference count on the block.
8872 *
8873 * when wc->stage == UPDATE_BACKREF, this function changes
8874 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8875 * to UPDATE_BACKREF previously while processing the block.
8876 *
8877 * NOTE: return value 1 means we should stop walking up.
8878 */
8879static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8880 struct btrfs_root *root,
8881 struct btrfs_path *path,
8882 struct walk_control *wc)
8883{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008884 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008885 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008886 int level = wc->level;
8887 struct extent_buffer *eb = path->nodes[level];
8888 u64 parent = 0;
8889
8890 if (wc->stage == UPDATE_BACKREF) {
8891 BUG_ON(wc->shared_level < level);
8892 if (level < wc->shared_level)
8893 goto out;
8894
Yan Zheng2c47e6052009-06-27 21:07:35 -04008895 ret = find_next_key(path, level + 1, &wc->update_progress);
8896 if (ret > 0)
8897 wc->update_ref = 0;
8898
8899 wc->stage = DROP_REFERENCE;
8900 wc->shared_level = -1;
8901 path->slots[level] = 0;
8902
8903 /*
8904 * check reference count again if the block isn't locked.
8905 * we should start walking down the tree again if reference
8906 * count is one.
8907 */
8908 if (!path->locks[level]) {
8909 BUG_ON(level == 0);
8910 btrfs_tree_lock(eb);
8911 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008912 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008913
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008914 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008915 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008916 &wc->refs[level],
8917 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008918 if (ret < 0) {
8919 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008920 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008921 return ret;
8922 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008923 BUG_ON(wc->refs[level] == 0);
8924 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04008925 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008926 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008927 return 1;
8928 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008929 }
8930 }
8931
8932 /* wc->stage == DROP_REFERENCE */
8933 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
8934
8935 if (wc->refs[level] == 1) {
8936 if (level == 0) {
8937 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07008938 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008939 else
Josef Bacike339a6b2014-07-02 10:54:25 -07008940 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008941 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008942 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb);
Mark Fasheh11526512014-07-17 12:39:01 -07008943 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008944 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008945 "error %d accounting leaf items. Quota is out of sync, rescan required.",
8946 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008947 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008948 }
8949 /* make block locked assertion in clean_tree_block happy */
8950 if (!path->locks[level] &&
8951 btrfs_header_generation(eb) == trans->transid) {
8952 btrfs_tree_lock(eb);
8953 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008954 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008955 }
David Sterba7c302b42017-02-10 18:47:57 +01008956 clean_tree_block(fs_info, eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008957 }
8958
8959 if (eb == root->node) {
8960 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8961 parent = eb->start;
8962 else
8963 BUG_ON(root->root_key.objectid !=
8964 btrfs_header_owner(eb));
8965 } else {
8966 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8967 parent = path->nodes[level + 1]->start;
8968 else
8969 BUG_ON(root->root_key.objectid !=
8970 btrfs_header_owner(path->nodes[level + 1]));
8971 }
8972
Jan Schmidt5581a512012-05-16 17:04:52 +02008973 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008974out:
8975 wc->refs[level] = 0;
8976 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008977 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008978}
8979
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008980static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
8981 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008982 struct btrfs_path *path,
8983 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008984{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008985 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008986 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008987 int ret;
8988
Yan Zheng2c47e6052009-06-27 21:07:35 -04008989 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04008990 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008991 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008992 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008993
Yan Zheng2c47e6052009-06-27 21:07:35 -04008994 if (level == 0)
8995 break;
8996
Yan, Zheng7a7965f2010-02-01 02:41:17 +00008997 if (path->slots[level] >=
8998 btrfs_header_nritems(path->nodes[level]))
8999 break;
9000
Yan, Zheng94fcca92009-10-09 09:25:16 -04009001 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009002 if (ret > 0) {
9003 path->slots[level]++;
9004 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00009005 } else if (ret < 0)
9006 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009007 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009008 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009009 return 0;
9010}
9011
Chris Masond3977122009-01-05 21:25:51 -05009012static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05009013 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04009014 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009015 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05009016{
Yan Zheng2c47e6052009-06-27 21:07:35 -04009017 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05009018 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04009019
Yan Zheng2c47e6052009-06-27 21:07:35 -04009020 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
9021 while (level < max_level && path->nodes[level]) {
9022 wc->level = level;
9023 if (path->slots[level] + 1 <
9024 btrfs_header_nritems(path->nodes[level])) {
9025 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05009026 return 0;
9027 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009028 ret = walk_up_proc(trans, root, path, wc);
9029 if (ret > 0)
9030 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05009031
Yan Zheng2c47e6052009-06-27 21:07:35 -04009032 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04009033 btrfs_tree_unlock_rw(path->nodes[level],
9034 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009035 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009036 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009037 free_extent_buffer(path->nodes[level]);
9038 path->nodes[level] = NULL;
9039 level++;
Chris Mason20524f02007-03-10 06:35:47 -05009040 }
9041 }
9042 return 1;
9043}
9044
Chris Mason9aca1d52007-03-13 11:09:37 -04009045/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04009046 * drop a subvolume tree.
9047 *
9048 * this function traverses the tree freeing any blocks that only
9049 * referenced by the tree.
9050 *
9051 * when a shared tree block is found. this function decreases its
9052 * reference count by one. if update_ref is true, this function
9053 * also make sure backrefs for the shared block and all lower level
9054 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00009055 *
9056 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04009057 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04009058int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009059 struct btrfs_block_rsv *block_rsv, int update_ref,
9060 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05009061{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009062 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04009063 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009064 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009065 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04009066 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009067 struct walk_control *wc;
9068 struct btrfs_key key;
9069 int err = 0;
9070 int ret;
9071 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04009072 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05009073
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009074 btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07009075
Chris Mason5caf2a02007-04-02 11:20:42 -04009076 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009077 if (!path) {
9078 err = -ENOMEM;
9079 goto out;
9080 }
Chris Mason20524f02007-03-10 06:35:47 -05009081
Yan Zheng2c47e6052009-06-27 21:07:35 -04009082 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07009083 if (!wc) {
9084 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009085 err = -ENOMEM;
9086 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07009087 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009088
Yan, Zhenga22285a2010-05-16 10:48:46 -04009089 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009090 if (IS_ERR(trans)) {
9091 err = PTR_ERR(trans);
9092 goto out_free;
9093 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00009094
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009095 if (block_rsv)
9096 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009097
Chris Mason9f3a7422007-08-07 15:52:19 -04009098 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009099 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009100 path->nodes[level] = btrfs_lock_root_node(root);
9101 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04009102 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009103 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009104 memset(&wc->update_progress, 0,
9105 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04009106 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04009107 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009108 memcpy(&wc->update_progress, &key,
9109 sizeof(wc->update_progress));
9110
Chris Mason6702ed42007-08-07 16:15:09 -04009111 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009112 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04009113 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009114 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
9115 path->lowest_level = 0;
9116 if (ret < 0) {
9117 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009118 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04009119 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009120 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009121
Chris Mason7d9eb122008-07-08 14:19:17 -04009122 /*
9123 * unlock our path, this is safe because only this
9124 * function is allowed to delete this snapshot
9125 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009126 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04009127
Yan Zheng2c47e6052009-06-27 21:07:35 -04009128 level = btrfs_header_level(root->node);
9129 while (1) {
9130 btrfs_tree_lock(path->nodes[level]);
9131 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009132 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009133
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009134 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009135 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05009136 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04009137 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009138 if (ret < 0) {
9139 err = ret;
9140 goto out_end_trans;
9141 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009142 BUG_ON(wc->refs[level] == 0);
9143
9144 if (level == root_item->drop_level)
9145 break;
9146
9147 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009148 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009149 WARN_ON(wc->refs[level] != 1);
9150 level--;
9151 }
9152 }
9153
9154 wc->level = level;
9155 wc->shared_level = -1;
9156 wc->stage = DROP_REFERENCE;
9157 wc->update_ref = update_ref;
9158 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009159 wc->for_reloc = for_reloc;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009160 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009161
9162 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00009163
Yan Zheng2c47e6052009-06-27 21:07:35 -04009164 ret = walk_down_tree(trans, root, path, wc);
9165 if (ret < 0) {
9166 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04009167 break;
9168 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009169
9170 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
9171 if (ret < 0) {
9172 err = ret;
9173 break;
9174 }
9175
9176 if (ret > 0) {
9177 BUG_ON(wc->stage != DROP_REFERENCE);
9178 break;
9179 }
9180
9181 if (wc->stage == DROP_REFERENCE) {
9182 level = wc->level;
9183 btrfs_node_key(path->nodes[level],
9184 &root_item->drop_progress,
9185 path->slots[level]);
9186 root_item->drop_level = level;
9187 }
9188
9189 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009190 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009191 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009192 ret = btrfs_update_root(trans, tree_root,
9193 &root->root_key,
9194 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009195 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009196 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009197 err = ret;
9198 goto out_end_trans;
9199 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009200
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009201 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009202 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009203 btrfs_debug(fs_info,
9204 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04009205 err = -EAGAIN;
9206 goto out_free;
9207 }
9208
Yan, Zhenga22285a2010-05-16 10:48:46 -04009209 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009210 if (IS_ERR(trans)) {
9211 err = PTR_ERR(trans);
9212 goto out_free;
9213 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009214 if (block_rsv)
9215 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04009216 }
Chris Mason20524f02007-03-10 06:35:47 -05009217 }
David Sterbab3b4aa72011-04-21 01:20:15 +02009218 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009219 if (err)
9220 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009221
Jeff Mahoney1cd54472017-08-17 10:25:11 -04009222 ret = btrfs_del_root(trans, fs_info, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009223 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009224 btrfs_abort_transaction(trans, ret);
Jeff Mahoneye19182c2017-12-04 13:11:45 -05009225 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009226 goto out_end_trans;
9227 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009228
Yan, Zheng76dda932009-09-21 16:00:26 -04009229 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00009230 ret = btrfs_find_root(tree_root, &root->root_key, path,
9231 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009232 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009233 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009234 err = ret;
9235 goto out_end_trans;
9236 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05009237 /* if we fail to delete the orphan item this time
9238 * around, it'll get picked up the next time.
9239 *
9240 * The most common failure here is just -ENOENT.
9241 */
9242 btrfs_del_orphan_item(trans, tree_root,
9243 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04009244 }
9245 }
9246
Miao Xie27cdeb72014-04-02 19:51:05 +08009247 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04009248 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009249 } else {
9250 free_extent_buffer(root->node);
9251 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00009252 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009253 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04009254 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009255out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009256 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009257out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04009258 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04009259 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009260out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04009261 /*
9262 * So if we need to stop dropping the snapshot for whatever reason we
9263 * need to make sure to add it back to the dead root list so that we
9264 * keep trying to do the work later. This also cleans up roots if we
9265 * don't have it in the radix (like when we recover after a power fail
9266 * or unmount) so we don't leak memory.
9267 */
Thomas Meyer897ca812017-10-07 16:02:21 +02009268 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04009269 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08009270 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009271 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04009272 return err;
Chris Mason20524f02007-03-10 06:35:47 -05009273}
Chris Mason9078a3e2007-04-26 16:46:15 -04009274
Yan Zheng2c47e6052009-06-27 21:07:35 -04009275/*
9276 * drop subtree rooted at tree block 'node'.
9277 *
9278 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009279 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04009280 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04009281int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
9282 struct btrfs_root *root,
9283 struct extent_buffer *node,
9284 struct extent_buffer *parent)
9285{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009286 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009287 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009288 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009289 int level;
9290 int parent_level;
9291 int ret = 0;
9292 int wret;
9293
Yan Zheng2c47e6052009-06-27 21:07:35 -04009294 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
9295
Yan Zhengf82d02d2008-10-29 14:49:05 -04009296 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009297 if (!path)
9298 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009299
Yan Zheng2c47e6052009-06-27 21:07:35 -04009300 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009301 if (!wc) {
9302 btrfs_free_path(path);
9303 return -ENOMEM;
9304 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009305
Chris Masonb9447ef2009-03-09 11:45:38 -04009306 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009307 parent_level = btrfs_header_level(parent);
9308 extent_buffer_get(parent);
9309 path->nodes[parent_level] = parent;
9310 path->slots[parent_level] = btrfs_header_nritems(parent);
9311
Chris Masonb9447ef2009-03-09 11:45:38 -04009312 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009313 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009314 path->nodes[level] = node;
9315 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009316 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009317
9318 wc->refs[parent_level] = 1;
9319 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
9320 wc->level = level;
9321 wc->shared_level = -1;
9322 wc->stage = DROP_REFERENCE;
9323 wc->update_ref = 0;
9324 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009325 wc->for_reloc = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009326 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009327
9328 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009329 wret = walk_down_tree(trans, root, path, wc);
9330 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04009331 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009332 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009333 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009334
Yan Zheng2c47e6052009-06-27 21:07:35 -04009335 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009336 if (wret < 0)
9337 ret = wret;
9338 if (wret != 0)
9339 break;
9340 }
9341
Yan Zheng2c47e6052009-06-27 21:07:35 -04009342 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009343 btrfs_free_path(path);
9344 return ret;
9345}
9346
Jeff Mahoney6202df62016-06-22 18:54:22 -04009347static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04009348{
9349 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009350 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04009351
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009352 /*
9353 * if restripe for this chunk_type is on pick target profile and
9354 * return, otherwise do the usual balance
9355 */
Jeff Mahoney6202df62016-06-22 18:54:22 -04009356 stripped = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009357 if (stripped)
9358 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02009359
Jeff Mahoney6202df62016-06-22 18:54:22 -04009360 num_devices = fs_info->fs_devices->rw_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05009361
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009362 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05009363 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009364 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
9365
Chris Masonec44a352008-04-28 15:29:52 -04009366 if (num_devices == 1) {
9367 stripped |= BTRFS_BLOCK_GROUP_DUP;
9368 stripped = flags & ~stripped;
9369
9370 /* turn raid0 into single device chunks */
9371 if (flags & BTRFS_BLOCK_GROUP_RAID0)
9372 return stripped;
9373
9374 /* turn mirroring into duplication */
9375 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
9376 BTRFS_BLOCK_GROUP_RAID10))
9377 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04009378 } else {
9379 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04009380 if (flags & stripped)
9381 return flags;
9382
9383 stripped |= BTRFS_BLOCK_GROUP_DUP;
9384 stripped = flags & ~stripped;
9385
9386 /* switch duplicated blocks with raid1 */
9387 if (flags & BTRFS_BLOCK_GROUP_DUP)
9388 return stripped | BTRFS_BLOCK_GROUP_RAID1;
9389
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009390 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04009391 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009392
Chris Masonec44a352008-04-28 15:29:52 -04009393 return flags;
9394}
9395
Zhaolei868f4012015-08-05 16:43:27 +08009396static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04009397{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009398 struct btrfs_space_info *sinfo = cache->space_info;
9399 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00009400 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009401 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04009402
Miao Xie199c36e2011-07-15 10:34:36 +00009403 /*
9404 * We need some metadata space and system metadata space for
9405 * allocating chunks in some corner cases until we force to set
9406 * it to be readonly.
9407 */
9408 if ((sinfo->flags &
9409 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
9410 !force)
Byongho Leeee221842015-12-15 01:42:10 +09009411 min_allocable_bytes = SZ_1M;
Miao Xie199c36e2011-07-15 10:34:36 +00009412 else
9413 min_allocable_bytes = 0;
9414
Yan, Zhengf0486c62010-05-16 10:46:25 -04009415 spin_lock(&sinfo->lock);
9416 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00009417
9418 if (cache->ro) {
Zhaolei868f4012015-08-05 16:43:27 +08009419 cache->ro++;
WuBo61cfea92011-07-26 03:30:11 +00009420 ret = 0;
9421 goto out;
9422 }
9423
Yan, Zhengf0486c62010-05-16 10:46:25 -04009424 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
9425 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04009426
Liu Bo41361352017-02-13 15:42:21 -08009427 if (btrfs_space_info_used(sinfo, true) + num_bytes +
Josef Bacik37be25b2011-08-05 10:25:38 -04009428 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04009429 sinfo->bytes_readonly += num_bytes;
Zhaolei868f4012015-08-05 16:43:27 +08009430 cache->ro++;
Josef Bacik633c0aa2014-10-31 09:49:34 -04009431 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009432 ret = 0;
9433 }
WuBo61cfea92011-07-26 03:30:11 +00009434out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04009435 spin_unlock(&cache->lock);
9436 spin_unlock(&sinfo->lock);
9437 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04009438}
9439
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009440int btrfs_inc_block_group_ro(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04009441 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009442
9443{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009444 struct btrfs_trans_handle *trans;
9445 u64 alloc_flags;
9446 int ret;
9447
Chris Mason1bbc6212015-04-06 12:46:08 -07009448again:
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009449 trans = btrfs_join_transaction(fs_info->extent_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009450 if (IS_ERR(trans))
9451 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009452
Chris Mason1bbc6212015-04-06 12:46:08 -07009453 /*
9454 * we're not allowed to set block groups readonly after the dirty
9455 * block groups cache has started writing. If it already started,
9456 * back off and let this transaction commit
9457 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009458 mutex_lock(&fs_info->ro_block_group_mutex);
Josef Bacik3204d332015-09-24 10:46:10 -04009459 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
Chris Mason1bbc6212015-04-06 12:46:08 -07009460 u64 transid = trans->transid;
9461
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009462 mutex_unlock(&fs_info->ro_block_group_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009463 btrfs_end_transaction(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07009464
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009465 ret = btrfs_wait_for_commit(fs_info, transid);
Chris Mason1bbc6212015-04-06 12:46:08 -07009466 if (ret)
9467 return ret;
9468 goto again;
9469 }
9470
Chris Mason153c35b2015-05-19 18:54:41 -07009471 /*
9472 * if we are changing raid levels, try to allocate a corresponding
9473 * block group with the new raid level.
9474 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009475 alloc_flags = update_block_group_flags(fs_info, cache->flags);
Chris Mason153c35b2015-05-19 18:54:41 -07009476 if (alloc_flags != cache->flags) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009477 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason153c35b2015-05-19 18:54:41 -07009478 CHUNK_ALLOC_FORCE);
9479 /*
9480 * ENOSPC is allowed here, we may have enough space
9481 * already allocated at the new raid level to
9482 * carry on
9483 */
9484 if (ret == -ENOSPC)
9485 ret = 0;
9486 if (ret < 0)
9487 goto out;
9488 }
Chris Mason1bbc6212015-04-06 12:46:08 -07009489
Zhaolei868f4012015-08-05 16:43:27 +08009490 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009491 if (!ret)
9492 goto out;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009493 alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags);
9494 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04009495 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009496 if (ret < 0)
9497 goto out;
Zhaolei868f4012015-08-05 16:43:27 +08009498 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009499out:
Shaohua Li2f081082015-01-09 10:40:15 -08009500 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009501 alloc_flags = update_block_group_flags(fs_info, cache->flags);
David Sterba34441362016-10-04 19:34:27 +02009502 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009503 check_system_chunk(trans, fs_info, alloc_flags);
David Sterba34441362016-10-04 19:34:27 +02009504 mutex_unlock(&fs_info->chunk_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009505 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009506 mutex_unlock(&fs_info->ro_block_group_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009507
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009508 btrfs_end_transaction(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009509 return ret;
9510}
9511
Chris Masonc87f08c2011-02-16 13:57:04 -05009512int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009513 struct btrfs_fs_info *fs_info, u64 type)
Chris Masonc87f08c2011-02-16 13:57:04 -05009514{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009515 u64 alloc_flags = get_alloc_profile(fs_info, type);
9516
9517 return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05009518}
9519
Miao Xie6d07bce2011-01-05 10:07:31 +00009520/*
9521 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04009522 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00009523 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009524u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00009525{
9526 struct btrfs_block_group_cache *block_group;
9527 u64 free_bytes = 0;
9528 int factor;
9529
Nicholas D Steeves01327612016-05-19 21:18:45 -04009530 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009531 if (list_empty(&sinfo->ro_bgs))
9532 return 0;
9533
9534 spin_lock(&sinfo->lock);
9535 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00009536 spin_lock(&block_group->lock);
9537
9538 if (!block_group->ro) {
9539 spin_unlock(&block_group->lock);
9540 continue;
9541 }
9542
9543 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
9544 BTRFS_BLOCK_GROUP_RAID10 |
9545 BTRFS_BLOCK_GROUP_DUP))
9546 factor = 2;
9547 else
9548 factor = 1;
9549
9550 free_bytes += (block_group->key.offset -
9551 btrfs_block_group_used(&block_group->item)) *
9552 factor;
9553
9554 spin_unlock(&block_group->lock);
9555 }
Miao Xie6d07bce2011-01-05 10:07:31 +00009556 spin_unlock(&sinfo->lock);
9557
9558 return free_bytes;
9559}
9560
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009561void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache)
Yan, Zhengf0486c62010-05-16 10:46:25 -04009562{
9563 struct btrfs_space_info *sinfo = cache->space_info;
9564 u64 num_bytes;
9565
9566 BUG_ON(!cache->ro);
9567
9568 spin_lock(&sinfo->lock);
9569 spin_lock(&cache->lock);
Zhaolei868f4012015-08-05 16:43:27 +08009570 if (!--cache->ro) {
9571 num_bytes = cache->key.offset - cache->reserved -
9572 cache->pinned - cache->bytes_super -
9573 btrfs_block_group_used(&cache->item);
9574 sinfo->bytes_readonly -= num_bytes;
9575 list_del_init(&cache->ro_list);
9576 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009577 spin_unlock(&cache->lock);
9578 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009579}
9580
Josef Bacikba1bf482009-09-11 16:11:19 -04009581/*
9582 * checks to see if its even possible to relocate this block group.
9583 *
9584 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9585 * ok to go ahead and try.
9586 */
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009587int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04009588{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009589 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04009590 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04009591 struct btrfs_space_info *space_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009592 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacikba1bf482009-09-11 16:11:19 -04009593 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04009594 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00009595 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04009596 u64 dev_min = 1;
9597 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009598 u64 target;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009599 int debug;
liubocdcb7252011-08-03 10:15:25 +00009600 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04009601 int full = 0;
9602 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05009603
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009604 debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG);
Qu Wenruo0305bc22016-03-23 11:38:17 +08009605
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009606 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04009607
Josef Bacikba1bf482009-09-11 16:11:19 -04009608 /* odd, couldn't find the block group, leave it alone */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009609 if (!block_group) {
9610 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009611 btrfs_warn(fs_info,
Qu Wenruo0305bc22016-03-23 11:38:17 +08009612 "can't find block group for bytenr %llu",
9613 bytenr);
Josef Bacikba1bf482009-09-11 16:11:19 -04009614 return -1;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009615 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009616
liubocdcb7252011-08-03 10:15:25 +00009617 min_free = btrfs_block_group_used(&block_group->item);
9618
Josef Bacikba1bf482009-09-11 16:11:19 -04009619 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00009620 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04009621 goto out;
Chris Mason323da792008-05-09 11:46:48 -04009622
Josef Bacikba1bf482009-09-11 16:11:19 -04009623 space_info = block_group->space_info;
9624 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04009625
Josef Bacikba1bf482009-09-11 16:11:19 -04009626 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04009627
Josef Bacikba1bf482009-09-11 16:11:19 -04009628 /*
9629 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04009630 * relocate it unless we're able to allocate a new chunk below.
9631 *
9632 * Otherwise, we need to make sure we have room in the space to handle
9633 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04009634 */
Chris Mason7ce618d2009-09-22 14:48:44 -04009635 if ((space_info->total_bytes != block_group->key.offset) &&
Liu Bo41361352017-02-13 15:42:21 -08009636 (btrfs_space_info_used(space_info, false) + min_free <
9637 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04009638 spin_unlock(&space_info->lock);
9639 goto out;
9640 }
9641 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009642
Josef Bacikba1bf482009-09-11 16:11:19 -04009643 /*
9644 * ok we don't have enough space, but maybe we have free space on our
9645 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009646 * alloc devices and guess if we have enough space. if this block
9647 * group is going to be restriped, run checks against the target
9648 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04009649 */
9650 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05009651
liubocdcb7252011-08-03 10:15:25 +00009652 /*
9653 * index:
9654 * 0: raid10
9655 * 1: raid1
9656 * 2: dup
9657 * 3: raid0
9658 * 4: single
9659 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009660 target = get_restripe_target(fs_info, block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009661 if (target) {
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009662 index = btrfs_bg_flags_to_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009663 } else {
9664 /*
9665 * this is just a balance, so if we were marked as full
9666 * we know there is no space for a new chunk
9667 */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009668 if (full) {
9669 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009670 btrfs_warn(fs_info,
9671 "no space to alloc new chunk for block group %llu",
9672 block_group->key.objectid);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009673 goto out;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009674 }
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009675
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009676 index = btrfs_bg_flags_to_raid_index(block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009677 }
9678
Miao Xiee6ec7162013-01-17 05:38:51 +00009679 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00009680 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04009681 /* Divide by 2 */
9682 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009683 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00009684 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00009685 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04009686 /* Multiply by 2 */
9687 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009688 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00009689 dev_min = fs_devices->rw_devices;
David Sterba47c57132015-02-20 18:43:47 +01009690 min_free = div64_u64(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00009691 }
9692
Josef Bacik6df9a952013-06-27 13:22:46 -04009693 /* We need to do this so that we can look at pending chunks */
9694 trans = btrfs_join_transaction(root);
9695 if (IS_ERR(trans)) {
9696 ret = PTR_ERR(trans);
9697 goto out;
9698 }
9699
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009700 mutex_lock(&fs_info->chunk_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04009701 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00009702 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04009703
Josef Bacikba1bf482009-09-11 16:11:19 -04009704 /*
9705 * check to make sure we can actually find a chunk with enough
9706 * space to fit our block group in.
9707 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01009708 if (device->total_bytes > device->bytes_used + min_free &&
Anand Jain401e29c2017-12-04 12:54:55 +08009709 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Josef Bacik6df9a952013-06-27 13:22:46 -04009710 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00009711 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04009712 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00009713 dev_nr++;
9714
9715 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05009716 break;
liubocdcb7252011-08-03 10:15:25 +00009717
Josef Bacikba1bf482009-09-11 16:11:19 -04009718 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05009719 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009720 }
Qu Wenruo0305bc22016-03-23 11:38:17 +08009721 if (debug && ret == -1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009722 btrfs_warn(fs_info,
9723 "no space to allocate a new chunk for block group %llu",
9724 block_group->key.objectid);
9725 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009726 btrfs_end_transaction(trans);
Chris Masonedbd8d42007-12-21 16:27:24 -05009727out:
Josef Bacikba1bf482009-09-11 16:11:19 -04009728 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05009729 return ret;
9730}
9731
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009732static int find_first_block_group(struct btrfs_fs_info *fs_info,
9733 struct btrfs_path *path,
9734 struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04009735{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009736 struct btrfs_root *root = fs_info->extent_root;
Chris Mason925baed2008-06-25 16:01:30 -04009737 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009738 struct btrfs_key found_key;
9739 struct extent_buffer *leaf;
9740 int slot;
9741
9742 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
9743 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009744 goto out;
9745
Chris Masond3977122009-01-05 21:25:51 -05009746 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009747 slot = path->slots[0];
9748 leaf = path->nodes[0];
9749 if (slot >= btrfs_header_nritems(leaf)) {
9750 ret = btrfs_next_leaf(root, path);
9751 if (ret == 0)
9752 continue;
9753 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009754 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04009755 break;
9756 }
9757 btrfs_item_key_to_cpu(leaf, &found_key, slot);
9758
9759 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04009760 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
Liu Bo6fb37b72016-06-22 18:31:27 -07009761 struct extent_map_tree *em_tree;
9762 struct extent_map *em;
9763
9764 em_tree = &root->fs_info->mapping_tree.map_tree;
9765 read_lock(&em_tree->lock);
9766 em = lookup_extent_mapping(em_tree, found_key.objectid,
9767 found_key.offset);
9768 read_unlock(&em_tree->lock);
9769 if (!em) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009770 btrfs_err(fs_info,
Liu Bo6fb37b72016-06-22 18:31:27 -07009771 "logical %llu len %llu found bg but no related chunk",
9772 found_key.objectid, found_key.offset);
9773 ret = -ENOENT;
9774 } else {
9775 ret = 0;
9776 }
Josef Bacik187ee582016-08-18 15:30:06 -04009777 free_extent_map(em);
Chris Mason925baed2008-06-25 16:01:30 -04009778 goto out;
9779 }
Chris Mason0b86a832008-03-24 15:01:56 -04009780 path->slots[0]++;
9781 }
Chris Mason925baed2008-06-25 16:01:30 -04009782out:
Chris Mason0b86a832008-03-24 15:01:56 -04009783 return ret;
9784}
9785
Josef Bacik0af3d002010-06-21 14:48:16 -04009786void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9787{
9788 struct btrfs_block_group_cache *block_group;
9789 u64 last = 0;
9790
9791 while (1) {
9792 struct inode *inode;
9793
9794 block_group = btrfs_lookup_first_block_group(info, last);
9795 while (block_group) {
9796 spin_lock(&block_group->lock);
9797 if (block_group->iref)
9798 break;
9799 spin_unlock(&block_group->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009800 block_group = next_block_group(info, block_group);
Josef Bacik0af3d002010-06-21 14:48:16 -04009801 }
9802 if (!block_group) {
9803 if (last == 0)
9804 break;
9805 last = 0;
9806 continue;
9807 }
9808
9809 inode = block_group->inode;
9810 block_group->iref = 0;
9811 block_group->inode = NULL;
9812 spin_unlock(&block_group->lock);
Liu Bof3bca802016-07-20 17:33:44 -07009813 ASSERT(block_group->io_ctl.inode == NULL);
Josef Bacik0af3d002010-06-21 14:48:16 -04009814 iput(inode);
9815 last = block_group->key.objectid + block_group->key.offset;
9816 btrfs_put_block_group(block_group);
9817 }
9818}
9819
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009820/*
9821 * Must be called only after stopping all workers, since we could have block
9822 * group caching kthreads running, and therefore they could race with us if we
9823 * freed the block groups before stopping them.
9824 */
Zheng Yan1a40e232008-09-26 10:09:34 -04009825int btrfs_free_block_groups(struct btrfs_fs_info *info)
9826{
9827 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04009828 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04009829 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04009830 struct rb_node *n;
9831
Josef Bacik9e351cc2014-03-13 15:42:13 -04009832 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009833 while (!list_empty(&info->caching_block_groups)) {
9834 caching_ctl = list_entry(info->caching_block_groups.next,
9835 struct btrfs_caching_control, list);
9836 list_del(&caching_ctl->list);
9837 put_caching_control(caching_ctl);
9838 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04009839 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009840
Josef Bacik47ab2a62014-09-18 11:20:02 -04009841 spin_lock(&info->unused_bgs_lock);
9842 while (!list_empty(&info->unused_bgs)) {
9843 block_group = list_first_entry(&info->unused_bgs,
9844 struct btrfs_block_group_cache,
9845 bg_list);
9846 list_del_init(&block_group->bg_list);
9847 btrfs_put_block_group(block_group);
9848 }
9849 spin_unlock(&info->unused_bgs_lock);
9850
Zheng Yan1a40e232008-09-26 10:09:34 -04009851 spin_lock(&info->block_group_cache_lock);
9852 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
9853 block_group = rb_entry(n, struct btrfs_block_group_cache,
9854 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04009855 rb_erase(&block_group->cache_node,
9856 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009857 RB_CLEAR_NODE(&block_group->cache_node);
Yan Zhengd899e052008-10-30 14:25:28 -04009858 spin_unlock(&info->block_group_cache_lock);
9859
Josef Bacik80eb2342008-10-29 14:49:05 -04009860 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04009861 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04009862 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05009863
Josef Bacik3c148742011-02-02 15:53:47 +00009864 /*
9865 * We haven't cached this block group, which means we could
9866 * possibly have excluded extents on this block group.
9867 */
Josef Bacik36cce922013-08-05 11:15:21 -04009868 if (block_group->cached == BTRFS_CACHE_NO ||
9869 block_group->cached == BTRFS_CACHE_ERROR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009870 free_excluded_extents(info, block_group);
Josef Bacik3c148742011-02-02 15:53:47 +00009871
Josef Bacik817d52f2009-07-13 21:29:25 -04009872 btrfs_remove_free_space_cache(block_group);
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009873 ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
Liu Bof3bca802016-07-20 17:33:44 -07009874 ASSERT(list_empty(&block_group->dirty_list));
9875 ASSERT(list_empty(&block_group->io_list));
9876 ASSERT(list_empty(&block_group->bg_list));
9877 ASSERT(atomic_read(&block_group->count) == 1);
Josef Bacik11dfe352009-11-13 20:12:59 +00009878 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04009879
9880 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009881 }
9882 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04009883
9884 /* now that all the block groups are freed, go through and
9885 * free all the space_info structs. This is only called during
9886 * the final stages of unmount, and so we know nobody is
9887 * using them. We call synchronize_rcu() once before we start,
9888 * just to be on the safe side.
9889 */
9890 synchronize_rcu();
9891
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04009892 release_global_block_rsv(info);
9893
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309894 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009895 int i;
9896
Chris Mason4184ea72009-03-10 12:39:20 -04009897 space_info = list_entry(info->space_info.next,
9898 struct btrfs_space_info,
9899 list);
Josef Bacikd555b6c2016-03-25 13:25:51 -04009900
9901 /*
9902 * Do not hide this behind enospc_debug, this is actually
9903 * important and indicates a real bug if this happens.
9904 */
9905 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00009906 space_info->bytes_reserved > 0 ||
Josef Bacikd555b6c2016-03-25 13:25:51 -04009907 space_info->bytes_may_use > 0))
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009908 dump_space_info(info, space_info, 0, 0);
Chris Mason4184ea72009-03-10 12:39:20 -04009909 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009910 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
9911 struct kobject *kobj;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009912 kobj = space_info->block_group_kobjs[i];
9913 space_info->block_group_kobjs[i] = NULL;
9914 if (kobj) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009915 kobject_del(kobj);
9916 kobject_put(kobj);
9917 }
9918 }
9919 kobject_del(&space_info->kobj);
9920 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04009921 }
Zheng Yan1a40e232008-09-26 10:09:34 -04009922 return 0;
9923}
9924
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009925/* link_block_group will queue up kobjects to add when we're reclaim-safe */
9926void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info)
9927{
9928 struct btrfs_space_info *space_info;
9929 struct raid_kobject *rkobj;
9930 LIST_HEAD(list);
9931 int index;
9932 int ret = 0;
9933
9934 spin_lock(&fs_info->pending_raid_kobjs_lock);
9935 list_splice_init(&fs_info->pending_raid_kobjs, &list);
9936 spin_unlock(&fs_info->pending_raid_kobjs_lock);
9937
9938 list_for_each_entry(rkobj, &list, list) {
9939 space_info = __find_space_info(fs_info, rkobj->flags);
9940 index = btrfs_bg_flags_to_raid_index(rkobj->flags);
9941
9942 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
9943 "%s", get_raid_name(index));
9944 if (ret) {
9945 kobject_put(&rkobj->kobj);
9946 break;
9947 }
9948 }
9949 if (ret)
9950 btrfs_warn(fs_info,
9951 "failed to add kobject for block cache, ignoring");
9952}
9953
Nikolay Borisovc434d212017-08-21 12:43:50 +03009954static void link_block_group(struct btrfs_block_group_cache *cache)
Yan, Zhengb742bb82010-05-16 10:46:24 -04009955{
Nikolay Borisovc434d212017-08-21 12:43:50 +03009956 struct btrfs_space_info *space_info = cache->space_info;
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009957 struct btrfs_fs_info *fs_info = cache->fs_info;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009958 int index = btrfs_bg_flags_to_raid_index(cache->flags);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009959 bool first = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04009960
9961 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009962 if (list_empty(&space_info->block_groups[index]))
9963 first = true;
9964 list_add_tail(&cache->list, &space_info->block_groups[index]);
9965 up_write(&space_info->groups_sem);
9966
9967 if (first) {
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009968 struct raid_kobject *rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
9969 if (!rkobj) {
9970 btrfs_warn(cache->fs_info,
9971 "couldn't alloc memory for raid level kobject");
9972 return;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009973 }
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009974 rkobj->flags = cache->flags;
9975 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
9976
9977 spin_lock(&fs_info->pending_raid_kobjs_lock);
9978 list_add_tail(&rkobj->list, &fs_info->pending_raid_kobjs);
9979 spin_unlock(&fs_info->pending_raid_kobjs_lock);
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009980 space_info->block_group_kobjs[index] = &rkobj->kobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009981 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04009982}
9983
Miao Xie920e4a52014-01-15 20:00:55 +08009984static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009985btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info,
9986 u64 start, u64 size)
Miao Xie920e4a52014-01-15 20:00:55 +08009987{
9988 struct btrfs_block_group_cache *cache;
9989
9990 cache = kzalloc(sizeof(*cache), GFP_NOFS);
9991 if (!cache)
9992 return NULL;
9993
9994 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
9995 GFP_NOFS);
9996 if (!cache->free_space_ctl) {
9997 kfree(cache);
9998 return NULL;
9999 }
10000
10001 cache->key.objectid = start;
10002 cache->key.offset = size;
10003 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
10004
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010005 cache->fs_info = fs_info;
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +030010006 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010007 set_free_space_tree_thresholds(cache);
10008
Miao Xie920e4a52014-01-15 20:00:55 +080010009 atomic_set(&cache->count, 1);
10010 spin_lock_init(&cache->lock);
Miao Xiee570fd22014-06-19 10:42:50 +080010011 init_rwsem(&cache->data_rwsem);
Miao Xie920e4a52014-01-15 20:00:55 +080010012 INIT_LIST_HEAD(&cache->list);
10013 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010014 INIT_LIST_HEAD(&cache->bg_list);
Josef Bacik633c0aa2014-10-31 09:49:34 -040010015 INIT_LIST_HEAD(&cache->ro_list);
Josef Bacikce93ec52014-11-17 15:45:48 -050010016 INIT_LIST_HEAD(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -070010017 INIT_LIST_HEAD(&cache->io_list);
Miao Xie920e4a52014-01-15 20:00:55 +080010018 btrfs_init_free_space_ctl(cache);
Filipe Manana04216822014-11-27 21:14:15 +000010019 atomic_set(&cache->trimming, 0);
Omar Sandovala5ed9182015-09-29 20:50:35 -070010020 mutex_init(&cache->free_space_lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +080010021 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
Miao Xie920e4a52014-01-15 20:00:55 +080010022
10023 return cache;
10024}
10025
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010026int btrfs_read_block_groups(struct btrfs_fs_info *info)
Chris Mason9078a3e2007-04-26 16:46:15 -040010027{
10028 struct btrfs_path *path;
10029 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010030 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -040010031 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -040010032 struct btrfs_key key;
10033 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -040010034 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -040010035 int need_clear = 0;
10036 u64 cache_gen;
Liu Bo49303382016-08-25 18:08:27 -070010037 u64 feature;
10038 int mixed;
10039
10040 feature = btrfs_super_incompat_flags(info->super_copy);
10041 mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS);
Chris Mason96b51792007-10-15 16:15:19 -040010042
Chris Mason9078a3e2007-04-26 16:46:15 -040010043 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -040010044 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010045 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Chris Mason9078a3e2007-04-26 16:46:15 -040010046 path = btrfs_alloc_path();
10047 if (!path)
10048 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +010010049 path->reada = READA_FORWARD;
Chris Mason9078a3e2007-04-26 16:46:15 -040010050
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010051 cache_gen = btrfs_super_cache_generation(info->super_copy);
10052 if (btrfs_test_opt(info, SPACE_CACHE) &&
10053 btrfs_super_generation(info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -040010054 need_clear = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010055 if (btrfs_test_opt(info, CLEAR_CACHE))
Josef Bacik88c2ba32010-09-21 14:21:34 -040010056 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -040010057
Chris Masond3977122009-01-05 21:25:51 -050010058 while (1) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010059 ret = find_first_block_group(info, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -040010060 if (ret > 0)
10061 break;
Chris Mason0b86a832008-03-24 15:01:56 -040010062 if (ret != 0)
10063 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +080010064
Chris Mason5f39d392007-10-15 16:14:19 -040010065 leaf = path->nodes[0];
10066 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +080010067
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010068 cache = btrfs_create_block_group_cache(info, found_key.objectid,
Miao Xie920e4a52014-01-15 20:00:55 +080010069 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -040010070 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -040010071 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -040010072 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -040010073 }
Josef Bacik96303082009-07-13 21:29:25 -040010074
Liu Bocf7c1ef2012-07-06 03:31:34 -060010075 if (need_clear) {
10076 /*
10077 * When we mount with old space cache, we need to
10078 * set BTRFS_DC_CLEAR and set dirty flag.
10079 *
10080 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
10081 * truncate the old free space cache inode and
10082 * setup a new one.
10083 * b) Setting 'dirty flag' makes sure that we flush
10084 * the new space cache info onto disk.
10085 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010086 if (btrfs_test_opt(info, SPACE_CACHE))
Josef Bacikce93ec52014-11-17 15:45:48 -050010087 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -060010088 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010089
Chris Mason5f39d392007-10-15 16:14:19 -040010090 read_extent_buffer(leaf, &cache->item,
10091 btrfs_item_ptr_offset(leaf, path->slots[0]),
10092 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +080010093 cache->flags = btrfs_block_group_flags(&cache->item);
Liu Bo49303382016-08-25 18:08:27 -070010094 if (!mixed &&
10095 ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
10096 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
10097 btrfs_err(info,
10098"bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
10099 cache->key.objectid);
10100 ret = -EINVAL;
10101 goto error;
10102 }
Chris Mason0b86a832008-03-24 15:01:56 -040010103
Chris Mason9078a3e2007-04-26 16:46:15 -040010104 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +020010105 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +080010106
Josef Bacik817d52f2009-07-13 21:29:25 -040010107 /*
Josef Bacik3c148742011-02-02 15:53:47 +000010108 * We need to exclude the super stripes now so that the space
10109 * info has super bytes accounted for, otherwise we'll think
10110 * we have more space than we actually do.
10111 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010112 ret = exclude_super_stripes(info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010113 if (ret) {
10114 /*
10115 * We may have excluded something, so call this just in
10116 * case.
10117 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010118 free_excluded_extents(info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010119 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010120 goto error;
10121 }
Josef Bacik3c148742011-02-02 15:53:47 +000010122
10123 /*
Josef Bacik817d52f2009-07-13 21:29:25 -040010124 * check for two cases, either we are full, and therefore
10125 * don't need to bother with the caching work since we won't
10126 * find any space, or we are empty, and we can just add all
10127 * the space in and be done with it. This saves us _alot_ of
10128 * time, particularly in the full case.
10129 */
10130 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
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 Mahoney2ff7e612016-06-22 18:54:24 -040010133 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010134 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -040010135 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010136 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010137 add_new_free_space(cache, info,
Josef Bacik817d52f2009-07-13 21:29:25 -040010138 found_key.objectid,
10139 found_key.objectid +
10140 found_key.offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010141 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010142 }
Chris Mason96b51792007-10-15 16:15:19 -040010143
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010144 ret = btrfs_add_block_group_cache(info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010145 if (ret) {
10146 btrfs_remove_free_space_cache(cache);
10147 btrfs_put_block_group(cache);
10148 goto error;
10149 }
10150
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010151 trace_btrfs_add_block_group(info, cache, 0);
Nikolay Borisovd2006e62017-05-22 09:35:50 +030010152 update_space_info(info, cache->flags, found_key.offset,
10153 btrfs_block_group_used(&cache->item),
10154 cache->bytes_super, &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010155
Chris Mason6324fbf2008-03-24 15:01:59 -040010156 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -040010157
Nikolay Borisovc434d212017-08-21 12:43:50 +030010158 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010159
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010160 set_avail_alloc_bits(info, cache->flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010161 if (btrfs_chunk_readonly(info, cache->key.objectid)) {
Zhaolei868f4012015-08-05 16:43:27 +080010162 inc_block_group_ro(cache, 1);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010163 } else if (btrfs_block_group_used(&cache->item) == 0) {
10164 spin_lock(&info->unused_bgs_lock);
10165 /* Should always be true but just in case. */
10166 if (list_empty(&cache->bg_list)) {
10167 btrfs_get_block_group(cache);
10168 list_add_tail(&cache->bg_list,
10169 &info->unused_bgs);
10170 }
10171 spin_unlock(&info->unused_bgs_lock);
10172 }
Chris Mason9078a3e2007-04-26 16:46:15 -040010173 }
Yan, Zhengb742bb82010-05-16 10:46:24 -040010174
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010175 list_for_each_entry_rcu(space_info, &info->space_info, list) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010176 if (!(get_alloc_profile(info, space_info->flags) &
Yan, Zhengb742bb82010-05-16 10:46:24 -040010177 (BTRFS_BLOCK_GROUP_RAID10 |
10178 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -050010179 BTRFS_BLOCK_GROUP_RAID5 |
10180 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -040010181 BTRFS_BLOCK_GROUP_DUP)))
10182 continue;
10183 /*
10184 * avoid allocating from un-mirrored block group if there are
10185 * mirrored block groups.
10186 */
chandan1095cc02013-07-16 12:28:56 +053010187 list_for_each_entry(cache,
10188 &space_info->block_groups[BTRFS_RAID_RAID0],
10189 list)
Zhaolei868f4012015-08-05 16:43:27 +080010190 inc_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +053010191 list_for_each_entry(cache,
10192 &space_info->block_groups[BTRFS_RAID_SINGLE],
10193 list)
Zhaolei868f4012015-08-05 16:43:27 +080010194 inc_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -040010195 }
Yan, Zhengf0486c62010-05-16 10:46:25 -040010196
Jeff Mahoney75cb3792018-03-20 15:25:26 -040010197 btrfs_add_raid_kobjects(info);
Yan, Zhengf0486c62010-05-16 10:46:25 -040010198 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -040010199 ret = 0;
10200error:
Chris Mason9078a3e2007-04-26 16:46:15 -040010201 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -040010202 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010203}
Chris Mason6324fbf2008-03-24 15:01:59 -040010204
Nikolay Borisov6c686b32018-02-07 17:55:40 +020010205void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
Josef Bacikea658ba2012-09-11 16:57:25 -040010206{
Nikolay Borisov6c686b32018-02-07 17:55:40 +020010207 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacikea658ba2012-09-11 16:57:25 -040010208 struct btrfs_block_group_cache *block_group, *tmp;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010209 struct btrfs_root *extent_root = fs_info->extent_root;
Josef Bacikea658ba2012-09-11 16:57:25 -040010210 struct btrfs_block_group_item item;
10211 struct btrfs_key key;
10212 int ret = 0;
Filipe Mananad9a05402015-10-03 13:13:13 +010010213 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010214
Filipe Mananad9a05402015-10-03 13:13:13 +010010215 trans->can_flush_pending_bgs = false;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010216 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
Josef Bacikea658ba2012-09-11 16:57:25 -040010217 if (ret)
Filipe Mananac92f6be2014-11-26 15:28:55 +000010218 goto next;
Josef Bacikea658ba2012-09-11 16:57:25 -040010219
10220 spin_lock(&block_group->lock);
10221 memcpy(&item, &block_group->item, sizeof(item));
10222 memcpy(&key, &block_group->key, sizeof(key));
10223 spin_unlock(&block_group->lock);
10224
10225 ret = btrfs_insert_item(trans, extent_root, &key, &item,
10226 sizeof(item));
10227 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010228 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010229 ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid,
10230 key.offset);
Josef Bacik6df9a952013-06-27 13:22:46 -040010231 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010232 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010233 add_block_group_free_space(trans, fs_info, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010234 /* already aborted the transaction if it failed. */
Filipe Mananac92f6be2014-11-26 15:28:55 +000010235next:
10236 list_del_init(&block_group->bg_list);
Josef Bacikea658ba2012-09-11 16:57:25 -040010237 }
Filipe Mananad9a05402015-10-03 13:13:13 +010010238 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010239}
10240
Chris Mason6324fbf2008-03-24 15:01:59 -040010241int btrfs_make_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010242 struct btrfs_fs_info *fs_info, u64 bytes_used,
Nikolay Borisov01744842017-07-27 14:22:11 +030010243 u64 type, u64 chunk_offset, u64 size)
Chris Mason6324fbf2008-03-24 15:01:59 -040010244{
Chris Mason6324fbf2008-03-24 15:01:59 -040010245 struct btrfs_block_group_cache *cache;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010246 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -040010247
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010248 btrfs_set_log_full_commit(fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -040010249
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010250 cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -040010251 if (!cache)
10252 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +080010253
Chris Mason6324fbf2008-03-24 15:01:59 -040010254 btrfs_set_block_group_used(&cache->item, bytes_used);
Nikolay Borisov01744842017-07-27 14:22:11 +030010255 btrfs_set_block_group_chunk_objectid(&cache->item,
10256 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Mason6324fbf2008-03-24 15:01:59 -040010257 btrfs_set_block_group_flags(&cache->item, type);
10258
Miao Xie920e4a52014-01-15 20:00:55 +080010259 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -040010260 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010261 cache->cached = BTRFS_CACHE_FINISHED;
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010262 cache->needs_free_space = 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010263 ret = exclude_super_stripes(fs_info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010264 if (ret) {
10265 /*
10266 * We may have excluded something, so call this just in
10267 * case.
10268 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010269 free_excluded_extents(fs_info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010270 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010271 return ret;
10272 }
Josef Bacik96303082009-07-13 21:29:25 -040010273
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010274 add_new_free_space(cache, fs_info, chunk_offset, chunk_offset + size);
Josef Bacik817d52f2009-07-13 21:29:25 -040010275
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010276 free_excluded_extents(fs_info, cache);
Yan Zheng11833d62009-09-11 16:11:19 -040010277
Josef Bacikd0bd4562015-09-23 14:54:14 -040010278#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010279 if (btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -040010280 u64 new_bytes_used = size - bytes_used;
10281
10282 bytes_used += new_bytes_used >> 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010283 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -040010284 }
10285#endif
Filipe Manana2e6e5182015-05-12 00:28:11 +010010286 /*
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010287 * Ensure the corresponding space_info object is created and
10288 * assigned to our block group. We want our bg to be added to the rbtree
10289 * with its ->space_info set.
Filipe Manana2e6e5182015-05-12 00:28:11 +010010290 */
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010291 cache->space_info = __find_space_info(fs_info, cache->flags);
Jeff Mahoneydc2d3002018-03-20 15:25:25 -040010292 ASSERT(cache->space_info);
Filipe Manana2e6e5182015-05-12 00:28:11 +010010293
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010294 ret = btrfs_add_block_group_cache(fs_info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010295 if (ret) {
10296 btrfs_remove_free_space_cache(cache);
10297 btrfs_put_block_group(cache);
10298 return ret;
10299 }
10300
Filipe Manana2e6e5182015-05-12 00:28:11 +010010301 /*
10302 * Now that our block group has its ->space_info set and is inserted in
10303 * the rbtree, update the space info's counters.
10304 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010305 trace_btrfs_add_block_group(fs_info, cache, 1);
Nikolay Borisovd2006e62017-05-22 09:35:50 +030010306 update_space_info(fs_info, cache->flags, size, bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -040010307 cache->bytes_super, &cache->space_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010308 update_global_block_rsv(fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -040010309
Nikolay Borisovc434d212017-08-21 12:43:50 +030010310 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010311
Josef Bacik47ab2a62014-09-18 11:20:02 -040010312 list_add_tail(&cache->bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -040010313
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010314 set_avail_alloc_bits(fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -040010315 return 0;
10316}
Zheng Yan1a40e232008-09-26 10:09:34 -040010317
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010318static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
10319{
Ilya Dryomov899c81e2012-03-27 17:09:16 +030010320 u64 extra_flags = chunk_to_extended(flags) &
10321 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010322
Miao Xiede98ced2013-01-29 10:13:12 +000010323 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010324 if (flags & BTRFS_BLOCK_GROUP_DATA)
10325 fs_info->avail_data_alloc_bits &= ~extra_flags;
10326 if (flags & BTRFS_BLOCK_GROUP_METADATA)
10327 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
10328 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
10329 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +000010330 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010331}
10332
Zheng Yan1a40e232008-09-26 10:09:34 -040010333int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010334 struct btrfs_fs_info *fs_info, u64 group_start,
Filipe Manana04216822014-11-27 21:14:15 +000010335 struct extent_map *em)
Zheng Yan1a40e232008-09-26 10:09:34 -040010336{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010337 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010338 struct btrfs_path *path;
10339 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -040010340 struct btrfs_free_cluster *cluster;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010341 struct btrfs_root *tree_root = fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010342 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -040010343 struct inode *inode;
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010344 struct kobject *kobj = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -040010345 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010346 int index;
Josef Bacik89a55892010-10-14 14:52:27 -040010347 int factor;
Filipe Manana4f69cb92014-11-26 15:28:51 +000010348 struct btrfs_caching_control *caching_ctl = NULL;
Filipe Manana04216822014-11-27 21:14:15 +000010349 bool remove_em;
Zheng Yan1a40e232008-09-26 10:09:34 -040010350
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010351 block_group = btrfs_lookup_block_group(fs_info, group_start);
Zheng Yan1a40e232008-09-26 10:09:34 -040010352 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -050010353 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -040010354
liubo9f7c43c2011-03-07 02:13:33 +000010355 /*
10356 * Free the reserved super bytes from this block group before
10357 * remove it.
10358 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010359 free_excluded_extents(fs_info, block_group);
Josef Bacikfd708b82017-09-29 15:43:50 -040010360 btrfs_free_ref_tree_range(fs_info, block_group->key.objectid,
10361 block_group->key.offset);
liubo9f7c43c2011-03-07 02:13:33 +000010362
Zheng Yan1a40e232008-09-26 10:09:34 -040010363 memcpy(&key, &block_group->key, sizeof(key));
Qu Wenruo3e72ee82018-01-30 18:20:45 +080010364 index = btrfs_bg_flags_to_raid_index(block_group->flags);
Josef Bacik89a55892010-10-14 14:52:27 -040010365 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
10366 BTRFS_BLOCK_GROUP_RAID1 |
10367 BTRFS_BLOCK_GROUP_RAID10))
10368 factor = 2;
10369 else
10370 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -040010371
Chris Mason44fb5512009-06-04 15:34:51 -040010372 /* make sure this block group isn't part of an allocation cluster */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010373 cluster = &fs_info->data_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010374 spin_lock(&cluster->refill_lock);
10375 btrfs_return_cluster_to_free_space(block_group, cluster);
10376 spin_unlock(&cluster->refill_lock);
10377
10378 /*
10379 * make sure this block group isn't part of a metadata
10380 * allocation cluster
10381 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010382 cluster = &fs_info->meta_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010383 spin_lock(&cluster->refill_lock);
10384 btrfs_return_cluster_to_free_space(block_group, cluster);
10385 spin_unlock(&cluster->refill_lock);
10386
Zheng Yan1a40e232008-09-26 10:09:34 -040010387 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010388 if (!path) {
10389 ret = -ENOMEM;
10390 goto out;
10391 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010392
Chris Mason1bbc6212015-04-06 12:46:08 -070010393 /*
10394 * get the inode first so any iput calls done for the io_list
10395 * aren't the final iput (no unlinks allowed now)
10396 */
Jeff Mahoney77ab86b2017-02-15 16:28:30 -050010397 inode = lookup_free_space_inode(fs_info, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010398
10399 mutex_lock(&trans->transaction->cache_write_mutex);
10400 /*
10401 * make sure our free spache cache IO is done before remove the
10402 * free space inode
10403 */
10404 spin_lock(&trans->transaction->dirty_bgs_lock);
10405 if (!list_empty(&block_group->io_list)) {
10406 list_del_init(&block_group->io_list);
10407
10408 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
10409
10410 spin_unlock(&trans->transaction->dirty_bgs_lock);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040010411 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010412 btrfs_put_block_group(block_group);
10413 spin_lock(&trans->transaction->dirty_bgs_lock);
10414 }
10415
10416 if (!list_empty(&block_group->dirty_list)) {
10417 list_del_init(&block_group->dirty_list);
10418 btrfs_put_block_group(block_group);
10419 }
10420 spin_unlock(&trans->transaction->dirty_bgs_lock);
10421 mutex_unlock(&trans->transaction->cache_write_mutex);
10422
Josef Bacik0af3d002010-06-21 14:48:16 -040010423 if (!IS_ERR(inode)) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010424 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010425 if (ret) {
10426 btrfs_add_delayed_iput(inode);
10427 goto out;
10428 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010429 clear_nlink(inode);
10430 /* One for the block groups ref */
10431 spin_lock(&block_group->lock);
10432 if (block_group->iref) {
10433 block_group->iref = 0;
10434 block_group->inode = NULL;
10435 spin_unlock(&block_group->lock);
10436 iput(inode);
10437 } else {
10438 spin_unlock(&block_group->lock);
10439 }
10440 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -040010441 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -040010442 }
10443
10444 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
10445 key.offset = block_group->key.objectid;
10446 key.type = 0;
10447
10448 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
10449 if (ret < 0)
10450 goto out;
10451 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +020010452 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010453 if (ret == 0) {
10454 ret = btrfs_del_item(trans, tree_root, path);
10455 if (ret)
10456 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +020010457 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010458 }
10459
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010460 spin_lock(&fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -040010461 rb_erase(&block_group->cache_node,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010462 &fs_info->block_group_cache_tree);
Filipe Manana292cbd52014-11-26 15:28:50 +000010463 RB_CLEAR_NODE(&block_group->cache_node);
Liu Boa1897fd2012-12-27 09:01:23 +000010464
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010465 if (fs_info->first_logical_byte == block_group->key.objectid)
10466 fs_info->first_logical_byte = (u64)-1;
10467 spin_unlock(&fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010468
Josef Bacik80eb2342008-10-29 14:49:05 -040010469 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -040010470 /*
10471 * we must use list_del_init so people can check to see if they
10472 * are still on the list after taking the semaphore
10473 */
10474 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010475 if (list_empty(&block_group->space_info->block_groups[index])) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010476 kobj = block_group->space_info->block_group_kobjs[index];
10477 block_group->space_info->block_group_kobjs[index] = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010478 clear_avail_alloc_bits(fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010479 }
Josef Bacik80eb2342008-10-29 14:49:05 -040010480 up_write(&block_group->space_info->groups_sem);
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010481 if (kobj) {
10482 kobject_del(kobj);
10483 kobject_put(kobj);
10484 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010485
Filipe Manana4f69cb92014-11-26 15:28:51 +000010486 if (block_group->has_caching_ctl)
10487 caching_ctl = get_caching_control(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -040010488 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -040010489 wait_block_group_cache_done(block_group);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010490 if (block_group->has_caching_ctl) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010491 down_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010492 if (!caching_ctl) {
10493 struct btrfs_caching_control *ctl;
10494
10495 list_for_each_entry(ctl,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010496 &fs_info->caching_block_groups, list)
Filipe Manana4f69cb92014-11-26 15:28:51 +000010497 if (ctl->block_group == block_group) {
10498 caching_ctl = ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +020010499 refcount_inc(&caching_ctl->count);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010500 break;
10501 }
10502 }
10503 if (caching_ctl)
10504 list_del_init(&caching_ctl->list);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010505 up_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010506 if (caching_ctl) {
10507 /* Once for the caching bgs list and once for us. */
10508 put_caching_control(caching_ctl);
10509 put_caching_control(caching_ctl);
10510 }
10511 }
Josef Bacik817d52f2009-07-13 21:29:25 -040010512
Josef Bacikce93ec52014-11-17 15:45:48 -050010513 spin_lock(&trans->transaction->dirty_bgs_lock);
10514 if (!list_empty(&block_group->dirty_list)) {
Chris Mason1bbc6212015-04-06 12:46:08 -070010515 WARN_ON(1);
10516 }
10517 if (!list_empty(&block_group->io_list)) {
10518 WARN_ON(1);
Josef Bacikce93ec52014-11-17 15:45:48 -050010519 }
10520 spin_unlock(&trans->transaction->dirty_bgs_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010521 btrfs_remove_free_space_cache(block_group);
10522
Yan Zhengc146afa2008-11-12 14:34:12 -050010523 spin_lock(&block_group->space_info->lock);
Filipe Manana75c68e92015-01-16 13:24:40 +000010524 list_del_init(&block_group->ro_list);
Zhao Lei18d018a2015-02-24 20:07:44 +080010525
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010526 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Zhao Lei18d018a2015-02-24 20:07:44 +080010527 WARN_ON(block_group->space_info->total_bytes
10528 < block_group->key.offset);
10529 WARN_ON(block_group->space_info->bytes_readonly
10530 < block_group->key.offset);
10531 WARN_ON(block_group->space_info->disk_total
10532 < block_group->key.offset * factor);
10533 }
Yan Zhengc146afa2008-11-12 14:34:12 -050010534 block_group->space_info->total_bytes -= block_group->key.offset;
10535 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -040010536 block_group->space_info->disk_total -= block_group->key.offset * factor;
Zhao Lei18d018a2015-02-24 20:07:44 +080010537
Yan Zhengc146afa2008-11-12 14:34:12 -050010538 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -040010539
Josef Bacik0af3d002010-06-21 14:48:16 -040010540 memcpy(&key, &block_group->key, sizeof(key));
10541
David Sterba34441362016-10-04 19:34:27 +020010542 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana495e64f2014-12-02 18:07:30 +000010543 if (!list_empty(&em->list)) {
10544 /* We're in the transaction->pending_chunks list. */
10545 free_extent_map(em);
10546 }
Filipe Manana04216822014-11-27 21:14:15 +000010547 spin_lock(&block_group->lock);
10548 block_group->removed = 1;
10549 /*
10550 * At this point trimming can't start on this block group, because we
10551 * removed the block group from the tree fs_info->block_group_cache_tree
10552 * so no one can't find it anymore and even if someone already got this
10553 * block group before we removed it from the rbtree, they have already
10554 * incremented block_group->trimming - if they didn't, they won't find
10555 * any free space entries because we already removed them all when we
10556 * called btrfs_remove_free_space_cache().
10557 *
10558 * And we must not remove the extent map from the fs_info->mapping_tree
10559 * to prevent the same logical address range and physical device space
10560 * ranges from being reused for a new block group. This is because our
10561 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
10562 * completely transactionless, so while it is trimming a range the
10563 * currently running transaction might finish and a new one start,
10564 * allowing for new block groups to be created that can reuse the same
10565 * physical device locations unless we take this special care.
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010566 *
10567 * There may also be an implicit trim operation if the file system
10568 * is mounted with -odiscard. The same protections must remain
10569 * in place until the extents have been discarded completely when
10570 * the transaction commit has completed.
Filipe Manana04216822014-11-27 21:14:15 +000010571 */
10572 remove_em = (atomic_read(&block_group->trimming) == 0);
10573 /*
10574 * Make sure a trimmer task always sees the em in the pinned_chunks list
10575 * if it sees block_group->removed == 1 (needs to lock block_group->lock
10576 * before checking block_group->removed).
10577 */
10578 if (!remove_em) {
10579 /*
10580 * Our em might be in trans->transaction->pending_chunks which
10581 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10582 * and so is the fs_info->pinned_chunks list.
10583 *
10584 * So at this point we must be holding the chunk_mutex to avoid
10585 * any races with chunk allocation (more specifically at
10586 * volumes.c:contains_pending_extent()), to ensure it always
10587 * sees the em, either in the pending_chunks list or in the
10588 * pinned_chunks list.
10589 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010590 list_move_tail(&em->list, &fs_info->pinned_chunks);
Filipe Manana04216822014-11-27 21:14:15 +000010591 }
10592 spin_unlock(&block_group->lock);
Filipe Manana04216822014-11-27 21:14:15 +000010593
10594 if (remove_em) {
10595 struct extent_map_tree *em_tree;
10596
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010597 em_tree = &fs_info->mapping_tree.map_tree;
Filipe Manana04216822014-11-27 21:14:15 +000010598 write_lock(&em_tree->lock);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010599 /*
10600 * The em might be in the pending_chunks list, so make sure the
10601 * chunk mutex is locked, since remove_extent_mapping() will
10602 * delete us from that list.
10603 */
Filipe Manana04216822014-11-27 21:14:15 +000010604 remove_extent_mapping(em_tree, em);
10605 write_unlock(&em_tree->lock);
10606 /* once for the tree */
10607 free_extent_map(em);
10608 }
10609
David Sterba34441362016-10-04 19:34:27 +020010610 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010611
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010612 ret = remove_block_group_free_space(trans, fs_info, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010613 if (ret)
10614 goto out;
10615
Chris Masonfa9c0d792009-04-03 09:47:43 -040010616 btrfs_put_block_group(block_group);
10617 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -040010618
10619 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
10620 if (ret > 0)
10621 ret = -EIO;
10622 if (ret < 0)
10623 goto out;
10624
10625 ret = btrfs_del_item(trans, root, path);
10626out:
10627 btrfs_free_path(path);
10628 return ret;
10629}
liuboacce9522011-01-06 19:30:25 +080010630
Filipe Manana8eab77f2015-11-13 23:57:16 +000010631struct btrfs_trans_handle *
Filipe Manana7fd01182015-11-13 23:57:17 +000010632btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info,
10633 const u64 chunk_offset)
Filipe Manana8eab77f2015-11-13 23:57:16 +000010634{
Filipe Manana7fd01182015-11-13 23:57:17 +000010635 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
10636 struct extent_map *em;
10637 struct map_lookup *map;
10638 unsigned int num_items;
10639
10640 read_lock(&em_tree->lock);
10641 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
10642 read_unlock(&em_tree->lock);
10643 ASSERT(em && em->start == chunk_offset);
10644
Filipe Manana8eab77f2015-11-13 23:57:16 +000010645 /*
Filipe Manana7fd01182015-11-13 23:57:17 +000010646 * We need to reserve 3 + N units from the metadata space info in order
10647 * to remove a block group (done at btrfs_remove_chunk() and at
10648 * btrfs_remove_block_group()), which are used for:
10649 *
Filipe Manana8eab77f2015-11-13 23:57:16 +000010650 * 1 unit for adding the free space inode's orphan (located in the tree
10651 * of tree roots).
Filipe Manana7fd01182015-11-13 23:57:17 +000010652 * 1 unit for deleting the block group item (located in the extent
10653 * tree).
10654 * 1 unit for deleting the free space item (located in tree of tree
10655 * roots).
10656 * N units for deleting N device extent items corresponding to each
10657 * stripe (located in the device tree).
10658 *
10659 * In order to remove a block group we also need to reserve units in the
10660 * system space info in order to update the chunk tree (update one or
10661 * more device items and remove one chunk item), but this is done at
10662 * btrfs_remove_chunk() through a call to check_system_chunk().
Filipe Manana8eab77f2015-11-13 23:57:16 +000010663 */
Jeff Mahoney95617d62015-06-03 10:55:48 -040010664 map = em->map_lookup;
Filipe Manana7fd01182015-11-13 23:57:17 +000010665 num_items = 3 + map->num_stripes;
10666 free_extent_map(em);
10667
Filipe Manana8eab77f2015-11-13 23:57:16 +000010668 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
Filipe Manana7fd01182015-11-13 23:57:17 +000010669 num_items, 1);
Filipe Manana8eab77f2015-11-13 23:57:16 +000010670}
10671
Josef Bacik47ab2a62014-09-18 11:20:02 -040010672/*
10673 * Process the unused_bgs list and remove any that don't have any allocated
10674 * space inside of them.
10675 */
10676void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10677{
10678 struct btrfs_block_group_cache *block_group;
10679 struct btrfs_space_info *space_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010680 struct btrfs_trans_handle *trans;
10681 int ret = 0;
10682
Josef Bacikafcdd122016-09-02 15:40:02 -040010683 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
Josef Bacik47ab2a62014-09-18 11:20:02 -040010684 return;
10685
10686 spin_lock(&fs_info->unused_bgs_lock);
10687 while (!list_empty(&fs_info->unused_bgs)) {
10688 u64 start, end;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010689 int trimming;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010690
10691 block_group = list_first_entry(&fs_info->unused_bgs,
10692 struct btrfs_block_group_cache,
10693 bg_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010694 list_del_init(&block_group->bg_list);
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010695
10696 space_info = block_group->space_info;
10697
Josef Bacik47ab2a62014-09-18 11:20:02 -040010698 if (ret || btrfs_mixed_space_info(space_info)) {
10699 btrfs_put_block_group(block_group);
10700 continue;
10701 }
10702 spin_unlock(&fs_info->unused_bgs_lock);
10703
Zhao Leid5f2e332015-10-08 18:46:44 +080010704 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +010010705
Josef Bacik47ab2a62014-09-18 11:20:02 -040010706 /* Don't want to race with allocators so take the groups_sem */
10707 down_write(&space_info->groups_sem);
10708 spin_lock(&block_group->lock);
10709 if (block_group->reserved ||
10710 btrfs_block_group_used(&block_group->item) ||
Chris Mason19c4d2f2016-10-10 13:43:31 -070010711 block_group->ro ||
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010712 list_is_singular(&block_group->list)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -040010713 /*
10714 * We want to bail if we made new allocations or have
10715 * outstanding allocations in this block group. We do
10716 * the ro check in case balance is currently acting on
10717 * this block group.
10718 */
10719 spin_unlock(&block_group->lock);
10720 up_write(&space_info->groups_sem);
10721 goto next;
10722 }
10723 spin_unlock(&block_group->lock);
10724
10725 /* We don't want to force the issue, only flip if it's ok. */
Zhaolei868f4012015-08-05 16:43:27 +080010726 ret = inc_block_group_ro(block_group, 0);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010727 up_write(&space_info->groups_sem);
10728 if (ret < 0) {
10729 ret = 0;
10730 goto next;
10731 }
10732
10733 /*
10734 * Want to do this before we do anything else so we can recover
10735 * properly if we fail to join the transaction.
10736 */
Filipe Manana7fd01182015-11-13 23:57:17 +000010737 trans = btrfs_start_trans_remove_block_group(fs_info,
10738 block_group->key.objectid);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010739 if (IS_ERR(trans)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010740 btrfs_dec_block_group_ro(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010741 ret = PTR_ERR(trans);
10742 goto next;
10743 }
10744
10745 /*
10746 * We could have pending pinned extents for this block group,
10747 * just delete them, we don't care about them anymore.
10748 */
10749 start = block_group->key.objectid;
10750 end = start + block_group->key.offset - 1;
Filipe Mananad4b450c2015-01-29 19:18:25 +000010751 /*
10752 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10753 * btrfs_finish_extent_commit(). If we are at transaction N,
10754 * another task might be running finish_extent_commit() for the
10755 * previous transaction N - 1, and have seen a range belonging
10756 * to the block group in freed_extents[] before we were able to
10757 * clear the whole block group range from freed_extents[]. This
10758 * means that task can lookup for the block group after we
10759 * unpinned it from freed_extents[] and removed it, leading to
10760 * a BUG_ON() at btrfs_unpin_extent_range().
10761 */
10762 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana758eb512014-11-03 14:08:39 +000010763 ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
David Sterba91166212016-04-26 23:54:39 +020010764 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010765 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010766 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010767 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010768 goto end_trans;
10769 }
10770 ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
David Sterba91166212016-04-26 23:54:39 +020010771 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010772 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010773 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010774 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010775 goto end_trans;
10776 }
Filipe Mananad4b450c2015-01-29 19:18:25 +000010777 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010778
10779 /* Reset pinned so btrfs_put_block_group doesn't complain */
Zhao Leic30666d2015-02-25 14:17:20 +080010780 spin_lock(&space_info->lock);
10781 spin_lock(&block_group->lock);
10782
10783 space_info->bytes_pinned -= block_group->pinned;
10784 space_info->bytes_readonly += block_group->pinned;
10785 percpu_counter_add(&space_info->total_bytes_pinned,
10786 -block_group->pinned);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010787 block_group->pinned = 0;
10788
Zhao Leic30666d2015-02-25 14:17:20 +080010789 spin_unlock(&block_group->lock);
10790 spin_unlock(&space_info->lock);
10791
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010792 /* DISCARD can flip during remount */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010793 trimming = btrfs_test_opt(fs_info, DISCARD);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010794
10795 /* Implicit trim during transaction commit. */
10796 if (trimming)
10797 btrfs_get_block_group_trimming(block_group);
10798
Josef Bacik47ab2a62014-09-18 11:20:02 -040010799 /*
10800 * Btrfs_remove_chunk will abort the transaction if things go
10801 * horribly wrong.
10802 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010803 ret = btrfs_remove_chunk(trans, fs_info,
Josef Bacik47ab2a62014-09-18 11:20:02 -040010804 block_group->key.objectid);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010805
10806 if (ret) {
10807 if (trimming)
10808 btrfs_put_block_group_trimming(block_group);
10809 goto end_trans;
10810 }
10811
10812 /*
10813 * If we're not mounted with -odiscard, we can just forget
10814 * about this block group. Otherwise we'll need to wait
10815 * until transaction commit to do the actual discard.
10816 */
10817 if (trimming) {
Filipe Manana348a0012015-11-27 12:16:16 +000010818 spin_lock(&fs_info->unused_bgs_lock);
10819 /*
10820 * A concurrent scrub might have added us to the list
10821 * fs_info->unused_bgs, so use a list_move operation
10822 * to add the block group to the deleted_bgs list.
10823 */
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010824 list_move(&block_group->bg_list,
10825 &trans->transaction->deleted_bgs);
Filipe Manana348a0012015-11-27 12:16:16 +000010826 spin_unlock(&fs_info->unused_bgs_lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010827 btrfs_get_block_group(block_group);
10828 }
Filipe Manana758eb512014-11-03 14:08:39 +000010829end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010830 btrfs_end_transaction(trans);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010831next:
Zhao Leid5f2e332015-10-08 18:46:44 +080010832 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010833 btrfs_put_block_group(block_group);
10834 spin_lock(&fs_info->unused_bgs_lock);
10835 }
10836 spin_unlock(&fs_info->unused_bgs_lock);
10837}
10838
liuboc59021f2011-03-07 02:13:14 +000010839int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
10840{
10841 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +000010842 struct btrfs_super_block *disk_super;
10843 u64 features;
10844 u64 flags;
10845 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +000010846 int ret;
10847
David Sterba6c417612011-04-13 15:41:04 +020010848 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +000010849 if (!btrfs_super_root(disk_super))
Dan Carpenter0dc924c52016-01-13 15:21:17 +030010850 return -EINVAL;
liuboc59021f2011-03-07 02:13:14 +000010851
liubo1aba86d2011-04-08 08:44:37 +000010852 features = btrfs_super_incompat_flags(disk_super);
10853 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
10854 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +000010855
liubo1aba86d2011-04-08 08:44:37 +000010856 flags = BTRFS_BLOCK_GROUP_SYSTEM;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010857 ret = create_space_info(fs_info, flags, &space_info);
liuboc59021f2011-03-07 02:13:14 +000010858 if (ret)
liubo1aba86d2011-04-08 08:44:37 +000010859 goto out;
liuboc59021f2011-03-07 02:13:14 +000010860
liubo1aba86d2011-04-08 08:44:37 +000010861 if (mixed) {
10862 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010863 ret = create_space_info(fs_info, flags, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010864 } else {
10865 flags = BTRFS_BLOCK_GROUP_METADATA;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010866 ret = create_space_info(fs_info, flags, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010867 if (ret)
10868 goto out;
10869
10870 flags = BTRFS_BLOCK_GROUP_DATA;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010871 ret = create_space_info(fs_info, flags, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010872 }
10873out:
liuboc59021f2011-03-07 02:13:14 +000010874 return ret;
10875}
10876
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010877int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
10878 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +080010879{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010880 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +080010881}
10882
Jeff Mahoney499f3772015-06-15 09:41:17 -040010883/*
10884 * It used to be that old block groups would be left around forever.
10885 * Iterating over them would be enough to trim unused space. Since we
10886 * now automatically remove them, we also need to iterate over unallocated
10887 * space.
10888 *
10889 * We don't want a transaction for this since the discard may take a
10890 * substantial amount of time. We don't require that a transaction be
10891 * running, but we do need to take a running transaction into account
10892 * to ensure that we're not discarding chunks that were released in
10893 * the current transaction.
10894 *
10895 * Holding the chunks lock will prevent other threads from allocating
10896 * or releasing chunks, but it won't prevent a running transaction
10897 * from committing and releasing the memory that the pending chunks
10898 * list head uses. For that, we need to take a reference to the
10899 * transaction.
10900 */
10901static int btrfs_trim_free_extents(struct btrfs_device *device,
10902 u64 minlen, u64 *trimmed)
10903{
10904 u64 start = 0, len = 0;
10905 int ret;
10906
10907 *trimmed = 0;
10908
10909 /* Not writeable = nothing to do. */
Anand Jainebbede42017-12-04 12:54:52 +080010910 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoney499f3772015-06-15 09:41:17 -040010911 return 0;
10912
10913 /* No free space = nothing to do. */
10914 if (device->total_bytes <= device->bytes_used)
10915 return 0;
10916
10917 ret = 0;
10918
10919 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -040010920 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010921 struct btrfs_transaction *trans;
10922 u64 bytes;
10923
10924 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
10925 if (ret)
10926 return ret;
10927
10928 down_read(&fs_info->commit_root_sem);
10929
10930 spin_lock(&fs_info->trans_lock);
10931 trans = fs_info->running_transaction;
10932 if (trans)
Elena Reshetova9b64f572017-03-03 10:55:11 +020010933 refcount_inc(&trans->use_count);
Jeff Mahoney499f3772015-06-15 09:41:17 -040010934 spin_unlock(&fs_info->trans_lock);
10935
10936 ret = find_free_dev_extent_start(trans, device, minlen, start,
10937 &start, &len);
10938 if (trans)
10939 btrfs_put_transaction(trans);
10940
10941 if (ret) {
10942 up_read(&fs_info->commit_root_sem);
10943 mutex_unlock(&fs_info->chunk_mutex);
10944 if (ret == -ENOSPC)
10945 ret = 0;
10946 break;
10947 }
10948
10949 ret = btrfs_issue_discard(device->bdev, start, len, &bytes);
10950 up_read(&fs_info->commit_root_sem);
10951 mutex_unlock(&fs_info->chunk_mutex);
10952
10953 if (ret)
10954 break;
10955
10956 start += len;
10957 *trimmed += bytes;
10958
10959 if (fatal_signal_pending(current)) {
10960 ret = -ERESTARTSYS;
10961 break;
10962 }
10963
10964 cond_resched();
10965 }
10966
10967 return ret;
10968}
10969
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010970int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +000010971{
Li Dongyangf7039b12011-03-24 10:24:28 +000010972 struct btrfs_block_group_cache *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010973 struct btrfs_device *device;
10974 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +000010975 u64 group_trimmed;
10976 u64 start;
10977 u64 end;
10978 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +080010979 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +000010980 int ret = 0;
10981
Liu Bo2cac13e2012-02-09 18:17:41 +080010982 /*
10983 * try to trim all FS space, our block group may start from non-zero.
10984 */
10985 if (range->len == total_bytes)
10986 cache = btrfs_lookup_first_block_group(fs_info, range->start);
10987 else
10988 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +000010989
10990 while (cache) {
10991 if (cache->key.objectid >= (range->start + range->len)) {
10992 btrfs_put_block_group(cache);
10993 break;
10994 }
10995
10996 start = max(range->start, cache->key.objectid);
10997 end = min(range->start + range->len,
10998 cache->key.objectid + cache->key.offset);
10999
11000 if (end - start >= range->minlen) {
11001 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +000011002 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -040011003 if (ret) {
11004 btrfs_put_block_group(cache);
11005 break;
11006 }
11007 ret = wait_block_group_cache_done(cache);
11008 if (ret) {
11009 btrfs_put_block_group(cache);
11010 break;
11011 }
Li Dongyangf7039b12011-03-24 10:24:28 +000011012 }
11013 ret = btrfs_trim_block_group(cache,
11014 &group_trimmed,
11015 start,
11016 end,
11017 range->minlen);
11018
11019 trimmed += group_trimmed;
11020 if (ret) {
11021 btrfs_put_block_group(cache);
11022 break;
11023 }
11024 }
11025
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040011026 cache = next_block_group(fs_info, cache);
Li Dongyangf7039b12011-03-24 10:24:28 +000011027 }
11028
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011029 mutex_lock(&fs_info->fs_devices->device_list_mutex);
11030 devices = &fs_info->fs_devices->alloc_list;
Jeff Mahoney499f3772015-06-15 09:41:17 -040011031 list_for_each_entry(device, devices, dev_alloc_list) {
11032 ret = btrfs_trim_free_extents(device, range->minlen,
11033 &group_trimmed);
11034 if (ret)
11035 break;
11036
11037 trimmed += group_trimmed;
11038 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011039 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -040011040
Li Dongyangf7039b12011-03-24 10:24:28 +000011041 range->len = trimmed;
11042 return ret;
11043}
Miao Xie8257b2d2014-03-06 13:38:19 +080011044
11045/*
David Sterbaea14b57f2017-06-22 02:19:11 +020011046 * btrfs_{start,end}_write_no_snapshotting() are similar to
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011047 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
11048 * data into the page cache through nocow before the subvolume is snapshoted,
11049 * but flush the data into disk after the snapshot creation, or to prevent
David Sterbaea14b57f2017-06-22 02:19:11 +020011050 * operations while snapshotting is ongoing and that cause the snapshot to be
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011051 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +080011052 */
David Sterbaea14b57f2017-06-22 02:19:11 +020011053void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011054{
11055 percpu_counter_dec(&root->subv_writers->counter);
11056 /*
David Sterbaa83342a2015-02-16 19:36:47 +010011057 * Make sure counter is updated before we wake up waiters.
Miao Xie8257b2d2014-03-06 13:38:19 +080011058 */
11059 smp_mb();
11060 if (waitqueue_active(&root->subv_writers->wait))
11061 wake_up(&root->subv_writers->wait);
11062}
11063
David Sterbaea14b57f2017-06-22 02:19:11 +020011064int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011065{
David Sterbaea14b57f2017-06-22 02:19:11 +020011066 if (atomic_read(&root->will_be_snapshotted))
Miao Xie8257b2d2014-03-06 13:38:19 +080011067 return 0;
11068
11069 percpu_counter_inc(&root->subv_writers->counter);
11070 /*
11071 * Make sure counter is updated before we check for snapshot creation.
11072 */
11073 smp_mb();
David Sterbaea14b57f2017-06-22 02:19:11 +020011074 if (atomic_read(&root->will_be_snapshotted)) {
11075 btrfs_end_write_no_snapshotting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +080011076 return 0;
11077 }
11078 return 1;
11079}
Zhao Lei0bc19f902016-01-06 18:56:36 +080011080
Zhao Lei0bc19f902016-01-06 18:56:36 +080011081void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
11082{
11083 while (true) {
11084 int ret;
11085
David Sterbaea14b57f2017-06-22 02:19:11 +020011086 ret = btrfs_start_write_no_snapshotting(root);
Zhao Lei0bc19f902016-01-06 18:56:36 +080011087 if (ret)
11088 break;
David Howells5e4def22017-11-02 15:27:44 +000011089 wait_on_atomic_t(&root->will_be_snapshotted, atomic_t_wait,
Zhao Lei0bc19f902016-01-06 18:56:36 +080011090 TASK_UNINTERRUPTIBLE);
11091 }
11092}