blob: 64346986f6f0b343144a507c309b515b1eee541a [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
David Sterbac1d7c512018-04-03 19:23:33 +02005
Zach Brownec6b9102007-07-11 10:00:37 -04006#include <linux/sched.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +01007#include <linux/sched/signal.h>
Chris Masonedbd8d42007-12-21 16:27:24 -05008#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -04009#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010010#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050011#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040012#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040013#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Sterbadff51cd2011-06-14 12:52:17 +020015#include <linux/ratelimit.h>
Josef Bacikb150a4f2013-06-19 15:00:04 -040016#include <linux/percpu_counter.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040017#include <linux/lockdep.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020018#include <linux/crc32c.h>
Miao Xie995946d2014-04-02 19:51:06 +080019#include "tree-log.h"
Chris Masonfec577f2007-02-26 10:40:21 -050020#include "disk-io.h"
21#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040022#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050023#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040024#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040025#include "free-space-cache.h"
Omar Sandoval1e144fb2015-09-29 20:50:37 -070026#include "free-space-tree.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060027#include "math.h"
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040028#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070029#include "qgroup.h"
Josef Bacikfd708b82017-09-29 15:43:50 -040030#include "ref-verify.h"
Chris Masonfec577f2007-02-26 10:40:21 -050031
Arne Jansen709c0482011-09-12 12:22:57 +020032#undef SCRAMBLE_DELAYED_REFS
33
Miao Xie9e622d62012-01-26 15:01:12 -050034/*
35 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040036 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
37 * if we really need one.
38 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040039 * CHUNK_ALLOC_LIMITED means to only try and allocate one
40 * if we have very few chunks already allocated. This is
41 * used as part of the clustering code to help make sure
42 * we have a good pool of storage to cluster in, without
43 * filling the FS with empty chunks
44 *
Miao Xie9e622d62012-01-26 15:01:12 -050045 * CHUNK_ALLOC_FORCE means it must try to allocate one
46 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040047 */
48enum {
49 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050050 CHUNK_ALLOC_LIMITED = 1,
51 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040052};
53
Yan Zheng5d4f98a2009-06-10 10:45:14 -040054static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040055 struct btrfs_fs_info *fs_info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080056 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040057 u64 root_objectid, u64 owner_objectid,
58 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080059 struct btrfs_delayed_extent_op *extra_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040060static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
61 struct extent_buffer *leaf,
62 struct btrfs_extent_item *ei);
63static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040064 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040065 u64 parent, u64 root_objectid,
66 u64 flags, u64 owner, u64 offset,
67 struct btrfs_key *ins, int ref_mod);
68static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +030069 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +030070 struct btrfs_delayed_extent_op *extent_op);
Josef Bacik6a632092009-02-20 11:00:09 -050071static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040072 struct btrfs_fs_info *fs_info, u64 flags,
Josef Bacik698d0082012-09-12 14:08:47 -040073 int force);
Yan Zheng11833d62009-09-11 16:11:19 -040074static int find_next_key(struct btrfs_path *path, int level,
75 struct btrfs_key *key);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -040076static void dump_space_info(struct btrfs_fs_info *fs_info,
77 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -040078 int dump_block_groups);
Josef Bacik5d803662013-02-07 16:06:02 -050079static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
80 u64 num_bytes);
Josef Bacik957780e2016-05-17 13:30:55 -040081static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
82 struct btrfs_space_info *space_info,
83 u64 num_bytes);
84static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
85 struct btrfs_space_info *space_info,
86 u64 num_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -050087
Josef Bacik817d52f2009-07-13 21:29:25 -040088static noinline int
89block_group_cache_done(struct btrfs_block_group_cache *cache)
90{
91 smp_mb();
Josef Bacik36cce922013-08-05 11:15:21 -040092 return cache->cached == BTRFS_CACHE_FINISHED ||
93 cache->cached == BTRFS_CACHE_ERROR;
Josef Bacik817d52f2009-07-13 21:29:25 -040094}
95
Josef Bacik0f9dd462008-09-23 13:14:11 -040096static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
97{
98 return (cache->flags & bits) == bits;
99}
100
Filipe Manana758f2df2015-11-19 11:45:48 +0000101void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000102{
103 atomic_inc(&cache->count);
104}
105
106void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
107{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400108 if (atomic_dec_and_test(&cache->count)) {
109 WARN_ON(cache->pinned > 0);
110 WARN_ON(cache->reserved > 0);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800111
112 /*
113 * If not empty, someone is still holding mutex of
114 * full_stripe_lock, which can only be released by caller.
115 * And it will definitely cause use-after-free when caller
116 * tries to release full stripe lock.
117 *
118 * No better way to resolve, but only to warn.
119 */
120 WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
Li Zefan34d52cb2011-03-29 13:46:06 +0800121 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000122 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400123 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000124}
125
Josef Bacik0f9dd462008-09-23 13:14:11 -0400126/*
127 * this adds the block group to the fs_info rb tree for the block group
128 * cache
129 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500130static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400131 struct btrfs_block_group_cache *block_group)
132{
133 struct rb_node **p;
134 struct rb_node *parent = NULL;
135 struct btrfs_block_group_cache *cache;
136
137 spin_lock(&info->block_group_cache_lock);
138 p = &info->block_group_cache_tree.rb_node;
139
140 while (*p) {
141 parent = *p;
142 cache = rb_entry(parent, struct btrfs_block_group_cache,
143 cache_node);
144 if (block_group->key.objectid < cache->key.objectid) {
145 p = &(*p)->rb_left;
146 } else if (block_group->key.objectid > cache->key.objectid) {
147 p = &(*p)->rb_right;
148 } else {
149 spin_unlock(&info->block_group_cache_lock);
150 return -EEXIST;
151 }
152 }
153
154 rb_link_node(&block_group->cache_node, parent, p);
155 rb_insert_color(&block_group->cache_node,
156 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000157
158 if (info->first_logical_byte > block_group->key.objectid)
159 info->first_logical_byte = block_group->key.objectid;
160
Josef Bacik0f9dd462008-09-23 13:14:11 -0400161 spin_unlock(&info->block_group_cache_lock);
162
163 return 0;
164}
165
166/*
167 * This will return the block group at or after bytenr if contains is 0, else
168 * it will return the block group that contains the bytenr
169 */
170static struct btrfs_block_group_cache *
171block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
172 int contains)
173{
174 struct btrfs_block_group_cache *cache, *ret = NULL;
175 struct rb_node *n;
176 u64 end, start;
177
178 spin_lock(&info->block_group_cache_lock);
179 n = info->block_group_cache_tree.rb_node;
180
181 while (n) {
182 cache = rb_entry(n, struct btrfs_block_group_cache,
183 cache_node);
184 end = cache->key.objectid + cache->key.offset - 1;
185 start = cache->key.objectid;
186
187 if (bytenr < start) {
188 if (!contains && (!ret || start < ret->key.objectid))
189 ret = cache;
190 n = n->rb_left;
191 } else if (bytenr > start) {
192 if (contains && bytenr <= end) {
193 ret = cache;
194 break;
195 }
196 n = n->rb_right;
197 } else {
198 ret = cache;
199 break;
200 }
201 }
Liu Boa1897fd2012-12-27 09:01:23 +0000202 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000203 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000204 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
205 info->first_logical_byte = ret->key.objectid;
206 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400207 spin_unlock(&info->block_group_cache_lock);
208
209 return ret;
210}
211
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400212static int add_excluded_extent(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400213 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400214{
Yan Zheng11833d62009-09-11 16:11:19 -0400215 u64 end = start + num_bytes - 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400216 set_extent_bits(&fs_info->freed_extents[0],
David Sterbaceeb0ae2016-04-26 23:54:39 +0200217 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400218 set_extent_bits(&fs_info->freed_extents[1],
David Sterbaceeb0ae2016-04-26 23:54:39 +0200219 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -0400220 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400221}
222
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400223static void free_excluded_extents(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400224 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400225{
Yan Zheng11833d62009-09-11 16:11:19 -0400226 u64 start, end;
227
228 start = cache->key.objectid;
229 end = start + cache->key.offset - 1;
230
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400231 clear_extent_bits(&fs_info->freed_extents[0],
David Sterba91166212016-04-26 23:54:39 +0200232 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400233 clear_extent_bits(&fs_info->freed_extents[1],
David Sterba91166212016-04-26 23:54:39 +0200234 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -0400235}
236
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400237static int exclude_super_stripes(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400238 struct btrfs_block_group_cache *cache)
239{
Josef Bacik817d52f2009-07-13 21:29:25 -0400240 u64 bytenr;
241 u64 *logical;
242 int stripe_len;
243 int i, nr, ret;
244
Yan, Zheng06b23312009-11-26 09:31:11 +0000245 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
246 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
247 cache->bytes_super += stripe_len;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400248 ret = add_excluded_extent(fs_info, cache->key.objectid,
Yan, Zheng06b23312009-11-26 09:31:11 +0000249 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400250 if (ret)
251 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000252 }
253
Josef Bacik817d52f2009-07-13 21:29:25 -0400254 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
255 bytenr = btrfs_sb_offset(i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400256 ret = btrfs_rmap_block(fs_info, cache->key.objectid,
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +0300257 bytenr, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400258 if (ret)
259 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400260
Josef Bacik817d52f2009-07-13 21:29:25 -0400261 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400262 u64 start, len;
263
264 if (logical[nr] > cache->key.objectid +
265 cache->key.offset)
266 continue;
267
268 if (logical[nr] + stripe_len <= cache->key.objectid)
269 continue;
270
271 start = logical[nr];
272 if (start < cache->key.objectid) {
273 start = cache->key.objectid;
274 len = (logical[nr] + stripe_len) - start;
275 } else {
276 len = min_t(u64, stripe_len,
277 cache->key.objectid +
278 cache->key.offset - start);
279 }
280
281 cache->bytes_super += len;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400282 ret = add_excluded_extent(fs_info, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400283 if (ret) {
284 kfree(logical);
285 return ret;
286 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400287 }
Yan Zheng11833d62009-09-11 16:11:19 -0400288
Josef Bacik817d52f2009-07-13 21:29:25 -0400289 kfree(logical);
290 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400291 return 0;
292}
293
Yan Zheng11833d62009-09-11 16:11:19 -0400294static struct btrfs_caching_control *
295get_caching_control(struct btrfs_block_group_cache *cache)
296{
297 struct btrfs_caching_control *ctl;
298
299 spin_lock(&cache->lock);
Josef Bacikdde5abe2010-09-16 16:17:03 -0400300 if (!cache->caching_ctl) {
301 spin_unlock(&cache->lock);
302 return NULL;
303 }
304
Yan Zheng11833d62009-09-11 16:11:19 -0400305 ctl = cache->caching_ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200306 refcount_inc(&ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400307 spin_unlock(&cache->lock);
308 return ctl;
309}
310
311static void put_caching_control(struct btrfs_caching_control *ctl)
312{
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200313 if (refcount_dec_and_test(&ctl->count))
Yan Zheng11833d62009-09-11 16:11:19 -0400314 kfree(ctl);
315}
316
Josef Bacikd0bd4562015-09-23 14:54:14 -0400317#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400318static void fragment_free_space(struct btrfs_block_group_cache *block_group)
Josef Bacikd0bd4562015-09-23 14:54:14 -0400319{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400320 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400321 u64 start = block_group->key.objectid;
322 u64 len = block_group->key.offset;
323 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400324 fs_info->nodesize : fs_info->sectorsize;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400325 u64 step = chunk << 1;
326
327 while (len > chunk) {
328 btrfs_remove_free_space(block_group, start, chunk);
329 start += step;
330 if (len < step)
331 len = 0;
332 else
333 len -= step;
334 }
335}
336#endif
337
Josef Bacik0f9dd462008-09-23 13:14:11 -0400338/*
339 * this is only called by cache_block_group, since we could have freed extents
340 * we need to check the pinned_extents for any extents that can't be used yet
341 * since their free space will be released as soon as the transaction commits.
342 */
Omar Sandovala5ed9182015-09-29 20:50:35 -0700343u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Nikolay Borisov4457c1c2018-05-10 15:44:45 +0300344 u64 start, u64 end)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400345{
Nikolay Borisov4457c1c2018-05-10 15:44:45 +0300346 struct btrfs_fs_info *info = block_group->fs_info;
Josef Bacik817d52f2009-07-13 21:29:25 -0400347 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400348 int ret;
349
350 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400351 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400352 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400353 EXTENT_DIRTY | EXTENT_UPTODATE,
354 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400355 if (ret)
356 break;
357
Yan, Zheng06b23312009-11-26 09:31:11 +0000358 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400359 start = extent_end + 1;
360 } else if (extent_start > start && extent_start < end) {
361 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400362 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500363 ret = btrfs_add_free_space(block_group, start,
364 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100365 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400366 start = extent_end + 1;
367 } else {
368 break;
369 }
370 }
371
372 if (start < end) {
373 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400374 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500375 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100376 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400377 }
378
Josef Bacik817d52f2009-07-13 21:29:25 -0400379 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400380}
381
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700382static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
Chris Masone37c9e62007-05-09 20:13:14 -0400383{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400384 struct btrfs_block_group_cache *block_group = caching_ctl->block_group;
385 struct btrfs_fs_info *fs_info = block_group->fs_info;
386 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400387 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400388 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400389 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400390 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400391 u64 last = 0;
392 u32 nritems;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700393 int ret;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400394 bool wakeup = true;
Chris Masonf510cfe2007-10-15 16:14:48 -0400395
Chris Masone37c9e62007-05-09 20:13:14 -0400396 path = btrfs_alloc_path();
397 if (!path)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700398 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400399
Josef Bacik817d52f2009-07-13 21:29:25 -0400400 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400401
Josef Bacikd0bd4562015-09-23 14:54:14 -0400402#ifdef CONFIG_BTRFS_DEBUG
403 /*
404 * If we're fragmenting we don't want to make anybody think we can
405 * allocate from this block group until we've had a chance to fragment
406 * the free space.
407 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400408 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -0400409 wakeup = false;
410#endif
Chris Mason5cd57b22008-06-25 16:01:30 -0400411 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400412 * We don't want to deadlock with somebody trying to allocate a new
413 * extent for the extent root while also trying to search the extent
414 * root to add free space. So we skip locking and search the commit
415 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400416 */
417 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400418 path->search_commit_root = 1;
David Sterbae4058b52015-11-27 16:31:35 +0100419 path->reada = READA_FORWARD;
Josef Bacik817d52f2009-07-13 21:29:25 -0400420
Yan Zhenge4404d62008-12-12 10:03:26 -0500421 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400422 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400423 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400424
Liu Bo52ee28d2013-07-11 17:51:15 +0800425next:
Yan Zheng11833d62009-09-11 16:11:19 -0400426 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400427 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700428 goto out;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500429
Yan Zheng11833d62009-09-11 16:11:19 -0400430 leaf = path->nodes[0];
431 nritems = btrfs_header_nritems(leaf);
432
Chris Masond3977122009-01-05 21:25:51 -0500433 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200434 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400435 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400436 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400437 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400438
Yan Zheng11833d62009-09-11 16:11:19 -0400439 if (path->slots[0] < nritems) {
440 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
441 } else {
442 ret = find_next_key(path, 0, &key);
443 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400444 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400445
Josef Bacikc9ea7b22013-09-19 10:02:11 -0400446 if (need_resched() ||
Josef Bacik9e351cc2014-03-13 15:42:13 -0400447 rwsem_is_contended(&fs_info->commit_root_sem)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400448 if (wakeup)
449 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400450 btrfs_release_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400451 up_read(&fs_info->commit_root_sem);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400452 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400453 cond_resched();
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700454 mutex_lock(&caching_ctl->mutex);
455 down_read(&fs_info->commit_root_sem);
456 goto next;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400457 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400458
459 ret = btrfs_next_leaf(extent_root, path);
460 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700461 goto out;
Josef Bacik0a3896d2013-04-19 14:37:26 -0400462 if (ret)
463 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400464 leaf = path->nodes[0];
465 nritems = btrfs_header_nritems(leaf);
466 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400467 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400468
Liu Bo52ee28d2013-07-11 17:51:15 +0800469 if (key.objectid < last) {
470 key.objectid = last;
471 key.offset = 0;
472 key.type = BTRFS_EXTENT_ITEM_KEY;
473
Josef Bacikd0bd4562015-09-23 14:54:14 -0400474 if (wakeup)
475 caching_ctl->progress = last;
Liu Bo52ee28d2013-07-11 17:51:15 +0800476 btrfs_release_path(path);
477 goto next;
478 }
479
Yan Zheng11833d62009-09-11 16:11:19 -0400480 if (key.objectid < block_group->key.objectid) {
481 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400482 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400483 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400484
Chris Masone37c9e62007-05-09 20:13:14 -0400485 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400486 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400487 break;
Yan7d7d6062007-09-14 16:15:28 -0400488
Josef Bacik3173a182013-03-07 14:22:04 -0500489 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
490 key.type == BTRFS_METADATA_ITEM_KEY) {
Nikolay Borisov4457c1c2018-05-10 15:44:45 +0300491 total_found += add_new_free_space(block_group, last,
Josef Bacik817d52f2009-07-13 21:29:25 -0400492 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500493 if (key.type == BTRFS_METADATA_ITEM_KEY)
494 last = key.objectid +
Jeff Mahoneyda170662016-06-15 09:22:56 -0400495 fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500496 else
497 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400498
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700499 if (total_found > CACHING_CTL_WAKE_UP) {
Yan Zheng11833d62009-09-11 16:11:19 -0400500 total_found = 0;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400501 if (wakeup)
502 wake_up(&caching_ctl->wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400503 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400504 }
Chris Masone37c9e62007-05-09 20:13:14 -0400505 path->slots[0]++;
506 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400507 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400508
Nikolay Borisov4457c1c2018-05-10 15:44:45 +0300509 total_found += add_new_free_space(block_group, last,
Josef Bacik817d52f2009-07-13 21:29:25 -0400510 block_group->key.objectid +
511 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400512 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400513
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700514out:
515 btrfs_free_path(path);
516 return ret;
517}
518
519static noinline void caching_thread(struct btrfs_work *work)
520{
521 struct btrfs_block_group_cache *block_group;
522 struct btrfs_fs_info *fs_info;
523 struct btrfs_caching_control *caching_ctl;
524 int ret;
525
526 caching_ctl = container_of(work, struct btrfs_caching_control, work);
527 block_group = caching_ctl->block_group;
528 fs_info = block_group->fs_info;
529
530 mutex_lock(&caching_ctl->mutex);
531 down_read(&fs_info->commit_root_sem);
532
Omar Sandoval1e144fb2015-09-29 20:50:37 -0700533 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
534 ret = load_free_space_tree(caching_ctl);
535 else
536 ret = load_extent_tree_free(caching_ctl);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700537
Josef Bacik817d52f2009-07-13 21:29:25 -0400538 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400539 block_group->caching_ctl = NULL;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700540 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
Josef Bacik817d52f2009-07-13 21:29:25 -0400541 spin_unlock(&block_group->lock);
542
Josef Bacikd0bd4562015-09-23 14:54:14 -0400543#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400544 if (btrfs_should_fragment_free_space(block_group)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400545 u64 bytes_used;
546
547 spin_lock(&block_group->space_info->lock);
548 spin_lock(&block_group->lock);
549 bytes_used = block_group->key.offset -
550 btrfs_block_group_used(&block_group->item);
551 block_group->space_info->bytes_used += bytes_used >> 1;
552 spin_unlock(&block_group->lock);
553 spin_unlock(&block_group->space_info->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400554 fragment_free_space(block_group);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400555 }
556#endif
557
558 caching_ctl->progress = (u64)-1;
Chris Masonf7d3d2f2015-12-18 11:11:10 -0800559
Josef Bacik9e351cc2014-03-13 15:42:13 -0400560 up_read(&fs_info->commit_root_sem);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400561 free_excluded_extents(fs_info, block_group);
Yan Zheng11833d62009-09-11 16:11:19 -0400562 mutex_unlock(&caching_ctl->mutex);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700563
Yan Zheng11833d62009-09-11 16:11:19 -0400564 wake_up(&caching_ctl->wait);
565
566 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000567 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400568}
569
Josef Bacik9d66e232010-08-25 16:54:15 -0400570static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400571 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400572{
Josef Bacik291c7d22011-11-14 13:52:14 -0500573 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400574 struct btrfs_fs_info *fs_info = cache->fs_info;
575 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400576 int ret = 0;
577
Josef Bacik291c7d22011-11-14 13:52:14 -0500578 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100579 if (!caching_ctl)
580 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500581
582 INIT_LIST_HEAD(&caching_ctl->list);
583 mutex_init(&caching_ctl->mutex);
584 init_waitqueue_head(&caching_ctl->wait);
585 caching_ctl->block_group = cache;
586 caching_ctl->progress = cache->key.objectid;
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200587 refcount_set(&caching_ctl->count, 1);
Liu Bo9e0af232014-08-15 23:36:53 +0800588 btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
589 caching_thread, NULL, NULL);
Josef Bacik291c7d22011-11-14 13:52:14 -0500590
591 spin_lock(&cache->lock);
592 /*
593 * This should be a rare occasion, but this could happen I think in the
594 * case where one thread starts to load the space cache info, and then
595 * some other thread starts a transaction commit which tries to do an
596 * allocation while the other thread is still loading the space cache
597 * info. The previous loop should have kept us from choosing this block
598 * group, but if we've moved to the state where we will wait on caching
599 * block groups we need to first check if we're doing a fast load here,
600 * so we can wait for it to finish, otherwise we could end up allocating
601 * from a block group who's cache gets evicted for one reason or
602 * another.
603 */
604 while (cache->cached == BTRFS_CACHE_FAST) {
605 struct btrfs_caching_control *ctl;
606
607 ctl = cache->caching_ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200608 refcount_inc(&ctl->count);
Josef Bacik291c7d22011-11-14 13:52:14 -0500609 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
610 spin_unlock(&cache->lock);
611
612 schedule();
613
614 finish_wait(&ctl->wait, &wait);
615 put_caching_control(ctl);
616 spin_lock(&cache->lock);
617 }
618
619 if (cache->cached != BTRFS_CACHE_NO) {
620 spin_unlock(&cache->lock);
621 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400622 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500623 }
624 WARN_ON(cache->caching_ctl);
625 cache->caching_ctl = caching_ctl;
626 cache->cached = BTRFS_CACHE_FAST;
627 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400628
Satoru Takeuchid8953d62017-09-12 20:08:08 +0900629 if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500630 mutex_lock(&caching_ctl->mutex);
Josef Bacik9d66e232010-08-25 16:54:15 -0400631 ret = load_free_space_cache(fs_info, cache);
632
633 spin_lock(&cache->lock);
634 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500635 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400636 cache->cached = BTRFS_CACHE_FINISHED;
637 cache->last_byte_to_unpin = (u64)-1;
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500638 caching_ctl->progress = (u64)-1;
Josef Bacik9d66e232010-08-25 16:54:15 -0400639 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500640 if (load_cache_only) {
641 cache->caching_ctl = NULL;
642 cache->cached = BTRFS_CACHE_NO;
643 } else {
644 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000645 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500646 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400647 }
648 spin_unlock(&cache->lock);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400649#ifdef CONFIG_BTRFS_DEBUG
650 if (ret == 1 &&
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400651 btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400652 u64 bytes_used;
653
654 spin_lock(&cache->space_info->lock);
655 spin_lock(&cache->lock);
656 bytes_used = cache->key.offset -
657 btrfs_block_group_used(&cache->item);
658 cache->space_info->bytes_used += bytes_used >> 1;
659 spin_unlock(&cache->lock);
660 spin_unlock(&cache->space_info->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400661 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400662 }
663#endif
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500664 mutex_unlock(&caching_ctl->mutex);
665
Josef Bacik291c7d22011-11-14 13:52:14 -0500666 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000667 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500668 put_caching_control(caching_ctl);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400669 free_excluded_extents(fs_info, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400670 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000671 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500672 } else {
673 /*
Omar Sandoval1e144fb2015-09-29 20:50:37 -0700674 * We're either using the free space tree or no caching at all.
675 * Set cached to the appropriate value and wakeup any waiters.
Josef Bacik291c7d22011-11-14 13:52:14 -0500676 */
677 spin_lock(&cache->lock);
678 if (load_cache_only) {
679 cache->caching_ctl = NULL;
680 cache->cached = BTRFS_CACHE_NO;
681 } else {
682 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000683 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500684 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400685 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500686 wake_up(&caching_ctl->wait);
687 }
688
689 if (load_cache_only) {
690 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400691 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400692 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400693
Josef Bacik9e351cc2014-03-13 15:42:13 -0400694 down_write(&fs_info->commit_root_sem);
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200695 refcount_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400696 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400697 up_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -0400698
Josef Bacik11dfe352009-11-13 20:12:59 +0000699 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400700
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800701 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400702
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400703 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400704}
705
Josef Bacik0f9dd462008-09-23 13:14:11 -0400706/*
707 * return the block group that starts at or after bytenr
708 */
Chris Masond3977122009-01-05 21:25:51 -0500709static struct btrfs_block_group_cache *
710btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400711{
Masahiro Yamadae2c89902016-09-13 04:35:52 +0900712 return block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400713}
714
Josef Bacik0f9dd462008-09-23 13:14:11 -0400715/*
Sankar P9f556842009-05-14 13:52:22 -0400716 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400717 */
Chris Masond3977122009-01-05 21:25:51 -0500718struct btrfs_block_group_cache *btrfs_lookup_block_group(
719 struct btrfs_fs_info *info,
720 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400721{
Masahiro Yamadae2c89902016-09-13 04:35:52 +0900722 return block_group_cache_tree_search(info, bytenr, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400723}
Chris Mason0b86a832008-03-24 15:01:56 -0400724
Josef Bacik0f9dd462008-09-23 13:14:11 -0400725static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
726 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400727{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400728 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400729 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400730
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200731 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400732
Chris Mason4184ea72009-03-10 12:39:20 -0400733 rcu_read_lock();
734 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400735 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400736 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400737 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400738 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400739 }
Chris Mason4184ea72009-03-10 12:39:20 -0400740 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400741 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400742}
743
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700744static void add_pinned_bytes(struct btrfs_fs_info *fs_info, s64 num_bytes,
Nikolay Borisov29d2b842018-03-30 12:58:47 +0300745 bool metadata, u64 root_objectid)
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700746{
747 struct btrfs_space_info *space_info;
748 u64 flags;
749
Nikolay Borisov29d2b842018-03-30 12:58:47 +0300750 if (metadata) {
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700751 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
752 flags = BTRFS_BLOCK_GROUP_SYSTEM;
753 else
754 flags = BTRFS_BLOCK_GROUP_METADATA;
755 } else {
756 flags = BTRFS_BLOCK_GROUP_DATA;
757 }
758
759 space_info = __find_space_info(fs_info, flags);
Omar Sandoval55e81962017-06-06 16:45:27 -0700760 ASSERT(space_info);
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700761 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
762}
763
Chris Mason4184ea72009-03-10 12:39:20 -0400764/*
765 * after adding space to the filesystem, we need to clear the full flags
766 * on all the space infos.
767 */
768void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
769{
770 struct list_head *head = &info->space_info;
771 struct btrfs_space_info *found;
772
773 rcu_read_lock();
774 list_for_each_entry_rcu(found, head, list)
775 found->full = 0;
776 rcu_read_unlock();
777}
778
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000779/* simple helper to search for an existing data extent at a given offset */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400780int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400781{
782 int ret;
783 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400784 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400785
Zheng Yan31840ae2008-09-23 13:14:14 -0400786 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700787 if (!path)
788 return -ENOMEM;
789
Chris Masone02119d2008-09-05 16:13:11 -0400790 key.objectid = start;
791 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500792 key.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400793 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400794 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500795 return ret;
796}
797
Chris Masond8d5f3e2007-12-11 12:42:00 -0500798/*
Josef Bacik3173a182013-03-07 14:22:04 -0500799 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400800 *
801 * the head node for delayed ref is used to store the sum of all the
802 * reference count modifications queued up in the rbtree. the head
803 * node may also store the extent flags to set. This way you can check
804 * to see what the reference count and extent flags would be if all of
805 * the delayed refs are not processed.
806 */
807int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400808 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500809 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400810{
811 struct btrfs_delayed_ref_head *head;
812 struct btrfs_delayed_ref_root *delayed_refs;
813 struct btrfs_path *path;
814 struct btrfs_extent_item *ei;
815 struct extent_buffer *leaf;
816 struct btrfs_key key;
817 u32 item_size;
818 u64 num_refs;
819 u64 extent_flags;
820 int ret;
821
Josef Bacik3173a182013-03-07 14:22:04 -0500822 /*
823 * If we don't have skinny metadata, don't bother doing anything
824 * different
825 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400826 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
827 offset = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500828 metadata = 0;
829 }
830
Yan, Zhenga22285a2010-05-16 10:48:46 -0400831 path = btrfs_alloc_path();
832 if (!path)
833 return -ENOMEM;
834
Yan, Zhenga22285a2010-05-16 10:48:46 -0400835 if (!trans) {
836 path->skip_locking = 1;
837 path->search_commit_root = 1;
838 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000839
840search_again:
841 key.objectid = bytenr;
842 key.offset = offset;
843 if (metadata)
844 key.type = BTRFS_METADATA_ITEM_KEY;
845 else
846 key.type = BTRFS_EXTENT_ITEM_KEY;
847
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400848 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400849 if (ret < 0)
850 goto out_free;
851
Josef Bacik3173a182013-03-07 14:22:04 -0500852 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100853 if (path->slots[0]) {
854 path->slots[0]--;
855 btrfs_item_key_to_cpu(path->nodes[0], &key,
856 path->slots[0]);
857 if (key.objectid == bytenr &&
858 key.type == BTRFS_EXTENT_ITEM_KEY &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400859 key.offset == fs_info->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100860 ret = 0;
861 }
Josef Bacik3173a182013-03-07 14:22:04 -0500862 }
863
Yan, Zhenga22285a2010-05-16 10:48:46 -0400864 if (ret == 0) {
865 leaf = path->nodes[0];
866 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
867 if (item_size >= sizeof(*ei)) {
868 ei = btrfs_item_ptr(leaf, path->slots[0],
869 struct btrfs_extent_item);
870 num_refs = btrfs_extent_refs(leaf, ei);
871 extent_flags = btrfs_extent_flags(leaf, ei);
872 } else {
873#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
874 struct btrfs_extent_item_v0 *ei0;
875 BUG_ON(item_size != sizeof(*ei0));
876 ei0 = btrfs_item_ptr(leaf, path->slots[0],
877 struct btrfs_extent_item_v0);
878 num_refs = btrfs_extent_refs_v0(leaf, ei0);
879 /* FIXME: this isn't correct for data */
880 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
881#else
882 BUG();
883#endif
884 }
885 BUG_ON(num_refs == 0);
886 } else {
887 num_refs = 0;
888 extent_flags = 0;
889 ret = 0;
890 }
891
892 if (!trans)
893 goto out;
894
895 delayed_refs = &trans->transaction->delayed_refs;
896 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -0800897 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400898 if (head) {
899 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -0400900 refcount_inc(&head->refs);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400901 spin_unlock(&delayed_refs->lock);
902
David Sterbab3b4aa72011-04-21 01:20:15 +0200903 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400904
David Sterba8cc33e52011-05-02 15:29:25 +0200905 /*
906 * Mutex was contended, block until it's released and try
907 * again
908 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400909 mutex_lock(&head->mutex);
910 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -0400911 btrfs_put_delayed_ref_head(head);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000912 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400913 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500914 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400915 if (head->extent_op && head->extent_op->update_flags)
916 extent_flags |= head->extent_op->flags_to_set;
917 else
918 BUG_ON(num_refs == 0);
919
Josef Bacikd2788502017-09-29 15:43:57 -0400920 num_refs += head->ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500921 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400922 mutex_unlock(&head->mutex);
923 }
924 spin_unlock(&delayed_refs->lock);
925out:
926 WARN_ON(num_refs == 0);
927 if (refs)
928 *refs = num_refs;
929 if (flags)
930 *flags = extent_flags;
931out_free:
932 btrfs_free_path(path);
933 return ret;
934}
935
936/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500937 * Back reference rules. Back refs have three main goals:
938 *
939 * 1) differentiate between all holders of references to an extent so that
940 * when a reference is dropped we can make sure it was a valid reference
941 * before freeing the extent.
942 *
943 * 2) Provide enough information to quickly find the holders of an extent
944 * if we notice a given block is corrupted or bad.
945 *
946 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
947 * maintenance. This is actually the same as #2, but with a slightly
948 * different use case.
949 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 * There are two kinds of back refs. The implicit back refs is optimized
951 * for pointers in non-shared tree blocks. For a given pointer in a block,
952 * back refs of this kind provide information about the block's owner tree
953 * and the pointer's key. These information allow us to find the block by
954 * b-tree searching. The full back refs is for pointers in tree blocks not
955 * referenced by their owner trees. The location of tree block is recorded
956 * in the back refs. Actually the full back refs is generic, and can be
957 * used in all cases the implicit back refs is used. The major shortcoming
958 * of the full back refs is its overhead. Every time a tree block gets
959 * COWed, we have to update back refs entry for all pointers in it.
960 *
961 * For a newly allocated tree block, we use implicit back refs for
962 * pointers in it. This means most tree related operations only involve
963 * implicit back refs. For a tree block created in old transaction, the
964 * only way to drop a reference to it is COW it. So we can detect the
965 * event that tree block loses its owner tree's reference and do the
966 * back refs conversion.
967 *
Nicholas D Steeves01327612016-05-19 21:18:45 -0400968 * When a tree block is COWed through a tree, there are four cases:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400969 *
970 * The reference count of the block is one and the tree is the block's
971 * owner tree. Nothing to do in this case.
972 *
973 * The reference count of the block is one and the tree is not the
974 * block's owner tree. In this case, full back refs is used for pointers
975 * in the block. Remove these full back refs, add implicit back refs for
976 * every pointers in the new block.
977 *
978 * The reference count of the block is greater than one and the tree is
979 * the block's owner tree. In this case, implicit back refs is used for
980 * pointers in the block. Add full back refs for every pointers in the
981 * block, increase lower level extents' reference counts. The original
982 * implicit back refs are entailed to the new block.
983 *
984 * The reference count of the block is greater than one and the tree is
985 * not the block's owner tree. Add implicit back refs for every pointer in
986 * the new block, increase lower level extents' reference count.
987 *
988 * Back Reference Key composing:
989 *
990 * The key objectid corresponds to the first byte in the extent,
991 * The key type is used to differentiate between types of back refs.
992 * There are different meanings of the key offset for different types
993 * of back refs.
994 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500995 * File extents can be referenced by:
996 *
997 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400998 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500999 * - different offsets inside a file (bookend extents in file.c)
1000 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001001 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001002 *
1003 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -05001004 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001005 * - original offset in the file
1006 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -04001007 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001008 * The key offset for the implicit back refs is hash of the first
1009 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001010 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001011 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001012 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001013 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001014 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001015 * The key offset for the implicit back refs is the first byte of
1016 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001017 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001018 * When a file extent is allocated, The implicit back refs is used.
1019 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001020 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021 * (root_key.objectid, inode objectid, offset in file, 1)
1022 *
1023 * When a file extent is removed file truncation, we find the
1024 * corresponding implicit back refs and check the following fields:
1025 *
1026 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -05001027 *
1028 * Btree extents can be referenced by:
1029 *
1030 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -05001031 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001032 * Both the implicit back refs and the full back refs for tree blocks
1033 * only consist of key. The key offset for the implicit back refs is
1034 * objectid of block's owner tree. The key offset for the full back refs
1035 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001036 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037 * When implicit back refs is used, information about the lowest key and
1038 * level of the tree block are required. These information are stored in
1039 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001040 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001041
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001042#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1043static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001044 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001045 struct btrfs_path *path,
1046 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -05001047{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001048 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001049 struct btrfs_extent_item *item;
1050 struct btrfs_extent_item_v0 *ei0;
1051 struct btrfs_extent_ref_v0 *ref0;
1052 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -04001053 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001054 struct btrfs_key key;
1055 struct btrfs_key found_key;
1056 u32 new_size = sizeof(*item);
1057 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -05001058 int ret;
1059
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001060 leaf = path->nodes[0];
1061 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -05001062
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001063 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1064 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1065 struct btrfs_extent_item_v0);
1066 refs = btrfs_extent_refs_v0(leaf, ei0);
1067
1068 if (owner == (u64)-1) {
1069 while (1) {
1070 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1071 ret = btrfs_next_leaf(root, path);
1072 if (ret < 0)
1073 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001074 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001075 leaf = path->nodes[0];
1076 }
1077 btrfs_item_key_to_cpu(leaf, &found_key,
1078 path->slots[0]);
1079 BUG_ON(key.objectid != found_key.objectid);
1080 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1081 path->slots[0]++;
1082 continue;
1083 }
1084 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1085 struct btrfs_extent_ref_v0);
1086 owner = btrfs_ref_objectid_v0(leaf, ref0);
1087 break;
1088 }
1089 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001090 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001091
1092 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1093 new_size += sizeof(*bi);
1094
1095 new_size -= sizeof(*ei0);
1096 ret = btrfs_search_slot(trans, root, &key, path,
1097 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001098 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001099 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001100 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001101
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001102 btrfs_extend_item(fs_info, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001103
1104 leaf = path->nodes[0];
1105 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1106 btrfs_set_extent_refs(leaf, item, refs);
1107 /* FIXME: get real generation */
1108 btrfs_set_extent_generation(leaf, item, 0);
1109 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1110 btrfs_set_extent_flags(leaf, item,
1111 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1112 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1113 bi = (struct btrfs_tree_block_info *)(item + 1);
1114 /* FIXME: get first key of the block */
David Sterbab159fa22016-11-08 18:09:03 +01001115 memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001116 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1117 } else {
1118 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1119 }
1120 btrfs_mark_buffer_dirty(leaf);
1121 return 0;
1122}
1123#endif
1124
Liu Bo167ce952017-08-18 15:15:18 -06001125/*
1126 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
1127 * is_data == BTRFS_REF_TYPE_DATA, data type is requried,
1128 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
1129 */
1130int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
1131 struct btrfs_extent_inline_ref *iref,
1132 enum btrfs_inline_ref_type is_data)
1133{
1134 int type = btrfs_extent_inline_ref_type(eb, iref);
Liu Bo64ecdb62017-08-18 15:15:24 -06001135 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
Liu Bo167ce952017-08-18 15:15:18 -06001136
1137 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1138 type == BTRFS_SHARED_BLOCK_REF_KEY ||
1139 type == BTRFS_SHARED_DATA_REF_KEY ||
1140 type == BTRFS_EXTENT_DATA_REF_KEY) {
1141 if (is_data == BTRFS_REF_TYPE_BLOCK) {
Liu Bo64ecdb62017-08-18 15:15:24 -06001142 if (type == BTRFS_TREE_BLOCK_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -06001143 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -06001144 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1145 ASSERT(eb->fs_info);
1146 /*
1147 * Every shared one has parent tree
1148 * block, which must be aligned to
1149 * nodesize.
1150 */
1151 if (offset &&
1152 IS_ALIGNED(offset, eb->fs_info->nodesize))
1153 return type;
1154 }
Liu Bo167ce952017-08-18 15:15:18 -06001155 } else if (is_data == BTRFS_REF_TYPE_DATA) {
Liu Bo64ecdb62017-08-18 15:15:24 -06001156 if (type == BTRFS_EXTENT_DATA_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -06001157 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -06001158 if (type == BTRFS_SHARED_DATA_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 {
1170 ASSERT(is_data == BTRFS_REF_TYPE_ANY);
1171 return type;
1172 }
1173 }
1174
1175 btrfs_print_leaf((struct extent_buffer *)eb);
1176 btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d",
1177 eb->start, type);
1178 WARN_ON(1);
1179
1180 return BTRFS_REF_TYPE_INVALID;
1181}
1182
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001183static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1184{
1185 u32 high_crc = ~(u32)0;
1186 u32 low_crc = ~(u32)0;
1187 __le64 lenum;
1188
1189 lenum = cpu_to_le64(root_objectid);
Nikolay Borisov9678c542018-01-08 11:45:05 +02001190 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001191 lenum = cpu_to_le64(owner);
Nikolay Borisov9678c542018-01-08 11:45:05 +02001192 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001193 lenum = cpu_to_le64(offset);
Nikolay Borisov9678c542018-01-08 11:45:05 +02001194 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001195
1196 return ((u64)high_crc << 31) ^ (u64)low_crc;
1197}
1198
1199static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1200 struct btrfs_extent_data_ref *ref)
1201{
1202 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1203 btrfs_extent_data_ref_objectid(leaf, ref),
1204 btrfs_extent_data_ref_offset(leaf, ref));
1205}
1206
1207static int match_extent_data_ref(struct extent_buffer *leaf,
1208 struct btrfs_extent_data_ref *ref,
1209 u64 root_objectid, u64 owner, u64 offset)
1210{
1211 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1212 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1213 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1214 return 0;
1215 return 1;
1216}
1217
1218static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001219 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001220 struct btrfs_path *path,
1221 u64 bytenr, u64 parent,
1222 u64 root_objectid,
1223 u64 owner, u64 offset)
1224{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001225 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001226 struct btrfs_key key;
1227 struct btrfs_extent_data_ref *ref;
1228 struct extent_buffer *leaf;
1229 u32 nritems;
1230 int ret;
1231 int recow;
1232 int err = -ENOENT;
1233
1234 key.objectid = bytenr;
1235 if (parent) {
1236 key.type = BTRFS_SHARED_DATA_REF_KEY;
1237 key.offset = parent;
1238 } else {
1239 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1240 key.offset = hash_extent_data_ref(root_objectid,
1241 owner, offset);
1242 }
1243again:
1244 recow = 0;
1245 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1246 if (ret < 0) {
1247 err = ret;
1248 goto fail;
1249 }
1250
1251 if (parent) {
1252 if (!ret)
1253 return 0;
1254#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1255 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001256 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001257 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1258 if (ret < 0) {
1259 err = ret;
1260 goto fail;
1261 }
1262 if (!ret)
1263 return 0;
1264#endif
1265 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001266 }
1267
1268 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001269 nritems = btrfs_header_nritems(leaf);
1270 while (1) {
1271 if (path->slots[0] >= nritems) {
1272 ret = btrfs_next_leaf(root, path);
1273 if (ret < 0)
1274 err = ret;
1275 if (ret)
1276 goto fail;
1277
1278 leaf = path->nodes[0];
1279 nritems = btrfs_header_nritems(leaf);
1280 recow = 1;
1281 }
1282
1283 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1284 if (key.objectid != bytenr ||
1285 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1286 goto fail;
1287
1288 ref = btrfs_item_ptr(leaf, path->slots[0],
1289 struct btrfs_extent_data_ref);
1290
1291 if (match_extent_data_ref(leaf, ref, root_objectid,
1292 owner, offset)) {
1293 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001294 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001295 goto again;
1296 }
1297 err = 0;
1298 break;
1299 }
1300 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001301 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001302fail:
1303 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001304}
1305
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001306static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001307 struct btrfs_path *path,
1308 u64 bytenr, u64 parent,
1309 u64 root_objectid, u64 owner,
1310 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001311{
Nikolay Borisov62b895a2018-06-20 15:48:44 +03001312 struct btrfs_root *root = trans->fs_info->extent_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04001313 struct btrfs_key key;
1314 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001315 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001316 u32 num_refs;
1317 int ret;
1318
1319 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001320 if (parent) {
1321 key.type = BTRFS_SHARED_DATA_REF_KEY;
1322 key.offset = parent;
1323 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001324 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001325 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1326 key.offset = hash_extent_data_ref(root_objectid,
1327 owner, offset);
1328 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001329 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001330
1331 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1332 if (ret && ret != -EEXIST)
1333 goto fail;
1334
1335 leaf = path->nodes[0];
1336 if (parent) {
1337 struct btrfs_shared_data_ref *ref;
1338 ref = btrfs_item_ptr(leaf, path->slots[0],
1339 struct btrfs_shared_data_ref);
1340 if (ret == 0) {
1341 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1342 } else {
1343 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1344 num_refs += refs_to_add;
1345 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1346 }
1347 } else {
1348 struct btrfs_extent_data_ref *ref;
1349 while (ret == -EEXIST) {
1350 ref = btrfs_item_ptr(leaf, path->slots[0],
1351 struct btrfs_extent_data_ref);
1352 if (match_extent_data_ref(leaf, ref, root_objectid,
1353 owner, offset))
1354 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001355 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001356 key.offset++;
1357 ret = btrfs_insert_empty_item(trans, root, path, &key,
1358 size);
1359 if (ret && ret != -EEXIST)
1360 goto fail;
1361
1362 leaf = path->nodes[0];
1363 }
1364 ref = btrfs_item_ptr(leaf, path->slots[0],
1365 struct btrfs_extent_data_ref);
1366 if (ret == 0) {
1367 btrfs_set_extent_data_ref_root(leaf, ref,
1368 root_objectid);
1369 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1370 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1371 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1372 } else {
1373 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1374 num_refs += refs_to_add;
1375 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1376 }
1377 }
1378 btrfs_mark_buffer_dirty(leaf);
1379 ret = 0;
1380fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001381 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001382 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001383}
1384
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001385static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001386 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -07001387 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001388{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001389 struct btrfs_key key;
1390 struct btrfs_extent_data_ref *ref1 = NULL;
1391 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001392 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001393 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001394 int ret = 0;
1395
1396 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1398
1399 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1400 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1401 struct btrfs_extent_data_ref);
1402 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1403 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1404 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1405 struct btrfs_shared_data_ref);
1406 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1407#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1408 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1409 struct btrfs_extent_ref_v0 *ref0;
1410 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1411 struct btrfs_extent_ref_v0);
1412 num_refs = btrfs_ref_count_v0(leaf, ref0);
1413#endif
1414 } else {
1415 BUG();
1416 }
1417
Chris Mason56bec292009-03-13 10:10:06 -04001418 BUG_ON(num_refs < refs_to_drop);
1419 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001420
Zheng Yan31840ae2008-09-23 13:14:14 -04001421 if (num_refs == 0) {
Nikolay Borisove9f62902018-06-20 15:48:46 +03001422 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -07001423 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001424 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001425 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1426 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1427 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1428 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1429#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1430 else {
1431 struct btrfs_extent_ref_v0 *ref0;
1432 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1433 struct btrfs_extent_ref_v0);
1434 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1435 }
1436#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001437 btrfs_mark_buffer_dirty(leaf);
1438 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001439 return ret;
1440}
1441
Zhaolei9ed0dea2015-08-06 22:16:24 +08001442static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001443 struct btrfs_extent_inline_ref *iref)
1444{
1445 struct btrfs_key key;
1446 struct extent_buffer *leaf;
1447 struct btrfs_extent_data_ref *ref1;
1448 struct btrfs_shared_data_ref *ref2;
1449 u32 num_refs = 0;
Liu Bo3de28d52017-08-18 15:15:19 -06001450 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001451
1452 leaf = path->nodes[0];
1453 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1454 if (iref) {
Liu Bo3de28d52017-08-18 15:15:19 -06001455 /*
1456 * If type is invalid, we should have bailed out earlier than
1457 * this call.
1458 */
1459 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
1460 ASSERT(type != BTRFS_REF_TYPE_INVALID);
1461 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001462 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1463 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1464 } else {
1465 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1466 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1467 }
1468 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1469 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1470 struct btrfs_extent_data_ref);
1471 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1472 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1473 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1474 struct btrfs_shared_data_ref);
1475 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1476#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1477 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1478 struct btrfs_extent_ref_v0 *ref0;
1479 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1480 struct btrfs_extent_ref_v0);
1481 num_refs = btrfs_ref_count_v0(leaf, ref0);
1482#endif
1483 } else {
1484 WARN_ON(1);
1485 }
1486 return num_refs;
1487}
1488
1489static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001490 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001491 struct btrfs_path *path,
1492 u64 bytenr, u64 parent,
1493 u64 root_objectid)
1494{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001495 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001496 struct btrfs_key key;
1497 int ret;
1498
1499 key.objectid = bytenr;
1500 if (parent) {
1501 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1502 key.offset = parent;
1503 } else {
1504 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1505 key.offset = root_objectid;
1506 }
1507
1508 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1509 if (ret > 0)
1510 ret = -ENOENT;
1511#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1512 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001513 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001514 key.type = BTRFS_EXTENT_REF_V0_KEY;
1515 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1516 if (ret > 0)
1517 ret = -ENOENT;
1518 }
1519#endif
1520 return ret;
1521}
1522
1523static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001524 struct btrfs_path *path,
1525 u64 bytenr, u64 parent,
1526 u64 root_objectid)
1527{
1528 struct btrfs_key key;
1529 int ret;
1530
1531 key.objectid = bytenr;
1532 if (parent) {
1533 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1534 key.offset = parent;
1535 } else {
1536 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1537 key.offset = root_objectid;
1538 }
1539
Nikolay Borisov10728402018-06-20 15:48:43 +03001540 ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001541 path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001542 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001543 return ret;
1544}
1545
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001546static inline int extent_ref_type(u64 parent, u64 owner)
1547{
1548 int type;
1549 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1550 if (parent > 0)
1551 type = BTRFS_SHARED_BLOCK_REF_KEY;
1552 else
1553 type = BTRFS_TREE_BLOCK_REF_KEY;
1554 } else {
1555 if (parent > 0)
1556 type = BTRFS_SHARED_DATA_REF_KEY;
1557 else
1558 type = BTRFS_EXTENT_DATA_REF_KEY;
1559 }
1560 return type;
1561}
1562
Yan Zheng2c47e6052009-06-27 21:07:35 -04001563static int find_next_key(struct btrfs_path *path, int level,
1564 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001565
1566{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001567 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001568 if (!path->nodes[level])
1569 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001570 if (path->slots[level] + 1 >=
1571 btrfs_header_nritems(path->nodes[level]))
1572 continue;
1573 if (level == 0)
1574 btrfs_item_key_to_cpu(path->nodes[level], key,
1575 path->slots[level] + 1);
1576 else
1577 btrfs_node_key_to_cpu(path->nodes[level], key,
1578 path->slots[level] + 1);
1579 return 0;
1580 }
1581 return 1;
1582}
1583
1584/*
1585 * look for inline back ref. if back ref is found, *ref_ret is set
1586 * to the address of inline back ref, and 0 is returned.
1587 *
1588 * if back ref isn't found, *ref_ret is set to the address where it
1589 * should be inserted, and -ENOENT is returned.
1590 *
1591 * if insert is true and there are too many inline back refs, the path
1592 * points to the extent item, and -EAGAIN is returned.
1593 *
1594 * NOTE: inline back refs are ordered in the same way that back ref
1595 * items in the tree are ordered.
1596 */
1597static noinline_for_stack
1598int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001599 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001600 struct btrfs_path *path,
1601 struct btrfs_extent_inline_ref **ref_ret,
1602 u64 bytenr, u64 num_bytes,
1603 u64 parent, u64 root_objectid,
1604 u64 owner, u64 offset, int insert)
1605{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001606 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001607 struct btrfs_key key;
1608 struct extent_buffer *leaf;
1609 struct btrfs_extent_item *ei;
1610 struct btrfs_extent_inline_ref *iref;
1611 u64 flags;
1612 u64 item_size;
1613 unsigned long ptr;
1614 unsigned long end;
1615 int extra_size;
1616 int type;
1617 int want;
1618 int ret;
1619 int err = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001620 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Liu Bo3de28d52017-08-18 15:15:19 -06001621 int needed;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001622
1623 key.objectid = bytenr;
1624 key.type = BTRFS_EXTENT_ITEM_KEY;
1625 key.offset = num_bytes;
1626
1627 want = extent_ref_type(parent, owner);
1628 if (insert) {
1629 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001630 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001631 } else
1632 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001633
1634 /*
Nikolay Borisov16d1c062018-06-18 14:59:26 +03001635 * Owner is our level, so we can just add one to get the level for the
1636 * block we are interested in.
Josef Bacik3173a182013-03-07 14:22:04 -05001637 */
1638 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1639 key.type = BTRFS_METADATA_ITEM_KEY;
1640 key.offset = owner;
1641 }
1642
1643again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001644 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1645 if (ret < 0) {
1646 err = ret;
1647 goto out;
1648 }
Josef Bacik3173a182013-03-07 14:22:04 -05001649
1650 /*
1651 * We may be a newly converted file system which still has the old fat
1652 * extent entries for metadata, so try and see if we have one of those.
1653 */
1654 if (ret > 0 && skinny_metadata) {
1655 skinny_metadata = false;
1656 if (path->slots[0]) {
1657 path->slots[0]--;
1658 btrfs_item_key_to_cpu(path->nodes[0], &key,
1659 path->slots[0]);
1660 if (key.objectid == bytenr &&
1661 key.type == BTRFS_EXTENT_ITEM_KEY &&
1662 key.offset == num_bytes)
1663 ret = 0;
1664 }
1665 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +01001666 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05001667 key.type = BTRFS_EXTENT_ITEM_KEY;
1668 key.offset = num_bytes;
1669 btrfs_release_path(path);
1670 goto again;
1671 }
1672 }
1673
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001674 if (ret && !insert) {
1675 err = -ENOENT;
1676 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301677 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -05001678 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -05001679 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001680 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001681
1682 leaf = path->nodes[0];
1683 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1684#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1685 if (item_size < sizeof(*ei)) {
1686 if (!insert) {
1687 err = -ENOENT;
1688 goto out;
1689 }
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001690 ret = convert_extent_item_v0(trans, fs_info, path, owner,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001691 extra_size);
1692 if (ret < 0) {
1693 err = ret;
1694 goto out;
1695 }
1696 leaf = path->nodes[0];
1697 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1698 }
1699#endif
1700 BUG_ON(item_size < sizeof(*ei));
1701
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001702 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1703 flags = btrfs_extent_flags(leaf, ei);
1704
1705 ptr = (unsigned long)(ei + 1);
1706 end = (unsigned long)ei + item_size;
1707
Josef Bacik3173a182013-03-07 14:22:04 -05001708 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001709 ptr += sizeof(struct btrfs_tree_block_info);
1710 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001711 }
1712
Liu Bo3de28d52017-08-18 15:15:19 -06001713 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
1714 needed = BTRFS_REF_TYPE_DATA;
1715 else
1716 needed = BTRFS_REF_TYPE_BLOCK;
1717
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001718 err = -ENOENT;
1719 while (1) {
1720 if (ptr >= end) {
1721 WARN_ON(ptr > end);
1722 break;
1723 }
1724 iref = (struct btrfs_extent_inline_ref *)ptr;
Liu Bo3de28d52017-08-18 15:15:19 -06001725 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
1726 if (type == BTRFS_REF_TYPE_INVALID) {
1727 err = -EINVAL;
1728 goto out;
1729 }
1730
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001731 if (want < type)
1732 break;
1733 if (want > type) {
1734 ptr += btrfs_extent_inline_ref_size(type);
1735 continue;
1736 }
1737
1738 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1739 struct btrfs_extent_data_ref *dref;
1740 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1741 if (match_extent_data_ref(leaf, dref, root_objectid,
1742 owner, offset)) {
1743 err = 0;
1744 break;
1745 }
1746 if (hash_extent_data_ref_item(leaf, dref) <
1747 hash_extent_data_ref(root_objectid, owner, offset))
1748 break;
1749 } else {
1750 u64 ref_offset;
1751 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1752 if (parent > 0) {
1753 if (parent == ref_offset) {
1754 err = 0;
1755 break;
1756 }
1757 if (ref_offset < parent)
1758 break;
1759 } else {
1760 if (root_objectid == ref_offset) {
1761 err = 0;
1762 break;
1763 }
1764 if (ref_offset < root_objectid)
1765 break;
1766 }
1767 }
1768 ptr += btrfs_extent_inline_ref_size(type);
1769 }
1770 if (err == -ENOENT && insert) {
1771 if (item_size + extra_size >=
1772 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1773 err = -EAGAIN;
1774 goto out;
1775 }
1776 /*
1777 * To add new inline back ref, we have to make sure
1778 * there is no corresponding back ref item.
1779 * For simplicity, we just do not add new inline back
1780 * ref if there is any kind of item for this block
1781 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001782 if (find_next_key(path, 0, &key) == 0 &&
1783 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001784 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001785 err = -EAGAIN;
1786 goto out;
1787 }
1788 }
1789 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1790out:
Yan Zheng85d41982009-06-11 08:51:10 -04001791 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001792 path->keep_locks = 0;
1793 btrfs_unlock_up_safe(path, 1);
1794 }
1795 return err;
1796}
1797
1798/*
1799 * helper to add new inline back ref
1800 */
1801static noinline_for_stack
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001802void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001803 struct btrfs_path *path,
1804 struct btrfs_extent_inline_ref *iref,
1805 u64 parent, u64 root_objectid,
1806 u64 owner, u64 offset, int refs_to_add,
1807 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001808{
1809 struct extent_buffer *leaf;
1810 struct btrfs_extent_item *ei;
1811 unsigned long ptr;
1812 unsigned long end;
1813 unsigned long item_offset;
1814 u64 refs;
1815 int size;
1816 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001817
1818 leaf = path->nodes[0];
1819 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1820 item_offset = (unsigned long)iref - (unsigned long)ei;
1821
1822 type = extent_ref_type(parent, owner);
1823 size = btrfs_extent_inline_ref_size(type);
1824
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001825 btrfs_extend_item(fs_info, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001826
1827 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1828 refs = btrfs_extent_refs(leaf, ei);
1829 refs += refs_to_add;
1830 btrfs_set_extent_refs(leaf, ei, refs);
1831 if (extent_op)
1832 __run_delayed_extent_op(extent_op, leaf, ei);
1833
1834 ptr = (unsigned long)ei + item_offset;
1835 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1836 if (ptr < end - size)
1837 memmove_extent_buffer(leaf, ptr + size, ptr,
1838 end - size - ptr);
1839
1840 iref = (struct btrfs_extent_inline_ref *)ptr;
1841 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1842 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1843 struct btrfs_extent_data_ref *dref;
1844 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1845 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1846 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1847 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1848 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1849 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1850 struct btrfs_shared_data_ref *sref;
1851 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1852 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1853 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1854 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1855 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1856 } else {
1857 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1858 }
1859 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001860}
1861
1862static int lookup_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001863 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001864 struct btrfs_path *path,
1865 struct btrfs_extent_inline_ref **ref_ret,
1866 u64 bytenr, u64 num_bytes, u64 parent,
1867 u64 root_objectid, u64 owner, u64 offset)
1868{
1869 int ret;
1870
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001871 ret = lookup_inline_extent_backref(trans, fs_info, path, ref_ret,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001872 bytenr, num_bytes, parent,
1873 root_objectid, owner, offset, 0);
1874 if (ret != -ENOENT)
1875 return ret;
1876
David Sterbab3b4aa72011-04-21 01:20:15 +02001877 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878 *ref_ret = NULL;
1879
1880 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001881 ret = lookup_tree_block_ref(trans, fs_info, path, bytenr,
1882 parent, root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001883 } else {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001884 ret = lookup_extent_data_ref(trans, fs_info, path, bytenr,
1885 parent, root_objectid, owner,
1886 offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001887 }
1888 return ret;
1889}
1890
1891/*
1892 * helper to update/remove inline back ref
1893 */
1894static noinline_for_stack
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001895void update_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001896 struct btrfs_path *path,
1897 struct btrfs_extent_inline_ref *iref,
1898 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001899 struct btrfs_delayed_extent_op *extent_op,
1900 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001901{
1902 struct extent_buffer *leaf;
1903 struct btrfs_extent_item *ei;
1904 struct btrfs_extent_data_ref *dref = NULL;
1905 struct btrfs_shared_data_ref *sref = NULL;
1906 unsigned long ptr;
1907 unsigned long end;
1908 u32 item_size;
1909 int size;
1910 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001911 u64 refs;
1912
1913 leaf = path->nodes[0];
1914 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1915 refs = btrfs_extent_refs(leaf, ei);
1916 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1917 refs += refs_to_mod;
1918 btrfs_set_extent_refs(leaf, ei, refs);
1919 if (extent_op)
1920 __run_delayed_extent_op(extent_op, leaf, ei);
1921
Liu Bo3de28d52017-08-18 15:15:19 -06001922 /*
1923 * If type is invalid, we should have bailed out after
1924 * lookup_inline_extent_backref().
1925 */
1926 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1927 ASSERT(type != BTRFS_REF_TYPE_INVALID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001928
1929 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1930 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1931 refs = btrfs_extent_data_ref_count(leaf, dref);
1932 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1933 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1934 refs = btrfs_shared_data_ref_count(leaf, sref);
1935 } else {
1936 refs = 1;
1937 BUG_ON(refs_to_mod != -1);
1938 }
1939
1940 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1941 refs += refs_to_mod;
1942
1943 if (refs > 0) {
1944 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1945 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1946 else
1947 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1948 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001949 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001950 size = btrfs_extent_inline_ref_size(type);
1951 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1952 ptr = (unsigned long)iref;
1953 end = (unsigned long)ei + item_size;
1954 if (ptr + size < end)
1955 memmove_extent_buffer(leaf, ptr, ptr + size,
1956 end - ptr - size);
1957 item_size -= size;
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001958 btrfs_truncate_item(fs_info, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001959 }
1960 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001961}
1962
1963static noinline_for_stack
1964int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001965 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001966 struct btrfs_path *path,
1967 u64 bytenr, u64 num_bytes, u64 parent,
1968 u64 root_objectid, u64 owner,
1969 u64 offset, int refs_to_add,
1970 struct btrfs_delayed_extent_op *extent_op)
1971{
1972 struct btrfs_extent_inline_ref *iref;
1973 int ret;
1974
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001975 ret = lookup_inline_extent_backref(trans, fs_info, path, &iref,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001976 bytenr, num_bytes, parent,
1977 root_objectid, owner, offset, 1);
1978 if (ret == 0) {
1979 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001980 update_inline_extent_backref(fs_info, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001981 refs_to_add, extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001982 } else if (ret == -ENOENT) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001983 setup_inline_extent_backref(fs_info, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001984 root_objectid, owner, offset,
1985 refs_to_add, extent_op);
1986 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001987 }
1988 return ret;
1989}
1990
1991static int insert_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001992 struct btrfs_path *path,
1993 u64 bytenr, u64 parent, u64 root_objectid,
1994 u64 owner, u64 offset, int refs_to_add)
1995{
1996 int ret;
1997 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1998 BUG_ON(refs_to_add != 1);
Nikolay Borisov10728402018-06-20 15:48:43 +03001999 ret = insert_tree_block_ref(trans, path, bytenr, parent,
2000 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002001 } else {
Nikolay Borisov62b895a2018-06-20 15:48:44 +03002002 ret = insert_extent_data_ref(trans, path, bytenr, parent,
2003 root_objectid, owner, offset,
2004 refs_to_add);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002005 }
2006 return ret;
2007}
2008
2009static int remove_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002010 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002011 struct btrfs_path *path,
2012 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07002013 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002014{
Jeff Mahoney143bede2012-03-01 14:56:26 +01002015 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002016
2017 BUG_ON(!is_data && refs_to_drop != 1);
2018 if (iref) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002019 update_inline_extent_backref(fs_info, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07002020 -refs_to_drop, NULL, last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002021 } else if (is_data) {
Nikolay Borisove9f62902018-06-20 15:48:46 +03002022 ret = remove_extent_data_ref(trans, path, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07002023 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002024 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07002025 *last_ref = 1;
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002026 ret = btrfs_del_item(trans, fs_info->extent_root, path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002027 }
2028 return ret;
2029}
2030
Jeff Mahoney86557862015-06-15 09:41:16 -04002031#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002032static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
2033 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05002034{
Jeff Mahoney86557862015-06-15 09:41:16 -04002035 int j, ret = 0;
2036 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002037 u64 aligned_start = ALIGN(start, 1 << 9);
2038
2039 if (WARN_ON(start != aligned_start)) {
2040 len -= aligned_start - start;
2041 len = round_down(len, 1 << 9);
2042 start = aligned_start;
2043 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002044
2045 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04002046
2047 if (!len)
2048 return 0;
2049
2050 end = start + len;
2051 bytes_left = len;
2052
2053 /* Skip any superblocks on this device. */
2054 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
2055 u64 sb_start = btrfs_sb_offset(j);
2056 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
2057 u64 size = sb_start - start;
2058
2059 if (!in_range(sb_start, start, bytes_left) &&
2060 !in_range(sb_end, start, bytes_left) &&
2061 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
2062 continue;
2063
2064 /*
2065 * Superblock spans beginning of range. Adjust start and
2066 * try again.
2067 */
2068 if (sb_start <= start) {
2069 start += sb_end - start;
2070 if (start > end) {
2071 bytes_left = 0;
2072 break;
2073 }
2074 bytes_left = end - start;
2075 continue;
2076 }
2077
2078 if (size) {
2079 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
2080 GFP_NOFS, 0);
2081 if (!ret)
2082 *discarded_bytes += size;
2083 else if (ret != -EOPNOTSUPP)
2084 return ret;
2085 }
2086
2087 start = sb_end;
2088 if (start > end) {
2089 bytes_left = 0;
2090 break;
2091 }
2092 bytes_left = end - start;
2093 }
2094
2095 if (bytes_left) {
2096 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002097 GFP_NOFS, 0);
2098 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04002099 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002100 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002101 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05002102}
Chris Mason15916de2008-11-19 21:17:22 -05002103
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002104int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00002105 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05002106{
Liu Hui1f3c79a2009-01-05 15:57:51 -05002107 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00002108 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02002109 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002110
Christoph Hellwige244a0a2009-10-14 09:24:59 -04002111
Filipe Manana29992412016-05-27 17:42:05 +01002112 /*
2113 * Avoid races with device replace and make sure our bbio has devices
2114 * associated to its stripes that don't go away while we are discarding.
2115 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002116 btrfs_bio_counter_inc_blocked(fs_info);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002117 /* Tell the block device(s) that the sectors can be discarded */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002118 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
2119 &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002120 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05002121 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02002122 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002123 int i;
2124
Liu Hui1f3c79a2009-01-05 15:57:51 -05002125
Jan Schmidta1d3c472011-08-04 17:15:33 +02002126 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002127 u64 bytes;
Anand Jain38b5f682017-11-29 18:53:43 +08002128 struct request_queue *req_q;
2129
Filipe Manana627e0872018-01-30 18:40:22 +00002130 if (!stripe->dev->bdev) {
2131 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
2132 continue;
2133 }
Anand Jain38b5f682017-11-29 18:53:43 +08002134 req_q = bdev_get_queue(stripe->dev->bdev);
2135 if (!blk_queue_discard(req_q))
Josef Bacikd5e20032011-08-04 14:52:27 +00002136 continue;
2137
Li Dongyang5378e602011-03-24 10:24:27 +00002138 ret = btrfs_issue_discard(stripe->dev->bdev,
2139 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002140 stripe->length,
2141 &bytes);
Li Dongyang5378e602011-03-24 10:24:27 +00002142 if (!ret)
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002143 discarded_bytes += bytes;
Li Dongyang5378e602011-03-24 10:24:27 +00002144 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002145 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00002146
2147 /*
2148 * Just in case we get back EOPNOTSUPP for some reason,
2149 * just ignore the return value so we don't screw up
2150 * people calling discard_extent.
2151 */
2152 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002153 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08002154 btrfs_put_bbio(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002155 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002156 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00002157
2158 if (actual_bytes)
2159 *actual_bytes = discarded_bytes;
2160
Liu Hui1f3c79a2009-01-05 15:57:51 -05002161
David Woodhouse53b381b2013-01-29 18:40:14 -05002162 if (ret == -EOPNOTSUPP)
2163 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002164 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002165}
2166
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002167/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002168int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002169 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002170 u64 bytenr, u64 num_bytes, u64 parent,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002171 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04002172{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002173 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07002174 int old_ref_mod, new_ref_mod;
Zheng Yan31840ae2008-09-23 13:14:14 -04002175 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002176
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002177 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
2178 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04002179
Josef Bacikfd708b82017-09-29 15:43:50 -04002180 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent, root_objectid,
2181 owner, offset, BTRFS_ADD_DELAYED_REF);
2182
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002183 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002184 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07002185 num_bytes, parent,
2186 root_objectid, (int)owner,
2187 BTRFS_ADD_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07002188 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002189 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002190 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07002191 num_bytes, parent,
2192 root_objectid, owner, offset,
Omar Sandovald7eae342017-06-06 16:45:31 -07002193 0, BTRFS_ADD_DELAYED_REF,
2194 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002195 }
Omar Sandovald7eae342017-06-06 16:45:31 -07002196
Nikolay Borisov29d2b842018-03-30 12:58:47 +03002197 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0) {
2198 bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
2199
2200 add_pinned_bytes(fs_info, -num_bytes, metadata, root_objectid);
2201 }
Omar Sandovald7eae342017-06-06 16:45:31 -07002202
Zheng Yan31840ae2008-09-23 13:14:14 -04002203 return ret;
2204}
2205
Nikolay Borisovbd3c6852018-06-18 14:59:25 +03002206/*
2207 * __btrfs_inc_extent_ref - insert backreference for a given extent
2208 *
2209 * @trans: Handle of transaction
2210 *
2211 * @node: The delayed ref node used to get the bytenr/length for
2212 * extent whose references are incremented.
2213 *
2214 * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
2215 * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
2216 * bytenr of the parent block. Since new extents are always
2217 * created with indirect references, this will only be the case
2218 * when relocating a shared extent. In that case, root_objectid
2219 * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
2220 * be 0
2221 *
2222 * @root_objectid: The id of the root where this modification has originated,
2223 * this can be either one of the well-known metadata trees or
2224 * the subvolume id which references this extent.
2225 *
2226 * @owner: For data extents it is the inode number of the owning file.
2227 * For metadata extents this parameter holds the level in the
2228 * tree of the extent.
2229 *
2230 * @offset: For metadata extents the offset is ignored and is currently
2231 * always passed as 0. For data extents it is the fileoffset
2232 * this extent belongs to.
2233 *
2234 * @refs_to_add Number of references to add
2235 *
2236 * @extent_op Pointer to a structure, holding information necessary when
2237 * updating a tree block's flags
2238 *
2239 */
Chris Mason925baed2008-06-25 16:01:30 -04002240static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002241 struct btrfs_fs_info *fs_info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002242 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002243 u64 parent, u64 root_objectid,
2244 u64 owner, u64 offset, int refs_to_add,
2245 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04002246{
Chris Mason5caf2a02007-04-02 11:20:42 -04002247 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002248 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04002249 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07002250 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002251 u64 bytenr = node->bytenr;
2252 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002253 u64 refs;
2254 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05002255
Chris Mason5caf2a02007-04-02 11:20:42 -04002256 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002257 if (!path)
2258 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04002259
David Sterbae4058b52015-11-27 16:31:35 +01002260 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002261 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002262 /* this will setup the path even if it fails to insert the back ref */
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002263 ret = insert_inline_extent_backref(trans, fs_info, path, bytenr,
2264 num_bytes, parent, root_objectid,
2265 owner, offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002266 refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08002267 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002268 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07002269
2270 /*
2271 * Ok we had -EAGAIN which means we didn't have space to insert and
2272 * inline extent ref, so just update the reference count and add a
2273 * normal backref.
2274 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002275 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07002276 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002277 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2278 refs = btrfs_extent_refs(leaf, item);
2279 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2280 if (extent_op)
2281 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002282
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002283 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002284 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002285
David Sterbae4058b52015-11-27 16:31:35 +01002286 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002287 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04002288 /* now insert the actual backref */
Nikolay Borisov37593412018-06-20 15:48:45 +03002289 ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid,
2290 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002291 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04002292 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002293out:
Chris Mason74493f72007-12-11 09:25:06 -05002294 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08002295 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002296}
2297
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002298static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002299 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002300 struct btrfs_delayed_ref_node *node,
2301 struct btrfs_delayed_extent_op *extent_op,
2302 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002303{
Chris Mason56bec292009-03-13 10:10:06 -04002304 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002305 struct btrfs_delayed_data_ref *ref;
2306 struct btrfs_key ins;
2307 u64 parent = 0;
2308 u64 ref_root = 0;
2309 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002310
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002311 ins.objectid = node->bytenr;
2312 ins.offset = node->num_bytes;
2313 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002314
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002315 ref = btrfs_delayed_node_to_data_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002316 trace_run_delayed_data_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002317
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002318 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2319 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002320 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002321
2322 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002323 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002324 flags |= extent_op->flags_to_set;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002325 ret = alloc_reserved_file_extent(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002326 parent, ref_root, flags,
2327 ref->objectid, ref->offset,
2328 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002329 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002330 ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002331 ref_root, ref->objectid,
2332 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002333 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002334 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002335 ret = __btrfs_free_extent(trans, fs_info, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002336 ref_root, ref->objectid,
2337 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002338 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002339 } else {
2340 BUG();
2341 }
Chris Mason56bec292009-03-13 10:10:06 -04002342 return ret;
2343}
2344
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002345static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2346 struct extent_buffer *leaf,
2347 struct btrfs_extent_item *ei)
2348{
2349 u64 flags = btrfs_extent_flags(leaf, ei);
2350 if (extent_op->update_flags) {
2351 flags |= extent_op->flags_to_set;
2352 btrfs_set_extent_flags(leaf, ei, flags);
2353 }
2354
2355 if (extent_op->update_key) {
2356 struct btrfs_tree_block_info *bi;
2357 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2358 bi = (struct btrfs_tree_block_info *)(ei + 1);
2359 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2360 }
2361}
2362
2363static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002364 struct btrfs_fs_info *fs_info,
Josef Bacikd2788502017-09-29 15:43:57 -04002365 struct btrfs_delayed_ref_head *head,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002366 struct btrfs_delayed_extent_op *extent_op)
2367{
2368 struct btrfs_key key;
2369 struct btrfs_path *path;
2370 struct btrfs_extent_item *ei;
2371 struct extent_buffer *leaf;
2372 u32 item_size;
2373 int ret;
2374 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002375 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002376
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002377 if (trans->aborted)
2378 return 0;
2379
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002380 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05002381 metadata = 0;
2382
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002383 path = btrfs_alloc_path();
2384 if (!path)
2385 return -ENOMEM;
2386
Josef Bacikd2788502017-09-29 15:43:57 -04002387 key.objectid = head->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002388
Josef Bacik3173a182013-03-07 14:22:04 -05002389 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002390 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002391 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002392 } else {
2393 key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacikd2788502017-09-29 15:43:57 -04002394 key.offset = head->num_bytes;
Josef Bacik3173a182013-03-07 14:22:04 -05002395 }
2396
2397again:
David Sterbae4058b52015-11-27 16:31:35 +01002398 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002399 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002400 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002401 if (ret < 0) {
2402 err = ret;
2403 goto out;
2404 }
2405 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002406 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002407 if (path->slots[0] > 0) {
2408 path->slots[0]--;
2409 btrfs_item_key_to_cpu(path->nodes[0], &key,
2410 path->slots[0]);
Josef Bacikd2788502017-09-29 15:43:57 -04002411 if (key.objectid == head->bytenr &&
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002412 key.type == BTRFS_EXTENT_ITEM_KEY &&
Josef Bacikd2788502017-09-29 15:43:57 -04002413 key.offset == head->num_bytes)
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002414 ret = 0;
2415 }
2416 if (ret > 0) {
2417 btrfs_release_path(path);
2418 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002419
Josef Bacikd2788502017-09-29 15:43:57 -04002420 key.objectid = head->bytenr;
2421 key.offset = head->num_bytes;
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002422 key.type = BTRFS_EXTENT_ITEM_KEY;
2423 goto again;
2424 }
2425 } else {
2426 err = -EIO;
2427 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002428 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002429 }
2430
2431 leaf = path->nodes[0];
2432 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2433#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2434 if (item_size < sizeof(*ei)) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002435 ret = convert_extent_item_v0(trans, fs_info, path, (u64)-1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002436 if (ret < 0) {
2437 err = ret;
2438 goto out;
2439 }
2440 leaf = path->nodes[0];
2441 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2442 }
2443#endif
2444 BUG_ON(item_size < sizeof(*ei));
2445 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2446 __run_delayed_extent_op(extent_op, leaf, ei);
2447
2448 btrfs_mark_buffer_dirty(leaf);
2449out:
2450 btrfs_free_path(path);
2451 return err;
2452}
2453
2454static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002455 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002456 struct btrfs_delayed_ref_node *node,
2457 struct btrfs_delayed_extent_op *extent_op,
2458 int insert_reserved)
2459{
2460 int ret = 0;
2461 struct btrfs_delayed_tree_ref *ref;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002462 u64 parent = 0;
2463 u64 ref_root = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002464
2465 ref = btrfs_delayed_node_to_tree_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002466 trace_run_delayed_tree_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002467
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002468 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2469 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002470 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002471
Liu Bo02794222016-09-14 19:19:05 -07002472 if (node->ref_mod != 1) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002473 btrfs_err(fs_info,
Liu Bo02794222016-09-14 19:19:05 -07002474 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
2475 node->bytenr, node->ref_mod, node->action, ref_root,
2476 parent);
2477 return -EIO;
2478 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002479 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002480 BUG_ON(!extent_op || !extent_op->update_flags);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03002481 ret = alloc_reserved_tree_block(trans, node, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002482 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002483 ret = __btrfs_inc_extent_ref(trans, fs_info, node,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002484 parent, ref_root,
2485 ref->level, 0, 1,
Josef Bacikfcebe452014-05-13 17:30:47 -07002486 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002487 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002488 ret = __btrfs_free_extent(trans, fs_info, node,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002489 parent, ref_root,
2490 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002491 } else {
2492 BUG();
2493 }
2494 return ret;
2495}
2496
Chris Mason56bec292009-03-13 10:10:06 -04002497/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002498static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002499 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002500 struct btrfs_delayed_ref_node *node,
2501 struct btrfs_delayed_extent_op *extent_op,
2502 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002503{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002504 int ret = 0;
2505
Josef Bacik857cc2f2013-10-07 15:21:08 -04002506 if (trans->aborted) {
2507 if (insert_reserved)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002508 btrfs_pin_extent(fs_info, node->bytenr,
Josef Bacik857cc2f2013-10-07 15:21:08 -04002509 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002510 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002511 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002512
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002513 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2514 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002515 ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002516 insert_reserved);
2517 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2518 node->type == BTRFS_SHARED_DATA_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002519 ret = run_delayed_data_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002520 insert_reserved);
2521 else
2522 BUG();
2523 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002524}
2525
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002526static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04002527select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002528{
Filipe Mananacffc3372015-07-09 13:13:44 +01002529 struct btrfs_delayed_ref_node *ref;
2530
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002531 if (RB_EMPTY_ROOT(&head->ref_tree))
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002532 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002533
Filipe Mananacffc3372015-07-09 13:13:44 +01002534 /*
2535 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2536 * This is to prevent a ref count from going down to zero, which deletes
2537 * the extent item from the extent tree, when there still are references
2538 * to add, which would fail because they would not find the extent item.
2539 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002540 if (!list_empty(&head->ref_add_list))
2541 return list_first_entry(&head->ref_add_list,
2542 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01002543
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002544 ref = rb_entry(rb_first(&head->ref_tree),
2545 struct btrfs_delayed_ref_node, ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002546 ASSERT(list_empty(&ref->add_list));
2547 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04002548}
2549
Josef Bacik2eadaa22017-09-29 15:43:52 -04002550static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
2551 struct btrfs_delayed_ref_head *head)
2552{
2553 spin_lock(&delayed_refs->lock);
2554 head->processing = 0;
2555 delayed_refs->num_heads_ready++;
2556 spin_unlock(&delayed_refs->lock);
2557 btrfs_delayed_ref_unlock(head);
2558}
2559
Josef Bacikb00e6252017-09-29 15:43:53 -04002560static int cleanup_extent_op(struct btrfs_trans_handle *trans,
2561 struct btrfs_fs_info *fs_info,
2562 struct btrfs_delayed_ref_head *head)
2563{
2564 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
2565 int ret;
2566
2567 if (!extent_op)
2568 return 0;
2569 head->extent_op = NULL;
2570 if (head->must_insert_reserved) {
2571 btrfs_free_delayed_extent_op(extent_op);
2572 return 0;
2573 }
2574 spin_unlock(&head->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002575 ret = run_delayed_extent_op(trans, fs_info, head, extent_op);
Josef Bacikb00e6252017-09-29 15:43:53 -04002576 btrfs_free_delayed_extent_op(extent_op);
2577 return ret ? ret : 1;
2578}
2579
Josef Bacik194ab0b2017-09-29 15:43:54 -04002580static int cleanup_ref_head(struct btrfs_trans_handle *trans,
2581 struct btrfs_fs_info *fs_info,
2582 struct btrfs_delayed_ref_head *head)
2583{
2584 struct btrfs_delayed_ref_root *delayed_refs;
2585 int ret;
2586
2587 delayed_refs = &trans->transaction->delayed_refs;
2588
2589 ret = cleanup_extent_op(trans, fs_info, head);
2590 if (ret < 0) {
2591 unselect_delayed_ref_head(delayed_refs, head);
2592 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
2593 return ret;
2594 } else if (ret) {
2595 return ret;
2596 }
2597
2598 /*
2599 * Need to drop our head ref lock and re-acquire the delayed ref lock
2600 * and then re-check to make sure nobody got added.
2601 */
2602 spin_unlock(&head->lock);
2603 spin_lock(&delayed_refs->lock);
2604 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002605 if (!RB_EMPTY_ROOT(&head->ref_tree) || head->extent_op) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04002606 spin_unlock(&head->lock);
2607 spin_unlock(&delayed_refs->lock);
2608 return 1;
2609 }
Josef Bacik194ab0b2017-09-29 15:43:54 -04002610 delayed_refs->num_heads--;
2611 rb_erase(&head->href_node, &delayed_refs->href_root);
Josef Bacikd2788502017-09-29 15:43:57 -04002612 RB_CLEAR_NODE(&head->href_node);
Josef Bacikc1103f72017-09-29 15:43:56 -04002613 spin_unlock(&head->lock);
Nikolay Borisov1e7a1422018-04-11 11:21:18 +03002614 spin_unlock(&delayed_refs->lock);
Josef Bacikc1103f72017-09-29 15:43:56 -04002615 atomic_dec(&delayed_refs->num_entries);
2616
Josef Bacikd2788502017-09-29 15:43:57 -04002617 trace_run_delayed_ref_head(fs_info, head, 0);
Josef Bacikc1103f72017-09-29 15:43:56 -04002618
2619 if (head->total_ref_mod < 0) {
Nikolay Borisov5e388e92018-04-18 09:41:54 +03002620 struct btrfs_space_info *space_info;
2621 u64 flags;
Josef Bacikc1103f72017-09-29 15:43:56 -04002622
Nikolay Borisov5e388e92018-04-18 09:41:54 +03002623 if (head->is_data)
2624 flags = BTRFS_BLOCK_GROUP_DATA;
2625 else if (head->is_system)
2626 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2627 else
2628 flags = BTRFS_BLOCK_GROUP_METADATA;
2629 space_info = __find_space_info(fs_info, flags);
2630 ASSERT(space_info);
2631 percpu_counter_add(&space_info->total_bytes_pinned,
Josef Bacikd2788502017-09-29 15:43:57 -04002632 -head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04002633
2634 if (head->is_data) {
2635 spin_lock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002636 delayed_refs->pending_csums -= head->num_bytes;
Josef Bacikc1103f72017-09-29 15:43:56 -04002637 spin_unlock(&delayed_refs->lock);
2638 }
2639 }
2640
2641 if (head->must_insert_reserved) {
Josef Bacikd2788502017-09-29 15:43:57 -04002642 btrfs_pin_extent(fs_info, head->bytenr,
2643 head->num_bytes, 1);
Josef Bacikc1103f72017-09-29 15:43:56 -04002644 if (head->is_data) {
Josef Bacikd2788502017-09-29 15:43:57 -04002645 ret = btrfs_del_csums(trans, fs_info, head->bytenr,
2646 head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04002647 }
2648 }
2649
2650 /* Also free its reserved qgroup space */
2651 btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
2652 head->qgroup_reserved);
2653 btrfs_delayed_ref_unlock(head);
Josef Bacikd2788502017-09-29 15:43:57 -04002654 btrfs_put_delayed_ref_head(head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002655 return 0;
2656}
2657
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002658/*
2659 * Returns 0 on success or if called with an already aborted transaction.
2660 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2661 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002662static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacikd7df2c72014-01-23 09:21:38 -05002663 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002664{
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002665 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002666 struct btrfs_delayed_ref_root *delayed_refs;
2667 struct btrfs_delayed_ref_node *ref;
2668 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002669 struct btrfs_delayed_extent_op *extent_op;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002670 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002671 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002672 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002673 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002674 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002675
2676 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002677 while (1) {
2678 if (!locked_ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002679 if (count >= nr)
Chris Mason56bec292009-03-13 10:10:06 -04002680 break;
Chris Mason56bec292009-03-13 10:10:06 -04002681
Josef Bacikd7df2c72014-01-23 09:21:38 -05002682 spin_lock(&delayed_refs->lock);
2683 locked_ref = btrfs_select_ref_head(trans);
2684 if (!locked_ref) {
2685 spin_unlock(&delayed_refs->lock);
2686 break;
2687 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002688
2689 /* grab the lock that says we are going to process
2690 * all the refs for this head */
2691 ret = btrfs_delayed_ref_lock(trans, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002692 spin_unlock(&delayed_refs->lock);
Chris Masonc3e69d52009-03-13 10:17:05 -04002693 /*
2694 * we may have dropped the spin lock to get the head
2695 * mutex lock, and that might have given someone else
2696 * time to free the head. If that's true, it has been
2697 * removed from our list and we can move on.
2698 */
2699 if (ret == -EAGAIN) {
2700 locked_ref = NULL;
2701 count++;
2702 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002703 }
2704 }
2705
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002706 /*
2707 * We need to try and merge add/drops of the same ref since we
2708 * can run into issues with relocate dropping the implicit ref
2709 * and then it being added back again before the drop can
2710 * finish. If we merged anything we need to re-loop so we can
2711 * get a good ref.
2712 * Or we can get node references of the same type that weren't
2713 * merged when created due to bumps in the tree mod seq, and
2714 * we need to merge them to prevent adding an inline extent
2715 * backref before dropping it (triggering a BUG_ON at
2716 * insert_inline_extent_backref()).
2717 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002718 spin_lock(&locked_ref->lock);
Nikolay Borisovbe97f132018-04-19 11:06:39 +03002719 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002720
Arne Jansend1270cd2011-09-13 15:16:43 +02002721 ref = select_delayed_ref(locked_ref);
2722
2723 if (ref && ref->seq &&
Nikolay Borisov41d0bd32018-04-04 15:57:42 +03002724 btrfs_check_delayed_seq(fs_info, ref->seq)) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002725 spin_unlock(&locked_ref->lock);
Josef Bacik2eadaa22017-09-29 15:43:52 -04002726 unselect_delayed_ref_head(delayed_refs, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002727 locked_ref = NULL;
Arne Jansend1270cd2011-09-13 15:16:43 +02002728 cond_resched();
Josef Bacik27a377d2014-02-07 13:57:59 -05002729 count++;
Arne Jansend1270cd2011-09-13 15:16:43 +02002730 continue;
2731 }
2732
Josef Bacikc1103f72017-09-29 15:43:56 -04002733 /*
2734 * We're done processing refs in this ref_head, clean everything
2735 * up and move on to the next ref_head.
2736 */
Chris Mason56bec292009-03-13 10:10:06 -04002737 if (!ref) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04002738 ret = cleanup_ref_head(trans, fs_info, locked_ref);
2739 if (ret > 0 ) {
Josef Bacikb00e6252017-09-29 15:43:53 -04002740 /* We dropped our lock, we need to loop. */
2741 ret = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002742 continue;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002743 } else if (ret) {
2744 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002745 }
Josef Bacikc1103f72017-09-29 15:43:56 -04002746 locked_ref = NULL;
2747 count++;
2748 continue;
2749 }
Chris Mason56bec292009-03-13 10:10:06 -04002750
Josef Bacikc1103f72017-09-29 15:43:56 -04002751 actual_count++;
2752 ref->in_tree = 0;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002753 rb_erase(&ref->ref_node, &locked_ref->ref_tree);
2754 RB_CLEAR_NODE(&ref->ref_node);
Josef Bacikc1103f72017-09-29 15:43:56 -04002755 if (!list_empty(&ref->add_list))
2756 list_del(&ref->add_list);
2757 /*
2758 * When we play the delayed ref, also correct the ref_mod on
2759 * head
2760 */
2761 switch (ref->action) {
2762 case BTRFS_ADD_DELAYED_REF:
2763 case BTRFS_ADD_DELAYED_EXTENT:
Josef Bacikd2788502017-09-29 15:43:57 -04002764 locked_ref->ref_mod -= ref->ref_mod;
Josef Bacikc1103f72017-09-29 15:43:56 -04002765 break;
2766 case BTRFS_DROP_DELAYED_REF:
Josef Bacikd2788502017-09-29 15:43:57 -04002767 locked_ref->ref_mod += ref->ref_mod;
Josef Bacikc1103f72017-09-29 15:43:56 -04002768 break;
2769 default:
2770 WARN_ON(1);
Arne Jansen22cd2e72012-08-09 00:16:53 -06002771 }
Josef Bacik1ce7a5e2017-09-29 15:43:55 -04002772 atomic_dec(&delayed_refs->num_entries);
2773
Josef Bacikb00e6252017-09-29 15:43:53 -04002774 /*
2775 * Record the must-insert_reserved flag before we drop the spin
2776 * lock.
2777 */
2778 must_insert_reserved = locked_ref->must_insert_reserved;
2779 locked_ref->must_insert_reserved = 0;
2780
2781 extent_op = locked_ref->extent_op;
2782 locked_ref->extent_op = NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002783 spin_unlock(&locked_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002784
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002785 ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002786 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002787
Miao Xie78a61842012-11-21 02:21:28 +00002788 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002789 if (ret) {
Josef Bacik2eadaa22017-09-29 15:43:52 -04002790 unselect_delayed_ref_head(delayed_refs, locked_ref);
Miao Xie093486c2012-12-19 08:10:10 +00002791 btrfs_put_delayed_ref(ref);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002792 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
2793 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002794 return ret;
2795 }
2796
Miao Xie093486c2012-12-19 08:10:10 +00002797 btrfs_put_delayed_ref(ref);
2798 count++;
Chris Mason1887be62009-03-13 10:11:24 -04002799 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002800 }
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002801
2802 /*
2803 * We don't want to include ref heads since we can have empty ref heads
2804 * and those will drastically skew our runtime down since we just do
2805 * accounting, no actual extent tree updates.
2806 */
2807 if (actual_count > 0) {
2808 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2809 u64 avg;
2810
2811 /*
2812 * We weigh the current average higher than our current runtime
2813 * to avoid large swings in the average.
2814 */
2815 spin_lock(&delayed_refs->lock);
2816 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002817 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002818 spin_unlock(&delayed_refs->lock);
2819 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002820 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002821}
2822
Arne Jansen709c0482011-09-12 12:22:57 +02002823#ifdef SCRAMBLE_DELAYED_REFS
2824/*
2825 * Normally delayed refs get processed in ascending bytenr order. This
2826 * correlates in most cases to the order added. To expose dependencies on this
2827 * order, we start to process the tree in the middle instead of the beginning
2828 */
2829static u64 find_middle(struct rb_root *root)
2830{
2831 struct rb_node *n = root->rb_node;
2832 struct btrfs_delayed_ref_node *entry;
2833 int alt = 1;
2834 u64 middle;
2835 u64 first = 0, last = 0;
2836
2837 n = rb_first(root);
2838 if (n) {
2839 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2840 first = entry->bytenr;
2841 }
2842 n = rb_last(root);
2843 if (n) {
2844 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2845 last = entry->bytenr;
2846 }
2847 n = root->rb_node;
2848
2849 while (n) {
2850 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2851 WARN_ON(!entry->in_tree);
2852
2853 middle = entry->bytenr;
2854
2855 if (alt)
2856 n = n->rb_left;
2857 else
2858 n = n->rb_right;
2859
2860 alt = 1 - alt;
2861 }
2862 return middle;
2863}
2864#endif
2865
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002866static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002867{
2868 u64 num_bytes;
2869
2870 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2871 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002872 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002873 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2874
2875 /*
2876 * 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 -04002877 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002878 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002879 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002880}
2881
Josef Bacik12621332015-02-03 07:50:16 -08002882/*
2883 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2884 * would require to store the csums for that many bytes.
2885 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002886u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002887{
2888 u64 csum_size;
2889 u64 num_csums_per_leaf;
2890 u64 num_csums;
2891
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002892 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002893 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002894 (u64)btrfs_super_csum_size(fs_info->super_copy));
2895 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002896 num_csums += num_csums_per_leaf - 1;
2897 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2898 return num_csums;
2899}
2900
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002901int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002902 struct btrfs_fs_info *fs_info)
Josef Bacik1be41b72013-06-12 13:56:06 -04002903{
2904 struct btrfs_block_rsv *global_rsv;
2905 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
Josef Bacik12621332015-02-03 07:50:16 -08002906 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
David Sterba165c8b02017-11-08 02:12:57 +01002907 unsigned int num_dirty_bgs = trans->transaction->num_dirty_bgs;
Josef Bacikcb723e42015-02-18 08:06:57 -08002908 u64 num_bytes, num_dirty_bgs_bytes;
Josef Bacik1be41b72013-06-12 13:56:06 -04002909 int ret = 0;
2910
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002911 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002912 num_heads = heads_to_leaves(fs_info, num_heads);
Josef Bacik1be41b72013-06-12 13:56:06 -04002913 if (num_heads > 1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002914 num_bytes += (num_heads - 1) * fs_info->nodesize;
Josef Bacik1be41b72013-06-12 13:56:06 -04002915 num_bytes <<= 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002916 num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
2917 fs_info->nodesize;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002918 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
Josef Bacikcb723e42015-02-18 08:06:57 -08002919 num_dirty_bgs);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002920 global_rsv = &fs_info->global_block_rsv;
Josef Bacik1be41b72013-06-12 13:56:06 -04002921
2922 /*
2923 * If we can't allocate any more chunks lets make sure we have _lots_ of
2924 * wiggle room since running delayed refs can create more delayed refs.
2925 */
Josef Bacikcb723e42015-02-18 08:06:57 -08002926 if (global_rsv->space_info->full) {
2927 num_dirty_bgs_bytes <<= 1;
Josef Bacik1be41b72013-06-12 13:56:06 -04002928 num_bytes <<= 1;
Josef Bacikcb723e42015-02-18 08:06:57 -08002929 }
Josef Bacik1be41b72013-06-12 13:56:06 -04002930
2931 spin_lock(&global_rsv->lock);
Josef Bacikcb723e42015-02-18 08:06:57 -08002932 if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
Josef Bacik1be41b72013-06-12 13:56:06 -04002933 ret = 1;
2934 spin_unlock(&global_rsv->lock);
2935 return ret;
2936}
2937
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002938int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002939 struct btrfs_fs_info *fs_info)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002940{
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002941 u64 num_entries =
2942 atomic_read(&trans->transaction->delayed_refs.num_entries);
2943 u64 avg_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002944 u64 val;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002945
2946 smp_mb();
2947 avg_runtime = fs_info->avg_delayed_ref_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002948 val = num_entries * avg_runtime;
Wang Xiaoguangdc1a90c2016-10-26 15:23:01 +08002949 if (val >= NSEC_PER_SEC)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002950 return 1;
Chris Masona79b7d42014-05-22 16:18:52 -07002951 if (val >= NSEC_PER_SEC / 2)
2952 return 2;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002953
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002954 return btrfs_check_space_for_delayed_refs(trans, fs_info);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002955}
2956
Chris Masona79b7d42014-05-22 16:18:52 -07002957struct async_delayed_refs {
2958 struct btrfs_root *root;
Josef Bacik31b96552016-04-11 17:37:40 -04002959 u64 transid;
Chris Masona79b7d42014-05-22 16:18:52 -07002960 int count;
2961 int error;
2962 int sync;
2963 struct completion wait;
2964 struct btrfs_work work;
2965};
2966
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002967static inline struct async_delayed_refs *
2968to_async_delayed_refs(struct btrfs_work *work)
2969{
2970 return container_of(work, struct async_delayed_refs, work);
2971}
2972
Chris Masona79b7d42014-05-22 16:18:52 -07002973static void delayed_ref_async_start(struct btrfs_work *work)
2974{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002975 struct async_delayed_refs *async = to_async_delayed_refs(work);
Chris Masona79b7d42014-05-22 16:18:52 -07002976 struct btrfs_trans_handle *trans;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002977 struct btrfs_fs_info *fs_info = async->root->fs_info;
Chris Masona79b7d42014-05-22 16:18:52 -07002978 int ret;
2979
Chris Mason0f873ec2016-04-27 09:59:38 -04002980 /* if the commit is already started, we don't need to wait here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002981 if (btrfs_transaction_blocked(fs_info))
Josef Bacik31b96552016-04-11 17:37:40 -04002982 goto done;
Josef Bacik31b96552016-04-11 17:37:40 -04002983
Chris Mason0f873ec2016-04-27 09:59:38 -04002984 trans = btrfs_join_transaction(async->root);
2985 if (IS_ERR(trans)) {
2986 async->error = PTR_ERR(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07002987 goto done;
2988 }
2989
2990 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002991 * trans->sync means that when we call end_transaction, we won't
Chris Masona79b7d42014-05-22 16:18:52 -07002992 * wait on delayed refs
2993 */
2994 trans->sync = true;
Chris Mason0f873ec2016-04-27 09:59:38 -04002995
2996 /* Don't bother flushing if we got into a different transaction */
2997 if (trans->transid > async->transid)
2998 goto end;
2999
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003000 ret = btrfs_run_delayed_refs(trans, async->count);
Chris Masona79b7d42014-05-22 16:18:52 -07003001 if (ret)
3002 async->error = ret;
Chris Mason0f873ec2016-04-27 09:59:38 -04003003end:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003004 ret = btrfs_end_transaction(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07003005 if (ret && !async->error)
3006 async->error = ret;
3007done:
3008 if (async->sync)
3009 complete(&async->wait);
3010 else
3011 kfree(async);
3012}
3013
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003014int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
Josef Bacik31b96552016-04-11 17:37:40 -04003015 unsigned long count, u64 transid, int wait)
Chris Masona79b7d42014-05-22 16:18:52 -07003016{
3017 struct async_delayed_refs *async;
3018 int ret;
3019
3020 async = kmalloc(sizeof(*async), GFP_NOFS);
3021 if (!async)
3022 return -ENOMEM;
3023
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003024 async->root = fs_info->tree_root;
Chris Masona79b7d42014-05-22 16:18:52 -07003025 async->count = count;
3026 async->error = 0;
Josef Bacik31b96552016-04-11 17:37:40 -04003027 async->transid = transid;
Chris Masona79b7d42014-05-22 16:18:52 -07003028 if (wait)
3029 async->sync = 1;
3030 else
3031 async->sync = 0;
3032 init_completion(&async->wait);
3033
Liu Bo9e0af232014-08-15 23:36:53 +08003034 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
3035 delayed_ref_async_start, NULL, NULL);
Chris Masona79b7d42014-05-22 16:18:52 -07003036
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003037 btrfs_queue_work(fs_info->extent_workers, &async->work);
Chris Masona79b7d42014-05-22 16:18:52 -07003038
3039 if (wait) {
3040 wait_for_completion(&async->wait);
3041 ret = async->error;
3042 kfree(async);
3043 return ret;
3044 }
3045 return 0;
3046}
3047
Chris Masonc3e69d52009-03-13 10:17:05 -04003048/*
3049 * this starts processing the delayed reference count updates and
3050 * extent insertions we have queued up so far. count can be
3051 * 0, which means to process everything in the tree at the start
3052 * of the run (but not newly added entries), or it can be some target
3053 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003054 *
3055 * Returns 0 on success or if called with an aborted transaction
3056 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04003057 */
3058int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003059 unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04003060{
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003061 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -04003062 struct rb_node *node;
3063 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08003064 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04003065 int ret;
3066 int run_all = count == (unsigned long)-1;
Filipe Mananad9a05402015-10-03 13:13:13 +01003067 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Chris Masonc3e69d52009-03-13 10:17:05 -04003068
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003069 /* We'll clean this up in btrfs_cleanup_transaction */
3070 if (trans->aborted)
3071 return 0;
3072
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003073 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08003074 return 0;
3075
Chris Masonc3e69d52009-03-13 10:17:05 -04003076 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08003077 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05003078 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05003079
Chris Masonc3e69d52009-03-13 10:17:05 -04003080again:
Arne Jansen709c0482011-09-12 12:22:57 +02003081#ifdef SCRAMBLE_DELAYED_REFS
3082 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
3083#endif
Filipe Mananad9a05402015-10-03 13:13:13 +01003084 trans->can_flush_pending_bgs = false;
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02003085 ret = __btrfs_run_delayed_refs(trans, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003086 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003087 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003088 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04003089 }
3090
Chris Mason56bec292009-03-13 10:10:06 -04003091 if (run_all) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05003092 if (!list_empty(&trans->new_bgs))
Nikolay Borisov6c686b32018-02-07 17:55:40 +02003093 btrfs_create_pending_block_groups(trans);
Josef Bacikea658ba2012-09-11 16:57:25 -04003094
Josef Bacikd7df2c72014-01-23 09:21:38 -05003095 spin_lock(&delayed_refs->lock);
Liu Boc46effa2013-10-14 12:59:45 +08003096 node = rb_first(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003097 if (!node) {
3098 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04003099 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05003100 }
Josef Bacikd2788502017-09-29 15:43:57 -04003101 head = rb_entry(node, struct btrfs_delayed_ref_head,
3102 href_node);
3103 refcount_inc(&head->refs);
Chris Mason56bec292009-03-13 10:10:06 -04003104 spin_unlock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04003105
3106 /* Mutex was contended, block until it's released and retry. */
3107 mutex_lock(&head->mutex);
3108 mutex_unlock(&head->mutex);
3109
3110 btrfs_put_delayed_ref_head(head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003111 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04003112 goto again;
3113 }
Chris Mason54aa1f42007-06-22 14:16:25 -04003114out:
Filipe Mananad9a05402015-10-03 13:13:13 +01003115 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Chris Masona28ec192007-03-06 20:08:01 -05003116 return 0;
3117}
3118
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003119int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003120 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003121 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04003122 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003123{
3124 struct btrfs_delayed_extent_op *extent_op;
3125 int ret;
3126
Miao Xie78a61842012-11-21 02:21:28 +00003127 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003128 if (!extent_op)
3129 return -ENOMEM;
3130
3131 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01003132 extent_op->update_flags = true;
3133 extent_op->update_key = false;
3134 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04003135 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003136
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003137 ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003138 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003139 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00003140 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003141 return ret;
3142}
3143
Liu Boe4c3b2d2017-01-30 12:25:28 -08003144static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003145 struct btrfs_path *path,
3146 u64 objectid, u64 offset, u64 bytenr)
3147{
3148 struct btrfs_delayed_ref_head *head;
3149 struct btrfs_delayed_ref_node *ref;
3150 struct btrfs_delayed_data_ref *data_ref;
3151 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08003152 struct btrfs_transaction *cur_trans;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04003153 struct rb_node *node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003154 int ret = 0;
3155
ethanwu998ac6d2018-04-29 15:59:42 +08003156 spin_lock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08003157 cur_trans = root->fs_info->running_transaction;
ethanwu998ac6d2018-04-29 15:59:42 +08003158 if (cur_trans)
3159 refcount_inc(&cur_trans->use_count);
3160 spin_unlock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08003161 if (!cur_trans)
3162 return 0;
3163
3164 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003165 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003166 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003167 if (!head) {
3168 spin_unlock(&delayed_refs->lock);
ethanwu998ac6d2018-04-29 15:59:42 +08003169 btrfs_put_transaction(cur_trans);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003170 return 0;
3171 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003172
3173 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04003174 refcount_inc(&head->refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003175 spin_unlock(&delayed_refs->lock);
3176
David Sterbab3b4aa72011-04-21 01:20:15 +02003177 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003178
David Sterba8cc33e52011-05-02 15:29:25 +02003179 /*
3180 * Mutex was contended, block until it's released and let
3181 * caller try again
3182 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003183 mutex_lock(&head->mutex);
3184 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04003185 btrfs_put_delayed_ref_head(head);
ethanwu998ac6d2018-04-29 15:59:42 +08003186 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003187 return -EAGAIN;
3188 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003189 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003190
3191 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04003192 /*
3193 * XXX: We should replace this with a proper search function in the
3194 * future.
3195 */
3196 for (node = rb_first(&head->ref_tree); node; node = rb_next(node)) {
3197 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003198 /* If it's a shared ref we know a cross reference exists */
3199 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
3200 ret = 1;
3201 break;
3202 }
3203
3204 data_ref = btrfs_delayed_node_to_data_ref(ref);
3205
3206 /*
3207 * If our ref doesn't match the one we're currently looking at
3208 * then we have a cross reference.
3209 */
3210 if (data_ref->root != root->root_key.objectid ||
3211 data_ref->objectid != objectid ||
3212 data_ref->offset != offset) {
3213 ret = 1;
3214 break;
3215 }
3216 }
3217 spin_unlock(&head->lock);
3218 mutex_unlock(&head->mutex);
ethanwu998ac6d2018-04-29 15:59:42 +08003219 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003220 return ret;
3221}
3222
Liu Boe4c3b2d2017-01-30 12:25:28 -08003223static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003224 struct btrfs_path *path,
3225 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05003226{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003227 struct btrfs_fs_info *fs_info = root->fs_info;
3228 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04003229 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003230 struct btrfs_extent_data_ref *ref;
3231 struct btrfs_extent_inline_ref *iref;
3232 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05003233 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003234 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06003235 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04003236 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05003237
Chris Masonbe20aa92007-12-17 20:14:01 -05003238 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04003239 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04003240 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05003241
Chris Masonbe20aa92007-12-17 20:14:01 -05003242 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3243 if (ret < 0)
3244 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003245 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04003246
3247 ret = -ENOENT;
3248 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04003249 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003250
Zheng Yan31840ae2008-09-23 13:14:14 -04003251 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04003252 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003253 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05003254
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003255 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05003256 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003257
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003258 ret = 1;
3259 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3260#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3261 if (item_size < sizeof(*ei)) {
3262 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3263 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003264 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003265#endif
3266 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
3267
3268 if (item_size != sizeof(*ei) +
3269 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
3270 goto out;
3271
3272 if (btrfs_extent_generation(leaf, ei) <=
3273 btrfs_root_last_snapshot(&root->root_item))
3274 goto out;
3275
3276 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
Liu Bo3de28d52017-08-18 15:15:19 -06003277
3278 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
3279 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003280 goto out;
3281
3282 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3283 if (btrfs_extent_refs(leaf, ei) !=
3284 btrfs_extent_data_ref_count(leaf, ref) ||
3285 btrfs_extent_data_ref_root(leaf, ref) !=
3286 root->root_key.objectid ||
3287 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3288 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3289 goto out;
3290
Yan Zhengf321e492008-07-30 09:26:11 -04003291 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05003292out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003293 return ret;
3294}
3295
Liu Boe4c3b2d2017-01-30 12:25:28 -08003296int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
3297 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003298{
3299 struct btrfs_path *path;
3300 int ret;
3301 int ret2;
3302
3303 path = btrfs_alloc_path();
3304 if (!path)
Su Yue9132c4f2018-05-30 14:49:10 +08003305 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003306
3307 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08003308 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003309 offset, bytenr);
3310 if (ret && ret != -ENOENT)
3311 goto out;
3312
Liu Boe4c3b2d2017-01-30 12:25:28 -08003313 ret2 = check_delayed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003314 offset, bytenr);
3315 } while (ret2 == -EAGAIN);
3316
3317 if (ret2 && ret2 != -ENOENT) {
3318 ret = ret2;
3319 goto out;
3320 }
3321
3322 if (ret != -ENOENT || ret2 != -ENOENT)
3323 ret = 0;
3324out:
Yan Zhengf321e492008-07-30 09:26:11 -04003325 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003326 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3327 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04003328 return ret;
3329}
3330
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003331static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05003332 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003333 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07003334 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04003335{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003336 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04003337 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003338 u64 num_bytes;
3339 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003340 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04003341 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003342 struct btrfs_key key;
3343 struct btrfs_file_extent_item *fi;
3344 int i;
3345 int level;
3346 int ret = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003347 int (*process_func)(struct btrfs_trans_handle *,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003348 struct btrfs_root *,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003349 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04003350
David Sterbafccb84c2014-09-29 23:53:21 +02003351
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003352 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003353 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003354
Zheng Yan31840ae2008-09-23 13:14:14 -04003355 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003356 nritems = btrfs_header_nritems(buf);
3357 level = btrfs_header_level(buf);
3358
Miao Xie27cdeb72014-04-02 19:51:05 +08003359 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003360 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003361
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003362 if (inc)
3363 process_func = btrfs_inc_extent_ref;
3364 else
3365 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003366
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003367 if (full_backref)
3368 parent = buf->start;
3369 else
3370 parent = 0;
3371
Zheng Yan31840ae2008-09-23 13:14:14 -04003372 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003373 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003374 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02003375 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003376 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003377 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003378 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003379 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003380 BTRFS_FILE_EXTENT_INLINE)
3381 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003382 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3383 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003384 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003385
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003386 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3387 key.offset -= btrfs_file_extent_offset(buf, fi);
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003388 ret = process_func(trans, root, bytenr, num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003389 parent, ref_root, key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003390 key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003391 if (ret)
3392 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003393 } else {
3394 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003395 num_bytes = fs_info->nodesize;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003396 ret = process_func(trans, root, bytenr, num_bytes,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003397 parent, ref_root, level - 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003398 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003399 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003400 }
3401 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003402 return 0;
3403fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003404 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003405}
3406
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003407int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003408 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04003409{
Josef Bacike339a6b2014-07-02 10:54:25 -07003410 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003411}
Zheng Yan31840ae2008-09-23 13:14:14 -04003412
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003413int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003414 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003415{
Josef Bacike339a6b2014-07-02 10:54:25 -07003416 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003417}
3418
Chris Mason9078a3e2007-04-26 16:46:15 -04003419static int write_one_cache_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003420 struct btrfs_fs_info *fs_info,
Chris Mason9078a3e2007-04-26 16:46:15 -04003421 struct btrfs_path *path,
3422 struct btrfs_block_group_cache *cache)
3423{
3424 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003425 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003426 unsigned long bi;
3427 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003428
Chris Mason9078a3e2007-04-26 16:46:15 -04003429 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003430 if (ret) {
3431 if (ret > 0)
3432 ret = -ENOENT;
Chris Mason54aa1f42007-06-22 14:16:25 -04003433 goto fail;
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003434 }
Chris Mason5f39d392007-10-15 16:14:19 -04003435
3436 leaf = path->nodes[0];
3437 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3438 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3439 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -04003440fail:
Filipe Manana24b89d02015-04-25 18:31:05 +01003441 btrfs_release_path(path);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003442 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003443
3444}
3445
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003446static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003447next_block_group(struct btrfs_fs_info *fs_info,
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003448 struct btrfs_block_group_cache *cache)
3449{
3450 struct rb_node *node;
Filipe Manana292cbd52014-11-26 15:28:50 +00003451
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003452 spin_lock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003453
3454 /* If our block group was removed, we need a full search. */
3455 if (RB_EMPTY_NODE(&cache->cache_node)) {
3456 const u64 next_bytenr = cache->key.objectid + cache->key.offset;
3457
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003458 spin_unlock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003459 btrfs_put_block_group(cache);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003460 cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
Filipe Manana292cbd52014-11-26 15:28:50 +00003461 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003462 node = rb_next(&cache->cache_node);
3463 btrfs_put_block_group(cache);
3464 if (node) {
3465 cache = rb_entry(node, struct btrfs_block_group_cache,
3466 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003467 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003468 } else
3469 cache = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003470 spin_unlock(&fs_info->block_group_cache_lock);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003471 return cache;
3472}
3473
Josef Bacik0af3d002010-06-21 14:48:16 -04003474static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3475 struct btrfs_trans_handle *trans,
3476 struct btrfs_path *path)
3477{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003478 struct btrfs_fs_info *fs_info = block_group->fs_info;
3479 struct btrfs_root *root = fs_info->tree_root;
Josef Bacik0af3d002010-06-21 14:48:16 -04003480 struct inode *inode = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08003481 struct extent_changeset *data_reserved = NULL;
Josef Bacik0af3d002010-06-21 14:48:16 -04003482 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003483 int dcs = BTRFS_DC_ERROR;
David Sterbaf8c269d2015-01-16 17:21:12 +01003484 u64 num_pages = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003485 int retries = 0;
3486 int ret = 0;
3487
3488 /*
3489 * If this block group is smaller than 100 megs don't bother caching the
3490 * block group.
3491 */
Byongho Leeee221842015-12-15 01:42:10 +09003492 if (block_group->key.offset < (100 * SZ_1M)) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003493 spin_lock(&block_group->lock);
3494 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3495 spin_unlock(&block_group->lock);
3496 return 0;
3497 }
3498
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003499 if (trans->aborted)
3500 return 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003501again:
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003502 inode = lookup_free_space_inode(fs_info, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003503 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3504 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003505 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003506 goto out;
3507 }
3508
3509 if (IS_ERR(inode)) {
3510 BUG_ON(retries);
3511 retries++;
3512
3513 if (block_group->ro)
3514 goto out_free;
3515
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003516 ret = create_free_space_inode(fs_info, trans, block_group,
3517 path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003518 if (ret)
3519 goto out_free;
3520 goto again;
3521 }
3522
3523 /*
3524 * We want to set the generation to 0, that way if anything goes wrong
3525 * from here on out we know not to trust this cache when we load up next
3526 * time.
3527 */
3528 BTRFS_I(inode)->generation = 0;
3529 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003530 if (ret) {
3531 /*
3532 * So theoretically we could recover from this, simply set the
3533 * super cache generation to 0 so we know to invalidate the
3534 * cache, but then we'd have to keep track of the block groups
3535 * that fail this way so we know we _have_ to reset this cache
3536 * before the next commit or risk reading stale cache. So to
3537 * limit our exposure to horrible edge cases lets just abort the
3538 * transaction, this only happens in really bad situations
3539 * anyway.
3540 */
Jeff Mahoney66642832016-06-10 18:19:25 -04003541 btrfs_abort_transaction(trans, ret);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003542 goto out_put;
3543 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003544 WARN_ON(ret);
3545
Josef Bacik8e138e02017-11-17 14:50:46 -05003546 /* We've already setup this transaction, go ahead and exit */
3547 if (block_group->cache_generation == trans->transid &&
3548 i_size_read(inode)) {
3549 dcs = BTRFS_DC_SETUP;
3550 goto out_put;
3551 }
3552
Josef Bacik0af3d002010-06-21 14:48:16 -04003553 if (i_size_read(inode) > 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003554 ret = btrfs_check_trunc_cache_free_space(fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003555 &fs_info->global_block_rsv);
Miao Xie7b61cd92013-05-13 13:55:09 +00003556 if (ret)
3557 goto out_put;
3558
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003559 ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003560 if (ret)
3561 goto out_put;
3562 }
3563
3564 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003565 if (block_group->cached != BTRFS_CACHE_FINISHED ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003566 !btrfs_test_opt(fs_info, SPACE_CACHE)) {
Liu Bocf7c1ef2012-07-06 03:31:34 -06003567 /*
3568 * don't bother trying to write stuff out _if_
3569 * a) we're not cached,
Liu Bo1a79c1f2017-03-06 13:49:02 -08003570 * b) we're with nospace_cache mount option,
3571 * c) we're with v2 space_cache (FREE_SPACE_TREE).
Liu Bocf7c1ef2012-07-06 03:31:34 -06003572 */
Josef Bacik2b209822010-12-03 13:17:53 -05003573 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003574 spin_unlock(&block_group->lock);
3575 goto out_put;
3576 }
3577 spin_unlock(&block_group->lock);
3578
Josef Bacik6fc823b2012-08-06 13:46:38 -06003579 /*
Josef Bacik2968b1f2015-10-01 12:55:18 -04003580 * We hit an ENOSPC when setting up the cache in this transaction, just
3581 * skip doing the setup, we've already cleared the cache so we're safe.
3582 */
3583 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
3584 ret = -ENOSPC;
3585 goto out_put;
3586 }
3587
3588 /*
Josef Bacik6fc823b2012-08-06 13:46:38 -06003589 * Try to preallocate enough space based on how big the block group is.
3590 * Keep in mind this has to include any pinned space which could end up
3591 * taking up quite a bit since it's not folded into the other space
3592 * cache.
3593 */
Byongho Leeee221842015-12-15 01:42:10 +09003594 num_pages = div_u64(block_group->key.offset, SZ_256M);
Josef Bacik0af3d002010-06-21 14:48:16 -04003595 if (!num_pages)
3596 num_pages = 1;
3597
Josef Bacik0af3d002010-06-21 14:48:16 -04003598 num_pages *= 16;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003599 num_pages *= PAGE_SIZE;
Josef Bacik0af3d002010-06-21 14:48:16 -04003600
Qu Wenruo364ecf32017-02-27 15:10:38 +08003601 ret = btrfs_check_data_free_space(inode, &data_reserved, 0, num_pages);
Josef Bacik0af3d002010-06-21 14:48:16 -04003602 if (ret)
3603 goto out_put;
3604
3605 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3606 num_pages, num_pages,
3607 &alloc_hint);
Josef Bacik2968b1f2015-10-01 12:55:18 -04003608 /*
3609 * Our cache requires contiguous chunks so that we don't modify a bunch
3610 * of metadata or split extents when writing the cache out, which means
3611 * we can enospc if we are heavily fragmented in addition to just normal
3612 * out of space conditions. So if we hit this just skip setting up any
3613 * other block groups for this transaction, maybe we'll unpin enough
3614 * space the next time around.
3615 */
Josef Bacik2b209822010-12-03 13:17:53 -05003616 if (!ret)
3617 dcs = BTRFS_DC_SETUP;
Josef Bacik2968b1f2015-10-01 12:55:18 -04003618 else if (ret == -ENOSPC)
3619 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
Josef Bacikc09544e2011-08-30 10:19:10 -04003620
Josef Bacik0af3d002010-06-21 14:48:16 -04003621out_put:
3622 iput(inode);
3623out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003624 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003625out:
3626 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003627 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003628 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003629 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003630 spin_unlock(&block_group->lock);
3631
Qu Wenruo364ecf32017-02-27 15:10:38 +08003632 extent_changeset_free(data_reserved);
Josef Bacik0af3d002010-06-21 14:48:16 -04003633 return ret;
3634}
3635
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003636int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003637 struct btrfs_fs_info *fs_info)
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003638{
3639 struct btrfs_block_group_cache *cache, *tmp;
3640 struct btrfs_transaction *cur_trans = trans->transaction;
3641 struct btrfs_path *path;
3642
3643 if (list_empty(&cur_trans->dirty_bgs) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003644 !btrfs_test_opt(fs_info, SPACE_CACHE))
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003645 return 0;
3646
3647 path = btrfs_alloc_path();
3648 if (!path)
3649 return -ENOMEM;
3650
3651 /* Could add new block groups, use _safe just in case */
3652 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
3653 dirty_list) {
3654 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3655 cache_save_setup(cache, trans, path);
3656 }
3657
3658 btrfs_free_path(path);
3659 return 0;
3660}
3661
Chris Mason1bbc6212015-04-06 12:46:08 -07003662/*
3663 * transaction commit does final block group cache writeback during a
3664 * critical section where nothing is allowed to change the FS. This is
3665 * required in order for the cache to actually match the block group,
3666 * but can introduce a lot of latency into the commit.
3667 *
3668 * So, btrfs_start_dirty_block_groups is here to kick off block group
3669 * cache IO. There's a chance we'll have to redo some of it if the
3670 * block group changes again during the commit, but it greatly reduces
3671 * the commit latency by getting rid of the easy block groups while
3672 * we're still allowing others to join the commit.
3673 */
Nikolay Borisov21217052018-02-07 17:55:41 +02003674int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
Chris Mason1bbc6212015-04-06 12:46:08 -07003675{
Nikolay Borisov21217052018-02-07 17:55:41 +02003676 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason1bbc6212015-04-06 12:46:08 -07003677 struct btrfs_block_group_cache *cache;
3678 struct btrfs_transaction *cur_trans = trans->transaction;
3679 int ret = 0;
3680 int should_put;
3681 struct btrfs_path *path = NULL;
3682 LIST_HEAD(dirty);
3683 struct list_head *io = &cur_trans->io_bgs;
3684 int num_started = 0;
3685 int loops = 0;
3686
3687 spin_lock(&cur_trans->dirty_bgs_lock);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003688 if (list_empty(&cur_trans->dirty_bgs)) {
3689 spin_unlock(&cur_trans->dirty_bgs_lock);
3690 return 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003691 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003692 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Chris Mason1bbc6212015-04-06 12:46:08 -07003693 spin_unlock(&cur_trans->dirty_bgs_lock);
3694
3695again:
Chris Mason1bbc6212015-04-06 12:46:08 -07003696 /*
3697 * make sure all the block groups on our dirty list actually
3698 * exist
3699 */
Nikolay Borisov6c686b32018-02-07 17:55:40 +02003700 btrfs_create_pending_block_groups(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07003701
3702 if (!path) {
3703 path = btrfs_alloc_path();
3704 if (!path)
3705 return -ENOMEM;
3706 }
3707
Filipe Mananab58d1a92015-04-25 18:29:16 +01003708 /*
3709 * cache_write_mutex is here only to save us from balance or automatic
3710 * removal of empty block groups deleting this block group while we are
3711 * writing out the cache
3712 */
3713 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003714 while (!list_empty(&dirty)) {
3715 cache = list_first_entry(&dirty,
3716 struct btrfs_block_group_cache,
3717 dirty_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07003718 /*
3719 * this can happen if something re-dirties a block
3720 * group that is already under IO. Just wait for it to
3721 * finish and then do it all again
3722 */
3723 if (!list_empty(&cache->io_list)) {
3724 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003725 btrfs_wait_cache_io(trans, cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003726 btrfs_put_block_group(cache);
3727 }
3728
3729
3730 /*
3731 * btrfs_wait_cache_io uses the cache->dirty_list to decide
3732 * if it should update the cache_state. Don't delete
3733 * until after we wait.
3734 *
3735 * Since we're not running in the commit critical section
3736 * we need the dirty_bgs_lock to protect from update_block_group
3737 */
3738 spin_lock(&cur_trans->dirty_bgs_lock);
3739 list_del_init(&cache->dirty_list);
3740 spin_unlock(&cur_trans->dirty_bgs_lock);
3741
3742 should_put = 1;
3743
3744 cache_save_setup(cache, trans, path);
3745
3746 if (cache->disk_cache_state == BTRFS_DC_SETUP) {
3747 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003748 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003749 cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003750 if (ret == 0 && cache->io_ctl.inode) {
3751 num_started++;
3752 should_put = 0;
3753
3754 /*
Nikolay Borisov45ae2c12018-02-08 18:25:18 +02003755 * The cache_write_mutex is protecting the
3756 * io_list, also refer to the definition of
3757 * btrfs_transaction::io_bgs for more details
Chris Mason1bbc6212015-04-06 12:46:08 -07003758 */
3759 list_add_tail(&cache->io_list, io);
3760 } else {
3761 /*
3762 * if we failed to write the cache, the
3763 * generation will be bad and life goes on
3764 */
3765 ret = 0;
3766 }
3767 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003768 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003769 ret = write_one_cache_group(trans, fs_info,
3770 path, cache);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003771 /*
3772 * Our block group might still be attached to the list
3773 * of new block groups in the transaction handle of some
3774 * other task (struct btrfs_trans_handle->new_bgs). This
3775 * means its block group item isn't yet in the extent
3776 * tree. If this happens ignore the error, as we will
3777 * try again later in the critical section of the
3778 * transaction commit.
3779 */
3780 if (ret == -ENOENT) {
3781 ret = 0;
3782 spin_lock(&cur_trans->dirty_bgs_lock);
3783 if (list_empty(&cache->dirty_list)) {
3784 list_add_tail(&cache->dirty_list,
3785 &cur_trans->dirty_bgs);
3786 btrfs_get_block_group(cache);
3787 }
3788 spin_unlock(&cur_trans->dirty_bgs_lock);
3789 } else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003790 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003791 }
3792 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003793
3794 /* if its not on the io list, we need to put the block group */
3795 if (should_put)
3796 btrfs_put_block_group(cache);
3797
3798 if (ret)
3799 break;
Filipe Mananab58d1a92015-04-25 18:29:16 +01003800
3801 /*
3802 * Avoid blocking other tasks for too long. It might even save
3803 * us from writing caches for block groups that are going to be
3804 * removed.
3805 */
3806 mutex_unlock(&trans->transaction->cache_write_mutex);
3807 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003808 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003809 mutex_unlock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003810
3811 /*
3812 * go through delayed refs for all the stuff we've just kicked off
3813 * and then loop back (just once)
3814 */
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003815 ret = btrfs_run_delayed_refs(trans, 0);
Chris Mason1bbc6212015-04-06 12:46:08 -07003816 if (!ret && loops == 0) {
3817 loops++;
3818 spin_lock(&cur_trans->dirty_bgs_lock);
3819 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003820 /*
3821 * dirty_bgs_lock protects us from concurrent block group
3822 * deletes too (not just cache_write_mutex).
3823 */
3824 if (!list_empty(&dirty)) {
3825 spin_unlock(&cur_trans->dirty_bgs_lock);
3826 goto again;
3827 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003828 spin_unlock(&cur_trans->dirty_bgs_lock);
Liu Boc79a1752016-07-20 17:44:12 -07003829 } else if (ret < 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003830 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
Chris Mason1bbc6212015-04-06 12:46:08 -07003831 }
3832
3833 btrfs_free_path(path);
3834 return ret;
3835}
3836
Chris Mason96b51792007-10-15 16:15:19 -04003837int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003838 struct btrfs_fs_info *fs_info)
Chris Mason9078a3e2007-04-26 16:46:15 -04003839{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003840 struct btrfs_block_group_cache *cache;
Josef Bacikce93ec52014-11-17 15:45:48 -05003841 struct btrfs_transaction *cur_trans = trans->transaction;
3842 int ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003843 int should_put;
Chris Mason9078a3e2007-04-26 16:46:15 -04003844 struct btrfs_path *path;
Chris Mason1bbc6212015-04-06 12:46:08 -07003845 struct list_head *io = &cur_trans->io_bgs;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003846 int num_started = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003847
3848 path = btrfs_alloc_path();
3849 if (!path)
3850 return -ENOMEM;
3851
Josef Bacikce93ec52014-11-17 15:45:48 -05003852 /*
Filipe Mananae44081e2015-12-18 03:02:48 +00003853 * Even though we are in the critical section of the transaction commit,
3854 * we can still have concurrent tasks adding elements to this
3855 * transaction's list of dirty block groups. These tasks correspond to
3856 * endio free space workers started when writeback finishes for a
3857 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
3858 * allocate new block groups as a result of COWing nodes of the root
3859 * tree when updating the free space inode. The writeback for the space
3860 * caches is triggered by an earlier call to
3861 * btrfs_start_dirty_block_groups() and iterations of the following
3862 * loop.
3863 * Also we want to do the cache_save_setup first and then run the
Josef Bacikce93ec52014-11-17 15:45:48 -05003864 * delayed refs to make sure we have the best chance at doing this all
3865 * in one shot.
3866 */
Filipe Mananae44081e2015-12-18 03:02:48 +00003867 spin_lock(&cur_trans->dirty_bgs_lock);
Josef Bacikce93ec52014-11-17 15:45:48 -05003868 while (!list_empty(&cur_trans->dirty_bgs)) {
3869 cache = list_first_entry(&cur_trans->dirty_bgs,
3870 struct btrfs_block_group_cache,
3871 dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003872
3873 /*
3874 * this can happen if cache_save_setup re-dirties a block
3875 * group that is already under IO. Just wait for it to
3876 * finish and then do it all again
3877 */
3878 if (!list_empty(&cache->io_list)) {
Filipe Mananae44081e2015-12-18 03:02:48 +00003879 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003880 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003881 btrfs_wait_cache_io(trans, cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003882 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003883 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003884 }
3885
Chris Mason1bbc6212015-04-06 12:46:08 -07003886 /*
3887 * don't remove from the dirty list until after we've waited
3888 * on any pending IO
3889 */
Josef Bacikce93ec52014-11-17 15:45:48 -05003890 list_del_init(&cache->dirty_list);
Filipe Mananae44081e2015-12-18 03:02:48 +00003891 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003892 should_put = 1;
3893
Chris Mason1bbc6212015-04-06 12:46:08 -07003894 cache_save_setup(cache, trans, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003895
Josef Bacikce93ec52014-11-17 15:45:48 -05003896 if (!ret)
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003897 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003898 (unsigned long) -1);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003899
3900 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3901 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003902 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003903 cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003904 if (ret == 0 && cache->io_ctl.inode) {
3905 num_started++;
3906 should_put = 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003907 list_add_tail(&cache->io_list, io);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003908 } else {
3909 /*
3910 * if we failed to write the cache, the
3911 * generation will be bad and life goes on
3912 */
3913 ret = 0;
3914 }
3915 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003916 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003917 ret = write_one_cache_group(trans, fs_info,
3918 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003919 /*
3920 * One of the free space endio workers might have
3921 * created a new block group while updating a free space
3922 * cache's inode (at inode.c:btrfs_finish_ordered_io())
3923 * and hasn't released its transaction handle yet, in
3924 * which case the new block group is still attached to
3925 * its transaction handle and its creation has not
3926 * finished yet (no block group item in the extent tree
3927 * yet, etc). If this is the case, wait for all free
3928 * space endio workers to finish and retry. This is a
3929 * a very rare case so no need for a more efficient and
3930 * complex approach.
3931 */
3932 if (ret == -ENOENT) {
3933 wait_event(cur_trans->writer_wait,
3934 atomic_read(&cur_trans->num_writers) == 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003935 ret = write_one_cache_group(trans, fs_info,
3936 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003937 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003938 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003939 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003940 }
Chris Masonc9dc4c62015-04-04 17:14:42 -07003941
3942 /* if its not on the io list, we need to put the block group */
3943 if (should_put)
3944 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003945 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003946 }
Filipe Mananae44081e2015-12-18 03:02:48 +00003947 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003948
Nikolay Borisov45ae2c12018-02-08 18:25:18 +02003949 /*
3950 * Refer to the definition of io_bgs member for details why it's safe
3951 * to use it without any locking
3952 */
Chris Mason1bbc6212015-04-06 12:46:08 -07003953 while (!list_empty(io)) {
3954 cache = list_first_entry(io, struct btrfs_block_group_cache,
Chris Masonc9dc4c62015-04-04 17:14:42 -07003955 io_list);
3956 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003957 btrfs_wait_cache_io(trans, cache, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003958 btrfs_put_block_group(cache);
3959 }
3960
Chris Mason9078a3e2007-04-26 16:46:15 -04003961 btrfs_free_path(path);
Josef Bacikce93ec52014-11-17 15:45:48 -05003962 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003963}
3964
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003965int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05003966{
3967 struct btrfs_block_group_cache *block_group;
3968 int readonly = 0;
3969
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003970 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003971 if (!block_group || block_group->ro)
3972 readonly = 1;
3973 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003974 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003975 return readonly;
3976}
3977
Filipe Mananaf78c4362016-05-09 13:15:41 +01003978bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3979{
3980 struct btrfs_block_group_cache *bg;
3981 bool ret = true;
3982
3983 bg = btrfs_lookup_block_group(fs_info, bytenr);
3984 if (!bg)
3985 return false;
3986
3987 spin_lock(&bg->lock);
3988 if (bg->ro)
3989 ret = false;
3990 else
3991 atomic_inc(&bg->nocow_writers);
3992 spin_unlock(&bg->lock);
3993
3994 /* no put on block group, done by btrfs_dec_nocow_writers */
3995 if (!ret)
3996 btrfs_put_block_group(bg);
3997
3998 return ret;
3999
4000}
4001
4002void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
4003{
4004 struct btrfs_block_group_cache *bg;
4005
4006 bg = btrfs_lookup_block_group(fs_info, bytenr);
4007 ASSERT(bg);
4008 if (atomic_dec_and_test(&bg->nocow_writers))
Peter Zijlstra46259562018-03-15 11:43:08 +01004009 wake_up_var(&bg->nocow_writers);
Filipe Mananaf78c4362016-05-09 13:15:41 +01004010 /*
4011 * Once for our lookup and once for the lookup done by a previous call
4012 * to btrfs_inc_nocow_writers()
4013 */
4014 btrfs_put_block_group(bg);
4015 btrfs_put_block_group(bg);
4016}
4017
Filipe Mananaf78c4362016-05-09 13:15:41 +01004018void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg)
4019{
Peter Zijlstra46259562018-03-15 11:43:08 +01004020 wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers));
Filipe Mananaf78c4362016-05-09 13:15:41 +01004021}
4022
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04004023static const char *alloc_name(u64 flags)
4024{
4025 switch (flags) {
4026 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
4027 return "mixed";
4028 case BTRFS_BLOCK_GROUP_METADATA:
4029 return "metadata";
4030 case BTRFS_BLOCK_GROUP_DATA:
4031 return "data";
4032 case BTRFS_BLOCK_GROUP_SYSTEM:
4033 return "system";
4034 default:
4035 WARN_ON(1);
4036 return "invalid-combination";
4037 };
4038}
4039
Lu Fengqi4ca61682018-05-28 14:30:27 +08004040static int create_space_info(struct btrfs_fs_info *info, u64 flags)
Nikolay Borisov2be12ef2017-05-22 09:35:49 +03004041{
4042
4043 struct btrfs_space_info *space_info;
4044 int i;
4045 int ret;
4046
4047 space_info = kzalloc(sizeof(*space_info), GFP_NOFS);
4048 if (!space_info)
4049 return -ENOMEM;
4050
4051 ret = percpu_counter_init(&space_info->total_bytes_pinned, 0,
4052 GFP_KERNEL);
4053 if (ret) {
4054 kfree(space_info);
4055 return ret;
4056 }
4057
4058 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
4059 INIT_LIST_HEAD(&space_info->block_groups[i]);
4060 init_rwsem(&space_info->groups_sem);
4061 spin_lock_init(&space_info->lock);
4062 space_info->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
4063 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
4064 init_waitqueue_head(&space_info->wait);
4065 INIT_LIST_HEAD(&space_info->ro_bgs);
4066 INIT_LIST_HEAD(&space_info->tickets);
4067 INIT_LIST_HEAD(&space_info->priority_tickets);
4068
4069 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
4070 info->space_info_kobj, "%s",
4071 alloc_name(space_info->flags));
4072 if (ret) {
4073 percpu_counter_destroy(&space_info->total_bytes_pinned);
4074 kfree(space_info);
4075 return ret;
4076 }
4077
Nikolay Borisov2be12ef2017-05-22 09:35:49 +03004078 list_add_rcu(&space_info->list, &info->space_info);
4079 if (flags & BTRFS_BLOCK_GROUP_DATA)
4080 info->data_sinfo = space_info;
4081
4082 return ret;
4083}
4084
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +03004085static void update_space_info(struct btrfs_fs_info *info, u64 flags,
Chris Mason593060d2008-03-25 16:50:33 -04004086 u64 total_bytes, u64 bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -04004087 u64 bytes_readonly,
Chris Mason593060d2008-03-25 16:50:33 -04004088 struct btrfs_space_info **space_info)
4089{
4090 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004091 int factor;
4092
4093 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
4094 BTRFS_BLOCK_GROUP_RAID10))
4095 factor = 2;
4096 else
4097 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04004098
4099 found = __find_space_info(info, flags);
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +03004100 ASSERT(found);
4101 spin_lock(&found->lock);
4102 found->total_bytes += total_bytes;
4103 found->disk_total += total_bytes * factor;
4104 found->bytes_used += bytes_used;
4105 found->disk_used += bytes_used * factor;
4106 found->bytes_readonly += bytes_readonly;
4107 if (total_bytes > 0)
4108 found->full = 0;
4109 space_info_add_new_bytes(info, found, total_bytes -
4110 bytes_used - bytes_readonly);
4111 spin_unlock(&found->lock);
4112 *space_info = found;
Chris Mason593060d2008-03-25 16:50:33 -04004113}
4114
Chris Mason8790d502008-04-03 16:29:03 -04004115static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
4116{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03004117 u64 extra_flags = chunk_to_extended(flags) &
4118 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004119
Miao Xiede98ced2013-01-29 10:13:12 +00004120 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004121 if (flags & BTRFS_BLOCK_GROUP_DATA)
4122 fs_info->avail_data_alloc_bits |= extra_flags;
4123 if (flags & BTRFS_BLOCK_GROUP_METADATA)
4124 fs_info->avail_metadata_alloc_bits |= extra_flags;
4125 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
4126 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004127 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04004128}
Chris Mason593060d2008-03-25 16:50:33 -04004129
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004130/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004131 * returns target flags in extended format or 0 if restripe for this
4132 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04004133 *
David Sterbadccdb072018-03-21 00:20:05 +01004134 * should be called with balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004135 */
4136static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
4137{
4138 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4139 u64 target = 0;
4140
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004141 if (!bctl)
4142 return 0;
4143
4144 if (flags & BTRFS_BLOCK_GROUP_DATA &&
4145 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4146 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
4147 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
4148 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4149 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
4150 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
4151 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4152 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
4153 }
4154
4155 return target;
4156}
4157
4158/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004159 * @flags: available profiles in extended format (see ctree.h)
4160 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004161 * Returns reduced profile in chunk format. If profile changing is in
4162 * progress (either running or paused) picks the target profile (if it's
4163 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004164 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004165static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04004166{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004167 u64 num_devices = fs_info->fs_devices->rw_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004168 u64 target;
Zhao Lei9c170b22015-09-15 21:08:08 +08004169 u64 raid_type;
4170 u64 allowed = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004171
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004172 /*
4173 * see if restripe for this chunk_type is in progress, if so
4174 * try to reduce to the target profile
4175 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004176 spin_lock(&fs_info->balance_lock);
4177 target = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004178 if (target) {
4179 /* pick target profile only if it's already available */
4180 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004181 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004182 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004183 }
4184 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004185 spin_unlock(&fs_info->balance_lock);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004186
David Woodhouse53b381b2013-01-29 18:40:14 -05004187 /* First, mask out the RAID levels which aren't possible */
Zhao Lei9c170b22015-09-15 21:08:08 +08004188 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
4189 if (num_devices >= btrfs_raid_array[raid_type].devs_min)
Anand Jain41a6e892018-04-25 19:01:43 +08004190 allowed |= btrfs_raid_array[raid_type].bg_flag;
Zhao Lei9c170b22015-09-15 21:08:08 +08004191 }
4192 allowed &= flags;
Chris Masona061fc82008-05-07 11:43:44 -04004193
Zhao Lei9c170b22015-09-15 21:08:08 +08004194 if (allowed & BTRFS_BLOCK_GROUP_RAID6)
4195 allowed = BTRFS_BLOCK_GROUP_RAID6;
4196 else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
4197 allowed = BTRFS_BLOCK_GROUP_RAID5;
4198 else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
4199 allowed = BTRFS_BLOCK_GROUP_RAID10;
4200 else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
4201 allowed = BTRFS_BLOCK_GROUP_RAID1;
4202 else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
4203 allowed = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04004204
Zhao Lei9c170b22015-09-15 21:08:08 +08004205 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
Chris Masonec44a352008-04-28 15:29:52 -04004206
Zhao Lei9c170b22015-09-15 21:08:08 +08004207 return extended_to_chunk(flags | allowed);
Chris Masonec44a352008-04-28 15:29:52 -04004208}
4209
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004210static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05004211{
Miao Xiede98ced2013-01-29 10:13:12 +00004212 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004213 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004214
4215 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004216 flags = orig_flags;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004217 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00004218
4219 if (flags & BTRFS_BLOCK_GROUP_DATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004220 flags |= fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004221 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004222 flags |= fs_info->avail_system_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004223 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004224 flags |= fs_info->avail_metadata_alloc_bits;
4225 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02004226
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004227 return btrfs_reduce_alloc_profile(fs_info, flags);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004228}
Josef Bacik6a632092009-02-20 11:00:09 -05004229
Jeff Mahoney1b868262017-05-17 11:38:35 -04004230static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04004231{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004232 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004233 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05004234 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004235
Yan, Zhengb742bb82010-05-16 10:46:24 -04004236 if (data)
4237 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004238 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb82010-05-16 10:46:24 -04004239 flags = BTRFS_BLOCK_GROUP_SYSTEM;
4240 else
4241 flags = BTRFS_BLOCK_GROUP_METADATA;
4242
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004243 ret = get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05004244 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004245}
4246
Jeff Mahoney1b868262017-05-17 11:38:35 -04004247u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info)
4248{
4249 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_DATA);
4250}
4251
4252u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info)
4253{
4254 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4255}
4256
4257u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info)
4258{
4259 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4260}
4261
Liu Bo41361352017-02-13 15:42:21 -08004262static u64 btrfs_space_info_used(struct btrfs_space_info *s_info,
4263 bool may_use_included)
4264{
4265 ASSERT(s_info);
4266 return s_info->bytes_used + s_info->bytes_reserved +
4267 s_info->bytes_pinned + s_info->bytes_readonly +
4268 (may_use_included ? s_info->bytes_may_use : 0);
4269}
4270
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004271int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05004272{
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004273 struct btrfs_root *root = inode->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004274 struct btrfs_fs_info *fs_info = root->fs_info;
Nikolay Borisov1174cad2017-07-11 13:47:50 +03004275 struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
Josef Bacikab6e24102010-03-19 14:38:13 +00004276 u64 used;
Zhao Lei94b947b2015-02-14 13:23:45 +08004277 int ret = 0;
Zhao Leic99f1b02015-03-02 19:32:20 +08004278 int need_commit = 2;
4279 int have_pinned_space;
Josef Bacik6a632092009-02-20 11:00:09 -05004280
4281 /* make sure bytes are sectorsize aligned */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004282 bytes = ALIGN(bytes, fs_info->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05004283
Miao Xie9dced182013-10-25 17:33:36 +08004284 if (btrfs_is_free_space_inode(inode)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004285 need_commit = 0;
Miao Xie9dced182013-10-25 17:33:36 +08004286 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04004287 }
4288
Josef Bacik6a632092009-02-20 11:00:09 -05004289again:
4290 /* make sure we have enough space to handle the data first */
4291 spin_lock(&data_sinfo->lock);
Liu Bo41361352017-02-13 15:42:21 -08004292 used = btrfs_space_info_used(data_sinfo, true);
Josef Bacikab6e24102010-03-19 14:38:13 +00004293
4294 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004295 struct btrfs_trans_handle *trans;
4296
Josef Bacik6a632092009-02-20 11:00:09 -05004297 /*
4298 * if we don't have enough free bytes in this space then we need
4299 * to alloc a new chunk.
4300 */
Zhao Leib9fd47c2015-02-09 14:40:20 +08004301 if (!data_sinfo->full) {
Josef Bacik6a632092009-02-20 11:00:09 -05004302 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05004303
Chris Mason0e4f8f82011-04-15 16:05:44 -04004304 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05004305 spin_unlock(&data_sinfo->lock);
Nikolay Borisov1174cad2017-07-11 13:47:50 +03004306
Jeff Mahoney1b868262017-05-17 11:38:35 -04004307 alloc_target = btrfs_data_alloc_profile(fs_info);
Miao Xie9dced182013-10-25 17:33:36 +08004308 /*
4309 * It is ugly that we don't call nolock join
4310 * transaction for the free space inode case here.
4311 * But it is safe because we only do the data space
4312 * reservation for the free space cache in the
4313 * transaction context, the common join transaction
4314 * just increase the counter of the current transaction
4315 * handler, doesn't try to acquire the trans_lock of
4316 * the fs.
4317 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004318 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004319 if (IS_ERR(trans))
4320 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05004321
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004322 ret = do_chunk_alloc(trans, fs_info, alloc_target,
Chris Mason0e4f8f82011-04-15 16:05:44 -04004323 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004324 btrfs_end_transaction(trans);
Miao Xied52a5b52011-01-05 10:07:18 +00004325 if (ret < 0) {
4326 if (ret != -ENOSPC)
4327 return ret;
Zhao Leic99f1b02015-03-02 19:32:20 +08004328 else {
4329 have_pinned_space = 1;
Miao Xied52a5b52011-01-05 10:07:18 +00004330 goto commit_trans;
Zhao Leic99f1b02015-03-02 19:32:20 +08004331 }
Miao Xied52a5b52011-01-05 10:07:18 +00004332 }
Chris Mason33b4d472009-09-22 14:45:50 -04004333
Josef Bacik6a632092009-02-20 11:00:09 -05004334 goto again;
4335 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004336
4337 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04004338 * If we don't have enough pinned space to deal with this
Zhao Lei94b947b2015-02-14 13:23:45 +08004339 * allocation, and no removed chunk in current transaction,
4340 * don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004341 */
Zhao Leic99f1b02015-03-02 19:32:20 +08004342 have_pinned_space = percpu_counter_compare(
4343 &data_sinfo->total_bytes_pinned,
4344 used + bytes - data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05004345 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004346
4347 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00004348commit_trans:
Nikolay Borisov92e2f7e2018-02-05 10:41:16 +02004349 if (need_commit) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004350 need_commit--;
Josef Bacikb150a4f2013-06-19 15:00:04 -04004351
Zhao Leie1746e82015-12-01 18:39:40 +08004352 if (need_commit > 0) {
Nikolay Borisov82b3e532018-04-23 10:54:13 +03004353 btrfs_start_delalloc_roots(fs_info, -1);
Chris Mason6374e57a2017-06-23 09:48:21 -07004354 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004355 (u64)-1);
Zhao Leie1746e82015-12-01 18:39:40 +08004356 }
Zhao Lei9a4e7272015-04-09 12:34:43 +08004357
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004358 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004359 if (IS_ERR(trans))
4360 return PTR_ERR(trans);
Zhao Leic99f1b02015-03-02 19:32:20 +08004361 if (have_pinned_space >= 0 ||
Josef Bacik3204d332015-09-24 10:46:10 -04004362 test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
4363 &trans->transaction->flags) ||
Zhao Leic99f1b02015-03-02 19:32:20 +08004364 need_commit > 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004365 ret = btrfs_commit_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004366 if (ret)
4367 return ret;
Zhao Leid7c15172015-02-26 10:49:20 +08004368 /*
Filipe Mananac2d6cb12016-01-15 11:05:12 +00004369 * The cleaner kthread might still be doing iput
4370 * operations. Wait for it to finish so that
4371 * more space is released.
Zhao Leid7c15172015-02-26 10:49:20 +08004372 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004373 mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
4374 mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
Zhao Lei94b947b2015-02-14 13:23:45 +08004375 goto again;
4376 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004377 btrfs_end_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004378 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004379 }
4380
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004381 trace_btrfs_space_reservation(fs_info,
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004382 "space_info:enospc",
4383 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004384 return -ENOSPC;
4385 }
4386 data_sinfo->bytes_may_use += bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004387 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004388 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004389 spin_unlock(&data_sinfo->lock);
4390
Dongsheng Yang237c0e92014-12-29 06:23:05 -05004391 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004392}
4393
Qu Wenruo364ecf32017-02-27 15:10:38 +08004394int btrfs_check_data_free_space(struct inode *inode,
4395 struct extent_changeset **reserved, u64 start, u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004396{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004397 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004398 int ret;
4399
4400 /* align the range */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004401 len = round_up(start + len, fs_info->sectorsize) -
4402 round_down(start, fs_info->sectorsize);
4403 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004404
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004405 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004406 if (ret < 0)
4407 return ret;
4408
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004409 /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
Qu Wenruo364ecf32017-02-27 15:10:38 +08004410 ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
Qu Wenruo7bc329c2017-02-27 15:10:36 +08004411 if (ret < 0)
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004412 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004413 else
4414 ret = 0;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004415 return ret;
4416}
4417
4418/*
Qu Wenruo4ceff072015-09-08 17:22:42 +08004419 * Called if we need to clear a data reservation for this inode
4420 * Normally in a error case.
4421 *
Qu Wenruo51773be2015-10-08 18:19:37 +08004422 * This one will *NOT* use accurate qgroup reserved space API, just for case
4423 * which we can't sleep and is sure it won't affect qgroup reserved space.
4424 * Like clear_bit_hook().
Qu Wenruo4ceff072015-09-08 17:22:42 +08004425 */
Qu Wenruo51773be2015-10-08 18:19:37 +08004426void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
4427 u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004428{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004429 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004430 struct btrfs_space_info *data_sinfo;
4431
4432 /* Make sure the range is aligned to sectorsize */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004433 len = round_up(start + len, fs_info->sectorsize) -
4434 round_down(start, fs_info->sectorsize);
4435 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004436
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004437 data_sinfo = fs_info->data_sinfo;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004438 spin_lock(&data_sinfo->lock);
4439 if (WARN_ON(data_sinfo->bytes_may_use < len))
4440 data_sinfo->bytes_may_use = 0;
4441 else
4442 data_sinfo->bytes_may_use -= len;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004443 trace_btrfs_space_reservation(fs_info, "space_info",
Qu Wenruo4ceff072015-09-08 17:22:42 +08004444 data_sinfo->flags, len, 0);
4445 spin_unlock(&data_sinfo->lock);
4446}
4447
Qu Wenruo51773be2015-10-08 18:19:37 +08004448/*
4449 * Called if we need to clear a data reservation for this inode
4450 * Normally in a error case.
4451 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04004452 * This one will handle the per-inode data rsv map for accurate reserved
Qu Wenruo51773be2015-10-08 18:19:37 +08004453 * space framework.
4454 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08004455void btrfs_free_reserved_data_space(struct inode *inode,
4456 struct extent_changeset *reserved, u64 start, u64 len)
Qu Wenruo51773be2015-10-08 18:19:37 +08004457{
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004458 struct btrfs_root *root = BTRFS_I(inode)->root;
4459
4460 /* Make sure the range is aligned to sectorsize */
Jeff Mahoneyda170662016-06-15 09:22:56 -04004461 len = round_up(start + len, root->fs_info->sectorsize) -
4462 round_down(start, root->fs_info->sectorsize);
4463 start = round_down(start, root->fs_info->sectorsize);
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004464
Qu Wenruo51773be2015-10-08 18:19:37 +08004465 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruobc42bda2017-02-27 15:10:39 +08004466 btrfs_qgroup_free_data(inode, reserved, start, len);
Qu Wenruo51773be2015-10-08 18:19:37 +08004467}
4468
Josef Bacik97e728d2009-04-21 17:40:57 -04004469static void force_metadata_allocation(struct btrfs_fs_info *info)
4470{
4471 struct list_head *head = &info->space_info;
4472 struct btrfs_space_info *found;
4473
4474 rcu_read_lock();
4475 list_for_each_entry_rcu(found, head, list) {
4476 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004477 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04004478 }
4479 rcu_read_unlock();
4480}
4481
Miao Xie3c76cd82013-04-25 10:12:38 +00004482static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
4483{
4484 return (global->size << 1);
4485}
4486
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004487static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
Josef Bacik698d0082012-09-12 14:08:47 -04004488 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04004489{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004490 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004491 u64 bytes_used = btrfs_space_info_used(sinfo, false);
Chris Masone5bc2452010-10-26 13:37:56 -04004492 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04004493
Chris Mason0e4f8f82011-04-15 16:05:44 -04004494 if (force == CHUNK_ALLOC_FORCE)
4495 return 1;
4496
4497 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04004498 * We need to take into account the global rsv because for all intents
4499 * and purposes it's used space. Don't worry about locking the
4500 * global_rsv, it doesn't change except when the transaction commits.
4501 */
Josef Bacik54338b52012-08-14 16:20:52 -04004502 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004503 bytes_used += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04004504
4505 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04004506 * in limited mode, we want to have some free space up to
4507 * about 1% of the FS size.
4508 */
4509 if (force == CHUNK_ALLOC_LIMITED) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004510 thresh = btrfs_super_total_bytes(fs_info->super_copy);
Byongho Leeee221842015-12-15 01:42:10 +09004511 thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
Chris Mason0e4f8f82011-04-15 16:05:44 -04004512
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004513 if (sinfo->total_bytes - bytes_used < thresh)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004514 return 1;
4515 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004516
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004517 if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04004518 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04004519 return 1;
4520}
4521
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004522static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004523{
4524 u64 num_dev;
4525
David Woodhouse53b381b2013-01-29 18:40:14 -05004526 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
4527 BTRFS_BLOCK_GROUP_RAID0 |
4528 BTRFS_BLOCK_GROUP_RAID5 |
4529 BTRFS_BLOCK_GROUP_RAID6))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004530 num_dev = fs_info->fs_devices->rw_devices;
Liu Bo15d1ff82012-03-29 09:57:44 -04004531 else if (type & BTRFS_BLOCK_GROUP_RAID1)
4532 num_dev = 2;
4533 else
4534 num_dev = 1; /* DUP or single */
4535
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004536 return num_dev;
Liu Bo15d1ff82012-03-29 09:57:44 -04004537}
4538
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004539/*
4540 * If @is_allocation is true, reserve space in the system space info necessary
4541 * for allocating a chunk, otherwise if it's false, reserve space necessary for
4542 * removing a chunk.
4543 */
4544void check_system_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004545 struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004546{
4547 struct btrfs_space_info *info;
4548 u64 left;
4549 u64 thresh;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004550 int ret = 0;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004551 u64 num_devs;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004552
4553 /*
4554 * Needed because we can end up allocating a system chunk and for an
4555 * atomic and race free space reservation in the chunk block reserve.
4556 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01004557 lockdep_assert_held(&fs_info->chunk_mutex);
Liu Bo15d1ff82012-03-29 09:57:44 -04004558
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004559 info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
Liu Bo15d1ff82012-03-29 09:57:44 -04004560 spin_lock(&info->lock);
Liu Bo41361352017-02-13 15:42:21 -08004561 left = info->total_bytes - btrfs_space_info_used(info, true);
Liu Bo15d1ff82012-03-29 09:57:44 -04004562 spin_unlock(&info->lock);
4563
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004564 num_devs = get_profile_num_devs(fs_info, type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004565
4566 /* num_devs device items to update and 1 chunk item to add or remove */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004567 thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) +
4568 btrfs_calc_trans_metadata_size(fs_info, 1);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004569
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004570 if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
4571 btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
4572 left, thresh, type);
4573 dump_space_info(fs_info, info, 0, 0);
Liu Bo15d1ff82012-03-29 09:57:44 -04004574 }
4575
4576 if (left < thresh) {
Jeff Mahoney1b868262017-05-17 11:38:35 -04004577 u64 flags = btrfs_system_alloc_profile(fs_info);
Liu Bo15d1ff82012-03-29 09:57:44 -04004578
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004579 /*
4580 * Ignore failure to create system chunk. We might end up not
4581 * needing it, as we might not need to COW all nodes/leafs from
4582 * the paths we visit in the chunk tree (they were already COWed
4583 * or created in the current transaction for example).
4584 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004585 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004586 }
4587
4588 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004589 ret = btrfs_block_rsv_add(fs_info->chunk_root,
4590 &fs_info->chunk_block_rsv,
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004591 thresh, BTRFS_RESERVE_NO_FLUSH);
4592 if (!ret)
4593 trans->chunk_bytes_reserved += thresh;
Liu Bo15d1ff82012-03-29 09:57:44 -04004594 }
4595}
4596
Liu Bo28b737f2016-07-29 11:09:50 -07004597/*
4598 * If force is CHUNK_ALLOC_FORCE:
4599 * - return 1 if it successfully allocates a chunk,
4600 * - return errors including -ENOSPC otherwise.
4601 * If force is NOT CHUNK_ALLOC_FORCE:
4602 * - return 0 if it doesn't need to allocate a new chunk,
4603 * - return 1 if it successfully allocates a chunk,
4604 * - return errors including -ENOSPC otherwise.
4605 */
Chris Mason6324fbf2008-03-24 15:01:59 -04004606static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004607 struct btrfs_fs_info *fs_info, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04004608{
4609 struct btrfs_space_info *space_info;
Josef Bacik6d741192011-04-11 20:20:11 -04004610 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05004611 int ret = 0;
4612
Josef Bacikc6b305a2012-12-18 09:16:16 -05004613 /* Don't re-enter if we're already allocating a chunk */
4614 if (trans->allocating_chunk)
4615 return -ENOSPC;
4616
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004617 space_info = __find_space_info(fs_info, flags);
Jeff Mahoneydc2d3002018-03-20 15:25:25 -04004618 ASSERT(space_info);
Chris Mason6324fbf2008-03-24 15:01:59 -04004619
Josef Bacik6d741192011-04-11 20:20:11 -04004620again:
Josef Bacik25179202008-10-29 14:49:05 -04004621 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05004622 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004623 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04004624 if (space_info->full) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004625 if (should_alloc_chunk(fs_info, space_info, force))
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004626 ret = -ENOSPC;
4627 else
4628 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04004629 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004630 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04004631 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004632
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004633 if (!should_alloc_chunk(fs_info, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04004634 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04004635 return 0;
4636 } else if (space_info->chunk_alloc) {
4637 wait_for_alloc = 1;
4638 } else {
4639 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04004640 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004641
Josef Bacik25179202008-10-29 14:49:05 -04004642 spin_unlock(&space_info->lock);
4643
Josef Bacik6d741192011-04-11 20:20:11 -04004644 mutex_lock(&fs_info->chunk_mutex);
4645
4646 /*
4647 * The chunk_mutex is held throughout the entirety of a chunk
4648 * allocation, so once we've acquired the chunk_mutex we know that the
4649 * other guy is done and we need to recheck and see if we should
4650 * allocate.
4651 */
4652 if (wait_for_alloc) {
4653 mutex_unlock(&fs_info->chunk_mutex);
4654 wait_for_alloc = 0;
Nikolay Borisov1e1c50a2018-04-05 10:40:15 +03004655 cond_resched();
Josef Bacik6d741192011-04-11 20:20:11 -04004656 goto again;
4657 }
4658
Josef Bacikc6b305a2012-12-18 09:16:16 -05004659 trans->allocating_chunk = true;
4660
Josef Bacik97e728d2009-04-21 17:40:57 -04004661 /*
Josef Bacik67377732010-09-16 16:19:09 -04004662 * If we have mixed data/metadata chunks we want to make sure we keep
4663 * allocating mixed chunks instead of individual chunks.
4664 */
4665 if (btrfs_mixed_space_info(space_info))
4666 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
4667
4668 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04004669 * if we're doing a data chunk, go ahead and make sure that
4670 * we keep a reasonable number of metadata chunks allocated in the
4671 * FS as well.
4672 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04004673 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04004674 fs_info->data_chunk_allocations++;
4675 if (!(fs_info->data_chunk_allocations %
4676 fs_info->metadata_ratio))
4677 force_metadata_allocation(fs_info);
4678 }
4679
Liu Bo15d1ff82012-03-29 09:57:44 -04004680 /*
4681 * Check if we have enough space in SYSTEM chunk because we may need
4682 * to update devices.
4683 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004684 check_system_chunk(trans, fs_info, flags);
Liu Bo15d1ff82012-03-29 09:57:44 -04004685
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004686 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05004687 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004688
Josef Bacik9ed74f22009-09-11 16:12:44 -04004689 spin_lock(&space_info->lock);
Nikolay Borisov57f16422018-04-11 11:21:19 +03004690 if (ret < 0) {
4691 if (ret == -ENOSPC)
4692 space_info->full = 1;
4693 else
4694 goto out;
4695 } else {
Yan, Zheng424499d2010-05-16 10:46:25 -04004696 ret = 1;
Nikolay Borisov57f16422018-04-11 11:21:19 +03004697 }
Josef Bacik6d741192011-04-11 20:20:11 -04004698
Chris Mason0e4f8f82011-04-15 16:05:44 -04004699 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004700out:
Josef Bacik6d741192011-04-11 20:20:11 -04004701 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004702 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03004703 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana00d80e32015-07-20 14:56:20 +01004704 /*
4705 * When we allocate a new chunk we reserve space in the chunk block
4706 * reserve to make sure we can COW nodes/leafs in the chunk tree or
4707 * add new nodes/leafs to it if we end up needing to do it when
4708 * inserting the chunk item and updating device items as part of the
4709 * second phase of chunk allocation, performed by
4710 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
4711 * large number of new block groups to create in our transaction
4712 * handle's new_bgs list to avoid exhausting the chunk block reserve
4713 * in extreme cases - like having a single transaction create many new
4714 * block groups when starting to write out the free space caches of all
4715 * the block groups that were made dirty during the lifetime of the
4716 * transaction.
4717 */
Filipe Mananad9a05402015-10-03 13:13:13 +01004718 if (trans->can_flush_pending_bgs &&
Byongho Leeee221842015-12-15 01:42:10 +09004719 trans->chunk_bytes_reserved >= (u64)SZ_2M) {
Nikolay Borisov6c686b32018-02-07 17:55:40 +02004720 btrfs_create_pending_block_groups(trans);
Filipe Manana00d80e32015-07-20 14:56:20 +01004721 btrfs_trans_release_chunk_metadata(trans);
4722 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004723 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04004724}
4725
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004726static int can_overcommit(struct btrfs_fs_info *fs_info,
Josef Bacika80c8dc2012-09-06 16:59:33 -04004727 struct btrfs_space_info *space_info, u64 bytes,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004728 enum btrfs_reserve_flush_enum flush,
4729 bool system_chunk)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004730{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004731 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04004732 u64 profile;
Miao Xie3c76cd82013-04-25 10:12:38 +00004733 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004734 u64 avail;
4735 u64 used;
4736
Josef Bacik957780e2016-05-17 13:30:55 -04004737 /* Don't overcommit when in mixed mode. */
4738 if (space_info->flags & BTRFS_BLOCK_GROUP_DATA)
4739 return 0;
4740
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004741 if (system_chunk)
4742 profile = btrfs_system_alloc_profile(fs_info);
4743 else
4744 profile = btrfs_metadata_alloc_profile(fs_info);
4745
Liu Bo41361352017-02-13 15:42:21 -08004746 used = btrfs_space_info_used(space_info, false);
Josef Bacik96f1bb52013-01-30 17:02:51 -05004747
Josef Bacik96f1bb52013-01-30 17:02:51 -05004748 /*
4749 * We only want to allow over committing if we have lots of actual space
4750 * free, but if we don't have enough space to handle the global reserve
4751 * space then we could end up having a real enospc problem when trying
4752 * to allocate a chunk or some other such important allocation.
4753 */
Miao Xie3c76cd82013-04-25 10:12:38 +00004754 spin_lock(&global_rsv->lock);
4755 space_size = calc_global_rsv_need_space(global_rsv);
4756 spin_unlock(&global_rsv->lock);
4757 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05004758 return 0;
4759
4760 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004761
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004762 avail = atomic64_read(&fs_info->free_chunk_space);
Josef Bacika80c8dc2012-09-06 16:59:33 -04004763
4764 /*
4765 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05004766 * space is actually useable. For raid56, the space info used
4767 * doesn't include the parity drive, so we don't have to
4768 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04004769 */
4770 if (profile & (BTRFS_BLOCK_GROUP_DUP |
4771 BTRFS_BLOCK_GROUP_RAID1 |
4772 BTRFS_BLOCK_GROUP_RAID10))
4773 avail >>= 1;
4774
4775 /*
Miao Xie561c2942012-10-16 11:32:18 +00004776 * If we aren't flushing all things, let us overcommit up to
4777 * 1/2th of the space. If we can flush, don't let us overcommit
4778 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04004779 */
Miao Xie08e007d2012-10-16 11:33:38 +00004780 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04004781 avail >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004782 else
Josef Bacik14575ae2013-09-17 10:48:00 -04004783 avail >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004784
Josef Bacik14575ae2013-09-17 10:48:00 -04004785 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004786 return 1;
4787 return 0;
4788}
4789
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004790static void btrfs_writeback_inodes_sb_nr(struct btrfs_fs_info *fs_info,
Miao Xie6c255e62014-03-06 13:55:01 +08004791 unsigned long nr_pages, int nr_items)
Miao Xieda633a42012-12-20 11:19:09 +00004792{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004793 struct super_block *sb = fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00004794
Josef Bacik925a6ef2013-06-20 12:31:27 -04004795 if (down_read_trylock(&sb->s_umount)) {
4796 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4797 up_read(&sb->s_umount);
4798 } else {
Miao Xieda633a42012-12-20 11:19:09 +00004799 /*
4800 * We needn't worry the filesystem going from r/w to r/o though
4801 * we don't acquire ->s_umount mutex, because the filesystem
4802 * should guarantee the delalloc inodes list be empty after
4803 * the filesystem is readonly(all dirty pages are written to
4804 * the disk).
4805 */
Nikolay Borisov82b3e532018-04-23 10:54:13 +03004806 btrfs_start_delalloc_roots(fs_info, nr_items);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004807 if (!current->journal_info)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004808 btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1);
Miao Xieda633a42012-12-20 11:19:09 +00004809 }
4810}
4811
Chris Mason6374e57a2017-06-23 09:48:21 -07004812static inline u64 calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004813 u64 to_reclaim)
Miao Xie18cd8ea2013-11-04 23:13:22 +08004814{
4815 u64 bytes;
Chris Mason6374e57a2017-06-23 09:48:21 -07004816 u64 nr;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004817
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004818 bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Chris Mason6374e57a2017-06-23 09:48:21 -07004819 nr = div64_u64(to_reclaim, bytes);
Miao Xie18cd8ea2013-11-04 23:13:22 +08004820 if (!nr)
4821 nr = 1;
4822 return nr;
4823}
4824
Byongho Leeee221842015-12-15 01:42:10 +09004825#define EXTENT_SIZE_PER_ITEM SZ_256K
Miao Xiec61a16a2013-11-04 23:13:23 +08004826
Yan, Zheng5da9d012010-05-16 10:46:25 -04004827/*
4828 * shrink metadata reservation for delalloc
4829 */
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004830static void shrink_delalloc(struct btrfs_fs_info *fs_info, u64 to_reclaim,
4831 u64 orig, bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004832{
Josef Bacik0019f102010-10-15 15:18:40 -04004833 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004834 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004835 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004836 u64 max_reclaim;
Chris Mason6374e57a2017-06-23 09:48:21 -07004837 u64 items;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004838 long time_left;
Miao Xied3ee29e32013-11-04 23:13:20 +08004839 unsigned long nr_pages;
4840 int loops;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004841
Miao Xiec61a16a2013-11-04 23:13:23 +08004842 /* Calc the number of the pages we need flush for space reservation */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004843 items = calc_reclaim_items_nr(fs_info, to_reclaim);
Chris Mason6374e57a2017-06-23 09:48:21 -07004844 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004845
Josef Bacik663350a2011-11-03 22:54:25 -04004846 trans = (struct btrfs_trans_handle *)current->journal_info;
Josef Bacik69fe2d72017-10-19 14:15:57 -04004847 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Chris Masonbf9022e2010-10-26 13:40:45 -04004848
Miao Xie963d6782013-01-29 10:10:51 +00004849 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004850 &fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004851 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004852 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004853 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004854 if (wait_ordered)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004855 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004856 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004857 }
4858
Miao Xied3ee29e32013-11-04 23:13:20 +08004859 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004860 while (delalloc_bytes && loops < 3) {
4861 max_reclaim = min(delalloc_bytes, to_reclaim);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004862 nr_pages = max_reclaim >> PAGE_SHIFT;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004863 btrfs_writeback_inodes_sb_nr(fs_info, nr_pages, items);
Josef Bacikdea31f52012-09-06 16:47:00 -04004864 /*
4865 * We need to wait for the async pages to actually start before
4866 * we do anything.
4867 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004868 max_reclaim = atomic_read(&fs_info->async_delalloc_pages);
Miao Xie9f3a0742013-11-04 23:13:24 +08004869 if (!max_reclaim)
4870 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004871
Miao Xie9f3a0742013-11-04 23:13:24 +08004872 if (max_reclaim <= nr_pages)
4873 max_reclaim = 0;
4874 else
4875 max_reclaim -= nr_pages;
4876
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004877 wait_event(fs_info->async_submit_wait,
4878 atomic_read(&fs_info->async_delalloc_pages) <=
Miao Xie9f3a0742013-11-04 23:13:24 +08004879 (int)max_reclaim);
4880skip_async:
Josef Bacik0019f102010-10-15 15:18:40 -04004881 spin_lock(&space_info->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04004882 if (list_empty(&space_info->tickets) &&
4883 list_empty(&space_info->priority_tickets)) {
4884 spin_unlock(&space_info->lock);
4885 break;
4886 }
Josef Bacik0019f102010-10-15 15:18:40 -04004887 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004888
Chris Mason36e39c42011-03-12 07:08:42 -05004889 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004890 if (wait_ordered && !trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004891 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf104d042011-10-14 13:56:58 -04004892 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004893 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004894 if (time_left)
4895 break;
4896 }
Miao Xie963d6782013-01-29 10:10:51 +00004897 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004898 &fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004899 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004900}
4901
Josef Bacik996478c2017-08-22 16:00:39 -04004902struct reserve_ticket {
4903 u64 bytes;
4904 int error;
4905 struct list_head list;
4906 wait_queue_head_t wait;
4907};
4908
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004909/**
Josef Bacik663350a2011-11-03 22:54:25 -04004910 * maybe_commit_transaction - possibly commit the transaction if its ok to
4911 * @root - the root we're allocating for
4912 * @bytes - the number of bytes we want to reserve
4913 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004914 *
Josef Bacik663350a2011-11-03 22:54:25 -04004915 * This will check to make sure that committing the transaction will actually
4916 * get us somewhere and then commit the transaction if it does. Otherwise it
4917 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004918 */
Jeff Mahoney0c9ab342017-02-15 16:28:28 -05004919static int may_commit_transaction(struct btrfs_fs_info *fs_info,
Josef Bacik996478c2017-08-22 16:00:39 -04004920 struct btrfs_space_info *space_info)
Josef Bacik663350a2011-11-03 22:54:25 -04004921{
Josef Bacik996478c2017-08-22 16:00:39 -04004922 struct reserve_ticket *ticket = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004923 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;
Josef Bacik663350a2011-11-03 22:54:25 -04004924 struct btrfs_trans_handle *trans;
Josef Bacik996478c2017-08-22 16:00:39 -04004925 u64 bytes;
Josef Bacik663350a2011-11-03 22:54:25 -04004926
4927 trans = (struct btrfs_trans_handle *)current->journal_info;
4928 if (trans)
4929 return -EAGAIN;
4930
Josef Bacik996478c2017-08-22 16:00:39 -04004931 spin_lock(&space_info->lock);
4932 if (!list_empty(&space_info->priority_tickets))
4933 ticket = list_first_entry(&space_info->priority_tickets,
4934 struct reserve_ticket, list);
4935 else if (!list_empty(&space_info->tickets))
4936 ticket = list_first_entry(&space_info->tickets,
4937 struct reserve_ticket, list);
4938 bytes = (ticket) ? ticket->bytes : 0;
4939 spin_unlock(&space_info->lock);
4940
4941 if (!bytes)
4942 return 0;
Josef Bacik663350a2011-11-03 22:54:25 -04004943
4944 /* See if there is enough pinned space to make this reservation */
Josef Bacikb150a4f2013-06-19 15:00:04 -04004945 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Miao Xie0424c542014-03-06 13:54:59 +08004946 bytes) >= 0)
Josef Bacik663350a2011-11-03 22:54:25 -04004947 goto commit;
Josef Bacik663350a2011-11-03 22:54:25 -04004948
4949 /*
4950 * See if there is some space in the delayed insertion reservation for
4951 * this reservation.
4952 */
4953 if (space_info != delayed_rsv->space_info)
4954 return -ENOSPC;
4955
4956 spin_lock(&delayed_rsv->lock);
Josef Bacik996478c2017-08-22 16:00:39 -04004957 if (delayed_rsv->size > bytes)
4958 bytes = 0;
4959 else
4960 bytes -= delayed_rsv->size;
Nikolay Borisov057aac32017-11-07 11:22:54 +02004961 spin_unlock(&delayed_rsv->lock);
4962
Josef Bacikb150a4f2013-06-19 15:00:04 -04004963 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Josef Bacik996478c2017-08-22 16:00:39 -04004964 bytes) < 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004965 return -ENOSPC;
4966 }
Josef Bacik663350a2011-11-03 22:54:25 -04004967
4968commit:
Jeff Mahoneya9b33112017-05-17 11:38:34 -04004969 trans = btrfs_join_transaction(fs_info->extent_root);
Josef Bacik663350a2011-11-03 22:54:25 -04004970 if (IS_ERR(trans))
4971 return -ENOSPC;
4972
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004973 return btrfs_commit_transaction(trans);
Josef Bacik663350a2011-11-03 22:54:25 -04004974}
4975
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03004976/*
4977 * Try to flush some data based on policy set by @state. This is only advisory
4978 * and may fail for various reasons. The caller is supposed to examine the
4979 * state of @space_info to detect the outcome.
4980 */
4981static void flush_space(struct btrfs_fs_info *fs_info,
Josef Bacik96c3f432012-06-21 14:05:49 -04004982 struct btrfs_space_info *space_info, u64 num_bytes,
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03004983 int state)
Josef Bacik96c3f432012-06-21 14:05:49 -04004984{
Jeff Mahoneya9b33112017-05-17 11:38:34 -04004985 struct btrfs_root *root = fs_info->extent_root;
Josef Bacik96c3f432012-06-21 14:05:49 -04004986 struct btrfs_trans_handle *trans;
4987 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004988 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004989
4990 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004991 case FLUSH_DELAYED_ITEMS_NR:
4992 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08004993 if (state == FLUSH_DELAYED_ITEMS_NR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004994 nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004995 else
Josef Bacik96c3f432012-06-21 14:05:49 -04004996 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004997
Josef Bacik96c3f432012-06-21 14:05:49 -04004998 trans = btrfs_join_transaction(root);
4999 if (IS_ERR(trans)) {
5000 ret = PTR_ERR(trans);
5001 break;
5002 }
Nikolay Borisove5c304e62018-02-07 17:55:43 +02005003 ret = btrfs_run_delayed_items_nr(trans, nr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005004 btrfs_end_transaction(trans);
Josef Bacik96c3f432012-06-21 14:05:49 -04005005 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04005006 case FLUSH_DELALLOC:
5007 case FLUSH_DELALLOC_WAIT:
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005008 shrink_delalloc(fs_info, num_bytes * 2, num_bytes,
Josef Bacik67b0fd62012-09-24 13:42:00 -04005009 state == FLUSH_DELALLOC_WAIT);
5010 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04005011 case ALLOC_CHUNK:
5012 trans = btrfs_join_transaction(root);
5013 if (IS_ERR(trans)) {
5014 ret = PTR_ERR(trans);
5015 break;
5016 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005017 ret = do_chunk_alloc(trans, fs_info,
Jeff Mahoney1b868262017-05-17 11:38:35 -04005018 btrfs_metadata_alloc_profile(fs_info),
Josef Bacikea658ba2012-09-11 16:57:25 -04005019 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005020 btrfs_end_transaction(trans);
Alex Lyakaseecba892015-12-06 12:32:31 +02005021 if (ret > 0 || ret == -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04005022 ret = 0;
5023 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04005024 case COMMIT_TRANS:
Josef Bacik996478c2017-08-22 16:00:39 -04005025 ret = may_commit_transaction(fs_info, space_info);
Josef Bacik96c3f432012-06-21 14:05:49 -04005026 break;
5027 default:
5028 ret = -ENOSPC;
5029 break;
5030 }
5031
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005032 trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, state,
5033 ret);
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03005034 return;
Josef Bacik96c3f432012-06-21 14:05:49 -04005035}
Miao Xie21c7e752014-05-13 17:29:04 -07005036
5037static inline u64
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005038btrfs_calc_reclaim_metadata_size(struct btrfs_fs_info *fs_info,
5039 struct btrfs_space_info *space_info,
5040 bool system_chunk)
Miao Xie21c7e752014-05-13 17:29:04 -07005041{
Josef Bacik957780e2016-05-17 13:30:55 -04005042 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07005043 u64 used;
5044 u64 expected;
Josef Bacik957780e2016-05-17 13:30:55 -04005045 u64 to_reclaim = 0;
Miao Xie21c7e752014-05-13 17:29:04 -07005046
Josef Bacik957780e2016-05-17 13:30:55 -04005047 list_for_each_entry(ticket, &space_info->tickets, list)
5048 to_reclaim += ticket->bytes;
5049 list_for_each_entry(ticket, &space_info->priority_tickets, list)
5050 to_reclaim += ticket->bytes;
5051 if (to_reclaim)
5052 return to_reclaim;
Miao Xie21c7e752014-05-13 17:29:04 -07005053
Wang Xiaoguange0af2482016-08-31 19:46:16 +08005054 to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005055 if (can_overcommit(fs_info, space_info, to_reclaim,
5056 BTRFS_RESERVE_FLUSH_ALL, system_chunk))
Wang Xiaoguange0af2482016-08-31 19:46:16 +08005057 return 0;
5058
Nikolay Borisov0eee8a42017-06-14 11:35:34 +03005059 used = btrfs_space_info_used(space_info, true);
5060
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005061 if (can_overcommit(fs_info, space_info, SZ_1M,
5062 BTRFS_RESERVE_FLUSH_ALL, system_chunk))
Miao Xie21c7e752014-05-13 17:29:04 -07005063 expected = div_factor_fine(space_info->total_bytes, 95);
5064 else
5065 expected = div_factor_fine(space_info->total_bytes, 90);
5066
5067 if (used > expected)
5068 to_reclaim = used - expected;
5069 else
5070 to_reclaim = 0;
5071 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
5072 space_info->bytes_reserved);
Miao Xie21c7e752014-05-13 17:29:04 -07005073 return to_reclaim;
5074}
5075
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005076static inline int need_do_async_reclaim(struct btrfs_fs_info *fs_info,
5077 struct btrfs_space_info *space_info,
5078 u64 used, bool system_chunk)
Miao Xie21c7e752014-05-13 17:29:04 -07005079{
Josef Bacik365c5312015-02-18 13:58:15 -08005080 u64 thresh = div_factor_fine(space_info->total_bytes, 98);
5081
5082 /* If we're just plain full then async reclaim just slows us down. */
Josef Bacikbaee8792016-01-26 09:35:38 -05005083 if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh)
Josef Bacik365c5312015-02-18 13:58:15 -08005084 return 0;
5085
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005086 if (!btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5087 system_chunk))
Liu Bo25ce4592014-09-10 12:58:50 +08005088 return 0;
Liu Bo25ce4592014-09-10 12:58:50 +08005089
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005090 return (used >= thresh && !btrfs_fs_closing(fs_info) &&
5091 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
Miao Xie21c7e752014-05-13 17:29:04 -07005092}
5093
Josef Bacik957780e2016-05-17 13:30:55 -04005094static void wake_all_tickets(struct list_head *head)
Miao Xie21c7e752014-05-13 17:29:04 -07005095{
Josef Bacik957780e2016-05-17 13:30:55 -04005096 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07005097
Josef Bacik957780e2016-05-17 13:30:55 -04005098 while (!list_empty(head)) {
5099 ticket = list_first_entry(head, struct reserve_ticket, list);
5100 list_del_init(&ticket->list);
5101 ticket->error = -ENOSPC;
5102 wake_up(&ticket->wait);
Miao Xie21c7e752014-05-13 17:29:04 -07005103 }
Miao Xie21c7e752014-05-13 17:29:04 -07005104}
5105
Josef Bacik957780e2016-05-17 13:30:55 -04005106/*
5107 * This is for normal flushers, we can wait all goddamned day if we want to. We
5108 * will loop and continuously try to flush as long as we are making progress.
5109 * We count progress as clearing off tickets each time we have to loop.
5110 */
Miao Xie21c7e752014-05-13 17:29:04 -07005111static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
5112{
5113 struct btrfs_fs_info *fs_info;
5114 struct btrfs_space_info *space_info;
5115 u64 to_reclaim;
5116 int flush_state;
Josef Bacik957780e2016-05-17 13:30:55 -04005117 int commit_cycles = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005118 u64 last_tickets_id;
Miao Xie21c7e752014-05-13 17:29:04 -07005119
5120 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
5121 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
5122
Josef Bacik957780e2016-05-17 13:30:55 -04005123 spin_lock(&space_info->lock);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005124 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5125 false);
Josef Bacik957780e2016-05-17 13:30:55 -04005126 if (!to_reclaim) {
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 }
Wang Xiaoguangce129652016-09-02 10:58:46 +08005131 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005132 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005133
5134 flush_state = FLUSH_DELAYED_ITEMS_NR;
5135 do {
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03005136 flush_space(fs_info, space_info, to_reclaim, flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005137 spin_lock(&space_info->lock);
5138 if (list_empty(&space_info->tickets)) {
5139 space_info->flush = 0;
5140 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005141 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005142 }
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005143 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info,
5144 space_info,
5145 false);
Wang Xiaoguangce129652016-09-02 10:58:46 +08005146 if (last_tickets_id == space_info->tickets_id) {
Josef Bacik957780e2016-05-17 13:30:55 -04005147 flush_state++;
5148 } else {
Wang Xiaoguangce129652016-09-02 10:58:46 +08005149 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005150 flush_state = FLUSH_DELAYED_ITEMS_NR;
5151 if (commit_cycles)
5152 commit_cycles--;
5153 }
5154
5155 if (flush_state > COMMIT_TRANS) {
5156 commit_cycles++;
5157 if (commit_cycles > 2) {
5158 wake_all_tickets(&space_info->tickets);
5159 space_info->flush = 0;
5160 } else {
5161 flush_state = FLUSH_DELAYED_ITEMS_NR;
5162 }
5163 }
5164 spin_unlock(&space_info->lock);
5165 } while (flush_state <= COMMIT_TRANS);
Miao Xie21c7e752014-05-13 17:29:04 -07005166}
5167
5168void btrfs_init_async_reclaim_work(struct work_struct *work)
5169{
5170 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
5171}
5172
Josef Bacik957780e2016-05-17 13:30:55 -04005173static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
5174 struct btrfs_space_info *space_info,
5175 struct reserve_ticket *ticket)
5176{
5177 u64 to_reclaim;
5178 int flush_state = FLUSH_DELAYED_ITEMS_NR;
5179
5180 spin_lock(&space_info->lock);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005181 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5182 false);
Josef Bacik957780e2016-05-17 13:30:55 -04005183 if (!to_reclaim) {
5184 spin_unlock(&space_info->lock);
5185 return;
5186 }
5187 spin_unlock(&space_info->lock);
5188
5189 do {
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005190 flush_space(fs_info, space_info, to_reclaim, flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005191 flush_state++;
5192 spin_lock(&space_info->lock);
5193 if (ticket->bytes == 0) {
5194 spin_unlock(&space_info->lock);
5195 return;
5196 }
5197 spin_unlock(&space_info->lock);
5198
5199 /*
5200 * Priority flushers can't wait on delalloc without
5201 * deadlocking.
5202 */
5203 if (flush_state == FLUSH_DELALLOC ||
5204 flush_state == FLUSH_DELALLOC_WAIT)
5205 flush_state = ALLOC_CHUNK;
5206 } while (flush_state < COMMIT_TRANS);
5207}
5208
5209static int wait_reserve_ticket(struct btrfs_fs_info *fs_info,
5210 struct btrfs_space_info *space_info,
5211 struct reserve_ticket *ticket, u64 orig_bytes)
5212
5213{
5214 DEFINE_WAIT(wait);
5215 int ret = 0;
5216
5217 spin_lock(&space_info->lock);
5218 while (ticket->bytes > 0 && ticket->error == 0) {
5219 ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE);
5220 if (ret) {
5221 ret = -EINTR;
5222 break;
5223 }
5224 spin_unlock(&space_info->lock);
5225
5226 schedule();
5227
5228 finish_wait(&ticket->wait, &wait);
5229 spin_lock(&space_info->lock);
5230 }
5231 if (!ret)
5232 ret = ticket->error;
5233 if (!list_empty(&ticket->list))
5234 list_del_init(&ticket->list);
5235 if (ticket->bytes && ticket->bytes < orig_bytes) {
5236 u64 num_bytes = orig_bytes - ticket->bytes;
5237 space_info->bytes_may_use -= num_bytes;
5238 trace_btrfs_space_reservation(fs_info, "space_info",
5239 space_info->flags, num_bytes, 0);
5240 }
5241 spin_unlock(&space_info->lock);
5242
5243 return ret;
5244}
5245
Josef Bacik663350a2011-11-03 22:54:25 -04005246/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005247 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5248 * @root - the root we're allocating for
Josef Bacik957780e2016-05-17 13:30:55 -04005249 * @space_info - the space info we want to allocate from
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005250 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04005251 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005252 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04005253 * This will reserve orig_bytes number of bytes from the space info associated
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005254 * with the block_rsv. If there is not enough space it will make an attempt to
5255 * flush out space to make room. It will do this by flushing delalloc if
5256 * possible or committing the transaction. If flush is 0 then no attempts to
5257 * regain reservations will be made and this will fail if there is not enough
5258 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005259 */
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005260static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
Josef Bacik957780e2016-05-17 13:30:55 -04005261 struct btrfs_space_info *space_info,
5262 u64 orig_bytes,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005263 enum btrfs_reserve_flush_enum flush,
5264 bool system_chunk)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005265{
Josef Bacik957780e2016-05-17 13:30:55 -04005266 struct reserve_ticket ticket;
Josef Bacik2bf64752011-09-26 17:12:22 -04005267 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005268 int ret = 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005269
Josef Bacik957780e2016-05-17 13:30:55 -04005270 ASSERT(orig_bytes);
Josef Bacik8ca17f02016-05-27 13:24:13 -04005271 ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL);
5272
Yan, Zhengf0486c62010-05-16 10:46:25 -04005273 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04005274 ret = -ENOSPC;
Liu Bo41361352017-02-13 15:42:21 -08005275 used = btrfs_space_info_used(space_info, true);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005276
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005277 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005278 * If we have enough space then hooray, make our reservation and carry
5279 * on. If not see if we can overcommit, and if we can, hooray carry on.
5280 * If not things get more complicated.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005281 */
Josef Bacik957780e2016-05-17 13:30:55 -04005282 if (used + orig_bytes <= space_info->total_bytes) {
5283 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005284 trace_btrfs_space_reservation(fs_info, "space_info",
5285 space_info->flags, orig_bytes, 1);
Josef Bacik957780e2016-05-17 13:30:55 -04005286 ret = 0;
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005287 } else if (can_overcommit(fs_info, space_info, orig_bytes, flush,
5288 system_chunk)) {
Josef Bacik44734ed2012-09-28 16:04:19 -04005289 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005290 trace_btrfs_space_reservation(fs_info, "space_info",
5291 space_info->flags, orig_bytes, 1);
Josef Bacik44734ed2012-09-28 16:04:19 -04005292 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04005293 }
5294
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005295 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005296 * If we couldn't make a reservation then setup our reservation ticket
5297 * and kick the async worker if it's not already running.
Miao Xie08e007d2012-10-16 11:33:38 +00005298 *
Josef Bacik957780e2016-05-17 13:30:55 -04005299 * If we are a priority flusher then we just need to add our ticket to
5300 * the list and we will do our own flushing further down.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005301 */
Josef Bacik72bcd992012-12-18 15:16:34 -05005302 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacik957780e2016-05-17 13:30:55 -04005303 ticket.bytes = orig_bytes;
5304 ticket.error = 0;
5305 init_waitqueue_head(&ticket.wait);
5306 if (flush == BTRFS_RESERVE_FLUSH_ALL) {
5307 list_add_tail(&ticket.list, &space_info->tickets);
5308 if (!space_info->flush) {
5309 space_info->flush = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005310 trace_btrfs_trigger_flush(fs_info,
Josef Bacikf376df22016-03-25 13:25:56 -04005311 space_info->flags,
5312 orig_bytes, flush,
5313 "enospc");
Josef Bacik957780e2016-05-17 13:30:55 -04005314 queue_work(system_unbound_wq,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005315 &fs_info->async_reclaim_work);
Josef Bacik957780e2016-05-17 13:30:55 -04005316 }
5317 } else {
5318 list_add_tail(&ticket.list,
5319 &space_info->priority_tickets);
5320 }
Miao Xie21c7e752014-05-13 17:29:04 -07005321 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
5322 used += orig_bytes;
Josef Bacikf6acfd52014-09-18 11:27:17 -04005323 /*
5324 * We will do the space reservation dance during log replay,
5325 * which means we won't have fs_info->fs_root set, so don't do
5326 * the async reclaim as we will panic.
5327 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005328 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) &&
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005329 need_do_async_reclaim(fs_info, space_info,
5330 used, system_chunk) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005331 !work_busy(&fs_info->async_reclaim_work)) {
5332 trace_btrfs_trigger_flush(fs_info, space_info->flags,
5333 orig_bytes, flush, "preempt");
Miao Xie21c7e752014-05-13 17:29:04 -07005334 queue_work(system_unbound_wq,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005335 &fs_info->async_reclaim_work);
Josef Bacikf376df22016-03-25 13:25:56 -04005336 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005337 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005338 spin_unlock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00005339 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik957780e2016-05-17 13:30:55 -04005340 return ret;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005341
Josef Bacik957780e2016-05-17 13:30:55 -04005342 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005343 return wait_reserve_ticket(fs_info, space_info, &ticket,
Josef Bacik957780e2016-05-17 13:30:55 -04005344 orig_bytes);
Miao Xie08e007d2012-10-16 11:33:38 +00005345
Josef Bacik957780e2016-05-17 13:30:55 -04005346 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005347 priority_reclaim_metadata_space(fs_info, space_info, &ticket);
Josef Bacik957780e2016-05-17 13:30:55 -04005348 spin_lock(&space_info->lock);
5349 if (ticket.bytes) {
5350 if (ticket.bytes < orig_bytes) {
5351 u64 num_bytes = orig_bytes - ticket.bytes;
5352 space_info->bytes_may_use -= num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005353 trace_btrfs_space_reservation(fs_info, "space_info",
5354 space_info->flags,
5355 num_bytes, 0);
Miao Xie08e007d2012-10-16 11:33:38 +00005356
Josef Bacik957780e2016-05-17 13:30:55 -04005357 }
5358 list_del_init(&ticket.list);
5359 ret = -ENOSPC;
5360 }
5361 spin_unlock(&space_info->lock);
5362 ASSERT(list_empty(&ticket.list));
5363 return ret;
5364}
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005365
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005366/**
5367 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5368 * @root - the root we're allocating for
5369 * @block_rsv - the block_rsv we're allocating for
5370 * @orig_bytes - the number of bytes we want
5371 * @flush - whether or not we can flush to make our reservation
5372 *
Josef Bacik957780e2016-05-17 13:30:55 -04005373 * This will reserve orgi_bytes number of bytes from the space info associated
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005374 * with the block_rsv. If there is not enough space it will make an attempt to
5375 * flush out space to make room. It will do this by flushing delalloc if
5376 * possible or committing the transaction. If flush is 0 then no attempts to
5377 * regain reservations will be made and this will fail if there is not enough
5378 * space already.
5379 */
5380static int reserve_metadata_bytes(struct btrfs_root *root,
5381 struct btrfs_block_rsv *block_rsv,
5382 u64 orig_bytes,
5383 enum btrfs_reserve_flush_enum flush)
5384{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005385 struct btrfs_fs_info *fs_info = root->fs_info;
5386 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04005387 int ret;
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005388 bool system_chunk = (root == fs_info->chunk_root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005389
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005390 ret = __reserve_metadata_bytes(fs_info, block_rsv->space_info,
5391 orig_bytes, flush, system_chunk);
Josef Bacik5d803662013-02-07 16:06:02 -05005392 if (ret == -ENOSPC &&
5393 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
Josef Bacik5d803662013-02-07 16:06:02 -05005394 if (block_rsv != global_rsv &&
5395 !block_rsv_use_bytes(global_rsv, orig_bytes))
5396 ret = 0;
5397 }
Nikolay Borisov9a3daff2017-12-15 12:05:37 +02005398 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005399 trace_btrfs_space_reservation(fs_info, "space_info:enospc",
Josef Bacik957780e2016-05-17 13:30:55 -04005400 block_rsv->space_info->flags,
5401 orig_bytes, 1);
Nikolay Borisov9a3daff2017-12-15 12:05:37 +02005402
5403 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
5404 dump_space_info(fs_info, block_rsv->space_info,
5405 orig_bytes, 0);
5406 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005407 return ret;
5408}
5409
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005410static struct btrfs_block_rsv *get_block_rsv(
5411 const struct btrfs_trans_handle *trans,
5412 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005413{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005414 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik4c13d752011-08-30 11:31:29 -04005415 struct btrfs_block_rsv *block_rsv = NULL;
5416
Alexandru Moisee9cf4392015-09-09 00:18:50 +00005417 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005418 (root == fs_info->csum_root && trans->adding_csums) ||
5419 (root == fs_info->uuid_root))
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02005420 block_rsv = trans->block_rsv;
5421
Josef Bacik4c13d752011-08-30 11:31:29 -04005422 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005423 block_rsv = root->block_rsv;
5424
5425 if (!block_rsv)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005426 block_rsv = &fs_info->empty_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005427
5428 return block_rsv;
5429}
5430
5431static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
5432 u64 num_bytes)
5433{
5434 int ret = -ENOSPC;
5435 spin_lock(&block_rsv->lock);
5436 if (block_rsv->reserved >= num_bytes) {
5437 block_rsv->reserved -= num_bytes;
5438 if (block_rsv->reserved < block_rsv->size)
5439 block_rsv->full = 0;
5440 ret = 0;
5441 }
5442 spin_unlock(&block_rsv->lock);
5443 return ret;
5444}
5445
5446static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
5447 u64 num_bytes, int update_size)
5448{
5449 spin_lock(&block_rsv->lock);
5450 block_rsv->reserved += num_bytes;
5451 if (update_size)
5452 block_rsv->size += num_bytes;
5453 else if (block_rsv->reserved >= block_rsv->size)
5454 block_rsv->full = 1;
5455 spin_unlock(&block_rsv->lock);
5456}
5457
Josef Bacikd52be812013-05-29 14:54:47 -04005458int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
5459 struct btrfs_block_rsv *dest, u64 num_bytes,
5460 int min_factor)
5461{
5462 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5463 u64 min_bytes;
5464
5465 if (global_rsv->space_info != dest->space_info)
5466 return -ENOSPC;
5467
5468 spin_lock(&global_rsv->lock);
5469 min_bytes = div_factor(global_rsv->size, min_factor);
5470 if (global_rsv->reserved < min_bytes + num_bytes) {
5471 spin_unlock(&global_rsv->lock);
5472 return -ENOSPC;
5473 }
5474 global_rsv->reserved -= num_bytes;
5475 if (global_rsv->reserved < global_rsv->size)
5476 global_rsv->full = 0;
5477 spin_unlock(&global_rsv->lock);
5478
5479 block_rsv_add_bytes(dest, num_bytes, 1);
5480 return 0;
5481}
5482
Josef Bacik957780e2016-05-17 13:30:55 -04005483/*
5484 * This is for space we already have accounted in space_info->bytes_may_use, so
5485 * basically when we're returning space from block_rsv's.
5486 */
5487static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
5488 struct btrfs_space_info *space_info,
5489 u64 num_bytes)
5490{
5491 struct reserve_ticket *ticket;
5492 struct list_head *head;
5493 u64 used;
5494 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH;
5495 bool check_overcommit = false;
5496
5497 spin_lock(&space_info->lock);
5498 head = &space_info->priority_tickets;
5499
5500 /*
5501 * If we are over our limit then we need to check and see if we can
5502 * overcommit, and if we can't then we just need to free up our space
5503 * and not satisfy any requests.
5504 */
Nikolay Borisov0eee8a42017-06-14 11:35:34 +03005505 used = btrfs_space_info_used(space_info, true);
Josef Bacik957780e2016-05-17 13:30:55 -04005506 if (used - num_bytes >= space_info->total_bytes)
5507 check_overcommit = true;
5508again:
5509 while (!list_empty(head) && num_bytes) {
5510 ticket = list_first_entry(head, struct reserve_ticket,
5511 list);
5512 /*
5513 * We use 0 bytes because this space is already reserved, so
5514 * adding the ticket space would be a double count.
5515 */
5516 if (check_overcommit &&
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005517 !can_overcommit(fs_info, space_info, 0, flush, false))
Josef Bacik957780e2016-05-17 13:30:55 -04005518 break;
5519 if (num_bytes >= ticket->bytes) {
5520 list_del_init(&ticket->list);
5521 num_bytes -= ticket->bytes;
5522 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005523 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005524 wake_up(&ticket->wait);
5525 } else {
5526 ticket->bytes -= num_bytes;
5527 num_bytes = 0;
5528 }
5529 }
5530
5531 if (num_bytes && head == &space_info->priority_tickets) {
5532 head = &space_info->tickets;
5533 flush = BTRFS_RESERVE_FLUSH_ALL;
5534 goto again;
5535 }
5536 space_info->bytes_may_use -= num_bytes;
5537 trace_btrfs_space_reservation(fs_info, "space_info",
5538 space_info->flags, num_bytes, 0);
5539 spin_unlock(&space_info->lock);
5540}
5541
5542/*
5543 * This is for newly allocated space that isn't accounted in
5544 * space_info->bytes_may_use yet. So if we allocate a chunk or unpin an extent
5545 * we use this helper.
5546 */
5547static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
5548 struct btrfs_space_info *space_info,
5549 u64 num_bytes)
5550{
5551 struct reserve_ticket *ticket;
5552 struct list_head *head = &space_info->priority_tickets;
5553
5554again:
5555 while (!list_empty(head) && num_bytes) {
5556 ticket = list_first_entry(head, struct reserve_ticket,
5557 list);
5558 if (num_bytes >= ticket->bytes) {
5559 trace_btrfs_space_reservation(fs_info, "space_info",
5560 space_info->flags,
5561 ticket->bytes, 1);
5562 list_del_init(&ticket->list);
5563 num_bytes -= ticket->bytes;
5564 space_info->bytes_may_use += ticket->bytes;
5565 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005566 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005567 wake_up(&ticket->wait);
5568 } else {
5569 trace_btrfs_space_reservation(fs_info, "space_info",
5570 space_info->flags,
5571 num_bytes, 1);
5572 space_info->bytes_may_use += num_bytes;
5573 ticket->bytes -= num_bytes;
5574 num_bytes = 0;
5575 }
5576 }
5577
5578 if (num_bytes && head == &space_info->priority_tickets) {
5579 head = &space_info->tickets;
5580 goto again;
5581 }
5582}
5583
Josef Bacik69fe2d72017-10-19 14:15:57 -04005584static u64 block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005585 struct btrfs_block_rsv *block_rsv,
Qu Wenruoff6bc372017-12-21 13:42:04 +08005586 struct btrfs_block_rsv *dest, u64 num_bytes,
5587 u64 *qgroup_to_release_ret)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005588{
5589 struct btrfs_space_info *space_info = block_rsv->space_info;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005590 u64 qgroup_to_release = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005591 u64 ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005592
5593 spin_lock(&block_rsv->lock);
Qu Wenruoff6bc372017-12-21 13:42:04 +08005594 if (num_bytes == (u64)-1) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005595 num_bytes = block_rsv->size;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005596 qgroup_to_release = block_rsv->qgroup_rsv_size;
5597 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005598 block_rsv->size -= num_bytes;
5599 if (block_rsv->reserved >= block_rsv->size) {
5600 num_bytes = block_rsv->reserved - block_rsv->size;
5601 block_rsv->reserved = block_rsv->size;
5602 block_rsv->full = 1;
5603 } else {
5604 num_bytes = 0;
5605 }
Qu Wenruoff6bc372017-12-21 13:42:04 +08005606 if (block_rsv->qgroup_rsv_reserved >= block_rsv->qgroup_rsv_size) {
5607 qgroup_to_release = block_rsv->qgroup_rsv_reserved -
5608 block_rsv->qgroup_rsv_size;
5609 block_rsv->qgroup_rsv_reserved = block_rsv->qgroup_rsv_size;
5610 } else {
5611 qgroup_to_release = 0;
5612 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005613 spin_unlock(&block_rsv->lock);
5614
Josef Bacik69fe2d72017-10-19 14:15:57 -04005615 ret = num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005616 if (num_bytes > 0) {
5617 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00005618 spin_lock(&dest->lock);
5619 if (!dest->full) {
5620 u64 bytes_to_add;
5621
5622 bytes_to_add = dest->size - dest->reserved;
5623 bytes_to_add = min(num_bytes, bytes_to_add);
5624 dest->reserved += bytes_to_add;
5625 if (dest->reserved >= dest->size)
5626 dest->full = 1;
5627 num_bytes -= bytes_to_add;
5628 }
5629 spin_unlock(&dest->lock);
5630 }
Josef Bacik957780e2016-05-17 13:30:55 -04005631 if (num_bytes)
5632 space_info_add_old_bytes(fs_info, space_info,
5633 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005634 }
Qu Wenruoff6bc372017-12-21 13:42:04 +08005635 if (qgroup_to_release_ret)
5636 *qgroup_to_release_ret = qgroup_to_release;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005637 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005638}
5639
Josef Bacik25d609f2016-03-25 13:25:48 -04005640int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
5641 struct btrfs_block_rsv *dst, u64 num_bytes,
5642 int update_size)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005643{
5644 int ret;
5645
5646 ret = block_rsv_use_bytes(src, num_bytes);
5647 if (ret)
5648 return ret;
5649
Josef Bacik25d609f2016-03-25 13:25:48 -04005650 block_rsv_add_bytes(dst, num_bytes, update_size);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005651 return 0;
5652}
5653
Miao Xie66d8f3d2012-09-06 04:02:28 -06005654void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005655{
5656 memset(rsv, 0, sizeof(*rsv));
5657 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06005658 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005659}
5660
Josef Bacik69fe2d72017-10-19 14:15:57 -04005661void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
5662 struct btrfs_block_rsv *rsv,
5663 unsigned short type)
5664{
5665 btrfs_init_block_rsv(rsv, type);
5666 rsv->space_info = __find_space_info(fs_info,
5667 BTRFS_BLOCK_GROUP_METADATA);
5668}
5669
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005670struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
Miao Xie66d8f3d2012-09-06 04:02:28 -06005671 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005672{
5673 struct btrfs_block_rsv *block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005674
5675 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
5676 if (!block_rsv)
5677 return NULL;
5678
Josef Bacik69fe2d72017-10-19 14:15:57 -04005679 btrfs_init_metadata_block_rsv(fs_info, block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005680 return block_rsv;
5681}
5682
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005683void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005684 struct btrfs_block_rsv *rsv)
5685{
Josef Bacik2aaa6652012-08-29 14:27:18 -04005686 if (!rsv)
5687 return;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005688 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Josef Bacikdabdb642011-08-08 12:50:18 -04005689 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005690}
5691
Chris Masoncdfb0802015-04-06 18:17:00 -07005692void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
5693{
5694 kfree(rsv);
5695}
5696
Miao Xie08e007d2012-10-16 11:33:38 +00005697int btrfs_block_rsv_add(struct btrfs_root *root,
5698 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
5699 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005700{
5701 int ret;
5702
5703 if (num_bytes == 0)
5704 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005705
Miao Xie61b520a2011-11-10 20:45:05 -05005706 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005707 if (!ret) {
5708 block_rsv_add_bytes(block_rsv, num_bytes, 1);
5709 return 0;
5710 }
5711
Yan, Zhengf0486c62010-05-16 10:46:25 -04005712 return ret;
5713}
5714
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005715int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005716{
5717 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005718 int ret = -ENOSPC;
5719
5720 if (!block_rsv)
5721 return 0;
5722
5723 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04005724 num_bytes = div_factor(block_rsv->size, min_factor);
5725 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005726 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005727 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005728
Josef Bacik36ba0222011-10-18 12:15:48 -04005729 return ret;
5730}
5731
Miao Xie08e007d2012-10-16 11:33:38 +00005732int btrfs_block_rsv_refill(struct btrfs_root *root,
5733 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
5734 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04005735{
5736 u64 num_bytes = 0;
5737 int ret = -ENOSPC;
5738
5739 if (!block_rsv)
5740 return 0;
5741
5742 spin_lock(&block_rsv->lock);
5743 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04005744 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005745 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04005746 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04005747 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005748 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04005749
Yan, Zhengf0486c62010-05-16 10:46:25 -04005750 if (!ret)
5751 return 0;
5752
Miao Xieaa38a712011-11-18 17:43:00 +08005753 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04005754 if (!ret) {
5755 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005756 return 0;
5757 }
5758
Josef Bacik13553e52011-08-08 13:33:21 -04005759 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005760}
5761
Josef Bacik69fe2d72017-10-19 14:15:57 -04005762/**
5763 * btrfs_inode_rsv_refill - refill the inode block rsv.
5764 * @inode - the inode we are refilling.
5765 * @flush - the flusing restriction.
5766 *
5767 * Essentially the same as btrfs_block_rsv_refill, except it uses the
5768 * block_rsv->size as the minimum size. We'll either refill the missing amount
5769 * or return if we already have enough space. This will also handle the resreve
5770 * tracepoint for the reserved amount.
5771 */
Qu Wenruo3f2dd7a2017-11-17 15:14:19 +08005772static int btrfs_inode_rsv_refill(struct btrfs_inode *inode,
5773 enum btrfs_reserve_flush_enum flush)
Josef Bacik69fe2d72017-10-19 14:15:57 -04005774{
5775 struct btrfs_root *root = inode->root;
5776 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
5777 u64 num_bytes = 0;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005778 u64 qgroup_num_bytes = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005779 int ret = -ENOSPC;
5780
5781 spin_lock(&block_rsv->lock);
5782 if (block_rsv->reserved < block_rsv->size)
5783 num_bytes = block_rsv->size - block_rsv->reserved;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005784 if (block_rsv->qgroup_rsv_reserved < block_rsv->qgroup_rsv_size)
5785 qgroup_num_bytes = block_rsv->qgroup_rsv_size -
5786 block_rsv->qgroup_rsv_reserved;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005787 spin_unlock(&block_rsv->lock);
5788
5789 if (num_bytes == 0)
5790 return 0;
5791
Qu Wenruoff6bc372017-12-21 13:42:04 +08005792 ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_num_bytes, true);
Qu Wenruo43b18592017-12-12 15:34:32 +08005793 if (ret)
5794 return ret;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005795 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
5796 if (!ret) {
5797 block_rsv_add_bytes(block_rsv, num_bytes, 0);
5798 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5799 btrfs_ino(inode), num_bytes, 1);
Qu Wenruoff6bc372017-12-21 13:42:04 +08005800
5801 /* Don't forget to increase qgroup_rsv_reserved */
5802 spin_lock(&block_rsv->lock);
5803 block_rsv->qgroup_rsv_reserved += qgroup_num_bytes;
5804 spin_unlock(&block_rsv->lock);
5805 } else
5806 btrfs_qgroup_free_meta_prealloc(root, qgroup_num_bytes);
Josef Bacik69fe2d72017-10-19 14:15:57 -04005807 return ret;
5808}
5809
5810/**
5811 * btrfs_inode_rsv_release - release any excessive reservation.
5812 * @inode - the inode we need to release from.
Qu Wenruo43b18592017-12-12 15:34:32 +08005813 * @qgroup_free - free or convert qgroup meta.
5814 * Unlike normal operation, qgroup meta reservation needs to know if we are
5815 * freeing qgroup reservation or just converting it into per-trans. Normally
5816 * @qgroup_free is true for error handling, and false for normal release.
Josef Bacik69fe2d72017-10-19 14:15:57 -04005817 *
5818 * This is the same as btrfs_block_rsv_release, except that it handles the
5819 * tracepoint for the reservation.
5820 */
Qu Wenruo43b18592017-12-12 15:34:32 +08005821static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
Josef Bacik69fe2d72017-10-19 14:15:57 -04005822{
5823 struct btrfs_fs_info *fs_info = inode->root->fs_info;
5824 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5825 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
5826 u64 released = 0;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005827 u64 qgroup_to_release = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005828
5829 /*
5830 * Since we statically set the block_rsv->size we just want to say we
5831 * are releasing 0 bytes, and then we'll just get the reservation over
5832 * the size free'd.
5833 */
Qu Wenruoff6bc372017-12-21 13:42:04 +08005834 released = block_rsv_release_bytes(fs_info, block_rsv, global_rsv, 0,
5835 &qgroup_to_release);
Josef Bacik69fe2d72017-10-19 14:15:57 -04005836 if (released > 0)
5837 trace_btrfs_space_reservation(fs_info, "delalloc",
5838 btrfs_ino(inode), released, 0);
Qu Wenruo43b18592017-12-12 15:34:32 +08005839 if (qgroup_free)
Qu Wenruoff6bc372017-12-21 13:42:04 +08005840 btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
Qu Wenruo43b18592017-12-12 15:34:32 +08005841 else
Qu Wenruoff6bc372017-12-21 13:42:04 +08005842 btrfs_qgroup_convert_reserved_meta(inode->root,
5843 qgroup_to_release);
Josef Bacik69fe2d72017-10-19 14:15:57 -04005844}
5845
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005846void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005847 struct btrfs_block_rsv *block_rsv,
5848 u64 num_bytes)
5849{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005850 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5851
Liu Bo17504582013-12-29 21:44:50 +08005852 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04005853 block_rsv->space_info != global_rsv->space_info)
5854 global_rsv = NULL;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005855 block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes, NULL);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005856}
5857
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005858static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
5859{
5860 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
5861 struct btrfs_space_info *sinfo = block_rsv->space_info;
5862 u64 num_bytes;
5863
Josef Bacikae2e4722016-05-27 12:58:35 -04005864 /*
5865 * The global block rsv is based on the size of the extent tree, the
5866 * checksum tree and the root tree. If the fs is empty we want to set
5867 * it to a minimal amount for safety.
5868 */
5869 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) +
5870 btrfs_root_used(&fs_info->csum_root->root_item) +
5871 btrfs_root_used(&fs_info->tree_root->root_item);
5872 num_bytes = max_t(u64, num_bytes, SZ_16M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005873
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005874 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005875 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005876
Byongho Leeee221842015-12-15 01:42:10 +09005877 block_rsv->size = min_t(u64, num_bytes, SZ_512M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005878
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005879 if (block_rsv->reserved < block_rsv->size) {
Liu Bo41361352017-02-13 15:42:21 -08005880 num_bytes = btrfs_space_info_used(sinfo, true);
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005881 if (sinfo->total_bytes > num_bytes) {
5882 num_bytes = sinfo->total_bytes - num_bytes;
5883 num_bytes = min(num_bytes,
5884 block_rsv->size - block_rsv->reserved);
5885 block_rsv->reserved += num_bytes;
5886 sinfo->bytes_may_use += num_bytes;
5887 trace_btrfs_space_reservation(fs_info, "space_info",
5888 sinfo->flags, num_bytes,
5889 1);
5890 }
5891 } else if (block_rsv->reserved > block_rsv->size) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005892 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04005893 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005894 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005895 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005896 block_rsv->reserved = block_rsv->size;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005897 }
David Sterba182608c2011-05-05 13:13:16 +02005898
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005899 if (block_rsv->reserved == block_rsv->size)
5900 block_rsv->full = 1;
5901 else
5902 block_rsv->full = 0;
5903
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005904 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005905 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005906}
5907
Yan, Zhengf0486c62010-05-16 10:46:25 -04005908static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
5909{
5910 struct btrfs_space_info *space_info;
5911
5912 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
5913 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005914
5915 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005916 fs_info->global_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005917 fs_info->trans_block_rsv.space_info = space_info;
5918 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04005919 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005920
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005921 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
5922 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
5923 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
5924 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00005925 if (fs_info->quota_root)
5926 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005927 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005928
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005929 update_global_block_rsv(fs_info);
5930}
5931
5932static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
5933{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005934 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
Qu Wenruoff6bc372017-12-21 13:42:04 +08005935 (u64)-1, NULL);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005936 WARN_ON(fs_info->trans_block_rsv.size > 0);
5937 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
5938 WARN_ON(fs_info->chunk_block_rsv.size > 0);
5939 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04005940 WARN_ON(fs_info->delayed_block_rsv.size > 0);
5941 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04005942}
5943
Yan, Zhenga22285a2010-05-16 10:48:46 -04005944
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005945/*
5946 * To be called after all the new block groups attached to the transaction
5947 * handle have been created (btrfs_create_pending_block_groups()).
5948 */
5949void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
5950{
Jeff Mahoney64b63582016-06-20 17:23:41 -04005951 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005952
5953 if (!trans->chunk_bytes_reserved)
5954 return;
5955
5956 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
5957
5958 block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL,
Qu Wenruoff6bc372017-12-21 13:42:04 +08005959 trans->chunk_bytes_reserved, NULL);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005960 trans->chunk_bytes_reserved = 0;
5961}
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 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005981{
Miao Xied5c12072013-02-28 10:04:33 +00005982 u64 num_bytes;
5983 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005984 struct btrfs_fs_info *fs_info = root->fs_info;
5985 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00005986
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005987 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Miao Xied5c12072013-02-28 10:04:33 +00005988 /* One for parent inode, two for dir entries */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005989 num_bytes = 3 * fs_info->nodesize;
Qu Wenruo733e03a2017-12-12 15:34:29 +08005990 ret = btrfs_qgroup_reserve_meta_prealloc(root, num_bytes, true);
Miao Xied5c12072013-02-28 10:04:33 +00005991 if (ret)
5992 return ret;
5993 } else {
5994 num_bytes = 0;
5995 }
5996
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005997 num_bytes = btrfs_calc_trans_metadata_size(fs_info, items);
5998 rsv->space_info = __find_space_info(fs_info,
Miao Xied5c12072013-02-28 10:04:33 +00005999 BTRFS_BLOCK_GROUP_METADATA);
6000 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
6001 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04006002
6003 if (ret == -ENOSPC && use_global_rsv)
Josef Bacik25d609f2016-03-25 13:25:48 -04006004 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04006005
Gu JinXiangc4c129d2018-05-30 11:00:38 +08006006 if (ret && num_bytes)
6007 btrfs_qgroup_free_meta_prealloc(root, num_bytes);
Miao Xied5c12072013-02-28 10:04:33 +00006008
6009 return ret;
6010}
6011
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006012void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
David Sterba7775c812017-02-10 19:18:18 +01006013 struct btrfs_block_rsv *rsv)
Miao Xied5c12072013-02-28 10:04:33 +00006014{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006015 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006016}
6017
Josef Bacik69fe2d72017-10-19 14:15:57 -04006018static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
6019 struct btrfs_inode *inode)
Josef Bacik9e0baf62011-07-15 15:16:44 +00006020{
Josef Bacik69fe2d72017-10-19 14:15:57 -04006021 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
6022 u64 reserve_size = 0;
Qu Wenruoff6bc372017-12-21 13:42:04 +08006023 u64 qgroup_rsv_size = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04006024 u64 csum_leaves;
6025 unsigned outstanding_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006026
Josef Bacik69fe2d72017-10-19 14:15:57 -04006027 lockdep_assert_held(&inode->lock);
6028 outstanding_extents = inode->outstanding_extents;
6029 if (outstanding_extents)
6030 reserve_size = btrfs_calc_trans_metadata_size(fs_info,
6031 outstanding_extents + 1);
6032 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
6033 inode->csum_bytes);
6034 reserve_size += btrfs_calc_trans_metadata_size(fs_info,
6035 csum_leaves);
Qu Wenruoff6bc372017-12-21 13:42:04 +08006036 /*
6037 * For qgroup rsv, the calculation is very simple:
6038 * account one nodesize for each outstanding extent
6039 *
6040 * This is overestimating in most cases.
6041 */
6042 qgroup_rsv_size = outstanding_extents * fs_info->nodesize;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006043
Josef Bacik69fe2d72017-10-19 14:15:57 -04006044 spin_lock(&block_rsv->lock);
6045 block_rsv->size = reserve_size;
Qu Wenruoff6bc372017-12-21 13:42:04 +08006046 block_rsv->qgroup_rsv_size = qgroup_rsv_size;
Josef Bacik69fe2d72017-10-19 14:15:57 -04006047 spin_unlock(&block_rsv->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006048}
6049
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006050int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006051{
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006052 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006053 unsigned nr_extents;
Miao Xie08e007d2012-10-16 11:33:38 +00006054 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07006055 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006056 bool delalloc_lock = true;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006057
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006058 /* If we are a free space inode we need to not flush since we will be in
6059 * the middle of a transaction commit. We also don't need the delalloc
6060 * mutex since we won't race with anybody. We need this mostly to make
6061 * lockdep shut its filthy mouth.
Josef Bacikbac357dc2016-07-20 16:48:45 -07006062 *
6063 * If we have a transaction open (can happen if we call truncate_block
6064 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006065 */
6066 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00006067 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006068 delalloc_lock = false;
Nikolay Borisovda07d4a2018-01-12 16:21:05 +02006069 } else {
6070 if (current->journal_info)
6071 flush = BTRFS_RESERVE_FLUSH_LIMIT;
Josef Bacikc09544e2011-08-30 10:19:10 -04006072
Nikolay Borisovda07d4a2018-01-12 16:21:05 +02006073 if (btrfs_transaction_in_commit(fs_info))
6074 schedule_timeout(1);
6075 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006076
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006077 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006078 mutex_lock(&inode->delalloc_mutex);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006079
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006080 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006081
6082 /* Add our new extents and calculate the new rsv size. */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006083 spin_lock(&inode->lock);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006084 nr_extents = count_max_extents(num_bytes);
Josef Bacik8b62f872017-10-19 14:15:55 -04006085 btrfs_mod_outstanding_extents(inode, nr_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006086 inode->csum_bytes += num_bytes;
6087 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006088 spin_unlock(&inode->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05006089
Josef Bacik69fe2d72017-10-19 14:15:57 -04006090 ret = btrfs_inode_rsv_refill(inode, flush);
Qu Wenruo43b18592017-12-12 15:34:32 +08006091 if (unlikely(ret))
Wang Shilong88e081bf2013-03-01 11:36:01 +00006092 goto out_fail;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006093
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006094 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006095 mutex_unlock(&inode->delalloc_mutex);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006096 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00006097
6098out_fail:
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006099 spin_lock(&inode->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04006100 nr_extents = count_max_extents(num_bytes);
6101 btrfs_mod_outstanding_extents(inode, -nr_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006102 inode->csum_bytes -= num_bytes;
6103 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006104 spin_unlock(&inode->lock);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006105
Qu Wenruo43b18592017-12-12 15:34:32 +08006106 btrfs_inode_rsv_release(inode, true);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006107 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006108 mutex_unlock(&inode->delalloc_mutex);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006109 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006110}
6111
Josef Bacik7709cde2011-08-04 10:25:02 -04006112/**
6113 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
Josef Bacik8b62f872017-10-19 14:15:55 -04006114 * @inode: the inode to release the reservation for.
6115 * @num_bytes: the number of bytes we are releasing.
Qu Wenruo43b18592017-12-12 15:34:32 +08006116 * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
Josef Bacik7709cde2011-08-04 10:25:02 -04006117 *
6118 * This will release the metadata reservation for an inode. This can be called
6119 * once we complete IO for a given set of bytes to release their metadata
Josef Bacik8b62f872017-10-19 14:15:55 -04006120 * reservations, or on error for the same reason.
Josef Bacik7709cde2011-08-04 10:25:02 -04006121 */
Qu Wenruo43b18592017-12-12 15:34:32 +08006122void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
6123 bool qgroup_free)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006124{
Nikolay Borisov691fa052017-02-20 13:50:42 +02006125 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006126
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006127 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006128 spin_lock(&inode->lock);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006129 inode->csum_bytes -= num_bytes;
6130 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006131 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006132
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006133 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04006134 return;
6135
Qu Wenruo43b18592017-12-12 15:34:32 +08006136 btrfs_inode_rsv_release(inode, qgroup_free);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006137}
6138
Josef Bacik7709cde2011-08-04 10:25:02 -04006139/**
Josef Bacik8b62f872017-10-19 14:15:55 -04006140 * btrfs_delalloc_release_extents - release our outstanding_extents
6141 * @inode: the inode to balance the reservation for.
6142 * @num_bytes: the number of bytes we originally reserved with
Qu Wenruo43b18592017-12-12 15:34:32 +08006143 * @qgroup_free: do we need to free qgroup meta reservation or convert them.
Josef Bacik8b62f872017-10-19 14:15:55 -04006144 *
6145 * When we reserve space we increase outstanding_extents for the extents we may
6146 * add. Once we've set the range as delalloc or created our ordered extents we
6147 * have outstanding_extents to track the real usage, so we use this to free our
6148 * temporarily tracked outstanding_extents. This _must_ be used in conjunction
6149 * with btrfs_delalloc_reserve_metadata.
6150 */
Qu Wenruo43b18592017-12-12 15:34:32 +08006151void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes,
6152 bool qgroup_free)
Josef Bacik8b62f872017-10-19 14:15:55 -04006153{
6154 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
6155 unsigned num_extents;
Josef Bacik8b62f872017-10-19 14:15:55 -04006156
6157 spin_lock(&inode->lock);
6158 num_extents = count_max_extents(num_bytes);
6159 btrfs_mod_outstanding_extents(inode, -num_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006160 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Josef Bacik8b62f872017-10-19 14:15:55 -04006161 spin_unlock(&inode->lock);
6162
Josef Bacik8b62f872017-10-19 14:15:55 -04006163 if (btrfs_is_testing(fs_info))
6164 return;
6165
Qu Wenruo43b18592017-12-12 15:34:32 +08006166 btrfs_inode_rsv_release(inode, qgroup_free);
Josef Bacik8b62f872017-10-19 14:15:55 -04006167}
6168
6169/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006170 * btrfs_delalloc_reserve_space - reserve data and metadata space for
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006171 * delalloc
6172 * @inode: inode we're writing to
6173 * @start: start range we are writing to
6174 * @len: how long the range we are writing to
Qu Wenruo364ecf32017-02-27 15:10:38 +08006175 * @reserved: mandatory parameter, record actually reserved qgroup ranges of
6176 * current reservation.
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006177 *
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006178 * This will do the following things
6179 *
6180 * o reserve space in data space info for num bytes
6181 * and reserve precious corresponding qgroup space
6182 * (Done in check_data_free_space)
6183 *
6184 * o reserve space for metadata space, based on the number of outstanding
6185 * extents and how much csums will be needed
6186 * also reserve metadata space in a per root over-reserve method.
6187 * o add to the inodes->delalloc_bytes
6188 * o add it to the fs_info's delalloc inodes list.
6189 * (Above 3 all done in delalloc_reserve_metadata)
6190 *
6191 * Return 0 for success
6192 * Return <0 for error(-ENOSPC or -EQUOT)
6193 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08006194int btrfs_delalloc_reserve_space(struct inode *inode,
6195 struct extent_changeset **reserved, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006196{
6197 int ret;
6198
Qu Wenruo364ecf32017-02-27 15:10:38 +08006199 ret = btrfs_check_data_free_space(inode, reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006200 if (ret < 0)
6201 return ret;
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006202 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006203 if (ret < 0)
Qu Wenruobc42bda2017-02-27 15:10:39 +08006204 btrfs_free_reserved_data_space(inode, *reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006205 return ret;
6206}
6207
6208/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006209 * btrfs_delalloc_release_space - release data and metadata space for delalloc
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006210 * @inode: inode we're releasing space for
6211 * @start: start position of the space already reserved
6212 * @len: the len of the space already reserved
Josef Bacik8b62f872017-10-19 14:15:55 -04006213 * @release_bytes: the len of the space we consumed or didn't use
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006214 *
6215 * This function will release the metadata space that was not used and will
6216 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
6217 * list if there are no delalloc bytes left.
6218 * Also it will handle the qgroup reserved space.
6219 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08006220void btrfs_delalloc_release_space(struct inode *inode,
Josef Bacik8b62f872017-10-19 14:15:55 -04006221 struct extent_changeset *reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08006222 u64 start, u64 len, bool qgroup_free)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006223{
Qu Wenruo43b18592017-12-12 15:34:32 +08006224 btrfs_delalloc_release_metadata(BTRFS_I(inode), len, qgroup_free);
Qu Wenruobc42bda2017-02-27 15:10:39 +08006225 btrfs_free_reserved_data_space(inode, reserved, start, len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006226}
6227
Josef Bacikce93ec52014-11-17 15:45:48 -05006228static int update_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6202df62016-06-22 18:54:22 -04006229 struct btrfs_fs_info *info, u64 bytenr,
Josef Bacikce93ec52014-11-17 15:45:48 -05006230 u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04006231{
Josef Bacik0af3d002010-06-21 14:48:16 -04006232 struct btrfs_block_group_cache *cache = NULL;
Chris Masondb945352007-10-15 16:15:53 -04006233 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006234 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04006235 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04006236 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04006237
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006238 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00006239 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02006240 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006241 if (alloc)
6242 old_val += num_bytes;
6243 else
6244 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02006245 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00006246 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006247
Chris Masond3977122009-01-05 21:25:51 -05006248 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04006249 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006250 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006251 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006252 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
6253 BTRFS_BLOCK_GROUP_RAID1 |
6254 BTRFS_BLOCK_GROUP_RAID10))
6255 factor = 2;
6256 else
6257 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04006258 /*
6259 * If this block group has free space cache written out, we
6260 * need to make sure to load it if we are removing space. This
6261 * is because we need the unpinning stage to actually add the
6262 * space back to the block group, otherwise we will leak space.
6263 */
6264 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00006265 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04006266
Chris Masondb945352007-10-15 16:15:53 -04006267 byte_in_group = bytenr - cache->key.objectid;
6268 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04006269
Josef Bacik25179202008-10-29 14:49:05 -04006270 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006271 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04006272
Jeff Mahoney6202df62016-06-22 18:54:22 -04006273 if (btrfs_test_opt(info, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04006274 cache->disk_cache_state < BTRFS_DC_CLEAR)
6275 cache->disk_cache_state = BTRFS_DC_CLEAR;
6276
Chris Mason9078a3e2007-04-26 16:46:15 -04006277 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04006278 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04006279 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04006280 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006281 btrfs_set_block_group_used(&cache->item, old_val);
6282 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006283 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006284 cache->space_info->bytes_used += num_bytes;
6285 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04006286 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006287 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04006288 } else {
Chris Masondb945352007-10-15 16:15:53 -04006289 old_val -= num_bytes;
Filipe Mananaae0ab002014-11-26 15:28:52 +00006290 btrfs_set_block_group_used(&cache->item, old_val);
6291 cache->pinned += num_bytes;
6292 cache->space_info->bytes_pinned += num_bytes;
6293 cache->space_info->bytes_used -= num_bytes;
6294 cache->space_info->disk_used -= num_bytes * factor;
6295 spin_unlock(&cache->lock);
6296 spin_unlock(&cache->space_info->lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04006297
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006298 trace_btrfs_space_reservation(info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006299 cache->space_info->flags,
6300 num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07006301 percpu_counter_add(&cache->space_info->total_bytes_pinned,
6302 num_bytes);
Filipe Mananaae0ab002014-11-26 15:28:52 +00006303 set_extent_dirty(info->pinned_extents,
6304 bytenr, bytenr + num_bytes - 1,
6305 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04006306 }
Chris Mason1bbc6212015-04-06 12:46:08 -07006307
6308 spin_lock(&trans->transaction->dirty_bgs_lock);
6309 if (list_empty(&cache->dirty_list)) {
6310 list_add_tail(&cache->dirty_list,
6311 &trans->transaction->dirty_bgs);
Bart Van Asschebece2e82018-06-20 10:03:31 -07006312 trans->transaction->num_dirty_bgs++;
Chris Mason1bbc6212015-04-06 12:46:08 -07006313 btrfs_get_block_group(cache);
6314 }
6315 spin_unlock(&trans->transaction->dirty_bgs_lock);
6316
Filipe Manana036a9342015-11-23 15:25:16 +00006317 /*
6318 * No longer have used bytes in this block group, queue it for
6319 * deletion. We do this after adding the block group to the
6320 * dirty list to avoid races between cleaner kthread and space
6321 * cache writeout.
6322 */
6323 if (!alloc && old_val == 0) {
6324 spin_lock(&info->unused_bgs_lock);
6325 if (list_empty(&cache->bg_list)) {
6326 btrfs_get_block_group(cache);
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +08006327 trace_btrfs_add_unused_block_group(cache);
Filipe Manana036a9342015-11-23 15:25:16 +00006328 list_add_tail(&cache->bg_list,
6329 &info->unused_bgs);
6330 }
6331 spin_unlock(&info->unused_bgs_lock);
6332 }
6333
Chris Masonfa9c0d792009-04-03 09:47:43 -04006334 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04006335 total -= num_bytes;
6336 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006337 }
6338 return 0;
6339}
Chris Mason6324fbf2008-03-24 15:01:59 -04006340
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006341static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04006342{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006343 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05006344 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006345
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006346 spin_lock(&fs_info->block_group_cache_lock);
6347 bytenr = fs_info->first_logical_byte;
6348 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00006349
6350 if (bytenr < (u64)-1)
6351 return bytenr;
6352
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006353 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006354 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04006355 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006356
Yan Zhengd2fb3432008-12-11 16:30:39 -05006357 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006358 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006359
6360 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04006361}
6362
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006363static int pin_down_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006364 struct btrfs_block_group_cache *cache,
6365 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05006366{
Yan Zheng11833d62009-09-11 16:11:19 -04006367 spin_lock(&cache->space_info->lock);
6368 spin_lock(&cache->lock);
6369 cache->pinned += num_bytes;
6370 cache->space_info->bytes_pinned += num_bytes;
6371 if (reserved) {
6372 cache->reserved -= num_bytes;
6373 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05006374 }
Yan Zheng11833d62009-09-11 16:11:19 -04006375 spin_unlock(&cache->lock);
6376 spin_unlock(&cache->space_info->lock);
6377
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006378 trace_btrfs_space_reservation(fs_info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006379 cache->space_info->flags, num_bytes, 1);
Omar Sandoval4da8b762017-06-06 16:45:28 -07006380 percpu_counter_add(&cache->space_info->total_bytes_pinned, num_bytes);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006381 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006382 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05006383 return 0;
6384}
Chris Mason9078a3e2007-04-26 16:46:15 -04006385
Yan, Zhengf0486c62010-05-16 10:46:25 -04006386/*
6387 * this function must be called within transaction
6388 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006389int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006390 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04006391{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006392 struct btrfs_block_group_cache *cache;
6393
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006394 cache = btrfs_lookup_block_group(fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006395 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006396
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006397 pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006398
6399 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04006400 return 0;
6401}
Zheng Yane8569812008-09-26 10:05:48 -04006402
Yan, Zhengf0486c62010-05-16 10:46:25 -04006403/*
Chris Masone688b7252011-10-31 20:52:39 -04006404 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04006405 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006406int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04006407 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006408{
Chris Masone688b7252011-10-31 20:52:39 -04006409 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04006410 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04006411
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006412 cache = btrfs_lookup_block_group(fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006413 if (!cache)
6414 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04006415
6416 /*
6417 * pull in the free space cache (if any) so that our pin
6418 * removes the free space from the cache. We have load_only set
6419 * to one because the slow code to read in the free extents does check
6420 * the pinned extents.
6421 */
Liu Bof6373bf2012-12-27 09:01:18 +00006422 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04006423
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006424 pin_down_extent(fs_info, cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04006425
6426 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04006427 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04006428 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006429 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04006430}
6431
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006432static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
6433 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006434{
6435 int ret;
6436 struct btrfs_block_group_cache *block_group;
6437 struct btrfs_caching_control *caching_ctl;
6438
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006439 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006440 if (!block_group)
6441 return -EINVAL;
6442
6443 cache_block_group(block_group, 0);
6444 caching_ctl = get_caching_control(block_group);
6445
6446 if (!caching_ctl) {
6447 /* Logic error */
6448 BUG_ON(!block_group_cache_done(block_group));
6449 ret = btrfs_remove_free_space(block_group, start, num_bytes);
6450 } else {
6451 mutex_lock(&caching_ctl->mutex);
6452
6453 if (start >= caching_ctl->progress) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006454 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006455 } else if (start + num_bytes <= caching_ctl->progress) {
6456 ret = btrfs_remove_free_space(block_group,
6457 start, num_bytes);
6458 } else {
6459 num_bytes = caching_ctl->progress - start;
6460 ret = btrfs_remove_free_space(block_group,
6461 start, num_bytes);
6462 if (ret)
6463 goto out_lock;
6464
6465 num_bytes = (start + num_bytes) -
6466 caching_ctl->progress;
6467 start = caching_ctl->progress;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006468 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006469 }
6470out_lock:
6471 mutex_unlock(&caching_ctl->mutex);
6472 put_caching_control(caching_ctl);
6473 }
6474 btrfs_put_block_group(block_group);
6475 return ret;
6476}
6477
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006478int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info,
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006479 struct extent_buffer *eb)
6480{
6481 struct btrfs_file_extent_item *item;
6482 struct btrfs_key key;
6483 int found_type;
6484 int i;
Gu Jinxiangb89311e2018-05-22 17:46:51 +08006485 int ret = 0;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006486
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006487 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006488 return 0;
6489
6490 for (i = 0; i < btrfs_header_nritems(eb); i++) {
6491 btrfs_item_key_to_cpu(eb, &key, i);
6492 if (key.type != BTRFS_EXTENT_DATA_KEY)
6493 continue;
6494 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
6495 found_type = btrfs_file_extent_type(eb, item);
6496 if (found_type == BTRFS_FILE_EXTENT_INLINE)
6497 continue;
6498 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
6499 continue;
6500 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
6501 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Gu Jinxiangb89311e2018-05-22 17:46:51 +08006502 ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
6503 if (ret)
6504 break;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006505 }
6506
Gu Jinxiangb89311e2018-05-22 17:46:51 +08006507 return ret;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006508}
6509
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006510static void
6511btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg)
6512{
6513 atomic_inc(&bg->reservations);
6514}
6515
6516void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
6517 const u64 start)
6518{
6519 struct btrfs_block_group_cache *bg;
6520
6521 bg = btrfs_lookup_block_group(fs_info, start);
6522 ASSERT(bg);
6523 if (atomic_dec_and_test(&bg->reservations))
Peter Zijlstra46259562018-03-15 11:43:08 +01006524 wake_up_var(&bg->reservations);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006525 btrfs_put_block_group(bg);
6526}
6527
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006528void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6529{
6530 struct btrfs_space_info *space_info = bg->space_info;
6531
6532 ASSERT(bg->ro);
6533
6534 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
6535 return;
6536
6537 /*
6538 * Our block group is read only but before we set it to read only,
6539 * some task might have had allocated an extent from it already, but it
6540 * has not yet created a respective ordered extent (and added it to a
6541 * root's list of ordered extents).
6542 * Therefore wait for any task currently allocating extents, since the
6543 * block group's reservations counter is incremented while a read lock
6544 * on the groups' semaphore is held and decremented after releasing
6545 * the read access on that semaphore and creating the ordered extent.
6546 */
6547 down_write(&space_info->groups_sem);
6548 up_write(&space_info->groups_sem);
6549
Peter Zijlstra46259562018-03-15 11:43:08 +01006550 wait_var_event(&bg->reservations, !atomic_read(&bg->reservations));
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006551}
6552
Josef Bacikfb25e912011-07-26 17:00:46 -04006553/**
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006554 * btrfs_add_reserved_bytes - update the block_group and space info counters
Josef Bacikfb25e912011-07-26 17:00:46 -04006555 * @cache: The cache we are manipulating
Wang Xiaoguang18513092016-07-25 15:51:40 +08006556 * @ram_bytes: The number of bytes of file content, and will be same to
6557 * @num_bytes except for the compress path.
Josef Bacikfb25e912011-07-26 17:00:46 -04006558 * @num_bytes: The number of bytes in question
Miao Xiee570fd22014-06-19 10:42:50 +08006559 * @delalloc: The blocks are allocated for the delalloc write
Josef Bacikfb25e912011-07-26 17:00:46 -04006560 *
Xiaoguang Wang745699e2016-09-23 12:38:50 +08006561 * This is called by the allocator when it reserves space. If this is a
6562 * reservation and the block group has become read only we cannot make the
6563 * reservation and return -EAGAIN, otherwise this function always succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04006564 */
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006565static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
Wang Xiaoguang18513092016-07-25 15:51:40 +08006566 u64 ram_bytes, u64 num_bytes, int delalloc)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006567{
Josef Bacikfb25e912011-07-26 17:00:46 -04006568 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006569 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006570
Josef Bacikfb25e912011-07-26 17:00:46 -04006571 spin_lock(&space_info->lock);
6572 spin_lock(&cache->lock);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006573 if (cache->ro) {
6574 ret = -EAGAIN;
Josef Bacikfb25e912011-07-26 17:00:46 -04006575 } else {
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006576 cache->reserved += num_bytes;
6577 space_info->bytes_reserved += num_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006578
Wang Xiaoguang18513092016-07-25 15:51:40 +08006579 trace_btrfs_space_reservation(cache->fs_info,
6580 "space_info", space_info->flags,
6581 ram_bytes, 0);
6582 space_info->bytes_may_use -= ram_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006583 if (delalloc)
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006584 cache->delalloc_bytes += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006585 }
Josef Bacikfb25e912011-07-26 17:00:46 -04006586 spin_unlock(&cache->lock);
6587 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006588 return ret;
6589}
6590
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006591/**
6592 * btrfs_free_reserved_bytes - update the block_group and space info counters
6593 * @cache: The cache we are manipulating
6594 * @num_bytes: The number of bytes in question
6595 * @delalloc: The blocks are allocated for the delalloc write
6596 *
6597 * This is called by somebody who is freeing space that was never actually used
6598 * on disk. For example if you reserve some space for a new leaf in transaction
6599 * A and before transaction A commits you free that leaf, you call this with
6600 * reserve set to 0 in order to clear the reservation.
6601 */
6602
6603static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
6604 u64 num_bytes, int delalloc)
6605{
6606 struct btrfs_space_info *space_info = cache->space_info;
6607 int ret = 0;
6608
6609 spin_lock(&space_info->lock);
6610 spin_lock(&cache->lock);
6611 if (cache->ro)
6612 space_info->bytes_readonly += num_bytes;
6613 cache->reserved -= num_bytes;
6614 space_info->bytes_reserved -= num_bytes;
6615
6616 if (delalloc)
6617 cache->delalloc_bytes -= num_bytes;
6618 spin_unlock(&cache->lock);
6619 spin_unlock(&space_info->lock);
6620 return ret;
6621}
David Sterba8b74c032017-02-10 19:20:56 +01006622void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04006623{
Yan Zheng11833d62009-09-11 16:11:19 -04006624 struct btrfs_caching_control *next;
6625 struct btrfs_caching_control *caching_ctl;
6626 struct btrfs_block_group_cache *cache;
6627
Josef Bacik9e351cc2014-03-13 15:42:13 -04006628 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04006629
6630 list_for_each_entry_safe(caching_ctl, next,
6631 &fs_info->caching_block_groups, list) {
6632 cache = caching_ctl->block_group;
6633 if (block_group_cache_done(cache)) {
6634 cache->last_byte_to_unpin = (u64)-1;
6635 list_del_init(&caching_ctl->list);
6636 put_caching_control(caching_ctl);
6637 } else {
6638 cache->last_byte_to_unpin = caching_ctl->progress;
6639 }
6640 }
6641
6642 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6643 fs_info->pinned_extents = &fs_info->freed_extents[1];
6644 else
6645 fs_info->pinned_extents = &fs_info->freed_extents[0];
6646
Josef Bacik9e351cc2014-03-13 15:42:13 -04006647 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006648
6649 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04006650}
6651
Josef Bacikc759c4e2015-10-02 15:25:10 -04006652/*
6653 * Returns the free cluster for the given space info and sets empty_cluster to
6654 * what it should be based on the mount options.
6655 */
6656static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006657fetch_cluster_info(struct btrfs_fs_info *fs_info,
6658 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04006659{
6660 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006661
6662 *empty_cluster = 0;
6663 if (btrfs_mixed_space_info(space_info))
6664 return ret;
6665
Josef Bacikc759c4e2015-10-02 15:25:10 -04006666 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006667 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006668 if (btrfs_test_opt(fs_info, SSD))
6669 *empty_cluster = SZ_2M;
6670 else
Byongho Leeee221842015-12-15 01:42:10 +09006671 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006672 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
6673 btrfs_test_opt(fs_info, SSD_SPREAD)) {
6674 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006675 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006676 }
6677
6678 return ret;
6679}
6680
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006681static int unpin_extent_range(struct btrfs_fs_info *fs_info,
6682 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00006683 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04006684{
Yan Zheng11833d62009-09-11 16:11:19 -04006685 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04006686 struct btrfs_space_info *space_info;
6687 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006688 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04006689 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006690 u64 total_unpinned = 0;
6691 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04006692 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04006693
6694 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04006695 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04006696 if (!cache ||
6697 start >= cache->key.objectid + cache->key.offset) {
6698 if (cache)
6699 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04006700 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006701 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006702 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04006703
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006704 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04006705 cache->space_info,
6706 &empty_cluster);
6707 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04006708 }
6709
6710 len = cache->key.objectid + cache->key.offset - start;
6711 len = min(len, end + 1 - start);
6712
6713 if (start < cache->last_byte_to_unpin) {
6714 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00006715 if (return_free_space)
6716 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04006717 }
Josef Bacik25179202008-10-29 14:49:05 -04006718
Yan, Zhengf0486c62010-05-16 10:46:25 -04006719 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006720 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006721 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006722
Josef Bacikc759c4e2015-10-02 15:25:10 -04006723 /*
6724 * If this space cluster has been marked as fragmented and we've
6725 * unpinned enough in this block group to potentially allow a
6726 * cluster to be created inside of it go ahead and clear the
6727 * fragmented check.
6728 */
6729 if (cluster && cluster->fragmented &&
6730 total_unpinned > empty_cluster) {
6731 spin_lock(&cluster->lock);
6732 cluster->fragmented = 0;
6733 spin_unlock(&cluster->lock);
6734 }
6735
Josef Bacik7b398f82012-10-22 15:52:28 -04006736 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006737 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006738 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006739 space_info->bytes_pinned -= len;
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006740
6741 trace_btrfs_space_reservation(fs_info, "pinned",
6742 space_info->flags, len, 0);
Josef Bacik4f4db212015-09-29 11:40:47 -04006743 space_info->max_extent_size = 0;
Liu Bod288db52014-07-02 16:58:01 +08006744 percpu_counter_add(&space_info->total_bytes_pinned, -len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006745 if (cache->ro) {
6746 space_info->bytes_readonly += len;
6747 readonly = true;
6748 }
Josef Bacik25179202008-10-29 14:49:05 -04006749 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006750 if (!readonly && return_free_space &&
6751 global_rsv->space_info == space_info) {
6752 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03006753
Josef Bacik7b398f82012-10-22 15:52:28 -04006754 spin_lock(&global_rsv->lock);
6755 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04006756 to_add = min(len, global_rsv->size -
6757 global_rsv->reserved);
6758 global_rsv->reserved += to_add;
6759 space_info->bytes_may_use += to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006760 if (global_rsv->reserved >= global_rsv->size)
6761 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04006762 trace_btrfs_space_reservation(fs_info,
6763 "space_info",
6764 space_info->flags,
6765 to_add, 1);
6766 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006767 }
6768 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006769 /* Add to any tickets we may have */
6770 if (len)
6771 space_info_add_new_bytes(fs_info, space_info,
6772 len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006773 }
6774 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006775 }
6776
6777 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006778 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04006779 return 0;
6780}
6781
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02006782int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
Chris Masona28ec192007-03-06 20:08:01 -05006783{
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02006784 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006785 struct btrfs_block_group_cache *block_group, *tmp;
6786 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04006787 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04006788 u64 start;
6789 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05006790 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05006791
Yan Zheng11833d62009-09-11 16:11:19 -04006792 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6793 unpin = &fs_info->freed_extents[1];
6794 else
6795 unpin = &fs_info->freed_extents[0];
6796
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006797 while (!trans->aborted) {
Filipe Mananad4b450c2015-01-29 19:18:25 +00006798 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04006799 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04006800 EXTENT_DIRTY, NULL);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006801 if (ret) {
6802 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05006803 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00006804 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006805
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006806 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006807 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00006808 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006809
David Sterbaaf6f8f62016-04-26 23:54:39 +02006810 clear_extent_dirty(unpin, start, end);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006811 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006812 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masonb9473432009-03-13 11:00:37 -04006813 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05006814 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006815
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006816 /*
6817 * Transaction is finished. We don't need the lock anymore. We
6818 * do need to clean up the block groups in case of a transaction
6819 * abort.
6820 */
6821 deleted_bgs = &trans->transaction->deleted_bgs;
6822 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
6823 u64 trimmed = 0;
6824
6825 ret = -EROFS;
6826 if (!trans->aborted)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006827 ret = btrfs_discard_extent(fs_info,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006828 block_group->key.objectid,
6829 block_group->key.offset,
6830 &trimmed);
6831
6832 list_del_init(&block_group->bg_list);
6833 btrfs_put_block_group_trimming(block_group);
6834 btrfs_put_block_group(block_group);
6835
6836 if (ret) {
6837 const char *errstr = btrfs_decode_error(ret);
6838 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02006839 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006840 ret, errstr);
6841 }
6842 }
6843
Chris Masone20d96d2007-03-22 12:13:20 -04006844 return 0;
6845}
6846
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006847static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006848 struct btrfs_fs_info *info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006849 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006850 u64 root_objectid, u64 owner_objectid,
6851 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006852 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05006853{
Chris Masone2fa7222007-03-12 16:22:34 -04006854 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006855 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04006856 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04006857 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006858 struct btrfs_extent_item *ei;
6859 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05006860 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006861 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05006862 int extent_slot = 0;
6863 int found_extent = 0;
6864 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006865 u32 item_size;
6866 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006867 u64 bytenr = node->bytenr;
6868 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07006869 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006870 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05006871
Chris Mason5caf2a02007-04-02 11:20:42 -04006872 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04006873 if (!path)
6874 return -ENOMEM;
6875
David Sterbae4058b52015-11-27 16:31:35 +01006876 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04006877 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006878
6879 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
6880 BUG_ON(!is_data && refs_to_drop != 1);
6881
Josef Bacik3173a182013-03-07 14:22:04 -05006882 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02006883 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05006884
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05006885 ret = lookup_extent_backref(trans, info, path, &iref,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006886 bytenr, num_bytes, parent,
6887 root_objectid, owner_objectid,
6888 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05006889 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05006890 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006891 while (extent_slot >= 0) {
6892 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05006893 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006894 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05006895 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006896 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
6897 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05006898 found_extent = 1;
6899 break;
6900 }
Josef Bacik3173a182013-03-07 14:22:04 -05006901 if (key.type == BTRFS_METADATA_ITEM_KEY &&
6902 key.offset == owner_objectid) {
6903 found_extent = 1;
6904 break;
6905 }
Chris Mason952fcca2008-02-18 16:33:44 -05006906 if (path->slots[0] - extent_slot > 5)
6907 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006908 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05006909 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006910#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6911 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
6912 if (found_extent && item_size < sizeof(*ei))
6913 found_extent = 0;
6914#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04006915 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006916 BUG_ON(iref);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05006917 ret = remove_extent_backref(trans, info, path, NULL,
6918 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07006919 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06006920 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006921 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006922 goto out;
6923 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006924 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04006925 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006926
6927 key.objectid = bytenr;
6928 key.type = BTRFS_EXTENT_ITEM_KEY;
6929 key.offset = num_bytes;
6930
Josef Bacik3173a182013-03-07 14:22:04 -05006931 if (!is_data && skinny_metadata) {
6932 key.type = BTRFS_METADATA_ITEM_KEY;
6933 key.offset = owner_objectid;
6934 }
6935
Zheng Yan31840ae2008-09-23 13:14:14 -04006936 ret = btrfs_search_slot(trans, extent_root,
6937 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05006938 if (ret > 0 && skinny_metadata && path->slots[0]) {
6939 /*
6940 * Couldn't find our skinny metadata item,
6941 * see if we have ye olde extent item.
6942 */
6943 path->slots[0]--;
6944 btrfs_item_key_to_cpu(path->nodes[0], &key,
6945 path->slots[0]);
6946 if (key.objectid == bytenr &&
6947 key.type == BTRFS_EXTENT_ITEM_KEY &&
6948 key.offset == num_bytes)
6949 ret = 0;
6950 }
6951
6952 if (ret > 0 && skinny_metadata) {
6953 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01006954 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05006955 key.type = BTRFS_EXTENT_ITEM_KEY;
6956 key.offset = num_bytes;
6957 btrfs_release_path(path);
6958 ret = btrfs_search_slot(trans, extent_root,
6959 &key, path, -1, 1);
6960 }
6961
Josef Bacikf3465ca2008-11-12 14:19:50 -05006962 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006963 btrfs_err(info,
6964 "umm, got %d back from search, was looking for %llu",
6965 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00006966 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02006967 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006968 }
David Sterba005d6422012-09-18 07:52:32 -06006969 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006970 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006971 goto out;
6972 }
Zheng Yan31840ae2008-09-23 13:14:14 -04006973 extent_slot = path->slots[0];
6974 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306975 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02006976 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006977 btrfs_err(info,
6978 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006979 bytenr, parent, root_objectid, owner_objectid,
6980 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04006981 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04006982 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006983 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04006984 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006985 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05006986 }
Chris Mason5f39d392007-10-15 16:14:19 -04006987
6988 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006989 item_size = btrfs_item_size_nr(leaf, extent_slot);
6990#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6991 if (item_size < sizeof(*ei)) {
6992 BUG_ON(found_extent || extent_slot != path->slots[0]);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05006993 ret = convert_extent_item_v0(trans, info, path, owner_objectid,
6994 0);
David Sterba005d6422012-09-18 07:52:32 -06006995 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006996 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006997 goto out;
6998 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006999
David Sterbab3b4aa72011-04-21 01:20:15 +02007000 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007001 path->leave_spinning = 1;
7002
7003 key.objectid = bytenr;
7004 key.type = BTRFS_EXTENT_ITEM_KEY;
7005 key.offset = num_bytes;
7006
7007 ret = btrfs_search_slot(trans, extent_root, &key, path,
7008 -1, 1);
7009 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007010 btrfs_err(info,
7011 "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007012 ret, bytenr);
David Sterbaa4f78752017-06-29 18:37:49 +02007013 btrfs_print_leaf(path->nodes[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007014 }
David Sterba005d6422012-09-18 07:52:32 -06007015 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007016 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007017 goto out;
7018 }
7019
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007020 extent_slot = path->slots[0];
7021 leaf = path->nodes[0];
7022 item_size = btrfs_item_size_nr(leaf, extent_slot);
7023 }
7024#endif
7025 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05007026 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04007027 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05007028 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
7029 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007030 struct btrfs_tree_block_info *bi;
7031 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
7032 bi = (struct btrfs_tree_block_info *)(ei + 1);
7033 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05007034 }
7035
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007036 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04007037 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007038 btrfs_err(info,
7039 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
7040 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04007041 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04007042 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04007043 goto out;
7044 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007045 refs -= refs_to_drop;
7046
7047 if (refs > 0) {
7048 if (extent_op)
7049 __run_delayed_extent_op(extent_op, leaf, ei);
7050 /*
7051 * In the case of inline back ref, reference count will
7052 * be updated by remove_extent_backref
7053 */
7054 if (iref) {
7055 BUG_ON(!found_extent);
7056 } else {
7057 btrfs_set_extent_refs(leaf, ei, refs);
7058 btrfs_mark_buffer_dirty(leaf);
7059 }
7060 if (found_extent) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05007061 ret = remove_extent_backref(trans, info, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007062 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07007063 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06007064 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007065 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007066 goto out;
7067 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007068 }
7069 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007070 if (found_extent) {
7071 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08007072 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007073 if (iref) {
7074 BUG_ON(path->slots[0] != extent_slot);
7075 } else {
7076 BUG_ON(path->slots[0] != extent_slot + 1);
7077 path->slots[0] = extent_slot;
7078 num_to_del = 2;
7079 }
Chris Mason78fae272007-03-25 11:35:08 -04007080 }
Chris Masonb9473432009-03-13 11:00:37 -04007081
Josef Bacikfcebe452014-05-13 17:30:47 -07007082 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05007083 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
7084 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06007085 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007086 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007087 goto out;
7088 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007089 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01007090
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007091 if (is_data) {
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007092 ret = btrfs_del_csums(trans, info, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06007093 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007094 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007095 goto out;
7096 }
Chris Mason459931e2008-12-10 09:10:46 -05007097 }
7098
Nikolay Borisove7355e52018-05-10 15:44:55 +03007099 ret = add_to_free_space_tree(trans, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007100 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007101 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007102 goto out;
7103 }
7104
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007105 ret = update_block_group(trans, info, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06007106 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007107 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007108 goto out;
7109 }
Chris Masona28ec192007-03-06 20:08:01 -05007110 }
Josef Bacikfcebe452014-05-13 17:30:47 -07007111 btrfs_release_path(path);
7112
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007113out:
Chris Mason5caf2a02007-04-02 11:20:42 -04007114 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05007115 return ret;
7116}
7117
7118/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007119 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04007120 * delayed ref for that extent as well. This searches the delayed ref tree for
7121 * a given extent, and if there are no other delayed refs to be processed, it
7122 * removes it from the tree.
7123 */
7124static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007125 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04007126{
7127 struct btrfs_delayed_ref_head *head;
7128 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007129 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04007130
7131 delayed_refs = &trans->transaction->delayed_refs;
7132 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08007133 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04007134 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08007135 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04007136
Josef Bacikd7df2c72014-01-23 09:21:38 -05007137 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04007138 if (!RB_EMPTY_ROOT(&head->ref_tree))
Chris Mason1887be62009-03-13 10:11:24 -04007139 goto out;
7140
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007141 if (head->extent_op) {
7142 if (!head->must_insert_reserved)
7143 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00007144 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007145 head->extent_op = NULL;
7146 }
7147
Chris Mason1887be62009-03-13 10:11:24 -04007148 /*
7149 * waiting for the lock here would deadlock. If someone else has it
7150 * locked they are already in the process of dropping it anyway
7151 */
7152 if (!mutex_trylock(&head->mutex))
7153 goto out;
7154
7155 /*
7156 * at this point we have a head with no other entries. Go
7157 * ahead and process it.
7158 */
Liu Boc46effa2013-10-14 12:59:45 +08007159 rb_erase(&head->href_node, &delayed_refs->href_root);
Josef Bacikd2788502017-09-29 15:43:57 -04007160 RB_CLEAR_NODE(&head->href_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05007161 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04007162
7163 /*
7164 * we don't take a ref on the node because we're removing it from the
7165 * tree, so we just steal the ref the tree was holding.
7166 */
Chris Masonc3e69d52009-03-13 10:17:05 -04007167 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007168 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04007169 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007170 head->processing = 0;
7171 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04007172 spin_unlock(&delayed_refs->lock);
7173
Yan, Zhengf0486c62010-05-16 10:46:25 -04007174 BUG_ON(head->extent_op);
7175 if (head->must_insert_reserved)
7176 ret = 1;
7177
7178 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04007179 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007180 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04007181out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05007182 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08007183
7184out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04007185 spin_unlock(&delayed_refs->lock);
7186 return 0;
7187}
7188
Yan, Zhengf0486c62010-05-16 10:46:25 -04007189void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7190 struct btrfs_root *root,
7191 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02007192 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007193{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007194 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb150a4f2013-06-19 15:00:04 -04007195 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007196 int ret;
7197
7198 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07007199 int old_ref_mod, new_ref_mod;
7200
Josef Bacikfd708b82017-09-29 15:43:50 -04007201 btrfs_ref_tree_mod(root, buf->start, buf->len, parent,
7202 root->root_key.objectid,
7203 btrfs_header_level(buf), 0,
7204 BTRFS_DROP_DELAYED_REF);
Omar Sandoval7be07912017-06-06 16:45:30 -07007205 ret = btrfs_add_delayed_tree_ref(fs_info, trans, buf->start,
7206 buf->len, parent,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007207 root->root_key.objectid,
7208 btrfs_header_level(buf),
Omar Sandoval7be07912017-06-06 16:45:30 -07007209 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007210 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007211 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07007212 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007213 }
7214
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007215 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
Filipe Manana62198722015-01-06 20:18:45 +00007216 struct btrfs_block_group_cache *cache;
7217
Yan, Zhengf0486c62010-05-16 10:46:25 -04007218 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007219 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007220 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04007221 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007222 }
7223
Omar Sandoval4da8b762017-06-06 16:45:28 -07007224 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007225 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00007226
Yan, Zhengf0486c62010-05-16 10:46:25 -04007227 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007228 pin_down_extent(fs_info, cache, buf->start,
7229 buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00007230 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04007231 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007232 }
7233
7234 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
7235
7236 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08007237 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00007238 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007239 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007240 }
7241out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04007242 if (pin)
Nikolay Borisov29d2b842018-03-30 12:58:47 +03007243 add_pinned_bytes(fs_info, buf->len, true,
Josef Bacikb150a4f2013-06-19 15:00:04 -04007244 root->root_key.objectid);
7245
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007246 if (last_ref) {
7247 /*
7248 * Deleting the buffer, clear the corrupt flag since it doesn't
7249 * matter anymore.
7250 */
7251 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
7252 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007253}
7254
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007255/* Can return -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007256int btrfs_free_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007257 struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007258 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01007259 u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04007260{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007261 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07007262 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04007263 int ret;
7264
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04007265 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007266 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02007267
Josef Bacikfd708b82017-09-29 15:43:50 -04007268 if (root_objectid != BTRFS_TREE_LOG_OBJECTID)
7269 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent,
7270 root_objectid, owner, offset,
7271 BTRFS_DROP_DELAYED_REF);
7272
Chris Mason56bec292009-03-13 10:10:06 -04007273 /*
7274 * tree log blocks never actually go into the extent allocation
7275 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04007276 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007277 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
7278 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04007279 /* unlocks the pinned mutex */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007280 btrfs_pin_extent(fs_info, bytenr, num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07007281 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04007282 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007283 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007284 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007285 num_bytes, parent,
7286 root_objectid, (int)owner,
7287 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007288 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007289 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007290 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007291 num_bytes, parent,
7292 root_objectid, owner, offset,
7293 0, BTRFS_DROP_DELAYED_REF,
Omar Sandovald7eae342017-06-06 16:45:31 -07007294 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04007295 }
Omar Sandovald7eae342017-06-06 16:45:31 -07007296
Nikolay Borisov29d2b842018-03-30 12:58:47 +03007297 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0) {
7298 bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
7299
7300 add_pinned_bytes(fs_info, num_bytes, metadata, root_objectid);
7301 }
Omar Sandovald7eae342017-06-06 16:45:31 -07007302
Chris Mason925baed2008-06-25 16:01:30 -04007303 return ret;
7304}
7305
Chris Masonfec577f2007-02-26 10:40:21 -05007306/*
Josef Bacik817d52f2009-07-13 21:29:25 -04007307 * when we wait for progress in the block group caching, its because
7308 * our allocation attempt failed at least once. So, we must sleep
7309 * and let some progress happen before we try again.
7310 *
7311 * This function will sleep at least once waiting for new free space to
7312 * show up, and then it will check the block group free space numbers
7313 * for our min num_bytes. Another option is to have it go ahead
7314 * and look in the rbtree for a free extent of a given size, but this
7315 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04007316 *
7317 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
7318 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04007319 */
Josef Bacik36cce922013-08-05 11:15:21 -04007320static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04007321wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
7322 u64 num_bytes)
7323{
Yan Zheng11833d62009-09-11 16:11:19 -04007324 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04007325
Yan Zheng11833d62009-09-11 16:11:19 -04007326 caching_ctl = get_caching_control(cache);
7327 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007328 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04007329
Yan Zheng11833d62009-09-11 16:11:19 -04007330 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08007331 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04007332
7333 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04007334}
7335
7336static noinline int
7337wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
7338{
7339 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04007340 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007341
7342 caching_ctl = get_caching_control(cache);
7343 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007344 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007345
7346 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04007347 if (cache->cached == BTRFS_CACHE_ERROR)
7348 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04007349 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04007350 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04007351}
7352
7353enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05007354 LOOP_CACHING_NOWAIT = 0,
7355 LOOP_CACHING_WAIT = 1,
7356 LOOP_ALLOC_CHUNK = 2,
7357 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04007358};
7359
Miao Xiee570fd22014-06-19 10:42:50 +08007360static inline void
7361btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
7362 int delalloc)
7363{
7364 if (delalloc)
7365 down_read(&cache->data_rwsem);
7366}
7367
7368static inline void
7369btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
7370 int delalloc)
7371{
7372 btrfs_get_block_group(cache);
7373 if (delalloc)
7374 down_read(&cache->data_rwsem);
7375}
7376
7377static struct btrfs_block_group_cache *
7378btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
7379 struct btrfs_free_cluster *cluster,
7380 int delalloc)
7381{
Sudip Mukherjee89771cc2016-02-16 13:32:47 +05307382 struct btrfs_block_group_cache *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007383
Miao Xiee570fd22014-06-19 10:42:50 +08007384 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007385 while (1) {
7386 used_bg = cluster->block_group;
7387 if (!used_bg)
7388 return NULL;
7389
7390 if (used_bg == block_group)
7391 return used_bg;
7392
7393 btrfs_get_block_group(used_bg);
7394
7395 if (!delalloc)
7396 return used_bg;
7397
7398 if (down_read_trylock(&used_bg->data_rwsem))
7399 return used_bg;
7400
7401 spin_unlock(&cluster->refill_lock);
7402
Liu Boe321f8a2016-11-30 16:11:04 -08007403 /* We should only have one-level nested. */
7404 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007405
7406 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007407 if (used_bg == cluster->block_group)
7408 return used_bg;
7409
7410 up_read(&used_bg->data_rwsem);
7411 btrfs_put_block_group(used_bg);
7412 }
Miao Xiee570fd22014-06-19 10:42:50 +08007413}
7414
7415static inline void
7416btrfs_release_block_group(struct btrfs_block_group_cache *cache,
7417 int delalloc)
7418{
7419 if (delalloc)
7420 up_read(&cache->data_rwsem);
7421 btrfs_put_block_group(cache);
7422}
7423
Josef Bacik817d52f2009-07-13 21:29:25 -04007424/*
Chris Masonfec577f2007-02-26 10:40:21 -05007425 * walks the btree of allocated extents and find a hole of a given size.
7426 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08007427 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04007428 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08007429 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05007430 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08007431 *
7432 * If there is no suitable free space, we will record the max size of
7433 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05007434 */
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05007435static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08007436 u64 ram_bytes, u64 num_bytes, u64 empty_size,
7437 u64 hint_byte, struct btrfs_key *ins,
7438 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007439{
Josef Bacik80eb2342008-10-29 14:49:05 -04007440 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007441 struct btrfs_root *root = fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007442 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04007443 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05007444 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08007445 u64 max_extent_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007446 u64 empty_cluster = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04007447 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007448 int loop = 0;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007449 int index = btrfs_bg_flags_to_raid_index(flags);
Josef Bacik0a243252009-09-11 16:11:20 -04007450 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007451 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04007452 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08007453 bool have_caching_bg = false;
chandan13a0db52015-11-02 13:59:46 +05307454 bool orig_have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007455 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05007456
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007457 WARN_ON(num_bytes < fs_info->sectorsize);
David Sterba962a2982014-06-04 18:41:45 +02007458 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04007459 ins->objectid = 0;
7460 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04007461
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007462 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05007463
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007464 space_info = __find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007465 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007466 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007467 return -ENOSPC;
7468 }
Josef Bacik2552d172009-04-03 10:14:19 -04007469
Josef Bacik67377732010-09-16 16:19:09 -04007470 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04007471 * If our free space is heavily fragmented we may not be able to make
7472 * big contiguous allocations, so instead of doing the expensive search
7473 * for free space, simply return ENOSPC with our max_extent_size so we
7474 * can go ahead and search for a more manageable chunk.
7475 *
7476 * If our max_extent_size is large enough for our allocation simply
7477 * disable clustering since we will likely not be able to find enough
7478 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04007479 */
Josef Bacik4f4db212015-09-29 11:40:47 -04007480 if (unlikely(space_info->max_extent_size)) {
7481 spin_lock(&space_info->lock);
7482 if (space_info->max_extent_size &&
7483 num_bytes > space_info->max_extent_size) {
7484 ins->offset = space_info->max_extent_size;
7485 spin_unlock(&space_info->lock);
7486 return -ENOSPC;
7487 } else if (space_info->max_extent_size) {
7488 use_cluster = false;
7489 }
7490 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007491 }
7492
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007493 last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04007494 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007495 spin_lock(&last_ptr->lock);
7496 if (last_ptr->block_group)
7497 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007498 if (last_ptr->fragmented) {
7499 /*
7500 * We still set window_start so we can keep track of the
7501 * last place we found an allocation to try and save
7502 * some time.
7503 */
7504 hint_byte = last_ptr->window_start;
7505 use_cluster = false;
7506 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007507 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007508 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007509
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007510 search_start = max(search_start, first_logical_byte(fs_info, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04007511 search_start = max(search_start, hint_byte);
Josef Bacik2552d172009-04-03 10:14:19 -04007512 if (search_start == hint_byte) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007513 block_group = btrfs_lookup_block_group(fs_info, search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04007514 /*
7515 * we don't want to use the block group if it doesn't match our
7516 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05007517 *
7518 * However if we are re-searching with an ideal block group
7519 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04007520 */
David Sterbab6919a52013-04-29 13:39:40 +00007521 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05007522 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04007523 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007524 if (list_empty(&block_group->list) ||
7525 block_group->ro) {
7526 /*
7527 * someone is removing this block group,
7528 * we can't jump into the have_block_group
7529 * target because our list pointers are not
7530 * valid
7531 */
7532 btrfs_put_block_group(block_group);
7533 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05007534 } else {
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007535 index = btrfs_bg_flags_to_raid_index(
7536 block_group->flags);
Miao Xiee570fd22014-06-19 10:42:50 +08007537 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04007538 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05007539 }
Josef Bacik2552d172009-04-03 10:14:19 -04007540 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007541 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04007542 }
Chris Mason42e70e72008-11-07 18:17:11 -05007543 }
Josef Bacik2552d172009-04-03 10:14:19 -04007544search:
Miao Xie60d2adb2011-09-09 17:34:35 +08007545 have_caching_bg = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007546 if (index == 0 || index == btrfs_bg_flags_to_raid_index(flags))
Josef Bacika5e681d2015-10-01 14:54:10 -04007547 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04007548 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007549 list_for_each_entry(block_group, &space_info->block_groups[index],
7550 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04007551 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04007552 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05007553
Jeff Mahoney14443932017-07-19 23:25:51 -04007554 /* If the block group is read-only, we can skip it entirely. */
7555 if (unlikely(block_group->ro))
7556 continue;
7557
Miao Xiee570fd22014-06-19 10:42:50 +08007558 btrfs_grab_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007559 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05007560
Chris Mason83a50de2010-12-13 15:06:46 -05007561 /*
7562 * this can happen if we end up cycling through all the
7563 * raid types, but we want to make sure we only allocate
7564 * for the proper type.
7565 */
David Sterbab6919a52013-04-29 13:39:40 +00007566 if (!block_group_bits(block_group, flags)) {
Bart Van Asschebece2e82018-06-20 10:03:31 -07007567 u64 extra = BTRFS_BLOCK_GROUP_DUP |
Chris Mason83a50de2010-12-13 15:06:46 -05007568 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007569 BTRFS_BLOCK_GROUP_RAID5 |
7570 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05007571 BTRFS_BLOCK_GROUP_RAID10;
7572
7573 /*
7574 * if they asked for extra copies and this block group
7575 * doesn't provide them, bail. This does allow us to
7576 * fill raid0 from raid1.
7577 */
David Sterbab6919a52013-04-29 13:39:40 +00007578 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05007579 goto loop;
7580 }
7581
Josef Bacik2552d172009-04-03 10:14:19 -04007582have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05007583 cached = block_group_cache_done(block_group);
7584 if (unlikely(!cached)) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007585 have_caching_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00007586 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04007587 BUG_ON(ret < 0);
7588 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05007589 }
7590
Josef Bacik36cce922013-08-05 11:15:21 -04007591 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
7592 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007593
Josef Bacik0a243252009-09-11 16:11:20 -04007594 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007595 * Ok we want to try and use the cluster allocator, so
7596 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04007597 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04007598 if (last_ptr && use_cluster) {
Miao Xie215a63d2014-01-15 20:00:56 +08007599 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05007600 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007601 /*
7602 * the refill lock keeps out other
7603 * people trying to start a new cluster
7604 */
Miao Xiee570fd22014-06-19 10:42:50 +08007605 used_block_group = btrfs_lock_cluster(block_group,
7606 last_ptr,
7607 delalloc);
7608 if (!used_block_group)
Chris Mason44fb5512009-06-04 15:34:51 -04007609 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04007610
Miao Xiee570fd22014-06-19 10:42:50 +08007611 if (used_block_group != block_group &&
7612 (used_block_group->ro ||
7613 !block_group_bits(used_block_group, flags)))
7614 goto release_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007615
7616 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007617 last_ptr,
7618 num_bytes,
7619 used_block_group->key.objectid,
7620 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007621 if (offset) {
7622 /* we have a block, we're done */
7623 spin_unlock(&last_ptr->refill_lock);
Qu Wenruo3dca5c92018-04-26 14:24:25 +08007624 trace_btrfs_reserve_extent_cluster(
Miao Xie89d43462014-01-15 20:00:57 +08007625 used_block_group,
7626 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08007627 if (used_block_group != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08007628 btrfs_release_block_group(block_group,
7629 delalloc);
Miao Xie215a63d2014-01-15 20:00:56 +08007630 block_group = used_block_group;
7631 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007632 goto checks;
7633 }
7634
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007635 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007636release_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007637 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7638 * set up a new clusters, so lets just skip it
7639 * and let the allocator find whatever block
7640 * it can find. If we reach this point, we
7641 * will have tried the cluster allocator
7642 * plenty of times and not have found
7643 * anything, so we are likely way too
7644 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007645 * anything.
7646 *
7647 * However, if the cluster is taken from the
7648 * current block group, release the cluster
7649 * first, so that we stand a better chance of
7650 * succeeding in the unclustered
7651 * allocation. */
7652 if (loop >= LOOP_NO_EMPTY_SIZE &&
Miao Xiee570fd22014-06-19 10:42:50 +08007653 used_block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007654 spin_unlock(&last_ptr->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007655 btrfs_release_block_group(used_block_group,
7656 delalloc);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007657 goto unclustered_alloc;
7658 }
7659
Chris Masonfa9c0d792009-04-03 09:47:43 -04007660 /*
7661 * this cluster didn't work out, free it and
7662 * start over
7663 */
7664 btrfs_return_cluster_to_free_space(NULL, last_ptr);
7665
Miao Xiee570fd22014-06-19 10:42:50 +08007666 if (used_block_group != block_group)
7667 btrfs_release_block_group(used_block_group,
7668 delalloc);
7669refill_cluster:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007670 if (loop >= LOOP_NO_EMPTY_SIZE) {
7671 spin_unlock(&last_ptr->refill_lock);
7672 goto unclustered_alloc;
7673 }
7674
Chris Mason8de972b2013-01-04 15:39:43 -05007675 aligned_cluster = max_t(unsigned long,
7676 empty_cluster + empty_size,
7677 block_group->full_stripe_len);
7678
Chris Masonfa9c0d792009-04-03 09:47:43 -04007679 /* allocate a cluster in this block group */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007680 ret = btrfs_find_space_cluster(fs_info, block_group,
Josef Bacik00361582013-08-14 14:02:47 -04007681 last_ptr, search_start,
7682 num_bytes,
7683 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007684 if (ret == 0) {
7685 /*
7686 * now pull our allocation out of this
7687 * cluster
7688 */
7689 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007690 last_ptr,
7691 num_bytes,
7692 search_start,
7693 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007694 if (offset) {
7695 /* we found one, proceed */
7696 spin_unlock(&last_ptr->refill_lock);
Qu Wenruo3dca5c92018-04-26 14:24:25 +08007697 trace_btrfs_reserve_extent_cluster(
Josef Bacik3f7de032011-11-10 08:29:20 -05007698 block_group, search_start,
7699 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007700 goto checks;
7701 }
Josef Bacik0a243252009-09-11 16:11:20 -04007702 } else if (!cached && loop > LOOP_CACHING_NOWAIT
7703 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007704 spin_unlock(&last_ptr->refill_lock);
7705
Josef Bacik0a243252009-09-11 16:11:20 -04007706 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007707 wait_block_group_cache_progress(block_group,
7708 num_bytes + empty_cluster + empty_size);
7709 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007710 }
Josef Bacik817d52f2009-07-13 21:29:25 -04007711
Chris Masonfa9c0d792009-04-03 09:47:43 -04007712 /*
7713 * at this point we either didn't find a cluster
7714 * or we weren't able to allocate a block from our
7715 * cluster. Free the cluster we've been trying
7716 * to use, and go to the next block group
7717 */
Josef Bacik0a243252009-09-11 16:11:20 -04007718 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007719 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04007720 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007721 }
7722
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007723unclustered_alloc:
Josef Bacikc759c4e2015-10-02 15:25:10 -04007724 /*
7725 * We are doing an unclustered alloc, set the fragmented flag so
7726 * we don't bother trying to setup a cluster again until we get
7727 * more space.
7728 */
7729 if (unlikely(last_ptr)) {
7730 spin_lock(&last_ptr->lock);
7731 last_ptr->fragmented = 1;
7732 spin_unlock(&last_ptr->lock);
7733 }
Liu Bo0c9b36e2017-02-13 15:42:30 -08007734 if (cached) {
7735 struct btrfs_free_space_ctl *ctl =
7736 block_group->free_space_ctl;
7737
7738 spin_lock(&ctl->tree_lock);
7739 if (ctl->free_space <
7740 num_bytes + empty_cluster + empty_size) {
7741 if (ctl->free_space > max_extent_size)
7742 max_extent_size = ctl->free_space;
7743 spin_unlock(&ctl->tree_lock);
7744 goto loop;
7745 }
7746 spin_unlock(&ctl->tree_lock);
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007747 }
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007748
Josef Bacik6226cb02009-04-03 10:14:18 -04007749 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08007750 num_bytes, empty_size,
7751 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007752 /*
7753 * If we didn't find a chunk, and we haven't failed on this
7754 * block group before, and this block group is in the middle of
7755 * caching and we are ok with waiting, then go ahead and wait
7756 * for progress to be made, and set failed_alloc to true.
7757 *
7758 * If failed_alloc is true then we've already waited on this
7759 * block group once and should move on to the next block group.
7760 */
7761 if (!offset && !failed_alloc && !cached &&
7762 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007763 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007764 num_bytes + empty_size);
7765 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007766 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007767 } else if (!offset) {
7768 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04007769 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007770checks:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007771 search_start = ALIGN(offset, fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04007772
Josef Bacik2552d172009-04-03 10:14:19 -04007773 /* move on to the next group */
7774 if (search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08007775 block_group->key.objectid + block_group->key.offset) {
7776 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04007777 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04007778 }
Josef Bacik80eb2342008-10-29 14:49:05 -04007779
Josef Bacik6226cb02009-04-03 10:14:18 -04007780 if (offset < search_start)
Miao Xie215a63d2014-01-15 20:00:56 +08007781 btrfs_add_free_space(block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04007782 search_start - offset);
7783 BUG_ON(offset > search_start);
7784
Wang Xiaoguang18513092016-07-25 15:51:40 +08007785 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
7786 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007787 if (ret == -EAGAIN) {
Miao Xie215a63d2014-01-15 20:00:56 +08007788 btrfs_add_free_space(block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007789 goto loop;
7790 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007791 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04007792
Josef Bacik2552d172009-04-03 10:14:19 -04007793 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007794 ins->objectid = search_start;
7795 ins->offset = num_bytes;
7796
Qu Wenruo3dca5c92018-04-26 14:24:25 +08007797 trace_btrfs_reserve_extent(block_group, search_start, num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08007798 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007799 break;
7800loop:
Josef Bacik0a243252009-09-11 16:11:20 -04007801 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007802 failed_alloc = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007803 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
7804 index);
Miao Xiee570fd22014-06-19 10:42:50 +08007805 btrfs_release_block_group(block_group, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04007806 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04007807 }
7808 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05007809
chandan13a0db52015-11-02 13:59:46 +05307810 if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg
7811 && !orig_have_caching_bg)
7812 orig_have_caching_bg = true;
7813
Miao Xie60d2adb2011-09-09 17:34:35 +08007814 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
7815 goto search;
7816
Yan, Zhengb742bb82010-05-16 10:46:24 -04007817 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
7818 goto search;
7819
Josef Bacik285ff5a2012-01-13 15:27:45 -05007820 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05007821 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
7822 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04007823 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
7824 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
7825 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
7826 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04007827 */
Josef Bacik723bda22011-05-27 16:11:38 -04007828 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04007829 index = 0;
Josef Bacika5e681d2015-10-01 14:54:10 -04007830 if (loop == LOOP_CACHING_NOWAIT) {
7831 /*
7832 * We want to skip the LOOP_CACHING_WAIT step if we
Nicholas D Steeves01327612016-05-19 21:18:45 -04007833 * don't have any uncached bgs and we've already done a
Josef Bacika5e681d2015-10-01 14:54:10 -04007834 * full search through.
7835 */
chandan13a0db52015-11-02 13:59:46 +05307836 if (orig_have_caching_bg || !full_search)
Josef Bacika5e681d2015-10-01 14:54:10 -04007837 loop = LOOP_CACHING_WAIT;
7838 else
7839 loop = LOOP_ALLOC_CHUNK;
7840 } else {
7841 loop++;
7842 }
7843
Josef Bacik817d52f2009-07-13 21:29:25 -04007844 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04007845 struct btrfs_trans_handle *trans;
Wang Shilongf017f152014-03-13 13:19:47 +08007846 int exist = 0;
Josef Bacik00361582013-08-14 14:02:47 -04007847
Wang Shilongf017f152014-03-13 13:19:47 +08007848 trans = current->journal_info;
7849 if (trans)
7850 exist = 1;
7851 else
7852 trans = btrfs_join_transaction(root);
7853
Josef Bacik00361582013-08-14 14:02:47 -04007854 if (IS_ERR(trans)) {
7855 ret = PTR_ERR(trans);
7856 goto out;
7857 }
7858
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007859 ret = do_chunk_alloc(trans, fs_info, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04007860 CHUNK_ALLOC_FORCE);
Josef Bacika5e681d2015-10-01 14:54:10 -04007861
7862 /*
7863 * If we can't allocate a new chunk we've already looped
7864 * through at least once, move on to the NO_EMPTY_SIZE
7865 * case.
7866 */
7867 if (ret == -ENOSPC)
7868 loop = LOOP_NO_EMPTY_SIZE;
7869
Josef Bacikea658ba2012-09-11 16:57:25 -04007870 /*
7871 * Do not bail out on ENOSPC since we
7872 * can do more things.
7873 */
Josef Bacik00361582013-08-14 14:02:47 -04007874 if (ret < 0 && ret != -ENOSPC)
Jeff Mahoney66642832016-06-10 18:19:25 -04007875 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -04007876 else
7877 ret = 0;
Wang Shilongf017f152014-03-13 13:19:47 +08007878 if (!exist)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007879 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04007880 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04007881 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04007882 }
7883
7884 if (loop == LOOP_NO_EMPTY_SIZE) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007885 /*
7886 * Don't loop again if we already have no empty_size and
7887 * no empty_cluster.
7888 */
7889 if (empty_size == 0 &&
7890 empty_cluster == 0) {
7891 ret = -ENOSPC;
7892 goto out;
7893 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007894 empty_size = 0;
7895 empty_cluster = 0;
7896 }
Chris Mason42e70e72008-11-07 18:17:11 -05007897
Josef Bacik723bda22011-05-27 16:11:38 -04007898 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04007899 } else if (!ins->objectid) {
7900 ret = -ENOSPC;
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007901 } else if (ins->objectid) {
Josef Bacikc759c4e2015-10-02 15:25:10 -04007902 if (!use_cluster && last_ptr) {
7903 spin_lock(&last_ptr->lock);
7904 last_ptr->window_start = ins->objectid;
7905 spin_unlock(&last_ptr->lock);
7906 }
Josef Bacik2552d172009-04-03 10:14:19 -04007907 ret = 0;
7908 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007909out:
Josef Bacik4f4db212015-09-29 11:40:47 -04007910 if (ret == -ENOSPC) {
7911 spin_lock(&space_info->lock);
7912 space_info->max_extent_size = max_extent_size;
7913 spin_unlock(&space_info->lock);
Miao Xiea4820392013-09-09 13:19:42 +08007914 ins->offset = max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04007915 }
Chris Mason0f70abe2007-02-28 16:46:22 -05007916 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05007917}
Chris Masonec44a352008-04-28 15:29:52 -04007918
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007919static void dump_space_info(struct btrfs_fs_info *fs_info,
7920 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007921 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04007922{
7923 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04007924 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007925
Josef Bacik9ed74f22009-09-11 16:12:44 -04007926 spin_lock(&info->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007927 btrfs_info(fs_info, "space_info %llu has %llu free, is %sfull",
7928 info->flags,
Liu Bo41361352017-02-13 15:42:21 -08007929 info->total_bytes - btrfs_space_info_used(info, true),
7930 info->full ? "" : "not ");
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007931 btrfs_info(fs_info,
7932 "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu",
7933 info->total_bytes, info->bytes_used, info->bytes_pinned,
7934 info->bytes_reserved, info->bytes_may_use,
7935 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007936 spin_unlock(&info->lock);
7937
7938 if (!dump_block_groups)
7939 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007940
Josef Bacik80eb2342008-10-29 14:49:05 -04007941 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007942again:
7943 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04007944 spin_lock(&cache->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007945 btrfs_info(fs_info,
7946 "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s",
7947 cache->key.objectid, cache->key.offset,
7948 btrfs_block_group_used(&cache->item), cache->pinned,
7949 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04007950 btrfs_dump_free_space(cache, bytes);
7951 spin_unlock(&cache->lock);
7952 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007953 if (++index < BTRFS_NR_RAID_TYPES)
7954 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04007955 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007956}
Zheng Yane8569812008-09-26 10:05:48 -04007957
Nikolay Borisov6f47c702018-03-13 12:22:32 +02007958/*
7959 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
7960 * hole that is at least as big as @num_bytes.
7961 *
7962 * @root - The root that will contain this extent
7963 *
7964 * @ram_bytes - The amount of space in ram that @num_bytes take. This
7965 * is used for accounting purposes. This value differs
7966 * from @num_bytes only in the case of compressed extents.
7967 *
7968 * @num_bytes - Number of bytes to allocate on-disk.
7969 *
7970 * @min_alloc_size - Indicates the minimum amount of space that the
7971 * allocator should try to satisfy. In some cases
7972 * @num_bytes may be larger than what is required and if
7973 * the filesystem is fragmented then allocation fails.
7974 * However, the presence of @min_alloc_size gives a
7975 * chance to try and satisfy the smaller allocation.
7976 *
7977 * @empty_size - A hint that you plan on doing more COW. This is the
7978 * size in bytes the allocator should try to find free
7979 * next to the block it returns. This is just a hint and
7980 * may be ignored by the allocator.
7981 *
7982 * @hint_byte - Hint to the allocator to start searching above the byte
7983 * address passed. It might be ignored.
7984 *
7985 * @ins - This key is modified to record the found hole. It will
7986 * have the following values:
7987 * ins->objectid == start position
7988 * ins->flags = BTRFS_EXTENT_ITEM_KEY
7989 * ins->offset == the size of the hole.
7990 *
7991 * @is_data - Boolean flag indicating whether an extent is
7992 * allocated for data (true) or metadata (false)
7993 *
7994 * @delalloc - Boolean flag indicating whether this allocation is for
7995 * delalloc or not. If 'true' data_rwsem of block groups
7996 * is going to be acquired.
7997 *
7998 *
7999 * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
8000 * case -ENOSPC is returned then @ins->offset will contain the size of the
8001 * largest available hole the allocator managed to find.
8002 */
Wang Xiaoguang18513092016-07-25 15:51:40 +08008003int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04008004 u64 num_bytes, u64 min_alloc_size,
8005 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08008006 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05008007{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008008 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04008009 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00008010 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05008011 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04008012
Jeff Mahoney1b868262017-05-17 11:38:35 -04008013 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04008014again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008015 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05008016 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08008017 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008018 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008019 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008020 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08008021 if (!final_tried && ins->offset) {
8022 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04008023 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008024 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05008025 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08008026 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05008027 if (num_bytes == min_alloc_size)
8028 final_tried = true;
8029 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008030 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05008031 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008032
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008033 sinfo = __find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008034 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008035 "allocation failed flags %llu, wanted %llu",
8036 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01008037 if (sinfo)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008038 dump_space_info(fs_info, sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05008039 }
Chris Mason925baed2008-06-25 16:01:30 -04008040 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008041
8042 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008043}
8044
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008045static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008046 u64 start, u64 len,
8047 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04008048{
Josef Bacik0f9dd462008-09-23 13:14:11 -04008049 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05008050 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008051
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008052 cache = btrfs_lookup_block_group(fs_info, start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008053 if (!cache) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008054 btrfs_err(fs_info, "Unable to find block group for %llu",
8055 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008056 return -ENOSPC;
8057 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05008058
Chris Masone688b7252011-10-31 20:52:39 -04008059 if (pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008060 pin_down_extent(fs_info, cache, start, len, 1);
Chris Masone688b7252011-10-31 20:52:39 -04008061 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008062 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008063 ret = btrfs_discard_extent(fs_info, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04008064 btrfs_add_free_space(cache, start, len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08008065 btrfs_free_reserved_bytes(cache, len, delalloc);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008066 trace_btrfs_reserved_extent_free(fs_info, start, len);
Chris Masone688b7252011-10-31 20:52:39 -04008067 }
Dongsheng Yang31193212014-12-12 16:44:35 +08008068
Chris Masonfa9c0d792009-04-03 09:47:43 -04008069 btrfs_put_block_group(cache);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008070 return ret;
8071}
8072
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008073int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008074 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04008075{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008076 return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04008077}
8078
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008079int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04008080 u64 start, u64 len)
8081{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008082 return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04008083}
8084
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008085static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008086 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008087 u64 parent, u64 root_objectid,
8088 u64 flags, u64 owner, u64 offset,
8089 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008090{
8091 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008092 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008093 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008094 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008095 struct extent_buffer *leaf;
8096 int type;
8097 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04008098
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008099 if (parent > 0)
8100 type = BTRFS_SHARED_DATA_REF_KEY;
8101 else
8102 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04008103
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008104 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05008105
8106 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00008107 if (!path)
8108 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05008109
Chris Masonb9473432009-03-13 11:00:37 -04008110 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008111 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8112 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008113 if (ret) {
8114 btrfs_free_path(path);
8115 return ret;
8116 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008117
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008118 leaf = path->nodes[0];
8119 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05008120 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008121 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
8122 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8123 btrfs_set_extent_flags(leaf, extent_item,
8124 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05008125
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008126 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
8127 btrfs_set_extent_inline_ref_type(leaf, iref, type);
8128 if (parent > 0) {
8129 struct btrfs_shared_data_ref *ref;
8130 ref = (struct btrfs_shared_data_ref *)(iref + 1);
8131 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8132 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
8133 } else {
8134 struct btrfs_extent_data_ref *ref;
8135 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
8136 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
8137 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
8138 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
8139 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
8140 }
Chris Mason47e4bb92008-02-01 14:51:59 -05008141
8142 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05008143 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04008144
Nikolay Borisov25a356d2018-05-10 15:44:54 +03008145 ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008146 if (ret)
8147 return ret;
8148
Jeff Mahoney6202df62016-06-22 18:54:22 -04008149 ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008150 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008151 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008152 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05008153 BUG();
8154 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008155 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008156 return ret;
8157}
8158
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008159static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008160 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03008161 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008162{
Nikolay Borisov9dcdbe02018-05-21 12:27:20 +03008163 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008164 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008165 struct btrfs_extent_item *extent_item;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008166 struct btrfs_key extent_key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008167 struct btrfs_tree_block_info *block_info;
8168 struct btrfs_extent_inline_ref *iref;
8169 struct btrfs_path *path;
8170 struct extent_buffer *leaf;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008171 struct btrfs_delayed_tree_ref *ref;
Josef Bacik3173a182013-03-07 14:22:04 -05008172 u32 size = sizeof(*extent_item) + sizeof(*iref);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008173 u64 num_bytes;
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03008174 u64 flags = extent_op->flags_to_set;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008175 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05008176
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008177 ref = btrfs_delayed_node_to_tree_ref(node);
8178
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008179 extent_key.objectid = node->bytenr;
8180 if (skinny_metadata) {
8181 extent_key.offset = ref->level;
8182 extent_key.type = BTRFS_METADATA_ITEM_KEY;
8183 num_bytes = fs_info->nodesize;
8184 } else {
8185 extent_key.offset = node->num_bytes;
8186 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik3173a182013-03-07 14:22:04 -05008187 size += sizeof(*block_info);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008188 num_bytes = node->num_bytes;
8189 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008190
8191 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04008192 if (!path) {
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008193 btrfs_free_and_pin_reserved_extent(fs_info,
8194 extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008195 fs_info->nodesize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07008196 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04008197 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008198
8199 path->leave_spinning = 1;
8200 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008201 &extent_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008202 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07008203 btrfs_free_path(path);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008204 btrfs_free_and_pin_reserved_extent(fs_info,
8205 extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008206 fs_info->nodesize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008207 return ret;
8208 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008209
8210 leaf = path->nodes[0];
8211 extent_item = btrfs_item_ptr(leaf, path->slots[0],
8212 struct btrfs_extent_item);
8213 btrfs_set_extent_refs(leaf, extent_item, 1);
8214 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8215 btrfs_set_extent_flags(leaf, extent_item,
8216 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008217
Josef Bacik3173a182013-03-07 14:22:04 -05008218 if (skinny_metadata) {
8219 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
8220 } else {
8221 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03008222 btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008223 btrfs_set_tree_block_level(leaf, block_info, ref->level);
Josef Bacik3173a182013-03-07 14:22:04 -05008224 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
8225 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008226
Nikolay Borisovd4b20732018-05-21 12:27:23 +03008227 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008228 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
8229 btrfs_set_extent_inline_ref_type(leaf, iref,
8230 BTRFS_SHARED_BLOCK_REF_KEY);
Nikolay Borisovd4b20732018-05-21 12:27:23 +03008231 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008232 } else {
8233 btrfs_set_extent_inline_ref_type(leaf, iref,
8234 BTRFS_TREE_BLOCK_REF_KEY);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008235 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008236 }
8237
8238 btrfs_mark_buffer_dirty(leaf);
8239 btrfs_free_path(path);
8240
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008241 ret = remove_from_free_space_tree(trans, extent_key.objectid,
8242 num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008243 if (ret)
8244 return ret;
8245
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008246 ret = update_block_group(trans, fs_info, extent_key.objectid,
Jeff Mahoney6202df62016-06-22 18:54:22 -04008247 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008248 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008249 btrfs_err(fs_info, "update block group failed for %llu %llu",
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008250 extent_key.objectid, extent_key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008251 BUG();
8252 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04008253
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008254 trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008255 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008256 return ret;
8257}
8258
8259int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008260 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08008261 u64 offset, u64 ram_bytes,
8262 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008263{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008264 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008265 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04008266
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008267 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04008268
Josef Bacikfd708b82017-09-29 15:43:50 -04008269 btrfs_ref_tree_mod(root, ins->objectid, ins->offset, 0,
8270 root->root_key.objectid, owner, offset,
8271 BTRFS_ADD_DELAYED_EXTENT);
8272
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008273 ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008274 ins->offset, 0,
8275 root->root_key.objectid, owner,
Omar Sandoval7be07912017-06-06 16:45:30 -07008276 offset, ram_bytes,
8277 BTRFS_ADD_DELAYED_EXTENT, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008278 return ret;
8279}
Chris Masone02119d2008-09-05 16:13:11 -04008280
8281/*
8282 * this is used by the tree logging recovery code. It records that
8283 * an extent has been allocated and makes sure to clear the free
8284 * space cache bits as well
8285 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008286int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008287 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008288 u64 root_objectid, u64 owner, u64 offset,
8289 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04008290{
8291 int ret;
8292 struct btrfs_block_group_cache *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008293 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008294
8295 /*
8296 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04008297 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008298 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008299 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008300 ret = __exclude_logged_extent(fs_info, ins->objectid,
8301 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008302 if (ret)
8303 return ret;
8304 }
Chris Masone02119d2008-09-05 16:13:11 -04008305
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008306 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008307 if (!block_group)
8308 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04008309
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008310 space_info = block_group->space_info;
8311 spin_lock(&space_info->lock);
8312 spin_lock(&block_group->lock);
8313 space_info->bytes_reserved += ins->offset;
8314 block_group->reserved += ins->offset;
8315 spin_unlock(&block_group->lock);
8316 spin_unlock(&space_info->lock);
8317
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008318 ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008319 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04008320 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04008321 return ret;
8322}
8323
Eric Sandeen48a3b632013-04-25 20:41:01 +00008324static struct extent_buffer *
8325btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
David Sterbafe864572014-06-15 02:28:42 +02008326 u64 bytenr, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04008327{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008328 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04008329 struct extent_buffer *buf;
8330
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008331 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008332 if (IS_ERR(buf))
8333 return buf;
8334
Chris Mason65b51a02008-08-01 15:11:20 -04008335 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04008336 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04008337 btrfs_tree_lock(buf);
David Sterba7c302b42017-02-10 18:47:57 +01008338 clean_tree_block(fs_info, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05008339 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008340
8341 btrfs_set_lock_blocking(buf);
David Sterba4db8c522015-12-03 13:06:46 +01008342 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008343
Chris Masond0c803c2008-09-11 16:17:57 -04008344 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01008345 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008346 /*
8347 * we allow two log transactions at a time, use different
8348 * EXENT bit to differentiate dirty pages.
8349 */
Filipe Manana656f30d2014-09-26 12:25:56 +01008350 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008351 set_extent_dirty(&root->dirty_log_pages, buf->start,
8352 buf->start + buf->len - 1, GFP_NOFS);
8353 else
8354 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02008355 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04008356 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01008357 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04008358 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
8359 buf->start + buf->len - 1, GFP_NOFS);
8360 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04008361 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05008362 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04008363 return buf;
8364}
8365
Yan, Zhengf0486c62010-05-16 10:46:25 -04008366static struct btrfs_block_rsv *
8367use_block_rsv(struct btrfs_trans_handle *trans,
8368 struct btrfs_root *root, u32 blocksize)
8369{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008370 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008371 struct btrfs_block_rsv *block_rsv;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008372 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008373 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00008374 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008375
8376 block_rsv = get_block_rsv(trans, root);
8377
Miao Xieb586b322013-05-13 13:55:10 +00008378 if (unlikely(block_rsv->size == 0))
8379 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00008380again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04008381 ret = block_rsv_use_bytes(block_rsv, blocksize);
8382 if (!ret)
8383 return block_rsv;
8384
Miao Xieb586b322013-05-13 13:55:10 +00008385 if (block_rsv->failfast)
8386 return ERR_PTR(ret);
8387
Miao Xied88033d2013-05-13 13:55:12 +00008388 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
8389 global_updated = true;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008390 update_global_block_rsv(fs_info);
Miao Xied88033d2013-05-13 13:55:12 +00008391 goto again;
8392 }
8393
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008394 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xieb586b322013-05-13 13:55:10 +00008395 static DEFINE_RATELIMIT_STATE(_rs,
8396 DEFAULT_RATELIMIT_INTERVAL * 10,
8397 /*DEFAULT_RATELIMIT_BURST*/ 1);
8398 if (__ratelimit(&_rs))
8399 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05008400 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00008401 }
8402try_reserve:
8403 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
8404 BTRFS_RESERVE_NO_FLUSH);
8405 if (!ret)
8406 return block_rsv;
8407 /*
8408 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00008409 * the global reserve if its space type is the same as the global
8410 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00008411 */
Miao Xie5881cfc2013-05-13 13:55:11 +00008412 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
8413 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00008414 ret = block_rsv_use_bytes(global_rsv, blocksize);
8415 if (!ret)
8416 return global_rsv;
8417 }
8418 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008419}
8420
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008421static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
8422 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008423{
8424 block_rsv_add_bytes(block_rsv, blocksize, 0);
Qu Wenruoff6bc372017-12-21 13:42:04 +08008425 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0, NULL);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008426}
8427
Chris Masonfec577f2007-02-26 10:40:21 -05008428/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04008429 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08008430 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05008431 */
David Sterba4d75f8a2014-06-15 01:54:12 +02008432struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08008433 struct btrfs_root *root,
8434 u64 parent, u64 root_objectid,
8435 const struct btrfs_disk_key *key,
8436 int level, u64 hint,
8437 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05008438{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008439 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04008440 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008441 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04008442 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008443 struct btrfs_delayed_extent_op *extent_op;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008444 u64 flags = 0;
8445 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008446 u32 blocksize = fs_info->nodesize;
8447 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008448
David Sterba05653ef2016-07-15 15:23:37 +02008449#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008450 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008451 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
David Sterbafe864572014-06-15 02:28:42 +02008452 level);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008453 if (!IS_ERR(buf))
8454 root->alloc_bytenr += blocksize;
8455 return buf;
8456 }
David Sterba05653ef2016-07-15 15:23:37 +02008457#endif
David Sterbafccb84c2014-09-29 23:53:21 +02008458
Yan, Zhengf0486c62010-05-16 10:46:25 -04008459 block_rsv = use_block_rsv(trans, root, blocksize);
8460 if (IS_ERR(block_rsv))
8461 return ERR_CAST(block_rsv);
8462
Wang Xiaoguang18513092016-07-25 15:51:40 +08008463 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08008464 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008465 if (ret)
8466 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05008467
David Sterbafe864572014-06-15 02:28:42 +02008468 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
Omar Sandoval67b78592015-02-24 02:47:04 -08008469 if (IS_ERR(buf)) {
8470 ret = PTR_ERR(buf);
8471 goto out_free_reserved;
8472 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008473
8474 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
8475 if (parent == 0)
8476 parent = ins.objectid;
8477 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8478 } else
8479 BUG_ON(parent > 0);
8480
8481 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00008482 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08008483 if (!extent_op) {
8484 ret = -ENOMEM;
8485 goto out_free_buf;
8486 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008487 if (key)
8488 memcpy(&extent_op->key, key, sizeof(extent_op->key));
8489 else
8490 memset(&extent_op->key, 0, sizeof(extent_op->key));
8491 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01008492 extent_op->update_key = skinny_metadata ? false : true;
8493 extent_op->update_flags = true;
8494 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04008495 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008496
Josef Bacikfd708b82017-09-29 15:43:50 -04008497 btrfs_ref_tree_mod(root, ins.objectid, ins.offset, parent,
8498 root_objectid, level, 0,
8499 BTRFS_ADD_DELAYED_EXTENT);
Omar Sandoval7be07912017-06-06 16:45:30 -07008500 ret = btrfs_add_delayed_tree_ref(fs_info, trans, ins.objectid,
8501 ins.offset, parent,
8502 root_objectid, level,
Omar Sandoval67b78592015-02-24 02:47:04 -08008503 BTRFS_ADD_DELAYED_EXTENT,
Omar Sandoval7be07912017-06-06 16:45:30 -07008504 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08008505 if (ret)
8506 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008507 }
Chris Masonfec577f2007-02-26 10:40:21 -05008508 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008509
8510out_free_delayed:
8511 btrfs_free_delayed_extent_op(extent_op);
8512out_free_buf:
8513 free_extent_buffer(buf);
8514out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008515 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008516out_unuse:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008517 unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08008518 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05008519}
Chris Masona28ec192007-03-06 20:08:01 -05008520
Yan Zheng2c47e6052009-06-27 21:07:35 -04008521struct walk_control {
8522 u64 refs[BTRFS_MAX_LEVEL];
8523 u64 flags[BTRFS_MAX_LEVEL];
8524 struct btrfs_key update_progress;
8525 int stage;
8526 int level;
8527 int shared_level;
8528 int update_ref;
8529 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008530 int reada_slot;
8531 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008532 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008533};
8534
8535#define DROP_REFERENCE 1
8536#define UPDATE_BACKREF 2
8537
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008538static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8539 struct btrfs_root *root,
8540 struct walk_control *wc,
8541 struct btrfs_path *path)
8542{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008543 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008544 u64 bytenr;
8545 u64 generation;
8546 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008547 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008548 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008549 struct btrfs_key key;
8550 struct extent_buffer *eb;
8551 int ret;
8552 int slot;
8553 int nread = 0;
8554
8555 if (path->slots[wc->level] < wc->reada_slot) {
8556 wc->reada_count = wc->reada_count * 2 / 3;
8557 wc->reada_count = max(wc->reada_count, 2);
8558 } else {
8559 wc->reada_count = wc->reada_count * 3 / 2;
8560 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008561 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008562 }
8563
8564 eb = path->nodes[wc->level];
8565 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008566
8567 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
8568 if (nread >= wc->reada_count)
8569 break;
8570
8571 cond_resched();
8572 bytenr = btrfs_node_blockptr(eb, slot);
8573 generation = btrfs_node_ptr_generation(eb, slot);
8574
8575 if (slot == path->slots[wc->level])
8576 goto reada;
8577
8578 if (wc->stage == UPDATE_BACKREF &&
8579 generation <= root->root_key.offset)
8580 continue;
8581
Yan, Zheng94fcca92009-10-09 09:25:16 -04008582 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008583 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05008584 wc->level - 1, 1, &refs,
8585 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008586 /* We don't care about errors in readahead. */
8587 if (ret < 0)
8588 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008589 BUG_ON(refs == 0);
8590
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008591 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008592 if (refs == 1)
8593 goto reada;
8594
Yan, Zheng94fcca92009-10-09 09:25:16 -04008595 if (wc->level == 1 &&
8596 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8597 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008598 if (!wc->update_ref ||
8599 generation <= root->root_key.offset)
8600 continue;
8601 btrfs_node_key_to_cpu(eb, &key, slot);
8602 ret = btrfs_comp_cpu_keys(&key,
8603 &wc->update_progress);
8604 if (ret < 0)
8605 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008606 } else {
8607 if (wc->level == 1 &&
8608 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8609 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008610 }
8611reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008612 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008613 nread++;
8614 }
8615 wc->reada_slot = slot;
8616}
8617
Chris Mason9aca1d52007-03-13 11:09:37 -04008618/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008619 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008620 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04008621 * when wc->stage == UPDATE_BACKREF, this function updates
8622 * back refs for pointers in the block.
8623 *
8624 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04008625 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008626static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8627 struct btrfs_root *root,
8628 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008629 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04008630{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008631 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008632 int level = wc->level;
8633 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04008634 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8635 int ret;
8636
8637 if (wc->stage == UPDATE_BACKREF &&
8638 btrfs_header_owner(eb) != root->root_key.objectid)
8639 return 1;
8640
8641 /*
8642 * when reference count of tree block is 1, it won't increase
8643 * again. once full backref flag is set, we never clear it.
8644 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04008645 if (lookup_info &&
8646 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
8647 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008648 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008649 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008650 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008651 &wc->refs[level],
8652 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008653 BUG_ON(ret == -ENOMEM);
8654 if (ret)
8655 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008656 BUG_ON(wc->refs[level] == 0);
8657 }
8658
Yan Zheng2c47e6052009-06-27 21:07:35 -04008659 if (wc->stage == DROP_REFERENCE) {
8660 if (wc->refs[level] > 1)
8661 return 1;
8662
8663 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04008664 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008665 path->locks[level] = 0;
8666 }
8667 return 0;
8668 }
8669
8670 /* wc->stage == UPDATE_BACKREF */
8671 if (!(wc->flags[level] & flag)) {
8672 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07008673 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008674 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07008675 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008676 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008677 ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04008678 eb->len, flag,
8679 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008680 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008681 wc->flags[level] |= flag;
8682 }
8683
8684 /*
8685 * the block is shared by multiple trees, so it's not good to
8686 * keep the tree lock
8687 */
8688 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04008689 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008690 path->locks[level] = 0;
8691 }
8692 return 0;
8693}
8694
8695/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008696 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008697 *
8698 * when wc->stage == DROP_REFERENCE, this function checks
8699 * reference count of the block pointed to. if the block
8700 * is shared and we need update back refs for the subtree
8701 * rooted at the block, this function changes wc->stage to
8702 * UPDATE_BACKREF. if the block is shared and there is no
8703 * need to update back, this function drops the reference
8704 * to the block.
8705 *
8706 * NOTE: return value 1 means we should stop walking down.
8707 */
8708static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8709 struct btrfs_root *root,
8710 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008711 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008712{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008713 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008714 u64 bytenr;
8715 u64 generation;
8716 u64 parent;
8717 u32 blocksize;
8718 struct btrfs_key key;
Qu Wenruo581c1762018-03-29 09:08:11 +08008719 struct btrfs_key first_key;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008720 struct extent_buffer *next;
8721 int level = wc->level;
8722 int reada = 0;
8723 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07008724 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008725
8726 generation = btrfs_node_ptr_generation(path->nodes[level],
8727 path->slots[level]);
8728 /*
8729 * if the lower level block was created before the snapshot
8730 * was created, we know there is no need to update back refs
8731 * for the subtree
8732 */
8733 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04008734 generation <= root->root_key.offset) {
8735 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008736 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008737 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008738
8739 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08008740 btrfs_node_key_to_cpu(path->nodes[level], &first_key,
8741 path->slots[level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008742 blocksize = fs_info->nodesize;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008743
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008744 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008745 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008746 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008747 if (IS_ERR(next))
8748 return PTR_ERR(next);
8749
Josef Bacikb2aaaa32013-07-05 17:05:38 -04008750 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
8751 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008752 reada = 1;
8753 }
8754 btrfs_tree_lock(next);
8755 btrfs_set_lock_blocking(next);
8756
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008757 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008758 &wc->refs[level - 1],
8759 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02008760 if (ret < 0)
8761 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008762
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008763 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008764 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02008765 ret = -EIO;
8766 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008767 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008768 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008769
Yan, Zheng94fcca92009-10-09 09:25:16 -04008770 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008771 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07008772 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008773 if (level == 1 &&
8774 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8775 goto skip;
8776
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008777 if (!wc->update_ref ||
8778 generation <= root->root_key.offset)
8779 goto skip;
8780
8781 btrfs_node_key_to_cpu(path->nodes[level], &key,
8782 path->slots[level]);
8783 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
8784 if (ret < 0)
8785 goto skip;
8786
8787 wc->stage = UPDATE_BACKREF;
8788 wc->shared_level = level - 1;
8789 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008790 } else {
8791 if (level == 1 &&
8792 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8793 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008794 }
8795
Chris Masonb9fab912012-05-06 07:23:47 -04008796 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008797 btrfs_tree_unlock(next);
8798 free_extent_buffer(next);
8799 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008800 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008801 }
8802
8803 if (!next) {
8804 if (reada && level == 1)
8805 reada_walk_down(trans, root, wc, path);
Qu Wenruo581c1762018-03-29 09:08:11 +08008806 next = read_tree_block(fs_info, bytenr, generation, level - 1,
8807 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08008808 if (IS_ERR(next)) {
8809 return PTR_ERR(next);
8810 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04008811 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008812 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04008813 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008814 btrfs_tree_lock(next);
8815 btrfs_set_lock_blocking(next);
8816 }
8817
8818 level--;
Josef Bacik48672682016-09-23 13:23:28 +02008819 ASSERT(level == btrfs_header_level(next));
8820 if (level != btrfs_header_level(next)) {
8821 btrfs_err(root->fs_info, "mismatched level");
8822 ret = -EIO;
8823 goto out_unlock;
8824 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008825 path->nodes[level] = next;
8826 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008827 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008828 wc->level = level;
8829 if (wc->level == 1)
8830 wc->reada_slot = 0;
8831 return 0;
8832skip:
8833 wc->refs[level - 1] = 0;
8834 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008835 if (wc->stage == DROP_REFERENCE) {
8836 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
8837 parent = path->nodes[level]->start;
8838 } else {
Josef Bacik48672682016-09-23 13:23:28 +02008839 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04008840 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02008841 if (root->root_key.objectid !=
8842 btrfs_header_owner(path->nodes[level])) {
8843 btrfs_err(root->fs_info,
8844 "mismatched block owner");
8845 ret = -EIO;
8846 goto out_unlock;
8847 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008848 parent = 0;
8849 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008850
Mark Fasheh11526512014-07-17 12:39:01 -07008851 if (need_account) {
Qu Wenruo33d1f052016-10-18 09:31:28 +08008852 ret = btrfs_qgroup_trace_subtree(trans, root, next,
8853 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07008854 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008855 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008856 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
8857 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008858 }
8859 }
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008860 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008861 parent, root->root_key.objectid,
8862 level - 1, 0);
Josef Bacik48672682016-09-23 13:23:28 +02008863 if (ret)
8864 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008865 }
Josef Bacik48672682016-09-23 13:23:28 +02008866
8867 *lookup_info = 1;
8868 ret = 1;
8869
8870out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008871 btrfs_tree_unlock(next);
8872 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02008873
8874 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008875}
8876
8877/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008878 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008879 *
8880 * when wc->stage == DROP_REFERENCE, this function drops
8881 * reference count on the block.
8882 *
8883 * when wc->stage == UPDATE_BACKREF, this function changes
8884 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8885 * to UPDATE_BACKREF previously while processing the block.
8886 *
8887 * NOTE: return value 1 means we should stop walking up.
8888 */
8889static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8890 struct btrfs_root *root,
8891 struct btrfs_path *path,
8892 struct walk_control *wc)
8893{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008894 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008895 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008896 int level = wc->level;
8897 struct extent_buffer *eb = path->nodes[level];
8898 u64 parent = 0;
8899
8900 if (wc->stage == UPDATE_BACKREF) {
8901 BUG_ON(wc->shared_level < level);
8902 if (level < wc->shared_level)
8903 goto out;
8904
Yan Zheng2c47e6052009-06-27 21:07:35 -04008905 ret = find_next_key(path, level + 1, &wc->update_progress);
8906 if (ret > 0)
8907 wc->update_ref = 0;
8908
8909 wc->stage = DROP_REFERENCE;
8910 wc->shared_level = -1;
8911 path->slots[level] = 0;
8912
8913 /*
8914 * check reference count again if the block isn't locked.
8915 * we should start walking down the tree again if reference
8916 * count is one.
8917 */
8918 if (!path->locks[level]) {
8919 BUG_ON(level == 0);
8920 btrfs_tree_lock(eb);
8921 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008922 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008923
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008924 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008925 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008926 &wc->refs[level],
8927 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008928 if (ret < 0) {
8929 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008930 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008931 return ret;
8932 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008933 BUG_ON(wc->refs[level] == 0);
8934 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04008935 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008936 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008937 return 1;
8938 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008939 }
8940 }
8941
8942 /* wc->stage == DROP_REFERENCE */
8943 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
8944
8945 if (wc->refs[level] == 1) {
8946 if (level == 0) {
8947 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07008948 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008949 else
Josef Bacike339a6b2014-07-02 10:54:25 -07008950 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008951 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008952 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb);
Mark Fasheh11526512014-07-17 12:39:01 -07008953 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008954 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008955 "error %d accounting leaf items. Quota is out of sync, rescan required.",
8956 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008957 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008958 }
8959 /* make block locked assertion in clean_tree_block happy */
8960 if (!path->locks[level] &&
8961 btrfs_header_generation(eb) == trans->transid) {
8962 btrfs_tree_lock(eb);
8963 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008964 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008965 }
David Sterba7c302b42017-02-10 18:47:57 +01008966 clean_tree_block(fs_info, eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008967 }
8968
8969 if (eb == root->node) {
8970 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8971 parent = eb->start;
8972 else
8973 BUG_ON(root->root_key.objectid !=
8974 btrfs_header_owner(eb));
8975 } else {
8976 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8977 parent = path->nodes[level + 1]->start;
8978 else
8979 BUG_ON(root->root_key.objectid !=
8980 btrfs_header_owner(path->nodes[level + 1]));
8981 }
8982
Jan Schmidt5581a512012-05-16 17:04:52 +02008983 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008984out:
8985 wc->refs[level] = 0;
8986 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008987 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008988}
8989
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008990static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
8991 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008992 struct btrfs_path *path,
8993 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008994{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008995 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008996 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008997 int ret;
8998
Yan Zheng2c47e6052009-06-27 21:07:35 -04008999 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04009000 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009001 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04009002 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009003
Yan Zheng2c47e6052009-06-27 21:07:35 -04009004 if (level == 0)
9005 break;
9006
Yan, Zheng7a7965f2010-02-01 02:41:17 +00009007 if (path->slots[level] >=
9008 btrfs_header_nritems(path->nodes[level]))
9009 break;
9010
Yan, Zheng94fcca92009-10-09 09:25:16 -04009011 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009012 if (ret > 0) {
9013 path->slots[level]++;
9014 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00009015 } else if (ret < 0)
9016 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009017 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009018 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009019 return 0;
9020}
9021
Chris Masond3977122009-01-05 21:25:51 -05009022static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05009023 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04009024 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009025 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05009026{
Yan Zheng2c47e6052009-06-27 21:07:35 -04009027 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05009028 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04009029
Yan Zheng2c47e6052009-06-27 21:07:35 -04009030 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
9031 while (level < max_level && path->nodes[level]) {
9032 wc->level = level;
9033 if (path->slots[level] + 1 <
9034 btrfs_header_nritems(path->nodes[level])) {
9035 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05009036 return 0;
9037 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009038 ret = walk_up_proc(trans, root, path, wc);
9039 if (ret > 0)
9040 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05009041
Yan Zheng2c47e6052009-06-27 21:07:35 -04009042 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04009043 btrfs_tree_unlock_rw(path->nodes[level],
9044 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009045 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009046 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009047 free_extent_buffer(path->nodes[level]);
9048 path->nodes[level] = NULL;
9049 level++;
Chris Mason20524f02007-03-10 06:35:47 -05009050 }
9051 }
9052 return 1;
9053}
9054
Chris Mason9aca1d52007-03-13 11:09:37 -04009055/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04009056 * drop a subvolume tree.
9057 *
9058 * this function traverses the tree freeing any blocks that only
9059 * referenced by the tree.
9060 *
9061 * when a shared tree block is found. this function decreases its
9062 * reference count by one. if update_ref is true, this function
9063 * also make sure backrefs for the shared block and all lower level
9064 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00009065 *
9066 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04009067 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04009068int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009069 struct btrfs_block_rsv *block_rsv, int update_ref,
9070 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05009071{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009072 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04009073 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009074 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009075 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04009076 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009077 struct walk_control *wc;
9078 struct btrfs_key key;
9079 int err = 0;
9080 int ret;
9081 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04009082 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05009083
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009084 btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07009085
Chris Mason5caf2a02007-04-02 11:20:42 -04009086 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009087 if (!path) {
9088 err = -ENOMEM;
9089 goto out;
9090 }
Chris Mason20524f02007-03-10 06:35:47 -05009091
Yan Zheng2c47e6052009-06-27 21:07:35 -04009092 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07009093 if (!wc) {
9094 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009095 err = -ENOMEM;
9096 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07009097 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009098
Yan, Zhenga22285a2010-05-16 10:48:46 -04009099 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009100 if (IS_ERR(trans)) {
9101 err = PTR_ERR(trans);
9102 goto out_free;
9103 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00009104
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009105 if (block_rsv)
9106 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009107
Chris Mason9f3a7422007-08-07 15:52:19 -04009108 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009109 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009110 path->nodes[level] = btrfs_lock_root_node(root);
9111 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04009112 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009113 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009114 memset(&wc->update_progress, 0,
9115 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04009116 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04009117 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009118 memcpy(&wc->update_progress, &key,
9119 sizeof(wc->update_progress));
9120
Chris Mason6702ed42007-08-07 16:15:09 -04009121 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009122 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04009123 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009124 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
9125 path->lowest_level = 0;
9126 if (ret < 0) {
9127 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009128 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04009129 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009130 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009131
Chris Mason7d9eb122008-07-08 14:19:17 -04009132 /*
9133 * unlock our path, this is safe because only this
9134 * function is allowed to delete this snapshot
9135 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009136 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04009137
Yan Zheng2c47e6052009-06-27 21:07:35 -04009138 level = btrfs_header_level(root->node);
9139 while (1) {
9140 btrfs_tree_lock(path->nodes[level]);
9141 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009142 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009143
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009144 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009145 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05009146 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04009147 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009148 if (ret < 0) {
9149 err = ret;
9150 goto out_end_trans;
9151 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009152 BUG_ON(wc->refs[level] == 0);
9153
9154 if (level == root_item->drop_level)
9155 break;
9156
9157 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009158 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009159 WARN_ON(wc->refs[level] != 1);
9160 level--;
9161 }
9162 }
9163
9164 wc->level = level;
9165 wc->shared_level = -1;
9166 wc->stage = DROP_REFERENCE;
9167 wc->update_ref = update_ref;
9168 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009169 wc->for_reloc = for_reloc;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009170 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009171
9172 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00009173
Yan Zheng2c47e6052009-06-27 21:07:35 -04009174 ret = walk_down_tree(trans, root, path, wc);
9175 if (ret < 0) {
9176 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04009177 break;
9178 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009179
9180 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
9181 if (ret < 0) {
9182 err = ret;
9183 break;
9184 }
9185
9186 if (ret > 0) {
9187 BUG_ON(wc->stage != DROP_REFERENCE);
9188 break;
9189 }
9190
9191 if (wc->stage == DROP_REFERENCE) {
9192 level = wc->level;
9193 btrfs_node_key(path->nodes[level],
9194 &root_item->drop_progress,
9195 path->slots[level]);
9196 root_item->drop_level = level;
9197 }
9198
9199 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009200 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009201 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009202 ret = btrfs_update_root(trans, tree_root,
9203 &root->root_key,
9204 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009205 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009206 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009207 err = ret;
9208 goto out_end_trans;
9209 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009210
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009211 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009212 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009213 btrfs_debug(fs_info,
9214 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04009215 err = -EAGAIN;
9216 goto out_free;
9217 }
9218
Yan, Zhenga22285a2010-05-16 10:48:46 -04009219 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009220 if (IS_ERR(trans)) {
9221 err = PTR_ERR(trans);
9222 goto out_free;
9223 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009224 if (block_rsv)
9225 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04009226 }
Chris Mason20524f02007-03-10 06:35:47 -05009227 }
David Sterbab3b4aa72011-04-21 01:20:15 +02009228 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009229 if (err)
9230 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009231
Jeff Mahoney1cd54472017-08-17 10:25:11 -04009232 ret = btrfs_del_root(trans, fs_info, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009233 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009234 btrfs_abort_transaction(trans, ret);
Jeff Mahoneye19182c2017-12-04 13:11:45 -05009235 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009236 goto out_end_trans;
9237 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009238
Yan, Zheng76dda932009-09-21 16:00:26 -04009239 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00009240 ret = btrfs_find_root(tree_root, &root->root_key, path,
9241 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009242 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009243 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009244 err = ret;
9245 goto out_end_trans;
9246 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05009247 /* if we fail to delete the orphan item this time
9248 * around, it'll get picked up the next time.
9249 *
9250 * The most common failure here is just -ENOENT.
9251 */
9252 btrfs_del_orphan_item(trans, tree_root,
9253 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04009254 }
9255 }
9256
Miao Xie27cdeb72014-04-02 19:51:05 +08009257 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04009258 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009259 } else {
9260 free_extent_buffer(root->node);
9261 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00009262 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009263 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04009264 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009265out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009266 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009267out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04009268 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04009269 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009270out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04009271 /*
9272 * So if we need to stop dropping the snapshot for whatever reason we
9273 * need to make sure to add it back to the dead root list so that we
9274 * keep trying to do the work later. This also cleans up roots if we
9275 * don't have it in the radix (like when we recover after a power fail
9276 * or unmount) so we don't leak memory.
9277 */
Thomas Meyer897ca812017-10-07 16:02:21 +02009278 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04009279 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08009280 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009281 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04009282 return err;
Chris Mason20524f02007-03-10 06:35:47 -05009283}
Chris Mason9078a3e2007-04-26 16:46:15 -04009284
Yan Zheng2c47e6052009-06-27 21:07:35 -04009285/*
9286 * drop subtree rooted at tree block 'node'.
9287 *
9288 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009289 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04009290 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04009291int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
9292 struct btrfs_root *root,
9293 struct extent_buffer *node,
9294 struct extent_buffer *parent)
9295{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009296 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009297 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009298 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009299 int level;
9300 int parent_level;
9301 int ret = 0;
9302 int wret;
9303
Yan Zheng2c47e6052009-06-27 21:07:35 -04009304 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
9305
Yan Zhengf82d02d2008-10-29 14:49:05 -04009306 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009307 if (!path)
9308 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009309
Yan Zheng2c47e6052009-06-27 21:07:35 -04009310 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009311 if (!wc) {
9312 btrfs_free_path(path);
9313 return -ENOMEM;
9314 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009315
Chris Masonb9447ef2009-03-09 11:45:38 -04009316 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009317 parent_level = btrfs_header_level(parent);
9318 extent_buffer_get(parent);
9319 path->nodes[parent_level] = parent;
9320 path->slots[parent_level] = btrfs_header_nritems(parent);
9321
Chris Masonb9447ef2009-03-09 11:45:38 -04009322 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009323 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009324 path->nodes[level] = node;
9325 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009326 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009327
9328 wc->refs[parent_level] = 1;
9329 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
9330 wc->level = level;
9331 wc->shared_level = -1;
9332 wc->stage = DROP_REFERENCE;
9333 wc->update_ref = 0;
9334 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009335 wc->for_reloc = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009336 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009337
9338 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009339 wret = walk_down_tree(trans, root, path, wc);
9340 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04009341 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009342 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009343 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009344
Yan Zheng2c47e6052009-06-27 21:07:35 -04009345 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009346 if (wret < 0)
9347 ret = wret;
9348 if (wret != 0)
9349 break;
9350 }
9351
Yan Zheng2c47e6052009-06-27 21:07:35 -04009352 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009353 btrfs_free_path(path);
9354 return ret;
9355}
9356
Jeff Mahoney6202df62016-06-22 18:54:22 -04009357static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04009358{
9359 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009360 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04009361
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009362 /*
9363 * if restripe for this chunk_type is on pick target profile and
9364 * return, otherwise do the usual balance
9365 */
Jeff Mahoney6202df62016-06-22 18:54:22 -04009366 stripped = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009367 if (stripped)
9368 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02009369
Jeff Mahoney6202df62016-06-22 18:54:22 -04009370 num_devices = fs_info->fs_devices->rw_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05009371
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009372 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05009373 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009374 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
9375
Chris Masonec44a352008-04-28 15:29:52 -04009376 if (num_devices == 1) {
9377 stripped |= BTRFS_BLOCK_GROUP_DUP;
9378 stripped = flags & ~stripped;
9379
9380 /* turn raid0 into single device chunks */
9381 if (flags & BTRFS_BLOCK_GROUP_RAID0)
9382 return stripped;
9383
9384 /* turn mirroring into duplication */
9385 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
9386 BTRFS_BLOCK_GROUP_RAID10))
9387 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04009388 } else {
9389 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04009390 if (flags & stripped)
9391 return flags;
9392
9393 stripped |= BTRFS_BLOCK_GROUP_DUP;
9394 stripped = flags & ~stripped;
9395
9396 /* switch duplicated blocks with raid1 */
9397 if (flags & BTRFS_BLOCK_GROUP_DUP)
9398 return stripped | BTRFS_BLOCK_GROUP_RAID1;
9399
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009400 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04009401 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009402
Chris Masonec44a352008-04-28 15:29:52 -04009403 return flags;
9404}
9405
Zhaolei868f4012015-08-05 16:43:27 +08009406static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04009407{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009408 struct btrfs_space_info *sinfo = cache->space_info;
9409 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00009410 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009411 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04009412
Miao Xie199c36e2011-07-15 10:34:36 +00009413 /*
9414 * We need some metadata space and system metadata space for
9415 * allocating chunks in some corner cases until we force to set
9416 * it to be readonly.
9417 */
9418 if ((sinfo->flags &
9419 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
9420 !force)
Byongho Leeee221842015-12-15 01:42:10 +09009421 min_allocable_bytes = SZ_1M;
Miao Xie199c36e2011-07-15 10:34:36 +00009422 else
9423 min_allocable_bytes = 0;
9424
Yan, Zhengf0486c62010-05-16 10:46:25 -04009425 spin_lock(&sinfo->lock);
9426 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00009427
9428 if (cache->ro) {
Zhaolei868f4012015-08-05 16:43:27 +08009429 cache->ro++;
WuBo61cfea92011-07-26 03:30:11 +00009430 ret = 0;
9431 goto out;
9432 }
9433
Yan, Zhengf0486c62010-05-16 10:46:25 -04009434 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
9435 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04009436
Liu Bo41361352017-02-13 15:42:21 -08009437 if (btrfs_space_info_used(sinfo, true) + num_bytes +
Josef Bacik37be25b2011-08-05 10:25:38 -04009438 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04009439 sinfo->bytes_readonly += num_bytes;
Zhaolei868f4012015-08-05 16:43:27 +08009440 cache->ro++;
Josef Bacik633c0aa2014-10-31 09:49:34 -04009441 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009442 ret = 0;
9443 }
WuBo61cfea92011-07-26 03:30:11 +00009444out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04009445 spin_unlock(&cache->lock);
9446 spin_unlock(&sinfo->lock);
9447 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04009448}
9449
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009450int btrfs_inc_block_group_ro(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04009451 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009452
9453{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009454 struct btrfs_trans_handle *trans;
9455 u64 alloc_flags;
9456 int ret;
9457
Chris Mason1bbc6212015-04-06 12:46:08 -07009458again:
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009459 trans = btrfs_join_transaction(fs_info->extent_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009460 if (IS_ERR(trans))
9461 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009462
Chris Mason1bbc6212015-04-06 12:46:08 -07009463 /*
9464 * we're not allowed to set block groups readonly after the dirty
9465 * block groups cache has started writing. If it already started,
9466 * back off and let this transaction commit
9467 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009468 mutex_lock(&fs_info->ro_block_group_mutex);
Josef Bacik3204d332015-09-24 10:46:10 -04009469 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
Chris Mason1bbc6212015-04-06 12:46:08 -07009470 u64 transid = trans->transid;
9471
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009472 mutex_unlock(&fs_info->ro_block_group_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009473 btrfs_end_transaction(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07009474
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009475 ret = btrfs_wait_for_commit(fs_info, transid);
Chris Mason1bbc6212015-04-06 12:46:08 -07009476 if (ret)
9477 return ret;
9478 goto again;
9479 }
9480
Chris Mason153c35b2015-05-19 18:54:41 -07009481 /*
9482 * if we are changing raid levels, try to allocate a corresponding
9483 * block group with the new raid level.
9484 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009485 alloc_flags = update_block_group_flags(fs_info, cache->flags);
Chris Mason153c35b2015-05-19 18:54:41 -07009486 if (alloc_flags != cache->flags) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009487 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason153c35b2015-05-19 18:54:41 -07009488 CHUNK_ALLOC_FORCE);
9489 /*
9490 * ENOSPC is allowed here, we may have enough space
9491 * already allocated at the new raid level to
9492 * carry on
9493 */
9494 if (ret == -ENOSPC)
9495 ret = 0;
9496 if (ret < 0)
9497 goto out;
9498 }
Chris Mason1bbc6212015-04-06 12:46:08 -07009499
Zhaolei868f4012015-08-05 16:43:27 +08009500 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009501 if (!ret)
9502 goto out;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009503 alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags);
9504 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04009505 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009506 if (ret < 0)
9507 goto out;
Zhaolei868f4012015-08-05 16:43:27 +08009508 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009509out:
Shaohua Li2f081082015-01-09 10:40:15 -08009510 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009511 alloc_flags = update_block_group_flags(fs_info, cache->flags);
David Sterba34441362016-10-04 19:34:27 +02009512 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009513 check_system_chunk(trans, fs_info, alloc_flags);
David Sterba34441362016-10-04 19:34:27 +02009514 mutex_unlock(&fs_info->chunk_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009515 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009516 mutex_unlock(&fs_info->ro_block_group_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009517
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009518 btrfs_end_transaction(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009519 return ret;
9520}
9521
Chris Masonc87f08c2011-02-16 13:57:04 -05009522int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009523 struct btrfs_fs_info *fs_info, u64 type)
Chris Masonc87f08c2011-02-16 13:57:04 -05009524{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009525 u64 alloc_flags = get_alloc_profile(fs_info, type);
9526
9527 return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05009528}
9529
Miao Xie6d07bce2011-01-05 10:07:31 +00009530/*
9531 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04009532 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00009533 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009534u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00009535{
9536 struct btrfs_block_group_cache *block_group;
9537 u64 free_bytes = 0;
9538 int factor;
9539
Nicholas D Steeves01327612016-05-19 21:18:45 -04009540 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009541 if (list_empty(&sinfo->ro_bgs))
9542 return 0;
9543
9544 spin_lock(&sinfo->lock);
9545 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00009546 spin_lock(&block_group->lock);
9547
9548 if (!block_group->ro) {
9549 spin_unlock(&block_group->lock);
9550 continue;
9551 }
9552
9553 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
9554 BTRFS_BLOCK_GROUP_RAID10 |
9555 BTRFS_BLOCK_GROUP_DUP))
9556 factor = 2;
9557 else
9558 factor = 1;
9559
9560 free_bytes += (block_group->key.offset -
9561 btrfs_block_group_used(&block_group->item)) *
9562 factor;
9563
9564 spin_unlock(&block_group->lock);
9565 }
Miao Xie6d07bce2011-01-05 10:07:31 +00009566 spin_unlock(&sinfo->lock);
9567
9568 return free_bytes;
9569}
9570
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009571void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache)
Yan, Zhengf0486c62010-05-16 10:46:25 -04009572{
9573 struct btrfs_space_info *sinfo = cache->space_info;
9574 u64 num_bytes;
9575
9576 BUG_ON(!cache->ro);
9577
9578 spin_lock(&sinfo->lock);
9579 spin_lock(&cache->lock);
Zhaolei868f4012015-08-05 16:43:27 +08009580 if (!--cache->ro) {
9581 num_bytes = cache->key.offset - cache->reserved -
9582 cache->pinned - cache->bytes_super -
9583 btrfs_block_group_used(&cache->item);
9584 sinfo->bytes_readonly -= num_bytes;
9585 list_del_init(&cache->ro_list);
9586 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009587 spin_unlock(&cache->lock);
9588 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009589}
9590
Josef Bacikba1bf482009-09-11 16:11:19 -04009591/*
9592 * checks to see if its even possible to relocate this block group.
9593 *
9594 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9595 * ok to go ahead and try.
9596 */
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009597int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04009598{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009599 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04009600 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04009601 struct btrfs_space_info *space_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009602 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacikba1bf482009-09-11 16:11:19 -04009603 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04009604 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00009605 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04009606 u64 dev_min = 1;
9607 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009608 u64 target;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009609 int debug;
liubocdcb7252011-08-03 10:15:25 +00009610 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04009611 int full = 0;
9612 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05009613
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009614 debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG);
Qu Wenruo0305bc22016-03-23 11:38:17 +08009615
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009616 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04009617
Josef Bacikba1bf482009-09-11 16:11:19 -04009618 /* odd, couldn't find the block group, leave it alone */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009619 if (!block_group) {
9620 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009621 btrfs_warn(fs_info,
Qu Wenruo0305bc22016-03-23 11:38:17 +08009622 "can't find block group for bytenr %llu",
9623 bytenr);
Josef Bacikba1bf482009-09-11 16:11:19 -04009624 return -1;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009625 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009626
liubocdcb7252011-08-03 10:15:25 +00009627 min_free = btrfs_block_group_used(&block_group->item);
9628
Josef Bacikba1bf482009-09-11 16:11:19 -04009629 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00009630 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04009631 goto out;
Chris Mason323da792008-05-09 11:46:48 -04009632
Josef Bacikba1bf482009-09-11 16:11:19 -04009633 space_info = block_group->space_info;
9634 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04009635
Josef Bacikba1bf482009-09-11 16:11:19 -04009636 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04009637
Josef Bacikba1bf482009-09-11 16:11:19 -04009638 /*
9639 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04009640 * relocate it unless we're able to allocate a new chunk below.
9641 *
9642 * Otherwise, we need to make sure we have room in the space to handle
9643 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04009644 */
Chris Mason7ce618d2009-09-22 14:48:44 -04009645 if ((space_info->total_bytes != block_group->key.offset) &&
Liu Bo41361352017-02-13 15:42:21 -08009646 (btrfs_space_info_used(space_info, false) + min_free <
9647 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04009648 spin_unlock(&space_info->lock);
9649 goto out;
9650 }
9651 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009652
Josef Bacikba1bf482009-09-11 16:11:19 -04009653 /*
9654 * ok we don't have enough space, but maybe we have free space on our
9655 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009656 * alloc devices and guess if we have enough space. if this block
9657 * group is going to be restriped, run checks against the target
9658 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04009659 */
9660 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05009661
liubocdcb7252011-08-03 10:15:25 +00009662 /*
9663 * index:
9664 * 0: raid10
9665 * 1: raid1
9666 * 2: dup
9667 * 3: raid0
9668 * 4: single
9669 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009670 target = get_restripe_target(fs_info, block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009671 if (target) {
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009672 index = btrfs_bg_flags_to_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009673 } else {
9674 /*
9675 * this is just a balance, so if we were marked as full
9676 * we know there is no space for a new chunk
9677 */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009678 if (full) {
9679 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009680 btrfs_warn(fs_info,
9681 "no space to alloc new chunk for block group %llu",
9682 block_group->key.objectid);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009683 goto out;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009684 }
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009685
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009686 index = btrfs_bg_flags_to_raid_index(block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009687 }
9688
Miao Xiee6ec7162013-01-17 05:38:51 +00009689 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00009690 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04009691 /* Divide by 2 */
9692 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009693 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00009694 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00009695 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04009696 /* Multiply by 2 */
9697 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009698 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00009699 dev_min = fs_devices->rw_devices;
David Sterba47c57132015-02-20 18:43:47 +01009700 min_free = div64_u64(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00009701 }
9702
Josef Bacik6df9a952013-06-27 13:22:46 -04009703 /* We need to do this so that we can look at pending chunks */
9704 trans = btrfs_join_transaction(root);
9705 if (IS_ERR(trans)) {
9706 ret = PTR_ERR(trans);
9707 goto out;
9708 }
9709
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009710 mutex_lock(&fs_info->chunk_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04009711 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00009712 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04009713
Josef Bacikba1bf482009-09-11 16:11:19 -04009714 /*
9715 * check to make sure we can actually find a chunk with enough
9716 * space to fit our block group in.
9717 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01009718 if (device->total_bytes > device->bytes_used + min_free &&
Anand Jain401e29c2017-12-04 12:54:55 +08009719 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Josef Bacik6df9a952013-06-27 13:22:46 -04009720 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00009721 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04009722 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00009723 dev_nr++;
9724
9725 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05009726 break;
liubocdcb7252011-08-03 10:15:25 +00009727
Josef Bacikba1bf482009-09-11 16:11:19 -04009728 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05009729 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009730 }
Qu Wenruo0305bc22016-03-23 11:38:17 +08009731 if (debug && ret == -1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009732 btrfs_warn(fs_info,
9733 "no space to allocate a new chunk for block group %llu",
9734 block_group->key.objectid);
9735 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009736 btrfs_end_transaction(trans);
Chris Masonedbd8d42007-12-21 16:27:24 -05009737out:
Josef Bacikba1bf482009-09-11 16:11:19 -04009738 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05009739 return ret;
9740}
9741
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009742static int find_first_block_group(struct btrfs_fs_info *fs_info,
9743 struct btrfs_path *path,
9744 struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04009745{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009746 struct btrfs_root *root = fs_info->extent_root;
Chris Mason925baed2008-06-25 16:01:30 -04009747 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009748 struct btrfs_key found_key;
9749 struct extent_buffer *leaf;
9750 int slot;
9751
9752 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
9753 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009754 goto out;
9755
Chris Masond3977122009-01-05 21:25:51 -05009756 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009757 slot = path->slots[0];
9758 leaf = path->nodes[0];
9759 if (slot >= btrfs_header_nritems(leaf)) {
9760 ret = btrfs_next_leaf(root, path);
9761 if (ret == 0)
9762 continue;
9763 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009764 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04009765 break;
9766 }
9767 btrfs_item_key_to_cpu(leaf, &found_key, slot);
9768
9769 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04009770 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
Liu Bo6fb37b72016-06-22 18:31:27 -07009771 struct extent_map_tree *em_tree;
9772 struct extent_map *em;
9773
9774 em_tree = &root->fs_info->mapping_tree.map_tree;
9775 read_lock(&em_tree->lock);
9776 em = lookup_extent_mapping(em_tree, found_key.objectid,
9777 found_key.offset);
9778 read_unlock(&em_tree->lock);
9779 if (!em) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009780 btrfs_err(fs_info,
Liu Bo6fb37b72016-06-22 18:31:27 -07009781 "logical %llu len %llu found bg but no related chunk",
9782 found_key.objectid, found_key.offset);
9783 ret = -ENOENT;
9784 } else {
9785 ret = 0;
9786 }
Josef Bacik187ee582016-08-18 15:30:06 -04009787 free_extent_map(em);
Chris Mason925baed2008-06-25 16:01:30 -04009788 goto out;
9789 }
Chris Mason0b86a832008-03-24 15:01:56 -04009790 path->slots[0]++;
9791 }
Chris Mason925baed2008-06-25 16:01:30 -04009792out:
Chris Mason0b86a832008-03-24 15:01:56 -04009793 return ret;
9794}
9795
Josef Bacik0af3d002010-06-21 14:48:16 -04009796void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9797{
9798 struct btrfs_block_group_cache *block_group;
9799 u64 last = 0;
9800
9801 while (1) {
9802 struct inode *inode;
9803
9804 block_group = btrfs_lookup_first_block_group(info, last);
9805 while (block_group) {
9806 spin_lock(&block_group->lock);
9807 if (block_group->iref)
9808 break;
9809 spin_unlock(&block_group->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009810 block_group = next_block_group(info, block_group);
Josef Bacik0af3d002010-06-21 14:48:16 -04009811 }
9812 if (!block_group) {
9813 if (last == 0)
9814 break;
9815 last = 0;
9816 continue;
9817 }
9818
9819 inode = block_group->inode;
9820 block_group->iref = 0;
9821 block_group->inode = NULL;
9822 spin_unlock(&block_group->lock);
Liu Bof3bca802016-07-20 17:33:44 -07009823 ASSERT(block_group->io_ctl.inode == NULL);
Josef Bacik0af3d002010-06-21 14:48:16 -04009824 iput(inode);
9825 last = block_group->key.objectid + block_group->key.offset;
9826 btrfs_put_block_group(block_group);
9827 }
9828}
9829
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009830/*
9831 * Must be called only after stopping all workers, since we could have block
9832 * group caching kthreads running, and therefore they could race with us if we
9833 * freed the block groups before stopping them.
9834 */
Zheng Yan1a40e232008-09-26 10:09:34 -04009835int btrfs_free_block_groups(struct btrfs_fs_info *info)
9836{
9837 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04009838 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04009839 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04009840 struct rb_node *n;
9841
Josef Bacik9e351cc2014-03-13 15:42:13 -04009842 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009843 while (!list_empty(&info->caching_block_groups)) {
9844 caching_ctl = list_entry(info->caching_block_groups.next,
9845 struct btrfs_caching_control, list);
9846 list_del(&caching_ctl->list);
9847 put_caching_control(caching_ctl);
9848 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04009849 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009850
Josef Bacik47ab2a62014-09-18 11:20:02 -04009851 spin_lock(&info->unused_bgs_lock);
9852 while (!list_empty(&info->unused_bgs)) {
9853 block_group = list_first_entry(&info->unused_bgs,
9854 struct btrfs_block_group_cache,
9855 bg_list);
9856 list_del_init(&block_group->bg_list);
9857 btrfs_put_block_group(block_group);
9858 }
9859 spin_unlock(&info->unused_bgs_lock);
9860
Zheng Yan1a40e232008-09-26 10:09:34 -04009861 spin_lock(&info->block_group_cache_lock);
9862 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
9863 block_group = rb_entry(n, struct btrfs_block_group_cache,
9864 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04009865 rb_erase(&block_group->cache_node,
9866 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009867 RB_CLEAR_NODE(&block_group->cache_node);
Yan Zhengd899e052008-10-30 14:25:28 -04009868 spin_unlock(&info->block_group_cache_lock);
9869
Josef Bacik80eb2342008-10-29 14:49:05 -04009870 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04009871 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04009872 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05009873
Josef Bacik3c148742011-02-02 15:53:47 +00009874 /*
9875 * We haven't cached this block group, which means we could
9876 * possibly have excluded extents on this block group.
9877 */
Josef Bacik36cce922013-08-05 11:15:21 -04009878 if (block_group->cached == BTRFS_CACHE_NO ||
9879 block_group->cached == BTRFS_CACHE_ERROR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009880 free_excluded_extents(info, block_group);
Josef Bacik3c148742011-02-02 15:53:47 +00009881
Josef Bacik817d52f2009-07-13 21:29:25 -04009882 btrfs_remove_free_space_cache(block_group);
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009883 ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
Liu Bof3bca802016-07-20 17:33:44 -07009884 ASSERT(list_empty(&block_group->dirty_list));
9885 ASSERT(list_empty(&block_group->io_list));
9886 ASSERT(list_empty(&block_group->bg_list));
9887 ASSERT(atomic_read(&block_group->count) == 1);
Josef Bacik11dfe352009-11-13 20:12:59 +00009888 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04009889
9890 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009891 }
9892 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04009893
9894 /* now that all the block groups are freed, go through and
9895 * free all the space_info structs. This is only called during
9896 * the final stages of unmount, and so we know nobody is
9897 * using them. We call synchronize_rcu() once before we start,
9898 * just to be on the safe side.
9899 */
9900 synchronize_rcu();
9901
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04009902 release_global_block_rsv(info);
9903
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309904 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009905 int i;
9906
Chris Mason4184ea72009-03-10 12:39:20 -04009907 space_info = list_entry(info->space_info.next,
9908 struct btrfs_space_info,
9909 list);
Josef Bacikd555b6c2016-03-25 13:25:51 -04009910
9911 /*
9912 * Do not hide this behind enospc_debug, this is actually
9913 * important and indicates a real bug if this happens.
9914 */
9915 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00009916 space_info->bytes_reserved > 0 ||
Josef Bacikd555b6c2016-03-25 13:25:51 -04009917 space_info->bytes_may_use > 0))
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009918 dump_space_info(info, space_info, 0, 0);
Chris Mason4184ea72009-03-10 12:39:20 -04009919 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009920 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
9921 struct kobject *kobj;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009922 kobj = space_info->block_group_kobjs[i];
9923 space_info->block_group_kobjs[i] = NULL;
9924 if (kobj) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009925 kobject_del(kobj);
9926 kobject_put(kobj);
9927 }
9928 }
9929 kobject_del(&space_info->kobj);
9930 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04009931 }
Zheng Yan1a40e232008-09-26 10:09:34 -04009932 return 0;
9933}
9934
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009935/* link_block_group will queue up kobjects to add when we're reclaim-safe */
9936void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info)
9937{
9938 struct btrfs_space_info *space_info;
9939 struct raid_kobject *rkobj;
9940 LIST_HEAD(list);
9941 int index;
9942 int ret = 0;
9943
9944 spin_lock(&fs_info->pending_raid_kobjs_lock);
9945 list_splice_init(&fs_info->pending_raid_kobjs, &list);
9946 spin_unlock(&fs_info->pending_raid_kobjs_lock);
9947
9948 list_for_each_entry(rkobj, &list, list) {
9949 space_info = __find_space_info(fs_info, rkobj->flags);
9950 index = btrfs_bg_flags_to_raid_index(rkobj->flags);
9951
9952 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
9953 "%s", get_raid_name(index));
9954 if (ret) {
9955 kobject_put(&rkobj->kobj);
9956 break;
9957 }
9958 }
9959 if (ret)
9960 btrfs_warn(fs_info,
9961 "failed to add kobject for block cache, ignoring");
9962}
9963
Nikolay Borisovc434d212017-08-21 12:43:50 +03009964static void link_block_group(struct btrfs_block_group_cache *cache)
Yan, Zhengb742bb82010-05-16 10:46:24 -04009965{
Nikolay Borisovc434d212017-08-21 12:43:50 +03009966 struct btrfs_space_info *space_info = cache->space_info;
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009967 struct btrfs_fs_info *fs_info = cache->fs_info;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009968 int index = btrfs_bg_flags_to_raid_index(cache->flags);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009969 bool first = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04009970
9971 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009972 if (list_empty(&space_info->block_groups[index]))
9973 first = true;
9974 list_add_tail(&cache->list, &space_info->block_groups[index]);
9975 up_write(&space_info->groups_sem);
9976
9977 if (first) {
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009978 struct raid_kobject *rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
9979 if (!rkobj) {
9980 btrfs_warn(cache->fs_info,
9981 "couldn't alloc memory for raid level kobject");
9982 return;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009983 }
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009984 rkobj->flags = cache->flags;
9985 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
9986
9987 spin_lock(&fs_info->pending_raid_kobjs_lock);
9988 list_add_tail(&rkobj->list, &fs_info->pending_raid_kobjs);
9989 spin_unlock(&fs_info->pending_raid_kobjs_lock);
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009990 space_info->block_group_kobjs[index] = &rkobj->kobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009991 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04009992}
9993
Miao Xie920e4a52014-01-15 20:00:55 +08009994static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009995btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info,
9996 u64 start, u64 size)
Miao Xie920e4a52014-01-15 20:00:55 +08009997{
9998 struct btrfs_block_group_cache *cache;
9999
10000 cache = kzalloc(sizeof(*cache), GFP_NOFS);
10001 if (!cache)
10002 return NULL;
10003
10004 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
10005 GFP_NOFS);
10006 if (!cache->free_space_ctl) {
10007 kfree(cache);
10008 return NULL;
10009 }
10010
10011 cache->key.objectid = start;
10012 cache->key.offset = size;
10013 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
10014
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010015 cache->fs_info = fs_info;
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +030010016 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010017 set_free_space_tree_thresholds(cache);
10018
Miao Xie920e4a52014-01-15 20:00:55 +080010019 atomic_set(&cache->count, 1);
10020 spin_lock_init(&cache->lock);
Miao Xiee570fd22014-06-19 10:42:50 +080010021 init_rwsem(&cache->data_rwsem);
Miao Xie920e4a52014-01-15 20:00:55 +080010022 INIT_LIST_HEAD(&cache->list);
10023 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010024 INIT_LIST_HEAD(&cache->bg_list);
Josef Bacik633c0aa2014-10-31 09:49:34 -040010025 INIT_LIST_HEAD(&cache->ro_list);
Josef Bacikce93ec52014-11-17 15:45:48 -050010026 INIT_LIST_HEAD(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -070010027 INIT_LIST_HEAD(&cache->io_list);
Miao Xie920e4a52014-01-15 20:00:55 +080010028 btrfs_init_free_space_ctl(cache);
Filipe Manana04216822014-11-27 21:14:15 +000010029 atomic_set(&cache->trimming, 0);
Omar Sandovala5ed9182015-09-29 20:50:35 -070010030 mutex_init(&cache->free_space_lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +080010031 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
Miao Xie920e4a52014-01-15 20:00:55 +080010032
10033 return cache;
10034}
10035
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010036int btrfs_read_block_groups(struct btrfs_fs_info *info)
Chris Mason9078a3e2007-04-26 16:46:15 -040010037{
10038 struct btrfs_path *path;
10039 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010040 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -040010041 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -040010042 struct btrfs_key key;
10043 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -040010044 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -040010045 int need_clear = 0;
10046 u64 cache_gen;
Liu Bo49303382016-08-25 18:08:27 -070010047 u64 feature;
10048 int mixed;
10049
10050 feature = btrfs_super_incompat_flags(info->super_copy);
10051 mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS);
Chris Mason96b51792007-10-15 16:15:19 -040010052
Chris Mason9078a3e2007-04-26 16:46:15 -040010053 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -040010054 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010055 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Chris Mason9078a3e2007-04-26 16:46:15 -040010056 path = btrfs_alloc_path();
10057 if (!path)
10058 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +010010059 path->reada = READA_FORWARD;
Chris Mason9078a3e2007-04-26 16:46:15 -040010060
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010061 cache_gen = btrfs_super_cache_generation(info->super_copy);
10062 if (btrfs_test_opt(info, SPACE_CACHE) &&
10063 btrfs_super_generation(info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -040010064 need_clear = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010065 if (btrfs_test_opt(info, CLEAR_CACHE))
Josef Bacik88c2ba32010-09-21 14:21:34 -040010066 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -040010067
Chris Masond3977122009-01-05 21:25:51 -050010068 while (1) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010069 ret = find_first_block_group(info, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -040010070 if (ret > 0)
10071 break;
Chris Mason0b86a832008-03-24 15:01:56 -040010072 if (ret != 0)
10073 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +080010074
Chris Mason5f39d392007-10-15 16:14:19 -040010075 leaf = path->nodes[0];
10076 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +080010077
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010078 cache = btrfs_create_block_group_cache(info, found_key.objectid,
Miao Xie920e4a52014-01-15 20:00:55 +080010079 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -040010080 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -040010081 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -040010082 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -040010083 }
Josef Bacik96303082009-07-13 21:29:25 -040010084
Liu Bocf7c1ef2012-07-06 03:31:34 -060010085 if (need_clear) {
10086 /*
10087 * When we mount with old space cache, we need to
10088 * set BTRFS_DC_CLEAR and set dirty flag.
10089 *
10090 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
10091 * truncate the old free space cache inode and
10092 * setup a new one.
10093 * b) Setting 'dirty flag' makes sure that we flush
10094 * the new space cache info onto disk.
10095 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010096 if (btrfs_test_opt(info, SPACE_CACHE))
Josef Bacikce93ec52014-11-17 15:45:48 -050010097 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -060010098 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010099
Chris Mason5f39d392007-10-15 16:14:19 -040010100 read_extent_buffer(leaf, &cache->item,
10101 btrfs_item_ptr_offset(leaf, path->slots[0]),
10102 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +080010103 cache->flags = btrfs_block_group_flags(&cache->item);
Liu Bo49303382016-08-25 18:08:27 -070010104 if (!mixed &&
10105 ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
10106 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
10107 btrfs_err(info,
10108"bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
10109 cache->key.objectid);
10110 ret = -EINVAL;
10111 goto error;
10112 }
Chris Mason0b86a832008-03-24 15:01:56 -040010113
Chris Mason9078a3e2007-04-26 16:46:15 -040010114 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +020010115 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +080010116
Josef Bacik817d52f2009-07-13 21:29:25 -040010117 /*
Josef Bacik3c148742011-02-02 15:53:47 +000010118 * We need to exclude the super stripes now so that the space
10119 * info has super bytes accounted for, otherwise we'll think
10120 * we have more space than we actually do.
10121 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010122 ret = exclude_super_stripes(info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010123 if (ret) {
10124 /*
10125 * We may have excluded something, so call this just in
10126 * case.
10127 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010128 free_excluded_extents(info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010129 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010130 goto error;
10131 }
Josef Bacik3c148742011-02-02 15:53:47 +000010132
10133 /*
Josef Bacik817d52f2009-07-13 21:29:25 -040010134 * check for two cases, either we are full, and therefore
10135 * don't need to bother with the caching work since we won't
10136 * find any space, or we are empty, and we can just add all
10137 * the space in and be done with it. This saves us _alot_ of
10138 * time, particularly in the full case.
10139 */
10140 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -040010141 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010142 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010143 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010144 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -040010145 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010146 cache->cached = BTRFS_CACHE_FINISHED;
Nikolay Borisov4457c1c2018-05-10 15:44:45 +030010147 add_new_free_space(cache, found_key.objectid,
Josef Bacik817d52f2009-07-13 21:29:25 -040010148 found_key.objectid +
10149 found_key.offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010150 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010151 }
Chris Mason96b51792007-10-15 16:15:19 -040010152
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010153 ret = btrfs_add_block_group_cache(info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010154 if (ret) {
10155 btrfs_remove_free_space_cache(cache);
10156 btrfs_put_block_group(cache);
10157 goto error;
10158 }
10159
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010160 trace_btrfs_add_block_group(info, cache, 0);
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +030010161 update_space_info(info, cache->flags, found_key.offset,
10162 btrfs_block_group_used(&cache->item),
10163 cache->bytes_super, &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010164
Chris Mason6324fbf2008-03-24 15:01:59 -040010165 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -040010166
Nikolay Borisovc434d212017-08-21 12:43:50 +030010167 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010168
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010169 set_avail_alloc_bits(info, cache->flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010170 if (btrfs_chunk_readonly(info, cache->key.objectid)) {
Zhaolei868f4012015-08-05 16:43:27 +080010171 inc_block_group_ro(cache, 1);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010172 } else if (btrfs_block_group_used(&cache->item) == 0) {
10173 spin_lock(&info->unused_bgs_lock);
10174 /* Should always be true but just in case. */
10175 if (list_empty(&cache->bg_list)) {
10176 btrfs_get_block_group(cache);
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +080010177 trace_btrfs_add_unused_block_group(cache);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010178 list_add_tail(&cache->bg_list,
10179 &info->unused_bgs);
10180 }
10181 spin_unlock(&info->unused_bgs_lock);
10182 }
Chris Mason9078a3e2007-04-26 16:46:15 -040010183 }
Yan, Zhengb742bb82010-05-16 10:46:24 -040010184
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010185 list_for_each_entry_rcu(space_info, &info->space_info, list) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010186 if (!(get_alloc_profile(info, space_info->flags) &
Yan, Zhengb742bb82010-05-16 10:46:24 -040010187 (BTRFS_BLOCK_GROUP_RAID10 |
10188 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -050010189 BTRFS_BLOCK_GROUP_RAID5 |
10190 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -040010191 BTRFS_BLOCK_GROUP_DUP)))
10192 continue;
10193 /*
10194 * avoid allocating from un-mirrored block group if there are
10195 * mirrored block groups.
10196 */
chandan1095cc02013-07-16 12:28:56 +053010197 list_for_each_entry(cache,
10198 &space_info->block_groups[BTRFS_RAID_RAID0],
10199 list)
Zhaolei868f4012015-08-05 16:43:27 +080010200 inc_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +053010201 list_for_each_entry(cache,
10202 &space_info->block_groups[BTRFS_RAID_SINGLE],
10203 list)
Zhaolei868f4012015-08-05 16:43:27 +080010204 inc_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -040010205 }
Yan, Zhengf0486c62010-05-16 10:46:25 -040010206
Jeff Mahoney75cb3792018-03-20 15:25:26 -040010207 btrfs_add_raid_kobjects(info);
Yan, Zhengf0486c62010-05-16 10:46:25 -040010208 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -040010209 ret = 0;
10210error:
Chris Mason9078a3e2007-04-26 16:46:15 -040010211 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -040010212 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010213}
Chris Mason6324fbf2008-03-24 15:01:59 -040010214
Nikolay Borisov6c686b32018-02-07 17:55:40 +020010215void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
Josef Bacikea658ba2012-09-11 16:57:25 -040010216{
Nikolay Borisov6c686b32018-02-07 17:55:40 +020010217 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacikea658ba2012-09-11 16:57:25 -040010218 struct btrfs_block_group_cache *block_group, *tmp;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010219 struct btrfs_root *extent_root = fs_info->extent_root;
Josef Bacikea658ba2012-09-11 16:57:25 -040010220 struct btrfs_block_group_item item;
10221 struct btrfs_key key;
10222 int ret = 0;
Filipe Mananad9a05402015-10-03 13:13:13 +010010223 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010224
Filipe Mananad9a05402015-10-03 13:13:13 +010010225 trans->can_flush_pending_bgs = false;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010226 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
Josef Bacikea658ba2012-09-11 16:57:25 -040010227 if (ret)
Filipe Mananac92f6be2014-11-26 15:28:55 +000010228 goto next;
Josef Bacikea658ba2012-09-11 16:57:25 -040010229
10230 spin_lock(&block_group->lock);
10231 memcpy(&item, &block_group->item, sizeof(item));
10232 memcpy(&key, &block_group->key, sizeof(key));
10233 spin_unlock(&block_group->lock);
10234
10235 ret = btrfs_insert_item(trans, extent_root, &key, &item,
10236 sizeof(item));
10237 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010238 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010239 ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid,
10240 key.offset);
Josef Bacik6df9a952013-06-27 13:22:46 -040010241 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010242 btrfs_abort_transaction(trans, ret);
Nikolay Borisove4e07112018-05-10 15:44:41 +030010243 add_block_group_free_space(trans, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010244 /* already aborted the transaction if it failed. */
Filipe Mananac92f6be2014-11-26 15:28:55 +000010245next:
10246 list_del_init(&block_group->bg_list);
Josef Bacikea658ba2012-09-11 16:57:25 -040010247 }
Filipe Mananad9a05402015-10-03 13:13:13 +010010248 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010249}
10250
Chris Mason6324fbf2008-03-24 15:01:59 -040010251int btrfs_make_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010252 struct btrfs_fs_info *fs_info, u64 bytes_used,
Nikolay Borisov01744842017-07-27 14:22:11 +030010253 u64 type, u64 chunk_offset, u64 size)
Chris Mason6324fbf2008-03-24 15:01:59 -040010254{
Chris Mason6324fbf2008-03-24 15:01:59 -040010255 struct btrfs_block_group_cache *cache;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010256 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -040010257
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010258 btrfs_set_log_full_commit(fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -040010259
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010260 cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -040010261 if (!cache)
10262 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +080010263
Chris Mason6324fbf2008-03-24 15:01:59 -040010264 btrfs_set_block_group_used(&cache->item, bytes_used);
Nikolay Borisov01744842017-07-27 14:22:11 +030010265 btrfs_set_block_group_chunk_objectid(&cache->item,
10266 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Mason6324fbf2008-03-24 15:01:59 -040010267 btrfs_set_block_group_flags(&cache->item, type);
10268
Miao Xie920e4a52014-01-15 20:00:55 +080010269 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -040010270 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010271 cache->cached = BTRFS_CACHE_FINISHED;
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010272 cache->needs_free_space = 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010273 ret = exclude_super_stripes(fs_info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010274 if (ret) {
10275 /*
10276 * We may have excluded something, so call this just in
10277 * case.
10278 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010279 free_excluded_extents(fs_info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010280 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010281 return ret;
10282 }
Josef Bacik96303082009-07-13 21:29:25 -040010283
Nikolay Borisov4457c1c2018-05-10 15:44:45 +030010284 add_new_free_space(cache, chunk_offset, chunk_offset + size);
Josef Bacik817d52f2009-07-13 21:29:25 -040010285
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010286 free_excluded_extents(fs_info, cache);
Yan Zheng11833d62009-09-11 16:11:19 -040010287
Josef Bacikd0bd4562015-09-23 14:54:14 -040010288#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010289 if (btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -040010290 u64 new_bytes_used = size - bytes_used;
10291
10292 bytes_used += new_bytes_used >> 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010293 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -040010294 }
10295#endif
Filipe Manana2e6e5182015-05-12 00:28:11 +010010296 /*
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010297 * Ensure the corresponding space_info object is created and
10298 * assigned to our block group. We want our bg to be added to the rbtree
10299 * with its ->space_info set.
Filipe Manana2e6e5182015-05-12 00:28:11 +010010300 */
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010301 cache->space_info = __find_space_info(fs_info, cache->flags);
Jeff Mahoneydc2d3002018-03-20 15:25:25 -040010302 ASSERT(cache->space_info);
Filipe Manana2e6e5182015-05-12 00:28:11 +010010303
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010304 ret = btrfs_add_block_group_cache(fs_info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010305 if (ret) {
10306 btrfs_remove_free_space_cache(cache);
10307 btrfs_put_block_group(cache);
10308 return ret;
10309 }
10310
Filipe Manana2e6e5182015-05-12 00:28:11 +010010311 /*
10312 * Now that our block group has its ->space_info set and is inserted in
10313 * the rbtree, update the space info's counters.
10314 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010315 trace_btrfs_add_block_group(fs_info, cache, 1);
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +030010316 update_space_info(fs_info, cache->flags, size, bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -040010317 cache->bytes_super, &cache->space_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010318 update_global_block_rsv(fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -040010319
Nikolay Borisovc434d212017-08-21 12:43:50 +030010320 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010321
Josef Bacik47ab2a62014-09-18 11:20:02 -040010322 list_add_tail(&cache->bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -040010323
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010324 set_avail_alloc_bits(fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -040010325 return 0;
10326}
Zheng Yan1a40e232008-09-26 10:09:34 -040010327
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010328static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
10329{
Ilya Dryomov899c81e2012-03-27 17:09:16 +030010330 u64 extra_flags = chunk_to_extended(flags) &
10331 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010332
Miao Xiede98ced2013-01-29 10:13:12 +000010333 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010334 if (flags & BTRFS_BLOCK_GROUP_DATA)
10335 fs_info->avail_data_alloc_bits &= ~extra_flags;
10336 if (flags & BTRFS_BLOCK_GROUP_METADATA)
10337 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
10338 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
10339 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +000010340 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010341}
10342
Zheng Yan1a40e232008-09-26 10:09:34 -040010343int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010344 struct btrfs_fs_info *fs_info, u64 group_start,
Filipe Manana04216822014-11-27 21:14:15 +000010345 struct extent_map *em)
Zheng Yan1a40e232008-09-26 10:09:34 -040010346{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010347 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010348 struct btrfs_path *path;
10349 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -040010350 struct btrfs_free_cluster *cluster;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010351 struct btrfs_root *tree_root = fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010352 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -040010353 struct inode *inode;
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010354 struct kobject *kobj = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -040010355 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010356 int index;
Josef Bacik89a55892010-10-14 14:52:27 -040010357 int factor;
Filipe Manana4f69cb92014-11-26 15:28:51 +000010358 struct btrfs_caching_control *caching_ctl = NULL;
Filipe Manana04216822014-11-27 21:14:15 +000010359 bool remove_em;
Zheng Yan1a40e232008-09-26 10:09:34 -040010360
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010361 block_group = btrfs_lookup_block_group(fs_info, group_start);
Zheng Yan1a40e232008-09-26 10:09:34 -040010362 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -050010363 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -040010364
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +080010365 trace_btrfs_remove_block_group(block_group);
liubo9f7c43c2011-03-07 02:13:33 +000010366 /*
10367 * Free the reserved super bytes from this block group before
10368 * remove it.
10369 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010370 free_excluded_extents(fs_info, block_group);
Josef Bacikfd708b82017-09-29 15:43:50 -040010371 btrfs_free_ref_tree_range(fs_info, block_group->key.objectid,
10372 block_group->key.offset);
liubo9f7c43c2011-03-07 02:13:33 +000010373
Zheng Yan1a40e232008-09-26 10:09:34 -040010374 memcpy(&key, &block_group->key, sizeof(key));
Qu Wenruo3e72ee82018-01-30 18:20:45 +080010375 index = btrfs_bg_flags_to_raid_index(block_group->flags);
Josef Bacik89a55892010-10-14 14:52:27 -040010376 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
10377 BTRFS_BLOCK_GROUP_RAID1 |
10378 BTRFS_BLOCK_GROUP_RAID10))
10379 factor = 2;
10380 else
10381 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -040010382
Chris Mason44fb5512009-06-04 15:34:51 -040010383 /* make sure this block group isn't part of an allocation cluster */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010384 cluster = &fs_info->data_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010385 spin_lock(&cluster->refill_lock);
10386 btrfs_return_cluster_to_free_space(block_group, cluster);
10387 spin_unlock(&cluster->refill_lock);
10388
10389 /*
10390 * make sure this block group isn't part of a metadata
10391 * allocation cluster
10392 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010393 cluster = &fs_info->meta_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010394 spin_lock(&cluster->refill_lock);
10395 btrfs_return_cluster_to_free_space(block_group, cluster);
10396 spin_unlock(&cluster->refill_lock);
10397
Zheng Yan1a40e232008-09-26 10:09:34 -040010398 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010399 if (!path) {
10400 ret = -ENOMEM;
10401 goto out;
10402 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010403
Chris Mason1bbc6212015-04-06 12:46:08 -070010404 /*
10405 * get the inode first so any iput calls done for the io_list
10406 * aren't the final iput (no unlinks allowed now)
10407 */
Jeff Mahoney77ab86b2017-02-15 16:28:30 -050010408 inode = lookup_free_space_inode(fs_info, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010409
10410 mutex_lock(&trans->transaction->cache_write_mutex);
10411 /*
10412 * make sure our free spache cache IO is done before remove the
10413 * free space inode
10414 */
10415 spin_lock(&trans->transaction->dirty_bgs_lock);
10416 if (!list_empty(&block_group->io_list)) {
10417 list_del_init(&block_group->io_list);
10418
10419 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
10420
10421 spin_unlock(&trans->transaction->dirty_bgs_lock);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040010422 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010423 btrfs_put_block_group(block_group);
10424 spin_lock(&trans->transaction->dirty_bgs_lock);
10425 }
10426
10427 if (!list_empty(&block_group->dirty_list)) {
10428 list_del_init(&block_group->dirty_list);
10429 btrfs_put_block_group(block_group);
10430 }
10431 spin_unlock(&trans->transaction->dirty_bgs_lock);
10432 mutex_unlock(&trans->transaction->cache_write_mutex);
10433
Josef Bacik0af3d002010-06-21 14:48:16 -040010434 if (!IS_ERR(inode)) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010435 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010436 if (ret) {
10437 btrfs_add_delayed_iput(inode);
10438 goto out;
10439 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010440 clear_nlink(inode);
10441 /* One for the block groups ref */
10442 spin_lock(&block_group->lock);
10443 if (block_group->iref) {
10444 block_group->iref = 0;
10445 block_group->inode = NULL;
10446 spin_unlock(&block_group->lock);
10447 iput(inode);
10448 } else {
10449 spin_unlock(&block_group->lock);
10450 }
10451 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -040010452 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -040010453 }
10454
10455 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
10456 key.offset = block_group->key.objectid;
10457 key.type = 0;
10458
10459 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
10460 if (ret < 0)
10461 goto out;
10462 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +020010463 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010464 if (ret == 0) {
10465 ret = btrfs_del_item(trans, tree_root, path);
10466 if (ret)
10467 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +020010468 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010469 }
10470
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010471 spin_lock(&fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -040010472 rb_erase(&block_group->cache_node,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010473 &fs_info->block_group_cache_tree);
Filipe Manana292cbd52014-11-26 15:28:50 +000010474 RB_CLEAR_NODE(&block_group->cache_node);
Liu Boa1897fd2012-12-27 09:01:23 +000010475
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010476 if (fs_info->first_logical_byte == block_group->key.objectid)
10477 fs_info->first_logical_byte = (u64)-1;
10478 spin_unlock(&fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010479
Josef Bacik80eb2342008-10-29 14:49:05 -040010480 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -040010481 /*
10482 * we must use list_del_init so people can check to see if they
10483 * are still on the list after taking the semaphore
10484 */
10485 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010486 if (list_empty(&block_group->space_info->block_groups[index])) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010487 kobj = block_group->space_info->block_group_kobjs[index];
10488 block_group->space_info->block_group_kobjs[index] = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010489 clear_avail_alloc_bits(fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010490 }
Josef Bacik80eb2342008-10-29 14:49:05 -040010491 up_write(&block_group->space_info->groups_sem);
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010492 if (kobj) {
10493 kobject_del(kobj);
10494 kobject_put(kobj);
10495 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010496
Filipe Manana4f69cb92014-11-26 15:28:51 +000010497 if (block_group->has_caching_ctl)
10498 caching_ctl = get_caching_control(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -040010499 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -040010500 wait_block_group_cache_done(block_group);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010501 if (block_group->has_caching_ctl) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010502 down_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010503 if (!caching_ctl) {
10504 struct btrfs_caching_control *ctl;
10505
10506 list_for_each_entry(ctl,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010507 &fs_info->caching_block_groups, list)
Filipe Manana4f69cb92014-11-26 15:28:51 +000010508 if (ctl->block_group == block_group) {
10509 caching_ctl = ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +020010510 refcount_inc(&caching_ctl->count);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010511 break;
10512 }
10513 }
10514 if (caching_ctl)
10515 list_del_init(&caching_ctl->list);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010516 up_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010517 if (caching_ctl) {
10518 /* Once for the caching bgs list and once for us. */
10519 put_caching_control(caching_ctl);
10520 put_caching_control(caching_ctl);
10521 }
10522 }
Josef Bacik817d52f2009-07-13 21:29:25 -040010523
Josef Bacikce93ec52014-11-17 15:45:48 -050010524 spin_lock(&trans->transaction->dirty_bgs_lock);
10525 if (!list_empty(&block_group->dirty_list)) {
Chris Mason1bbc6212015-04-06 12:46:08 -070010526 WARN_ON(1);
10527 }
10528 if (!list_empty(&block_group->io_list)) {
10529 WARN_ON(1);
Josef Bacikce93ec52014-11-17 15:45:48 -050010530 }
10531 spin_unlock(&trans->transaction->dirty_bgs_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010532 btrfs_remove_free_space_cache(block_group);
10533
Yan Zhengc146afa2008-11-12 14:34:12 -050010534 spin_lock(&block_group->space_info->lock);
Filipe Manana75c68e92015-01-16 13:24:40 +000010535 list_del_init(&block_group->ro_list);
Zhao Lei18d018a2015-02-24 20:07:44 +080010536
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010537 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Zhao Lei18d018a2015-02-24 20:07:44 +080010538 WARN_ON(block_group->space_info->total_bytes
10539 < block_group->key.offset);
10540 WARN_ON(block_group->space_info->bytes_readonly
10541 < block_group->key.offset);
10542 WARN_ON(block_group->space_info->disk_total
10543 < block_group->key.offset * factor);
10544 }
Yan Zhengc146afa2008-11-12 14:34:12 -050010545 block_group->space_info->total_bytes -= block_group->key.offset;
10546 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -040010547 block_group->space_info->disk_total -= block_group->key.offset * factor;
Zhao Lei18d018a2015-02-24 20:07:44 +080010548
Yan Zhengc146afa2008-11-12 14:34:12 -050010549 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -040010550
Josef Bacik0af3d002010-06-21 14:48:16 -040010551 memcpy(&key, &block_group->key, sizeof(key));
10552
David Sterba34441362016-10-04 19:34:27 +020010553 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana495e64f2014-12-02 18:07:30 +000010554 if (!list_empty(&em->list)) {
10555 /* We're in the transaction->pending_chunks list. */
10556 free_extent_map(em);
10557 }
Filipe Manana04216822014-11-27 21:14:15 +000010558 spin_lock(&block_group->lock);
10559 block_group->removed = 1;
10560 /*
10561 * At this point trimming can't start on this block group, because we
10562 * removed the block group from the tree fs_info->block_group_cache_tree
10563 * so no one can't find it anymore and even if someone already got this
10564 * block group before we removed it from the rbtree, they have already
10565 * incremented block_group->trimming - if they didn't, they won't find
10566 * any free space entries because we already removed them all when we
10567 * called btrfs_remove_free_space_cache().
10568 *
10569 * And we must not remove the extent map from the fs_info->mapping_tree
10570 * to prevent the same logical address range and physical device space
10571 * ranges from being reused for a new block group. This is because our
10572 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
10573 * completely transactionless, so while it is trimming a range the
10574 * currently running transaction might finish and a new one start,
10575 * allowing for new block groups to be created that can reuse the same
10576 * physical device locations unless we take this special care.
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010577 *
10578 * There may also be an implicit trim operation if the file system
10579 * is mounted with -odiscard. The same protections must remain
10580 * in place until the extents have been discarded completely when
10581 * the transaction commit has completed.
Filipe Manana04216822014-11-27 21:14:15 +000010582 */
10583 remove_em = (atomic_read(&block_group->trimming) == 0);
10584 /*
10585 * Make sure a trimmer task always sees the em in the pinned_chunks list
10586 * if it sees block_group->removed == 1 (needs to lock block_group->lock
10587 * before checking block_group->removed).
10588 */
10589 if (!remove_em) {
10590 /*
10591 * Our em might be in trans->transaction->pending_chunks which
10592 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10593 * and so is the fs_info->pinned_chunks list.
10594 *
10595 * So at this point we must be holding the chunk_mutex to avoid
10596 * any races with chunk allocation (more specifically at
10597 * volumes.c:contains_pending_extent()), to ensure it always
10598 * sees the em, either in the pending_chunks list or in the
10599 * pinned_chunks list.
10600 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010601 list_move_tail(&em->list, &fs_info->pinned_chunks);
Filipe Manana04216822014-11-27 21:14:15 +000010602 }
10603 spin_unlock(&block_group->lock);
Filipe Manana04216822014-11-27 21:14:15 +000010604
10605 if (remove_em) {
10606 struct extent_map_tree *em_tree;
10607
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010608 em_tree = &fs_info->mapping_tree.map_tree;
Filipe Manana04216822014-11-27 21:14:15 +000010609 write_lock(&em_tree->lock);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010610 /*
10611 * The em might be in the pending_chunks list, so make sure the
10612 * chunk mutex is locked, since remove_extent_mapping() will
10613 * delete us from that list.
10614 */
Filipe Manana04216822014-11-27 21:14:15 +000010615 remove_extent_mapping(em_tree, em);
10616 write_unlock(&em_tree->lock);
10617 /* once for the tree */
10618 free_extent_map(em);
10619 }
10620
David Sterba34441362016-10-04 19:34:27 +020010621 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010622
Nikolay Borisovf3f72772018-05-10 15:44:46 +030010623 ret = remove_block_group_free_space(trans, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010624 if (ret)
10625 goto out;
10626
Chris Masonfa9c0d792009-04-03 09:47:43 -040010627 btrfs_put_block_group(block_group);
10628 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -040010629
10630 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
10631 if (ret > 0)
10632 ret = -EIO;
10633 if (ret < 0)
10634 goto out;
10635
10636 ret = btrfs_del_item(trans, root, path);
10637out:
10638 btrfs_free_path(path);
10639 return ret;
10640}
liuboacce9522011-01-06 19:30:25 +080010641
Filipe Manana8eab77f2015-11-13 23:57:16 +000010642struct btrfs_trans_handle *
Filipe Manana7fd01182015-11-13 23:57:17 +000010643btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info,
10644 const u64 chunk_offset)
Filipe Manana8eab77f2015-11-13 23:57:16 +000010645{
Filipe Manana7fd01182015-11-13 23:57:17 +000010646 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
10647 struct extent_map *em;
10648 struct map_lookup *map;
10649 unsigned int num_items;
10650
10651 read_lock(&em_tree->lock);
10652 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
10653 read_unlock(&em_tree->lock);
10654 ASSERT(em && em->start == chunk_offset);
10655
Filipe Manana8eab77f2015-11-13 23:57:16 +000010656 /*
Filipe Manana7fd01182015-11-13 23:57:17 +000010657 * We need to reserve 3 + N units from the metadata space info in order
10658 * to remove a block group (done at btrfs_remove_chunk() and at
10659 * btrfs_remove_block_group()), which are used for:
10660 *
Filipe Manana8eab77f2015-11-13 23:57:16 +000010661 * 1 unit for adding the free space inode's orphan (located in the tree
10662 * of tree roots).
Filipe Manana7fd01182015-11-13 23:57:17 +000010663 * 1 unit for deleting the block group item (located in the extent
10664 * tree).
10665 * 1 unit for deleting the free space item (located in tree of tree
10666 * roots).
10667 * N units for deleting N device extent items corresponding to each
10668 * stripe (located in the device tree).
10669 *
10670 * In order to remove a block group we also need to reserve units in the
10671 * system space info in order to update the chunk tree (update one or
10672 * more device items and remove one chunk item), but this is done at
10673 * btrfs_remove_chunk() through a call to check_system_chunk().
Filipe Manana8eab77f2015-11-13 23:57:16 +000010674 */
Jeff Mahoney95617d62015-06-03 10:55:48 -040010675 map = em->map_lookup;
Filipe Manana7fd01182015-11-13 23:57:17 +000010676 num_items = 3 + map->num_stripes;
10677 free_extent_map(em);
10678
Filipe Manana8eab77f2015-11-13 23:57:16 +000010679 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
Filipe Manana7fd01182015-11-13 23:57:17 +000010680 num_items, 1);
Filipe Manana8eab77f2015-11-13 23:57:16 +000010681}
10682
Josef Bacik47ab2a62014-09-18 11:20:02 -040010683/*
10684 * Process the unused_bgs list and remove any that don't have any allocated
10685 * space inside of them.
10686 */
10687void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10688{
10689 struct btrfs_block_group_cache *block_group;
10690 struct btrfs_space_info *space_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010691 struct btrfs_trans_handle *trans;
10692 int ret = 0;
10693
Josef Bacikafcdd122016-09-02 15:40:02 -040010694 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
Josef Bacik47ab2a62014-09-18 11:20:02 -040010695 return;
10696
10697 spin_lock(&fs_info->unused_bgs_lock);
10698 while (!list_empty(&fs_info->unused_bgs)) {
10699 u64 start, end;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010700 int trimming;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010701
10702 block_group = list_first_entry(&fs_info->unused_bgs,
10703 struct btrfs_block_group_cache,
10704 bg_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010705 list_del_init(&block_group->bg_list);
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010706
10707 space_info = block_group->space_info;
10708
Josef Bacik47ab2a62014-09-18 11:20:02 -040010709 if (ret || btrfs_mixed_space_info(space_info)) {
10710 btrfs_put_block_group(block_group);
10711 continue;
10712 }
10713 spin_unlock(&fs_info->unused_bgs_lock);
10714
Zhao Leid5f2e332015-10-08 18:46:44 +080010715 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +010010716
Josef Bacik47ab2a62014-09-18 11:20:02 -040010717 /* Don't want to race with allocators so take the groups_sem */
10718 down_write(&space_info->groups_sem);
10719 spin_lock(&block_group->lock);
10720 if (block_group->reserved ||
10721 btrfs_block_group_used(&block_group->item) ||
Chris Mason19c4d2f2016-10-10 13:43:31 -070010722 block_group->ro ||
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010723 list_is_singular(&block_group->list)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -040010724 /*
10725 * We want to bail if we made new allocations or have
10726 * outstanding allocations in this block group. We do
10727 * the ro check in case balance is currently acting on
10728 * this block group.
10729 */
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +080010730 trace_btrfs_skip_unused_block_group(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010731 spin_unlock(&block_group->lock);
10732 up_write(&space_info->groups_sem);
10733 goto next;
10734 }
10735 spin_unlock(&block_group->lock);
10736
10737 /* We don't want to force the issue, only flip if it's ok. */
Zhaolei868f4012015-08-05 16:43:27 +080010738 ret = inc_block_group_ro(block_group, 0);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010739 up_write(&space_info->groups_sem);
10740 if (ret < 0) {
10741 ret = 0;
10742 goto next;
10743 }
10744
10745 /*
10746 * Want to do this before we do anything else so we can recover
10747 * properly if we fail to join the transaction.
10748 */
Filipe Manana7fd01182015-11-13 23:57:17 +000010749 trans = btrfs_start_trans_remove_block_group(fs_info,
10750 block_group->key.objectid);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010751 if (IS_ERR(trans)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010752 btrfs_dec_block_group_ro(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010753 ret = PTR_ERR(trans);
10754 goto next;
10755 }
10756
10757 /*
10758 * We could have pending pinned extents for this block group,
10759 * just delete them, we don't care about them anymore.
10760 */
10761 start = block_group->key.objectid;
10762 end = start + block_group->key.offset - 1;
Filipe Mananad4b450c2015-01-29 19:18:25 +000010763 /*
10764 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10765 * btrfs_finish_extent_commit(). If we are at transaction N,
10766 * another task might be running finish_extent_commit() for the
10767 * previous transaction N - 1, and have seen a range belonging
10768 * to the block group in freed_extents[] before we were able to
10769 * clear the whole block group range from freed_extents[]. This
10770 * means that task can lookup for the block group after we
10771 * unpinned it from freed_extents[] and removed it, leading to
10772 * a BUG_ON() at btrfs_unpin_extent_range().
10773 */
10774 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana758eb512014-11-03 14:08:39 +000010775 ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
David Sterba91166212016-04-26 23:54:39 +020010776 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010777 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010778 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010779 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010780 goto end_trans;
10781 }
10782 ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
David Sterba91166212016-04-26 23:54:39 +020010783 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010784 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010785 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010786 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010787 goto end_trans;
10788 }
Filipe Mananad4b450c2015-01-29 19:18:25 +000010789 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010790
10791 /* Reset pinned so btrfs_put_block_group doesn't complain */
Zhao Leic30666d2015-02-25 14:17:20 +080010792 spin_lock(&space_info->lock);
10793 spin_lock(&block_group->lock);
10794
10795 space_info->bytes_pinned -= block_group->pinned;
10796 space_info->bytes_readonly += block_group->pinned;
10797 percpu_counter_add(&space_info->total_bytes_pinned,
10798 -block_group->pinned);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010799 block_group->pinned = 0;
10800
Zhao Leic30666d2015-02-25 14:17:20 +080010801 spin_unlock(&block_group->lock);
10802 spin_unlock(&space_info->lock);
10803
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010804 /* DISCARD can flip during remount */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010805 trimming = btrfs_test_opt(fs_info, DISCARD);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010806
10807 /* Implicit trim during transaction commit. */
10808 if (trimming)
10809 btrfs_get_block_group_trimming(block_group);
10810
Josef Bacik47ab2a62014-09-18 11:20:02 -040010811 /*
10812 * Btrfs_remove_chunk will abort the transaction if things go
10813 * horribly wrong.
10814 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010815 ret = btrfs_remove_chunk(trans, fs_info,
Josef Bacik47ab2a62014-09-18 11:20:02 -040010816 block_group->key.objectid);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010817
10818 if (ret) {
10819 if (trimming)
10820 btrfs_put_block_group_trimming(block_group);
10821 goto end_trans;
10822 }
10823
10824 /*
10825 * If we're not mounted with -odiscard, we can just forget
10826 * about this block group. Otherwise we'll need to wait
10827 * until transaction commit to do the actual discard.
10828 */
10829 if (trimming) {
Filipe Manana348a0012015-11-27 12:16:16 +000010830 spin_lock(&fs_info->unused_bgs_lock);
10831 /*
10832 * A concurrent scrub might have added us to the list
10833 * fs_info->unused_bgs, so use a list_move operation
10834 * to add the block group to the deleted_bgs list.
10835 */
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010836 list_move(&block_group->bg_list,
10837 &trans->transaction->deleted_bgs);
Filipe Manana348a0012015-11-27 12:16:16 +000010838 spin_unlock(&fs_info->unused_bgs_lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010839 btrfs_get_block_group(block_group);
10840 }
Filipe Manana758eb512014-11-03 14:08:39 +000010841end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010842 btrfs_end_transaction(trans);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010843next:
Zhao Leid5f2e332015-10-08 18:46:44 +080010844 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010845 btrfs_put_block_group(block_group);
10846 spin_lock(&fs_info->unused_bgs_lock);
10847 }
10848 spin_unlock(&fs_info->unused_bgs_lock);
10849}
10850
liuboc59021f2011-03-07 02:13:14 +000010851int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
10852{
liubo1aba86d2011-04-08 08:44:37 +000010853 struct btrfs_super_block *disk_super;
10854 u64 features;
10855 u64 flags;
10856 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +000010857 int ret;
10858
David Sterba6c417612011-04-13 15:41:04 +020010859 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +000010860 if (!btrfs_super_root(disk_super))
Dan Carpenter0dc924c52016-01-13 15:21:17 +030010861 return -EINVAL;
liuboc59021f2011-03-07 02:13:14 +000010862
liubo1aba86d2011-04-08 08:44:37 +000010863 features = btrfs_super_incompat_flags(disk_super);
10864 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
10865 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +000010866
liubo1aba86d2011-04-08 08:44:37 +000010867 flags = BTRFS_BLOCK_GROUP_SYSTEM;
Lu Fengqi4ca61682018-05-28 14:30:27 +080010868 ret = create_space_info(fs_info, flags);
liuboc59021f2011-03-07 02:13:14 +000010869 if (ret)
liubo1aba86d2011-04-08 08:44:37 +000010870 goto out;
liuboc59021f2011-03-07 02:13:14 +000010871
liubo1aba86d2011-04-08 08:44:37 +000010872 if (mixed) {
10873 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
Lu Fengqi4ca61682018-05-28 14:30:27 +080010874 ret = create_space_info(fs_info, flags);
liubo1aba86d2011-04-08 08:44:37 +000010875 } else {
10876 flags = BTRFS_BLOCK_GROUP_METADATA;
Lu Fengqi4ca61682018-05-28 14:30:27 +080010877 ret = create_space_info(fs_info, flags);
liubo1aba86d2011-04-08 08:44:37 +000010878 if (ret)
10879 goto out;
10880
10881 flags = BTRFS_BLOCK_GROUP_DATA;
Lu Fengqi4ca61682018-05-28 14:30:27 +080010882 ret = create_space_info(fs_info, flags);
liubo1aba86d2011-04-08 08:44:37 +000010883 }
10884out:
liuboc59021f2011-03-07 02:13:14 +000010885 return ret;
10886}
10887
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010888int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
10889 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +080010890{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010891 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +080010892}
10893
Jeff Mahoney499f3772015-06-15 09:41:17 -040010894/*
10895 * It used to be that old block groups would be left around forever.
10896 * Iterating over them would be enough to trim unused space. Since we
10897 * now automatically remove them, we also need to iterate over unallocated
10898 * space.
10899 *
10900 * We don't want a transaction for this since the discard may take a
10901 * substantial amount of time. We don't require that a transaction be
10902 * running, but we do need to take a running transaction into account
10903 * to ensure that we're not discarding chunks that were released in
10904 * the current transaction.
10905 *
10906 * Holding the chunks lock will prevent other threads from allocating
10907 * or releasing chunks, but it won't prevent a running transaction
10908 * from committing and releasing the memory that the pending chunks
10909 * list head uses. For that, we need to take a reference to the
10910 * transaction.
10911 */
10912static int btrfs_trim_free_extents(struct btrfs_device *device,
10913 u64 minlen, u64 *trimmed)
10914{
10915 u64 start = 0, len = 0;
10916 int ret;
10917
10918 *trimmed = 0;
10919
10920 /* Not writeable = nothing to do. */
Anand Jainebbede42017-12-04 12:54:52 +080010921 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoney499f3772015-06-15 09:41:17 -040010922 return 0;
10923
10924 /* No free space = nothing to do. */
10925 if (device->total_bytes <= device->bytes_used)
10926 return 0;
10927
10928 ret = 0;
10929
10930 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -040010931 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010932 struct btrfs_transaction *trans;
10933 u64 bytes;
10934
10935 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
10936 if (ret)
10937 return ret;
10938
10939 down_read(&fs_info->commit_root_sem);
10940
10941 spin_lock(&fs_info->trans_lock);
10942 trans = fs_info->running_transaction;
10943 if (trans)
Elena Reshetova9b64f572017-03-03 10:55:11 +020010944 refcount_inc(&trans->use_count);
Jeff Mahoney499f3772015-06-15 09:41:17 -040010945 spin_unlock(&fs_info->trans_lock);
10946
10947 ret = find_free_dev_extent_start(trans, device, minlen, start,
10948 &start, &len);
10949 if (trans)
10950 btrfs_put_transaction(trans);
10951
10952 if (ret) {
10953 up_read(&fs_info->commit_root_sem);
10954 mutex_unlock(&fs_info->chunk_mutex);
10955 if (ret == -ENOSPC)
10956 ret = 0;
10957 break;
10958 }
10959
10960 ret = btrfs_issue_discard(device->bdev, start, len, &bytes);
10961 up_read(&fs_info->commit_root_sem);
10962 mutex_unlock(&fs_info->chunk_mutex);
10963
10964 if (ret)
10965 break;
10966
10967 start += len;
10968 *trimmed += bytes;
10969
10970 if (fatal_signal_pending(current)) {
10971 ret = -ERESTARTSYS;
10972 break;
10973 }
10974
10975 cond_resched();
10976 }
10977
10978 return ret;
10979}
10980
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010981int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +000010982{
Li Dongyangf7039b12011-03-24 10:24:28 +000010983 struct btrfs_block_group_cache *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010984 struct btrfs_device *device;
10985 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +000010986 u64 group_trimmed;
10987 u64 start;
10988 u64 end;
10989 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +080010990 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +000010991 int ret = 0;
10992
Liu Bo2cac13e2012-02-09 18:17:41 +080010993 /*
10994 * try to trim all FS space, our block group may start from non-zero.
10995 */
10996 if (range->len == total_bytes)
10997 cache = btrfs_lookup_first_block_group(fs_info, range->start);
10998 else
10999 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +000011000
11001 while (cache) {
11002 if (cache->key.objectid >= (range->start + range->len)) {
11003 btrfs_put_block_group(cache);
11004 break;
11005 }
11006
11007 start = max(range->start, cache->key.objectid);
11008 end = min(range->start + range->len,
11009 cache->key.objectid + cache->key.offset);
11010
11011 if (end - start >= range->minlen) {
11012 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +000011013 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -040011014 if (ret) {
11015 btrfs_put_block_group(cache);
11016 break;
11017 }
11018 ret = wait_block_group_cache_done(cache);
11019 if (ret) {
11020 btrfs_put_block_group(cache);
11021 break;
11022 }
Li Dongyangf7039b12011-03-24 10:24:28 +000011023 }
11024 ret = btrfs_trim_block_group(cache,
11025 &group_trimmed,
11026 start,
11027 end,
11028 range->minlen);
11029
11030 trimmed += group_trimmed;
11031 if (ret) {
11032 btrfs_put_block_group(cache);
11033 break;
11034 }
11035 }
11036
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040011037 cache = next_block_group(fs_info, cache);
Li Dongyangf7039b12011-03-24 10:24:28 +000011038 }
11039
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011040 mutex_lock(&fs_info->fs_devices->device_list_mutex);
11041 devices = &fs_info->fs_devices->alloc_list;
Jeff Mahoney499f3772015-06-15 09:41:17 -040011042 list_for_each_entry(device, devices, dev_alloc_list) {
11043 ret = btrfs_trim_free_extents(device, range->minlen,
11044 &group_trimmed);
11045 if (ret)
11046 break;
11047
11048 trimmed += group_trimmed;
11049 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011050 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -040011051
Li Dongyangf7039b12011-03-24 10:24:28 +000011052 range->len = trimmed;
11053 return ret;
11054}
Miao Xie8257b2d2014-03-06 13:38:19 +080011055
11056/*
David Sterbaea14b57f2017-06-22 02:19:11 +020011057 * btrfs_{start,end}_write_no_snapshotting() are similar to
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011058 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
11059 * data into the page cache through nocow before the subvolume is snapshoted,
11060 * but flush the data into disk after the snapshot creation, or to prevent
David Sterbaea14b57f2017-06-22 02:19:11 +020011061 * operations while snapshotting is ongoing and that cause the snapshot to be
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011062 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +080011063 */
David Sterbaea14b57f2017-06-22 02:19:11 +020011064void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011065{
11066 percpu_counter_dec(&root->subv_writers->counter);
David Sterba093258e2018-02-26 16:15:17 +010011067 cond_wake_up(&root->subv_writers->wait);
Miao Xie8257b2d2014-03-06 13:38:19 +080011068}
11069
David Sterbaea14b57f2017-06-22 02:19:11 +020011070int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011071{
David Sterbaea14b57f2017-06-22 02:19:11 +020011072 if (atomic_read(&root->will_be_snapshotted))
Miao Xie8257b2d2014-03-06 13:38:19 +080011073 return 0;
11074
11075 percpu_counter_inc(&root->subv_writers->counter);
11076 /*
11077 * Make sure counter is updated before we check for snapshot creation.
11078 */
11079 smp_mb();
David Sterbaea14b57f2017-06-22 02:19:11 +020011080 if (atomic_read(&root->will_be_snapshotted)) {
11081 btrfs_end_write_no_snapshotting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +080011082 return 0;
11083 }
11084 return 1;
11085}
Zhao Lei0bc19f902016-01-06 18:56:36 +080011086
Zhao Lei0bc19f902016-01-06 18:56:36 +080011087void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
11088{
11089 while (true) {
11090 int ret;
11091
David Sterbaea14b57f2017-06-22 02:19:11 +020011092 ret = btrfs_start_write_no_snapshotting(root);
Zhao Lei0bc19f902016-01-06 18:56:36 +080011093 if (ret)
11094 break;
Peter Zijlstra46259562018-03-15 11:43:08 +010011095 wait_var_event(&root->will_be_snapshotted,
11096 !atomic_read(&root->will_be_snapshotted));
Zhao Lei0bc19f902016-01-06 18:56:36 +080011097 }
11098}