blob: 102911286497e8c87231b79b0a37be976cde72bf [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, Zhengb742bb822010-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001599 struct btrfs_path *path,
1600 struct btrfs_extent_inline_ref **ref_ret,
1601 u64 bytenr, u64 num_bytes,
1602 u64 parent, u64 root_objectid,
1603 u64 owner, u64 offset, int insert)
1604{
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001605 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001871 ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
1872 num_bytes, parent, root_objectid,
1873 owner, offset, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001874 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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001895void update_inline_extent_backref(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001896 struct btrfs_extent_inline_ref *iref,
1897 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001898 struct btrfs_delayed_extent_op *extent_op,
1899 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001900{
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001901 struct extent_buffer *leaf = path->nodes[0];
1902 struct btrfs_fs_info *fs_info = leaf->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001903 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
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001913 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1914 refs = btrfs_extent_refs(leaf, ei);
1915 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1916 refs += refs_to_mod;
1917 btrfs_set_extent_refs(leaf, ei, refs);
1918 if (extent_op)
1919 __run_delayed_extent_op(extent_op, leaf, ei);
1920
Liu Bo3de28d52017-08-18 15:15:19 -06001921 /*
1922 * If type is invalid, we should have bailed out after
1923 * lookup_inline_extent_backref().
1924 */
1925 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1926 ASSERT(type != BTRFS_REF_TYPE_INVALID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001927
1928 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1929 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1930 refs = btrfs_extent_data_ref_count(leaf, dref);
1931 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1932 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1933 refs = btrfs_shared_data_ref_count(leaf, sref);
1934 } else {
1935 refs = 1;
1936 BUG_ON(refs_to_mod != -1);
1937 }
1938
1939 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1940 refs += refs_to_mod;
1941
1942 if (refs > 0) {
1943 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1944 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1945 else
1946 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1947 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001948 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001949 size = btrfs_extent_inline_ref_size(type);
1950 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1951 ptr = (unsigned long)iref;
1952 end = (unsigned long)ei + item_size;
1953 if (ptr + size < end)
1954 memmove_extent_buffer(leaf, ptr, ptr + size,
1955 end - ptr - size);
1956 item_size -= size;
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001957 btrfs_truncate_item(fs_info, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001958 }
1959 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001960}
1961
1962static noinline_for_stack
1963int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001964 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001965 struct btrfs_path *path,
1966 u64 bytenr, u64 num_bytes, u64 parent,
1967 u64 root_objectid, u64 owner,
1968 u64 offset, int refs_to_add,
1969 struct btrfs_delayed_extent_op *extent_op)
1970{
1971 struct btrfs_extent_inline_ref *iref;
1972 int ret;
1973
Nikolay Borisov867cc1f2018-06-20 15:48:48 +03001974 ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
1975 num_bytes, parent, root_objectid,
1976 owner, offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001977 if (ret == 0) {
1978 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Nikolay Borisov61a18f12018-06-20 15:48:49 +03001979 update_inline_extent_backref(path, iref, refs_to_add,
1980 extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001981 } else if (ret == -ENOENT) {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001982 setup_inline_extent_backref(fs_info, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001983 root_objectid, owner, offset,
1984 refs_to_add, extent_op);
1985 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001986 }
1987 return ret;
1988}
1989
1990static int insert_extent_backref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001991 struct btrfs_path *path,
1992 u64 bytenr, u64 parent, u64 root_objectid,
1993 u64 owner, u64 offset, int refs_to_add)
1994{
1995 int ret;
1996 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1997 BUG_ON(refs_to_add != 1);
Nikolay Borisov10728402018-06-20 15:48:43 +03001998 ret = insert_tree_block_ref(trans, path, bytenr, parent,
1999 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002000 } else {
Nikolay Borisov62b895a2018-06-20 15:48:44 +03002001 ret = insert_extent_data_ref(trans, path, bytenr, parent,
2002 root_objectid, owner, offset,
2003 refs_to_add);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002004 }
2005 return ret;
2006}
2007
2008static int remove_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05002009 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002010 struct btrfs_path *path,
2011 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07002012 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002013{
Jeff Mahoney143bede2012-03-01 14:56:26 +01002014 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002015
2016 BUG_ON(!is_data && refs_to_drop != 1);
2017 if (iref) {
Nikolay Borisov61a18f12018-06-20 15:48:49 +03002018 update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
2019 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002020 } else if (is_data) {
Nikolay Borisove9f62902018-06-20 15:48:46 +03002021 ret = remove_extent_data_ref(trans, path, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07002022 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002023 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07002024 *last_ref = 1;
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05002025 ret = btrfs_del_item(trans, fs_info->extent_root, path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002026 }
2027 return ret;
2028}
2029
Jeff Mahoney86557862015-06-15 09:41:16 -04002030#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002031static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
2032 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05002033{
Jeff Mahoney86557862015-06-15 09:41:16 -04002034 int j, ret = 0;
2035 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002036 u64 aligned_start = ALIGN(start, 1 << 9);
2037
2038 if (WARN_ON(start != aligned_start)) {
2039 len -= aligned_start - start;
2040 len = round_down(len, 1 << 9);
2041 start = aligned_start;
2042 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002043
2044 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04002045
2046 if (!len)
2047 return 0;
2048
2049 end = start + len;
2050 bytes_left = len;
2051
2052 /* Skip any superblocks on this device. */
2053 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
2054 u64 sb_start = btrfs_sb_offset(j);
2055 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
2056 u64 size = sb_start - start;
2057
2058 if (!in_range(sb_start, start, bytes_left) &&
2059 !in_range(sb_end, start, bytes_left) &&
2060 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
2061 continue;
2062
2063 /*
2064 * Superblock spans beginning of range. Adjust start and
2065 * try again.
2066 */
2067 if (sb_start <= start) {
2068 start += sb_end - start;
2069 if (start > end) {
2070 bytes_left = 0;
2071 break;
2072 }
2073 bytes_left = end - start;
2074 continue;
2075 }
2076
2077 if (size) {
2078 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
2079 GFP_NOFS, 0);
2080 if (!ret)
2081 *discarded_bytes += size;
2082 else if (ret != -EOPNOTSUPP)
2083 return ret;
2084 }
2085
2086 start = sb_end;
2087 if (start > end) {
2088 bytes_left = 0;
2089 break;
2090 }
2091 bytes_left = end - start;
2092 }
2093
2094 if (bytes_left) {
2095 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002096 GFP_NOFS, 0);
2097 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04002098 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002099 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002100 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05002101}
Chris Mason15916de2008-11-19 21:17:22 -05002102
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002103int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00002104 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05002105{
Liu Hui1f3c79a2009-01-05 15:57:51 -05002106 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00002107 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02002108 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002109
Christoph Hellwige244a0a2009-10-14 09:24:59 -04002110
Filipe Manana29992412016-05-27 17:42:05 +01002111 /*
2112 * Avoid races with device replace and make sure our bbio has devices
2113 * associated to its stripes that don't go away while we are discarding.
2114 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002115 btrfs_bio_counter_inc_blocked(fs_info);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002116 /* Tell the block device(s) that the sectors can be discarded */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002117 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
2118 &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002119 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05002120 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02002121 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002122 int i;
2123
Liu Hui1f3c79a2009-01-05 15:57:51 -05002124
Jan Schmidta1d3c472011-08-04 17:15:33 +02002125 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002126 u64 bytes;
Anand Jain38b5f682017-11-29 18:53:43 +08002127 struct request_queue *req_q;
2128
Filipe Manana627e0872018-01-30 18:40:22 +00002129 if (!stripe->dev->bdev) {
2130 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
2131 continue;
2132 }
Anand Jain38b5f682017-11-29 18:53:43 +08002133 req_q = bdev_get_queue(stripe->dev->bdev);
2134 if (!blk_queue_discard(req_q))
Josef Bacikd5e20032011-08-04 14:52:27 +00002135 continue;
2136
Li Dongyang5378e602011-03-24 10:24:27 +00002137 ret = btrfs_issue_discard(stripe->dev->bdev,
2138 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002139 stripe->length,
2140 &bytes);
Li Dongyang5378e602011-03-24 10:24:27 +00002141 if (!ret)
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002142 discarded_bytes += bytes;
Li Dongyang5378e602011-03-24 10:24:27 +00002143 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002144 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00002145
2146 /*
2147 * Just in case we get back EOPNOTSUPP for some reason,
2148 * just ignore the return value so we don't screw up
2149 * people calling discard_extent.
2150 */
2151 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002152 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08002153 btrfs_put_bbio(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002154 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002155 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00002156
2157 if (actual_bytes)
2158 *actual_bytes = discarded_bytes;
2159
Liu Hui1f3c79a2009-01-05 15:57:51 -05002160
David Woodhouse53b381b2013-01-29 18:40:14 -05002161 if (ret == -EOPNOTSUPP)
2162 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002163 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002164}
2165
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002166/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002167int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002168 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002169 u64 bytenr, u64 num_bytes, u64 parent,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002170 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04002171{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002172 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07002173 int old_ref_mod, new_ref_mod;
Zheng Yan31840ae2008-09-23 13:14:14 -04002174 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002175
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002176 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
2177 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04002178
Josef Bacikfd708b82017-09-29 15:43:50 -04002179 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent, root_objectid,
2180 owner, offset, BTRFS_ADD_DELAYED_REF);
2181
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002182 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002183 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07002184 num_bytes, parent,
2185 root_objectid, (int)owner,
2186 BTRFS_ADD_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07002187 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002188 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002189 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07002190 num_bytes, parent,
2191 root_objectid, owner, offset,
Omar Sandovald7eae342017-06-06 16:45:31 -07002192 0, BTRFS_ADD_DELAYED_REF,
2193 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002194 }
Omar Sandovald7eae342017-06-06 16:45:31 -07002195
Nikolay Borisov29d2b842018-03-30 12:58:47 +03002196 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0) {
2197 bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
2198
2199 add_pinned_bytes(fs_info, -num_bytes, metadata, root_objectid);
2200 }
Omar Sandovald7eae342017-06-06 16:45:31 -07002201
Zheng Yan31840ae2008-09-23 13:14:14 -04002202 return ret;
2203}
2204
Nikolay Borisovbd3c6852018-06-18 14:59:25 +03002205/*
2206 * __btrfs_inc_extent_ref - insert backreference for a given extent
2207 *
2208 * @trans: Handle of transaction
2209 *
2210 * @node: The delayed ref node used to get the bytenr/length for
2211 * extent whose references are incremented.
2212 *
2213 * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
2214 * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
2215 * bytenr of the parent block. Since new extents are always
2216 * created with indirect references, this will only be the case
2217 * when relocating a shared extent. In that case, root_objectid
2218 * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
2219 * be 0
2220 *
2221 * @root_objectid: The id of the root where this modification has originated,
2222 * this can be either one of the well-known metadata trees or
2223 * the subvolume id which references this extent.
2224 *
2225 * @owner: For data extents it is the inode number of the owning file.
2226 * For metadata extents this parameter holds the level in the
2227 * tree of the extent.
2228 *
2229 * @offset: For metadata extents the offset is ignored and is currently
2230 * always passed as 0. For data extents it is the fileoffset
2231 * this extent belongs to.
2232 *
2233 * @refs_to_add Number of references to add
2234 *
2235 * @extent_op Pointer to a structure, holding information necessary when
2236 * updating a tree block's flags
2237 *
2238 */
Chris Mason925baed2008-06-25 16:01:30 -04002239static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002240 struct btrfs_fs_info *fs_info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002241 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002242 u64 parent, u64 root_objectid,
2243 u64 owner, u64 offset, int refs_to_add,
2244 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04002245{
Chris Mason5caf2a02007-04-02 11:20:42 -04002246 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002247 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04002248 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07002249 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002250 u64 bytenr = node->bytenr;
2251 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002252 u64 refs;
2253 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05002254
Chris Mason5caf2a02007-04-02 11:20:42 -04002255 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002256 if (!path)
2257 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04002258
David Sterbae4058b52015-11-27 16:31:35 +01002259 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002260 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002261 /* this will setup the path even if it fails to insert the back ref */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05002262 ret = insert_inline_extent_backref(trans, fs_info, path, bytenr,
2263 num_bytes, parent, root_objectid,
2264 owner, offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002265 refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08002266 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002267 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07002268
2269 /*
2270 * Ok we had -EAGAIN which means we didn't have space to insert and
2271 * inline extent ref, so just update the reference count and add a
2272 * normal backref.
2273 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002274 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07002275 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002276 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2277 refs = btrfs_extent_refs(leaf, item);
2278 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2279 if (extent_op)
2280 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002281
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002282 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002283 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002284
David Sterbae4058b52015-11-27 16:31:35 +01002285 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002286 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04002287 /* now insert the actual backref */
Nikolay Borisov37593412018-06-20 15:48:45 +03002288 ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid,
2289 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002290 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04002291 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002292out:
Chris Mason74493f72007-12-11 09:25:06 -05002293 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08002294 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002295}
2296
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002297static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002298 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002299 struct btrfs_delayed_ref_node *node,
2300 struct btrfs_delayed_extent_op *extent_op,
2301 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002302{
Chris Mason56bec292009-03-13 10:10:06 -04002303 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002304 struct btrfs_delayed_data_ref *ref;
2305 struct btrfs_key ins;
2306 u64 parent = 0;
2307 u64 ref_root = 0;
2308 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002309
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002310 ins.objectid = node->bytenr;
2311 ins.offset = node->num_bytes;
2312 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002313
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002314 ref = btrfs_delayed_node_to_data_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002315 trace_run_delayed_data_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002316
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002317 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2318 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002319 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002320
2321 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002322 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002323 flags |= extent_op->flags_to_set;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002324 ret = alloc_reserved_file_extent(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002325 parent, ref_root, flags,
2326 ref->objectid, ref->offset,
2327 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002328 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002329 ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002330 ref_root, ref->objectid,
2331 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002332 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002333 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002334 ret = __btrfs_free_extent(trans, fs_info, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002335 ref_root, ref->objectid,
2336 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002337 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002338 } else {
2339 BUG();
2340 }
Chris Mason56bec292009-03-13 10:10:06 -04002341 return ret;
2342}
2343
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002344static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2345 struct extent_buffer *leaf,
2346 struct btrfs_extent_item *ei)
2347{
2348 u64 flags = btrfs_extent_flags(leaf, ei);
2349 if (extent_op->update_flags) {
2350 flags |= extent_op->flags_to_set;
2351 btrfs_set_extent_flags(leaf, ei, flags);
2352 }
2353
2354 if (extent_op->update_key) {
2355 struct btrfs_tree_block_info *bi;
2356 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2357 bi = (struct btrfs_tree_block_info *)(ei + 1);
2358 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2359 }
2360}
2361
2362static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002363 struct btrfs_fs_info *fs_info,
Josef Bacikd2788502017-09-29 15:43:57 -04002364 struct btrfs_delayed_ref_head *head,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002365 struct btrfs_delayed_extent_op *extent_op)
2366{
2367 struct btrfs_key key;
2368 struct btrfs_path *path;
2369 struct btrfs_extent_item *ei;
2370 struct extent_buffer *leaf;
2371 u32 item_size;
2372 int ret;
2373 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002374 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002375
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002376 if (trans->aborted)
2377 return 0;
2378
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002379 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05002380 metadata = 0;
2381
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002382 path = btrfs_alloc_path();
2383 if (!path)
2384 return -ENOMEM;
2385
Josef Bacikd2788502017-09-29 15:43:57 -04002386 key.objectid = head->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002387
Josef Bacik3173a182013-03-07 14:22:04 -05002388 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002389 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002390 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002391 } else {
2392 key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacikd2788502017-09-29 15:43:57 -04002393 key.offset = head->num_bytes;
Josef Bacik3173a182013-03-07 14:22:04 -05002394 }
2395
2396again:
David Sterbae4058b52015-11-27 16:31:35 +01002397 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002398 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002399 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002400 if (ret < 0) {
2401 err = ret;
2402 goto out;
2403 }
2404 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002405 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002406 if (path->slots[0] > 0) {
2407 path->slots[0]--;
2408 btrfs_item_key_to_cpu(path->nodes[0], &key,
2409 path->slots[0]);
Josef Bacikd2788502017-09-29 15:43:57 -04002410 if (key.objectid == head->bytenr &&
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002411 key.type == BTRFS_EXTENT_ITEM_KEY &&
Josef Bacikd2788502017-09-29 15:43:57 -04002412 key.offset == head->num_bytes)
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002413 ret = 0;
2414 }
2415 if (ret > 0) {
2416 btrfs_release_path(path);
2417 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002418
Josef Bacikd2788502017-09-29 15:43:57 -04002419 key.objectid = head->bytenr;
2420 key.offset = head->num_bytes;
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002421 key.type = BTRFS_EXTENT_ITEM_KEY;
2422 goto again;
2423 }
2424 } else {
2425 err = -EIO;
2426 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002427 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002428 }
2429
2430 leaf = path->nodes[0];
2431 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2432#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2433 if (item_size < sizeof(*ei)) {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05002434 ret = convert_extent_item_v0(trans, fs_info, path, (u64)-1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002435 if (ret < 0) {
2436 err = ret;
2437 goto out;
2438 }
2439 leaf = path->nodes[0];
2440 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2441 }
2442#endif
2443 BUG_ON(item_size < sizeof(*ei));
2444 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2445 __run_delayed_extent_op(extent_op, leaf, ei);
2446
2447 btrfs_mark_buffer_dirty(leaf);
2448out:
2449 btrfs_free_path(path);
2450 return err;
2451}
2452
2453static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002454 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002455 struct btrfs_delayed_ref_node *node,
2456 struct btrfs_delayed_extent_op *extent_op,
2457 int insert_reserved)
2458{
2459 int ret = 0;
2460 struct btrfs_delayed_tree_ref *ref;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002461 u64 parent = 0;
2462 u64 ref_root = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002463
2464 ref = btrfs_delayed_node_to_tree_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002465 trace_run_delayed_tree_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002466
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002467 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2468 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002469 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002470
Liu Bo02794222016-09-14 19:19:05 -07002471 if (node->ref_mod != 1) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002472 btrfs_err(fs_info,
Liu Bo02794222016-09-14 19:19:05 -07002473 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
2474 node->bytenr, node->ref_mod, node->action, ref_root,
2475 parent);
2476 return -EIO;
2477 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002478 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002479 BUG_ON(!extent_op || !extent_op->update_flags);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03002480 ret = alloc_reserved_tree_block(trans, node, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002481 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002482 ret = __btrfs_inc_extent_ref(trans, fs_info, node,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002483 parent, ref_root,
2484 ref->level, 0, 1,
Josef Bacikfcebe452014-05-13 17:30:47 -07002485 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002486 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002487 ret = __btrfs_free_extent(trans, fs_info, node,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002488 parent, ref_root,
2489 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002490 } else {
2491 BUG();
2492 }
2493 return ret;
2494}
2495
Chris Mason56bec292009-03-13 10:10:06 -04002496/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002497static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002498 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002499 struct btrfs_delayed_ref_node *node,
2500 struct btrfs_delayed_extent_op *extent_op,
2501 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002502{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002503 int ret = 0;
2504
Josef Bacik857cc2f2013-10-07 15:21:08 -04002505 if (trans->aborted) {
2506 if (insert_reserved)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002507 btrfs_pin_extent(fs_info, node->bytenr,
Josef Bacik857cc2f2013-10-07 15:21:08 -04002508 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002509 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002510 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002511
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002512 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2513 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002514 ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002515 insert_reserved);
2516 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2517 node->type == BTRFS_SHARED_DATA_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002518 ret = run_delayed_data_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002519 insert_reserved);
2520 else
2521 BUG();
2522 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002523}
2524
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002525static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04002526select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002527{
Filipe Mananacffc3372015-07-09 13:13:44 +01002528 struct btrfs_delayed_ref_node *ref;
2529
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002530 if (RB_EMPTY_ROOT(&head->ref_tree))
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002531 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002532
Filipe Mananacffc3372015-07-09 13:13:44 +01002533 /*
2534 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2535 * This is to prevent a ref count from going down to zero, which deletes
2536 * the extent item from the extent tree, when there still are references
2537 * to add, which would fail because they would not find the extent item.
2538 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002539 if (!list_empty(&head->ref_add_list))
2540 return list_first_entry(&head->ref_add_list,
2541 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01002542
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002543 ref = rb_entry(rb_first(&head->ref_tree),
2544 struct btrfs_delayed_ref_node, ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002545 ASSERT(list_empty(&ref->add_list));
2546 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04002547}
2548
Josef Bacik2eadaa22017-09-29 15:43:52 -04002549static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
2550 struct btrfs_delayed_ref_head *head)
2551{
2552 spin_lock(&delayed_refs->lock);
2553 head->processing = 0;
2554 delayed_refs->num_heads_ready++;
2555 spin_unlock(&delayed_refs->lock);
2556 btrfs_delayed_ref_unlock(head);
2557}
2558
Josef Bacikb00e6252017-09-29 15:43:53 -04002559static int cleanup_extent_op(struct btrfs_trans_handle *trans,
2560 struct btrfs_fs_info *fs_info,
2561 struct btrfs_delayed_ref_head *head)
2562{
2563 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
2564 int ret;
2565
2566 if (!extent_op)
2567 return 0;
2568 head->extent_op = NULL;
2569 if (head->must_insert_reserved) {
2570 btrfs_free_delayed_extent_op(extent_op);
2571 return 0;
2572 }
2573 spin_unlock(&head->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002574 ret = run_delayed_extent_op(trans, fs_info, head, extent_op);
Josef Bacikb00e6252017-09-29 15:43:53 -04002575 btrfs_free_delayed_extent_op(extent_op);
2576 return ret ? ret : 1;
2577}
2578
Josef Bacik194ab0b2017-09-29 15:43:54 -04002579static int cleanup_ref_head(struct btrfs_trans_handle *trans,
2580 struct btrfs_fs_info *fs_info,
2581 struct btrfs_delayed_ref_head *head)
2582{
2583 struct btrfs_delayed_ref_root *delayed_refs;
2584 int ret;
2585
2586 delayed_refs = &trans->transaction->delayed_refs;
2587
2588 ret = cleanup_extent_op(trans, fs_info, head);
2589 if (ret < 0) {
2590 unselect_delayed_ref_head(delayed_refs, head);
2591 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
2592 return ret;
2593 } else if (ret) {
2594 return ret;
2595 }
2596
2597 /*
2598 * Need to drop our head ref lock and re-acquire the delayed ref lock
2599 * and then re-check to make sure nobody got added.
2600 */
2601 spin_unlock(&head->lock);
2602 spin_lock(&delayed_refs->lock);
2603 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002604 if (!RB_EMPTY_ROOT(&head->ref_tree) || head->extent_op) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04002605 spin_unlock(&head->lock);
2606 spin_unlock(&delayed_refs->lock);
2607 return 1;
2608 }
Josef Bacik194ab0b2017-09-29 15:43:54 -04002609 delayed_refs->num_heads--;
2610 rb_erase(&head->href_node, &delayed_refs->href_root);
Josef Bacikd2788502017-09-29 15:43:57 -04002611 RB_CLEAR_NODE(&head->href_node);
Josef Bacikc1103f72017-09-29 15:43:56 -04002612 spin_unlock(&head->lock);
Nikolay Borisov1e7a1422018-04-11 11:21:18 +03002613 spin_unlock(&delayed_refs->lock);
Josef Bacikc1103f72017-09-29 15:43:56 -04002614 atomic_dec(&delayed_refs->num_entries);
2615
Josef Bacikd2788502017-09-29 15:43:57 -04002616 trace_run_delayed_ref_head(fs_info, head, 0);
Josef Bacikc1103f72017-09-29 15:43:56 -04002617
2618 if (head->total_ref_mod < 0) {
Nikolay Borisov5e388e92018-04-18 09:41:54 +03002619 struct btrfs_space_info *space_info;
2620 u64 flags;
Josef Bacikc1103f72017-09-29 15:43:56 -04002621
Nikolay Borisov5e388e92018-04-18 09:41:54 +03002622 if (head->is_data)
2623 flags = BTRFS_BLOCK_GROUP_DATA;
2624 else if (head->is_system)
2625 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2626 else
2627 flags = BTRFS_BLOCK_GROUP_METADATA;
2628 space_info = __find_space_info(fs_info, flags);
2629 ASSERT(space_info);
2630 percpu_counter_add(&space_info->total_bytes_pinned,
Josef Bacikd2788502017-09-29 15:43:57 -04002631 -head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04002632
2633 if (head->is_data) {
2634 spin_lock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002635 delayed_refs->pending_csums -= head->num_bytes;
Josef Bacikc1103f72017-09-29 15:43:56 -04002636 spin_unlock(&delayed_refs->lock);
2637 }
2638 }
2639
2640 if (head->must_insert_reserved) {
Josef Bacikd2788502017-09-29 15:43:57 -04002641 btrfs_pin_extent(fs_info, head->bytenr,
2642 head->num_bytes, 1);
Josef Bacikc1103f72017-09-29 15:43:56 -04002643 if (head->is_data) {
Josef Bacikd2788502017-09-29 15:43:57 -04002644 ret = btrfs_del_csums(trans, fs_info, head->bytenr,
2645 head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04002646 }
2647 }
2648
2649 /* Also free its reserved qgroup space */
2650 btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
2651 head->qgroup_reserved);
2652 btrfs_delayed_ref_unlock(head);
Josef Bacikd2788502017-09-29 15:43:57 -04002653 btrfs_put_delayed_ref_head(head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002654 return 0;
2655}
2656
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002657/*
2658 * Returns 0 on success or if called with an already aborted transaction.
2659 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2660 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002661static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacikd7df2c72014-01-23 09:21:38 -05002662 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002663{
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02002664 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002665 struct btrfs_delayed_ref_root *delayed_refs;
2666 struct btrfs_delayed_ref_node *ref;
2667 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002668 struct btrfs_delayed_extent_op *extent_op;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002669 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002670 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002671 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002672 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002673 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002674
2675 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002676 while (1) {
2677 if (!locked_ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002678 if (count >= nr)
Chris Mason56bec292009-03-13 10:10:06 -04002679 break;
Chris Mason56bec292009-03-13 10:10:06 -04002680
Josef Bacikd7df2c72014-01-23 09:21:38 -05002681 spin_lock(&delayed_refs->lock);
2682 locked_ref = btrfs_select_ref_head(trans);
2683 if (!locked_ref) {
2684 spin_unlock(&delayed_refs->lock);
2685 break;
2686 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002687
2688 /* grab the lock that says we are going to process
2689 * all the refs for this head */
2690 ret = btrfs_delayed_ref_lock(trans, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002691 spin_unlock(&delayed_refs->lock);
Chris Masonc3e69d52009-03-13 10:17:05 -04002692 /*
2693 * we may have dropped the spin lock to get the head
2694 * mutex lock, and that might have given someone else
2695 * time to free the head. If that's true, it has been
2696 * removed from our list and we can move on.
2697 */
2698 if (ret == -EAGAIN) {
2699 locked_ref = NULL;
2700 count++;
2701 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002702 }
2703 }
2704
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002705 /*
2706 * We need to try and merge add/drops of the same ref since we
2707 * can run into issues with relocate dropping the implicit ref
2708 * and then it being added back again before the drop can
2709 * finish. If we merged anything we need to re-loop so we can
2710 * get a good ref.
2711 * Or we can get node references of the same type that weren't
2712 * merged when created due to bumps in the tree mod seq, and
2713 * we need to merge them to prevent adding an inline extent
2714 * backref before dropping it (triggering a BUG_ON at
2715 * insert_inline_extent_backref()).
2716 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002717 spin_lock(&locked_ref->lock);
Nikolay Borisovbe97f132018-04-19 11:06:39 +03002718 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002719
Arne Jansend1270cd2011-09-13 15:16:43 +02002720 ref = select_delayed_ref(locked_ref);
2721
2722 if (ref && ref->seq &&
Nikolay Borisov41d0bd32018-04-04 15:57:42 +03002723 btrfs_check_delayed_seq(fs_info, ref->seq)) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002724 spin_unlock(&locked_ref->lock);
Josef Bacik2eadaa22017-09-29 15:43:52 -04002725 unselect_delayed_ref_head(delayed_refs, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002726 locked_ref = NULL;
Arne Jansend1270cd2011-09-13 15:16:43 +02002727 cond_resched();
Josef Bacik27a377d2014-02-07 13:57:59 -05002728 count++;
Arne Jansend1270cd2011-09-13 15:16:43 +02002729 continue;
2730 }
2731
Josef Bacikc1103f72017-09-29 15:43:56 -04002732 /*
2733 * We're done processing refs in this ref_head, clean everything
2734 * up and move on to the next ref_head.
2735 */
Chris Mason56bec292009-03-13 10:10:06 -04002736 if (!ref) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04002737 ret = cleanup_ref_head(trans, fs_info, locked_ref);
2738 if (ret > 0 ) {
Josef Bacikb00e6252017-09-29 15:43:53 -04002739 /* We dropped our lock, we need to loop. */
2740 ret = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002741 continue;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002742 } else if (ret) {
2743 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002744 }
Josef Bacikc1103f72017-09-29 15:43:56 -04002745 locked_ref = NULL;
2746 count++;
2747 continue;
2748 }
Chris Mason56bec292009-03-13 10:10:06 -04002749
Josef Bacikc1103f72017-09-29 15:43:56 -04002750 actual_count++;
2751 ref->in_tree = 0;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04002752 rb_erase(&ref->ref_node, &locked_ref->ref_tree);
2753 RB_CLEAR_NODE(&ref->ref_node);
Josef Bacikc1103f72017-09-29 15:43:56 -04002754 if (!list_empty(&ref->add_list))
2755 list_del(&ref->add_list);
2756 /*
2757 * When we play the delayed ref, also correct the ref_mod on
2758 * head
2759 */
2760 switch (ref->action) {
2761 case BTRFS_ADD_DELAYED_REF:
2762 case BTRFS_ADD_DELAYED_EXTENT:
Josef Bacikd2788502017-09-29 15:43:57 -04002763 locked_ref->ref_mod -= ref->ref_mod;
Josef Bacikc1103f72017-09-29 15:43:56 -04002764 break;
2765 case BTRFS_DROP_DELAYED_REF:
Josef Bacikd2788502017-09-29 15:43:57 -04002766 locked_ref->ref_mod += ref->ref_mod;
Josef Bacikc1103f72017-09-29 15:43:56 -04002767 break;
2768 default:
2769 WARN_ON(1);
Arne Jansen22cd2e72012-08-09 00:16:53 -06002770 }
Josef Bacik1ce7a5e2017-09-29 15:43:55 -04002771 atomic_dec(&delayed_refs->num_entries);
2772
Josef Bacikb00e6252017-09-29 15:43:53 -04002773 /*
2774 * Record the must-insert_reserved flag before we drop the spin
2775 * lock.
2776 */
2777 must_insert_reserved = locked_ref->must_insert_reserved;
2778 locked_ref->must_insert_reserved = 0;
2779
2780 extent_op = locked_ref->extent_op;
2781 locked_ref->extent_op = NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002782 spin_unlock(&locked_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002783
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002784 ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002785 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002786
Miao Xie78a61842012-11-21 02:21:28 +00002787 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002788 if (ret) {
Josef Bacik2eadaa22017-09-29 15:43:52 -04002789 unselect_delayed_ref_head(delayed_refs, locked_ref);
Miao Xie093486c2012-12-19 08:10:10 +00002790 btrfs_put_delayed_ref(ref);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002791 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
2792 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002793 return ret;
2794 }
2795
Miao Xie093486c2012-12-19 08:10:10 +00002796 btrfs_put_delayed_ref(ref);
2797 count++;
Chris Mason1887be62009-03-13 10:11:24 -04002798 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002799 }
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002800
2801 /*
2802 * We don't want to include ref heads since we can have empty ref heads
2803 * and those will drastically skew our runtime down since we just do
2804 * accounting, no actual extent tree updates.
2805 */
2806 if (actual_count > 0) {
2807 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2808 u64 avg;
2809
2810 /*
2811 * We weigh the current average higher than our current runtime
2812 * to avoid large swings in the average.
2813 */
2814 spin_lock(&delayed_refs->lock);
2815 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002816 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002817 spin_unlock(&delayed_refs->lock);
2818 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002819 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002820}
2821
Arne Jansen709c0482011-09-12 12:22:57 +02002822#ifdef SCRAMBLE_DELAYED_REFS
2823/*
2824 * Normally delayed refs get processed in ascending bytenr order. This
2825 * correlates in most cases to the order added. To expose dependencies on this
2826 * order, we start to process the tree in the middle instead of the beginning
2827 */
2828static u64 find_middle(struct rb_root *root)
2829{
2830 struct rb_node *n = root->rb_node;
2831 struct btrfs_delayed_ref_node *entry;
2832 int alt = 1;
2833 u64 middle;
2834 u64 first = 0, last = 0;
2835
2836 n = rb_first(root);
2837 if (n) {
2838 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2839 first = entry->bytenr;
2840 }
2841 n = rb_last(root);
2842 if (n) {
2843 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2844 last = entry->bytenr;
2845 }
2846 n = root->rb_node;
2847
2848 while (n) {
2849 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2850 WARN_ON(!entry->in_tree);
2851
2852 middle = entry->bytenr;
2853
2854 if (alt)
2855 n = n->rb_left;
2856 else
2857 n = n->rb_right;
2858
2859 alt = 1 - alt;
2860 }
2861 return middle;
2862}
2863#endif
2864
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002865static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002866{
2867 u64 num_bytes;
2868
2869 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2870 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002871 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002872 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2873
2874 /*
2875 * 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 -04002876 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002877 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002878 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002879}
2880
Josef Bacik12621332015-02-03 07:50:16 -08002881/*
2882 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2883 * would require to store the csums for that many bytes.
2884 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002885u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002886{
2887 u64 csum_size;
2888 u64 num_csums_per_leaf;
2889 u64 num_csums;
2890
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002891 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002892 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002893 (u64)btrfs_super_csum_size(fs_info->super_copy));
2894 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002895 num_csums += num_csums_per_leaf - 1;
2896 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2897 return num_csums;
2898}
2899
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002900int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002901 struct btrfs_fs_info *fs_info)
Josef Bacik1be41b72013-06-12 13:56:06 -04002902{
2903 struct btrfs_block_rsv *global_rsv;
2904 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
Josef Bacik12621332015-02-03 07:50:16 -08002905 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
David Sterba165c8b02017-11-08 02:12:57 +01002906 unsigned int num_dirty_bgs = trans->transaction->num_dirty_bgs;
Josef Bacikcb723e42015-02-18 08:06:57 -08002907 u64 num_bytes, num_dirty_bgs_bytes;
Josef Bacik1be41b72013-06-12 13:56:06 -04002908 int ret = 0;
2909
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002910 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002911 num_heads = heads_to_leaves(fs_info, num_heads);
Josef Bacik1be41b72013-06-12 13:56:06 -04002912 if (num_heads > 1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002913 num_bytes += (num_heads - 1) * fs_info->nodesize;
Josef Bacik1be41b72013-06-12 13:56:06 -04002914 num_bytes <<= 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002915 num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
2916 fs_info->nodesize;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002917 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
Josef Bacikcb723e42015-02-18 08:06:57 -08002918 num_dirty_bgs);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002919 global_rsv = &fs_info->global_block_rsv;
Josef Bacik1be41b72013-06-12 13:56:06 -04002920
2921 /*
2922 * If we can't allocate any more chunks lets make sure we have _lots_ of
2923 * wiggle room since running delayed refs can create more delayed refs.
2924 */
Josef Bacikcb723e42015-02-18 08:06:57 -08002925 if (global_rsv->space_info->full) {
2926 num_dirty_bgs_bytes <<= 1;
Josef Bacik1be41b72013-06-12 13:56:06 -04002927 num_bytes <<= 1;
Josef Bacikcb723e42015-02-18 08:06:57 -08002928 }
Josef Bacik1be41b72013-06-12 13:56:06 -04002929
2930 spin_lock(&global_rsv->lock);
Josef Bacikcb723e42015-02-18 08:06:57 -08002931 if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
Josef Bacik1be41b72013-06-12 13:56:06 -04002932 ret = 1;
2933 spin_unlock(&global_rsv->lock);
2934 return ret;
2935}
2936
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002937int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002938 struct btrfs_fs_info *fs_info)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002939{
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002940 u64 num_entries =
2941 atomic_read(&trans->transaction->delayed_refs.num_entries);
2942 u64 avg_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002943 u64 val;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002944
2945 smp_mb();
2946 avg_runtime = fs_info->avg_delayed_ref_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002947 val = num_entries * avg_runtime;
Wang Xiaoguangdc1a90c2016-10-26 15:23:01 +08002948 if (val >= NSEC_PER_SEC)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002949 return 1;
Chris Masona79b7d42014-05-22 16:18:52 -07002950 if (val >= NSEC_PER_SEC / 2)
2951 return 2;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002952
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002953 return btrfs_check_space_for_delayed_refs(trans, fs_info);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002954}
2955
Chris Masona79b7d42014-05-22 16:18:52 -07002956struct async_delayed_refs {
2957 struct btrfs_root *root;
Josef Bacik31b96552016-04-11 17:37:40 -04002958 u64 transid;
Chris Masona79b7d42014-05-22 16:18:52 -07002959 int count;
2960 int error;
2961 int sync;
2962 struct completion wait;
2963 struct btrfs_work work;
2964};
2965
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002966static inline struct async_delayed_refs *
2967to_async_delayed_refs(struct btrfs_work *work)
2968{
2969 return container_of(work, struct async_delayed_refs, work);
2970}
2971
Chris Masona79b7d42014-05-22 16:18:52 -07002972static void delayed_ref_async_start(struct btrfs_work *work)
2973{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002974 struct async_delayed_refs *async = to_async_delayed_refs(work);
Chris Masona79b7d42014-05-22 16:18:52 -07002975 struct btrfs_trans_handle *trans;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002976 struct btrfs_fs_info *fs_info = async->root->fs_info;
Chris Masona79b7d42014-05-22 16:18:52 -07002977 int ret;
2978
Chris Mason0f873ec2016-04-27 09:59:38 -04002979 /* if the commit is already started, we don't need to wait here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002980 if (btrfs_transaction_blocked(fs_info))
Josef Bacik31b96552016-04-11 17:37:40 -04002981 goto done;
Josef Bacik31b96552016-04-11 17:37:40 -04002982
Chris Mason0f873ec2016-04-27 09:59:38 -04002983 trans = btrfs_join_transaction(async->root);
2984 if (IS_ERR(trans)) {
2985 async->error = PTR_ERR(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07002986 goto done;
2987 }
2988
2989 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002990 * trans->sync means that when we call end_transaction, we won't
Chris Masona79b7d42014-05-22 16:18:52 -07002991 * wait on delayed refs
2992 */
2993 trans->sync = true;
Chris Mason0f873ec2016-04-27 09:59:38 -04002994
2995 /* Don't bother flushing if we got into a different transaction */
2996 if (trans->transid > async->transid)
2997 goto end;
2998
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02002999 ret = btrfs_run_delayed_refs(trans, async->count);
Chris Masona79b7d42014-05-22 16:18:52 -07003000 if (ret)
3001 async->error = ret;
Chris Mason0f873ec2016-04-27 09:59:38 -04003002end:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003003 ret = btrfs_end_transaction(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07003004 if (ret && !async->error)
3005 async->error = ret;
3006done:
3007 if (async->sync)
3008 complete(&async->wait);
3009 else
3010 kfree(async);
3011}
3012
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003013int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
Josef Bacik31b96552016-04-11 17:37:40 -04003014 unsigned long count, u64 transid, int wait)
Chris Masona79b7d42014-05-22 16:18:52 -07003015{
3016 struct async_delayed_refs *async;
3017 int ret;
3018
3019 async = kmalloc(sizeof(*async), GFP_NOFS);
3020 if (!async)
3021 return -ENOMEM;
3022
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003023 async->root = fs_info->tree_root;
Chris Masona79b7d42014-05-22 16:18:52 -07003024 async->count = count;
3025 async->error = 0;
Josef Bacik31b96552016-04-11 17:37:40 -04003026 async->transid = transid;
Chris Masona79b7d42014-05-22 16:18:52 -07003027 if (wait)
3028 async->sync = 1;
3029 else
3030 async->sync = 0;
3031 init_completion(&async->wait);
3032
Liu Bo9e0af232014-08-15 23:36:53 +08003033 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
3034 delayed_ref_async_start, NULL, NULL);
Chris Masona79b7d42014-05-22 16:18:52 -07003035
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003036 btrfs_queue_work(fs_info->extent_workers, &async->work);
Chris Masona79b7d42014-05-22 16:18:52 -07003037
3038 if (wait) {
3039 wait_for_completion(&async->wait);
3040 ret = async->error;
3041 kfree(async);
3042 return ret;
3043 }
3044 return 0;
3045}
3046
Chris Masonc3e69d52009-03-13 10:17:05 -04003047/*
3048 * this starts processing the delayed reference count updates and
3049 * extent insertions we have queued up so far. count can be
3050 * 0, which means to process everything in the tree at the start
3051 * of the run (but not newly added entries), or it can be some target
3052 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003053 *
3054 * Returns 0 on success or if called with an aborted transaction
3055 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04003056 */
3057int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003058 unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04003059{
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003060 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonc3e69d52009-03-13 10:17:05 -04003061 struct rb_node *node;
3062 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08003063 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04003064 int ret;
3065 int run_all = count == (unsigned long)-1;
Filipe Mananad9a05402015-10-03 13:13:13 +01003066 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Chris Masonc3e69d52009-03-13 10:17:05 -04003067
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003068 /* We'll clean this up in btrfs_cleanup_transaction */
3069 if (trans->aborted)
3070 return 0;
3071
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003072 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08003073 return 0;
3074
Chris Masonc3e69d52009-03-13 10:17:05 -04003075 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08003076 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05003077 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05003078
Chris Masonc3e69d52009-03-13 10:17:05 -04003079again:
Arne Jansen709c0482011-09-12 12:22:57 +02003080#ifdef SCRAMBLE_DELAYED_REFS
3081 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
3082#endif
Filipe Mananad9a05402015-10-03 13:13:13 +01003083 trans->can_flush_pending_bgs = false;
Nikolay Borisov0a1e4582018-03-15 16:00:27 +02003084 ret = __btrfs_run_delayed_refs(trans, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003085 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003086 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003087 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04003088 }
3089
Chris Mason56bec292009-03-13 10:10:06 -04003090 if (run_all) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05003091 if (!list_empty(&trans->new_bgs))
Nikolay Borisov6c686b32018-02-07 17:55:40 +02003092 btrfs_create_pending_block_groups(trans);
Josef Bacikea658ba2012-09-11 16:57:25 -04003093
Josef Bacikd7df2c72014-01-23 09:21:38 -05003094 spin_lock(&delayed_refs->lock);
Liu Boc46effa2013-10-14 12:59:45 +08003095 node = rb_first(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003096 if (!node) {
3097 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04003098 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05003099 }
Josef Bacikd2788502017-09-29 15:43:57 -04003100 head = rb_entry(node, struct btrfs_delayed_ref_head,
3101 href_node);
3102 refcount_inc(&head->refs);
Chris Mason56bec292009-03-13 10:10:06 -04003103 spin_unlock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04003104
3105 /* Mutex was contended, block until it's released and retry. */
3106 mutex_lock(&head->mutex);
3107 mutex_unlock(&head->mutex);
3108
3109 btrfs_put_delayed_ref_head(head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003110 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04003111 goto again;
3112 }
Chris Mason54aa1f42007-06-22 14:16:25 -04003113out:
Filipe Mananad9a05402015-10-03 13:13:13 +01003114 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Chris Masona28ec192007-03-06 20:08:01 -05003115 return 0;
3116}
3117
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003118int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003119 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003120 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04003121 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003122{
3123 struct btrfs_delayed_extent_op *extent_op;
3124 int ret;
3125
Miao Xie78a61842012-11-21 02:21:28 +00003126 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003127 if (!extent_op)
3128 return -ENOMEM;
3129
3130 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01003131 extent_op->update_flags = true;
3132 extent_op->update_key = false;
3133 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04003134 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003135
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003136 ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003137 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003138 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00003139 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003140 return ret;
3141}
3142
Liu Boe4c3b2d2017-01-30 12:25:28 -08003143static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003144 struct btrfs_path *path,
3145 u64 objectid, u64 offset, u64 bytenr)
3146{
3147 struct btrfs_delayed_ref_head *head;
3148 struct btrfs_delayed_ref_node *ref;
3149 struct btrfs_delayed_data_ref *data_ref;
3150 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08003151 struct btrfs_transaction *cur_trans;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04003152 struct rb_node *node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003153 int ret = 0;
3154
ethanwu998ac6d2018-04-29 15:59:42 +08003155 spin_lock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08003156 cur_trans = root->fs_info->running_transaction;
ethanwu998ac6d2018-04-29 15:59:42 +08003157 if (cur_trans)
3158 refcount_inc(&cur_trans->use_count);
3159 spin_unlock(&root->fs_info->trans_lock);
Liu Boe4c3b2d2017-01-30 12:25:28 -08003160 if (!cur_trans)
3161 return 0;
3162
3163 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003164 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003165 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003166 if (!head) {
3167 spin_unlock(&delayed_refs->lock);
ethanwu998ac6d2018-04-29 15:59:42 +08003168 btrfs_put_transaction(cur_trans);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003169 return 0;
3170 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003171
3172 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04003173 refcount_inc(&head->refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003174 spin_unlock(&delayed_refs->lock);
3175
David Sterbab3b4aa72011-04-21 01:20:15 +02003176 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003177
David Sterba8cc33e52011-05-02 15:29:25 +02003178 /*
3179 * Mutex was contended, block until it's released and let
3180 * caller try again
3181 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003182 mutex_lock(&head->mutex);
3183 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04003184 btrfs_put_delayed_ref_head(head);
ethanwu998ac6d2018-04-29 15:59:42 +08003185 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003186 return -EAGAIN;
3187 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003188 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003189
3190 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04003191 /*
3192 * XXX: We should replace this with a proper search function in the
3193 * future.
3194 */
3195 for (node = rb_first(&head->ref_tree); node; node = rb_next(node)) {
3196 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003197 /* If it's a shared ref we know a cross reference exists */
3198 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
3199 ret = 1;
3200 break;
3201 }
3202
3203 data_ref = btrfs_delayed_node_to_data_ref(ref);
3204
3205 /*
3206 * If our ref doesn't match the one we're currently looking at
3207 * then we have a cross reference.
3208 */
3209 if (data_ref->root != root->root_key.objectid ||
3210 data_ref->objectid != objectid ||
3211 data_ref->offset != offset) {
3212 ret = 1;
3213 break;
3214 }
3215 }
3216 spin_unlock(&head->lock);
3217 mutex_unlock(&head->mutex);
ethanwu998ac6d2018-04-29 15:59:42 +08003218 btrfs_put_transaction(cur_trans);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003219 return ret;
3220}
3221
Liu Boe4c3b2d2017-01-30 12:25:28 -08003222static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003223 struct btrfs_path *path,
3224 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05003225{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003226 struct btrfs_fs_info *fs_info = root->fs_info;
3227 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04003228 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003229 struct btrfs_extent_data_ref *ref;
3230 struct btrfs_extent_inline_ref *iref;
3231 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05003232 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003233 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06003234 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04003235 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05003236
Chris Masonbe20aa92007-12-17 20:14:01 -05003237 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04003238 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04003239 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05003240
Chris Masonbe20aa92007-12-17 20:14:01 -05003241 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3242 if (ret < 0)
3243 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003244 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04003245
3246 ret = -ENOENT;
3247 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04003248 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003249
Zheng Yan31840ae2008-09-23 13:14:14 -04003250 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04003251 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003252 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05003253
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003254 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05003255 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003256
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003257 ret = 1;
3258 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3259#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3260 if (item_size < sizeof(*ei)) {
3261 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3262 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003263 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003264#endif
3265 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
3266
3267 if (item_size != sizeof(*ei) +
3268 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
3269 goto out;
3270
3271 if (btrfs_extent_generation(leaf, ei) <=
3272 btrfs_root_last_snapshot(&root->root_item))
3273 goto out;
3274
3275 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
Liu Bo3de28d52017-08-18 15:15:19 -06003276
3277 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
3278 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003279 goto out;
3280
3281 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3282 if (btrfs_extent_refs(leaf, ei) !=
3283 btrfs_extent_data_ref_count(leaf, ref) ||
3284 btrfs_extent_data_ref_root(leaf, ref) !=
3285 root->root_key.objectid ||
3286 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3287 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3288 goto out;
3289
Yan Zhengf321e492008-07-30 09:26:11 -04003290 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05003291out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003292 return ret;
3293}
3294
Liu Boe4c3b2d2017-01-30 12:25:28 -08003295int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
3296 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003297{
3298 struct btrfs_path *path;
3299 int ret;
3300 int ret2;
3301
3302 path = btrfs_alloc_path();
3303 if (!path)
Su Yue9132c4f2018-05-30 14:49:10 +08003304 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003305
3306 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08003307 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003308 offset, bytenr);
3309 if (ret && ret != -ENOENT)
3310 goto out;
3311
Liu Boe4c3b2d2017-01-30 12:25:28 -08003312 ret2 = check_delayed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003313 offset, bytenr);
3314 } while (ret2 == -EAGAIN);
3315
3316 if (ret2 && ret2 != -ENOENT) {
3317 ret = ret2;
3318 goto out;
3319 }
3320
3321 if (ret != -ENOENT || ret2 != -ENOENT)
3322 ret = 0;
3323out:
Yan Zhengf321e492008-07-30 09:26:11 -04003324 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003325 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3326 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04003327 return ret;
3328}
3329
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003330static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05003331 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003332 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07003333 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04003334{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003335 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04003336 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003337 u64 num_bytes;
3338 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003339 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04003340 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003341 struct btrfs_key key;
3342 struct btrfs_file_extent_item *fi;
3343 int i;
3344 int level;
3345 int ret = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003346 int (*process_func)(struct btrfs_trans_handle *,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003347 struct btrfs_root *,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003348 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04003349
David Sterbafccb84c2014-09-29 23:53:21 +02003350
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003351 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003352 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003353
Zheng Yan31840ae2008-09-23 13:14:14 -04003354 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003355 nritems = btrfs_header_nritems(buf);
3356 level = btrfs_header_level(buf);
3357
Miao Xie27cdeb72014-04-02 19:51:05 +08003358 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003359 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003360
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003361 if (inc)
3362 process_func = btrfs_inc_extent_ref;
3363 else
3364 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003365
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003366 if (full_backref)
3367 parent = buf->start;
3368 else
3369 parent = 0;
3370
Zheng Yan31840ae2008-09-23 13:14:14 -04003371 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003372 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003373 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02003374 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003375 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003376 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003377 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003378 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003379 BTRFS_FILE_EXTENT_INLINE)
3380 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003381 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3382 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003383 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003384
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003385 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3386 key.offset -= btrfs_file_extent_offset(buf, fi);
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003387 ret = process_func(trans, root, bytenr, num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003388 parent, ref_root, key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003389 key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003390 if (ret)
3391 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003392 } else {
3393 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003394 num_bytes = fs_info->nodesize;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003395 ret = process_func(trans, root, bytenr, num_bytes,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003396 parent, ref_root, level - 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003397 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003398 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003399 }
3400 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003401 return 0;
3402fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003403 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003404}
3405
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003406int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003407 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04003408{
Josef Bacike339a6b2014-07-02 10:54:25 -07003409 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003410}
Zheng Yan31840ae2008-09-23 13:14:14 -04003411
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003412int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003413 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003414{
Josef Bacike339a6b2014-07-02 10:54:25 -07003415 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003416}
3417
Chris Mason9078a3e2007-04-26 16:46:15 -04003418static int write_one_cache_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003419 struct btrfs_fs_info *fs_info,
Chris Mason9078a3e2007-04-26 16:46:15 -04003420 struct btrfs_path *path,
3421 struct btrfs_block_group_cache *cache)
3422{
3423 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003424 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003425 unsigned long bi;
3426 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003427
Chris Mason9078a3e2007-04-26 16:46:15 -04003428 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003429 if (ret) {
3430 if (ret > 0)
3431 ret = -ENOENT;
Chris Mason54aa1f42007-06-22 14:16:25 -04003432 goto fail;
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003433 }
Chris Mason5f39d392007-10-15 16:14:19 -04003434
3435 leaf = path->nodes[0];
3436 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3437 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3438 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -04003439fail:
Filipe Manana24b89d02015-04-25 18:31:05 +01003440 btrfs_release_path(path);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003441 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003442
3443}
3444
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003445static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003446next_block_group(struct btrfs_fs_info *fs_info,
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003447 struct btrfs_block_group_cache *cache)
3448{
3449 struct rb_node *node;
Filipe Manana292cbd52014-11-26 15:28:50 +00003450
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003451 spin_lock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003452
3453 /* If our block group was removed, we need a full search. */
3454 if (RB_EMPTY_NODE(&cache->cache_node)) {
3455 const u64 next_bytenr = cache->key.objectid + cache->key.offset;
3456
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003457 spin_unlock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003458 btrfs_put_block_group(cache);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003459 cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
Filipe Manana292cbd52014-11-26 15:28:50 +00003460 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003461 node = rb_next(&cache->cache_node);
3462 btrfs_put_block_group(cache);
3463 if (node) {
3464 cache = rb_entry(node, struct btrfs_block_group_cache,
3465 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003466 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003467 } else
3468 cache = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003469 spin_unlock(&fs_info->block_group_cache_lock);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003470 return cache;
3471}
3472
Josef Bacik0af3d002010-06-21 14:48:16 -04003473static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3474 struct btrfs_trans_handle *trans,
3475 struct btrfs_path *path)
3476{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003477 struct btrfs_fs_info *fs_info = block_group->fs_info;
3478 struct btrfs_root *root = fs_info->tree_root;
Josef Bacik0af3d002010-06-21 14:48:16 -04003479 struct inode *inode = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08003480 struct extent_changeset *data_reserved = NULL;
Josef Bacik0af3d002010-06-21 14:48:16 -04003481 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003482 int dcs = BTRFS_DC_ERROR;
David Sterbaf8c269d2015-01-16 17:21:12 +01003483 u64 num_pages = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003484 int retries = 0;
3485 int ret = 0;
3486
3487 /*
3488 * If this block group is smaller than 100 megs don't bother caching the
3489 * block group.
3490 */
Byongho Leeee221842015-12-15 01:42:10 +09003491 if (block_group->key.offset < (100 * SZ_1M)) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003492 spin_lock(&block_group->lock);
3493 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3494 spin_unlock(&block_group->lock);
3495 return 0;
3496 }
3497
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003498 if (trans->aborted)
3499 return 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003500again:
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003501 inode = lookup_free_space_inode(fs_info, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003502 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3503 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003504 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003505 goto out;
3506 }
3507
3508 if (IS_ERR(inode)) {
3509 BUG_ON(retries);
3510 retries++;
3511
3512 if (block_group->ro)
3513 goto out_free;
3514
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003515 ret = create_free_space_inode(fs_info, trans, block_group,
3516 path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003517 if (ret)
3518 goto out_free;
3519 goto again;
3520 }
3521
3522 /*
3523 * We want to set the generation to 0, that way if anything goes wrong
3524 * from here on out we know not to trust this cache when we load up next
3525 * time.
3526 */
3527 BTRFS_I(inode)->generation = 0;
3528 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003529 if (ret) {
3530 /*
3531 * So theoretically we could recover from this, simply set the
3532 * super cache generation to 0 so we know to invalidate the
3533 * cache, but then we'd have to keep track of the block groups
3534 * that fail this way so we know we _have_ to reset this cache
3535 * before the next commit or risk reading stale cache. So to
3536 * limit our exposure to horrible edge cases lets just abort the
3537 * transaction, this only happens in really bad situations
3538 * anyway.
3539 */
Jeff Mahoney66642832016-06-10 18:19:25 -04003540 btrfs_abort_transaction(trans, ret);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003541 goto out_put;
3542 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003543 WARN_ON(ret);
3544
Josef Bacik8e138e02017-11-17 14:50:46 -05003545 /* We've already setup this transaction, go ahead and exit */
3546 if (block_group->cache_generation == trans->transid &&
3547 i_size_read(inode)) {
3548 dcs = BTRFS_DC_SETUP;
3549 goto out_put;
3550 }
3551
Josef Bacik0af3d002010-06-21 14:48:16 -04003552 if (i_size_read(inode) > 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003553 ret = btrfs_check_trunc_cache_free_space(fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003554 &fs_info->global_block_rsv);
Miao Xie7b61cd92013-05-13 13:55:09 +00003555 if (ret)
3556 goto out_put;
3557
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003558 ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003559 if (ret)
3560 goto out_put;
3561 }
3562
3563 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003564 if (block_group->cached != BTRFS_CACHE_FINISHED ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003565 !btrfs_test_opt(fs_info, SPACE_CACHE)) {
Liu Bocf7c1ef2012-07-06 03:31:34 -06003566 /*
3567 * don't bother trying to write stuff out _if_
3568 * a) we're not cached,
Liu Bo1a79c1f2017-03-06 13:49:02 -08003569 * b) we're with nospace_cache mount option,
3570 * c) we're with v2 space_cache (FREE_SPACE_TREE).
Liu Bocf7c1ef2012-07-06 03:31:34 -06003571 */
Josef Bacik2b209822010-12-03 13:17:53 -05003572 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003573 spin_unlock(&block_group->lock);
3574 goto out_put;
3575 }
3576 spin_unlock(&block_group->lock);
3577
Josef Bacik6fc823b2012-08-06 13:46:38 -06003578 /*
Josef Bacik2968b1f2015-10-01 12:55:18 -04003579 * We hit an ENOSPC when setting up the cache in this transaction, just
3580 * skip doing the setup, we've already cleared the cache so we're safe.
3581 */
3582 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
3583 ret = -ENOSPC;
3584 goto out_put;
3585 }
3586
3587 /*
Josef Bacik6fc823b2012-08-06 13:46:38 -06003588 * Try to preallocate enough space based on how big the block group is.
3589 * Keep in mind this has to include any pinned space which could end up
3590 * taking up quite a bit since it's not folded into the other space
3591 * cache.
3592 */
Byongho Leeee221842015-12-15 01:42:10 +09003593 num_pages = div_u64(block_group->key.offset, SZ_256M);
Josef Bacik0af3d002010-06-21 14:48:16 -04003594 if (!num_pages)
3595 num_pages = 1;
3596
Josef Bacik0af3d002010-06-21 14:48:16 -04003597 num_pages *= 16;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003598 num_pages *= PAGE_SIZE;
Josef Bacik0af3d002010-06-21 14:48:16 -04003599
Qu Wenruo364ecf32017-02-27 15:10:38 +08003600 ret = btrfs_check_data_free_space(inode, &data_reserved, 0, num_pages);
Josef Bacik0af3d002010-06-21 14:48:16 -04003601 if (ret)
3602 goto out_put;
3603
3604 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3605 num_pages, num_pages,
3606 &alloc_hint);
Josef Bacik2968b1f2015-10-01 12:55:18 -04003607 /*
3608 * Our cache requires contiguous chunks so that we don't modify a bunch
3609 * of metadata or split extents when writing the cache out, which means
3610 * we can enospc if we are heavily fragmented in addition to just normal
3611 * out of space conditions. So if we hit this just skip setting up any
3612 * other block groups for this transaction, maybe we'll unpin enough
3613 * space the next time around.
3614 */
Josef Bacik2b209822010-12-03 13:17:53 -05003615 if (!ret)
3616 dcs = BTRFS_DC_SETUP;
Josef Bacik2968b1f2015-10-01 12:55:18 -04003617 else if (ret == -ENOSPC)
3618 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
Josef Bacikc09544e2011-08-30 10:19:10 -04003619
Josef Bacik0af3d002010-06-21 14:48:16 -04003620out_put:
3621 iput(inode);
3622out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003623 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003624out:
3625 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003626 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003627 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003628 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003629 spin_unlock(&block_group->lock);
3630
Qu Wenruo364ecf32017-02-27 15:10:38 +08003631 extent_changeset_free(data_reserved);
Josef Bacik0af3d002010-06-21 14:48:16 -04003632 return ret;
3633}
3634
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003635int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003636 struct btrfs_fs_info *fs_info)
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003637{
3638 struct btrfs_block_group_cache *cache, *tmp;
3639 struct btrfs_transaction *cur_trans = trans->transaction;
3640 struct btrfs_path *path;
3641
3642 if (list_empty(&cur_trans->dirty_bgs) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003643 !btrfs_test_opt(fs_info, SPACE_CACHE))
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003644 return 0;
3645
3646 path = btrfs_alloc_path();
3647 if (!path)
3648 return -ENOMEM;
3649
3650 /* Could add new block groups, use _safe just in case */
3651 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
3652 dirty_list) {
3653 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3654 cache_save_setup(cache, trans, path);
3655 }
3656
3657 btrfs_free_path(path);
3658 return 0;
3659}
3660
Chris Mason1bbc6212015-04-06 12:46:08 -07003661/*
3662 * transaction commit does final block group cache writeback during a
3663 * critical section where nothing is allowed to change the FS. This is
3664 * required in order for the cache to actually match the block group,
3665 * but can introduce a lot of latency into the commit.
3666 *
3667 * So, btrfs_start_dirty_block_groups is here to kick off block group
3668 * cache IO. There's a chance we'll have to redo some of it if the
3669 * block group changes again during the commit, but it greatly reduces
3670 * the commit latency by getting rid of the easy block groups while
3671 * we're still allowing others to join the commit.
3672 */
Nikolay Borisov21217052018-02-07 17:55:41 +02003673int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
Chris Mason1bbc6212015-04-06 12:46:08 -07003674{
Nikolay Borisov21217052018-02-07 17:55:41 +02003675 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason1bbc6212015-04-06 12:46:08 -07003676 struct btrfs_block_group_cache *cache;
3677 struct btrfs_transaction *cur_trans = trans->transaction;
3678 int ret = 0;
3679 int should_put;
3680 struct btrfs_path *path = NULL;
3681 LIST_HEAD(dirty);
3682 struct list_head *io = &cur_trans->io_bgs;
3683 int num_started = 0;
3684 int loops = 0;
3685
3686 spin_lock(&cur_trans->dirty_bgs_lock);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003687 if (list_empty(&cur_trans->dirty_bgs)) {
3688 spin_unlock(&cur_trans->dirty_bgs_lock);
3689 return 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003690 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003691 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Chris Mason1bbc6212015-04-06 12:46:08 -07003692 spin_unlock(&cur_trans->dirty_bgs_lock);
3693
3694again:
Chris Mason1bbc6212015-04-06 12:46:08 -07003695 /*
3696 * make sure all the block groups on our dirty list actually
3697 * exist
3698 */
Nikolay Borisov6c686b32018-02-07 17:55:40 +02003699 btrfs_create_pending_block_groups(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07003700
3701 if (!path) {
3702 path = btrfs_alloc_path();
3703 if (!path)
3704 return -ENOMEM;
3705 }
3706
Filipe Mananab58d1a92015-04-25 18:29:16 +01003707 /*
3708 * cache_write_mutex is here only to save us from balance or automatic
3709 * removal of empty block groups deleting this block group while we are
3710 * writing out the cache
3711 */
3712 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003713 while (!list_empty(&dirty)) {
3714 cache = list_first_entry(&dirty,
3715 struct btrfs_block_group_cache,
3716 dirty_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07003717 /*
3718 * this can happen if something re-dirties a block
3719 * group that is already under IO. Just wait for it to
3720 * finish and then do it all again
3721 */
3722 if (!list_empty(&cache->io_list)) {
3723 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003724 btrfs_wait_cache_io(trans, cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003725 btrfs_put_block_group(cache);
3726 }
3727
3728
3729 /*
3730 * btrfs_wait_cache_io uses the cache->dirty_list to decide
3731 * if it should update the cache_state. Don't delete
3732 * until after we wait.
3733 *
3734 * Since we're not running in the commit critical section
3735 * we need the dirty_bgs_lock to protect from update_block_group
3736 */
3737 spin_lock(&cur_trans->dirty_bgs_lock);
3738 list_del_init(&cache->dirty_list);
3739 spin_unlock(&cur_trans->dirty_bgs_lock);
3740
3741 should_put = 1;
3742
3743 cache_save_setup(cache, trans, path);
3744
3745 if (cache->disk_cache_state == BTRFS_DC_SETUP) {
3746 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003747 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003748 cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003749 if (ret == 0 && cache->io_ctl.inode) {
3750 num_started++;
3751 should_put = 0;
3752
3753 /*
Nikolay Borisov45ae2c12018-02-08 18:25:18 +02003754 * The cache_write_mutex is protecting the
3755 * io_list, also refer to the definition of
3756 * btrfs_transaction::io_bgs for more details
Chris Mason1bbc6212015-04-06 12:46:08 -07003757 */
3758 list_add_tail(&cache->io_list, io);
3759 } else {
3760 /*
3761 * if we failed to write the cache, the
3762 * generation will be bad and life goes on
3763 */
3764 ret = 0;
3765 }
3766 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003767 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003768 ret = write_one_cache_group(trans, fs_info,
3769 path, cache);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003770 /*
3771 * Our block group might still be attached to the list
3772 * of new block groups in the transaction handle of some
3773 * other task (struct btrfs_trans_handle->new_bgs). This
3774 * means its block group item isn't yet in the extent
3775 * tree. If this happens ignore the error, as we will
3776 * try again later in the critical section of the
3777 * transaction commit.
3778 */
3779 if (ret == -ENOENT) {
3780 ret = 0;
3781 spin_lock(&cur_trans->dirty_bgs_lock);
3782 if (list_empty(&cache->dirty_list)) {
3783 list_add_tail(&cache->dirty_list,
3784 &cur_trans->dirty_bgs);
3785 btrfs_get_block_group(cache);
3786 }
3787 spin_unlock(&cur_trans->dirty_bgs_lock);
3788 } else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003789 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003790 }
3791 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003792
3793 /* if its not on the io list, we need to put the block group */
3794 if (should_put)
3795 btrfs_put_block_group(cache);
3796
3797 if (ret)
3798 break;
Filipe Mananab58d1a92015-04-25 18:29:16 +01003799
3800 /*
3801 * Avoid blocking other tasks for too long. It might even save
3802 * us from writing caches for block groups that are going to be
3803 * removed.
3804 */
3805 mutex_unlock(&trans->transaction->cache_write_mutex);
3806 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003807 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003808 mutex_unlock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003809
3810 /*
3811 * go through delayed refs for all the stuff we've just kicked off
3812 * and then loop back (just once)
3813 */
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003814 ret = btrfs_run_delayed_refs(trans, 0);
Chris Mason1bbc6212015-04-06 12:46:08 -07003815 if (!ret && loops == 0) {
3816 loops++;
3817 spin_lock(&cur_trans->dirty_bgs_lock);
3818 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003819 /*
3820 * dirty_bgs_lock protects us from concurrent block group
3821 * deletes too (not just cache_write_mutex).
3822 */
3823 if (!list_empty(&dirty)) {
3824 spin_unlock(&cur_trans->dirty_bgs_lock);
3825 goto again;
3826 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003827 spin_unlock(&cur_trans->dirty_bgs_lock);
Liu Boc79a1752016-07-20 17:44:12 -07003828 } else if (ret < 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003829 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
Chris Mason1bbc6212015-04-06 12:46:08 -07003830 }
3831
3832 btrfs_free_path(path);
3833 return ret;
3834}
3835
Chris Mason96b51792007-10-15 16:15:19 -04003836int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003837 struct btrfs_fs_info *fs_info)
Chris Mason9078a3e2007-04-26 16:46:15 -04003838{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003839 struct btrfs_block_group_cache *cache;
Josef Bacikce93ec52014-11-17 15:45:48 -05003840 struct btrfs_transaction *cur_trans = trans->transaction;
3841 int ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003842 int should_put;
Chris Mason9078a3e2007-04-26 16:46:15 -04003843 struct btrfs_path *path;
Chris Mason1bbc6212015-04-06 12:46:08 -07003844 struct list_head *io = &cur_trans->io_bgs;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003845 int num_started = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003846
3847 path = btrfs_alloc_path();
3848 if (!path)
3849 return -ENOMEM;
3850
Josef Bacikce93ec52014-11-17 15:45:48 -05003851 /*
Filipe Mananae44081e2015-12-18 03:02:48 +00003852 * Even though we are in the critical section of the transaction commit,
3853 * we can still have concurrent tasks adding elements to this
3854 * transaction's list of dirty block groups. These tasks correspond to
3855 * endio free space workers started when writeback finishes for a
3856 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
3857 * allocate new block groups as a result of COWing nodes of the root
3858 * tree when updating the free space inode. The writeback for the space
3859 * caches is triggered by an earlier call to
3860 * btrfs_start_dirty_block_groups() and iterations of the following
3861 * loop.
3862 * Also we want to do the cache_save_setup first and then run the
Josef Bacikce93ec52014-11-17 15:45:48 -05003863 * delayed refs to make sure we have the best chance at doing this all
3864 * in one shot.
3865 */
Filipe Mananae44081e2015-12-18 03:02:48 +00003866 spin_lock(&cur_trans->dirty_bgs_lock);
Josef Bacikce93ec52014-11-17 15:45:48 -05003867 while (!list_empty(&cur_trans->dirty_bgs)) {
3868 cache = list_first_entry(&cur_trans->dirty_bgs,
3869 struct btrfs_block_group_cache,
3870 dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003871
3872 /*
3873 * this can happen if cache_save_setup re-dirties a block
3874 * group that is already under IO. Just wait for it to
3875 * finish and then do it all again
3876 */
3877 if (!list_empty(&cache->io_list)) {
Filipe Mananae44081e2015-12-18 03:02:48 +00003878 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003879 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003880 btrfs_wait_cache_io(trans, cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003881 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003882 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003883 }
3884
Chris Mason1bbc6212015-04-06 12:46:08 -07003885 /*
3886 * don't remove from the dirty list until after we've waited
3887 * on any pending IO
3888 */
Josef Bacikce93ec52014-11-17 15:45:48 -05003889 list_del_init(&cache->dirty_list);
Filipe Mananae44081e2015-12-18 03:02:48 +00003890 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003891 should_put = 1;
3892
Chris Mason1bbc6212015-04-06 12:46:08 -07003893 cache_save_setup(cache, trans, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003894
Josef Bacikce93ec52014-11-17 15:45:48 -05003895 if (!ret)
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02003896 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003897 (unsigned long) -1);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003898
3899 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3900 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003901 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003902 cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003903 if (ret == 0 && cache->io_ctl.inode) {
3904 num_started++;
3905 should_put = 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003906 list_add_tail(&cache->io_list, io);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003907 } else {
3908 /*
3909 * if we failed to write the cache, the
3910 * generation will be bad and life goes on
3911 */
3912 ret = 0;
3913 }
3914 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003915 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003916 ret = write_one_cache_group(trans, fs_info,
3917 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003918 /*
3919 * One of the free space endio workers might have
3920 * created a new block group while updating a free space
3921 * cache's inode (at inode.c:btrfs_finish_ordered_io())
3922 * and hasn't released its transaction handle yet, in
3923 * which case the new block group is still attached to
3924 * its transaction handle and its creation has not
3925 * finished yet (no block group item in the extent tree
3926 * yet, etc). If this is the case, wait for all free
3927 * space endio workers to finish and retry. This is a
3928 * a very rare case so no need for a more efficient and
3929 * complex approach.
3930 */
3931 if (ret == -ENOENT) {
3932 wait_event(cur_trans->writer_wait,
3933 atomic_read(&cur_trans->num_writers) == 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003934 ret = write_one_cache_group(trans, fs_info,
3935 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003936 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003937 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003938 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003939 }
Chris Masonc9dc4c62015-04-04 17:14:42 -07003940
3941 /* if its not on the io list, we need to put the block group */
3942 if (should_put)
3943 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003944 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003945 }
Filipe Mananae44081e2015-12-18 03:02:48 +00003946 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003947
Nikolay Borisov45ae2c12018-02-08 18:25:18 +02003948 /*
3949 * Refer to the definition of io_bgs member for details why it's safe
3950 * to use it without any locking
3951 */
Chris Mason1bbc6212015-04-06 12:46:08 -07003952 while (!list_empty(io)) {
3953 cache = list_first_entry(io, struct btrfs_block_group_cache,
Chris Masonc9dc4c62015-04-04 17:14:42 -07003954 io_list);
3955 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003956 btrfs_wait_cache_io(trans, cache, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003957 btrfs_put_block_group(cache);
3958 }
3959
Chris Mason9078a3e2007-04-26 16:46:15 -04003960 btrfs_free_path(path);
Josef Bacikce93ec52014-11-17 15:45:48 -05003961 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003962}
3963
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003964int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05003965{
3966 struct btrfs_block_group_cache *block_group;
3967 int readonly = 0;
3968
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003969 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003970 if (!block_group || block_group->ro)
3971 readonly = 1;
3972 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003973 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003974 return readonly;
3975}
3976
Filipe Mananaf78c4362016-05-09 13:15:41 +01003977bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3978{
3979 struct btrfs_block_group_cache *bg;
3980 bool ret = true;
3981
3982 bg = btrfs_lookup_block_group(fs_info, bytenr);
3983 if (!bg)
3984 return false;
3985
3986 spin_lock(&bg->lock);
3987 if (bg->ro)
3988 ret = false;
3989 else
3990 atomic_inc(&bg->nocow_writers);
3991 spin_unlock(&bg->lock);
3992
3993 /* no put on block group, done by btrfs_dec_nocow_writers */
3994 if (!ret)
3995 btrfs_put_block_group(bg);
3996
3997 return ret;
3998
3999}
4000
4001void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
4002{
4003 struct btrfs_block_group_cache *bg;
4004
4005 bg = btrfs_lookup_block_group(fs_info, bytenr);
4006 ASSERT(bg);
4007 if (atomic_dec_and_test(&bg->nocow_writers))
Peter Zijlstra46259562018-03-15 11:43:08 +01004008 wake_up_var(&bg->nocow_writers);
Filipe Mananaf78c4362016-05-09 13:15:41 +01004009 /*
4010 * Once for our lookup and once for the lookup done by a previous call
4011 * to btrfs_inc_nocow_writers()
4012 */
4013 btrfs_put_block_group(bg);
4014 btrfs_put_block_group(bg);
4015}
4016
Filipe Mananaf78c4362016-05-09 13:15:41 +01004017void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg)
4018{
Peter Zijlstra46259562018-03-15 11:43:08 +01004019 wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers));
Filipe Mananaf78c4362016-05-09 13:15:41 +01004020}
4021
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04004022static const char *alloc_name(u64 flags)
4023{
4024 switch (flags) {
4025 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
4026 return "mixed";
4027 case BTRFS_BLOCK_GROUP_METADATA:
4028 return "metadata";
4029 case BTRFS_BLOCK_GROUP_DATA:
4030 return "data";
4031 case BTRFS_BLOCK_GROUP_SYSTEM:
4032 return "system";
4033 default:
4034 WARN_ON(1);
4035 return "invalid-combination";
4036 };
4037}
4038
Lu Fengqi4ca61682018-05-28 14:30:27 +08004039static int create_space_info(struct btrfs_fs_info *info, u64 flags)
Nikolay Borisov2be12ef2017-05-22 09:35:49 +03004040{
4041
4042 struct btrfs_space_info *space_info;
4043 int i;
4044 int ret;
4045
4046 space_info = kzalloc(sizeof(*space_info), GFP_NOFS);
4047 if (!space_info)
4048 return -ENOMEM;
4049
4050 ret = percpu_counter_init(&space_info->total_bytes_pinned, 0,
4051 GFP_KERNEL);
4052 if (ret) {
4053 kfree(space_info);
4054 return ret;
4055 }
4056
4057 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
4058 INIT_LIST_HEAD(&space_info->block_groups[i]);
4059 init_rwsem(&space_info->groups_sem);
4060 spin_lock_init(&space_info->lock);
4061 space_info->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
4062 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
4063 init_waitqueue_head(&space_info->wait);
4064 INIT_LIST_HEAD(&space_info->ro_bgs);
4065 INIT_LIST_HEAD(&space_info->tickets);
4066 INIT_LIST_HEAD(&space_info->priority_tickets);
4067
4068 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
4069 info->space_info_kobj, "%s",
4070 alloc_name(space_info->flags));
4071 if (ret) {
4072 percpu_counter_destroy(&space_info->total_bytes_pinned);
4073 kfree(space_info);
4074 return ret;
4075 }
4076
Nikolay Borisov2be12ef2017-05-22 09:35:49 +03004077 list_add_rcu(&space_info->list, &info->space_info);
4078 if (flags & BTRFS_BLOCK_GROUP_DATA)
4079 info->data_sinfo = space_info;
4080
4081 return ret;
4082}
4083
Nikolay Borisovd2006e62017-05-22 09:35:50 +03004084static void update_space_info(struct btrfs_fs_info *info, u64 flags,
Chris Mason593060d2008-03-25 16:50:33 -04004085 u64 total_bytes, u64 bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -04004086 u64 bytes_readonly,
Chris Mason593060d2008-03-25 16:50:33 -04004087 struct btrfs_space_info **space_info)
4088{
4089 struct btrfs_space_info *found;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004090 int factor;
4091
4092 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
4093 BTRFS_BLOCK_GROUP_RAID10))
4094 factor = 2;
4095 else
4096 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04004097
4098 found = __find_space_info(info, flags);
Nikolay Borisovd2006e62017-05-22 09:35:50 +03004099 ASSERT(found);
4100 spin_lock(&found->lock);
4101 found->total_bytes += total_bytes;
4102 found->disk_total += total_bytes * factor;
4103 found->bytes_used += bytes_used;
4104 found->disk_used += bytes_used * factor;
4105 found->bytes_readonly += bytes_readonly;
4106 if (total_bytes > 0)
4107 found->full = 0;
4108 space_info_add_new_bytes(info, found, total_bytes -
4109 bytes_used - bytes_readonly);
4110 spin_unlock(&found->lock);
4111 *space_info = found;
Chris Mason593060d2008-03-25 16:50:33 -04004112}
4113
Chris Mason8790d502008-04-03 16:29:03 -04004114static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
4115{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03004116 u64 extra_flags = chunk_to_extended(flags) &
4117 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004118
Miao Xiede98ced2013-01-29 10:13:12 +00004119 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004120 if (flags & BTRFS_BLOCK_GROUP_DATA)
4121 fs_info->avail_data_alloc_bits |= extra_flags;
4122 if (flags & BTRFS_BLOCK_GROUP_METADATA)
4123 fs_info->avail_metadata_alloc_bits |= extra_flags;
4124 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
4125 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004126 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04004127}
Chris Mason593060d2008-03-25 16:50:33 -04004128
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004129/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004130 * returns target flags in extended format or 0 if restripe for this
4131 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04004132 *
David Sterbadccdb072018-03-21 00:20:05 +01004133 * should be called with balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004134 */
4135static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
4136{
4137 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4138 u64 target = 0;
4139
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004140 if (!bctl)
4141 return 0;
4142
4143 if (flags & BTRFS_BLOCK_GROUP_DATA &&
4144 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4145 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
4146 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
4147 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4148 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
4149 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
4150 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4151 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
4152 }
4153
4154 return target;
4155}
4156
4157/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004158 * @flags: available profiles in extended format (see ctree.h)
4159 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004160 * Returns reduced profile in chunk format. If profile changing is in
4161 * progress (either running or paused) picks the target profile (if it's
4162 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004163 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004164static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04004165{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004166 u64 num_devices = fs_info->fs_devices->rw_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004167 u64 target;
Zhao Lei9c170b22015-09-15 21:08:08 +08004168 u64 raid_type;
4169 u64 allowed = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004170
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004171 /*
4172 * see if restripe for this chunk_type is in progress, if so
4173 * try to reduce to the target profile
4174 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004175 spin_lock(&fs_info->balance_lock);
4176 target = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004177 if (target) {
4178 /* pick target profile only if it's already available */
4179 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004180 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004181 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004182 }
4183 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004184 spin_unlock(&fs_info->balance_lock);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004185
David Woodhouse53b381b2013-01-29 18:40:14 -05004186 /* First, mask out the RAID levels which aren't possible */
Zhao Lei9c170b22015-09-15 21:08:08 +08004187 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
4188 if (num_devices >= btrfs_raid_array[raid_type].devs_min)
Anand Jain41a6e892018-04-25 19:01:43 +08004189 allowed |= btrfs_raid_array[raid_type].bg_flag;
Zhao Lei9c170b22015-09-15 21:08:08 +08004190 }
4191 allowed &= flags;
Chris Masona061fc82008-05-07 11:43:44 -04004192
Zhao Lei9c170b22015-09-15 21:08:08 +08004193 if (allowed & BTRFS_BLOCK_GROUP_RAID6)
4194 allowed = BTRFS_BLOCK_GROUP_RAID6;
4195 else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
4196 allowed = BTRFS_BLOCK_GROUP_RAID5;
4197 else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
4198 allowed = BTRFS_BLOCK_GROUP_RAID10;
4199 else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
4200 allowed = BTRFS_BLOCK_GROUP_RAID1;
4201 else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
4202 allowed = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04004203
Zhao Lei9c170b22015-09-15 21:08:08 +08004204 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
Chris Masonec44a352008-04-28 15:29:52 -04004205
Zhao Lei9c170b22015-09-15 21:08:08 +08004206 return extended_to_chunk(flags | allowed);
Chris Masonec44a352008-04-28 15:29:52 -04004207}
4208
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004209static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05004210{
Miao Xiede98ced2013-01-29 10:13:12 +00004211 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004212 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004213
4214 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004215 flags = orig_flags;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004216 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00004217
4218 if (flags & BTRFS_BLOCK_GROUP_DATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004219 flags |= fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004220 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004221 flags |= fs_info->avail_system_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004222 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004223 flags |= fs_info->avail_metadata_alloc_bits;
4224 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02004225
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004226 return btrfs_reduce_alloc_profile(fs_info, flags);
Yan, Zhengb742bb822010-05-16 10:46:24 -04004227}
Josef Bacik6a632092009-02-20 11:00:09 -05004228
Jeff Mahoney1b868262017-05-17 11:38:35 -04004229static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04004230{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004231 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004232 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05004233 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004234
Yan, Zhengb742bb822010-05-16 10:46:24 -04004235 if (data)
4236 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004237 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb822010-05-16 10:46:24 -04004238 flags = BTRFS_BLOCK_GROUP_SYSTEM;
4239 else
4240 flags = BTRFS_BLOCK_GROUP_METADATA;
4241
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004242 ret = get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05004243 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004244}
4245
Jeff Mahoney1b868262017-05-17 11:38:35 -04004246u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info)
4247{
4248 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_DATA);
4249}
4250
4251u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info)
4252{
4253 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4254}
4255
4256u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info)
4257{
4258 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4259}
4260
Liu Bo41361352017-02-13 15:42:21 -08004261static u64 btrfs_space_info_used(struct btrfs_space_info *s_info,
4262 bool may_use_included)
4263{
4264 ASSERT(s_info);
4265 return s_info->bytes_used + s_info->bytes_reserved +
4266 s_info->bytes_pinned + s_info->bytes_readonly +
4267 (may_use_included ? s_info->bytes_may_use : 0);
4268}
4269
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004270int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05004271{
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004272 struct btrfs_root *root = inode->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004273 struct btrfs_fs_info *fs_info = root->fs_info;
Nikolay Borisov1174cad2017-07-11 13:47:50 +03004274 struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
Josef Bacikab6e24102010-03-19 14:38:13 +00004275 u64 used;
Zhao Lei94b947b2015-02-14 13:23:45 +08004276 int ret = 0;
Zhao Leic99f1b02015-03-02 19:32:20 +08004277 int need_commit = 2;
4278 int have_pinned_space;
Josef Bacik6a632092009-02-20 11:00:09 -05004279
4280 /* make sure bytes are sectorsize aligned */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004281 bytes = ALIGN(bytes, fs_info->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05004282
Miao Xie9dced182013-10-25 17:33:36 +08004283 if (btrfs_is_free_space_inode(inode)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004284 need_commit = 0;
Miao Xie9dced182013-10-25 17:33:36 +08004285 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04004286 }
4287
Josef Bacik6a632092009-02-20 11:00:09 -05004288again:
4289 /* make sure we have enough space to handle the data first */
4290 spin_lock(&data_sinfo->lock);
Liu Bo41361352017-02-13 15:42:21 -08004291 used = btrfs_space_info_used(data_sinfo, true);
Josef Bacikab6e24102010-03-19 14:38:13 +00004292
4293 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004294 struct btrfs_trans_handle *trans;
4295
Josef Bacik6a632092009-02-20 11:00:09 -05004296 /*
4297 * if we don't have enough free bytes in this space then we need
4298 * to alloc a new chunk.
4299 */
Zhao Leib9fd47c2015-02-09 14:40:20 +08004300 if (!data_sinfo->full) {
Josef Bacik6a632092009-02-20 11:00:09 -05004301 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05004302
Chris Mason0e4f8f82011-04-15 16:05:44 -04004303 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05004304 spin_unlock(&data_sinfo->lock);
Nikolay Borisov1174cad2017-07-11 13:47:50 +03004305
Jeff Mahoney1b868262017-05-17 11:38:35 -04004306 alloc_target = btrfs_data_alloc_profile(fs_info);
Miao Xie9dced182013-10-25 17:33:36 +08004307 /*
4308 * It is ugly that we don't call nolock join
4309 * transaction for the free space inode case here.
4310 * But it is safe because we only do the data space
4311 * reservation for the free space cache in the
4312 * transaction context, the common join transaction
4313 * just increase the counter of the current transaction
4314 * handler, doesn't try to acquire the trans_lock of
4315 * the fs.
4316 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004317 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004318 if (IS_ERR(trans))
4319 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05004320
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004321 ret = do_chunk_alloc(trans, fs_info, alloc_target,
Chris Mason0e4f8f82011-04-15 16:05:44 -04004322 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004323 btrfs_end_transaction(trans);
Miao Xied52a5b52011-01-05 10:07:18 +00004324 if (ret < 0) {
4325 if (ret != -ENOSPC)
4326 return ret;
Zhao Leic99f1b02015-03-02 19:32:20 +08004327 else {
4328 have_pinned_space = 1;
Miao Xied52a5b52011-01-05 10:07:18 +00004329 goto commit_trans;
Zhao Leic99f1b02015-03-02 19:32:20 +08004330 }
Miao Xied52a5b52011-01-05 10:07:18 +00004331 }
Chris Mason33b4d472009-09-22 14:45:50 -04004332
Josef Bacik6a632092009-02-20 11:00:09 -05004333 goto again;
4334 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004335
4336 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04004337 * If we don't have enough pinned space to deal with this
Zhao Lei94b947b2015-02-14 13:23:45 +08004338 * allocation, and no removed chunk in current transaction,
4339 * don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004340 */
Zhao Leic99f1b02015-03-02 19:32:20 +08004341 have_pinned_space = percpu_counter_compare(
4342 &data_sinfo->total_bytes_pinned,
4343 used + bytes - data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05004344 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004345
4346 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00004347commit_trans:
Nikolay Borisov92e2f7e2018-02-05 10:41:16 +02004348 if (need_commit) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004349 need_commit--;
Josef Bacikb150a4f2013-06-19 15:00:04 -04004350
Zhao Leie1746e82015-12-01 18:39:40 +08004351 if (need_commit > 0) {
Nikolay Borisov82b3e532018-04-23 10:54:13 +03004352 btrfs_start_delalloc_roots(fs_info, -1);
Chris Mason6374e57a2017-06-23 09:48:21 -07004353 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004354 (u64)-1);
Zhao Leie1746e82015-12-01 18:39:40 +08004355 }
Zhao Lei9a4e7272015-04-09 12:34:43 +08004356
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004357 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004358 if (IS_ERR(trans))
4359 return PTR_ERR(trans);
Zhao Leic99f1b02015-03-02 19:32:20 +08004360 if (have_pinned_space >= 0 ||
Josef Bacik3204d332015-09-24 10:46:10 -04004361 test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
4362 &trans->transaction->flags) ||
Zhao Leic99f1b02015-03-02 19:32:20 +08004363 need_commit > 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004364 ret = btrfs_commit_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004365 if (ret)
4366 return ret;
Zhao Leid7c15172015-02-26 10:49:20 +08004367 /*
Filipe Mananac2d6cb12016-01-15 11:05:12 +00004368 * The cleaner kthread might still be doing iput
4369 * operations. Wait for it to finish so that
4370 * more space is released.
Zhao Leid7c15172015-02-26 10:49:20 +08004371 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004372 mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
4373 mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
Zhao Lei94b947b2015-02-14 13:23:45 +08004374 goto again;
4375 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004376 btrfs_end_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004377 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004378 }
4379
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004380 trace_btrfs_space_reservation(fs_info,
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004381 "space_info:enospc",
4382 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004383 return -ENOSPC;
4384 }
4385 data_sinfo->bytes_may_use += bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004386 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004387 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004388 spin_unlock(&data_sinfo->lock);
4389
Dongsheng Yang237c0e92014-12-29 06:23:05 -05004390 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004391}
4392
Qu Wenruo364ecf32017-02-27 15:10:38 +08004393int btrfs_check_data_free_space(struct inode *inode,
4394 struct extent_changeset **reserved, u64 start, u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004395{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004396 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004397 int ret;
4398
4399 /* align the range */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004400 len = round_up(start + len, fs_info->sectorsize) -
4401 round_down(start, fs_info->sectorsize);
4402 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004403
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004404 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004405 if (ret < 0)
4406 return ret;
4407
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004408 /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
Qu Wenruo364ecf32017-02-27 15:10:38 +08004409 ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
Qu Wenruo7bc329c2017-02-27 15:10:36 +08004410 if (ret < 0)
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004411 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004412 else
4413 ret = 0;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004414 return ret;
4415}
4416
4417/*
Qu Wenruo4ceff072015-09-08 17:22:42 +08004418 * Called if we need to clear a data reservation for this inode
4419 * Normally in a error case.
4420 *
Qu Wenruo51773be2015-10-08 18:19:37 +08004421 * This one will *NOT* use accurate qgroup reserved space API, just for case
4422 * which we can't sleep and is sure it won't affect qgroup reserved space.
4423 * Like clear_bit_hook().
Qu Wenruo4ceff072015-09-08 17:22:42 +08004424 */
Qu Wenruo51773be2015-10-08 18:19:37 +08004425void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
4426 u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004427{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004428 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004429 struct btrfs_space_info *data_sinfo;
4430
4431 /* Make sure the range is aligned to sectorsize */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004432 len = round_up(start + len, fs_info->sectorsize) -
4433 round_down(start, fs_info->sectorsize);
4434 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004435
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004436 data_sinfo = fs_info->data_sinfo;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004437 spin_lock(&data_sinfo->lock);
4438 if (WARN_ON(data_sinfo->bytes_may_use < len))
4439 data_sinfo->bytes_may_use = 0;
4440 else
4441 data_sinfo->bytes_may_use -= len;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004442 trace_btrfs_space_reservation(fs_info, "space_info",
Qu Wenruo4ceff072015-09-08 17:22:42 +08004443 data_sinfo->flags, len, 0);
4444 spin_unlock(&data_sinfo->lock);
4445}
4446
Qu Wenruo51773be2015-10-08 18:19:37 +08004447/*
4448 * Called if we need to clear a data reservation for this inode
4449 * Normally in a error case.
4450 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04004451 * This one will handle the per-inode data rsv map for accurate reserved
Qu Wenruo51773be2015-10-08 18:19:37 +08004452 * space framework.
4453 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08004454void btrfs_free_reserved_data_space(struct inode *inode,
4455 struct extent_changeset *reserved, u64 start, u64 len)
Qu Wenruo51773be2015-10-08 18:19:37 +08004456{
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004457 struct btrfs_root *root = BTRFS_I(inode)->root;
4458
4459 /* Make sure the range is aligned to sectorsize */
Jeff Mahoneyda170662016-06-15 09:22:56 -04004460 len = round_up(start + len, root->fs_info->sectorsize) -
4461 round_down(start, root->fs_info->sectorsize);
4462 start = round_down(start, root->fs_info->sectorsize);
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004463
Qu Wenruo51773be2015-10-08 18:19:37 +08004464 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruobc42bda2017-02-27 15:10:39 +08004465 btrfs_qgroup_free_data(inode, reserved, start, len);
Qu Wenruo51773be2015-10-08 18:19:37 +08004466}
4467
Josef Bacik97e728d2009-04-21 17:40:57 -04004468static void force_metadata_allocation(struct btrfs_fs_info *info)
4469{
4470 struct list_head *head = &info->space_info;
4471 struct btrfs_space_info *found;
4472
4473 rcu_read_lock();
4474 list_for_each_entry_rcu(found, head, list) {
4475 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004476 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04004477 }
4478 rcu_read_unlock();
4479}
4480
Miao Xie3c76cd82013-04-25 10:12:38 +00004481static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
4482{
4483 return (global->size << 1);
4484}
4485
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004486static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
Josef Bacik698d0082012-09-12 14:08:47 -04004487 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04004488{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004489 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004490 u64 bytes_used = btrfs_space_info_used(sinfo, false);
Chris Masone5bc2452010-10-26 13:37:56 -04004491 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04004492
Chris Mason0e4f8f82011-04-15 16:05:44 -04004493 if (force == CHUNK_ALLOC_FORCE)
4494 return 1;
4495
4496 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04004497 * We need to take into account the global rsv because for all intents
4498 * and purposes it's used space. Don't worry about locking the
4499 * global_rsv, it doesn't change except when the transaction commits.
4500 */
Josef Bacik54338b52012-08-14 16:20:52 -04004501 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004502 bytes_used += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04004503
4504 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04004505 * in limited mode, we want to have some free space up to
4506 * about 1% of the FS size.
4507 */
4508 if (force == CHUNK_ALLOC_LIMITED) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004509 thresh = btrfs_super_total_bytes(fs_info->super_copy);
Byongho Leeee221842015-12-15 01:42:10 +09004510 thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
Chris Mason0e4f8f82011-04-15 16:05:44 -04004511
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004512 if (sinfo->total_bytes - bytes_used < thresh)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004513 return 1;
4514 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004515
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004516 if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04004517 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04004518 return 1;
4519}
4520
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004521static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004522{
4523 u64 num_dev;
4524
David Woodhouse53b381b2013-01-29 18:40:14 -05004525 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
4526 BTRFS_BLOCK_GROUP_RAID0 |
4527 BTRFS_BLOCK_GROUP_RAID5 |
4528 BTRFS_BLOCK_GROUP_RAID6))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004529 num_dev = fs_info->fs_devices->rw_devices;
Liu Bo15d1ff82012-03-29 09:57:44 -04004530 else if (type & BTRFS_BLOCK_GROUP_RAID1)
4531 num_dev = 2;
4532 else
4533 num_dev = 1; /* DUP or single */
4534
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004535 return num_dev;
Liu Bo15d1ff82012-03-29 09:57:44 -04004536}
4537
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004538/*
4539 * If @is_allocation is true, reserve space in the system space info necessary
4540 * for allocating a chunk, otherwise if it's false, reserve space necessary for
4541 * removing a chunk.
4542 */
4543void check_system_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004544 struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004545{
4546 struct btrfs_space_info *info;
4547 u64 left;
4548 u64 thresh;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004549 int ret = 0;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004550 u64 num_devs;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004551
4552 /*
4553 * Needed because we can end up allocating a system chunk and for an
4554 * atomic and race free space reservation in the chunk block reserve.
4555 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01004556 lockdep_assert_held(&fs_info->chunk_mutex);
Liu Bo15d1ff82012-03-29 09:57:44 -04004557
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004558 info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
Liu Bo15d1ff82012-03-29 09:57:44 -04004559 spin_lock(&info->lock);
Liu Bo41361352017-02-13 15:42:21 -08004560 left = info->total_bytes - btrfs_space_info_used(info, true);
Liu Bo15d1ff82012-03-29 09:57:44 -04004561 spin_unlock(&info->lock);
4562
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004563 num_devs = get_profile_num_devs(fs_info, type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004564
4565 /* num_devs device items to update and 1 chunk item to add or remove */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004566 thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) +
4567 btrfs_calc_trans_metadata_size(fs_info, 1);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004568
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004569 if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
4570 btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
4571 left, thresh, type);
4572 dump_space_info(fs_info, info, 0, 0);
Liu Bo15d1ff82012-03-29 09:57:44 -04004573 }
4574
4575 if (left < thresh) {
Jeff Mahoney1b868262017-05-17 11:38:35 -04004576 u64 flags = btrfs_system_alloc_profile(fs_info);
Liu Bo15d1ff82012-03-29 09:57:44 -04004577
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004578 /*
4579 * Ignore failure to create system chunk. We might end up not
4580 * needing it, as we might not need to COW all nodes/leafs from
4581 * the paths we visit in the chunk tree (they were already COWed
4582 * or created in the current transaction for example).
4583 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004584 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004585 }
4586
4587 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004588 ret = btrfs_block_rsv_add(fs_info->chunk_root,
4589 &fs_info->chunk_block_rsv,
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004590 thresh, BTRFS_RESERVE_NO_FLUSH);
4591 if (!ret)
4592 trans->chunk_bytes_reserved += thresh;
Liu Bo15d1ff82012-03-29 09:57:44 -04004593 }
4594}
4595
Liu Bo28b737f2016-07-29 11:09:50 -07004596/*
4597 * If force is CHUNK_ALLOC_FORCE:
4598 * - return 1 if it successfully allocates a chunk,
4599 * - return errors including -ENOSPC otherwise.
4600 * If force is NOT CHUNK_ALLOC_FORCE:
4601 * - return 0 if it doesn't need to allocate a new chunk,
4602 * - return 1 if it successfully allocates a chunk,
4603 * - return errors including -ENOSPC otherwise.
4604 */
Chris Mason6324fbf2008-03-24 15:01:59 -04004605static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004606 struct btrfs_fs_info *fs_info, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04004607{
4608 struct btrfs_space_info *space_info;
Josef Bacik6d741192011-04-11 20:20:11 -04004609 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05004610 int ret = 0;
4611
Josef Bacikc6b305a2012-12-18 09:16:16 -05004612 /* Don't re-enter if we're already allocating a chunk */
4613 if (trans->allocating_chunk)
4614 return -ENOSPC;
4615
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004616 space_info = __find_space_info(fs_info, flags);
Jeff Mahoneydc2d3002018-03-20 15:25:25 -04004617 ASSERT(space_info);
Chris Mason6324fbf2008-03-24 15:01:59 -04004618
Josef Bacik6d741192011-04-11 20:20:11 -04004619again:
Josef Bacik25179202008-10-29 14:49:05 -04004620 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05004621 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004622 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04004623 if (space_info->full) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004624 if (should_alloc_chunk(fs_info, space_info, force))
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004625 ret = -ENOSPC;
4626 else
4627 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04004628 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004629 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04004630 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004631
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004632 if (!should_alloc_chunk(fs_info, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04004633 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04004634 return 0;
4635 } else if (space_info->chunk_alloc) {
4636 wait_for_alloc = 1;
4637 } else {
4638 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04004639 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004640
Josef Bacik25179202008-10-29 14:49:05 -04004641 spin_unlock(&space_info->lock);
4642
Josef Bacik6d741192011-04-11 20:20:11 -04004643 mutex_lock(&fs_info->chunk_mutex);
4644
4645 /*
4646 * The chunk_mutex is held throughout the entirety of a chunk
4647 * allocation, so once we've acquired the chunk_mutex we know that the
4648 * other guy is done and we need to recheck and see if we should
4649 * allocate.
4650 */
4651 if (wait_for_alloc) {
4652 mutex_unlock(&fs_info->chunk_mutex);
4653 wait_for_alloc = 0;
Nikolay Borisov1e1c50a2018-04-05 10:40:15 +03004654 cond_resched();
Josef Bacik6d741192011-04-11 20:20:11 -04004655 goto again;
4656 }
4657
Josef Bacikc6b305a2012-12-18 09:16:16 -05004658 trans->allocating_chunk = true;
4659
Josef Bacik97e728d2009-04-21 17:40:57 -04004660 /*
Josef Bacik67377732010-09-16 16:19:09 -04004661 * If we have mixed data/metadata chunks we want to make sure we keep
4662 * allocating mixed chunks instead of individual chunks.
4663 */
4664 if (btrfs_mixed_space_info(space_info))
4665 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
4666
4667 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04004668 * if we're doing a data chunk, go ahead and make sure that
4669 * we keep a reasonable number of metadata chunks allocated in the
4670 * FS as well.
4671 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04004672 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04004673 fs_info->data_chunk_allocations++;
4674 if (!(fs_info->data_chunk_allocations %
4675 fs_info->metadata_ratio))
4676 force_metadata_allocation(fs_info);
4677 }
4678
Liu Bo15d1ff82012-03-29 09:57:44 -04004679 /*
4680 * Check if we have enough space in SYSTEM chunk because we may need
4681 * to update devices.
4682 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004683 check_system_chunk(trans, fs_info, flags);
Liu Bo15d1ff82012-03-29 09:57:44 -04004684
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004685 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05004686 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004687
Josef Bacik9ed74f22009-09-11 16:12:44 -04004688 spin_lock(&space_info->lock);
Nikolay Borisov57f16422018-04-11 11:21:19 +03004689 if (ret < 0) {
4690 if (ret == -ENOSPC)
4691 space_info->full = 1;
4692 else
4693 goto out;
4694 } else {
Yan, Zheng424499d2010-05-16 10:46:25 -04004695 ret = 1;
Nikolay Borisov57f16422018-04-11 11:21:19 +03004696 }
Josef Bacik6d741192011-04-11 20:20:11 -04004697
Chris Mason0e4f8f82011-04-15 16:05:44 -04004698 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004699out:
Josef Bacik6d741192011-04-11 20:20:11 -04004700 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004701 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03004702 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana00d80e32015-07-20 14:56:20 +01004703 /*
4704 * When we allocate a new chunk we reserve space in the chunk block
4705 * reserve to make sure we can COW nodes/leafs in the chunk tree or
4706 * add new nodes/leafs to it if we end up needing to do it when
4707 * inserting the chunk item and updating device items as part of the
4708 * second phase of chunk allocation, performed by
4709 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
4710 * large number of new block groups to create in our transaction
4711 * handle's new_bgs list to avoid exhausting the chunk block reserve
4712 * in extreme cases - like having a single transaction create many new
4713 * block groups when starting to write out the free space caches of all
4714 * the block groups that were made dirty during the lifetime of the
4715 * transaction.
4716 */
Filipe Mananad9a05402015-10-03 13:13:13 +01004717 if (trans->can_flush_pending_bgs &&
Byongho Leeee221842015-12-15 01:42:10 +09004718 trans->chunk_bytes_reserved >= (u64)SZ_2M) {
Nikolay Borisov6c686b32018-02-07 17:55:40 +02004719 btrfs_create_pending_block_groups(trans);
Filipe Manana00d80e32015-07-20 14:56:20 +01004720 btrfs_trans_release_chunk_metadata(trans);
4721 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004722 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04004723}
4724
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004725static int can_overcommit(struct btrfs_fs_info *fs_info,
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004726 struct btrfs_space_info *space_info, u64 bytes,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004727 enum btrfs_reserve_flush_enum flush,
4728 bool system_chunk)
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004729{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004730 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04004731 u64 profile;
Miao Xie3c76cd82013-04-25 10:12:38 +00004732 u64 space_size;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004733 u64 avail;
4734 u64 used;
4735
Josef Bacik957780e2016-05-17 13:30:55 -04004736 /* Don't overcommit when in mixed mode. */
4737 if (space_info->flags & BTRFS_BLOCK_GROUP_DATA)
4738 return 0;
4739
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004740 if (system_chunk)
4741 profile = btrfs_system_alloc_profile(fs_info);
4742 else
4743 profile = btrfs_metadata_alloc_profile(fs_info);
4744
Liu Bo41361352017-02-13 15:42:21 -08004745 used = btrfs_space_info_used(space_info, false);
Josef Bacik96f1bb52013-01-30 17:02:51 -05004746
Josef Bacik96f1bb52013-01-30 17:02:51 -05004747 /*
4748 * We only want to allow over committing if we have lots of actual space
4749 * free, but if we don't have enough space to handle the global reserve
4750 * space then we could end up having a real enospc problem when trying
4751 * to allocate a chunk or some other such important allocation.
4752 */
Miao Xie3c76cd82013-04-25 10:12:38 +00004753 spin_lock(&global_rsv->lock);
4754 space_size = calc_global_rsv_need_space(global_rsv);
4755 spin_unlock(&global_rsv->lock);
4756 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05004757 return 0;
4758
4759 used += space_info->bytes_may_use;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004760
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004761 avail = atomic64_read(&fs_info->free_chunk_space);
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004762
4763 /*
4764 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05004765 * space is actually useable. For raid56, the space info used
4766 * doesn't include the parity drive, so we don't have to
4767 * change the math
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004768 */
4769 if (profile & (BTRFS_BLOCK_GROUP_DUP |
4770 BTRFS_BLOCK_GROUP_RAID1 |
4771 BTRFS_BLOCK_GROUP_RAID10))
4772 avail >>= 1;
4773
4774 /*
Miao Xie561c2942012-10-16 11:32:18 +00004775 * If we aren't flushing all things, let us overcommit up to
4776 * 1/2th of the space. If we can flush, don't let us overcommit
4777 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004778 */
Miao Xie08e007d2012-10-16 11:33:38 +00004779 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04004780 avail >>= 3;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004781 else
Josef Bacik14575ae2013-09-17 10:48:00 -04004782 avail >>= 1;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004783
Josef Bacik14575ae2013-09-17 10:48:00 -04004784 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004785 return 1;
4786 return 0;
4787}
4788
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004789static void btrfs_writeback_inodes_sb_nr(struct btrfs_fs_info *fs_info,
Miao Xie6c255e62014-03-06 13:55:01 +08004790 unsigned long nr_pages, int nr_items)
Miao Xieda633a42012-12-20 11:19:09 +00004791{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004792 struct super_block *sb = fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00004793
Josef Bacik925a6ef2013-06-20 12:31:27 -04004794 if (down_read_trylock(&sb->s_umount)) {
4795 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4796 up_read(&sb->s_umount);
4797 } else {
Miao Xieda633a42012-12-20 11:19:09 +00004798 /*
4799 * We needn't worry the filesystem going from r/w to r/o though
4800 * we don't acquire ->s_umount mutex, because the filesystem
4801 * should guarantee the delalloc inodes list be empty after
4802 * the filesystem is readonly(all dirty pages are written to
4803 * the disk).
4804 */
Nikolay Borisov82b3e532018-04-23 10:54:13 +03004805 btrfs_start_delalloc_roots(fs_info, nr_items);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004806 if (!current->journal_info)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004807 btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1);
Miao Xieda633a42012-12-20 11:19:09 +00004808 }
4809}
4810
Chris Mason6374e57a2017-06-23 09:48:21 -07004811static inline u64 calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004812 u64 to_reclaim)
Miao Xie18cd8ea2013-11-04 23:13:22 +08004813{
4814 u64 bytes;
Chris Mason6374e57a2017-06-23 09:48:21 -07004815 u64 nr;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004816
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004817 bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Chris Mason6374e57a2017-06-23 09:48:21 -07004818 nr = div64_u64(to_reclaim, bytes);
Miao Xie18cd8ea2013-11-04 23:13:22 +08004819 if (!nr)
4820 nr = 1;
4821 return nr;
4822}
4823
Byongho Leeee221842015-12-15 01:42:10 +09004824#define EXTENT_SIZE_PER_ITEM SZ_256K
Miao Xiec61a16a2013-11-04 23:13:23 +08004825
Yan, Zheng5da9d012010-05-16 10:46:25 -04004826/*
4827 * shrink metadata reservation for delalloc
4828 */
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004829static void shrink_delalloc(struct btrfs_fs_info *fs_info, u64 to_reclaim,
4830 u64 orig, bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004831{
Josef Bacik0019f102010-10-15 15:18:40 -04004832 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004833 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004834 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004835 u64 max_reclaim;
Chris Mason6374e57a2017-06-23 09:48:21 -07004836 u64 items;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004837 long time_left;
Miao Xied3ee29e32013-11-04 23:13:20 +08004838 unsigned long nr_pages;
4839 int loops;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004840
Miao Xiec61a16a2013-11-04 23:13:23 +08004841 /* Calc the number of the pages we need flush for space reservation */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004842 items = calc_reclaim_items_nr(fs_info, to_reclaim);
Chris Mason6374e57a2017-06-23 09:48:21 -07004843 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004844
Josef Bacik663350a2011-11-03 22:54:25 -04004845 trans = (struct btrfs_trans_handle *)current->journal_info;
Josef Bacik69fe2d72017-10-19 14:15:57 -04004846 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Chris Masonbf9022e2010-10-26 13:40:45 -04004847
Miao Xie963d6782013-01-29 10:10:51 +00004848 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004849 &fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004850 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004851 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004852 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004853 if (wait_ordered)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004854 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004855 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004856 }
4857
Miao Xied3ee29e32013-11-04 23:13:20 +08004858 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004859 while (delalloc_bytes && loops < 3) {
4860 max_reclaim = min(delalloc_bytes, to_reclaim);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004861 nr_pages = max_reclaim >> PAGE_SHIFT;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004862 btrfs_writeback_inodes_sb_nr(fs_info, nr_pages, items);
Josef Bacikdea31f52012-09-06 16:47:00 -04004863 /*
4864 * We need to wait for the async pages to actually start before
4865 * we do anything.
4866 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004867 max_reclaim = atomic_read(&fs_info->async_delalloc_pages);
Miao Xie9f3a0742013-11-04 23:13:24 +08004868 if (!max_reclaim)
4869 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004870
Miao Xie9f3a0742013-11-04 23:13:24 +08004871 if (max_reclaim <= nr_pages)
4872 max_reclaim = 0;
4873 else
4874 max_reclaim -= nr_pages;
4875
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004876 wait_event(fs_info->async_submit_wait,
4877 atomic_read(&fs_info->async_delalloc_pages) <=
Miao Xie9f3a0742013-11-04 23:13:24 +08004878 (int)max_reclaim);
4879skip_async:
Josef Bacik0019f102010-10-15 15:18:40 -04004880 spin_lock(&space_info->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04004881 if (list_empty(&space_info->tickets) &&
4882 list_empty(&space_info->priority_tickets)) {
4883 spin_unlock(&space_info->lock);
4884 break;
4885 }
Josef Bacik0019f102010-10-15 15:18:40 -04004886 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004887
Chris Mason36e39c42011-03-12 07:08:42 -05004888 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004889 if (wait_ordered && !trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004890 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf104d042011-10-14 13:56:58 -04004891 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004892 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004893 if (time_left)
4894 break;
4895 }
Miao Xie963d6782013-01-29 10:10:51 +00004896 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004897 &fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004898 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004899}
4900
Josef Bacik996478c2017-08-22 16:00:39 -04004901struct reserve_ticket {
4902 u64 bytes;
4903 int error;
4904 struct list_head list;
4905 wait_queue_head_t wait;
4906};
4907
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004908/**
Josef Bacik663350a2011-11-03 22:54:25 -04004909 * maybe_commit_transaction - possibly commit the transaction if its ok to
4910 * @root - the root we're allocating for
4911 * @bytes - the number of bytes we want to reserve
4912 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004913 *
Josef Bacik663350a2011-11-03 22:54:25 -04004914 * This will check to make sure that committing the transaction will actually
4915 * get us somewhere and then commit the transaction if it does. Otherwise it
4916 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004917 */
Jeff Mahoney0c9ab342017-02-15 16:28:28 -05004918static int may_commit_transaction(struct btrfs_fs_info *fs_info,
Josef Bacik996478c2017-08-22 16:00:39 -04004919 struct btrfs_space_info *space_info)
Josef Bacik663350a2011-11-03 22:54:25 -04004920{
Josef Bacik996478c2017-08-22 16:00:39 -04004921 struct reserve_ticket *ticket = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004922 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;
Josef Bacik663350a2011-11-03 22:54:25 -04004923 struct btrfs_trans_handle *trans;
Josef Bacik996478c2017-08-22 16:00:39 -04004924 u64 bytes;
Josef Bacik663350a2011-11-03 22:54:25 -04004925
4926 trans = (struct btrfs_trans_handle *)current->journal_info;
4927 if (trans)
4928 return -EAGAIN;
4929
Josef Bacik996478c2017-08-22 16:00:39 -04004930 spin_lock(&space_info->lock);
4931 if (!list_empty(&space_info->priority_tickets))
4932 ticket = list_first_entry(&space_info->priority_tickets,
4933 struct reserve_ticket, list);
4934 else if (!list_empty(&space_info->tickets))
4935 ticket = list_first_entry(&space_info->tickets,
4936 struct reserve_ticket, list);
4937 bytes = (ticket) ? ticket->bytes : 0;
4938 spin_unlock(&space_info->lock);
4939
4940 if (!bytes)
4941 return 0;
Josef Bacik663350a2011-11-03 22:54:25 -04004942
4943 /* See if there is enough pinned space to make this reservation */
Josef Bacikb150a4f2013-06-19 15:00:04 -04004944 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Miao Xie0424c542014-03-06 13:54:59 +08004945 bytes) >= 0)
Josef Bacik663350a2011-11-03 22:54:25 -04004946 goto commit;
Josef Bacik663350a2011-11-03 22:54:25 -04004947
4948 /*
4949 * See if there is some space in the delayed insertion reservation for
4950 * this reservation.
4951 */
4952 if (space_info != delayed_rsv->space_info)
4953 return -ENOSPC;
4954
4955 spin_lock(&delayed_rsv->lock);
Josef Bacik996478c2017-08-22 16:00:39 -04004956 if (delayed_rsv->size > bytes)
4957 bytes = 0;
4958 else
4959 bytes -= delayed_rsv->size;
Nikolay Borisov057aac32017-11-07 11:22:54 +02004960 spin_unlock(&delayed_rsv->lock);
4961
Josef Bacikb150a4f2013-06-19 15:00:04 -04004962 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Josef Bacik996478c2017-08-22 16:00:39 -04004963 bytes) < 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004964 return -ENOSPC;
4965 }
Josef Bacik663350a2011-11-03 22:54:25 -04004966
4967commit:
Jeff Mahoneya9b33112017-05-17 11:38:34 -04004968 trans = btrfs_join_transaction(fs_info->extent_root);
Josef Bacik663350a2011-11-03 22:54:25 -04004969 if (IS_ERR(trans))
4970 return -ENOSPC;
4971
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004972 return btrfs_commit_transaction(trans);
Josef Bacik663350a2011-11-03 22:54:25 -04004973}
4974
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03004975/*
4976 * Try to flush some data based on policy set by @state. This is only advisory
4977 * and may fail for various reasons. The caller is supposed to examine the
4978 * state of @space_info to detect the outcome.
4979 */
4980static void flush_space(struct btrfs_fs_info *fs_info,
Josef Bacik96c3f432012-06-21 14:05:49 -04004981 struct btrfs_space_info *space_info, u64 num_bytes,
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03004982 int state)
Josef Bacik96c3f432012-06-21 14:05:49 -04004983{
Jeff Mahoneya9b33112017-05-17 11:38:34 -04004984 struct btrfs_root *root = fs_info->extent_root;
Josef Bacik96c3f432012-06-21 14:05:49 -04004985 struct btrfs_trans_handle *trans;
4986 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004987 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004988
4989 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004990 case FLUSH_DELAYED_ITEMS_NR:
4991 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08004992 if (state == FLUSH_DELAYED_ITEMS_NR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004993 nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004994 else
Josef Bacik96c3f432012-06-21 14:05:49 -04004995 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004996
Josef Bacik96c3f432012-06-21 14:05:49 -04004997 trans = btrfs_join_transaction(root);
4998 if (IS_ERR(trans)) {
4999 ret = PTR_ERR(trans);
5000 break;
5001 }
Nikolay Borisove5c304e62018-02-07 17:55:43 +02005002 ret = btrfs_run_delayed_items_nr(trans, nr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005003 btrfs_end_transaction(trans);
Josef Bacik96c3f432012-06-21 14:05:49 -04005004 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04005005 case FLUSH_DELALLOC:
5006 case FLUSH_DELALLOC_WAIT:
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005007 shrink_delalloc(fs_info, num_bytes * 2, num_bytes,
Josef Bacik67b0fd62012-09-24 13:42:00 -04005008 state == FLUSH_DELALLOC_WAIT);
5009 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04005010 case ALLOC_CHUNK:
5011 trans = btrfs_join_transaction(root);
5012 if (IS_ERR(trans)) {
5013 ret = PTR_ERR(trans);
5014 break;
5015 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005016 ret = do_chunk_alloc(trans, fs_info,
Jeff Mahoney1b868262017-05-17 11:38:35 -04005017 btrfs_metadata_alloc_profile(fs_info),
Josef Bacikea658ba2012-09-11 16:57:25 -04005018 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005019 btrfs_end_transaction(trans);
Alex Lyakaseecba892015-12-06 12:32:31 +02005020 if (ret > 0 || ret == -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04005021 ret = 0;
5022 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04005023 case COMMIT_TRANS:
Josef Bacik996478c2017-08-22 16:00:39 -04005024 ret = may_commit_transaction(fs_info, space_info);
Josef Bacik96c3f432012-06-21 14:05:49 -04005025 break;
5026 default:
5027 ret = -ENOSPC;
5028 break;
5029 }
5030
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005031 trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, state,
5032 ret);
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03005033 return;
Josef Bacik96c3f432012-06-21 14:05:49 -04005034}
Miao Xie21c7e752014-05-13 17:29:04 -07005035
5036static inline u64
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005037btrfs_calc_reclaim_metadata_size(struct btrfs_fs_info *fs_info,
5038 struct btrfs_space_info *space_info,
5039 bool system_chunk)
Miao Xie21c7e752014-05-13 17:29:04 -07005040{
Josef Bacik957780e2016-05-17 13:30:55 -04005041 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07005042 u64 used;
5043 u64 expected;
Josef Bacik957780e2016-05-17 13:30:55 -04005044 u64 to_reclaim = 0;
Miao Xie21c7e752014-05-13 17:29:04 -07005045
Josef Bacik957780e2016-05-17 13:30:55 -04005046 list_for_each_entry(ticket, &space_info->tickets, list)
5047 to_reclaim += ticket->bytes;
5048 list_for_each_entry(ticket, &space_info->priority_tickets, list)
5049 to_reclaim += ticket->bytes;
5050 if (to_reclaim)
5051 return to_reclaim;
Miao Xie21c7e752014-05-13 17:29:04 -07005052
Wang Xiaoguange0af2482016-08-31 19:46:16 +08005053 to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005054 if (can_overcommit(fs_info, space_info, to_reclaim,
5055 BTRFS_RESERVE_FLUSH_ALL, system_chunk))
Wang Xiaoguange0af2482016-08-31 19:46:16 +08005056 return 0;
5057
Nikolay Borisov0eee8a42017-06-14 11:35:34 +03005058 used = btrfs_space_info_used(space_info, true);
5059
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005060 if (can_overcommit(fs_info, space_info, SZ_1M,
5061 BTRFS_RESERVE_FLUSH_ALL, system_chunk))
Miao Xie21c7e752014-05-13 17:29:04 -07005062 expected = div_factor_fine(space_info->total_bytes, 95);
5063 else
5064 expected = div_factor_fine(space_info->total_bytes, 90);
5065
5066 if (used > expected)
5067 to_reclaim = used - expected;
5068 else
5069 to_reclaim = 0;
5070 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
5071 space_info->bytes_reserved);
Miao Xie21c7e752014-05-13 17:29:04 -07005072 return to_reclaim;
5073}
5074
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005075static inline int need_do_async_reclaim(struct btrfs_fs_info *fs_info,
5076 struct btrfs_space_info *space_info,
5077 u64 used, bool system_chunk)
Miao Xie21c7e752014-05-13 17:29:04 -07005078{
Josef Bacik365c5312015-02-18 13:58:15 -08005079 u64 thresh = div_factor_fine(space_info->total_bytes, 98);
5080
5081 /* If we're just plain full then async reclaim just slows us down. */
Josef Bacikbaee8792016-01-26 09:35:38 -05005082 if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh)
Josef Bacik365c5312015-02-18 13:58:15 -08005083 return 0;
5084
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005085 if (!btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5086 system_chunk))
Liu Bo25ce4592014-09-10 12:58:50 +08005087 return 0;
Liu Bo25ce4592014-09-10 12:58:50 +08005088
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005089 return (used >= thresh && !btrfs_fs_closing(fs_info) &&
5090 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
Miao Xie21c7e752014-05-13 17:29:04 -07005091}
5092
Josef Bacik957780e2016-05-17 13:30:55 -04005093static void wake_all_tickets(struct list_head *head)
Miao Xie21c7e752014-05-13 17:29:04 -07005094{
Josef Bacik957780e2016-05-17 13:30:55 -04005095 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07005096
Josef Bacik957780e2016-05-17 13:30:55 -04005097 while (!list_empty(head)) {
5098 ticket = list_first_entry(head, struct reserve_ticket, list);
5099 list_del_init(&ticket->list);
5100 ticket->error = -ENOSPC;
5101 wake_up(&ticket->wait);
Miao Xie21c7e752014-05-13 17:29:04 -07005102 }
Miao Xie21c7e752014-05-13 17:29:04 -07005103}
5104
Josef Bacik957780e2016-05-17 13:30:55 -04005105/*
5106 * This is for normal flushers, we can wait all goddamned day if we want to. We
5107 * will loop and continuously try to flush as long as we are making progress.
5108 * We count progress as clearing off tickets each time we have to loop.
5109 */
Miao Xie21c7e752014-05-13 17:29:04 -07005110static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
5111{
5112 struct btrfs_fs_info *fs_info;
5113 struct btrfs_space_info *space_info;
5114 u64 to_reclaim;
5115 int flush_state;
Josef Bacik957780e2016-05-17 13:30:55 -04005116 int commit_cycles = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005117 u64 last_tickets_id;
Miao Xie21c7e752014-05-13 17:29:04 -07005118
5119 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
5120 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
5121
Josef Bacik957780e2016-05-17 13:30:55 -04005122 spin_lock(&space_info->lock);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005123 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5124 false);
Josef Bacik957780e2016-05-17 13:30:55 -04005125 if (!to_reclaim) {
5126 space_info->flush = 0;
5127 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005128 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005129 }
Wang Xiaoguangce129652016-09-02 10:58:46 +08005130 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005131 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005132
5133 flush_state = FLUSH_DELAYED_ITEMS_NR;
5134 do {
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03005135 flush_space(fs_info, space_info, to_reclaim, flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005136 spin_lock(&space_info->lock);
5137 if (list_empty(&space_info->tickets)) {
5138 space_info->flush = 0;
5139 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005140 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005141 }
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005142 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info,
5143 space_info,
5144 false);
Wang Xiaoguangce129652016-09-02 10:58:46 +08005145 if (last_tickets_id == space_info->tickets_id) {
Josef Bacik957780e2016-05-17 13:30:55 -04005146 flush_state++;
5147 } else {
Wang Xiaoguangce129652016-09-02 10:58:46 +08005148 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005149 flush_state = FLUSH_DELAYED_ITEMS_NR;
5150 if (commit_cycles)
5151 commit_cycles--;
5152 }
5153
5154 if (flush_state > COMMIT_TRANS) {
5155 commit_cycles++;
5156 if (commit_cycles > 2) {
5157 wake_all_tickets(&space_info->tickets);
5158 space_info->flush = 0;
5159 } else {
5160 flush_state = FLUSH_DELAYED_ITEMS_NR;
5161 }
5162 }
5163 spin_unlock(&space_info->lock);
5164 } while (flush_state <= COMMIT_TRANS);
Miao Xie21c7e752014-05-13 17:29:04 -07005165}
5166
5167void btrfs_init_async_reclaim_work(struct work_struct *work)
5168{
5169 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
5170}
5171
Josef Bacik957780e2016-05-17 13:30:55 -04005172static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
5173 struct btrfs_space_info *space_info,
5174 struct reserve_ticket *ticket)
5175{
5176 u64 to_reclaim;
5177 int flush_state = FLUSH_DELAYED_ITEMS_NR;
5178
5179 spin_lock(&space_info->lock);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005180 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5181 false);
Josef Bacik957780e2016-05-17 13:30:55 -04005182 if (!to_reclaim) {
5183 spin_unlock(&space_info->lock);
5184 return;
5185 }
5186 spin_unlock(&space_info->lock);
5187
5188 do {
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005189 flush_space(fs_info, space_info, to_reclaim, flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005190 flush_state++;
5191 spin_lock(&space_info->lock);
5192 if (ticket->bytes == 0) {
5193 spin_unlock(&space_info->lock);
5194 return;
5195 }
5196 spin_unlock(&space_info->lock);
5197
5198 /*
5199 * Priority flushers can't wait on delalloc without
5200 * deadlocking.
5201 */
5202 if (flush_state == FLUSH_DELALLOC ||
5203 flush_state == FLUSH_DELALLOC_WAIT)
5204 flush_state = ALLOC_CHUNK;
5205 } while (flush_state < COMMIT_TRANS);
5206}
5207
5208static int wait_reserve_ticket(struct btrfs_fs_info *fs_info,
5209 struct btrfs_space_info *space_info,
5210 struct reserve_ticket *ticket, u64 orig_bytes)
5211
5212{
5213 DEFINE_WAIT(wait);
5214 int ret = 0;
5215
5216 spin_lock(&space_info->lock);
5217 while (ticket->bytes > 0 && ticket->error == 0) {
5218 ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE);
5219 if (ret) {
5220 ret = -EINTR;
5221 break;
5222 }
5223 spin_unlock(&space_info->lock);
5224
5225 schedule();
5226
5227 finish_wait(&ticket->wait, &wait);
5228 spin_lock(&space_info->lock);
5229 }
5230 if (!ret)
5231 ret = ticket->error;
5232 if (!list_empty(&ticket->list))
5233 list_del_init(&ticket->list);
5234 if (ticket->bytes && ticket->bytes < orig_bytes) {
5235 u64 num_bytes = orig_bytes - ticket->bytes;
5236 space_info->bytes_may_use -= num_bytes;
5237 trace_btrfs_space_reservation(fs_info, "space_info",
5238 space_info->flags, num_bytes, 0);
5239 }
5240 spin_unlock(&space_info->lock);
5241
5242 return ret;
5243}
5244
Josef Bacik663350a2011-11-03 22:54:25 -04005245/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005246 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5247 * @root - the root we're allocating for
Josef Bacik957780e2016-05-17 13:30:55 -04005248 * @space_info - the space info we want to allocate from
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005249 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04005250 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005251 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04005252 * This will reserve orig_bytes number of bytes from the space info associated
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005253 * with the block_rsv. If there is not enough space it will make an attempt to
5254 * flush out space to make room. It will do this by flushing delalloc if
5255 * possible or committing the transaction. If flush is 0 then no attempts to
5256 * regain reservations will be made and this will fail if there is not enough
5257 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005258 */
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005259static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
Josef Bacik957780e2016-05-17 13:30:55 -04005260 struct btrfs_space_info *space_info,
5261 u64 orig_bytes,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005262 enum btrfs_reserve_flush_enum flush,
5263 bool system_chunk)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005264{
Josef Bacik957780e2016-05-17 13:30:55 -04005265 struct reserve_ticket ticket;
Josef Bacik2bf64752011-09-26 17:12:22 -04005266 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005267 int ret = 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005268
Josef Bacik957780e2016-05-17 13:30:55 -04005269 ASSERT(orig_bytes);
Josef Bacik8ca17f02016-05-27 13:24:13 -04005270 ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL);
5271
Yan, Zhengf0486c62010-05-16 10:46:25 -04005272 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04005273 ret = -ENOSPC;
Liu Bo41361352017-02-13 15:42:21 -08005274 used = btrfs_space_info_used(space_info, true);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005275
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005276 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005277 * If we have enough space then hooray, make our reservation and carry
5278 * on. If not see if we can overcommit, and if we can, hooray carry on.
5279 * If not things get more complicated.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005280 */
Josef Bacik957780e2016-05-17 13:30:55 -04005281 if (used + orig_bytes <= space_info->total_bytes) {
5282 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005283 trace_btrfs_space_reservation(fs_info, "space_info",
5284 space_info->flags, orig_bytes, 1);
Josef Bacik957780e2016-05-17 13:30:55 -04005285 ret = 0;
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005286 } else if (can_overcommit(fs_info, space_info, orig_bytes, flush,
5287 system_chunk)) {
Josef Bacik44734ed2012-09-28 16:04:19 -04005288 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005289 trace_btrfs_space_reservation(fs_info, "space_info",
5290 space_info->flags, orig_bytes, 1);
Josef Bacik44734ed2012-09-28 16:04:19 -04005291 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04005292 }
5293
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005294 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005295 * If we couldn't make a reservation then setup our reservation ticket
5296 * and kick the async worker if it's not already running.
Miao Xie08e007d2012-10-16 11:33:38 +00005297 *
Josef Bacik957780e2016-05-17 13:30:55 -04005298 * If we are a priority flusher then we just need to add our ticket to
5299 * the list and we will do our own flushing further down.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005300 */
Josef Bacik72bcd992012-12-18 15:16:34 -05005301 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacik957780e2016-05-17 13:30:55 -04005302 ticket.bytes = orig_bytes;
5303 ticket.error = 0;
5304 init_waitqueue_head(&ticket.wait);
5305 if (flush == BTRFS_RESERVE_FLUSH_ALL) {
5306 list_add_tail(&ticket.list, &space_info->tickets);
5307 if (!space_info->flush) {
5308 space_info->flush = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005309 trace_btrfs_trigger_flush(fs_info,
Josef Bacikf376df22016-03-25 13:25:56 -04005310 space_info->flags,
5311 orig_bytes, flush,
5312 "enospc");
Josef Bacik957780e2016-05-17 13:30:55 -04005313 queue_work(system_unbound_wq,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005314 &fs_info->async_reclaim_work);
Josef Bacik957780e2016-05-17 13:30:55 -04005315 }
5316 } else {
5317 list_add_tail(&ticket.list,
5318 &space_info->priority_tickets);
5319 }
Miao Xie21c7e752014-05-13 17:29:04 -07005320 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
5321 used += orig_bytes;
Josef Bacikf6acfd52014-09-18 11:27:17 -04005322 /*
5323 * We will do the space reservation dance during log replay,
5324 * which means we won't have fs_info->fs_root set, so don't do
5325 * the async reclaim as we will panic.
5326 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005327 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) &&
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005328 need_do_async_reclaim(fs_info, space_info,
5329 used, system_chunk) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005330 !work_busy(&fs_info->async_reclaim_work)) {
5331 trace_btrfs_trigger_flush(fs_info, space_info->flags,
5332 orig_bytes, flush, "preempt");
Miao Xie21c7e752014-05-13 17:29:04 -07005333 queue_work(system_unbound_wq,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005334 &fs_info->async_reclaim_work);
Josef Bacikf376df22016-03-25 13:25:56 -04005335 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005336 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005337 spin_unlock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00005338 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik957780e2016-05-17 13:30:55 -04005339 return ret;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005340
Josef Bacik957780e2016-05-17 13:30:55 -04005341 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005342 return wait_reserve_ticket(fs_info, space_info, &ticket,
Josef Bacik957780e2016-05-17 13:30:55 -04005343 orig_bytes);
Miao Xie08e007d2012-10-16 11:33:38 +00005344
Josef Bacik957780e2016-05-17 13:30:55 -04005345 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005346 priority_reclaim_metadata_space(fs_info, space_info, &ticket);
Josef Bacik957780e2016-05-17 13:30:55 -04005347 spin_lock(&space_info->lock);
5348 if (ticket.bytes) {
5349 if (ticket.bytes < orig_bytes) {
5350 u64 num_bytes = orig_bytes - ticket.bytes;
5351 space_info->bytes_may_use -= num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005352 trace_btrfs_space_reservation(fs_info, "space_info",
5353 space_info->flags,
5354 num_bytes, 0);
Miao Xie08e007d2012-10-16 11:33:38 +00005355
Josef Bacik957780e2016-05-17 13:30:55 -04005356 }
5357 list_del_init(&ticket.list);
5358 ret = -ENOSPC;
5359 }
5360 spin_unlock(&space_info->lock);
5361 ASSERT(list_empty(&ticket.list));
5362 return ret;
5363}
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005364
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005365/**
5366 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5367 * @root - the root we're allocating for
5368 * @block_rsv - the block_rsv we're allocating for
5369 * @orig_bytes - the number of bytes we want
5370 * @flush - whether or not we can flush to make our reservation
5371 *
Josef Bacik957780e2016-05-17 13:30:55 -04005372 * This will reserve orgi_bytes number of bytes from the space info associated
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005373 * with the block_rsv. If there is not enough space it will make an attempt to
5374 * flush out space to make room. It will do this by flushing delalloc if
5375 * possible or committing the transaction. If flush is 0 then no attempts to
5376 * regain reservations will be made and this will fail if there is not enough
5377 * space already.
5378 */
5379static int reserve_metadata_bytes(struct btrfs_root *root,
5380 struct btrfs_block_rsv *block_rsv,
5381 u64 orig_bytes,
5382 enum btrfs_reserve_flush_enum flush)
5383{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005384 struct btrfs_fs_info *fs_info = root->fs_info;
5385 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04005386 int ret;
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005387 bool system_chunk = (root == fs_info->chunk_root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005388
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005389 ret = __reserve_metadata_bytes(fs_info, block_rsv->space_info,
5390 orig_bytes, flush, system_chunk);
Josef Bacik5d803662013-02-07 16:06:02 -05005391 if (ret == -ENOSPC &&
5392 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
Josef Bacik5d803662013-02-07 16:06:02 -05005393 if (block_rsv != global_rsv &&
5394 !block_rsv_use_bytes(global_rsv, orig_bytes))
5395 ret = 0;
5396 }
Nikolay Borisov9a3daff2017-12-15 12:05:37 +02005397 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005398 trace_btrfs_space_reservation(fs_info, "space_info:enospc",
Josef Bacik957780e2016-05-17 13:30:55 -04005399 block_rsv->space_info->flags,
5400 orig_bytes, 1);
Nikolay Borisov9a3daff2017-12-15 12:05:37 +02005401
5402 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG))
5403 dump_space_info(fs_info, block_rsv->space_info,
5404 orig_bytes, 0);
5405 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005406 return ret;
5407}
5408
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005409static struct btrfs_block_rsv *get_block_rsv(
5410 const struct btrfs_trans_handle *trans,
5411 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005412{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005413 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik4c13d752011-08-30 11:31:29 -04005414 struct btrfs_block_rsv *block_rsv = NULL;
5415
Alexandru Moisee9cf4392015-09-09 00:18:50 +00005416 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005417 (root == fs_info->csum_root && trans->adding_csums) ||
5418 (root == fs_info->uuid_root))
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02005419 block_rsv = trans->block_rsv;
5420
Josef Bacik4c13d752011-08-30 11:31:29 -04005421 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005422 block_rsv = root->block_rsv;
5423
5424 if (!block_rsv)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005425 block_rsv = &fs_info->empty_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005426
5427 return block_rsv;
5428}
5429
5430static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
5431 u64 num_bytes)
5432{
5433 int ret = -ENOSPC;
5434 spin_lock(&block_rsv->lock);
5435 if (block_rsv->reserved >= num_bytes) {
5436 block_rsv->reserved -= num_bytes;
5437 if (block_rsv->reserved < block_rsv->size)
5438 block_rsv->full = 0;
5439 ret = 0;
5440 }
5441 spin_unlock(&block_rsv->lock);
5442 return ret;
5443}
5444
5445static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
5446 u64 num_bytes, int update_size)
5447{
5448 spin_lock(&block_rsv->lock);
5449 block_rsv->reserved += num_bytes;
5450 if (update_size)
5451 block_rsv->size += num_bytes;
5452 else if (block_rsv->reserved >= block_rsv->size)
5453 block_rsv->full = 1;
5454 spin_unlock(&block_rsv->lock);
5455}
5456
Josef Bacikd52be812013-05-29 14:54:47 -04005457int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
5458 struct btrfs_block_rsv *dest, u64 num_bytes,
5459 int min_factor)
5460{
5461 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5462 u64 min_bytes;
5463
5464 if (global_rsv->space_info != dest->space_info)
5465 return -ENOSPC;
5466
5467 spin_lock(&global_rsv->lock);
5468 min_bytes = div_factor(global_rsv->size, min_factor);
5469 if (global_rsv->reserved < min_bytes + num_bytes) {
5470 spin_unlock(&global_rsv->lock);
5471 return -ENOSPC;
5472 }
5473 global_rsv->reserved -= num_bytes;
5474 if (global_rsv->reserved < global_rsv->size)
5475 global_rsv->full = 0;
5476 spin_unlock(&global_rsv->lock);
5477
5478 block_rsv_add_bytes(dest, num_bytes, 1);
5479 return 0;
5480}
5481
Josef Bacik957780e2016-05-17 13:30:55 -04005482/*
5483 * This is for space we already have accounted in space_info->bytes_may_use, so
5484 * basically when we're returning space from block_rsv's.
5485 */
5486static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
5487 struct btrfs_space_info *space_info,
5488 u64 num_bytes)
5489{
5490 struct reserve_ticket *ticket;
5491 struct list_head *head;
5492 u64 used;
5493 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH;
5494 bool check_overcommit = false;
5495
5496 spin_lock(&space_info->lock);
5497 head = &space_info->priority_tickets;
5498
5499 /*
5500 * If we are over our limit then we need to check and see if we can
5501 * overcommit, and if we can't then we just need to free up our space
5502 * and not satisfy any requests.
5503 */
Nikolay Borisov0eee8a42017-06-14 11:35:34 +03005504 used = btrfs_space_info_used(space_info, true);
Josef Bacik957780e2016-05-17 13:30:55 -04005505 if (used - num_bytes >= space_info->total_bytes)
5506 check_overcommit = true;
5507again:
5508 while (!list_empty(head) && num_bytes) {
5509 ticket = list_first_entry(head, struct reserve_ticket,
5510 list);
5511 /*
5512 * We use 0 bytes because this space is already reserved, so
5513 * adding the ticket space would be a double count.
5514 */
5515 if (check_overcommit &&
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005516 !can_overcommit(fs_info, space_info, 0, flush, false))
Josef Bacik957780e2016-05-17 13:30:55 -04005517 break;
5518 if (num_bytes >= ticket->bytes) {
5519 list_del_init(&ticket->list);
5520 num_bytes -= ticket->bytes;
5521 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005522 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005523 wake_up(&ticket->wait);
5524 } else {
5525 ticket->bytes -= num_bytes;
5526 num_bytes = 0;
5527 }
5528 }
5529
5530 if (num_bytes && head == &space_info->priority_tickets) {
5531 head = &space_info->tickets;
5532 flush = BTRFS_RESERVE_FLUSH_ALL;
5533 goto again;
5534 }
5535 space_info->bytes_may_use -= num_bytes;
5536 trace_btrfs_space_reservation(fs_info, "space_info",
5537 space_info->flags, num_bytes, 0);
5538 spin_unlock(&space_info->lock);
5539}
5540
5541/*
5542 * This is for newly allocated space that isn't accounted in
5543 * space_info->bytes_may_use yet. So if we allocate a chunk or unpin an extent
5544 * we use this helper.
5545 */
5546static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
5547 struct btrfs_space_info *space_info,
5548 u64 num_bytes)
5549{
5550 struct reserve_ticket *ticket;
5551 struct list_head *head = &space_info->priority_tickets;
5552
5553again:
5554 while (!list_empty(head) && num_bytes) {
5555 ticket = list_first_entry(head, struct reserve_ticket,
5556 list);
5557 if (num_bytes >= ticket->bytes) {
5558 trace_btrfs_space_reservation(fs_info, "space_info",
5559 space_info->flags,
5560 ticket->bytes, 1);
5561 list_del_init(&ticket->list);
5562 num_bytes -= ticket->bytes;
5563 space_info->bytes_may_use += ticket->bytes;
5564 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005565 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005566 wake_up(&ticket->wait);
5567 } else {
5568 trace_btrfs_space_reservation(fs_info, "space_info",
5569 space_info->flags,
5570 num_bytes, 1);
5571 space_info->bytes_may_use += num_bytes;
5572 ticket->bytes -= num_bytes;
5573 num_bytes = 0;
5574 }
5575 }
5576
5577 if (num_bytes && head == &space_info->priority_tickets) {
5578 head = &space_info->tickets;
5579 goto again;
5580 }
5581}
5582
Josef Bacik69fe2d72017-10-19 14:15:57 -04005583static u64 block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005584 struct btrfs_block_rsv *block_rsv,
Qu Wenruoff6bc372017-12-21 13:42:04 +08005585 struct btrfs_block_rsv *dest, u64 num_bytes,
5586 u64 *qgroup_to_release_ret)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005587{
5588 struct btrfs_space_info *space_info = block_rsv->space_info;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005589 u64 qgroup_to_release = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005590 u64 ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005591
5592 spin_lock(&block_rsv->lock);
Qu Wenruoff6bc372017-12-21 13:42:04 +08005593 if (num_bytes == (u64)-1) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005594 num_bytes = block_rsv->size;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005595 qgroup_to_release = block_rsv->qgroup_rsv_size;
5596 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005597 block_rsv->size -= num_bytes;
5598 if (block_rsv->reserved >= block_rsv->size) {
5599 num_bytes = block_rsv->reserved - block_rsv->size;
5600 block_rsv->reserved = block_rsv->size;
5601 block_rsv->full = 1;
5602 } else {
5603 num_bytes = 0;
5604 }
Qu Wenruoff6bc372017-12-21 13:42:04 +08005605 if (block_rsv->qgroup_rsv_reserved >= block_rsv->qgroup_rsv_size) {
5606 qgroup_to_release = block_rsv->qgroup_rsv_reserved -
5607 block_rsv->qgroup_rsv_size;
5608 block_rsv->qgroup_rsv_reserved = block_rsv->qgroup_rsv_size;
5609 } else {
5610 qgroup_to_release = 0;
5611 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005612 spin_unlock(&block_rsv->lock);
5613
Josef Bacik69fe2d72017-10-19 14:15:57 -04005614 ret = num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005615 if (num_bytes > 0) {
5616 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00005617 spin_lock(&dest->lock);
5618 if (!dest->full) {
5619 u64 bytes_to_add;
5620
5621 bytes_to_add = dest->size - dest->reserved;
5622 bytes_to_add = min(num_bytes, bytes_to_add);
5623 dest->reserved += bytes_to_add;
5624 if (dest->reserved >= dest->size)
5625 dest->full = 1;
5626 num_bytes -= bytes_to_add;
5627 }
5628 spin_unlock(&dest->lock);
5629 }
Josef Bacik957780e2016-05-17 13:30:55 -04005630 if (num_bytes)
5631 space_info_add_old_bytes(fs_info, space_info,
5632 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005633 }
Qu Wenruoff6bc372017-12-21 13:42:04 +08005634 if (qgroup_to_release_ret)
5635 *qgroup_to_release_ret = qgroup_to_release;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005636 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005637}
5638
Josef Bacik25d609f2016-03-25 13:25:48 -04005639int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
5640 struct btrfs_block_rsv *dst, u64 num_bytes,
5641 int update_size)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005642{
5643 int ret;
5644
5645 ret = block_rsv_use_bytes(src, num_bytes);
5646 if (ret)
5647 return ret;
5648
Josef Bacik25d609f2016-03-25 13:25:48 -04005649 block_rsv_add_bytes(dst, num_bytes, update_size);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005650 return 0;
5651}
5652
Miao Xie66d8f3d2012-09-06 04:02:28 -06005653void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005654{
5655 memset(rsv, 0, sizeof(*rsv));
5656 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06005657 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005658}
5659
Josef Bacik69fe2d72017-10-19 14:15:57 -04005660void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
5661 struct btrfs_block_rsv *rsv,
5662 unsigned short type)
5663{
5664 btrfs_init_block_rsv(rsv, type);
5665 rsv->space_info = __find_space_info(fs_info,
5666 BTRFS_BLOCK_GROUP_METADATA);
5667}
5668
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005669struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
Miao Xie66d8f3d2012-09-06 04:02:28 -06005670 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005671{
5672 struct btrfs_block_rsv *block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005673
5674 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
5675 if (!block_rsv)
5676 return NULL;
5677
Josef Bacik69fe2d72017-10-19 14:15:57 -04005678 btrfs_init_metadata_block_rsv(fs_info, block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005679 return block_rsv;
5680}
5681
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005682void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005683 struct btrfs_block_rsv *rsv)
5684{
Josef Bacik2aaa6652012-08-29 14:27:18 -04005685 if (!rsv)
5686 return;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005687 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Josef Bacikdabdb642011-08-08 12:50:18 -04005688 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005689}
5690
Chris Masoncdfb0802015-04-06 18:17:00 -07005691void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
5692{
5693 kfree(rsv);
5694}
5695
Miao Xie08e007d2012-10-16 11:33:38 +00005696int btrfs_block_rsv_add(struct btrfs_root *root,
5697 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
5698 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005699{
5700 int ret;
5701
5702 if (num_bytes == 0)
5703 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005704
Miao Xie61b520a2011-11-10 20:45:05 -05005705 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005706 if (!ret) {
5707 block_rsv_add_bytes(block_rsv, num_bytes, 1);
5708 return 0;
5709 }
5710
Yan, Zhengf0486c62010-05-16 10:46:25 -04005711 return ret;
5712}
5713
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005714int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005715{
5716 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005717 int ret = -ENOSPC;
5718
5719 if (!block_rsv)
5720 return 0;
5721
5722 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04005723 num_bytes = div_factor(block_rsv->size, min_factor);
5724 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005725 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005726 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005727
Josef Bacik36ba0222011-10-18 12:15:48 -04005728 return ret;
5729}
5730
Miao Xie08e007d2012-10-16 11:33:38 +00005731int btrfs_block_rsv_refill(struct btrfs_root *root,
5732 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
5733 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04005734{
5735 u64 num_bytes = 0;
5736 int ret = -ENOSPC;
5737
5738 if (!block_rsv)
5739 return 0;
5740
5741 spin_lock(&block_rsv->lock);
5742 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04005743 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005744 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04005745 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04005746 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005747 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04005748
Yan, Zhengf0486c62010-05-16 10:46:25 -04005749 if (!ret)
5750 return 0;
5751
Miao Xieaa38a712011-11-18 17:43:00 +08005752 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04005753 if (!ret) {
5754 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005755 return 0;
5756 }
5757
Josef Bacik13553e52011-08-08 13:33:21 -04005758 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005759}
5760
Josef Bacik69fe2d72017-10-19 14:15:57 -04005761/**
5762 * btrfs_inode_rsv_refill - refill the inode block rsv.
5763 * @inode - the inode we are refilling.
5764 * @flush - the flusing restriction.
5765 *
5766 * Essentially the same as btrfs_block_rsv_refill, except it uses the
5767 * block_rsv->size as the minimum size. We'll either refill the missing amount
5768 * or return if we already have enough space. This will also handle the resreve
5769 * tracepoint for the reserved amount.
5770 */
Qu Wenruo3f2dd7a2017-11-17 15:14:19 +08005771static int btrfs_inode_rsv_refill(struct btrfs_inode *inode,
5772 enum btrfs_reserve_flush_enum flush)
Josef Bacik69fe2d72017-10-19 14:15:57 -04005773{
5774 struct btrfs_root *root = inode->root;
5775 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
5776 u64 num_bytes = 0;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005777 u64 qgroup_num_bytes = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005778 int ret = -ENOSPC;
5779
5780 spin_lock(&block_rsv->lock);
5781 if (block_rsv->reserved < block_rsv->size)
5782 num_bytes = block_rsv->size - block_rsv->reserved;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005783 if (block_rsv->qgroup_rsv_reserved < block_rsv->qgroup_rsv_size)
5784 qgroup_num_bytes = block_rsv->qgroup_rsv_size -
5785 block_rsv->qgroup_rsv_reserved;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005786 spin_unlock(&block_rsv->lock);
5787
5788 if (num_bytes == 0)
5789 return 0;
5790
Qu Wenruoff6bc372017-12-21 13:42:04 +08005791 ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_num_bytes, true);
Qu Wenruo43b18592017-12-12 15:34:32 +08005792 if (ret)
5793 return ret;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005794 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
5795 if (!ret) {
5796 block_rsv_add_bytes(block_rsv, num_bytes, 0);
5797 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5798 btrfs_ino(inode), num_bytes, 1);
Qu Wenruoff6bc372017-12-21 13:42:04 +08005799
5800 /* Don't forget to increase qgroup_rsv_reserved */
5801 spin_lock(&block_rsv->lock);
5802 block_rsv->qgroup_rsv_reserved += qgroup_num_bytes;
5803 spin_unlock(&block_rsv->lock);
5804 } else
5805 btrfs_qgroup_free_meta_prealloc(root, qgroup_num_bytes);
Josef Bacik69fe2d72017-10-19 14:15:57 -04005806 return ret;
5807}
5808
5809/**
5810 * btrfs_inode_rsv_release - release any excessive reservation.
5811 * @inode - the inode we need to release from.
Qu Wenruo43b18592017-12-12 15:34:32 +08005812 * @qgroup_free - free or convert qgroup meta.
5813 * Unlike normal operation, qgroup meta reservation needs to know if we are
5814 * freeing qgroup reservation or just converting it into per-trans. Normally
5815 * @qgroup_free is true for error handling, and false for normal release.
Josef Bacik69fe2d72017-10-19 14:15:57 -04005816 *
5817 * This is the same as btrfs_block_rsv_release, except that it handles the
5818 * tracepoint for the reservation.
5819 */
Qu Wenruo43b18592017-12-12 15:34:32 +08005820static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
Josef Bacik69fe2d72017-10-19 14:15:57 -04005821{
5822 struct btrfs_fs_info *fs_info = inode->root->fs_info;
5823 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5824 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
5825 u64 released = 0;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005826 u64 qgroup_to_release = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04005827
5828 /*
5829 * Since we statically set the block_rsv->size we just want to say we
5830 * are releasing 0 bytes, and then we'll just get the reservation over
5831 * the size free'd.
5832 */
Qu Wenruoff6bc372017-12-21 13:42:04 +08005833 released = block_rsv_release_bytes(fs_info, block_rsv, global_rsv, 0,
5834 &qgroup_to_release);
Josef Bacik69fe2d72017-10-19 14:15:57 -04005835 if (released > 0)
5836 trace_btrfs_space_reservation(fs_info, "delalloc",
5837 btrfs_ino(inode), released, 0);
Qu Wenruo43b18592017-12-12 15:34:32 +08005838 if (qgroup_free)
Qu Wenruoff6bc372017-12-21 13:42:04 +08005839 btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
Qu Wenruo43b18592017-12-12 15:34:32 +08005840 else
Qu Wenruoff6bc372017-12-21 13:42:04 +08005841 btrfs_qgroup_convert_reserved_meta(inode->root,
5842 qgroup_to_release);
Josef Bacik69fe2d72017-10-19 14:15:57 -04005843}
5844
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005845void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005846 struct btrfs_block_rsv *block_rsv,
5847 u64 num_bytes)
5848{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005849 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5850
Liu Bo17504582013-12-29 21:44:50 +08005851 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04005852 block_rsv->space_info != global_rsv->space_info)
5853 global_rsv = NULL;
Qu Wenruoff6bc372017-12-21 13:42:04 +08005854 block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes, NULL);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005855}
5856
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005857static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
5858{
5859 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
5860 struct btrfs_space_info *sinfo = block_rsv->space_info;
5861 u64 num_bytes;
5862
Josef Bacikae2e4722016-05-27 12:58:35 -04005863 /*
5864 * The global block rsv is based on the size of the extent tree, the
5865 * checksum tree and the root tree. If the fs is empty we want to set
5866 * it to a minimal amount for safety.
5867 */
5868 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) +
5869 btrfs_root_used(&fs_info->csum_root->root_item) +
5870 btrfs_root_used(&fs_info->tree_root->root_item);
5871 num_bytes = max_t(u64, num_bytes, SZ_16M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005872
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005873 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005874 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005875
Byongho Leeee221842015-12-15 01:42:10 +09005876 block_rsv->size = min_t(u64, num_bytes, SZ_512M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005877
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005878 if (block_rsv->reserved < block_rsv->size) {
Liu Bo41361352017-02-13 15:42:21 -08005879 num_bytes = btrfs_space_info_used(sinfo, true);
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005880 if (sinfo->total_bytes > num_bytes) {
5881 num_bytes = sinfo->total_bytes - num_bytes;
5882 num_bytes = min(num_bytes,
5883 block_rsv->size - block_rsv->reserved);
5884 block_rsv->reserved += num_bytes;
5885 sinfo->bytes_may_use += num_bytes;
5886 trace_btrfs_space_reservation(fs_info, "space_info",
5887 sinfo->flags, num_bytes,
5888 1);
5889 }
5890 } else if (block_rsv->reserved > block_rsv->size) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005891 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04005892 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005893 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005894 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005895 block_rsv->reserved = block_rsv->size;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005896 }
David Sterba182608c2011-05-05 13:13:16 +02005897
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005898 if (block_rsv->reserved == block_rsv->size)
5899 block_rsv->full = 1;
5900 else
5901 block_rsv->full = 0;
5902
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005903 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005904 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005905}
5906
Yan, Zhengf0486c62010-05-16 10:46:25 -04005907static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
5908{
5909 struct btrfs_space_info *space_info;
5910
5911 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
5912 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005913
5914 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005915 fs_info->global_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005916 fs_info->trans_block_rsv.space_info = space_info;
5917 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04005918 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005919
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005920 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
5921 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
5922 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
5923 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00005924 if (fs_info->quota_root)
5925 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005926 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005927
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005928 update_global_block_rsv(fs_info);
5929}
5930
5931static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
5932{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005933 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
Qu Wenruoff6bc372017-12-21 13:42:04 +08005934 (u64)-1, NULL);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005935 WARN_ON(fs_info->trans_block_rsv.size > 0);
5936 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
5937 WARN_ON(fs_info->chunk_block_rsv.size > 0);
5938 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04005939 WARN_ON(fs_info->delayed_block_rsv.size > 0);
5940 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04005941}
5942
Yan, Zhenga22285a2010-05-16 10:48:46 -04005943
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005944/*
5945 * To be called after all the new block groups attached to the transaction
5946 * handle have been created (btrfs_create_pending_block_groups()).
5947 */
5948void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
5949{
Jeff Mahoney64b63582016-06-20 17:23:41 -04005950 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005951
5952 if (!trans->chunk_bytes_reserved)
5953 return;
5954
5955 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
5956
5957 block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL,
Qu Wenruoff6bc372017-12-21 13:42:04 +08005958 trans->chunk_bytes_reserved, NULL);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005959 trans->chunk_bytes_reserved = 0;
5960}
5961
Miao Xied5c12072013-02-28 10:04:33 +00005962/*
5963 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
5964 * root: the root of the parent directory
5965 * rsv: block reservation
5966 * items: the number of items that we need do reservation
5967 * qgroup_reserved: used to return the reserved size in qgroup
5968 *
5969 * This function is used to reserve the space for snapshot/subvolume
5970 * creation and deletion. Those operations are different with the
5971 * common file/directory operations, they change two fs/file trees
5972 * and root tree, the number of items that the qgroup reserves is
5973 * different with the free space reservation. So we can not use
Nicholas D Steeves01327612016-05-19 21:18:45 -04005974 * the space reservation mechanism in start_transaction().
Miao Xied5c12072013-02-28 10:04:33 +00005975 */
5976int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
5977 struct btrfs_block_rsv *rsv,
5978 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005979 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005980{
Miao Xied5c12072013-02-28 10:04:33 +00005981 u64 num_bytes;
5982 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005983 struct btrfs_fs_info *fs_info = root->fs_info;
5984 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00005985
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005986 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Miao Xied5c12072013-02-28 10:04:33 +00005987 /* One for parent inode, two for dir entries */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005988 num_bytes = 3 * fs_info->nodesize;
Qu Wenruo733e03a2017-12-12 15:34:29 +08005989 ret = btrfs_qgroup_reserve_meta_prealloc(root, num_bytes, true);
Miao Xied5c12072013-02-28 10:04:33 +00005990 if (ret)
5991 return ret;
5992 } else {
5993 num_bytes = 0;
5994 }
5995
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005996 num_bytes = btrfs_calc_trans_metadata_size(fs_info, items);
5997 rsv->space_info = __find_space_info(fs_info,
Miao Xied5c12072013-02-28 10:04:33 +00005998 BTRFS_BLOCK_GROUP_METADATA);
5999 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
6000 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04006001
6002 if (ret == -ENOSPC && use_global_rsv)
Josef Bacik25d609f2016-03-25 13:25:48 -04006003 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04006004
Gu JinXiangc4c129d2018-05-30 11:00:38 +08006005 if (ret && num_bytes)
6006 btrfs_qgroup_free_meta_prealloc(root, num_bytes);
Miao Xied5c12072013-02-28 10:04:33 +00006007
6008 return ret;
6009}
6010
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006011void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
David Sterba7775c812017-02-10 19:18:18 +01006012 struct btrfs_block_rsv *rsv)
Miao Xied5c12072013-02-28 10:04:33 +00006013{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006014 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006015}
6016
Josef Bacik69fe2d72017-10-19 14:15:57 -04006017static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
6018 struct btrfs_inode *inode)
Josef Bacik9e0baf62011-07-15 15:16:44 +00006019{
Josef Bacik69fe2d72017-10-19 14:15:57 -04006020 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
6021 u64 reserve_size = 0;
Qu Wenruoff6bc372017-12-21 13:42:04 +08006022 u64 qgroup_rsv_size = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04006023 u64 csum_leaves;
6024 unsigned outstanding_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006025
Josef Bacik69fe2d72017-10-19 14:15:57 -04006026 lockdep_assert_held(&inode->lock);
6027 outstanding_extents = inode->outstanding_extents;
6028 if (outstanding_extents)
6029 reserve_size = btrfs_calc_trans_metadata_size(fs_info,
6030 outstanding_extents + 1);
6031 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
6032 inode->csum_bytes);
6033 reserve_size += btrfs_calc_trans_metadata_size(fs_info,
6034 csum_leaves);
Qu Wenruoff6bc372017-12-21 13:42:04 +08006035 /*
6036 * For qgroup rsv, the calculation is very simple:
6037 * account one nodesize for each outstanding extent
6038 *
6039 * This is overestimating in most cases.
6040 */
6041 qgroup_rsv_size = outstanding_extents * fs_info->nodesize;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006042
Josef Bacik69fe2d72017-10-19 14:15:57 -04006043 spin_lock(&block_rsv->lock);
6044 block_rsv->size = reserve_size;
Qu Wenruoff6bc372017-12-21 13:42:04 +08006045 block_rsv->qgroup_rsv_size = qgroup_rsv_size;
Josef Bacik69fe2d72017-10-19 14:15:57 -04006046 spin_unlock(&block_rsv->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006047}
6048
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006049int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006050{
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006051 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006052 unsigned nr_extents;
Miao Xie08e007d2012-10-16 11:33:38 +00006053 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07006054 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006055 bool delalloc_lock = true;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006056
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006057 /* If we are a free space inode we need to not flush since we will be in
6058 * the middle of a transaction commit. We also don't need the delalloc
6059 * mutex since we won't race with anybody. We need this mostly to make
6060 * lockdep shut its filthy mouth.
Josef Bacikbac357dc2016-07-20 16:48:45 -07006061 *
6062 * If we have a transaction open (can happen if we call truncate_block
6063 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006064 */
6065 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00006066 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006067 delalloc_lock = false;
Nikolay Borisovda07d4a2018-01-12 16:21:05 +02006068 } else {
6069 if (current->journal_info)
6070 flush = BTRFS_RESERVE_FLUSH_LIMIT;
Josef Bacikc09544e2011-08-30 10:19:10 -04006071
Nikolay Borisovda07d4a2018-01-12 16:21:05 +02006072 if (btrfs_transaction_in_commit(fs_info))
6073 schedule_timeout(1);
6074 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006075
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006076 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006077 mutex_lock(&inode->delalloc_mutex);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006078
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006079 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006080
6081 /* Add our new extents and calculate the new rsv size. */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006082 spin_lock(&inode->lock);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006083 nr_extents = count_max_extents(num_bytes);
Josef Bacik8b62f872017-10-19 14:15:55 -04006084 btrfs_mod_outstanding_extents(inode, nr_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006085 inode->csum_bytes += num_bytes;
6086 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006087 spin_unlock(&inode->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05006088
Josef Bacik69fe2d72017-10-19 14:15:57 -04006089 ret = btrfs_inode_rsv_refill(inode, flush);
Qu Wenruo43b18592017-12-12 15:34:32 +08006090 if (unlikely(ret))
Wang Shilong88e081bf2013-03-01 11:36:01 +00006091 goto out_fail;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006092
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006093 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006094 mutex_unlock(&inode->delalloc_mutex);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006095 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00006096
6097out_fail:
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006098 spin_lock(&inode->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04006099 nr_extents = count_max_extents(num_bytes);
6100 btrfs_mod_outstanding_extents(inode, -nr_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006101 inode->csum_bytes -= num_bytes;
6102 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006103 spin_unlock(&inode->lock);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006104
Qu Wenruo43b18592017-12-12 15:34:32 +08006105 btrfs_inode_rsv_release(inode, true);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006106 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006107 mutex_unlock(&inode->delalloc_mutex);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006108 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006109}
6110
Josef Bacik7709cde2011-08-04 10:25:02 -04006111/**
6112 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
Josef Bacik8b62f872017-10-19 14:15:55 -04006113 * @inode: the inode to release the reservation for.
6114 * @num_bytes: the number of bytes we are releasing.
Qu Wenruo43b18592017-12-12 15:34:32 +08006115 * @qgroup_free: free qgroup reservation or convert it to per-trans reservation
Josef Bacik7709cde2011-08-04 10:25:02 -04006116 *
6117 * This will release the metadata reservation for an inode. This can be called
6118 * once we complete IO for a given set of bytes to release their metadata
Josef Bacik8b62f872017-10-19 14:15:55 -04006119 * reservations, or on error for the same reason.
Josef Bacik7709cde2011-08-04 10:25:02 -04006120 */
Qu Wenruo43b18592017-12-12 15:34:32 +08006121void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
6122 bool qgroup_free)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006123{
Nikolay Borisov691fa052017-02-20 13:50:42 +02006124 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006125
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006126 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006127 spin_lock(&inode->lock);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006128 inode->csum_bytes -= num_bytes;
6129 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006130 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006131
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006132 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04006133 return;
6134
Qu Wenruo43b18592017-12-12 15:34:32 +08006135 btrfs_inode_rsv_release(inode, qgroup_free);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006136}
6137
Josef Bacik7709cde2011-08-04 10:25:02 -04006138/**
Josef Bacik8b62f872017-10-19 14:15:55 -04006139 * btrfs_delalloc_release_extents - release our outstanding_extents
6140 * @inode: the inode to balance the reservation for.
6141 * @num_bytes: the number of bytes we originally reserved with
Qu Wenruo43b18592017-12-12 15:34:32 +08006142 * @qgroup_free: do we need to free qgroup meta reservation or convert them.
Josef Bacik8b62f872017-10-19 14:15:55 -04006143 *
6144 * When we reserve space we increase outstanding_extents for the extents we may
6145 * add. Once we've set the range as delalloc or created our ordered extents we
6146 * have outstanding_extents to track the real usage, so we use this to free our
6147 * temporarily tracked outstanding_extents. This _must_ be used in conjunction
6148 * with btrfs_delalloc_reserve_metadata.
6149 */
Qu Wenruo43b18592017-12-12 15:34:32 +08006150void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes,
6151 bool qgroup_free)
Josef Bacik8b62f872017-10-19 14:15:55 -04006152{
6153 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
6154 unsigned num_extents;
Josef Bacik8b62f872017-10-19 14:15:55 -04006155
6156 spin_lock(&inode->lock);
6157 num_extents = count_max_extents(num_bytes);
6158 btrfs_mod_outstanding_extents(inode, -num_extents);
Josef Bacik69fe2d72017-10-19 14:15:57 -04006159 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
Josef Bacik8b62f872017-10-19 14:15:55 -04006160 spin_unlock(&inode->lock);
6161
Josef Bacik8b62f872017-10-19 14:15:55 -04006162 if (btrfs_is_testing(fs_info))
6163 return;
6164
Qu Wenruo43b18592017-12-12 15:34:32 +08006165 btrfs_inode_rsv_release(inode, qgroup_free);
Josef Bacik8b62f872017-10-19 14:15:55 -04006166}
6167
6168/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006169 * btrfs_delalloc_reserve_space - reserve data and metadata space for
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006170 * delalloc
6171 * @inode: inode we're writing to
6172 * @start: start range we are writing to
6173 * @len: how long the range we are writing to
Qu Wenruo364ecf32017-02-27 15:10:38 +08006174 * @reserved: mandatory parameter, record actually reserved qgroup ranges of
6175 * current reservation.
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006176 *
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006177 * This will do the following things
6178 *
6179 * o reserve space in data space info for num bytes
6180 * and reserve precious corresponding qgroup space
6181 * (Done in check_data_free_space)
6182 *
6183 * o reserve space for metadata space, based on the number of outstanding
6184 * extents and how much csums will be needed
6185 * also reserve metadata space in a per root over-reserve method.
6186 * o add to the inodes->delalloc_bytes
6187 * o add it to the fs_info's delalloc inodes list.
6188 * (Above 3 all done in delalloc_reserve_metadata)
6189 *
6190 * Return 0 for success
6191 * Return <0 for error(-ENOSPC or -EQUOT)
6192 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08006193int btrfs_delalloc_reserve_space(struct inode *inode,
6194 struct extent_changeset **reserved, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006195{
6196 int ret;
6197
Qu Wenruo364ecf32017-02-27 15:10:38 +08006198 ret = btrfs_check_data_free_space(inode, reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006199 if (ret < 0)
6200 return ret;
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006201 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006202 if (ret < 0)
Qu Wenruobc42bda2017-02-27 15:10:39 +08006203 btrfs_free_reserved_data_space(inode, *reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006204 return ret;
6205}
6206
6207/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006208 * btrfs_delalloc_release_space - release data and metadata space for delalloc
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006209 * @inode: inode we're releasing space for
6210 * @start: start position of the space already reserved
6211 * @len: the len of the space already reserved
Josef Bacik8b62f872017-10-19 14:15:55 -04006212 * @release_bytes: the len of the space we consumed or didn't use
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006213 *
6214 * This function will release the metadata space that was not used and will
6215 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
6216 * list if there are no delalloc bytes left.
6217 * Also it will handle the qgroup reserved space.
6218 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08006219void btrfs_delalloc_release_space(struct inode *inode,
Josef Bacik8b62f872017-10-19 14:15:55 -04006220 struct extent_changeset *reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08006221 u64 start, u64 len, bool qgroup_free)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006222{
Qu Wenruo43b18592017-12-12 15:34:32 +08006223 btrfs_delalloc_release_metadata(BTRFS_I(inode), len, qgroup_free);
Qu Wenruobc42bda2017-02-27 15:10:39 +08006224 btrfs_free_reserved_data_space(inode, reserved, start, len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006225}
6226
Josef Bacikce93ec52014-11-17 15:45:48 -05006227static int update_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6202df62016-06-22 18:54:22 -04006228 struct btrfs_fs_info *info, u64 bytenr,
Josef Bacikce93ec52014-11-17 15:45:48 -05006229 u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04006230{
Josef Bacik0af3d002010-06-21 14:48:16 -04006231 struct btrfs_block_group_cache *cache = NULL;
Chris Masondb945352007-10-15 16:15:53 -04006232 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006233 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04006234 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04006235 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04006236
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006237 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00006238 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02006239 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006240 if (alloc)
6241 old_val += num_bytes;
6242 else
6243 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02006244 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00006245 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006246
Chris Masond3977122009-01-05 21:25:51 -05006247 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04006248 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006249 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006250 return -ENOENT;
Yan, Zhengb742bb822010-05-16 10:46:24 -04006251 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
6252 BTRFS_BLOCK_GROUP_RAID1 |
6253 BTRFS_BLOCK_GROUP_RAID10))
6254 factor = 2;
6255 else
6256 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04006257 /*
6258 * If this block group has free space cache written out, we
6259 * need to make sure to load it if we are removing space. This
6260 * is because we need the unpinning stage to actually add the
6261 * space back to the block group, otherwise we will leak space.
6262 */
6263 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00006264 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04006265
Chris Masondb945352007-10-15 16:15:53 -04006266 byte_in_group = bytenr - cache->key.objectid;
6267 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04006268
Josef Bacik25179202008-10-29 14:49:05 -04006269 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006270 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04006271
Jeff Mahoney6202df62016-06-22 18:54:22 -04006272 if (btrfs_test_opt(info, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04006273 cache->disk_cache_state < BTRFS_DC_CLEAR)
6274 cache->disk_cache_state = BTRFS_DC_CLEAR;
6275
Chris Mason9078a3e2007-04-26 16:46:15 -04006276 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04006277 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04006278 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04006279 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006280 btrfs_set_block_group_used(&cache->item, old_val);
6281 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006282 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb822010-05-16 10:46:24 -04006283 cache->space_info->bytes_used += num_bytes;
6284 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04006285 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006286 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04006287 } else {
Chris Masondb945352007-10-15 16:15:53 -04006288 old_val -= num_bytes;
Filipe Mananaae0ab002014-11-26 15:28:52 +00006289 btrfs_set_block_group_used(&cache->item, old_val);
6290 cache->pinned += num_bytes;
6291 cache->space_info->bytes_pinned += num_bytes;
6292 cache->space_info->bytes_used -= num_bytes;
6293 cache->space_info->disk_used -= num_bytes * factor;
6294 spin_unlock(&cache->lock);
6295 spin_unlock(&cache->space_info->lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04006296
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006297 trace_btrfs_space_reservation(info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006298 cache->space_info->flags,
6299 num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07006300 percpu_counter_add(&cache->space_info->total_bytes_pinned,
6301 num_bytes);
Filipe Mananaae0ab002014-11-26 15:28:52 +00006302 set_extent_dirty(info->pinned_extents,
6303 bytenr, bytenr + num_bytes - 1,
6304 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04006305 }
Chris Mason1bbc6212015-04-06 12:46:08 -07006306
6307 spin_lock(&trans->transaction->dirty_bgs_lock);
6308 if (list_empty(&cache->dirty_list)) {
6309 list_add_tail(&cache->dirty_list,
6310 &trans->transaction->dirty_bgs);
Bart Van Asschebece2e82018-06-20 10:03:31 -07006311 trans->transaction->num_dirty_bgs++;
Chris Mason1bbc6212015-04-06 12:46:08 -07006312 btrfs_get_block_group(cache);
6313 }
6314 spin_unlock(&trans->transaction->dirty_bgs_lock);
6315
Filipe Manana036a9342015-11-23 15:25:16 +00006316 /*
6317 * No longer have used bytes in this block group, queue it for
6318 * deletion. We do this after adding the block group to the
6319 * dirty list to avoid races between cleaner kthread and space
6320 * cache writeout.
6321 */
6322 if (!alloc && old_val == 0) {
6323 spin_lock(&info->unused_bgs_lock);
6324 if (list_empty(&cache->bg_list)) {
6325 btrfs_get_block_group(cache);
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +08006326 trace_btrfs_add_unused_block_group(cache);
Filipe Manana036a9342015-11-23 15:25:16 +00006327 list_add_tail(&cache->bg_list,
6328 &info->unused_bgs);
6329 }
6330 spin_unlock(&info->unused_bgs_lock);
6331 }
6332
Chris Masonfa9c0d792009-04-03 09:47:43 -04006333 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04006334 total -= num_bytes;
6335 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006336 }
6337 return 0;
6338}
Chris Mason6324fbf2008-03-24 15:01:59 -04006339
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006340static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04006341{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006342 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05006343 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006344
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006345 spin_lock(&fs_info->block_group_cache_lock);
6346 bytenr = fs_info->first_logical_byte;
6347 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00006348
6349 if (bytenr < (u64)-1)
6350 return bytenr;
6351
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006352 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006353 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04006354 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006355
Yan Zhengd2fb3432008-12-11 16:30:39 -05006356 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006357 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006358
6359 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04006360}
6361
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006362static int pin_down_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006363 struct btrfs_block_group_cache *cache,
6364 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05006365{
Yan Zheng11833d62009-09-11 16:11:19 -04006366 spin_lock(&cache->space_info->lock);
6367 spin_lock(&cache->lock);
6368 cache->pinned += num_bytes;
6369 cache->space_info->bytes_pinned += num_bytes;
6370 if (reserved) {
6371 cache->reserved -= num_bytes;
6372 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05006373 }
Yan Zheng11833d62009-09-11 16:11:19 -04006374 spin_unlock(&cache->lock);
6375 spin_unlock(&cache->space_info->lock);
6376
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006377 trace_btrfs_space_reservation(fs_info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006378 cache->space_info->flags, num_bytes, 1);
Omar Sandoval4da8b762017-06-06 16:45:28 -07006379 percpu_counter_add(&cache->space_info->total_bytes_pinned, num_bytes);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006380 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006381 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05006382 return 0;
6383}
Chris Mason9078a3e2007-04-26 16:46:15 -04006384
Yan, Zhengf0486c62010-05-16 10:46:25 -04006385/*
6386 * this function must be called within transaction
6387 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006388int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006389 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04006390{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006391 struct btrfs_block_group_cache *cache;
6392
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006393 cache = btrfs_lookup_block_group(fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006394 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006395
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006396 pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006397
6398 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04006399 return 0;
6400}
Zheng Yane8569812008-09-26 10:05:48 -04006401
Yan, Zhengf0486c62010-05-16 10:46:25 -04006402/*
Chris Masone688b7252011-10-31 20:52:39 -04006403 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04006404 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006405int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04006406 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006407{
Chris Masone688b7252011-10-31 20:52:39 -04006408 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04006409 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04006410
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006411 cache = btrfs_lookup_block_group(fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006412 if (!cache)
6413 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04006414
6415 /*
6416 * pull in the free space cache (if any) so that our pin
6417 * removes the free space from the cache. We have load_only set
6418 * to one because the slow code to read in the free extents does check
6419 * the pinned extents.
6420 */
Liu Bof6373bf2012-12-27 09:01:18 +00006421 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04006422
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006423 pin_down_extent(fs_info, cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04006424
6425 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04006426 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04006427 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006428 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04006429}
6430
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006431static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
6432 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006433{
6434 int ret;
6435 struct btrfs_block_group_cache *block_group;
6436 struct btrfs_caching_control *caching_ctl;
6437
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006438 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006439 if (!block_group)
6440 return -EINVAL;
6441
6442 cache_block_group(block_group, 0);
6443 caching_ctl = get_caching_control(block_group);
6444
6445 if (!caching_ctl) {
6446 /* Logic error */
6447 BUG_ON(!block_group_cache_done(block_group));
6448 ret = btrfs_remove_free_space(block_group, start, num_bytes);
6449 } else {
6450 mutex_lock(&caching_ctl->mutex);
6451
6452 if (start >= caching_ctl->progress) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006453 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006454 } else if (start + num_bytes <= caching_ctl->progress) {
6455 ret = btrfs_remove_free_space(block_group,
6456 start, num_bytes);
6457 } else {
6458 num_bytes = caching_ctl->progress - start;
6459 ret = btrfs_remove_free_space(block_group,
6460 start, num_bytes);
6461 if (ret)
6462 goto out_lock;
6463
6464 num_bytes = (start + num_bytes) -
6465 caching_ctl->progress;
6466 start = caching_ctl->progress;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006467 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006468 }
6469out_lock:
6470 mutex_unlock(&caching_ctl->mutex);
6471 put_caching_control(caching_ctl);
6472 }
6473 btrfs_put_block_group(block_group);
6474 return ret;
6475}
6476
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006477int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info,
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006478 struct extent_buffer *eb)
6479{
6480 struct btrfs_file_extent_item *item;
6481 struct btrfs_key key;
6482 int found_type;
6483 int i;
Gu Jinxiangb89311e2018-05-22 17:46:51 +08006484 int ret = 0;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006485
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006486 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006487 return 0;
6488
6489 for (i = 0; i < btrfs_header_nritems(eb); i++) {
6490 btrfs_item_key_to_cpu(eb, &key, i);
6491 if (key.type != BTRFS_EXTENT_DATA_KEY)
6492 continue;
6493 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
6494 found_type = btrfs_file_extent_type(eb, item);
6495 if (found_type == BTRFS_FILE_EXTENT_INLINE)
6496 continue;
6497 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
6498 continue;
6499 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
6500 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Gu Jinxiangb89311e2018-05-22 17:46:51 +08006501 ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
6502 if (ret)
6503 break;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006504 }
6505
Gu Jinxiangb89311e2018-05-22 17:46:51 +08006506 return ret;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006507}
6508
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006509static void
6510btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg)
6511{
6512 atomic_inc(&bg->reservations);
6513}
6514
6515void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
6516 const u64 start)
6517{
6518 struct btrfs_block_group_cache *bg;
6519
6520 bg = btrfs_lookup_block_group(fs_info, start);
6521 ASSERT(bg);
6522 if (atomic_dec_and_test(&bg->reservations))
Peter Zijlstra46259562018-03-15 11:43:08 +01006523 wake_up_var(&bg->reservations);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006524 btrfs_put_block_group(bg);
6525}
6526
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006527void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6528{
6529 struct btrfs_space_info *space_info = bg->space_info;
6530
6531 ASSERT(bg->ro);
6532
6533 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
6534 return;
6535
6536 /*
6537 * Our block group is read only but before we set it to read only,
6538 * some task might have had allocated an extent from it already, but it
6539 * has not yet created a respective ordered extent (and added it to a
6540 * root's list of ordered extents).
6541 * Therefore wait for any task currently allocating extents, since the
6542 * block group's reservations counter is incremented while a read lock
6543 * on the groups' semaphore is held and decremented after releasing
6544 * the read access on that semaphore and creating the ordered extent.
6545 */
6546 down_write(&space_info->groups_sem);
6547 up_write(&space_info->groups_sem);
6548
Peter Zijlstra46259562018-03-15 11:43:08 +01006549 wait_var_event(&bg->reservations, !atomic_read(&bg->reservations));
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006550}
6551
Josef Bacikfb25e912011-07-26 17:00:46 -04006552/**
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006553 * btrfs_add_reserved_bytes - update the block_group and space info counters
Josef Bacikfb25e912011-07-26 17:00:46 -04006554 * @cache: The cache we are manipulating
Wang Xiaoguang18513092016-07-25 15:51:40 +08006555 * @ram_bytes: The number of bytes of file content, and will be same to
6556 * @num_bytes except for the compress path.
Josef Bacikfb25e912011-07-26 17:00:46 -04006557 * @num_bytes: The number of bytes in question
Miao Xiee570fd22014-06-19 10:42:50 +08006558 * @delalloc: The blocks are allocated for the delalloc write
Josef Bacikfb25e912011-07-26 17:00:46 -04006559 *
Xiaoguang Wang745699e2016-09-23 12:38:50 +08006560 * This is called by the allocator when it reserves space. If this is a
6561 * reservation and the block group has become read only we cannot make the
6562 * reservation and return -EAGAIN, otherwise this function always succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04006563 */
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006564static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
Wang Xiaoguang18513092016-07-25 15:51:40 +08006565 u64 ram_bytes, u64 num_bytes, int delalloc)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006566{
Josef Bacikfb25e912011-07-26 17:00:46 -04006567 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006568 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006569
Josef Bacikfb25e912011-07-26 17:00:46 -04006570 spin_lock(&space_info->lock);
6571 spin_lock(&cache->lock);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006572 if (cache->ro) {
6573 ret = -EAGAIN;
Josef Bacikfb25e912011-07-26 17:00:46 -04006574 } else {
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006575 cache->reserved += num_bytes;
6576 space_info->bytes_reserved += num_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006577
Wang Xiaoguang18513092016-07-25 15:51:40 +08006578 trace_btrfs_space_reservation(cache->fs_info,
6579 "space_info", space_info->flags,
6580 ram_bytes, 0);
6581 space_info->bytes_may_use -= ram_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006582 if (delalloc)
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006583 cache->delalloc_bytes += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006584 }
Josef Bacikfb25e912011-07-26 17:00:46 -04006585 spin_unlock(&cache->lock);
6586 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006587 return ret;
6588}
6589
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006590/**
6591 * btrfs_free_reserved_bytes - update the block_group and space info counters
6592 * @cache: The cache we are manipulating
6593 * @num_bytes: The number of bytes in question
6594 * @delalloc: The blocks are allocated for the delalloc write
6595 *
6596 * This is called by somebody who is freeing space that was never actually used
6597 * on disk. For example if you reserve some space for a new leaf in transaction
6598 * A and before transaction A commits you free that leaf, you call this with
6599 * reserve set to 0 in order to clear the reservation.
6600 */
6601
6602static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
6603 u64 num_bytes, int delalloc)
6604{
6605 struct btrfs_space_info *space_info = cache->space_info;
6606 int ret = 0;
6607
6608 spin_lock(&space_info->lock);
6609 spin_lock(&cache->lock);
6610 if (cache->ro)
6611 space_info->bytes_readonly += num_bytes;
6612 cache->reserved -= num_bytes;
6613 space_info->bytes_reserved -= num_bytes;
6614
6615 if (delalloc)
6616 cache->delalloc_bytes -= num_bytes;
6617 spin_unlock(&cache->lock);
6618 spin_unlock(&space_info->lock);
6619 return ret;
6620}
David Sterba8b74c032017-02-10 19:20:56 +01006621void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04006622{
Yan Zheng11833d62009-09-11 16:11:19 -04006623 struct btrfs_caching_control *next;
6624 struct btrfs_caching_control *caching_ctl;
6625 struct btrfs_block_group_cache *cache;
6626
Josef Bacik9e351cc2014-03-13 15:42:13 -04006627 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04006628
6629 list_for_each_entry_safe(caching_ctl, next,
6630 &fs_info->caching_block_groups, list) {
6631 cache = caching_ctl->block_group;
6632 if (block_group_cache_done(cache)) {
6633 cache->last_byte_to_unpin = (u64)-1;
6634 list_del_init(&caching_ctl->list);
6635 put_caching_control(caching_ctl);
6636 } else {
6637 cache->last_byte_to_unpin = caching_ctl->progress;
6638 }
6639 }
6640
6641 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6642 fs_info->pinned_extents = &fs_info->freed_extents[1];
6643 else
6644 fs_info->pinned_extents = &fs_info->freed_extents[0];
6645
Josef Bacik9e351cc2014-03-13 15:42:13 -04006646 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006647
6648 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04006649}
6650
Josef Bacikc759c4e2015-10-02 15:25:10 -04006651/*
6652 * Returns the free cluster for the given space info and sets empty_cluster to
6653 * what it should be based on the mount options.
6654 */
6655static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006656fetch_cluster_info(struct btrfs_fs_info *fs_info,
6657 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04006658{
6659 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006660
6661 *empty_cluster = 0;
6662 if (btrfs_mixed_space_info(space_info))
6663 return ret;
6664
Josef Bacikc759c4e2015-10-02 15:25:10 -04006665 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006666 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006667 if (btrfs_test_opt(fs_info, SSD))
6668 *empty_cluster = SZ_2M;
6669 else
Byongho Leeee221842015-12-15 01:42:10 +09006670 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006671 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
6672 btrfs_test_opt(fs_info, SSD_SPREAD)) {
6673 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006674 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006675 }
6676
6677 return ret;
6678}
6679
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006680static int unpin_extent_range(struct btrfs_fs_info *fs_info,
6681 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00006682 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04006683{
Yan Zheng11833d62009-09-11 16:11:19 -04006684 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04006685 struct btrfs_space_info *space_info;
6686 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006687 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04006688 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006689 u64 total_unpinned = 0;
6690 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04006691 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04006692
6693 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04006694 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04006695 if (!cache ||
6696 start >= cache->key.objectid + cache->key.offset) {
6697 if (cache)
6698 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04006699 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006700 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006701 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04006702
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006703 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04006704 cache->space_info,
6705 &empty_cluster);
6706 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04006707 }
6708
6709 len = cache->key.objectid + cache->key.offset - start;
6710 len = min(len, end + 1 - start);
6711
6712 if (start < cache->last_byte_to_unpin) {
6713 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00006714 if (return_free_space)
6715 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04006716 }
Josef Bacik25179202008-10-29 14:49:05 -04006717
Yan, Zhengf0486c62010-05-16 10:46:25 -04006718 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006719 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006720 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006721
Josef Bacikc759c4e2015-10-02 15:25:10 -04006722 /*
6723 * If this space cluster has been marked as fragmented and we've
6724 * unpinned enough in this block group to potentially allow a
6725 * cluster to be created inside of it go ahead and clear the
6726 * fragmented check.
6727 */
6728 if (cluster && cluster->fragmented &&
6729 total_unpinned > empty_cluster) {
6730 spin_lock(&cluster->lock);
6731 cluster->fragmented = 0;
6732 spin_unlock(&cluster->lock);
6733 }
6734
Josef Bacik7b398f82012-10-22 15:52:28 -04006735 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006736 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006737 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006738 space_info->bytes_pinned -= len;
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006739
6740 trace_btrfs_space_reservation(fs_info, "pinned",
6741 space_info->flags, len, 0);
Josef Bacik4f4db212015-09-29 11:40:47 -04006742 space_info->max_extent_size = 0;
Liu Bod288db52014-07-02 16:58:01 +08006743 percpu_counter_add(&space_info->total_bytes_pinned, -len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006744 if (cache->ro) {
6745 space_info->bytes_readonly += len;
6746 readonly = true;
6747 }
Josef Bacik25179202008-10-29 14:49:05 -04006748 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006749 if (!readonly && return_free_space &&
6750 global_rsv->space_info == space_info) {
6751 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03006752
Josef Bacik7b398f82012-10-22 15:52:28 -04006753 spin_lock(&global_rsv->lock);
6754 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04006755 to_add = min(len, global_rsv->size -
6756 global_rsv->reserved);
6757 global_rsv->reserved += to_add;
6758 space_info->bytes_may_use += to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006759 if (global_rsv->reserved >= global_rsv->size)
6760 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04006761 trace_btrfs_space_reservation(fs_info,
6762 "space_info",
6763 space_info->flags,
6764 to_add, 1);
6765 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006766 }
6767 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006768 /* Add to any tickets we may have */
6769 if (len)
6770 space_info_add_new_bytes(fs_info, space_info,
6771 len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006772 }
6773 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006774 }
6775
6776 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006777 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04006778 return 0;
6779}
6780
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02006781int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
Chris Masona28ec192007-03-06 20:08:01 -05006782{
Nikolay Borisov5ead2dd2018-03-15 16:00:26 +02006783 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006784 struct btrfs_block_group_cache *block_group, *tmp;
6785 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04006786 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04006787 u64 start;
6788 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05006789 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05006790
Yan Zheng11833d62009-09-11 16:11:19 -04006791 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6792 unpin = &fs_info->freed_extents[1];
6793 else
6794 unpin = &fs_info->freed_extents[0];
6795
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006796 while (!trans->aborted) {
Filipe Mananad4b450c2015-01-29 19:18:25 +00006797 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04006798 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04006799 EXTENT_DIRTY, NULL);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006800 if (ret) {
6801 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05006802 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00006803 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006804
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006805 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006806 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00006807 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006808
David Sterbaaf6f8f62016-04-26 23:54:39 +02006809 clear_extent_dirty(unpin, start, end);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006810 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006811 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masonb9473432009-03-13 11:00:37 -04006812 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05006813 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006814
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006815 /*
6816 * Transaction is finished. We don't need the lock anymore. We
6817 * do need to clean up the block groups in case of a transaction
6818 * abort.
6819 */
6820 deleted_bgs = &trans->transaction->deleted_bgs;
6821 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
6822 u64 trimmed = 0;
6823
6824 ret = -EROFS;
6825 if (!trans->aborted)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006826 ret = btrfs_discard_extent(fs_info,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006827 block_group->key.objectid,
6828 block_group->key.offset,
6829 &trimmed);
6830
6831 list_del_init(&block_group->bg_list);
6832 btrfs_put_block_group_trimming(block_group);
6833 btrfs_put_block_group(block_group);
6834
6835 if (ret) {
6836 const char *errstr = btrfs_decode_error(ret);
6837 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02006838 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006839 ret, errstr);
6840 }
6841 }
6842
Chris Masone20d96d2007-03-22 12:13:20 -04006843 return 0;
6844}
6845
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006846static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006847 struct btrfs_fs_info *info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006848 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006849 u64 root_objectid, u64 owner_objectid,
6850 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006851 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05006852{
Chris Masone2fa7222007-03-12 16:22:34 -04006853 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006854 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04006855 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04006856 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006857 struct btrfs_extent_item *ei;
6858 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05006859 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006860 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05006861 int extent_slot = 0;
6862 int found_extent = 0;
6863 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006864 u32 item_size;
6865 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006866 u64 bytenr = node->bytenr;
6867 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07006868 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006869 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05006870
Chris Mason5caf2a02007-04-02 11:20:42 -04006871 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04006872 if (!path)
6873 return -ENOMEM;
6874
David Sterbae4058b52015-11-27 16:31:35 +01006875 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04006876 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006877
6878 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
6879 BUG_ON(!is_data && refs_to_drop != 1);
6880
Josef Bacik3173a182013-03-07 14:22:04 -05006881 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02006882 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05006883
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05006884 ret = lookup_extent_backref(trans, info, path, &iref,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006885 bytenr, num_bytes, parent,
6886 root_objectid, owner_objectid,
6887 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05006888 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05006889 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006890 while (extent_slot >= 0) {
6891 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05006892 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006893 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05006894 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006895 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
6896 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05006897 found_extent = 1;
6898 break;
6899 }
Josef Bacik3173a182013-03-07 14:22:04 -05006900 if (key.type == BTRFS_METADATA_ITEM_KEY &&
6901 key.offset == owner_objectid) {
6902 found_extent = 1;
6903 break;
6904 }
Chris Mason952fcca2008-02-18 16:33:44 -05006905 if (path->slots[0] - extent_slot > 5)
6906 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006907 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05006908 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006909#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6910 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
6911 if (found_extent && item_size < sizeof(*ei))
6912 found_extent = 0;
6913#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04006914 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006915 BUG_ON(iref);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05006916 ret = remove_extent_backref(trans, info, path, NULL,
6917 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07006918 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06006919 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006920 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006921 goto out;
6922 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006923 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04006924 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006925
6926 key.objectid = bytenr;
6927 key.type = BTRFS_EXTENT_ITEM_KEY;
6928 key.offset = num_bytes;
6929
Josef Bacik3173a182013-03-07 14:22:04 -05006930 if (!is_data && skinny_metadata) {
6931 key.type = BTRFS_METADATA_ITEM_KEY;
6932 key.offset = owner_objectid;
6933 }
6934
Zheng Yan31840ae2008-09-23 13:14:14 -04006935 ret = btrfs_search_slot(trans, extent_root,
6936 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05006937 if (ret > 0 && skinny_metadata && path->slots[0]) {
6938 /*
6939 * Couldn't find our skinny metadata item,
6940 * see if we have ye olde extent item.
6941 */
6942 path->slots[0]--;
6943 btrfs_item_key_to_cpu(path->nodes[0], &key,
6944 path->slots[0]);
6945 if (key.objectid == bytenr &&
6946 key.type == BTRFS_EXTENT_ITEM_KEY &&
6947 key.offset == num_bytes)
6948 ret = 0;
6949 }
6950
6951 if (ret > 0 && skinny_metadata) {
6952 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01006953 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05006954 key.type = BTRFS_EXTENT_ITEM_KEY;
6955 key.offset = num_bytes;
6956 btrfs_release_path(path);
6957 ret = btrfs_search_slot(trans, extent_root,
6958 &key, path, -1, 1);
6959 }
6960
Josef Bacikf3465ca2008-11-12 14:19:50 -05006961 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006962 btrfs_err(info,
6963 "umm, got %d back from search, was looking for %llu",
6964 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00006965 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02006966 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006967 }
David Sterba005d6422012-09-18 07:52:32 -06006968 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006969 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006970 goto out;
6971 }
Zheng Yan31840ae2008-09-23 13:14:14 -04006972 extent_slot = path->slots[0];
6973 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306974 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02006975 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006976 btrfs_err(info,
6977 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006978 bytenr, parent, root_objectid, owner_objectid,
6979 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04006980 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04006981 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006982 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04006983 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006984 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05006985 }
Chris Mason5f39d392007-10-15 16:14:19 -04006986
6987 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006988 item_size = btrfs_item_size_nr(leaf, extent_slot);
6989#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6990 if (item_size < sizeof(*ei)) {
6991 BUG_ON(found_extent || extent_slot != path->slots[0]);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05006992 ret = convert_extent_item_v0(trans, info, path, owner_objectid,
6993 0);
David Sterba005d6422012-09-18 07:52:32 -06006994 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006995 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006996 goto out;
6997 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006998
David Sterbab3b4aa72011-04-21 01:20:15 +02006999 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007000 path->leave_spinning = 1;
7001
7002 key.objectid = bytenr;
7003 key.type = BTRFS_EXTENT_ITEM_KEY;
7004 key.offset = num_bytes;
7005
7006 ret = btrfs_search_slot(trans, extent_root, &key, path,
7007 -1, 1);
7008 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007009 btrfs_err(info,
7010 "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007011 ret, bytenr);
David Sterbaa4f78752017-06-29 18:37:49 +02007012 btrfs_print_leaf(path->nodes[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007013 }
David Sterba005d6422012-09-18 07:52:32 -06007014 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007015 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007016 goto out;
7017 }
7018
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007019 extent_slot = path->slots[0];
7020 leaf = path->nodes[0];
7021 item_size = btrfs_item_size_nr(leaf, extent_slot);
7022 }
7023#endif
7024 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05007025 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04007026 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05007027 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
7028 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007029 struct btrfs_tree_block_info *bi;
7030 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
7031 bi = (struct btrfs_tree_block_info *)(ei + 1);
7032 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05007033 }
7034
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007035 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04007036 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007037 btrfs_err(info,
7038 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
7039 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04007040 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04007041 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04007042 goto out;
7043 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007044 refs -= refs_to_drop;
7045
7046 if (refs > 0) {
7047 if (extent_op)
7048 __run_delayed_extent_op(extent_op, leaf, ei);
7049 /*
7050 * In the case of inline back ref, reference count will
7051 * be updated by remove_extent_backref
7052 */
7053 if (iref) {
7054 BUG_ON(!found_extent);
7055 } else {
7056 btrfs_set_extent_refs(leaf, ei, refs);
7057 btrfs_mark_buffer_dirty(leaf);
7058 }
7059 if (found_extent) {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05007060 ret = remove_extent_backref(trans, info, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007061 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07007062 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06007063 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007064 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007065 goto out;
7066 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007067 }
7068 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007069 if (found_extent) {
7070 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08007071 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007072 if (iref) {
7073 BUG_ON(path->slots[0] != extent_slot);
7074 } else {
7075 BUG_ON(path->slots[0] != extent_slot + 1);
7076 path->slots[0] = extent_slot;
7077 num_to_del = 2;
7078 }
Chris Mason78fae272007-03-25 11:35:08 -04007079 }
Chris Masonb9473432009-03-13 11:00:37 -04007080
Josef Bacikfcebe452014-05-13 17:30:47 -07007081 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05007082 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
7083 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06007084 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007085 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007086 goto out;
7087 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007088 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01007089
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007090 if (is_data) {
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007091 ret = btrfs_del_csums(trans, info, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06007092 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007093 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007094 goto out;
7095 }
Chris Mason459931e2008-12-10 09:10:46 -05007096 }
7097
Nikolay Borisove7355e52018-05-10 15:44:55 +03007098 ret = add_to_free_space_tree(trans, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007099 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007100 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007101 goto out;
7102 }
7103
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007104 ret = update_block_group(trans, info, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06007105 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007106 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007107 goto out;
7108 }
Chris Masona28ec192007-03-06 20:08:01 -05007109 }
Josef Bacikfcebe452014-05-13 17:30:47 -07007110 btrfs_release_path(path);
7111
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007112out:
Chris Mason5caf2a02007-04-02 11:20:42 -04007113 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05007114 return ret;
7115}
7116
7117/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007118 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04007119 * delayed ref for that extent as well. This searches the delayed ref tree for
7120 * a given extent, and if there are no other delayed refs to be processed, it
7121 * removes it from the tree.
7122 */
7123static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007124 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04007125{
7126 struct btrfs_delayed_ref_head *head;
7127 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007128 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04007129
7130 delayed_refs = &trans->transaction->delayed_refs;
7131 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08007132 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04007133 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08007134 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04007135
Josef Bacikd7df2c72014-01-23 09:21:38 -05007136 spin_lock(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04007137 if (!RB_EMPTY_ROOT(&head->ref_tree))
Chris Mason1887be62009-03-13 10:11:24 -04007138 goto out;
7139
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007140 if (head->extent_op) {
7141 if (!head->must_insert_reserved)
7142 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00007143 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007144 head->extent_op = NULL;
7145 }
7146
Chris Mason1887be62009-03-13 10:11:24 -04007147 /*
7148 * waiting for the lock here would deadlock. If someone else has it
7149 * locked they are already in the process of dropping it anyway
7150 */
7151 if (!mutex_trylock(&head->mutex))
7152 goto out;
7153
7154 /*
7155 * at this point we have a head with no other entries. Go
7156 * ahead and process it.
7157 */
Liu Boc46effa2013-10-14 12:59:45 +08007158 rb_erase(&head->href_node, &delayed_refs->href_root);
Josef Bacikd2788502017-09-29 15:43:57 -04007159 RB_CLEAR_NODE(&head->href_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05007160 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04007161
7162 /*
7163 * we don't take a ref on the node because we're removing it from the
7164 * tree, so we just steal the ref the tree was holding.
7165 */
Chris Masonc3e69d52009-03-13 10:17:05 -04007166 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007167 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04007168 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007169 head->processing = 0;
7170 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04007171 spin_unlock(&delayed_refs->lock);
7172
Yan, Zhengf0486c62010-05-16 10:46:25 -04007173 BUG_ON(head->extent_op);
7174 if (head->must_insert_reserved)
7175 ret = 1;
7176
7177 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04007178 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007179 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04007180out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05007181 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08007182
7183out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04007184 spin_unlock(&delayed_refs->lock);
7185 return 0;
7186}
7187
Yan, Zhengf0486c62010-05-16 10:46:25 -04007188void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7189 struct btrfs_root *root,
7190 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02007191 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007192{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007193 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb150a4f2013-06-19 15:00:04 -04007194 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007195 int ret;
7196
7197 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07007198 int old_ref_mod, new_ref_mod;
7199
Josef Bacikfd708b82017-09-29 15:43:50 -04007200 btrfs_ref_tree_mod(root, buf->start, buf->len, parent,
7201 root->root_key.objectid,
7202 btrfs_header_level(buf), 0,
7203 BTRFS_DROP_DELAYED_REF);
Omar Sandoval7be07912017-06-06 16:45:30 -07007204 ret = btrfs_add_delayed_tree_ref(fs_info, trans, buf->start,
7205 buf->len, parent,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007206 root->root_key.objectid,
7207 btrfs_header_level(buf),
Omar Sandoval7be07912017-06-06 16:45:30 -07007208 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007209 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007210 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07007211 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007212 }
7213
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007214 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
Filipe Manana62198722015-01-06 20:18:45 +00007215 struct btrfs_block_group_cache *cache;
7216
Yan, Zhengf0486c62010-05-16 10:46:25 -04007217 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007218 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007219 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04007220 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007221 }
7222
Omar Sandoval4da8b762017-06-06 16:45:28 -07007223 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007224 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00007225
Yan, Zhengf0486c62010-05-16 10:46:25 -04007226 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007227 pin_down_extent(fs_info, cache, buf->start,
7228 buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00007229 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04007230 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007231 }
7232
7233 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
7234
7235 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08007236 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00007237 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007238 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007239 }
7240out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04007241 if (pin)
Nikolay Borisov29d2b842018-03-30 12:58:47 +03007242 add_pinned_bytes(fs_info, buf->len, true,
Josef Bacikb150a4f2013-06-19 15:00:04 -04007243 root->root_key.objectid);
7244
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007245 if (last_ref) {
7246 /*
7247 * Deleting the buffer, clear the corrupt flag since it doesn't
7248 * matter anymore.
7249 */
7250 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
7251 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007252}
7253
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007254/* Can return -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007255int btrfs_free_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007256 struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007257 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01007258 u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04007259{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007260 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07007261 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04007262 int ret;
7263
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04007264 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007265 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02007266
Josef Bacikfd708b82017-09-29 15:43:50 -04007267 if (root_objectid != BTRFS_TREE_LOG_OBJECTID)
7268 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent,
7269 root_objectid, owner, offset,
7270 BTRFS_DROP_DELAYED_REF);
7271
Chris Mason56bec292009-03-13 10:10:06 -04007272 /*
7273 * tree log blocks never actually go into the extent allocation
7274 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04007275 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007276 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
7277 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04007278 /* unlocks the pinned mutex */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007279 btrfs_pin_extent(fs_info, bytenr, num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07007280 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04007281 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007282 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007283 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007284 num_bytes, parent,
7285 root_objectid, (int)owner,
7286 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007287 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007288 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007289 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007290 num_bytes, parent,
7291 root_objectid, owner, offset,
7292 0, BTRFS_DROP_DELAYED_REF,
Omar Sandovald7eae342017-06-06 16:45:31 -07007293 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04007294 }
Omar Sandovald7eae342017-06-06 16:45:31 -07007295
Nikolay Borisov29d2b842018-03-30 12:58:47 +03007296 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0) {
7297 bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
7298
7299 add_pinned_bytes(fs_info, num_bytes, metadata, root_objectid);
7300 }
Omar Sandovald7eae342017-06-06 16:45:31 -07007301
Chris Mason925baed2008-06-25 16:01:30 -04007302 return ret;
7303}
7304
Chris Masonfec577f2007-02-26 10:40:21 -05007305/*
Josef Bacik817d52f2009-07-13 21:29:25 -04007306 * when we wait for progress in the block group caching, its because
7307 * our allocation attempt failed at least once. So, we must sleep
7308 * and let some progress happen before we try again.
7309 *
7310 * This function will sleep at least once waiting for new free space to
7311 * show up, and then it will check the block group free space numbers
7312 * for our min num_bytes. Another option is to have it go ahead
7313 * and look in the rbtree for a free extent of a given size, but this
7314 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04007315 *
7316 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
7317 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04007318 */
Josef Bacik36cce922013-08-05 11:15:21 -04007319static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04007320wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
7321 u64 num_bytes)
7322{
Yan Zheng11833d62009-09-11 16:11:19 -04007323 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04007324
Yan Zheng11833d62009-09-11 16:11:19 -04007325 caching_ctl = get_caching_control(cache);
7326 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007327 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04007328
Yan Zheng11833d62009-09-11 16:11:19 -04007329 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08007330 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04007331
7332 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04007333}
7334
7335static noinline int
7336wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
7337{
7338 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04007339 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007340
7341 caching_ctl = get_caching_control(cache);
7342 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007343 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007344
7345 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04007346 if (cache->cached == BTRFS_CACHE_ERROR)
7347 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04007348 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04007349 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04007350}
7351
7352enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05007353 LOOP_CACHING_NOWAIT = 0,
7354 LOOP_CACHING_WAIT = 1,
7355 LOOP_ALLOC_CHUNK = 2,
7356 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04007357};
7358
Miao Xiee570fd22014-06-19 10:42:50 +08007359static inline void
7360btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
7361 int delalloc)
7362{
7363 if (delalloc)
7364 down_read(&cache->data_rwsem);
7365}
7366
7367static inline void
7368btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
7369 int delalloc)
7370{
7371 btrfs_get_block_group(cache);
7372 if (delalloc)
7373 down_read(&cache->data_rwsem);
7374}
7375
7376static struct btrfs_block_group_cache *
7377btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
7378 struct btrfs_free_cluster *cluster,
7379 int delalloc)
7380{
Sudip Mukherjee89771cc2016-02-16 13:32:47 +05307381 struct btrfs_block_group_cache *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007382
Miao Xiee570fd22014-06-19 10:42:50 +08007383 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007384 while (1) {
7385 used_bg = cluster->block_group;
7386 if (!used_bg)
7387 return NULL;
7388
7389 if (used_bg == block_group)
7390 return used_bg;
7391
7392 btrfs_get_block_group(used_bg);
7393
7394 if (!delalloc)
7395 return used_bg;
7396
7397 if (down_read_trylock(&used_bg->data_rwsem))
7398 return used_bg;
7399
7400 spin_unlock(&cluster->refill_lock);
7401
Liu Boe321f8a2016-11-30 16:11:04 -08007402 /* We should only have one-level nested. */
7403 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007404
7405 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007406 if (used_bg == cluster->block_group)
7407 return used_bg;
7408
7409 up_read(&used_bg->data_rwsem);
7410 btrfs_put_block_group(used_bg);
7411 }
Miao Xiee570fd22014-06-19 10:42:50 +08007412}
7413
7414static inline void
7415btrfs_release_block_group(struct btrfs_block_group_cache *cache,
7416 int delalloc)
7417{
7418 if (delalloc)
7419 up_read(&cache->data_rwsem);
7420 btrfs_put_block_group(cache);
7421}
7422
Josef Bacik817d52f2009-07-13 21:29:25 -04007423/*
Chris Masonfec577f2007-02-26 10:40:21 -05007424 * walks the btree of allocated extents and find a hole of a given size.
7425 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08007426 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04007427 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08007428 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05007429 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08007430 *
7431 * If there is no suitable free space, we will record the max size of
7432 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05007433 */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05007434static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08007435 u64 ram_bytes, u64 num_bytes, u64 empty_size,
7436 u64 hint_byte, struct btrfs_key *ins,
7437 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007438{
Josef Bacik80eb2342008-10-29 14:49:05 -04007439 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007440 struct btrfs_root *root = fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007441 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04007442 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05007443 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08007444 u64 max_extent_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007445 u64 empty_cluster = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04007446 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007447 int loop = 0;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007448 int index = btrfs_bg_flags_to_raid_index(flags);
Josef Bacik0a243252009-09-11 16:11:20 -04007449 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007450 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04007451 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08007452 bool have_caching_bg = false;
chandan13a0db52015-11-02 13:59:46 +05307453 bool orig_have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007454 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05007455
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007456 WARN_ON(num_bytes < fs_info->sectorsize);
David Sterba962a2982014-06-04 18:41:45 +02007457 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04007458 ins->objectid = 0;
7459 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04007460
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007461 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05007462
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007463 space_info = __find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007464 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007465 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007466 return -ENOSPC;
7467 }
Josef Bacik2552d172009-04-03 10:14:19 -04007468
Josef Bacik67377732010-09-16 16:19:09 -04007469 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04007470 * If our free space is heavily fragmented we may not be able to make
7471 * big contiguous allocations, so instead of doing the expensive search
7472 * for free space, simply return ENOSPC with our max_extent_size so we
7473 * can go ahead and search for a more manageable chunk.
7474 *
7475 * If our max_extent_size is large enough for our allocation simply
7476 * disable clustering since we will likely not be able to find enough
7477 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04007478 */
Josef Bacik4f4db212015-09-29 11:40:47 -04007479 if (unlikely(space_info->max_extent_size)) {
7480 spin_lock(&space_info->lock);
7481 if (space_info->max_extent_size &&
7482 num_bytes > space_info->max_extent_size) {
7483 ins->offset = space_info->max_extent_size;
7484 spin_unlock(&space_info->lock);
7485 return -ENOSPC;
7486 } else if (space_info->max_extent_size) {
7487 use_cluster = false;
7488 }
7489 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007490 }
7491
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007492 last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04007493 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007494 spin_lock(&last_ptr->lock);
7495 if (last_ptr->block_group)
7496 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007497 if (last_ptr->fragmented) {
7498 /*
7499 * We still set window_start so we can keep track of the
7500 * last place we found an allocation to try and save
7501 * some time.
7502 */
7503 hint_byte = last_ptr->window_start;
7504 use_cluster = false;
7505 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007506 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007507 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007508
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007509 search_start = max(search_start, first_logical_byte(fs_info, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04007510 search_start = max(search_start, hint_byte);
Josef Bacik2552d172009-04-03 10:14:19 -04007511 if (search_start == hint_byte) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007512 block_group = btrfs_lookup_block_group(fs_info, search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04007513 /*
7514 * we don't want to use the block group if it doesn't match our
7515 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05007516 *
7517 * However if we are re-searching with an ideal block group
7518 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04007519 */
David Sterbab6919a52013-04-29 13:39:40 +00007520 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05007521 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04007522 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007523 if (list_empty(&block_group->list) ||
7524 block_group->ro) {
7525 /*
7526 * someone is removing this block group,
7527 * we can't jump into the have_block_group
7528 * target because our list pointers are not
7529 * valid
7530 */
7531 btrfs_put_block_group(block_group);
7532 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05007533 } else {
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007534 index = btrfs_bg_flags_to_raid_index(
7535 block_group->flags);
Miao Xiee570fd22014-06-19 10:42:50 +08007536 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04007537 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05007538 }
Josef Bacik2552d172009-04-03 10:14:19 -04007539 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007540 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04007541 }
Chris Mason42e70e72008-11-07 18:17:11 -05007542 }
Josef Bacik2552d172009-04-03 10:14:19 -04007543search:
Miao Xie60d2adb2011-09-09 17:34:35 +08007544 have_caching_bg = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007545 if (index == 0 || index == btrfs_bg_flags_to_raid_index(flags))
Josef Bacika5e681d2015-10-01 14:54:10 -04007546 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04007547 down_read(&space_info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007548 list_for_each_entry(block_group, &space_info->block_groups[index],
7549 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04007550 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04007551 int cached;
Chris Mason8a1413a22008-11-10 16:13:54 -05007552
Jeff Mahoney14443932017-07-19 23:25:51 -04007553 /* If the block group is read-only, we can skip it entirely. */
7554 if (unlikely(block_group->ro))
7555 continue;
7556
Miao Xiee570fd22014-06-19 10:42:50 +08007557 btrfs_grab_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007558 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05007559
Chris Mason83a50de2010-12-13 15:06:46 -05007560 /*
7561 * this can happen if we end up cycling through all the
7562 * raid types, but we want to make sure we only allocate
7563 * for the proper type.
7564 */
David Sterbab6919a52013-04-29 13:39:40 +00007565 if (!block_group_bits(block_group, flags)) {
Bart Van Asschebece2e82018-06-20 10:03:31 -07007566 u64 extra = BTRFS_BLOCK_GROUP_DUP |
Chris Mason83a50de2010-12-13 15:06:46 -05007567 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007568 BTRFS_BLOCK_GROUP_RAID5 |
7569 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05007570 BTRFS_BLOCK_GROUP_RAID10;
7571
7572 /*
7573 * if they asked for extra copies and this block group
7574 * doesn't provide them, bail. This does allow us to
7575 * fill raid0 from raid1.
7576 */
David Sterbab6919a52013-04-29 13:39:40 +00007577 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05007578 goto loop;
7579 }
7580
Josef Bacik2552d172009-04-03 10:14:19 -04007581have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05007582 cached = block_group_cache_done(block_group);
7583 if (unlikely(!cached)) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007584 have_caching_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00007585 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04007586 BUG_ON(ret < 0);
7587 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05007588 }
7589
Josef Bacik36cce922013-08-05 11:15:21 -04007590 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
7591 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007592
Josef Bacik0a243252009-09-11 16:11:20 -04007593 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007594 * Ok we want to try and use the cluster allocator, so
7595 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04007596 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04007597 if (last_ptr && use_cluster) {
Miao Xie215a63d2014-01-15 20:00:56 +08007598 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05007599 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007600 /*
7601 * the refill lock keeps out other
7602 * people trying to start a new cluster
7603 */
Miao Xiee570fd22014-06-19 10:42:50 +08007604 used_block_group = btrfs_lock_cluster(block_group,
7605 last_ptr,
7606 delalloc);
7607 if (!used_block_group)
Chris Mason44fb5512009-06-04 15:34:51 -04007608 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04007609
Miao Xiee570fd22014-06-19 10:42:50 +08007610 if (used_block_group != block_group &&
7611 (used_block_group->ro ||
7612 !block_group_bits(used_block_group, flags)))
7613 goto release_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007614
7615 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007616 last_ptr,
7617 num_bytes,
7618 used_block_group->key.objectid,
7619 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007620 if (offset) {
7621 /* we have a block, we're done */
7622 spin_unlock(&last_ptr->refill_lock);
Qu Wenruo3dca5c92018-04-26 14:24:25 +08007623 trace_btrfs_reserve_extent_cluster(
Miao Xie89d43462014-01-15 20:00:57 +08007624 used_block_group,
7625 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08007626 if (used_block_group != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08007627 btrfs_release_block_group(block_group,
7628 delalloc);
Miao Xie215a63d2014-01-15 20:00:56 +08007629 block_group = used_block_group;
7630 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007631 goto checks;
7632 }
7633
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007634 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007635release_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007636 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7637 * set up a new clusters, so lets just skip it
7638 * and let the allocator find whatever block
7639 * it can find. If we reach this point, we
7640 * will have tried the cluster allocator
7641 * plenty of times and not have found
7642 * anything, so we are likely way too
7643 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007644 * anything.
7645 *
7646 * However, if the cluster is taken from the
7647 * current block group, release the cluster
7648 * first, so that we stand a better chance of
7649 * succeeding in the unclustered
7650 * allocation. */
7651 if (loop >= LOOP_NO_EMPTY_SIZE &&
Miao Xiee570fd22014-06-19 10:42:50 +08007652 used_block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007653 spin_unlock(&last_ptr->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007654 btrfs_release_block_group(used_block_group,
7655 delalloc);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007656 goto unclustered_alloc;
7657 }
7658
Chris Masonfa9c0d792009-04-03 09:47:43 -04007659 /*
7660 * this cluster didn't work out, free it and
7661 * start over
7662 */
7663 btrfs_return_cluster_to_free_space(NULL, last_ptr);
7664
Miao Xiee570fd22014-06-19 10:42:50 +08007665 if (used_block_group != block_group)
7666 btrfs_release_block_group(used_block_group,
7667 delalloc);
7668refill_cluster:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007669 if (loop >= LOOP_NO_EMPTY_SIZE) {
7670 spin_unlock(&last_ptr->refill_lock);
7671 goto unclustered_alloc;
7672 }
7673
Chris Mason8de972b2013-01-04 15:39:43 -05007674 aligned_cluster = max_t(unsigned long,
7675 empty_cluster + empty_size,
7676 block_group->full_stripe_len);
7677
Chris Masonfa9c0d792009-04-03 09:47:43 -04007678 /* allocate a cluster in this block group */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007679 ret = btrfs_find_space_cluster(fs_info, block_group,
Josef Bacik00361582013-08-14 14:02:47 -04007680 last_ptr, search_start,
7681 num_bytes,
7682 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007683 if (ret == 0) {
7684 /*
7685 * now pull our allocation out of this
7686 * cluster
7687 */
7688 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007689 last_ptr,
7690 num_bytes,
7691 search_start,
7692 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007693 if (offset) {
7694 /* we found one, proceed */
7695 spin_unlock(&last_ptr->refill_lock);
Qu Wenruo3dca5c92018-04-26 14:24:25 +08007696 trace_btrfs_reserve_extent_cluster(
Josef Bacik3f7de032011-11-10 08:29:20 -05007697 block_group, search_start,
7698 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007699 goto checks;
7700 }
Josef Bacik0a243252009-09-11 16:11:20 -04007701 } else if (!cached && loop > LOOP_CACHING_NOWAIT
7702 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007703 spin_unlock(&last_ptr->refill_lock);
7704
Josef Bacik0a243252009-09-11 16:11:20 -04007705 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007706 wait_block_group_cache_progress(block_group,
7707 num_bytes + empty_cluster + empty_size);
7708 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007709 }
Josef Bacik817d52f2009-07-13 21:29:25 -04007710
Chris Masonfa9c0d792009-04-03 09:47:43 -04007711 /*
7712 * at this point we either didn't find a cluster
7713 * or we weren't able to allocate a block from our
7714 * cluster. Free the cluster we've been trying
7715 * to use, and go to the next block group
7716 */
Josef Bacik0a243252009-09-11 16:11:20 -04007717 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007718 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04007719 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007720 }
7721
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007722unclustered_alloc:
Josef Bacikc759c4e2015-10-02 15:25:10 -04007723 /*
7724 * We are doing an unclustered alloc, set the fragmented flag so
7725 * we don't bother trying to setup a cluster again until we get
7726 * more space.
7727 */
7728 if (unlikely(last_ptr)) {
7729 spin_lock(&last_ptr->lock);
7730 last_ptr->fragmented = 1;
7731 spin_unlock(&last_ptr->lock);
7732 }
Liu Bo0c9b36e2017-02-13 15:42:30 -08007733 if (cached) {
7734 struct btrfs_free_space_ctl *ctl =
7735 block_group->free_space_ctl;
7736
7737 spin_lock(&ctl->tree_lock);
7738 if (ctl->free_space <
7739 num_bytes + empty_cluster + empty_size) {
7740 if (ctl->free_space > max_extent_size)
7741 max_extent_size = ctl->free_space;
7742 spin_unlock(&ctl->tree_lock);
7743 goto loop;
7744 }
7745 spin_unlock(&ctl->tree_lock);
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007746 }
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007747
Josef Bacik6226cb02009-04-03 10:14:18 -04007748 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08007749 num_bytes, empty_size,
7750 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007751 /*
7752 * If we didn't find a chunk, and we haven't failed on this
7753 * block group before, and this block group is in the middle of
7754 * caching and we are ok with waiting, then go ahead and wait
7755 * for progress to be made, and set failed_alloc to true.
7756 *
7757 * If failed_alloc is true then we've already waited on this
7758 * block group once and should move on to the next block group.
7759 */
7760 if (!offset && !failed_alloc && !cached &&
7761 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007762 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007763 num_bytes + empty_size);
7764 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007765 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007766 } else if (!offset) {
7767 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04007768 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007769checks:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007770 search_start = ALIGN(offset, fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04007771
Josef Bacik2552d172009-04-03 10:14:19 -04007772 /* move on to the next group */
7773 if (search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08007774 block_group->key.objectid + block_group->key.offset) {
7775 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04007776 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04007777 }
Josef Bacik80eb2342008-10-29 14:49:05 -04007778
Josef Bacik6226cb02009-04-03 10:14:18 -04007779 if (offset < search_start)
Miao Xie215a63d2014-01-15 20:00:56 +08007780 btrfs_add_free_space(block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04007781 search_start - offset);
7782 BUG_ON(offset > search_start);
7783
Wang Xiaoguang18513092016-07-25 15:51:40 +08007784 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
7785 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007786 if (ret == -EAGAIN) {
Miao Xie215a63d2014-01-15 20:00:56 +08007787 btrfs_add_free_space(block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007788 goto loop;
7789 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007790 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04007791
Josef Bacik2552d172009-04-03 10:14:19 -04007792 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007793 ins->objectid = search_start;
7794 ins->offset = num_bytes;
7795
Qu Wenruo3dca5c92018-04-26 14:24:25 +08007796 trace_btrfs_reserve_extent(block_group, search_start, num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08007797 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007798 break;
7799loop:
Josef Bacik0a243252009-09-11 16:11:20 -04007800 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007801 failed_alloc = false;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08007802 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
7803 index);
Miao Xiee570fd22014-06-19 10:42:50 +08007804 btrfs_release_block_group(block_group, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04007805 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04007806 }
7807 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05007808
chandan13a0db52015-11-02 13:59:46 +05307809 if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg
7810 && !orig_have_caching_bg)
7811 orig_have_caching_bg = true;
7812
Miao Xie60d2adb2011-09-09 17:34:35 +08007813 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
7814 goto search;
7815
Yan, Zhengb742bb822010-05-16 10:46:24 -04007816 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
7817 goto search;
7818
Josef Bacik285ff5a2012-01-13 15:27:45 -05007819 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05007820 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
7821 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04007822 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
7823 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
7824 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
7825 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04007826 */
Josef Bacik723bda22011-05-27 16:11:38 -04007827 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb822010-05-16 10:46:24 -04007828 index = 0;
Josef Bacika5e681d2015-10-01 14:54:10 -04007829 if (loop == LOOP_CACHING_NOWAIT) {
7830 /*
7831 * We want to skip the LOOP_CACHING_WAIT step if we
Nicholas D Steeves01327612016-05-19 21:18:45 -04007832 * don't have any uncached bgs and we've already done a
Josef Bacika5e681d2015-10-01 14:54:10 -04007833 * full search through.
7834 */
chandan13a0db52015-11-02 13:59:46 +05307835 if (orig_have_caching_bg || !full_search)
Josef Bacika5e681d2015-10-01 14:54:10 -04007836 loop = LOOP_CACHING_WAIT;
7837 else
7838 loop = LOOP_ALLOC_CHUNK;
7839 } else {
7840 loop++;
7841 }
7842
Josef Bacik817d52f2009-07-13 21:29:25 -04007843 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04007844 struct btrfs_trans_handle *trans;
Wang Shilongf017f152014-03-13 13:19:47 +08007845 int exist = 0;
Josef Bacik00361582013-08-14 14:02:47 -04007846
Wang Shilongf017f152014-03-13 13:19:47 +08007847 trans = current->journal_info;
7848 if (trans)
7849 exist = 1;
7850 else
7851 trans = btrfs_join_transaction(root);
7852
Josef Bacik00361582013-08-14 14:02:47 -04007853 if (IS_ERR(trans)) {
7854 ret = PTR_ERR(trans);
7855 goto out;
7856 }
7857
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007858 ret = do_chunk_alloc(trans, fs_info, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04007859 CHUNK_ALLOC_FORCE);
Josef Bacika5e681d2015-10-01 14:54:10 -04007860
7861 /*
7862 * If we can't allocate a new chunk we've already looped
7863 * through at least once, move on to the NO_EMPTY_SIZE
7864 * case.
7865 */
7866 if (ret == -ENOSPC)
7867 loop = LOOP_NO_EMPTY_SIZE;
7868
Josef Bacikea658ba2012-09-11 16:57:25 -04007869 /*
7870 * Do not bail out on ENOSPC since we
7871 * can do more things.
7872 */
Josef Bacik00361582013-08-14 14:02:47 -04007873 if (ret < 0 && ret != -ENOSPC)
Jeff Mahoney66642832016-06-10 18:19:25 -04007874 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -04007875 else
7876 ret = 0;
Wang Shilongf017f152014-03-13 13:19:47 +08007877 if (!exist)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007878 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04007879 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04007880 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04007881 }
7882
7883 if (loop == LOOP_NO_EMPTY_SIZE) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007884 /*
7885 * Don't loop again if we already have no empty_size and
7886 * no empty_cluster.
7887 */
7888 if (empty_size == 0 &&
7889 empty_cluster == 0) {
7890 ret = -ENOSPC;
7891 goto out;
7892 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007893 empty_size = 0;
7894 empty_cluster = 0;
7895 }
Chris Mason42e70e72008-11-07 18:17:11 -05007896
Josef Bacik723bda22011-05-27 16:11:38 -04007897 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04007898 } else if (!ins->objectid) {
7899 ret = -ENOSPC;
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007900 } else if (ins->objectid) {
Josef Bacikc759c4e2015-10-02 15:25:10 -04007901 if (!use_cluster && last_ptr) {
7902 spin_lock(&last_ptr->lock);
7903 last_ptr->window_start = ins->objectid;
7904 spin_unlock(&last_ptr->lock);
7905 }
Josef Bacik2552d172009-04-03 10:14:19 -04007906 ret = 0;
7907 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007908out:
Josef Bacik4f4db212015-09-29 11:40:47 -04007909 if (ret == -ENOSPC) {
7910 spin_lock(&space_info->lock);
7911 space_info->max_extent_size = max_extent_size;
7912 spin_unlock(&space_info->lock);
Miao Xiea4820392013-09-09 13:19:42 +08007913 ins->offset = max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04007914 }
Chris Mason0f70abe2007-02-28 16:46:22 -05007915 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05007916}
Chris Masonec44a352008-04-28 15:29:52 -04007917
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007918static void dump_space_info(struct btrfs_fs_info *fs_info,
7919 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007920 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04007921{
7922 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb822010-05-16 10:46:24 -04007923 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007924
Josef Bacik9ed74f22009-09-11 16:12:44 -04007925 spin_lock(&info->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007926 btrfs_info(fs_info, "space_info %llu has %llu free, is %sfull",
7927 info->flags,
Liu Bo41361352017-02-13 15:42:21 -08007928 info->total_bytes - btrfs_space_info_used(info, true),
7929 info->full ? "" : "not ");
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007930 btrfs_info(fs_info,
7931 "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu",
7932 info->total_bytes, info->bytes_used, info->bytes_pinned,
7933 info->bytes_reserved, info->bytes_may_use,
7934 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007935 spin_unlock(&info->lock);
7936
7937 if (!dump_block_groups)
7938 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007939
Josef Bacik80eb2342008-10-29 14:49:05 -04007940 down_read(&info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007941again:
7942 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04007943 spin_lock(&cache->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007944 btrfs_info(fs_info,
7945 "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s",
7946 cache->key.objectid, cache->key.offset,
7947 btrfs_block_group_used(&cache->item), cache->pinned,
7948 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04007949 btrfs_dump_free_space(cache, bytes);
7950 spin_unlock(&cache->lock);
7951 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04007952 if (++index < BTRFS_NR_RAID_TYPES)
7953 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04007954 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007955}
Zheng Yane8569812008-09-26 10:05:48 -04007956
Nikolay Borisov6f47c702018-03-13 12:22:32 +02007957/*
7958 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
7959 * hole that is at least as big as @num_bytes.
7960 *
7961 * @root - The root that will contain this extent
7962 *
7963 * @ram_bytes - The amount of space in ram that @num_bytes take. This
7964 * is used for accounting purposes. This value differs
7965 * from @num_bytes only in the case of compressed extents.
7966 *
7967 * @num_bytes - Number of bytes to allocate on-disk.
7968 *
7969 * @min_alloc_size - Indicates the minimum amount of space that the
7970 * allocator should try to satisfy. In some cases
7971 * @num_bytes may be larger than what is required and if
7972 * the filesystem is fragmented then allocation fails.
7973 * However, the presence of @min_alloc_size gives a
7974 * chance to try and satisfy the smaller allocation.
7975 *
7976 * @empty_size - A hint that you plan on doing more COW. This is the
7977 * size in bytes the allocator should try to find free
7978 * next to the block it returns. This is just a hint and
7979 * may be ignored by the allocator.
7980 *
7981 * @hint_byte - Hint to the allocator to start searching above the byte
7982 * address passed. It might be ignored.
7983 *
7984 * @ins - This key is modified to record the found hole. It will
7985 * have the following values:
7986 * ins->objectid == start position
7987 * ins->flags = BTRFS_EXTENT_ITEM_KEY
7988 * ins->offset == the size of the hole.
7989 *
7990 * @is_data - Boolean flag indicating whether an extent is
7991 * allocated for data (true) or metadata (false)
7992 *
7993 * @delalloc - Boolean flag indicating whether this allocation is for
7994 * delalloc or not. If 'true' data_rwsem of block groups
7995 * is going to be acquired.
7996 *
7997 *
7998 * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
7999 * case -ENOSPC is returned then @ins->offset will contain the size of the
8000 * largest available hole the allocator managed to find.
8001 */
Wang Xiaoguang18513092016-07-25 15:51:40 +08008002int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04008003 u64 num_bytes, u64 min_alloc_size,
8004 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08008005 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05008006{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008007 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04008008 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00008009 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05008010 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04008011
Jeff Mahoney1b868262017-05-17 11:38:35 -04008012 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04008013again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008014 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05008015 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08008016 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008017 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008018 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008019 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08008020 if (!final_tried && ins->offset) {
8021 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04008022 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008023 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05008024 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08008025 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05008026 if (num_bytes == min_alloc_size)
8027 final_tried = true;
8028 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008029 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05008030 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008031
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008032 sinfo = __find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008033 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008034 "allocation failed flags %llu, wanted %llu",
8035 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01008036 if (sinfo)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008037 dump_space_info(fs_info, sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05008038 }
Chris Mason925baed2008-06-25 16:01:30 -04008039 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008040
8041 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008042}
8043
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008044static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008045 u64 start, u64 len,
8046 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04008047{
Josef Bacik0f9dd462008-09-23 13:14:11 -04008048 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05008049 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008050
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008051 cache = btrfs_lookup_block_group(fs_info, start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008052 if (!cache) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008053 btrfs_err(fs_info, "Unable to find block group for %llu",
8054 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008055 return -ENOSPC;
8056 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05008057
Chris Masone688b7252011-10-31 20:52:39 -04008058 if (pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008059 pin_down_extent(fs_info, cache, start, len, 1);
Chris Masone688b7252011-10-31 20:52:39 -04008060 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008061 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008062 ret = btrfs_discard_extent(fs_info, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04008063 btrfs_add_free_space(cache, start, len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08008064 btrfs_free_reserved_bytes(cache, len, delalloc);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008065 trace_btrfs_reserved_extent_free(fs_info, start, len);
Chris Masone688b7252011-10-31 20:52:39 -04008066 }
Dongsheng Yang31193212014-12-12 16:44:35 +08008067
Chris Masonfa9c0d792009-04-03 09:47:43 -04008068 btrfs_put_block_group(cache);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008069 return ret;
8070}
8071
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008072int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008073 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04008074{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008075 return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04008076}
8077
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008078int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04008079 u64 start, u64 len)
8080{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008081 return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04008082}
8083
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008084static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008085 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008086 u64 parent, u64 root_objectid,
8087 u64 flags, u64 owner, u64 offset,
8088 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008089{
8090 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008091 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008092 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008093 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008094 struct extent_buffer *leaf;
8095 int type;
8096 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04008097
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008098 if (parent > 0)
8099 type = BTRFS_SHARED_DATA_REF_KEY;
8100 else
8101 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04008102
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008103 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05008104
8105 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00008106 if (!path)
8107 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05008108
Chris Masonb9473432009-03-13 11:00:37 -04008109 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008110 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8111 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008112 if (ret) {
8113 btrfs_free_path(path);
8114 return ret;
8115 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008116
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008117 leaf = path->nodes[0];
8118 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05008119 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008120 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
8121 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8122 btrfs_set_extent_flags(leaf, extent_item,
8123 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05008124
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008125 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
8126 btrfs_set_extent_inline_ref_type(leaf, iref, type);
8127 if (parent > 0) {
8128 struct btrfs_shared_data_ref *ref;
8129 ref = (struct btrfs_shared_data_ref *)(iref + 1);
8130 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8131 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
8132 } else {
8133 struct btrfs_extent_data_ref *ref;
8134 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
8135 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
8136 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
8137 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
8138 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
8139 }
Chris Mason47e4bb92008-02-01 14:51:59 -05008140
8141 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05008142 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04008143
Nikolay Borisov25a356d2018-05-10 15:44:54 +03008144 ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008145 if (ret)
8146 return ret;
8147
Jeff Mahoney6202df62016-06-22 18:54:22 -04008148 ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008149 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008150 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008151 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05008152 BUG();
8153 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008154 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008155 return ret;
8156}
8157
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008158static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008159 struct btrfs_delayed_ref_node *node,
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03008160 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008161{
Nikolay Borisov9dcdbe02018-05-21 12:27:20 +03008162 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008163 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008164 struct btrfs_extent_item *extent_item;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008165 struct btrfs_key extent_key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008166 struct btrfs_tree_block_info *block_info;
8167 struct btrfs_extent_inline_ref *iref;
8168 struct btrfs_path *path;
8169 struct extent_buffer *leaf;
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008170 struct btrfs_delayed_tree_ref *ref;
Josef Bacik3173a182013-03-07 14:22:04 -05008171 u32 size = sizeof(*extent_item) + sizeof(*iref);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008172 u64 num_bytes;
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03008173 u64 flags = extent_op->flags_to_set;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008174 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05008175
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008176 ref = btrfs_delayed_node_to_tree_ref(node);
8177
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008178 extent_key.objectid = node->bytenr;
8179 if (skinny_metadata) {
8180 extent_key.offset = ref->level;
8181 extent_key.type = BTRFS_METADATA_ITEM_KEY;
8182 num_bytes = fs_info->nodesize;
8183 } else {
8184 extent_key.offset = node->num_bytes;
8185 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik3173a182013-03-07 14:22:04 -05008186 size += sizeof(*block_info);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008187 num_bytes = node->num_bytes;
8188 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008189
8190 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04008191 if (!path) {
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008192 btrfs_free_and_pin_reserved_extent(fs_info,
8193 extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008194 fs_info->nodesize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07008195 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04008196 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008197
8198 path->leave_spinning = 1;
8199 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008200 &extent_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008201 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07008202 btrfs_free_path(path);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008203 btrfs_free_and_pin_reserved_extent(fs_info,
8204 extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008205 fs_info->nodesize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008206 return ret;
8207 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008208
8209 leaf = path->nodes[0];
8210 extent_item = btrfs_item_ptr(leaf, path->slots[0],
8211 struct btrfs_extent_item);
8212 btrfs_set_extent_refs(leaf, extent_item, 1);
8213 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8214 btrfs_set_extent_flags(leaf, extent_item,
8215 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008216
Josef Bacik3173a182013-03-07 14:22:04 -05008217 if (skinny_metadata) {
8218 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
8219 } else {
8220 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
Nikolay Borisov21ebfbe2018-05-21 12:27:22 +03008221 btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008222 btrfs_set_tree_block_level(leaf, block_info, ref->level);
Josef Bacik3173a182013-03-07 14:22:04 -05008223 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
8224 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008225
Nikolay Borisovd4b20732018-05-21 12:27:23 +03008226 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008227 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
8228 btrfs_set_extent_inline_ref_type(leaf, iref,
8229 BTRFS_SHARED_BLOCK_REF_KEY);
Nikolay Borisovd4b20732018-05-21 12:27:23 +03008230 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008231 } else {
8232 btrfs_set_extent_inline_ref_type(leaf, iref,
8233 BTRFS_TREE_BLOCK_REF_KEY);
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008234 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008235 }
8236
8237 btrfs_mark_buffer_dirty(leaf);
8238 btrfs_free_path(path);
8239
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008240 ret = remove_from_free_space_tree(trans, extent_key.objectid,
8241 num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008242 if (ret)
8243 return ret;
8244
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008245 ret = update_block_group(trans, fs_info, extent_key.objectid,
Jeff Mahoney6202df62016-06-22 18:54:22 -04008246 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008247 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008248 btrfs_err(fs_info, "update block group failed for %llu %llu",
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008249 extent_key.objectid, extent_key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008250 BUG();
8251 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04008252
Nikolay Borisov4e6bd4e2018-05-21 12:27:21 +03008253 trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008254 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008255 return ret;
8256}
8257
8258int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008259 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08008260 u64 offset, u64 ram_bytes,
8261 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008262{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008263 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008264 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04008265
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008266 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04008267
Josef Bacikfd708b82017-09-29 15:43:50 -04008268 btrfs_ref_tree_mod(root, ins->objectid, ins->offset, 0,
8269 root->root_key.objectid, owner, offset,
8270 BTRFS_ADD_DELAYED_EXTENT);
8271
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008272 ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008273 ins->offset, 0,
8274 root->root_key.objectid, owner,
Omar Sandoval7be07912017-06-06 16:45:30 -07008275 offset, ram_bytes,
8276 BTRFS_ADD_DELAYED_EXTENT, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008277 return ret;
8278}
Chris Masone02119d2008-09-05 16:13:11 -04008279
8280/*
8281 * this is used by the tree logging recovery code. It records that
8282 * an extent has been allocated and makes sure to clear the free
8283 * space cache bits as well
8284 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008285int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008286 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008287 u64 root_objectid, u64 owner, u64 offset,
8288 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04008289{
8290 int ret;
8291 struct btrfs_block_group_cache *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008292 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008293
8294 /*
8295 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04008296 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008297 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008298 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008299 ret = __exclude_logged_extent(fs_info, ins->objectid,
8300 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008301 if (ret)
8302 return ret;
8303 }
Chris Masone02119d2008-09-05 16:13:11 -04008304
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008305 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008306 if (!block_group)
8307 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04008308
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008309 space_info = block_group->space_info;
8310 spin_lock(&space_info->lock);
8311 spin_lock(&block_group->lock);
8312 space_info->bytes_reserved += ins->offset;
8313 block_group->reserved += ins->offset;
8314 spin_unlock(&block_group->lock);
8315 spin_unlock(&space_info->lock);
8316
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008317 ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008318 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04008319 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04008320 return ret;
8321}
8322
Eric Sandeen48a3b632013-04-25 20:41:01 +00008323static struct extent_buffer *
8324btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
David Sterbafe864572014-06-15 02:28:42 +02008325 u64 bytenr, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04008326{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008327 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04008328 struct extent_buffer *buf;
8329
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008330 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008331 if (IS_ERR(buf))
8332 return buf;
8333
Chris Mason65b51a02008-08-01 15:11:20 -04008334 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04008335 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04008336 btrfs_tree_lock(buf);
David Sterba7c302b42017-02-10 18:47:57 +01008337 clean_tree_block(fs_info, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05008338 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008339
8340 btrfs_set_lock_blocking(buf);
David Sterba4db8c522015-12-03 13:06:46 +01008341 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008342
Chris Masond0c803c2008-09-11 16:17:57 -04008343 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01008344 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008345 /*
8346 * we allow two log transactions at a time, use different
8347 * EXENT bit to differentiate dirty pages.
8348 */
Filipe Manana656f30d2014-09-26 12:25:56 +01008349 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008350 set_extent_dirty(&root->dirty_log_pages, buf->start,
8351 buf->start + buf->len - 1, GFP_NOFS);
8352 else
8353 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02008354 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04008355 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01008356 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04008357 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
8358 buf->start + buf->len - 1, GFP_NOFS);
8359 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04008360 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05008361 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04008362 return buf;
8363}
8364
Yan, Zhengf0486c62010-05-16 10:46:25 -04008365static struct btrfs_block_rsv *
8366use_block_rsv(struct btrfs_trans_handle *trans,
8367 struct btrfs_root *root, u32 blocksize)
8368{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008369 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008370 struct btrfs_block_rsv *block_rsv;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008371 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008372 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00008373 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008374
8375 block_rsv = get_block_rsv(trans, root);
8376
Miao Xieb586b322013-05-13 13:55:10 +00008377 if (unlikely(block_rsv->size == 0))
8378 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00008379again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04008380 ret = block_rsv_use_bytes(block_rsv, blocksize);
8381 if (!ret)
8382 return block_rsv;
8383
Miao Xieb586b322013-05-13 13:55:10 +00008384 if (block_rsv->failfast)
8385 return ERR_PTR(ret);
8386
Miao Xied88033d2013-05-13 13:55:12 +00008387 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
8388 global_updated = true;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008389 update_global_block_rsv(fs_info);
Miao Xied88033d2013-05-13 13:55:12 +00008390 goto again;
8391 }
8392
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008393 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xieb586b322013-05-13 13:55:10 +00008394 static DEFINE_RATELIMIT_STATE(_rs,
8395 DEFAULT_RATELIMIT_INTERVAL * 10,
8396 /*DEFAULT_RATELIMIT_BURST*/ 1);
8397 if (__ratelimit(&_rs))
8398 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05008399 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00008400 }
8401try_reserve:
8402 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
8403 BTRFS_RESERVE_NO_FLUSH);
8404 if (!ret)
8405 return block_rsv;
8406 /*
8407 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00008408 * the global reserve if its space type is the same as the global
8409 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00008410 */
Miao Xie5881cfc2013-05-13 13:55:11 +00008411 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
8412 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00008413 ret = block_rsv_use_bytes(global_rsv, blocksize);
8414 if (!ret)
8415 return global_rsv;
8416 }
8417 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008418}
8419
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008420static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
8421 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008422{
8423 block_rsv_add_bytes(block_rsv, blocksize, 0);
Qu Wenruoff6bc372017-12-21 13:42:04 +08008424 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0, NULL);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008425}
8426
Chris Masonfec577f2007-02-26 10:40:21 -05008427/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04008428 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08008429 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05008430 */
David Sterba4d75f8a2014-06-15 01:54:12 +02008431struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08008432 struct btrfs_root *root,
8433 u64 parent, u64 root_objectid,
8434 const struct btrfs_disk_key *key,
8435 int level, u64 hint,
8436 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05008437{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008438 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04008439 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008440 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04008441 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008442 struct btrfs_delayed_extent_op *extent_op;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008443 u64 flags = 0;
8444 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008445 u32 blocksize = fs_info->nodesize;
8446 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008447
David Sterba05653ef2016-07-15 15:23:37 +02008448#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008449 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008450 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
David Sterbafe864572014-06-15 02:28:42 +02008451 level);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008452 if (!IS_ERR(buf))
8453 root->alloc_bytenr += blocksize;
8454 return buf;
8455 }
David Sterba05653ef2016-07-15 15:23:37 +02008456#endif
David Sterbafccb84c2014-09-29 23:53:21 +02008457
Yan, Zhengf0486c62010-05-16 10:46:25 -04008458 block_rsv = use_block_rsv(trans, root, blocksize);
8459 if (IS_ERR(block_rsv))
8460 return ERR_CAST(block_rsv);
8461
Wang Xiaoguang18513092016-07-25 15:51:40 +08008462 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08008463 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008464 if (ret)
8465 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05008466
David Sterbafe864572014-06-15 02:28:42 +02008467 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
Omar Sandoval67b78592015-02-24 02:47:04 -08008468 if (IS_ERR(buf)) {
8469 ret = PTR_ERR(buf);
8470 goto out_free_reserved;
8471 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008472
8473 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
8474 if (parent == 0)
8475 parent = ins.objectid;
8476 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8477 } else
8478 BUG_ON(parent > 0);
8479
8480 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00008481 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08008482 if (!extent_op) {
8483 ret = -ENOMEM;
8484 goto out_free_buf;
8485 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008486 if (key)
8487 memcpy(&extent_op->key, key, sizeof(extent_op->key));
8488 else
8489 memset(&extent_op->key, 0, sizeof(extent_op->key));
8490 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01008491 extent_op->update_key = skinny_metadata ? false : true;
8492 extent_op->update_flags = true;
8493 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04008494 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008495
Josef Bacikfd708b82017-09-29 15:43:50 -04008496 btrfs_ref_tree_mod(root, ins.objectid, ins.offset, parent,
8497 root_objectid, level, 0,
8498 BTRFS_ADD_DELAYED_EXTENT);
Omar Sandoval7be07912017-06-06 16:45:30 -07008499 ret = btrfs_add_delayed_tree_ref(fs_info, trans, ins.objectid,
8500 ins.offset, parent,
8501 root_objectid, level,
Omar Sandoval67b78592015-02-24 02:47:04 -08008502 BTRFS_ADD_DELAYED_EXTENT,
Omar Sandoval7be07912017-06-06 16:45:30 -07008503 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08008504 if (ret)
8505 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008506 }
Chris Masonfec577f2007-02-26 10:40:21 -05008507 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008508
8509out_free_delayed:
8510 btrfs_free_delayed_extent_op(extent_op);
8511out_free_buf:
8512 free_extent_buffer(buf);
8513out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008514 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008515out_unuse:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008516 unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08008517 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05008518}
Chris Masona28ec192007-03-06 20:08:01 -05008519
Yan Zheng2c47e6052009-06-27 21:07:35 -04008520struct walk_control {
8521 u64 refs[BTRFS_MAX_LEVEL];
8522 u64 flags[BTRFS_MAX_LEVEL];
8523 struct btrfs_key update_progress;
8524 int stage;
8525 int level;
8526 int shared_level;
8527 int update_ref;
8528 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008529 int reada_slot;
8530 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008531 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008532};
8533
8534#define DROP_REFERENCE 1
8535#define UPDATE_BACKREF 2
8536
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008537static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8538 struct btrfs_root *root,
8539 struct walk_control *wc,
8540 struct btrfs_path *path)
8541{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008542 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008543 u64 bytenr;
8544 u64 generation;
8545 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008546 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008547 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008548 struct btrfs_key key;
8549 struct extent_buffer *eb;
8550 int ret;
8551 int slot;
8552 int nread = 0;
8553
8554 if (path->slots[wc->level] < wc->reada_slot) {
8555 wc->reada_count = wc->reada_count * 2 / 3;
8556 wc->reada_count = max(wc->reada_count, 2);
8557 } else {
8558 wc->reada_count = wc->reada_count * 3 / 2;
8559 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008560 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008561 }
8562
8563 eb = path->nodes[wc->level];
8564 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008565
8566 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
8567 if (nread >= wc->reada_count)
8568 break;
8569
8570 cond_resched();
8571 bytenr = btrfs_node_blockptr(eb, slot);
8572 generation = btrfs_node_ptr_generation(eb, slot);
8573
8574 if (slot == path->slots[wc->level])
8575 goto reada;
8576
8577 if (wc->stage == UPDATE_BACKREF &&
8578 generation <= root->root_key.offset)
8579 continue;
8580
Yan, Zheng94fcca92009-10-09 09:25:16 -04008581 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008582 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05008583 wc->level - 1, 1, &refs,
8584 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008585 /* We don't care about errors in readahead. */
8586 if (ret < 0)
8587 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008588 BUG_ON(refs == 0);
8589
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008590 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008591 if (refs == 1)
8592 goto reada;
8593
Yan, Zheng94fcca92009-10-09 09:25:16 -04008594 if (wc->level == 1 &&
8595 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8596 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008597 if (!wc->update_ref ||
8598 generation <= root->root_key.offset)
8599 continue;
8600 btrfs_node_key_to_cpu(eb, &key, slot);
8601 ret = btrfs_comp_cpu_keys(&key,
8602 &wc->update_progress);
8603 if (ret < 0)
8604 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008605 } else {
8606 if (wc->level == 1 &&
8607 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8608 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008609 }
8610reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008611 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008612 nread++;
8613 }
8614 wc->reada_slot = slot;
8615}
8616
Chris Mason9aca1d52007-03-13 11:09:37 -04008617/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008618 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008619 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04008620 * when wc->stage == UPDATE_BACKREF, this function updates
8621 * back refs for pointers in the block.
8622 *
8623 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04008624 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008625static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8626 struct btrfs_root *root,
8627 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008628 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04008629{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008630 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008631 int level = wc->level;
8632 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04008633 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8634 int ret;
8635
8636 if (wc->stage == UPDATE_BACKREF &&
8637 btrfs_header_owner(eb) != root->root_key.objectid)
8638 return 1;
8639
8640 /*
8641 * when reference count of tree block is 1, it won't increase
8642 * again. once full backref flag is set, we never clear it.
8643 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04008644 if (lookup_info &&
8645 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
8646 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008647 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008648 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008649 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008650 &wc->refs[level],
8651 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008652 BUG_ON(ret == -ENOMEM);
8653 if (ret)
8654 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008655 BUG_ON(wc->refs[level] == 0);
8656 }
8657
Yan Zheng2c47e6052009-06-27 21:07:35 -04008658 if (wc->stage == DROP_REFERENCE) {
8659 if (wc->refs[level] > 1)
8660 return 1;
8661
8662 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04008663 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008664 path->locks[level] = 0;
8665 }
8666 return 0;
8667 }
8668
8669 /* wc->stage == UPDATE_BACKREF */
8670 if (!(wc->flags[level] & flag)) {
8671 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07008672 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008673 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07008674 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008675 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008676 ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04008677 eb->len, flag,
8678 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008679 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008680 wc->flags[level] |= flag;
8681 }
8682
8683 /*
8684 * the block is shared by multiple trees, so it's not good to
8685 * keep the tree lock
8686 */
8687 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04008688 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008689 path->locks[level] = 0;
8690 }
8691 return 0;
8692}
8693
8694/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008695 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008696 *
8697 * when wc->stage == DROP_REFERENCE, this function checks
8698 * reference count of the block pointed to. if the block
8699 * is shared and we need update back refs for the subtree
8700 * rooted at the block, this function changes wc->stage to
8701 * UPDATE_BACKREF. if the block is shared and there is no
8702 * need to update back, this function drops the reference
8703 * to the block.
8704 *
8705 * NOTE: return value 1 means we should stop walking down.
8706 */
8707static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8708 struct btrfs_root *root,
8709 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008710 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008711{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008712 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008713 u64 bytenr;
8714 u64 generation;
8715 u64 parent;
8716 u32 blocksize;
8717 struct btrfs_key key;
Qu Wenruo581c1762018-03-29 09:08:11 +08008718 struct btrfs_key first_key;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008719 struct extent_buffer *next;
8720 int level = wc->level;
8721 int reada = 0;
8722 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07008723 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008724
8725 generation = btrfs_node_ptr_generation(path->nodes[level],
8726 path->slots[level]);
8727 /*
8728 * if the lower level block was created before the snapshot
8729 * was created, we know there is no need to update back refs
8730 * for the subtree
8731 */
8732 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04008733 generation <= root->root_key.offset) {
8734 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008735 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008736 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008737
8738 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08008739 btrfs_node_key_to_cpu(path->nodes[level], &first_key,
8740 path->slots[level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008741 blocksize = fs_info->nodesize;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008742
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008743 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008744 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008745 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008746 if (IS_ERR(next))
8747 return PTR_ERR(next);
8748
Josef Bacikb2aaaa32013-07-05 17:05:38 -04008749 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
8750 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008751 reada = 1;
8752 }
8753 btrfs_tree_lock(next);
8754 btrfs_set_lock_blocking(next);
8755
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008756 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008757 &wc->refs[level - 1],
8758 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02008759 if (ret < 0)
8760 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008761
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008762 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008763 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02008764 ret = -EIO;
8765 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008766 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008767 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008768
Yan, Zheng94fcca92009-10-09 09:25:16 -04008769 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008770 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07008771 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008772 if (level == 1 &&
8773 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8774 goto skip;
8775
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008776 if (!wc->update_ref ||
8777 generation <= root->root_key.offset)
8778 goto skip;
8779
8780 btrfs_node_key_to_cpu(path->nodes[level], &key,
8781 path->slots[level]);
8782 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
8783 if (ret < 0)
8784 goto skip;
8785
8786 wc->stage = UPDATE_BACKREF;
8787 wc->shared_level = level - 1;
8788 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008789 } else {
8790 if (level == 1 &&
8791 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8792 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008793 }
8794
Chris Masonb9fab912012-05-06 07:23:47 -04008795 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008796 btrfs_tree_unlock(next);
8797 free_extent_buffer(next);
8798 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008799 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008800 }
8801
8802 if (!next) {
8803 if (reada && level == 1)
8804 reada_walk_down(trans, root, wc, path);
Qu Wenruo581c1762018-03-29 09:08:11 +08008805 next = read_tree_block(fs_info, bytenr, generation, level - 1,
8806 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08008807 if (IS_ERR(next)) {
8808 return PTR_ERR(next);
8809 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04008810 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008811 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04008812 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008813 btrfs_tree_lock(next);
8814 btrfs_set_lock_blocking(next);
8815 }
8816
8817 level--;
Josef Bacik48672682016-09-23 13:23:28 +02008818 ASSERT(level == btrfs_header_level(next));
8819 if (level != btrfs_header_level(next)) {
8820 btrfs_err(root->fs_info, "mismatched level");
8821 ret = -EIO;
8822 goto out_unlock;
8823 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008824 path->nodes[level] = next;
8825 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008826 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008827 wc->level = level;
8828 if (wc->level == 1)
8829 wc->reada_slot = 0;
8830 return 0;
8831skip:
8832 wc->refs[level - 1] = 0;
8833 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008834 if (wc->stage == DROP_REFERENCE) {
8835 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
8836 parent = path->nodes[level]->start;
8837 } else {
Josef Bacik48672682016-09-23 13:23:28 +02008838 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04008839 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02008840 if (root->root_key.objectid !=
8841 btrfs_header_owner(path->nodes[level])) {
8842 btrfs_err(root->fs_info,
8843 "mismatched block owner");
8844 ret = -EIO;
8845 goto out_unlock;
8846 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008847 parent = 0;
8848 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008849
Mark Fasheh11526512014-07-17 12:39:01 -07008850 if (need_account) {
Qu Wenruo33d1f052016-10-18 09:31:28 +08008851 ret = btrfs_qgroup_trace_subtree(trans, root, next,
8852 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07008853 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008854 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008855 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
8856 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008857 }
8858 }
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008859 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008860 parent, root->root_key.objectid,
8861 level - 1, 0);
Josef Bacik48672682016-09-23 13:23:28 +02008862 if (ret)
8863 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008864 }
Josef Bacik48672682016-09-23 13:23:28 +02008865
8866 *lookup_info = 1;
8867 ret = 1;
8868
8869out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008870 btrfs_tree_unlock(next);
8871 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02008872
8873 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008874}
8875
8876/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008877 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008878 *
8879 * when wc->stage == DROP_REFERENCE, this function drops
8880 * reference count on the block.
8881 *
8882 * when wc->stage == UPDATE_BACKREF, this function changes
8883 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8884 * to UPDATE_BACKREF previously while processing the block.
8885 *
8886 * NOTE: return value 1 means we should stop walking up.
8887 */
8888static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8889 struct btrfs_root *root,
8890 struct btrfs_path *path,
8891 struct walk_control *wc)
8892{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008893 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008894 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008895 int level = wc->level;
8896 struct extent_buffer *eb = path->nodes[level];
8897 u64 parent = 0;
8898
8899 if (wc->stage == UPDATE_BACKREF) {
8900 BUG_ON(wc->shared_level < level);
8901 if (level < wc->shared_level)
8902 goto out;
8903
Yan Zheng2c47e6052009-06-27 21:07:35 -04008904 ret = find_next_key(path, level + 1, &wc->update_progress);
8905 if (ret > 0)
8906 wc->update_ref = 0;
8907
8908 wc->stage = DROP_REFERENCE;
8909 wc->shared_level = -1;
8910 path->slots[level] = 0;
8911
8912 /*
8913 * check reference count again if the block isn't locked.
8914 * we should start walking down the tree again if reference
8915 * count is one.
8916 */
8917 if (!path->locks[level]) {
8918 BUG_ON(level == 0);
8919 btrfs_tree_lock(eb);
8920 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008921 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008922
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008923 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008924 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008925 &wc->refs[level],
8926 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008927 if (ret < 0) {
8928 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008929 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008930 return ret;
8931 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008932 BUG_ON(wc->refs[level] == 0);
8933 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04008934 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008935 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008936 return 1;
8937 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008938 }
8939 }
8940
8941 /* wc->stage == DROP_REFERENCE */
8942 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
8943
8944 if (wc->refs[level] == 1) {
8945 if (level == 0) {
8946 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07008947 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008948 else
Josef Bacike339a6b2014-07-02 10:54:25 -07008949 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008950 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008951 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb);
Mark Fasheh11526512014-07-17 12:39:01 -07008952 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008953 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008954 "error %d accounting leaf items. Quota is out of sync, rescan required.",
8955 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008956 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008957 }
8958 /* make block locked assertion in clean_tree_block happy */
8959 if (!path->locks[level] &&
8960 btrfs_header_generation(eb) == trans->transid) {
8961 btrfs_tree_lock(eb);
8962 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008963 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008964 }
David Sterba7c302b42017-02-10 18:47:57 +01008965 clean_tree_block(fs_info, eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008966 }
8967
8968 if (eb == root->node) {
8969 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8970 parent = eb->start;
8971 else
8972 BUG_ON(root->root_key.objectid !=
8973 btrfs_header_owner(eb));
8974 } else {
8975 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8976 parent = path->nodes[level + 1]->start;
8977 else
8978 BUG_ON(root->root_key.objectid !=
8979 btrfs_header_owner(path->nodes[level + 1]));
8980 }
8981
Jan Schmidt5581a512012-05-16 17:04:52 +02008982 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008983out:
8984 wc->refs[level] = 0;
8985 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008986 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008987}
8988
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008989static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
8990 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008991 struct btrfs_path *path,
8992 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008993{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008994 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008995 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008996 int ret;
8997
Yan Zheng2c47e6052009-06-27 21:07:35 -04008998 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04008999 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009000 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04009001 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009002
Yan Zheng2c47e6052009-06-27 21:07:35 -04009003 if (level == 0)
9004 break;
9005
Yan, Zheng7a7965f2010-02-01 02:41:17 +00009006 if (path->slots[level] >=
9007 btrfs_header_nritems(path->nodes[level]))
9008 break;
9009
Yan, Zheng94fcca92009-10-09 09:25:16 -04009010 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009011 if (ret > 0) {
9012 path->slots[level]++;
9013 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00009014 } else if (ret < 0)
9015 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009016 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009017 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009018 return 0;
9019}
9020
Chris Masond3977122009-01-05 21:25:51 -05009021static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05009022 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04009023 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009024 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05009025{
Yan Zheng2c47e6052009-06-27 21:07:35 -04009026 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05009027 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04009028
Yan Zheng2c47e6052009-06-27 21:07:35 -04009029 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
9030 while (level < max_level && path->nodes[level]) {
9031 wc->level = level;
9032 if (path->slots[level] + 1 <
9033 btrfs_header_nritems(path->nodes[level])) {
9034 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05009035 return 0;
9036 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009037 ret = walk_up_proc(trans, root, path, wc);
9038 if (ret > 0)
9039 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05009040
Yan Zheng2c47e6052009-06-27 21:07:35 -04009041 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04009042 btrfs_tree_unlock_rw(path->nodes[level],
9043 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009044 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009045 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009046 free_extent_buffer(path->nodes[level]);
9047 path->nodes[level] = NULL;
9048 level++;
Chris Mason20524f02007-03-10 06:35:47 -05009049 }
9050 }
9051 return 1;
9052}
9053
Chris Mason9aca1d52007-03-13 11:09:37 -04009054/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04009055 * drop a subvolume tree.
9056 *
9057 * this function traverses the tree freeing any blocks that only
9058 * referenced by the tree.
9059 *
9060 * when a shared tree block is found. this function decreases its
9061 * reference count by one. if update_ref is true, this function
9062 * also make sure backrefs for the shared block and all lower level
9063 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00009064 *
9065 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04009066 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04009067int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009068 struct btrfs_block_rsv *block_rsv, int update_ref,
9069 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05009070{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009071 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04009072 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009073 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009074 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04009075 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009076 struct walk_control *wc;
9077 struct btrfs_key key;
9078 int err = 0;
9079 int ret;
9080 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04009081 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05009082
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009083 btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07009084
Chris Mason5caf2a02007-04-02 11:20:42 -04009085 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009086 if (!path) {
9087 err = -ENOMEM;
9088 goto out;
9089 }
Chris Mason20524f02007-03-10 06:35:47 -05009090
Yan Zheng2c47e6052009-06-27 21:07:35 -04009091 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07009092 if (!wc) {
9093 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009094 err = -ENOMEM;
9095 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07009096 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009097
Yan, Zhenga22285a2010-05-16 10:48:46 -04009098 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009099 if (IS_ERR(trans)) {
9100 err = PTR_ERR(trans);
9101 goto out_free;
9102 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00009103
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009104 if (block_rsv)
9105 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009106
Chris Mason9f3a7422007-08-07 15:52:19 -04009107 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009108 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009109 path->nodes[level] = btrfs_lock_root_node(root);
9110 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04009111 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009112 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009113 memset(&wc->update_progress, 0,
9114 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04009115 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04009116 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009117 memcpy(&wc->update_progress, &key,
9118 sizeof(wc->update_progress));
9119
Chris Mason6702ed42007-08-07 16:15:09 -04009120 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009121 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04009122 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009123 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
9124 path->lowest_level = 0;
9125 if (ret < 0) {
9126 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009127 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04009128 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009129 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009130
Chris Mason7d9eb122008-07-08 14:19:17 -04009131 /*
9132 * unlock our path, this is safe because only this
9133 * function is allowed to delete this snapshot
9134 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009135 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04009136
Yan Zheng2c47e6052009-06-27 21:07:35 -04009137 level = btrfs_header_level(root->node);
9138 while (1) {
9139 btrfs_tree_lock(path->nodes[level]);
9140 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009141 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009142
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009143 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009144 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05009145 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04009146 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009147 if (ret < 0) {
9148 err = ret;
9149 goto out_end_trans;
9150 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009151 BUG_ON(wc->refs[level] == 0);
9152
9153 if (level == root_item->drop_level)
9154 break;
9155
9156 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009157 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009158 WARN_ON(wc->refs[level] != 1);
9159 level--;
9160 }
9161 }
9162
9163 wc->level = level;
9164 wc->shared_level = -1;
9165 wc->stage = DROP_REFERENCE;
9166 wc->update_ref = update_ref;
9167 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009168 wc->for_reloc = for_reloc;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009169 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009170
9171 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00009172
Yan Zheng2c47e6052009-06-27 21:07:35 -04009173 ret = walk_down_tree(trans, root, path, wc);
9174 if (ret < 0) {
9175 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04009176 break;
9177 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009178
9179 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
9180 if (ret < 0) {
9181 err = ret;
9182 break;
9183 }
9184
9185 if (ret > 0) {
9186 BUG_ON(wc->stage != DROP_REFERENCE);
9187 break;
9188 }
9189
9190 if (wc->stage == DROP_REFERENCE) {
9191 level = wc->level;
9192 btrfs_node_key(path->nodes[level],
9193 &root_item->drop_progress,
9194 path->slots[level]);
9195 root_item->drop_level = level;
9196 }
9197
9198 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009199 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009200 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009201 ret = btrfs_update_root(trans, tree_root,
9202 &root->root_key,
9203 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009204 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009205 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009206 err = ret;
9207 goto out_end_trans;
9208 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009209
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009210 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009211 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009212 btrfs_debug(fs_info,
9213 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04009214 err = -EAGAIN;
9215 goto out_free;
9216 }
9217
Yan, Zhenga22285a2010-05-16 10:48:46 -04009218 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009219 if (IS_ERR(trans)) {
9220 err = PTR_ERR(trans);
9221 goto out_free;
9222 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009223 if (block_rsv)
9224 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04009225 }
Chris Mason20524f02007-03-10 06:35:47 -05009226 }
David Sterbab3b4aa72011-04-21 01:20:15 +02009227 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009228 if (err)
9229 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009230
Jeff Mahoney1cd54472017-08-17 10:25:11 -04009231 ret = btrfs_del_root(trans, fs_info, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009232 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009233 btrfs_abort_transaction(trans, ret);
Jeff Mahoneye19182c2017-12-04 13:11:45 -05009234 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009235 goto out_end_trans;
9236 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009237
Yan, Zheng76dda932009-09-21 16:00:26 -04009238 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00009239 ret = btrfs_find_root(tree_root, &root->root_key, path,
9240 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009241 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009242 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009243 err = ret;
9244 goto out_end_trans;
9245 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05009246 /* if we fail to delete the orphan item this time
9247 * around, it'll get picked up the next time.
9248 *
9249 * The most common failure here is just -ENOENT.
9250 */
9251 btrfs_del_orphan_item(trans, tree_root,
9252 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04009253 }
9254 }
9255
Miao Xie27cdeb72014-04-02 19:51:05 +08009256 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04009257 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009258 } else {
9259 free_extent_buffer(root->node);
9260 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00009261 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009262 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04009263 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009264out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009265 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009266out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04009267 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04009268 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009269out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04009270 /*
9271 * So if we need to stop dropping the snapshot for whatever reason we
9272 * need to make sure to add it back to the dead root list so that we
9273 * keep trying to do the work later. This also cleans up roots if we
9274 * don't have it in the radix (like when we recover after a power fail
9275 * or unmount) so we don't leak memory.
9276 */
Thomas Meyer897ca812017-10-07 16:02:21 +02009277 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04009278 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08009279 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009280 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04009281 return err;
Chris Mason20524f02007-03-10 06:35:47 -05009282}
Chris Mason9078a3e2007-04-26 16:46:15 -04009283
Yan Zheng2c47e6052009-06-27 21:07:35 -04009284/*
9285 * drop subtree rooted at tree block 'node'.
9286 *
9287 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009288 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04009289 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04009290int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
9291 struct btrfs_root *root,
9292 struct extent_buffer *node,
9293 struct extent_buffer *parent)
9294{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009295 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009296 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009297 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009298 int level;
9299 int parent_level;
9300 int ret = 0;
9301 int wret;
9302
Yan Zheng2c47e6052009-06-27 21:07:35 -04009303 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
9304
Yan Zhengf82d02d2008-10-29 14:49:05 -04009305 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009306 if (!path)
9307 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009308
Yan Zheng2c47e6052009-06-27 21:07:35 -04009309 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009310 if (!wc) {
9311 btrfs_free_path(path);
9312 return -ENOMEM;
9313 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009314
Chris Masonb9447ef82009-03-09 11:45:38 -04009315 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009316 parent_level = btrfs_header_level(parent);
9317 extent_buffer_get(parent);
9318 path->nodes[parent_level] = parent;
9319 path->slots[parent_level] = btrfs_header_nritems(parent);
9320
Chris Masonb9447ef82009-03-09 11:45:38 -04009321 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009322 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009323 path->nodes[level] = node;
9324 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009325 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009326
9327 wc->refs[parent_level] = 1;
9328 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
9329 wc->level = level;
9330 wc->shared_level = -1;
9331 wc->stage = DROP_REFERENCE;
9332 wc->update_ref = 0;
9333 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009334 wc->for_reloc = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009335 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009336
9337 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009338 wret = walk_down_tree(trans, root, path, wc);
9339 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04009340 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009341 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009342 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009343
Yan Zheng2c47e6052009-06-27 21:07:35 -04009344 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009345 if (wret < 0)
9346 ret = wret;
9347 if (wret != 0)
9348 break;
9349 }
9350
Yan Zheng2c47e6052009-06-27 21:07:35 -04009351 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009352 btrfs_free_path(path);
9353 return ret;
9354}
9355
Jeff Mahoney6202df62016-06-22 18:54:22 -04009356static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04009357{
9358 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009359 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04009360
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009361 /*
9362 * if restripe for this chunk_type is on pick target profile and
9363 * return, otherwise do the usual balance
9364 */
Jeff Mahoney6202df62016-06-22 18:54:22 -04009365 stripped = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009366 if (stripped)
9367 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02009368
Jeff Mahoney6202df62016-06-22 18:54:22 -04009369 num_devices = fs_info->fs_devices->rw_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05009370
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009371 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05009372 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009373 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
9374
Chris Masonec44a352008-04-28 15:29:52 -04009375 if (num_devices == 1) {
9376 stripped |= BTRFS_BLOCK_GROUP_DUP;
9377 stripped = flags & ~stripped;
9378
9379 /* turn raid0 into single device chunks */
9380 if (flags & BTRFS_BLOCK_GROUP_RAID0)
9381 return stripped;
9382
9383 /* turn mirroring into duplication */
9384 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
9385 BTRFS_BLOCK_GROUP_RAID10))
9386 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04009387 } else {
9388 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04009389 if (flags & stripped)
9390 return flags;
9391
9392 stripped |= BTRFS_BLOCK_GROUP_DUP;
9393 stripped = flags & ~stripped;
9394
9395 /* switch duplicated blocks with raid1 */
9396 if (flags & BTRFS_BLOCK_GROUP_DUP)
9397 return stripped | BTRFS_BLOCK_GROUP_RAID1;
9398
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009399 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04009400 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009401
Chris Masonec44a352008-04-28 15:29:52 -04009402 return flags;
9403}
9404
Zhaolei868f4012015-08-05 16:43:27 +08009405static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04009406{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009407 struct btrfs_space_info *sinfo = cache->space_info;
9408 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00009409 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009410 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04009411
Miao Xie199c36e2011-07-15 10:34:36 +00009412 /*
9413 * We need some metadata space and system metadata space for
9414 * allocating chunks in some corner cases until we force to set
9415 * it to be readonly.
9416 */
9417 if ((sinfo->flags &
9418 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
9419 !force)
Byongho Leeee221842015-12-15 01:42:10 +09009420 min_allocable_bytes = SZ_1M;
Miao Xie199c36e2011-07-15 10:34:36 +00009421 else
9422 min_allocable_bytes = 0;
9423
Yan, Zhengf0486c62010-05-16 10:46:25 -04009424 spin_lock(&sinfo->lock);
9425 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00009426
9427 if (cache->ro) {
Zhaolei868f4012015-08-05 16:43:27 +08009428 cache->ro++;
WuBo61cfea92011-07-26 03:30:11 +00009429 ret = 0;
9430 goto out;
9431 }
9432
Yan, Zhengf0486c62010-05-16 10:46:25 -04009433 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
9434 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04009435
Liu Bo41361352017-02-13 15:42:21 -08009436 if (btrfs_space_info_used(sinfo, true) + num_bytes +
Josef Bacik37be25b2011-08-05 10:25:38 -04009437 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04009438 sinfo->bytes_readonly += num_bytes;
Zhaolei868f4012015-08-05 16:43:27 +08009439 cache->ro++;
Josef Bacik633c0aa2014-10-31 09:49:34 -04009440 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009441 ret = 0;
9442 }
WuBo61cfea92011-07-26 03:30:11 +00009443out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04009444 spin_unlock(&cache->lock);
9445 spin_unlock(&sinfo->lock);
9446 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04009447}
9448
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009449int btrfs_inc_block_group_ro(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04009450 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009451
9452{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009453 struct btrfs_trans_handle *trans;
9454 u64 alloc_flags;
9455 int ret;
9456
Chris Mason1bbc6212015-04-06 12:46:08 -07009457again:
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009458 trans = btrfs_join_transaction(fs_info->extent_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009459 if (IS_ERR(trans))
9460 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009461
Chris Mason1bbc6212015-04-06 12:46:08 -07009462 /*
9463 * we're not allowed to set block groups readonly after the dirty
9464 * block groups cache has started writing. If it already started,
9465 * back off and let this transaction commit
9466 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009467 mutex_lock(&fs_info->ro_block_group_mutex);
Josef Bacik3204d332015-09-24 10:46:10 -04009468 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
Chris Mason1bbc6212015-04-06 12:46:08 -07009469 u64 transid = trans->transid;
9470
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009471 mutex_unlock(&fs_info->ro_block_group_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009472 btrfs_end_transaction(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07009473
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009474 ret = btrfs_wait_for_commit(fs_info, transid);
Chris Mason1bbc6212015-04-06 12:46:08 -07009475 if (ret)
9476 return ret;
9477 goto again;
9478 }
9479
Chris Mason153c35b2015-05-19 18:54:41 -07009480 /*
9481 * if we are changing raid levels, try to allocate a corresponding
9482 * block group with the new raid level.
9483 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009484 alloc_flags = update_block_group_flags(fs_info, cache->flags);
Chris Mason153c35b2015-05-19 18:54:41 -07009485 if (alloc_flags != cache->flags) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009486 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason153c35b2015-05-19 18:54:41 -07009487 CHUNK_ALLOC_FORCE);
9488 /*
9489 * ENOSPC is allowed here, we may have enough space
9490 * already allocated at the new raid level to
9491 * carry on
9492 */
9493 if (ret == -ENOSPC)
9494 ret = 0;
9495 if (ret < 0)
9496 goto out;
9497 }
Chris Mason1bbc6212015-04-06 12:46:08 -07009498
Zhaolei868f4012015-08-05 16:43:27 +08009499 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009500 if (!ret)
9501 goto out;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009502 alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags);
9503 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04009504 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009505 if (ret < 0)
9506 goto out;
Zhaolei868f4012015-08-05 16:43:27 +08009507 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009508out:
Shaohua Li2f081082015-01-09 10:40:15 -08009509 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009510 alloc_flags = update_block_group_flags(fs_info, cache->flags);
David Sterba34441362016-10-04 19:34:27 +02009511 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009512 check_system_chunk(trans, fs_info, alloc_flags);
David Sterba34441362016-10-04 19:34:27 +02009513 mutex_unlock(&fs_info->chunk_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009514 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009515 mutex_unlock(&fs_info->ro_block_group_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009516
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009517 btrfs_end_transaction(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009518 return ret;
9519}
9520
Chris Masonc87f08c2011-02-16 13:57:04 -05009521int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009522 struct btrfs_fs_info *fs_info, u64 type)
Chris Masonc87f08c2011-02-16 13:57:04 -05009523{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009524 u64 alloc_flags = get_alloc_profile(fs_info, type);
9525
9526 return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05009527}
9528
Miao Xie6d07bce2011-01-05 10:07:31 +00009529/*
9530 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04009531 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00009532 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009533u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00009534{
9535 struct btrfs_block_group_cache *block_group;
9536 u64 free_bytes = 0;
9537 int factor;
9538
Nicholas D Steeves01327612016-05-19 21:18:45 -04009539 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009540 if (list_empty(&sinfo->ro_bgs))
9541 return 0;
9542
9543 spin_lock(&sinfo->lock);
9544 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00009545 spin_lock(&block_group->lock);
9546
9547 if (!block_group->ro) {
9548 spin_unlock(&block_group->lock);
9549 continue;
9550 }
9551
9552 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
9553 BTRFS_BLOCK_GROUP_RAID10 |
9554 BTRFS_BLOCK_GROUP_DUP))
9555 factor = 2;
9556 else
9557 factor = 1;
9558
9559 free_bytes += (block_group->key.offset -
9560 btrfs_block_group_used(&block_group->item)) *
9561 factor;
9562
9563 spin_unlock(&block_group->lock);
9564 }
Miao Xie6d07bce2011-01-05 10:07:31 +00009565 spin_unlock(&sinfo->lock);
9566
9567 return free_bytes;
9568}
9569
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009570void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache)
Yan, Zhengf0486c62010-05-16 10:46:25 -04009571{
9572 struct btrfs_space_info *sinfo = cache->space_info;
9573 u64 num_bytes;
9574
9575 BUG_ON(!cache->ro);
9576
9577 spin_lock(&sinfo->lock);
9578 spin_lock(&cache->lock);
Zhaolei868f4012015-08-05 16:43:27 +08009579 if (!--cache->ro) {
9580 num_bytes = cache->key.offset - cache->reserved -
9581 cache->pinned - cache->bytes_super -
9582 btrfs_block_group_used(&cache->item);
9583 sinfo->bytes_readonly -= num_bytes;
9584 list_del_init(&cache->ro_list);
9585 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009586 spin_unlock(&cache->lock);
9587 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009588}
9589
Josef Bacikba1bf482009-09-11 16:11:19 -04009590/*
9591 * checks to see if its even possible to relocate this block group.
9592 *
9593 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9594 * ok to go ahead and try.
9595 */
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009596int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04009597{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009598 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04009599 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04009600 struct btrfs_space_info *space_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009601 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacikba1bf482009-09-11 16:11:19 -04009602 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04009603 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00009604 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04009605 u64 dev_min = 1;
9606 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009607 u64 target;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009608 int debug;
liubocdcb7252011-08-03 10:15:25 +00009609 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04009610 int full = 0;
9611 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05009612
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009613 debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG);
Qu Wenruo0305bc22016-03-23 11:38:17 +08009614
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009615 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04009616
Josef Bacikba1bf482009-09-11 16:11:19 -04009617 /* odd, couldn't find the block group, leave it alone */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009618 if (!block_group) {
9619 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009620 btrfs_warn(fs_info,
Qu Wenruo0305bc22016-03-23 11:38:17 +08009621 "can't find block group for bytenr %llu",
9622 bytenr);
Josef Bacikba1bf482009-09-11 16:11:19 -04009623 return -1;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009624 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009625
liubocdcb7252011-08-03 10:15:25 +00009626 min_free = btrfs_block_group_used(&block_group->item);
9627
Josef Bacikba1bf482009-09-11 16:11:19 -04009628 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00009629 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04009630 goto out;
Chris Mason323da792008-05-09 11:46:48 -04009631
Josef Bacikba1bf482009-09-11 16:11:19 -04009632 space_info = block_group->space_info;
9633 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04009634
Josef Bacikba1bf482009-09-11 16:11:19 -04009635 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04009636
Josef Bacikba1bf482009-09-11 16:11:19 -04009637 /*
9638 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04009639 * relocate it unless we're able to allocate a new chunk below.
9640 *
9641 * Otherwise, we need to make sure we have room in the space to handle
9642 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04009643 */
Chris Mason7ce618d2009-09-22 14:48:44 -04009644 if ((space_info->total_bytes != block_group->key.offset) &&
Liu Bo41361352017-02-13 15:42:21 -08009645 (btrfs_space_info_used(space_info, false) + min_free <
9646 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04009647 spin_unlock(&space_info->lock);
9648 goto out;
9649 }
9650 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009651
Josef Bacikba1bf482009-09-11 16:11:19 -04009652 /*
9653 * ok we don't have enough space, but maybe we have free space on our
9654 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009655 * alloc devices and guess if we have enough space. if this block
9656 * group is going to be restriped, run checks against the target
9657 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04009658 */
9659 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05009660
liubocdcb7252011-08-03 10:15:25 +00009661 /*
9662 * index:
9663 * 0: raid10
9664 * 1: raid1
9665 * 2: dup
9666 * 3: raid0
9667 * 4: single
9668 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009669 target = get_restripe_target(fs_info, block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009670 if (target) {
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009671 index = btrfs_bg_flags_to_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009672 } else {
9673 /*
9674 * this is just a balance, so if we were marked as full
9675 * we know there is no space for a new chunk
9676 */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009677 if (full) {
9678 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009679 btrfs_warn(fs_info,
9680 "no space to alloc new chunk for block group %llu",
9681 block_group->key.objectid);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009682 goto out;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009683 }
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009684
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009685 index = btrfs_bg_flags_to_raid_index(block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009686 }
9687
Miao Xiee6ec7162013-01-17 05:38:51 +00009688 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00009689 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04009690 /* Divide by 2 */
9691 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009692 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00009693 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00009694 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04009695 /* Multiply by 2 */
9696 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009697 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00009698 dev_min = fs_devices->rw_devices;
David Sterba47c57132015-02-20 18:43:47 +01009699 min_free = div64_u64(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00009700 }
9701
Josef Bacik6df9a952013-06-27 13:22:46 -04009702 /* We need to do this so that we can look at pending chunks */
9703 trans = btrfs_join_transaction(root);
9704 if (IS_ERR(trans)) {
9705 ret = PTR_ERR(trans);
9706 goto out;
9707 }
9708
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009709 mutex_lock(&fs_info->chunk_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04009710 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00009711 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04009712
Josef Bacikba1bf482009-09-11 16:11:19 -04009713 /*
9714 * check to make sure we can actually find a chunk with enough
9715 * space to fit our block group in.
9716 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01009717 if (device->total_bytes > device->bytes_used + min_free &&
Anand Jain401e29c2017-12-04 12:54:55 +08009718 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Josef Bacik6df9a952013-06-27 13:22:46 -04009719 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00009720 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04009721 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00009722 dev_nr++;
9723
9724 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05009725 break;
liubocdcb7252011-08-03 10:15:25 +00009726
Josef Bacikba1bf482009-09-11 16:11:19 -04009727 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05009728 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009729 }
Qu Wenruo0305bc22016-03-23 11:38:17 +08009730 if (debug && ret == -1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009731 btrfs_warn(fs_info,
9732 "no space to allocate a new chunk for block group %llu",
9733 block_group->key.objectid);
9734 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009735 btrfs_end_transaction(trans);
Chris Masonedbd8d42007-12-21 16:27:24 -05009736out:
Josef Bacikba1bf482009-09-11 16:11:19 -04009737 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05009738 return ret;
9739}
9740
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009741static int find_first_block_group(struct btrfs_fs_info *fs_info,
9742 struct btrfs_path *path,
9743 struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04009744{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009745 struct btrfs_root *root = fs_info->extent_root;
Chris Mason925baed2008-06-25 16:01:30 -04009746 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009747 struct btrfs_key found_key;
9748 struct extent_buffer *leaf;
9749 int slot;
9750
9751 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
9752 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009753 goto out;
9754
Chris Masond3977122009-01-05 21:25:51 -05009755 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009756 slot = path->slots[0];
9757 leaf = path->nodes[0];
9758 if (slot >= btrfs_header_nritems(leaf)) {
9759 ret = btrfs_next_leaf(root, path);
9760 if (ret == 0)
9761 continue;
9762 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009763 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04009764 break;
9765 }
9766 btrfs_item_key_to_cpu(leaf, &found_key, slot);
9767
9768 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04009769 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
Liu Bo6fb37b72016-06-22 18:31:27 -07009770 struct extent_map_tree *em_tree;
9771 struct extent_map *em;
9772
9773 em_tree = &root->fs_info->mapping_tree.map_tree;
9774 read_lock(&em_tree->lock);
9775 em = lookup_extent_mapping(em_tree, found_key.objectid,
9776 found_key.offset);
9777 read_unlock(&em_tree->lock);
9778 if (!em) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009779 btrfs_err(fs_info,
Liu Bo6fb37b72016-06-22 18:31:27 -07009780 "logical %llu len %llu found bg but no related chunk",
9781 found_key.objectid, found_key.offset);
9782 ret = -ENOENT;
9783 } else {
9784 ret = 0;
9785 }
Josef Bacik187ee582016-08-18 15:30:06 -04009786 free_extent_map(em);
Chris Mason925baed2008-06-25 16:01:30 -04009787 goto out;
9788 }
Chris Mason0b86a832008-03-24 15:01:56 -04009789 path->slots[0]++;
9790 }
Chris Mason925baed2008-06-25 16:01:30 -04009791out:
Chris Mason0b86a832008-03-24 15:01:56 -04009792 return ret;
9793}
9794
Josef Bacik0af3d002010-06-21 14:48:16 -04009795void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9796{
9797 struct btrfs_block_group_cache *block_group;
9798 u64 last = 0;
9799
9800 while (1) {
9801 struct inode *inode;
9802
9803 block_group = btrfs_lookup_first_block_group(info, last);
9804 while (block_group) {
9805 spin_lock(&block_group->lock);
9806 if (block_group->iref)
9807 break;
9808 spin_unlock(&block_group->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009809 block_group = next_block_group(info, block_group);
Josef Bacik0af3d002010-06-21 14:48:16 -04009810 }
9811 if (!block_group) {
9812 if (last == 0)
9813 break;
9814 last = 0;
9815 continue;
9816 }
9817
9818 inode = block_group->inode;
9819 block_group->iref = 0;
9820 block_group->inode = NULL;
9821 spin_unlock(&block_group->lock);
Liu Bof3bca802016-07-20 17:33:44 -07009822 ASSERT(block_group->io_ctl.inode == NULL);
Josef Bacik0af3d002010-06-21 14:48:16 -04009823 iput(inode);
9824 last = block_group->key.objectid + block_group->key.offset;
9825 btrfs_put_block_group(block_group);
9826 }
9827}
9828
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009829/*
9830 * Must be called only after stopping all workers, since we could have block
9831 * group caching kthreads running, and therefore they could race with us if we
9832 * freed the block groups before stopping them.
9833 */
Zheng Yan1a40e232008-09-26 10:09:34 -04009834int btrfs_free_block_groups(struct btrfs_fs_info *info)
9835{
9836 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04009837 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04009838 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04009839 struct rb_node *n;
9840
Josef Bacik9e351cc2014-03-13 15:42:13 -04009841 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009842 while (!list_empty(&info->caching_block_groups)) {
9843 caching_ctl = list_entry(info->caching_block_groups.next,
9844 struct btrfs_caching_control, list);
9845 list_del(&caching_ctl->list);
9846 put_caching_control(caching_ctl);
9847 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04009848 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009849
Josef Bacik47ab2a62014-09-18 11:20:02 -04009850 spin_lock(&info->unused_bgs_lock);
9851 while (!list_empty(&info->unused_bgs)) {
9852 block_group = list_first_entry(&info->unused_bgs,
9853 struct btrfs_block_group_cache,
9854 bg_list);
9855 list_del_init(&block_group->bg_list);
9856 btrfs_put_block_group(block_group);
9857 }
9858 spin_unlock(&info->unused_bgs_lock);
9859
Zheng Yan1a40e232008-09-26 10:09:34 -04009860 spin_lock(&info->block_group_cache_lock);
9861 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
9862 block_group = rb_entry(n, struct btrfs_block_group_cache,
9863 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04009864 rb_erase(&block_group->cache_node,
9865 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009866 RB_CLEAR_NODE(&block_group->cache_node);
Yan Zhengd899e052008-10-30 14:25:28 -04009867 spin_unlock(&info->block_group_cache_lock);
9868
Josef Bacik80eb2342008-10-29 14:49:05 -04009869 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04009870 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04009871 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05009872
Josef Bacik3c148742011-02-02 15:53:47 +00009873 /*
9874 * We haven't cached this block group, which means we could
9875 * possibly have excluded extents on this block group.
9876 */
Josef Bacik36cce922013-08-05 11:15:21 -04009877 if (block_group->cached == BTRFS_CACHE_NO ||
9878 block_group->cached == BTRFS_CACHE_ERROR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009879 free_excluded_extents(info, block_group);
Josef Bacik3c148742011-02-02 15:53:47 +00009880
Josef Bacik817d52f2009-07-13 21:29:25 -04009881 btrfs_remove_free_space_cache(block_group);
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009882 ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
Liu Bof3bca802016-07-20 17:33:44 -07009883 ASSERT(list_empty(&block_group->dirty_list));
9884 ASSERT(list_empty(&block_group->io_list));
9885 ASSERT(list_empty(&block_group->bg_list));
9886 ASSERT(atomic_read(&block_group->count) == 1);
Josef Bacik11dfe352009-11-13 20:12:59 +00009887 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04009888
9889 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009890 }
9891 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04009892
9893 /* now that all the block groups are freed, go through and
9894 * free all the space_info structs. This is only called during
9895 * the final stages of unmount, and so we know nobody is
9896 * using them. We call synchronize_rcu() once before we start,
9897 * just to be on the safe side.
9898 */
9899 synchronize_rcu();
9900
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04009901 release_global_block_rsv(info);
9902
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309903 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009904 int i;
9905
Chris Mason4184ea72009-03-10 12:39:20 -04009906 space_info = list_entry(info->space_info.next,
9907 struct btrfs_space_info,
9908 list);
Josef Bacikd555b6c32016-03-25 13:25:51 -04009909
9910 /*
9911 * Do not hide this behind enospc_debug, this is actually
9912 * important and indicates a real bug if this happens.
9913 */
9914 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00009915 space_info->bytes_reserved > 0 ||
Josef Bacikd555b6c32016-03-25 13:25:51 -04009916 space_info->bytes_may_use > 0))
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009917 dump_space_info(info, space_info, 0, 0);
Chris Mason4184ea72009-03-10 12:39:20 -04009918 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009919 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
9920 struct kobject *kobj;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009921 kobj = space_info->block_group_kobjs[i];
9922 space_info->block_group_kobjs[i] = NULL;
9923 if (kobj) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009924 kobject_del(kobj);
9925 kobject_put(kobj);
9926 }
9927 }
9928 kobject_del(&space_info->kobj);
9929 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04009930 }
Zheng Yan1a40e232008-09-26 10:09:34 -04009931 return 0;
9932}
9933
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009934/* link_block_group will queue up kobjects to add when we're reclaim-safe */
9935void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info)
9936{
9937 struct btrfs_space_info *space_info;
9938 struct raid_kobject *rkobj;
9939 LIST_HEAD(list);
9940 int index;
9941 int ret = 0;
9942
9943 spin_lock(&fs_info->pending_raid_kobjs_lock);
9944 list_splice_init(&fs_info->pending_raid_kobjs, &list);
9945 spin_unlock(&fs_info->pending_raid_kobjs_lock);
9946
9947 list_for_each_entry(rkobj, &list, list) {
9948 space_info = __find_space_info(fs_info, rkobj->flags);
9949 index = btrfs_bg_flags_to_raid_index(rkobj->flags);
9950
9951 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
9952 "%s", get_raid_name(index));
9953 if (ret) {
9954 kobject_put(&rkobj->kobj);
9955 break;
9956 }
9957 }
9958 if (ret)
9959 btrfs_warn(fs_info,
9960 "failed to add kobject for block cache, ignoring");
9961}
9962
Nikolay Borisovc434d212017-08-21 12:43:50 +03009963static void link_block_group(struct btrfs_block_group_cache *cache)
Yan, Zhengb742bb822010-05-16 10:46:24 -04009964{
Nikolay Borisovc434d212017-08-21 12:43:50 +03009965 struct btrfs_space_info *space_info = cache->space_info;
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009966 struct btrfs_fs_info *fs_info = cache->fs_info;
Qu Wenruo3e72ee82018-01-30 18:20:45 +08009967 int index = btrfs_bg_flags_to_raid_index(cache->flags);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009968 bool first = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04009969
9970 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009971 if (list_empty(&space_info->block_groups[index]))
9972 first = true;
9973 list_add_tail(&cache->list, &space_info->block_groups[index]);
9974 up_write(&space_info->groups_sem);
9975
9976 if (first) {
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009977 struct raid_kobject *rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
9978 if (!rkobj) {
9979 btrfs_warn(cache->fs_info,
9980 "couldn't alloc memory for raid level kobject");
9981 return;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009982 }
Jeff Mahoney75cb3792018-03-20 15:25:26 -04009983 rkobj->flags = cache->flags;
9984 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
9985
9986 spin_lock(&fs_info->pending_raid_kobjs_lock);
9987 list_add_tail(&rkobj->list, &fs_info->pending_raid_kobjs);
9988 spin_unlock(&fs_info->pending_raid_kobjs_lock);
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009989 space_info->block_group_kobjs[index] = &rkobj->kobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009990 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04009991}
9992
Miao Xie920e4a52014-01-15 20:00:55 +08009993static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009994btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info,
9995 u64 start, u64 size)
Miao Xie920e4a52014-01-15 20:00:55 +08009996{
9997 struct btrfs_block_group_cache *cache;
9998
9999 cache = kzalloc(sizeof(*cache), GFP_NOFS);
10000 if (!cache)
10001 return NULL;
10002
10003 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
10004 GFP_NOFS);
10005 if (!cache->free_space_ctl) {
10006 kfree(cache);
10007 return NULL;
10008 }
10009
10010 cache->key.objectid = start;
10011 cache->key.offset = size;
10012 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
10013
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010014 cache->fs_info = fs_info;
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +030010015 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010016 set_free_space_tree_thresholds(cache);
10017
Miao Xie920e4a52014-01-15 20:00:55 +080010018 atomic_set(&cache->count, 1);
10019 spin_lock_init(&cache->lock);
Miao Xiee570fd22014-06-19 10:42:50 +080010020 init_rwsem(&cache->data_rwsem);
Miao Xie920e4a52014-01-15 20:00:55 +080010021 INIT_LIST_HEAD(&cache->list);
10022 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010023 INIT_LIST_HEAD(&cache->bg_list);
Josef Bacik633c0aa2014-10-31 09:49:34 -040010024 INIT_LIST_HEAD(&cache->ro_list);
Josef Bacikce93ec52014-11-17 15:45:48 -050010025 INIT_LIST_HEAD(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -070010026 INIT_LIST_HEAD(&cache->io_list);
Miao Xie920e4a52014-01-15 20:00:55 +080010027 btrfs_init_free_space_ctl(cache);
Filipe Manana04216822014-11-27 21:14:15 +000010028 atomic_set(&cache->trimming, 0);
Omar Sandovala5ed9182015-09-29 20:50:35 -070010029 mutex_init(&cache->free_space_lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +080010030 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
Miao Xie920e4a52014-01-15 20:00:55 +080010031
10032 return cache;
10033}
10034
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010035int btrfs_read_block_groups(struct btrfs_fs_info *info)
Chris Mason9078a3e2007-04-26 16:46:15 -040010036{
10037 struct btrfs_path *path;
10038 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010039 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -040010040 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -040010041 struct btrfs_key key;
10042 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -040010043 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -040010044 int need_clear = 0;
10045 u64 cache_gen;
Liu Bo49303382016-08-25 18:08:27 -070010046 u64 feature;
10047 int mixed;
10048
10049 feature = btrfs_super_incompat_flags(info->super_copy);
10050 mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS);
Chris Mason96b51792007-10-15 16:15:19 -040010051
Chris Mason9078a3e2007-04-26 16:46:15 -040010052 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -040010053 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010054 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Chris Mason9078a3e2007-04-26 16:46:15 -040010055 path = btrfs_alloc_path();
10056 if (!path)
10057 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +010010058 path->reada = READA_FORWARD;
Chris Mason9078a3e2007-04-26 16:46:15 -040010059
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010060 cache_gen = btrfs_super_cache_generation(info->super_copy);
10061 if (btrfs_test_opt(info, SPACE_CACHE) &&
10062 btrfs_super_generation(info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -040010063 need_clear = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010064 if (btrfs_test_opt(info, CLEAR_CACHE))
Josef Bacik88c2ba32010-09-21 14:21:34 -040010065 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -040010066
Chris Masond3977122009-01-05 21:25:51 -050010067 while (1) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010068 ret = find_first_block_group(info, path, &key);
Yan, Zhengb742bb822010-05-16 10:46:24 -040010069 if (ret > 0)
10070 break;
Chris Mason0b86a832008-03-24 15:01:56 -040010071 if (ret != 0)
10072 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +080010073
Chris Mason5f39d392007-10-15 16:14:19 -040010074 leaf = path->nodes[0];
10075 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +080010076
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010077 cache = btrfs_create_block_group_cache(info, found_key.objectid,
Miao Xie920e4a52014-01-15 20:00:55 +080010078 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -040010079 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -040010080 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -040010081 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -040010082 }
Josef Bacik96303082009-07-13 21:29:25 -040010083
Liu Bocf7c1ef2012-07-06 03:31:34 -060010084 if (need_clear) {
10085 /*
10086 * When we mount with old space cache, we need to
10087 * set BTRFS_DC_CLEAR and set dirty flag.
10088 *
10089 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
10090 * truncate the old free space cache inode and
10091 * setup a new one.
10092 * b) Setting 'dirty flag' makes sure that we flush
10093 * the new space cache info onto disk.
10094 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010095 if (btrfs_test_opt(info, SPACE_CACHE))
Josef Bacikce93ec52014-11-17 15:45:48 -050010096 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -060010097 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010098
Chris Mason5f39d392007-10-15 16:14:19 -040010099 read_extent_buffer(leaf, &cache->item,
10100 btrfs_item_ptr_offset(leaf, path->slots[0]),
10101 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +080010102 cache->flags = btrfs_block_group_flags(&cache->item);
Liu Bo49303382016-08-25 18:08:27 -070010103 if (!mixed &&
10104 ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
10105 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
10106 btrfs_err(info,
10107"bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
10108 cache->key.objectid);
10109 ret = -EINVAL;
10110 goto error;
10111 }
Chris Mason0b86a832008-03-24 15:01:56 -040010112
Chris Mason9078a3e2007-04-26 16:46:15 -040010113 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +020010114 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +080010115
Josef Bacik817d52f2009-07-13 21:29:25 -040010116 /*
Josef Bacik3c148742011-02-02 15:53:47 +000010117 * We need to exclude the super stripes now so that the space
10118 * info has super bytes accounted for, otherwise we'll think
10119 * we have more space than we actually do.
10120 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010121 ret = exclude_super_stripes(info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010122 if (ret) {
10123 /*
10124 * We may have excluded something, so call this just in
10125 * case.
10126 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010127 free_excluded_extents(info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010128 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010129 goto error;
10130 }
Josef Bacik3c148742011-02-02 15:53:47 +000010131
10132 /*
Josef Bacik817d52f2009-07-13 21:29:25 -040010133 * check for two cases, either we are full, and therefore
10134 * don't need to bother with the caching work since we won't
10135 * find any space, or we are empty, and we can just add all
10136 * the space in and be done with it. This saves us _alot_ of
10137 * time, particularly in the full case.
10138 */
10139 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -040010140 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010141 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010142 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010143 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -040010144 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010145 cache->cached = BTRFS_CACHE_FINISHED;
Nikolay Borisov4457c1c2018-05-10 15:44:45 +030010146 add_new_free_space(cache, found_key.objectid,
Josef Bacik817d52f2009-07-13 21:29:25 -040010147 found_key.objectid +
10148 found_key.offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010149 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010150 }
Chris Mason96b51792007-10-15 16:15:19 -040010151
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010152 ret = btrfs_add_block_group_cache(info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010153 if (ret) {
10154 btrfs_remove_free_space_cache(cache);
10155 btrfs_put_block_group(cache);
10156 goto error;
10157 }
10158
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010159 trace_btrfs_add_block_group(info, cache, 0);
Nikolay Borisovd2006e62017-05-22 09:35:50 +030010160 update_space_info(info, cache->flags, found_key.offset,
10161 btrfs_block_group_used(&cache->item),
10162 cache->bytes_super, &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010163
Chris Mason6324fbf2008-03-24 15:01:59 -040010164 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -040010165
Nikolay Borisovc434d212017-08-21 12:43:50 +030010166 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010167
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010168 set_avail_alloc_bits(info, cache->flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010169 if (btrfs_chunk_readonly(info, cache->key.objectid)) {
Zhaolei868f4012015-08-05 16:43:27 +080010170 inc_block_group_ro(cache, 1);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010171 } else if (btrfs_block_group_used(&cache->item) == 0) {
10172 spin_lock(&info->unused_bgs_lock);
10173 /* Should always be true but just in case. */
10174 if (list_empty(&cache->bg_list)) {
10175 btrfs_get_block_group(cache);
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +080010176 trace_btrfs_add_unused_block_group(cache);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010177 list_add_tail(&cache->bg_list,
10178 &info->unused_bgs);
10179 }
10180 spin_unlock(&info->unused_bgs_lock);
10181 }
Chris Mason9078a3e2007-04-26 16:46:15 -040010182 }
Yan, Zhengb742bb822010-05-16 10:46:24 -040010183
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010184 list_for_each_entry_rcu(space_info, &info->space_info, list) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010185 if (!(get_alloc_profile(info, space_info->flags) &
Yan, Zhengb742bb822010-05-16 10:46:24 -040010186 (BTRFS_BLOCK_GROUP_RAID10 |
10187 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -050010188 BTRFS_BLOCK_GROUP_RAID5 |
10189 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb822010-05-16 10:46:24 -040010190 BTRFS_BLOCK_GROUP_DUP)))
10191 continue;
10192 /*
10193 * avoid allocating from un-mirrored block group if there are
10194 * mirrored block groups.
10195 */
chandan1095cc02013-07-16 12:28:56 +053010196 list_for_each_entry(cache,
10197 &space_info->block_groups[BTRFS_RAID_RAID0],
10198 list)
Zhaolei868f4012015-08-05 16:43:27 +080010199 inc_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +053010200 list_for_each_entry(cache,
10201 &space_info->block_groups[BTRFS_RAID_SINGLE],
10202 list)
Zhaolei868f4012015-08-05 16:43:27 +080010203 inc_block_group_ro(cache, 1);
Yan, Zhengb742bb822010-05-16 10:46:24 -040010204 }
Yan, Zhengf0486c62010-05-16 10:46:25 -040010205
Jeff Mahoney75cb3792018-03-20 15:25:26 -040010206 btrfs_add_raid_kobjects(info);
Yan, Zhengf0486c62010-05-16 10:46:25 -040010207 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -040010208 ret = 0;
10209error:
Chris Mason9078a3e2007-04-26 16:46:15 -040010210 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -040010211 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010212}
Chris Mason6324fbf2008-03-24 15:01:59 -040010213
Nikolay Borisov6c686b32018-02-07 17:55:40 +020010214void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans)
Josef Bacikea658ba2012-09-11 16:57:25 -040010215{
Nikolay Borisov6c686b32018-02-07 17:55:40 +020010216 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacikea658ba2012-09-11 16:57:25 -040010217 struct btrfs_block_group_cache *block_group, *tmp;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010218 struct btrfs_root *extent_root = fs_info->extent_root;
Josef Bacikea658ba2012-09-11 16:57:25 -040010219 struct btrfs_block_group_item item;
10220 struct btrfs_key key;
10221 int ret = 0;
Filipe Mananad9a05402015-10-03 13:13:13 +010010222 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010223
Filipe Mananad9a05402015-10-03 13:13:13 +010010224 trans->can_flush_pending_bgs = false;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010225 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
Josef Bacikea658ba2012-09-11 16:57:25 -040010226 if (ret)
Filipe Mananac92f6be2014-11-26 15:28:55 +000010227 goto next;
Josef Bacikea658ba2012-09-11 16:57:25 -040010228
10229 spin_lock(&block_group->lock);
10230 memcpy(&item, &block_group->item, sizeof(item));
10231 memcpy(&key, &block_group->key, sizeof(key));
10232 spin_unlock(&block_group->lock);
10233
10234 ret = btrfs_insert_item(trans, extent_root, &key, &item,
10235 sizeof(item));
10236 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010237 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010238 ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid,
10239 key.offset);
Josef Bacik6df9a952013-06-27 13:22:46 -040010240 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010241 btrfs_abort_transaction(trans, ret);
Nikolay Borisove4e07112018-05-10 15:44:41 +030010242 add_block_group_free_space(trans, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010243 /* already aborted the transaction if it failed. */
Filipe Mananac92f6be2014-11-26 15:28:55 +000010244next:
10245 list_del_init(&block_group->bg_list);
Josef Bacikea658ba2012-09-11 16:57:25 -040010246 }
Filipe Mananad9a05402015-10-03 13:13:13 +010010247 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010248}
10249
Chris Mason6324fbf2008-03-24 15:01:59 -040010250int btrfs_make_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010251 struct btrfs_fs_info *fs_info, u64 bytes_used,
Nikolay Borisov01744842017-07-27 14:22:11 +030010252 u64 type, u64 chunk_offset, u64 size)
Chris Mason6324fbf2008-03-24 15:01:59 -040010253{
Chris Mason6324fbf2008-03-24 15:01:59 -040010254 struct btrfs_block_group_cache *cache;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010255 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -040010256
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010257 btrfs_set_log_full_commit(fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -040010258
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010259 cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -040010260 if (!cache)
10261 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +080010262
Chris Mason6324fbf2008-03-24 15:01:59 -040010263 btrfs_set_block_group_used(&cache->item, bytes_used);
Nikolay Borisov01744842017-07-27 14:22:11 +030010264 btrfs_set_block_group_chunk_objectid(&cache->item,
10265 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Mason6324fbf2008-03-24 15:01:59 -040010266 btrfs_set_block_group_flags(&cache->item, type);
10267
Miao Xie920e4a52014-01-15 20:00:55 +080010268 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -040010269 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010270 cache->cached = BTRFS_CACHE_FINISHED;
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010271 cache->needs_free_space = 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010272 ret = exclude_super_stripes(fs_info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010273 if (ret) {
10274 /*
10275 * We may have excluded something, so call this just in
10276 * case.
10277 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010278 free_excluded_extents(fs_info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010279 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010280 return ret;
10281 }
Josef Bacik96303082009-07-13 21:29:25 -040010282
Nikolay Borisov4457c1c2018-05-10 15:44:45 +030010283 add_new_free_space(cache, chunk_offset, chunk_offset + size);
Josef Bacik817d52f2009-07-13 21:29:25 -040010284
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010285 free_excluded_extents(fs_info, cache);
Yan Zheng11833d62009-09-11 16:11:19 -040010286
Josef Bacikd0bd4562015-09-23 14:54:14 -040010287#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010288 if (btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -040010289 u64 new_bytes_used = size - bytes_used;
10290
10291 bytes_used += new_bytes_used >> 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010292 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -040010293 }
10294#endif
Filipe Manana2e6e5182015-05-12 00:28:11 +010010295 /*
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010296 * Ensure the corresponding space_info object is created and
10297 * assigned to our block group. We want our bg to be added to the rbtree
10298 * with its ->space_info set.
Filipe Manana2e6e5182015-05-12 00:28:11 +010010299 */
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010300 cache->space_info = __find_space_info(fs_info, cache->flags);
Jeff Mahoneydc2d3002018-03-20 15:25:25 -040010301 ASSERT(cache->space_info);
Filipe Manana2e6e5182015-05-12 00:28:11 +010010302
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010303 ret = btrfs_add_block_group_cache(fs_info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010304 if (ret) {
10305 btrfs_remove_free_space_cache(cache);
10306 btrfs_put_block_group(cache);
10307 return ret;
10308 }
10309
Filipe Manana2e6e5182015-05-12 00:28:11 +010010310 /*
10311 * Now that our block group has its ->space_info set and is inserted in
10312 * the rbtree, update the space info's counters.
10313 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010314 trace_btrfs_add_block_group(fs_info, cache, 1);
Nikolay Borisovd2006e62017-05-22 09:35:50 +030010315 update_space_info(fs_info, cache->flags, size, bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -040010316 cache->bytes_super, &cache->space_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010317 update_global_block_rsv(fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -040010318
Nikolay Borisovc434d212017-08-21 12:43:50 +030010319 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010320
Josef Bacik47ab2a62014-09-18 11:20:02 -040010321 list_add_tail(&cache->bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -040010322
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010323 set_avail_alloc_bits(fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -040010324 return 0;
10325}
Zheng Yan1a40e232008-09-26 10:09:34 -040010326
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010327static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
10328{
Ilya Dryomov899c81e2012-03-27 17:09:16 +030010329 u64 extra_flags = chunk_to_extended(flags) &
10330 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010331
Miao Xiede98ced2013-01-29 10:13:12 +000010332 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010333 if (flags & BTRFS_BLOCK_GROUP_DATA)
10334 fs_info->avail_data_alloc_bits &= ~extra_flags;
10335 if (flags & BTRFS_BLOCK_GROUP_METADATA)
10336 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
10337 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
10338 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +000010339 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010340}
10341
Zheng Yan1a40e232008-09-26 10:09:34 -040010342int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010343 struct btrfs_fs_info *fs_info, u64 group_start,
Filipe Manana04216822014-11-27 21:14:15 +000010344 struct extent_map *em)
Zheng Yan1a40e232008-09-26 10:09:34 -040010345{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010346 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010347 struct btrfs_path *path;
10348 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -040010349 struct btrfs_free_cluster *cluster;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010350 struct btrfs_root *tree_root = fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010351 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -040010352 struct inode *inode;
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010353 struct kobject *kobj = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -040010354 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010355 int index;
Josef Bacik89a55892010-10-14 14:52:27 -040010356 int factor;
Filipe Manana4f69cb92014-11-26 15:28:51 +000010357 struct btrfs_caching_control *caching_ctl = NULL;
Filipe Manana04216822014-11-27 21:14:15 +000010358 bool remove_em;
Zheng Yan1a40e232008-09-26 10:09:34 -040010359
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010360 block_group = btrfs_lookup_block_group(fs_info, group_start);
Zheng Yan1a40e232008-09-26 10:09:34 -040010361 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -050010362 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -040010363
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +080010364 trace_btrfs_remove_block_group(block_group);
liubo9f7c43c2011-03-07 02:13:33 +000010365 /*
10366 * Free the reserved super bytes from this block group before
10367 * remove it.
10368 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010369 free_excluded_extents(fs_info, block_group);
Josef Bacikfd708b82017-09-29 15:43:50 -040010370 btrfs_free_ref_tree_range(fs_info, block_group->key.objectid,
10371 block_group->key.offset);
liubo9f7c43c2011-03-07 02:13:33 +000010372
Zheng Yan1a40e232008-09-26 10:09:34 -040010373 memcpy(&key, &block_group->key, sizeof(key));
Qu Wenruo3e72ee82018-01-30 18:20:45 +080010374 index = btrfs_bg_flags_to_raid_index(block_group->flags);
Josef Bacik89a55892010-10-14 14:52:27 -040010375 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
10376 BTRFS_BLOCK_GROUP_RAID1 |
10377 BTRFS_BLOCK_GROUP_RAID10))
10378 factor = 2;
10379 else
10380 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -040010381
Chris Mason44fb5512009-06-04 15:34:51 -040010382 /* make sure this block group isn't part of an allocation cluster */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010383 cluster = &fs_info->data_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010384 spin_lock(&cluster->refill_lock);
10385 btrfs_return_cluster_to_free_space(block_group, cluster);
10386 spin_unlock(&cluster->refill_lock);
10387
10388 /*
10389 * make sure this block group isn't part of a metadata
10390 * allocation cluster
10391 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010392 cluster = &fs_info->meta_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010393 spin_lock(&cluster->refill_lock);
10394 btrfs_return_cluster_to_free_space(block_group, cluster);
10395 spin_unlock(&cluster->refill_lock);
10396
Zheng Yan1a40e232008-09-26 10:09:34 -040010397 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010398 if (!path) {
10399 ret = -ENOMEM;
10400 goto out;
10401 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010402
Chris Mason1bbc6212015-04-06 12:46:08 -070010403 /*
10404 * get the inode first so any iput calls done for the io_list
10405 * aren't the final iput (no unlinks allowed now)
10406 */
Jeff Mahoney77ab86b2017-02-15 16:28:30 -050010407 inode = lookup_free_space_inode(fs_info, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010408
10409 mutex_lock(&trans->transaction->cache_write_mutex);
10410 /*
10411 * make sure our free spache cache IO is done before remove the
10412 * free space inode
10413 */
10414 spin_lock(&trans->transaction->dirty_bgs_lock);
10415 if (!list_empty(&block_group->io_list)) {
10416 list_del_init(&block_group->io_list);
10417
10418 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
10419
10420 spin_unlock(&trans->transaction->dirty_bgs_lock);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040010421 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010422 btrfs_put_block_group(block_group);
10423 spin_lock(&trans->transaction->dirty_bgs_lock);
10424 }
10425
10426 if (!list_empty(&block_group->dirty_list)) {
10427 list_del_init(&block_group->dirty_list);
10428 btrfs_put_block_group(block_group);
10429 }
10430 spin_unlock(&trans->transaction->dirty_bgs_lock);
10431 mutex_unlock(&trans->transaction->cache_write_mutex);
10432
Josef Bacik0af3d002010-06-21 14:48:16 -040010433 if (!IS_ERR(inode)) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010434 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010435 if (ret) {
10436 btrfs_add_delayed_iput(inode);
10437 goto out;
10438 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010439 clear_nlink(inode);
10440 /* One for the block groups ref */
10441 spin_lock(&block_group->lock);
10442 if (block_group->iref) {
10443 block_group->iref = 0;
10444 block_group->inode = NULL;
10445 spin_unlock(&block_group->lock);
10446 iput(inode);
10447 } else {
10448 spin_unlock(&block_group->lock);
10449 }
10450 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -040010451 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -040010452 }
10453
10454 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
10455 key.offset = block_group->key.objectid;
10456 key.type = 0;
10457
10458 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
10459 if (ret < 0)
10460 goto out;
10461 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +020010462 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010463 if (ret == 0) {
10464 ret = btrfs_del_item(trans, tree_root, path);
10465 if (ret)
10466 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +020010467 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010468 }
10469
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010470 spin_lock(&fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -040010471 rb_erase(&block_group->cache_node,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010472 &fs_info->block_group_cache_tree);
Filipe Manana292cbd52014-11-26 15:28:50 +000010473 RB_CLEAR_NODE(&block_group->cache_node);
Liu Boa1897fd2012-12-27 09:01:23 +000010474
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010475 if (fs_info->first_logical_byte == block_group->key.objectid)
10476 fs_info->first_logical_byte = (u64)-1;
10477 spin_unlock(&fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010478
Josef Bacik80eb2342008-10-29 14:49:05 -040010479 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -040010480 /*
10481 * we must use list_del_init so people can check to see if they
10482 * are still on the list after taking the semaphore
10483 */
10484 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010485 if (list_empty(&block_group->space_info->block_groups[index])) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010486 kobj = block_group->space_info->block_group_kobjs[index];
10487 block_group->space_info->block_group_kobjs[index] = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010488 clear_avail_alloc_bits(fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010489 }
Josef Bacik80eb2342008-10-29 14:49:05 -040010490 up_write(&block_group->space_info->groups_sem);
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010491 if (kobj) {
10492 kobject_del(kobj);
10493 kobject_put(kobj);
10494 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010495
Filipe Manana4f69cb92014-11-26 15:28:51 +000010496 if (block_group->has_caching_ctl)
10497 caching_ctl = get_caching_control(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -040010498 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -040010499 wait_block_group_cache_done(block_group);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010500 if (block_group->has_caching_ctl) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010501 down_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010502 if (!caching_ctl) {
10503 struct btrfs_caching_control *ctl;
10504
10505 list_for_each_entry(ctl,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010506 &fs_info->caching_block_groups, list)
Filipe Manana4f69cb92014-11-26 15:28:51 +000010507 if (ctl->block_group == block_group) {
10508 caching_ctl = ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +020010509 refcount_inc(&caching_ctl->count);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010510 break;
10511 }
10512 }
10513 if (caching_ctl)
10514 list_del_init(&caching_ctl->list);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010515 up_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010516 if (caching_ctl) {
10517 /* Once for the caching bgs list and once for us. */
10518 put_caching_control(caching_ctl);
10519 put_caching_control(caching_ctl);
10520 }
10521 }
Josef Bacik817d52f2009-07-13 21:29:25 -040010522
Josef Bacikce93ec52014-11-17 15:45:48 -050010523 spin_lock(&trans->transaction->dirty_bgs_lock);
10524 if (!list_empty(&block_group->dirty_list)) {
Chris Mason1bbc6212015-04-06 12:46:08 -070010525 WARN_ON(1);
10526 }
10527 if (!list_empty(&block_group->io_list)) {
10528 WARN_ON(1);
Josef Bacikce93ec52014-11-17 15:45:48 -050010529 }
10530 spin_unlock(&trans->transaction->dirty_bgs_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010531 btrfs_remove_free_space_cache(block_group);
10532
Yan Zhengc146afa2008-11-12 14:34:12 -050010533 spin_lock(&block_group->space_info->lock);
Filipe Manana75c68e92015-01-16 13:24:40 +000010534 list_del_init(&block_group->ro_list);
Zhao Lei18d018a2015-02-24 20:07:44 +080010535
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010536 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Zhao Lei18d018a2015-02-24 20:07:44 +080010537 WARN_ON(block_group->space_info->total_bytes
10538 < block_group->key.offset);
10539 WARN_ON(block_group->space_info->bytes_readonly
10540 < block_group->key.offset);
10541 WARN_ON(block_group->space_info->disk_total
10542 < block_group->key.offset * factor);
10543 }
Yan Zhengc146afa2008-11-12 14:34:12 -050010544 block_group->space_info->total_bytes -= block_group->key.offset;
10545 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -040010546 block_group->space_info->disk_total -= block_group->key.offset * factor;
Zhao Lei18d018a2015-02-24 20:07:44 +080010547
Yan Zhengc146afa2008-11-12 14:34:12 -050010548 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -040010549
Josef Bacik0af3d002010-06-21 14:48:16 -040010550 memcpy(&key, &block_group->key, sizeof(key));
10551
David Sterba34441362016-10-04 19:34:27 +020010552 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana495e64f2014-12-02 18:07:30 +000010553 if (!list_empty(&em->list)) {
10554 /* We're in the transaction->pending_chunks list. */
10555 free_extent_map(em);
10556 }
Filipe Manana04216822014-11-27 21:14:15 +000010557 spin_lock(&block_group->lock);
10558 block_group->removed = 1;
10559 /*
10560 * At this point trimming can't start on this block group, because we
10561 * removed the block group from the tree fs_info->block_group_cache_tree
10562 * so no one can't find it anymore and even if someone already got this
10563 * block group before we removed it from the rbtree, they have already
10564 * incremented block_group->trimming - if they didn't, they won't find
10565 * any free space entries because we already removed them all when we
10566 * called btrfs_remove_free_space_cache().
10567 *
10568 * And we must not remove the extent map from the fs_info->mapping_tree
10569 * to prevent the same logical address range and physical device space
10570 * ranges from being reused for a new block group. This is because our
10571 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
10572 * completely transactionless, so while it is trimming a range the
10573 * currently running transaction might finish and a new one start,
10574 * allowing for new block groups to be created that can reuse the same
10575 * physical device locations unless we take this special care.
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010576 *
10577 * There may also be an implicit trim operation if the file system
10578 * is mounted with -odiscard. The same protections must remain
10579 * in place until the extents have been discarded completely when
10580 * the transaction commit has completed.
Filipe Manana04216822014-11-27 21:14:15 +000010581 */
10582 remove_em = (atomic_read(&block_group->trimming) == 0);
10583 /*
10584 * Make sure a trimmer task always sees the em in the pinned_chunks list
10585 * if it sees block_group->removed == 1 (needs to lock block_group->lock
10586 * before checking block_group->removed).
10587 */
10588 if (!remove_em) {
10589 /*
10590 * Our em might be in trans->transaction->pending_chunks which
10591 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10592 * and so is the fs_info->pinned_chunks list.
10593 *
10594 * So at this point we must be holding the chunk_mutex to avoid
10595 * any races with chunk allocation (more specifically at
10596 * volumes.c:contains_pending_extent()), to ensure it always
10597 * sees the em, either in the pending_chunks list or in the
10598 * pinned_chunks list.
10599 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010600 list_move_tail(&em->list, &fs_info->pinned_chunks);
Filipe Manana04216822014-11-27 21:14:15 +000010601 }
10602 spin_unlock(&block_group->lock);
Filipe Manana04216822014-11-27 21:14:15 +000010603
10604 if (remove_em) {
10605 struct extent_map_tree *em_tree;
10606
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010607 em_tree = &fs_info->mapping_tree.map_tree;
Filipe Manana04216822014-11-27 21:14:15 +000010608 write_lock(&em_tree->lock);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010609 /*
10610 * The em might be in the pending_chunks list, so make sure the
10611 * chunk mutex is locked, since remove_extent_mapping() will
10612 * delete us from that list.
10613 */
Filipe Manana04216822014-11-27 21:14:15 +000010614 remove_extent_mapping(em_tree, em);
10615 write_unlock(&em_tree->lock);
10616 /* once for the tree */
10617 free_extent_map(em);
10618 }
10619
David Sterba34441362016-10-04 19:34:27 +020010620 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010621
Nikolay Borisovf3f72772018-05-10 15:44:46 +030010622 ret = remove_block_group_free_space(trans, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010623 if (ret)
10624 goto out;
10625
Chris Masonfa9c0d792009-04-03 09:47:43 -040010626 btrfs_put_block_group(block_group);
10627 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -040010628
10629 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
10630 if (ret > 0)
10631 ret = -EIO;
10632 if (ret < 0)
10633 goto out;
10634
10635 ret = btrfs_del_item(trans, root, path);
10636out:
10637 btrfs_free_path(path);
10638 return ret;
10639}
liuboacce9522011-01-06 19:30:25 +080010640
Filipe Manana8eab77f2015-11-13 23:57:16 +000010641struct btrfs_trans_handle *
Filipe Manana7fd01182015-11-13 23:57:17 +000010642btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info,
10643 const u64 chunk_offset)
Filipe Manana8eab77f2015-11-13 23:57:16 +000010644{
Filipe Manana7fd01182015-11-13 23:57:17 +000010645 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
10646 struct extent_map *em;
10647 struct map_lookup *map;
10648 unsigned int num_items;
10649
10650 read_lock(&em_tree->lock);
10651 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
10652 read_unlock(&em_tree->lock);
10653 ASSERT(em && em->start == chunk_offset);
10654
Filipe Manana8eab77f2015-11-13 23:57:16 +000010655 /*
Filipe Manana7fd01182015-11-13 23:57:17 +000010656 * We need to reserve 3 + N units from the metadata space info in order
10657 * to remove a block group (done at btrfs_remove_chunk() and at
10658 * btrfs_remove_block_group()), which are used for:
10659 *
Filipe Manana8eab77f2015-11-13 23:57:16 +000010660 * 1 unit for adding the free space inode's orphan (located in the tree
10661 * of tree roots).
Filipe Manana7fd01182015-11-13 23:57:17 +000010662 * 1 unit for deleting the block group item (located in the extent
10663 * tree).
10664 * 1 unit for deleting the free space item (located in tree of tree
10665 * roots).
10666 * N units for deleting N device extent items corresponding to each
10667 * stripe (located in the device tree).
10668 *
10669 * In order to remove a block group we also need to reserve units in the
10670 * system space info in order to update the chunk tree (update one or
10671 * more device items and remove one chunk item), but this is done at
10672 * btrfs_remove_chunk() through a call to check_system_chunk().
Filipe Manana8eab77f2015-11-13 23:57:16 +000010673 */
Jeff Mahoney95617d62015-06-03 10:55:48 -040010674 map = em->map_lookup;
Filipe Manana7fd01182015-11-13 23:57:17 +000010675 num_items = 3 + map->num_stripes;
10676 free_extent_map(em);
10677
Filipe Manana8eab77f2015-11-13 23:57:16 +000010678 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
Filipe Manana7fd01182015-11-13 23:57:17 +000010679 num_items, 1);
Filipe Manana8eab77f2015-11-13 23:57:16 +000010680}
10681
Josef Bacik47ab2a62014-09-18 11:20:02 -040010682/*
10683 * Process the unused_bgs list and remove any that don't have any allocated
10684 * space inside of them.
10685 */
10686void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10687{
10688 struct btrfs_block_group_cache *block_group;
10689 struct btrfs_space_info *space_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010690 struct btrfs_trans_handle *trans;
10691 int ret = 0;
10692
Josef Bacikafcdd122016-09-02 15:40:02 -040010693 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
Josef Bacik47ab2a62014-09-18 11:20:02 -040010694 return;
10695
10696 spin_lock(&fs_info->unused_bgs_lock);
10697 while (!list_empty(&fs_info->unused_bgs)) {
10698 u64 start, end;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010699 int trimming;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010700
10701 block_group = list_first_entry(&fs_info->unused_bgs,
10702 struct btrfs_block_group_cache,
10703 bg_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010704 list_del_init(&block_group->bg_list);
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010705
10706 space_info = block_group->space_info;
10707
Josef Bacik47ab2a62014-09-18 11:20:02 -040010708 if (ret || btrfs_mixed_space_info(space_info)) {
10709 btrfs_put_block_group(block_group);
10710 continue;
10711 }
10712 spin_unlock(&fs_info->unused_bgs_lock);
10713
Zhao Leid5f2e332015-10-08 18:46:44 +080010714 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +010010715
Josef Bacik47ab2a62014-09-18 11:20:02 -040010716 /* Don't want to race with allocators so take the groups_sem */
10717 down_write(&space_info->groups_sem);
10718 spin_lock(&block_group->lock);
10719 if (block_group->reserved ||
10720 btrfs_block_group_used(&block_group->item) ||
Chris Mason19c4d2f2016-10-10 13:43:31 -070010721 block_group->ro ||
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010722 list_is_singular(&block_group->list)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -040010723 /*
10724 * We want to bail if we made new allocations or have
10725 * outstanding allocations in this block group. We do
10726 * the ro check in case balance is currently acting on
10727 * this block group.
10728 */
Qu Wenruo4ed0a7a2018-04-26 17:17:20 +080010729 trace_btrfs_skip_unused_block_group(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010730 spin_unlock(&block_group->lock);
10731 up_write(&space_info->groups_sem);
10732 goto next;
10733 }
10734 spin_unlock(&block_group->lock);
10735
10736 /* We don't want to force the issue, only flip if it's ok. */
Zhaolei868f4012015-08-05 16:43:27 +080010737 ret = inc_block_group_ro(block_group, 0);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010738 up_write(&space_info->groups_sem);
10739 if (ret < 0) {
10740 ret = 0;
10741 goto next;
10742 }
10743
10744 /*
10745 * Want to do this before we do anything else so we can recover
10746 * properly if we fail to join the transaction.
10747 */
Filipe Manana7fd01182015-11-13 23:57:17 +000010748 trans = btrfs_start_trans_remove_block_group(fs_info,
10749 block_group->key.objectid);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010750 if (IS_ERR(trans)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010751 btrfs_dec_block_group_ro(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010752 ret = PTR_ERR(trans);
10753 goto next;
10754 }
10755
10756 /*
10757 * We could have pending pinned extents for this block group,
10758 * just delete them, we don't care about them anymore.
10759 */
10760 start = block_group->key.objectid;
10761 end = start + block_group->key.offset - 1;
Filipe Mananad4b450c2015-01-29 19:18:25 +000010762 /*
10763 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10764 * btrfs_finish_extent_commit(). If we are at transaction N,
10765 * another task might be running finish_extent_commit() for the
10766 * previous transaction N - 1, and have seen a range belonging
10767 * to the block group in freed_extents[] before we were able to
10768 * clear the whole block group range from freed_extents[]. This
10769 * means that task can lookup for the block group after we
10770 * unpinned it from freed_extents[] and removed it, leading to
10771 * a BUG_ON() at btrfs_unpin_extent_range().
10772 */
10773 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana758eb512014-11-03 14:08:39 +000010774 ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
David Sterba91166212016-04-26 23:54:39 +020010775 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010776 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010777 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010778 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010779 goto end_trans;
10780 }
10781 ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
David Sterba91166212016-04-26 23:54:39 +020010782 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010783 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010784 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010785 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010786 goto end_trans;
10787 }
Filipe Mananad4b450c2015-01-29 19:18:25 +000010788 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010789
10790 /* Reset pinned so btrfs_put_block_group doesn't complain */
Zhao Leic30666d2015-02-25 14:17:20 +080010791 spin_lock(&space_info->lock);
10792 spin_lock(&block_group->lock);
10793
10794 space_info->bytes_pinned -= block_group->pinned;
10795 space_info->bytes_readonly += block_group->pinned;
10796 percpu_counter_add(&space_info->total_bytes_pinned,
10797 -block_group->pinned);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010798 block_group->pinned = 0;
10799
Zhao Leic30666d2015-02-25 14:17:20 +080010800 spin_unlock(&block_group->lock);
10801 spin_unlock(&space_info->lock);
10802
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010803 /* DISCARD can flip during remount */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010804 trimming = btrfs_test_opt(fs_info, DISCARD);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010805
10806 /* Implicit trim during transaction commit. */
10807 if (trimming)
10808 btrfs_get_block_group_trimming(block_group);
10809
Josef Bacik47ab2a62014-09-18 11:20:02 -040010810 /*
10811 * Btrfs_remove_chunk will abort the transaction if things go
10812 * horribly wrong.
10813 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010814 ret = btrfs_remove_chunk(trans, fs_info,
Josef Bacik47ab2a62014-09-18 11:20:02 -040010815 block_group->key.objectid);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010816
10817 if (ret) {
10818 if (trimming)
10819 btrfs_put_block_group_trimming(block_group);
10820 goto end_trans;
10821 }
10822
10823 /*
10824 * If we're not mounted with -odiscard, we can just forget
10825 * about this block group. Otherwise we'll need to wait
10826 * until transaction commit to do the actual discard.
10827 */
10828 if (trimming) {
Filipe Manana348a0012015-11-27 12:16:16 +000010829 spin_lock(&fs_info->unused_bgs_lock);
10830 /*
10831 * A concurrent scrub might have added us to the list
10832 * fs_info->unused_bgs, so use a list_move operation
10833 * to add the block group to the deleted_bgs list.
10834 */
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010835 list_move(&block_group->bg_list,
10836 &trans->transaction->deleted_bgs);
Filipe Manana348a0012015-11-27 12:16:16 +000010837 spin_unlock(&fs_info->unused_bgs_lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010838 btrfs_get_block_group(block_group);
10839 }
Filipe Manana758eb512014-11-03 14:08:39 +000010840end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010841 btrfs_end_transaction(trans);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010842next:
Zhao Leid5f2e332015-10-08 18:46:44 +080010843 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010844 btrfs_put_block_group(block_group);
10845 spin_lock(&fs_info->unused_bgs_lock);
10846 }
10847 spin_unlock(&fs_info->unused_bgs_lock);
10848}
10849
liuboc59021f2011-03-07 02:13:14 +000010850int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
10851{
liubo1aba86d2011-04-08 08:44:37 +000010852 struct btrfs_super_block *disk_super;
10853 u64 features;
10854 u64 flags;
10855 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +000010856 int ret;
10857
David Sterba6c417612011-04-13 15:41:04 +020010858 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +000010859 if (!btrfs_super_root(disk_super))
Dan Carpenter0dc924c52016-01-13 15:21:17 +030010860 return -EINVAL;
liuboc59021f2011-03-07 02:13:14 +000010861
liubo1aba86d2011-04-08 08:44:37 +000010862 features = btrfs_super_incompat_flags(disk_super);
10863 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
10864 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +000010865
liubo1aba86d2011-04-08 08:44:37 +000010866 flags = BTRFS_BLOCK_GROUP_SYSTEM;
Lu Fengqi4ca61682018-05-28 14:30:27 +080010867 ret = create_space_info(fs_info, flags);
liuboc59021f2011-03-07 02:13:14 +000010868 if (ret)
liubo1aba86d2011-04-08 08:44:37 +000010869 goto out;
liuboc59021f2011-03-07 02:13:14 +000010870
liubo1aba86d2011-04-08 08:44:37 +000010871 if (mixed) {
10872 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
Lu Fengqi4ca61682018-05-28 14:30:27 +080010873 ret = create_space_info(fs_info, flags);
liubo1aba86d2011-04-08 08:44:37 +000010874 } else {
10875 flags = BTRFS_BLOCK_GROUP_METADATA;
Lu Fengqi4ca61682018-05-28 14:30:27 +080010876 ret = create_space_info(fs_info, flags);
liubo1aba86d2011-04-08 08:44:37 +000010877 if (ret)
10878 goto out;
10879
10880 flags = BTRFS_BLOCK_GROUP_DATA;
Lu Fengqi4ca61682018-05-28 14:30:27 +080010881 ret = create_space_info(fs_info, flags);
liubo1aba86d2011-04-08 08:44:37 +000010882 }
10883out:
liuboc59021f2011-03-07 02:13:14 +000010884 return ret;
10885}
10886
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010887int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
10888 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +080010889{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010890 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +080010891}
10892
Jeff Mahoney499f3772015-06-15 09:41:17 -040010893/*
10894 * It used to be that old block groups would be left around forever.
10895 * Iterating over them would be enough to trim unused space. Since we
10896 * now automatically remove them, we also need to iterate over unallocated
10897 * space.
10898 *
10899 * We don't want a transaction for this since the discard may take a
10900 * substantial amount of time. We don't require that a transaction be
10901 * running, but we do need to take a running transaction into account
10902 * to ensure that we're not discarding chunks that were released in
10903 * the current transaction.
10904 *
10905 * Holding the chunks lock will prevent other threads from allocating
10906 * or releasing chunks, but it won't prevent a running transaction
10907 * from committing and releasing the memory that the pending chunks
10908 * list head uses. For that, we need to take a reference to the
10909 * transaction.
10910 */
10911static int btrfs_trim_free_extents(struct btrfs_device *device,
10912 u64 minlen, u64 *trimmed)
10913{
10914 u64 start = 0, len = 0;
10915 int ret;
10916
10917 *trimmed = 0;
10918
10919 /* Not writeable = nothing to do. */
Anand Jainebbede42017-12-04 12:54:52 +080010920 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoney499f3772015-06-15 09:41:17 -040010921 return 0;
10922
10923 /* No free space = nothing to do. */
10924 if (device->total_bytes <= device->bytes_used)
10925 return 0;
10926
10927 ret = 0;
10928
10929 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -040010930 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010931 struct btrfs_transaction *trans;
10932 u64 bytes;
10933
10934 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
10935 if (ret)
10936 return ret;
10937
10938 down_read(&fs_info->commit_root_sem);
10939
10940 spin_lock(&fs_info->trans_lock);
10941 trans = fs_info->running_transaction;
10942 if (trans)
Elena Reshetova9b64f572017-03-03 10:55:11 +020010943 refcount_inc(&trans->use_count);
Jeff Mahoney499f3772015-06-15 09:41:17 -040010944 spin_unlock(&fs_info->trans_lock);
10945
10946 ret = find_free_dev_extent_start(trans, device, minlen, start,
10947 &start, &len);
10948 if (trans)
10949 btrfs_put_transaction(trans);
10950
10951 if (ret) {
10952 up_read(&fs_info->commit_root_sem);
10953 mutex_unlock(&fs_info->chunk_mutex);
10954 if (ret == -ENOSPC)
10955 ret = 0;
10956 break;
10957 }
10958
10959 ret = btrfs_issue_discard(device->bdev, start, len, &bytes);
10960 up_read(&fs_info->commit_root_sem);
10961 mutex_unlock(&fs_info->chunk_mutex);
10962
10963 if (ret)
10964 break;
10965
10966 start += len;
10967 *trimmed += bytes;
10968
10969 if (fatal_signal_pending(current)) {
10970 ret = -ERESTARTSYS;
10971 break;
10972 }
10973
10974 cond_resched();
10975 }
10976
10977 return ret;
10978}
10979
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010980int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +000010981{
Li Dongyangf7039b12011-03-24 10:24:28 +000010982 struct btrfs_block_group_cache *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010983 struct btrfs_device *device;
10984 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +000010985 u64 group_trimmed;
10986 u64 start;
10987 u64 end;
10988 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +080010989 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +000010990 int ret = 0;
10991
Liu Bo2cac13e2012-02-09 18:17:41 +080010992 /*
10993 * try to trim all FS space, our block group may start from non-zero.
10994 */
10995 if (range->len == total_bytes)
10996 cache = btrfs_lookup_first_block_group(fs_info, range->start);
10997 else
10998 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +000010999
11000 while (cache) {
11001 if (cache->key.objectid >= (range->start + range->len)) {
11002 btrfs_put_block_group(cache);
11003 break;
11004 }
11005
11006 start = max(range->start, cache->key.objectid);
11007 end = min(range->start + range->len,
11008 cache->key.objectid + cache->key.offset);
11009
11010 if (end - start >= range->minlen) {
11011 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +000011012 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -040011013 if (ret) {
11014 btrfs_put_block_group(cache);
11015 break;
11016 }
11017 ret = wait_block_group_cache_done(cache);
11018 if (ret) {
11019 btrfs_put_block_group(cache);
11020 break;
11021 }
Li Dongyangf7039b12011-03-24 10:24:28 +000011022 }
11023 ret = btrfs_trim_block_group(cache,
11024 &group_trimmed,
11025 start,
11026 end,
11027 range->minlen);
11028
11029 trimmed += group_trimmed;
11030 if (ret) {
11031 btrfs_put_block_group(cache);
11032 break;
11033 }
11034 }
11035
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040011036 cache = next_block_group(fs_info, cache);
Li Dongyangf7039b12011-03-24 10:24:28 +000011037 }
11038
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011039 mutex_lock(&fs_info->fs_devices->device_list_mutex);
11040 devices = &fs_info->fs_devices->alloc_list;
Jeff Mahoney499f3772015-06-15 09:41:17 -040011041 list_for_each_entry(device, devices, dev_alloc_list) {
11042 ret = btrfs_trim_free_extents(device, range->minlen,
11043 &group_trimmed);
11044 if (ret)
11045 break;
11046
11047 trimmed += group_trimmed;
11048 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011049 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -040011050
Li Dongyangf7039b12011-03-24 10:24:28 +000011051 range->len = trimmed;
11052 return ret;
11053}
Miao Xie8257b2d2014-03-06 13:38:19 +080011054
11055/*
David Sterbaea14b57f2017-06-22 02:19:11 +020011056 * btrfs_{start,end}_write_no_snapshotting() are similar to
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011057 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
11058 * data into the page cache through nocow before the subvolume is snapshoted,
11059 * but flush the data into disk after the snapshot creation, or to prevent
David Sterbaea14b57f2017-06-22 02:19:11 +020011060 * operations while snapshotting is ongoing and that cause the snapshot to be
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011061 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +080011062 */
David Sterbaea14b57f2017-06-22 02:19:11 +020011063void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011064{
11065 percpu_counter_dec(&root->subv_writers->counter);
David Sterba093258e2018-02-26 16:15:17 +010011066 cond_wake_up(&root->subv_writers->wait);
Miao Xie8257b2d2014-03-06 13:38:19 +080011067}
11068
David Sterbaea14b57f2017-06-22 02:19:11 +020011069int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011070{
David Sterbaea14b57f2017-06-22 02:19:11 +020011071 if (atomic_read(&root->will_be_snapshotted))
Miao Xie8257b2d2014-03-06 13:38:19 +080011072 return 0;
11073
11074 percpu_counter_inc(&root->subv_writers->counter);
11075 /*
11076 * Make sure counter is updated before we check for snapshot creation.
11077 */
11078 smp_mb();
David Sterbaea14b57f2017-06-22 02:19:11 +020011079 if (atomic_read(&root->will_be_snapshotted)) {
11080 btrfs_end_write_no_snapshotting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +080011081 return 0;
11082 }
11083 return 1;
11084}
Zhao Lei0bc19f902016-01-06 18:56:36 +080011085
Zhao Lei0bc19f902016-01-06 18:56:36 +080011086void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
11087{
11088 while (true) {
11089 int ret;
11090
David Sterbaea14b57f2017-06-22 02:19:11 +020011091 ret = btrfs_start_write_no_snapshotting(root);
Zhao Lei0bc19f902016-01-06 18:56:36 +080011092 if (ret)
11093 break;
Peter Zijlstra46259562018-03-15 11:43:08 +010011094 wait_var_event(&root->will_be_snapshotted,
11095 !atomic_read(&root->will_be_snapshotted));
Zhao Lei0bc19f902016-01-06 18:56:36 +080011096 }
11097}