blob: aaa346562df62ded96a5539c03082afac42af735 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +010019#include <linux/sched/signal.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050020#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040021#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010022#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050023#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040024#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040025#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
David Sterbadff51cd2011-06-14 12:52:17 +020027#include <linux/ratelimit.h>
Josef Bacikb150a4f2013-06-19 15:00:04 -040028#include <linux/percpu_counter.h>
Chris Mason74493f72007-12-11 09:25:06 -050029#include "hash.h"
Miao Xie995946d2014-04-02 19:51:06 +080030#include "tree-log.h"
Chris Masonfec577f2007-02-26 10:40:21 -050031#include "disk-io.h"
32#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040033#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050034#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040035#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040036#include "free-space-cache.h"
Omar Sandoval1e144fb2015-09-29 20:50:37 -070037#include "free-space-tree.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060038#include "math.h"
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040039#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070040#include "qgroup.h"
Josef Bacikfd708b82017-09-29 15:43:50 -040041#include "ref-verify.h"
Chris Masonfec577f2007-02-26 10:40:21 -050042
Arne Jansen709c0482011-09-12 12:22:57 +020043#undef SCRAMBLE_DELAYED_REFS
44
Miao Xie9e622d62012-01-26 15:01:12 -050045/*
46 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040047 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
48 * if we really need one.
49 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040050 * CHUNK_ALLOC_LIMITED means to only try and allocate one
51 * if we have very few chunks already allocated. This is
52 * used as part of the clustering code to help make sure
53 * we have a good pool of storage to cluster in, without
54 * filling the FS with empty chunks
55 *
Miao Xie9e622d62012-01-26 15:01:12 -050056 * CHUNK_ALLOC_FORCE means it must try to allocate one
57 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040058 */
59enum {
60 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050061 CHUNK_ALLOC_LIMITED = 1,
62 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040063};
64
Yan Zheng5d4f98a2009-06-10 10:45:14 -040065static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040066 struct btrfs_fs_info *fs_info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080067 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040068 u64 root_objectid, u64 owner_objectid,
69 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080070 struct btrfs_delayed_extent_op *extra_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040071static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
72 struct extent_buffer *leaf,
73 struct btrfs_extent_item *ei);
74static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040075 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040076 u64 parent, u64 root_objectid,
77 u64 flags, u64 owner, u64 offset,
78 struct btrfs_key *ins, int ref_mod);
79static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040080 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040081 u64 parent, u64 root_objectid,
82 u64 flags, struct btrfs_disk_key *key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +010083 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050084static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040085 struct btrfs_fs_info *fs_info, u64 flags,
Josef Bacik698d0082012-09-12 14:08:47 -040086 int force);
Yan Zheng11833d62009-09-11 16:11:19 -040087static int find_next_key(struct btrfs_path *path, int level,
88 struct btrfs_key *key);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -040089static void dump_space_info(struct btrfs_fs_info *fs_info,
90 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -040091 int dump_block_groups);
Josef Bacik5d803662013-02-07 16:06:02 -050092static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
93 u64 num_bytes);
Josef Bacik957780e2016-05-17 13:30:55 -040094static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
95 struct btrfs_space_info *space_info,
96 u64 num_bytes);
97static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
98 struct btrfs_space_info *space_info,
99 u64 num_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -0500100
Josef Bacik817d52f2009-07-13 21:29:25 -0400101static noinline int
102block_group_cache_done(struct btrfs_block_group_cache *cache)
103{
104 smp_mb();
Josef Bacik36cce922013-08-05 11:15:21 -0400105 return cache->cached == BTRFS_CACHE_FINISHED ||
106 cache->cached == BTRFS_CACHE_ERROR;
Josef Bacik817d52f2009-07-13 21:29:25 -0400107}
108
Josef Bacik0f9dd462008-09-23 13:14:11 -0400109static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
110{
111 return (cache->flags & bits) == bits;
112}
113
Filipe Manana758f2df2015-11-19 11:45:48 +0000114void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000115{
116 atomic_inc(&cache->count);
117}
118
119void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
120{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400121 if (atomic_dec_and_test(&cache->count)) {
122 WARN_ON(cache->pinned > 0);
123 WARN_ON(cache->reserved > 0);
Qu Wenruo0966a7b2017-04-14 08:35:54 +0800124
125 /*
126 * If not empty, someone is still holding mutex of
127 * full_stripe_lock, which can only be released by caller.
128 * And it will definitely cause use-after-free when caller
129 * tries to release full stripe lock.
130 *
131 * No better way to resolve, but only to warn.
132 */
133 WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root));
Li Zefan34d52cb2011-03-29 13:46:06 +0800134 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000135 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400136 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000137}
138
Josef Bacik0f9dd462008-09-23 13:14:11 -0400139/*
140 * this adds the block group to the fs_info rb tree for the block group
141 * cache
142 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500143static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400144 struct btrfs_block_group_cache *block_group)
145{
146 struct rb_node **p;
147 struct rb_node *parent = NULL;
148 struct btrfs_block_group_cache *cache;
149
150 spin_lock(&info->block_group_cache_lock);
151 p = &info->block_group_cache_tree.rb_node;
152
153 while (*p) {
154 parent = *p;
155 cache = rb_entry(parent, struct btrfs_block_group_cache,
156 cache_node);
157 if (block_group->key.objectid < cache->key.objectid) {
158 p = &(*p)->rb_left;
159 } else if (block_group->key.objectid > cache->key.objectid) {
160 p = &(*p)->rb_right;
161 } else {
162 spin_unlock(&info->block_group_cache_lock);
163 return -EEXIST;
164 }
165 }
166
167 rb_link_node(&block_group->cache_node, parent, p);
168 rb_insert_color(&block_group->cache_node,
169 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000170
171 if (info->first_logical_byte > block_group->key.objectid)
172 info->first_logical_byte = block_group->key.objectid;
173
Josef Bacik0f9dd462008-09-23 13:14:11 -0400174 spin_unlock(&info->block_group_cache_lock);
175
176 return 0;
177}
178
179/*
180 * This will return the block group at or after bytenr if contains is 0, else
181 * it will return the block group that contains the bytenr
182 */
183static struct btrfs_block_group_cache *
184block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
185 int contains)
186{
187 struct btrfs_block_group_cache *cache, *ret = NULL;
188 struct rb_node *n;
189 u64 end, start;
190
191 spin_lock(&info->block_group_cache_lock);
192 n = info->block_group_cache_tree.rb_node;
193
194 while (n) {
195 cache = rb_entry(n, struct btrfs_block_group_cache,
196 cache_node);
197 end = cache->key.objectid + cache->key.offset - 1;
198 start = cache->key.objectid;
199
200 if (bytenr < start) {
201 if (!contains && (!ret || start < ret->key.objectid))
202 ret = cache;
203 n = n->rb_left;
204 } else if (bytenr > start) {
205 if (contains && bytenr <= end) {
206 ret = cache;
207 break;
208 }
209 n = n->rb_right;
210 } else {
211 ret = cache;
212 break;
213 }
214 }
Liu Boa1897fd2012-12-27 09:01:23 +0000215 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000216 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000217 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
218 info->first_logical_byte = ret->key.objectid;
219 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400220 spin_unlock(&info->block_group_cache_lock);
221
222 return ret;
223}
224
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400225static int add_excluded_extent(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400226 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400227{
Yan Zheng11833d62009-09-11 16:11:19 -0400228 u64 end = start + num_bytes - 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400229 set_extent_bits(&fs_info->freed_extents[0],
David Sterbaceeb0ae2016-04-26 23:54:39 +0200230 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400231 set_extent_bits(&fs_info->freed_extents[1],
David Sterbaceeb0ae2016-04-26 23:54:39 +0200232 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -0400233 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400234}
235
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400236static void free_excluded_extents(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400237 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400238{
Yan Zheng11833d62009-09-11 16:11:19 -0400239 u64 start, end;
240
241 start = cache->key.objectid;
242 end = start + cache->key.offset - 1;
243
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400244 clear_extent_bits(&fs_info->freed_extents[0],
David Sterba91166212016-04-26 23:54:39 +0200245 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400246 clear_extent_bits(&fs_info->freed_extents[1],
David Sterba91166212016-04-26 23:54:39 +0200247 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -0400248}
249
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400250static int exclude_super_stripes(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400251 struct btrfs_block_group_cache *cache)
252{
Josef Bacik817d52f2009-07-13 21:29:25 -0400253 u64 bytenr;
254 u64 *logical;
255 int stripe_len;
256 int i, nr, ret;
257
Yan, Zheng06b23312009-11-26 09:31:11 +0000258 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
259 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
260 cache->bytes_super += stripe_len;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400261 ret = add_excluded_extent(fs_info, cache->key.objectid,
Yan, Zheng06b23312009-11-26 09:31:11 +0000262 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400263 if (ret)
264 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000265 }
266
Josef Bacik817d52f2009-07-13 21:29:25 -0400267 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
268 bytenr = btrfs_sb_offset(i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400269 ret = btrfs_rmap_block(fs_info, cache->key.objectid,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -0400270 bytenr, 0, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400271 if (ret)
272 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400273
Josef Bacik817d52f2009-07-13 21:29:25 -0400274 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400275 u64 start, len;
276
277 if (logical[nr] > cache->key.objectid +
278 cache->key.offset)
279 continue;
280
281 if (logical[nr] + stripe_len <= cache->key.objectid)
282 continue;
283
284 start = logical[nr];
285 if (start < cache->key.objectid) {
286 start = cache->key.objectid;
287 len = (logical[nr] + stripe_len) - start;
288 } else {
289 len = min_t(u64, stripe_len,
290 cache->key.objectid +
291 cache->key.offset - start);
292 }
293
294 cache->bytes_super += len;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400295 ret = add_excluded_extent(fs_info, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400296 if (ret) {
297 kfree(logical);
298 return ret;
299 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400300 }
Yan Zheng11833d62009-09-11 16:11:19 -0400301
Josef Bacik817d52f2009-07-13 21:29:25 -0400302 kfree(logical);
303 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400304 return 0;
305}
306
Yan Zheng11833d62009-09-11 16:11:19 -0400307static struct btrfs_caching_control *
308get_caching_control(struct btrfs_block_group_cache *cache)
309{
310 struct btrfs_caching_control *ctl;
311
312 spin_lock(&cache->lock);
Josef Bacikdde5abe2010-09-16 16:17:03 -0400313 if (!cache->caching_ctl) {
314 spin_unlock(&cache->lock);
315 return NULL;
316 }
317
Yan Zheng11833d62009-09-11 16:11:19 -0400318 ctl = cache->caching_ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200319 refcount_inc(&ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400320 spin_unlock(&cache->lock);
321 return ctl;
322}
323
324static void put_caching_control(struct btrfs_caching_control *ctl)
325{
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200326 if (refcount_dec_and_test(&ctl->count))
Yan Zheng11833d62009-09-11 16:11:19 -0400327 kfree(ctl);
328}
329
Josef Bacikd0bd4562015-09-23 14:54:14 -0400330#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400331static void fragment_free_space(struct btrfs_block_group_cache *block_group)
Josef Bacikd0bd4562015-09-23 14:54:14 -0400332{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400333 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400334 u64 start = block_group->key.objectid;
335 u64 len = block_group->key.offset;
336 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400337 fs_info->nodesize : fs_info->sectorsize;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400338 u64 step = chunk << 1;
339
340 while (len > chunk) {
341 btrfs_remove_free_space(block_group, start, chunk);
342 start += step;
343 if (len < step)
344 len = 0;
345 else
346 len -= step;
347 }
348}
349#endif
350
Josef Bacik0f9dd462008-09-23 13:14:11 -0400351/*
352 * this is only called by cache_block_group, since we could have freed extents
353 * we need to check the pinned_extents for any extents that can't be used yet
354 * since their free space will be released as soon as the transaction commits.
355 */
Omar Sandovala5ed9182015-09-29 20:50:35 -0700356u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
357 struct btrfs_fs_info *info, u64 start, u64 end)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400358{
Josef Bacik817d52f2009-07-13 21:29:25 -0400359 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400360 int ret;
361
362 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400363 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400364 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400365 EXTENT_DIRTY | EXTENT_UPTODATE,
366 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400367 if (ret)
368 break;
369
Yan, Zheng06b23312009-11-26 09:31:11 +0000370 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400371 start = extent_end + 1;
372 } else if (extent_start > start && extent_start < end) {
373 size = extent_start - 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,
376 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100377 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400378 start = extent_end + 1;
379 } else {
380 break;
381 }
382 }
383
384 if (start < end) {
385 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400386 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500387 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100388 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400389 }
390
Josef Bacik817d52f2009-07-13 21:29:25 -0400391 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400392}
393
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700394static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
Chris Masone37c9e62007-05-09 20:13:14 -0400395{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400396 struct btrfs_block_group_cache *block_group = caching_ctl->block_group;
397 struct btrfs_fs_info *fs_info = block_group->fs_info;
398 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400399 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400400 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400401 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400402 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400403 u64 last = 0;
404 u32 nritems;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700405 int ret;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400406 bool wakeup = true;
Chris Masonf510cfe2007-10-15 16:14:48 -0400407
Chris Masone37c9e62007-05-09 20:13:14 -0400408 path = btrfs_alloc_path();
409 if (!path)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700410 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400411
Josef Bacik817d52f2009-07-13 21:29:25 -0400412 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400413
Josef Bacikd0bd4562015-09-23 14:54:14 -0400414#ifdef CONFIG_BTRFS_DEBUG
415 /*
416 * If we're fragmenting we don't want to make anybody think we can
417 * allocate from this block group until we've had a chance to fragment
418 * the free space.
419 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400420 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -0400421 wakeup = false;
422#endif
Chris Mason5cd57b22008-06-25 16:01:30 -0400423 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400424 * We don't want to deadlock with somebody trying to allocate a new
425 * extent for the extent root while also trying to search the extent
426 * root to add free space. So we skip locking and search the commit
427 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400428 */
429 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400430 path->search_commit_root = 1;
David Sterbae4058b52015-11-27 16:31:35 +0100431 path->reada = READA_FORWARD;
Josef Bacik817d52f2009-07-13 21:29:25 -0400432
Yan Zhenge4404d62008-12-12 10:03:26 -0500433 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400434 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400435 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400436
Liu Bo52ee28d2013-07-11 17:51:15 +0800437next:
Yan Zheng11833d62009-09-11 16:11:19 -0400438 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400439 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700440 goto out;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500441
Yan Zheng11833d62009-09-11 16:11:19 -0400442 leaf = path->nodes[0];
443 nritems = btrfs_header_nritems(leaf);
444
Chris Masond3977122009-01-05 21:25:51 -0500445 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200446 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400447 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400448 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400449 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400450
Yan Zheng11833d62009-09-11 16:11:19 -0400451 if (path->slots[0] < nritems) {
452 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
453 } else {
454 ret = find_next_key(path, 0, &key);
455 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400456 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400457
Josef Bacikc9ea7b22013-09-19 10:02:11 -0400458 if (need_resched() ||
Josef Bacik9e351cc2014-03-13 15:42:13 -0400459 rwsem_is_contended(&fs_info->commit_root_sem)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400460 if (wakeup)
461 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400462 btrfs_release_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400463 up_read(&fs_info->commit_root_sem);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400464 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400465 cond_resched();
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700466 mutex_lock(&caching_ctl->mutex);
467 down_read(&fs_info->commit_root_sem);
468 goto next;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400469 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400470
471 ret = btrfs_next_leaf(extent_root, path);
472 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700473 goto out;
Josef Bacik0a3896d2013-04-19 14:37:26 -0400474 if (ret)
475 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400476 leaf = path->nodes[0];
477 nritems = btrfs_header_nritems(leaf);
478 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400479 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400480
Liu Bo52ee28d2013-07-11 17:51:15 +0800481 if (key.objectid < last) {
482 key.objectid = last;
483 key.offset = 0;
484 key.type = BTRFS_EXTENT_ITEM_KEY;
485
Josef Bacikd0bd4562015-09-23 14:54:14 -0400486 if (wakeup)
487 caching_ctl->progress = last;
Liu Bo52ee28d2013-07-11 17:51:15 +0800488 btrfs_release_path(path);
489 goto next;
490 }
491
Yan Zheng11833d62009-09-11 16:11:19 -0400492 if (key.objectid < block_group->key.objectid) {
493 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400494 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400495 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400496
Chris Masone37c9e62007-05-09 20:13:14 -0400497 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400498 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400499 break;
Yan7d7d6062007-09-14 16:15:28 -0400500
Josef Bacik3173a182013-03-07 14:22:04 -0500501 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
502 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400503 total_found += add_new_free_space(block_group,
504 fs_info, last,
505 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500506 if (key.type == BTRFS_METADATA_ITEM_KEY)
507 last = key.objectid +
Jeff Mahoneyda170662016-06-15 09:22:56 -0400508 fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500509 else
510 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400511
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700512 if (total_found > CACHING_CTL_WAKE_UP) {
Yan Zheng11833d62009-09-11 16:11:19 -0400513 total_found = 0;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400514 if (wakeup)
515 wake_up(&caching_ctl->wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400516 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400517 }
Chris Masone37c9e62007-05-09 20:13:14 -0400518 path->slots[0]++;
519 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400520 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400521
522 total_found += add_new_free_space(block_group, fs_info, last,
523 block_group->key.objectid +
524 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400525 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400526
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700527out:
528 btrfs_free_path(path);
529 return ret;
530}
531
532static noinline void caching_thread(struct btrfs_work *work)
533{
534 struct btrfs_block_group_cache *block_group;
535 struct btrfs_fs_info *fs_info;
536 struct btrfs_caching_control *caching_ctl;
Chris Masonb4570aa2015-12-30 07:37:26 -0800537 struct btrfs_root *extent_root;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700538 int ret;
539
540 caching_ctl = container_of(work, struct btrfs_caching_control, work);
541 block_group = caching_ctl->block_group;
542 fs_info = block_group->fs_info;
Chris Masonb4570aa2015-12-30 07:37:26 -0800543 extent_root = fs_info->extent_root;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700544
545 mutex_lock(&caching_ctl->mutex);
546 down_read(&fs_info->commit_root_sem);
547
Omar Sandoval1e144fb2015-09-29 20:50:37 -0700548 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
549 ret = load_free_space_tree(caching_ctl);
550 else
551 ret = load_extent_tree_free(caching_ctl);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700552
Josef Bacik817d52f2009-07-13 21:29:25 -0400553 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400554 block_group->caching_ctl = NULL;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700555 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
Josef Bacik817d52f2009-07-13 21:29:25 -0400556 spin_unlock(&block_group->lock);
557
Josef Bacikd0bd4562015-09-23 14:54:14 -0400558#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400559 if (btrfs_should_fragment_free_space(block_group)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400560 u64 bytes_used;
561
562 spin_lock(&block_group->space_info->lock);
563 spin_lock(&block_group->lock);
564 bytes_used = block_group->key.offset -
565 btrfs_block_group_used(&block_group->item);
566 block_group->space_info->bytes_used += bytes_used >> 1;
567 spin_unlock(&block_group->lock);
568 spin_unlock(&block_group->space_info->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400569 fragment_free_space(block_group);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400570 }
571#endif
572
573 caching_ctl->progress = (u64)-1;
Chris Masonf7d3d2f2015-12-18 11:11:10 -0800574
Josef Bacik9e351cc2014-03-13 15:42:13 -0400575 up_read(&fs_info->commit_root_sem);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400576 free_excluded_extents(fs_info, block_group);
Yan Zheng11833d62009-09-11 16:11:19 -0400577 mutex_unlock(&caching_ctl->mutex);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700578
Yan Zheng11833d62009-09-11 16:11:19 -0400579 wake_up(&caching_ctl->wait);
580
581 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000582 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400583}
584
Josef Bacik9d66e232010-08-25 16:54:15 -0400585static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400586 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400587{
Josef Bacik291c7d22011-11-14 13:52:14 -0500588 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400589 struct btrfs_fs_info *fs_info = cache->fs_info;
590 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400591 int ret = 0;
592
Josef Bacik291c7d22011-11-14 13:52:14 -0500593 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100594 if (!caching_ctl)
595 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500596
597 INIT_LIST_HEAD(&caching_ctl->list);
598 mutex_init(&caching_ctl->mutex);
599 init_waitqueue_head(&caching_ctl->wait);
600 caching_ctl->block_group = cache;
601 caching_ctl->progress = cache->key.objectid;
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200602 refcount_set(&caching_ctl->count, 1);
Liu Bo9e0af232014-08-15 23:36:53 +0800603 btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
604 caching_thread, NULL, NULL);
Josef Bacik291c7d22011-11-14 13:52:14 -0500605
606 spin_lock(&cache->lock);
607 /*
608 * This should be a rare occasion, but this could happen I think in the
609 * case where one thread starts to load the space cache info, and then
610 * some other thread starts a transaction commit which tries to do an
611 * allocation while the other thread is still loading the space cache
612 * info. The previous loop should have kept us from choosing this block
613 * group, but if we've moved to the state where we will wait on caching
614 * block groups we need to first check if we're doing a fast load here,
615 * so we can wait for it to finish, otherwise we could end up allocating
616 * from a block group who's cache gets evicted for one reason or
617 * another.
618 */
619 while (cache->cached == BTRFS_CACHE_FAST) {
620 struct btrfs_caching_control *ctl;
621
622 ctl = cache->caching_ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200623 refcount_inc(&ctl->count);
Josef Bacik291c7d22011-11-14 13:52:14 -0500624 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
625 spin_unlock(&cache->lock);
626
627 schedule();
628
629 finish_wait(&ctl->wait, &wait);
630 put_caching_control(ctl);
631 spin_lock(&cache->lock);
632 }
633
634 if (cache->cached != BTRFS_CACHE_NO) {
635 spin_unlock(&cache->lock);
636 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400637 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500638 }
639 WARN_ON(cache->caching_ctl);
640 cache->caching_ctl = caching_ctl;
641 cache->cached = BTRFS_CACHE_FAST;
642 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400643
Satoru Takeuchid8953d62017-09-12 20:08:08 +0900644 if (btrfs_test_opt(fs_info, SPACE_CACHE)) {
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500645 mutex_lock(&caching_ctl->mutex);
Josef Bacik9d66e232010-08-25 16:54:15 -0400646 ret = load_free_space_cache(fs_info, cache);
647
648 spin_lock(&cache->lock);
649 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500650 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400651 cache->cached = BTRFS_CACHE_FINISHED;
652 cache->last_byte_to_unpin = (u64)-1;
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500653 caching_ctl->progress = (u64)-1;
Josef Bacik9d66e232010-08-25 16:54:15 -0400654 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500655 if (load_cache_only) {
656 cache->caching_ctl = NULL;
657 cache->cached = BTRFS_CACHE_NO;
658 } else {
659 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000660 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500661 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400662 }
663 spin_unlock(&cache->lock);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400664#ifdef CONFIG_BTRFS_DEBUG
665 if (ret == 1 &&
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400666 btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400667 u64 bytes_used;
668
669 spin_lock(&cache->space_info->lock);
670 spin_lock(&cache->lock);
671 bytes_used = cache->key.offset -
672 btrfs_block_group_used(&cache->item);
673 cache->space_info->bytes_used += bytes_used >> 1;
674 spin_unlock(&cache->lock);
675 spin_unlock(&cache->space_info->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400676 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400677 }
678#endif
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500679 mutex_unlock(&caching_ctl->mutex);
680
Josef Bacik291c7d22011-11-14 13:52:14 -0500681 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000682 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500683 put_caching_control(caching_ctl);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400684 free_excluded_extents(fs_info, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400685 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000686 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500687 } else {
688 /*
Omar Sandoval1e144fb2015-09-29 20:50:37 -0700689 * We're either using the free space tree or no caching at all.
690 * Set cached to the appropriate value and wakeup any waiters.
Josef Bacik291c7d22011-11-14 13:52:14 -0500691 */
692 spin_lock(&cache->lock);
693 if (load_cache_only) {
694 cache->caching_ctl = NULL;
695 cache->cached = BTRFS_CACHE_NO;
696 } else {
697 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000698 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500699 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400700 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500701 wake_up(&caching_ctl->wait);
702 }
703
704 if (load_cache_only) {
705 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400706 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400707 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400708
Josef Bacik9e351cc2014-03-13 15:42:13 -0400709 down_write(&fs_info->commit_root_sem);
Elena Reshetova1e4f4712017-03-03 10:55:14 +0200710 refcount_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400711 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400712 up_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -0400713
Josef Bacik11dfe352009-11-13 20:12:59 +0000714 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400715
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800716 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400717
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400718 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400719}
720
Josef Bacik0f9dd462008-09-23 13:14:11 -0400721/*
722 * return the block group that starts at or after bytenr
723 */
Chris Masond3977122009-01-05 21:25:51 -0500724static struct btrfs_block_group_cache *
725btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400726{
Masahiro Yamadae2c89902016-09-13 04:35:52 +0900727 return block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400728}
729
Josef Bacik0f9dd462008-09-23 13:14:11 -0400730/*
Sankar P9f556842009-05-14 13:52:22 -0400731 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400732 */
Chris Masond3977122009-01-05 21:25:51 -0500733struct btrfs_block_group_cache *btrfs_lookup_block_group(
734 struct btrfs_fs_info *info,
735 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400736{
Masahiro Yamadae2c89902016-09-13 04:35:52 +0900737 return block_group_cache_tree_search(info, bytenr, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400738}
Chris Mason0b86a832008-03-24 15:01:56 -0400739
Josef Bacik0f9dd462008-09-23 13:14:11 -0400740static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
741 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400742{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400743 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400744 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400745
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200746 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400747
Chris Mason4184ea72009-03-10 12:39:20 -0400748 rcu_read_lock();
749 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400750 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400751 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400752 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400753 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400754 }
Chris Mason4184ea72009-03-10 12:39:20 -0400755 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400756 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400757}
758
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700759static void add_pinned_bytes(struct btrfs_fs_info *fs_info, s64 num_bytes,
760 u64 owner, u64 root_objectid)
761{
762 struct btrfs_space_info *space_info;
763 u64 flags;
764
765 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
766 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
767 flags = BTRFS_BLOCK_GROUP_SYSTEM;
768 else
769 flags = BTRFS_BLOCK_GROUP_METADATA;
770 } else {
771 flags = BTRFS_BLOCK_GROUP_DATA;
772 }
773
774 space_info = __find_space_info(fs_info, flags);
Omar Sandoval55e81962017-06-06 16:45:27 -0700775 ASSERT(space_info);
Omar Sandoval0d9f8242017-06-06 16:45:26 -0700776 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
777}
778
Chris Mason4184ea72009-03-10 12:39:20 -0400779/*
780 * after adding space to the filesystem, we need to clear the full flags
781 * on all the space infos.
782 */
783void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
784{
785 struct list_head *head = &info->space_info;
786 struct btrfs_space_info *found;
787
788 rcu_read_lock();
789 list_for_each_entry_rcu(found, head, list)
790 found->full = 0;
791 rcu_read_unlock();
792}
793
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000794/* simple helper to search for an existing data extent at a given offset */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400795int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400796{
797 int ret;
798 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400799 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400800
Zheng Yan31840ae2008-09-23 13:14:14 -0400801 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700802 if (!path)
803 return -ENOMEM;
804
Chris Masone02119d2008-09-05 16:13:11 -0400805 key.objectid = start;
806 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500807 key.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400808 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400809 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500810 return ret;
811}
812
Chris Masond8d5f3e2007-12-11 12:42:00 -0500813/*
Josef Bacik3173a182013-03-07 14:22:04 -0500814 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400815 *
816 * the head node for delayed ref is used to store the sum of all the
817 * reference count modifications queued up in the rbtree. the head
818 * node may also store the extent flags to set. This way you can check
819 * to see what the reference count and extent flags would be if all of
820 * the delayed refs are not processed.
821 */
822int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400823 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500824 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400825{
826 struct btrfs_delayed_ref_head *head;
827 struct btrfs_delayed_ref_root *delayed_refs;
828 struct btrfs_path *path;
829 struct btrfs_extent_item *ei;
830 struct extent_buffer *leaf;
831 struct btrfs_key key;
832 u32 item_size;
833 u64 num_refs;
834 u64 extent_flags;
835 int ret;
836
Josef Bacik3173a182013-03-07 14:22:04 -0500837 /*
838 * If we don't have skinny metadata, don't bother doing anything
839 * different
840 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400841 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
842 offset = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500843 metadata = 0;
844 }
845
Yan, Zhenga22285a2010-05-16 10:48:46 -0400846 path = btrfs_alloc_path();
847 if (!path)
848 return -ENOMEM;
849
Yan, Zhenga22285a2010-05-16 10:48:46 -0400850 if (!trans) {
851 path->skip_locking = 1;
852 path->search_commit_root = 1;
853 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000854
855search_again:
856 key.objectid = bytenr;
857 key.offset = offset;
858 if (metadata)
859 key.type = BTRFS_METADATA_ITEM_KEY;
860 else
861 key.type = BTRFS_EXTENT_ITEM_KEY;
862
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400863 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400864 if (ret < 0)
865 goto out_free;
866
Josef Bacik3173a182013-03-07 14:22:04 -0500867 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100868 if (path->slots[0]) {
869 path->slots[0]--;
870 btrfs_item_key_to_cpu(path->nodes[0], &key,
871 path->slots[0]);
872 if (key.objectid == bytenr &&
873 key.type == BTRFS_EXTENT_ITEM_KEY &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400874 key.offset == fs_info->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100875 ret = 0;
876 }
Josef Bacik3173a182013-03-07 14:22:04 -0500877 }
878
Yan, Zhenga22285a2010-05-16 10:48:46 -0400879 if (ret == 0) {
880 leaf = path->nodes[0];
881 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
882 if (item_size >= sizeof(*ei)) {
883 ei = btrfs_item_ptr(leaf, path->slots[0],
884 struct btrfs_extent_item);
885 num_refs = btrfs_extent_refs(leaf, ei);
886 extent_flags = btrfs_extent_flags(leaf, ei);
887 } else {
888#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
889 struct btrfs_extent_item_v0 *ei0;
890 BUG_ON(item_size != sizeof(*ei0));
891 ei0 = btrfs_item_ptr(leaf, path->slots[0],
892 struct btrfs_extent_item_v0);
893 num_refs = btrfs_extent_refs_v0(leaf, ei0);
894 /* FIXME: this isn't correct for data */
895 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
896#else
897 BUG();
898#endif
899 }
900 BUG_ON(num_refs == 0);
901 } else {
902 num_refs = 0;
903 extent_flags = 0;
904 ret = 0;
905 }
906
907 if (!trans)
908 goto out;
909
910 delayed_refs = &trans->transaction->delayed_refs;
911 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -0800912 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400913 if (head) {
914 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -0400915 refcount_inc(&head->refs);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400916 spin_unlock(&delayed_refs->lock);
917
David Sterbab3b4aa72011-04-21 01:20:15 +0200918 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400919
David Sterba8cc33e52011-05-02 15:29:25 +0200920 /*
921 * Mutex was contended, block until it's released and try
922 * again
923 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400924 mutex_lock(&head->mutex);
925 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -0400926 btrfs_put_delayed_ref_head(head);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000927 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400928 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500929 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400930 if (head->extent_op && head->extent_op->update_flags)
931 extent_flags |= head->extent_op->flags_to_set;
932 else
933 BUG_ON(num_refs == 0);
934
Josef Bacikd2788502017-09-29 15:43:57 -0400935 num_refs += head->ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500936 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400937 mutex_unlock(&head->mutex);
938 }
939 spin_unlock(&delayed_refs->lock);
940out:
941 WARN_ON(num_refs == 0);
942 if (refs)
943 *refs = num_refs;
944 if (flags)
945 *flags = extent_flags;
946out_free:
947 btrfs_free_path(path);
948 return ret;
949}
950
951/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500952 * Back reference rules. Back refs have three main goals:
953 *
954 * 1) differentiate between all holders of references to an extent so that
955 * when a reference is dropped we can make sure it was a valid reference
956 * before freeing the extent.
957 *
958 * 2) Provide enough information to quickly find the holders of an extent
959 * if we notice a given block is corrupted or bad.
960 *
961 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
962 * maintenance. This is actually the same as #2, but with a slightly
963 * different use case.
964 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400965 * There are two kinds of back refs. The implicit back refs is optimized
966 * for pointers in non-shared tree blocks. For a given pointer in a block,
967 * back refs of this kind provide information about the block's owner tree
968 * and the pointer's key. These information allow us to find the block by
969 * b-tree searching. The full back refs is for pointers in tree blocks not
970 * referenced by their owner trees. The location of tree block is recorded
971 * in the back refs. Actually the full back refs is generic, and can be
972 * used in all cases the implicit back refs is used. The major shortcoming
973 * of the full back refs is its overhead. Every time a tree block gets
974 * COWed, we have to update back refs entry for all pointers in it.
975 *
976 * For a newly allocated tree block, we use implicit back refs for
977 * pointers in it. This means most tree related operations only involve
978 * implicit back refs. For a tree block created in old transaction, the
979 * only way to drop a reference to it is COW it. So we can detect the
980 * event that tree block loses its owner tree's reference and do the
981 * back refs conversion.
982 *
Nicholas D Steeves01327612016-05-19 21:18:45 -0400983 * When a tree block is COWed through a tree, there are four cases:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400984 *
985 * The reference count of the block is one and the tree is the block's
986 * owner tree. Nothing to do in this case.
987 *
988 * The reference count of the block is one and the tree is not the
989 * block's owner tree. In this case, full back refs is used for pointers
990 * in the block. Remove these full back refs, add implicit back refs for
991 * every pointers in the new block.
992 *
993 * The reference count of the block is greater than one and the tree is
994 * the block's owner tree. In this case, implicit back refs is used for
995 * pointers in the block. Add full back refs for every pointers in the
996 * block, increase lower level extents' reference counts. The original
997 * implicit back refs are entailed to the new block.
998 *
999 * The reference count of the block is greater than one and the tree is
1000 * not the block's owner tree. Add implicit back refs for every pointer in
1001 * the new block, increase lower level extents' reference count.
1002 *
1003 * Back Reference Key composing:
1004 *
1005 * The key objectid corresponds to the first byte in the extent,
1006 * The key type is used to differentiate between types of back refs.
1007 * There are different meanings of the key offset for different types
1008 * of back refs.
1009 *
Chris Masond8d5f3e2007-12-11 12:42:00 -05001010 * File extents can be referenced by:
1011 *
1012 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -04001013 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -05001014 * - different offsets inside a file (bookend extents in file.c)
1015 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001016 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001017 *
1018 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -05001019 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001020 * - original offset in the file
1021 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -04001022 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001023 * The key offset for the implicit back refs is hash of the first
1024 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001025 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001026 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001027 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001028 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001029 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001030 * The key offset for the implicit back refs is the first byte of
1031 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001032 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001033 * When a file extent is allocated, The implicit back refs is used.
1034 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001035 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001036 * (root_key.objectid, inode objectid, offset in file, 1)
1037 *
1038 * When a file extent is removed file truncation, we find the
1039 * corresponding implicit back refs and check the following fields:
1040 *
1041 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -05001042 *
1043 * Btree extents can be referenced by:
1044 *
1045 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -05001046 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001047 * Both the implicit back refs and the full back refs for tree blocks
1048 * only consist of key. The key offset for the implicit back refs is
1049 * objectid of block's owner tree. The key offset for the full back refs
1050 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001051 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001052 * When implicit back refs is used, information about the lowest key and
1053 * level of the tree block are required. These information are stored in
1054 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001055 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001056
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001057#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1058static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001059 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001060 struct btrfs_path *path,
1061 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -05001062{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001063 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001064 struct btrfs_extent_item *item;
1065 struct btrfs_extent_item_v0 *ei0;
1066 struct btrfs_extent_ref_v0 *ref0;
1067 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -04001068 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001069 struct btrfs_key key;
1070 struct btrfs_key found_key;
1071 u32 new_size = sizeof(*item);
1072 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -05001073 int ret;
1074
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001075 leaf = path->nodes[0];
1076 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -05001077
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001078 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1079 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1080 struct btrfs_extent_item_v0);
1081 refs = btrfs_extent_refs_v0(leaf, ei0);
1082
1083 if (owner == (u64)-1) {
1084 while (1) {
1085 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1086 ret = btrfs_next_leaf(root, path);
1087 if (ret < 0)
1088 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001089 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001090 leaf = path->nodes[0];
1091 }
1092 btrfs_item_key_to_cpu(leaf, &found_key,
1093 path->slots[0]);
1094 BUG_ON(key.objectid != found_key.objectid);
1095 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1096 path->slots[0]++;
1097 continue;
1098 }
1099 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1100 struct btrfs_extent_ref_v0);
1101 owner = btrfs_ref_objectid_v0(leaf, ref0);
1102 break;
1103 }
1104 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001105 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001106
1107 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1108 new_size += sizeof(*bi);
1109
1110 new_size -= sizeof(*ei0);
1111 ret = btrfs_search_slot(trans, root, &key, path,
1112 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001113 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001114 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001115 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001116
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001117 btrfs_extend_item(fs_info, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001118
1119 leaf = path->nodes[0];
1120 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1121 btrfs_set_extent_refs(leaf, item, refs);
1122 /* FIXME: get real generation */
1123 btrfs_set_extent_generation(leaf, item, 0);
1124 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1125 btrfs_set_extent_flags(leaf, item,
1126 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1127 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1128 bi = (struct btrfs_tree_block_info *)(item + 1);
1129 /* FIXME: get first key of the block */
David Sterbab159fa22016-11-08 18:09:03 +01001130 memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001131 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1132 } else {
1133 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1134 }
1135 btrfs_mark_buffer_dirty(leaf);
1136 return 0;
1137}
1138#endif
1139
Liu Bo167ce952017-08-18 15:15:18 -06001140/*
1141 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
1142 * is_data == BTRFS_REF_TYPE_DATA, data type is requried,
1143 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
1144 */
1145int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
1146 struct btrfs_extent_inline_ref *iref,
1147 enum btrfs_inline_ref_type is_data)
1148{
1149 int type = btrfs_extent_inline_ref_type(eb, iref);
Liu Bo64ecdb62017-08-18 15:15:24 -06001150 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
Liu Bo167ce952017-08-18 15:15:18 -06001151
1152 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1153 type == BTRFS_SHARED_BLOCK_REF_KEY ||
1154 type == BTRFS_SHARED_DATA_REF_KEY ||
1155 type == BTRFS_EXTENT_DATA_REF_KEY) {
1156 if (is_data == BTRFS_REF_TYPE_BLOCK) {
Liu Bo64ecdb62017-08-18 15:15:24 -06001157 if (type == BTRFS_TREE_BLOCK_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -06001158 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -06001159 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1160 ASSERT(eb->fs_info);
1161 /*
1162 * Every shared one has parent tree
1163 * block, which must be aligned to
1164 * nodesize.
1165 */
1166 if (offset &&
1167 IS_ALIGNED(offset, eb->fs_info->nodesize))
1168 return type;
1169 }
Liu Bo167ce952017-08-18 15:15:18 -06001170 } else if (is_data == BTRFS_REF_TYPE_DATA) {
Liu Bo64ecdb62017-08-18 15:15:24 -06001171 if (type == BTRFS_EXTENT_DATA_REF_KEY)
Liu Bo167ce952017-08-18 15:15:18 -06001172 return type;
Liu Bo64ecdb62017-08-18 15:15:24 -06001173 if (type == BTRFS_SHARED_DATA_REF_KEY) {
1174 ASSERT(eb->fs_info);
1175 /*
1176 * Every shared one has parent tree
1177 * block, which must be aligned to
1178 * nodesize.
1179 */
1180 if (offset &&
1181 IS_ALIGNED(offset, eb->fs_info->nodesize))
1182 return type;
1183 }
Liu Bo167ce952017-08-18 15:15:18 -06001184 } else {
1185 ASSERT(is_data == BTRFS_REF_TYPE_ANY);
1186 return type;
1187 }
1188 }
1189
1190 btrfs_print_leaf((struct extent_buffer *)eb);
1191 btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d",
1192 eb->start, type);
1193 WARN_ON(1);
1194
1195 return BTRFS_REF_TYPE_INVALID;
1196}
1197
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001198static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1199{
1200 u32 high_crc = ~(u32)0;
1201 u32 low_crc = ~(u32)0;
1202 __le64 lenum;
1203
1204 lenum = cpu_to_le64(root_objectid);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001205 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001206 lenum = cpu_to_le64(owner);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001207 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001208 lenum = cpu_to_le64(offset);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001209 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001210
1211 return ((u64)high_crc << 31) ^ (u64)low_crc;
1212}
1213
1214static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1215 struct btrfs_extent_data_ref *ref)
1216{
1217 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1218 btrfs_extent_data_ref_objectid(leaf, ref),
1219 btrfs_extent_data_ref_offset(leaf, ref));
1220}
1221
1222static int match_extent_data_ref(struct extent_buffer *leaf,
1223 struct btrfs_extent_data_ref *ref,
1224 u64 root_objectid, u64 owner, u64 offset)
1225{
1226 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1227 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1228 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1229 return 0;
1230 return 1;
1231}
1232
1233static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001234 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001235 struct btrfs_path *path,
1236 u64 bytenr, u64 parent,
1237 u64 root_objectid,
1238 u64 owner, u64 offset)
1239{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001240 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001241 struct btrfs_key key;
1242 struct btrfs_extent_data_ref *ref;
1243 struct extent_buffer *leaf;
1244 u32 nritems;
1245 int ret;
1246 int recow;
1247 int err = -ENOENT;
1248
1249 key.objectid = bytenr;
1250 if (parent) {
1251 key.type = BTRFS_SHARED_DATA_REF_KEY;
1252 key.offset = parent;
1253 } else {
1254 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1255 key.offset = hash_extent_data_ref(root_objectid,
1256 owner, offset);
1257 }
1258again:
1259 recow = 0;
1260 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1261 if (ret < 0) {
1262 err = ret;
1263 goto fail;
1264 }
1265
1266 if (parent) {
1267 if (!ret)
1268 return 0;
1269#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1270 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001271 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001272 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1273 if (ret < 0) {
1274 err = ret;
1275 goto fail;
1276 }
1277 if (!ret)
1278 return 0;
1279#endif
1280 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001281 }
1282
1283 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001284 nritems = btrfs_header_nritems(leaf);
1285 while (1) {
1286 if (path->slots[0] >= nritems) {
1287 ret = btrfs_next_leaf(root, path);
1288 if (ret < 0)
1289 err = ret;
1290 if (ret)
1291 goto fail;
1292
1293 leaf = path->nodes[0];
1294 nritems = btrfs_header_nritems(leaf);
1295 recow = 1;
1296 }
1297
1298 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1299 if (key.objectid != bytenr ||
1300 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1301 goto fail;
1302
1303 ref = btrfs_item_ptr(leaf, path->slots[0],
1304 struct btrfs_extent_data_ref);
1305
1306 if (match_extent_data_ref(leaf, ref, root_objectid,
1307 owner, offset)) {
1308 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001309 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001310 goto again;
1311 }
1312 err = 0;
1313 break;
1314 }
1315 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001316 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001317fail:
1318 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001319}
1320
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001321static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001322 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001323 struct btrfs_path *path,
1324 u64 bytenr, u64 parent,
1325 u64 root_objectid, u64 owner,
1326 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001327{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001328 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04001329 struct btrfs_key key;
1330 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001331 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001332 u32 num_refs;
1333 int ret;
1334
1335 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001336 if (parent) {
1337 key.type = BTRFS_SHARED_DATA_REF_KEY;
1338 key.offset = parent;
1339 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001340 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001341 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1342 key.offset = hash_extent_data_ref(root_objectid,
1343 owner, offset);
1344 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001345 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001346
1347 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1348 if (ret && ret != -EEXIST)
1349 goto fail;
1350
1351 leaf = path->nodes[0];
1352 if (parent) {
1353 struct btrfs_shared_data_ref *ref;
1354 ref = btrfs_item_ptr(leaf, path->slots[0],
1355 struct btrfs_shared_data_ref);
1356 if (ret == 0) {
1357 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1358 } else {
1359 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1360 num_refs += refs_to_add;
1361 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1362 }
1363 } else {
1364 struct btrfs_extent_data_ref *ref;
1365 while (ret == -EEXIST) {
1366 ref = btrfs_item_ptr(leaf, path->slots[0],
1367 struct btrfs_extent_data_ref);
1368 if (match_extent_data_ref(leaf, ref, root_objectid,
1369 owner, offset))
1370 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001371 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001372 key.offset++;
1373 ret = btrfs_insert_empty_item(trans, root, path, &key,
1374 size);
1375 if (ret && ret != -EEXIST)
1376 goto fail;
1377
1378 leaf = path->nodes[0];
1379 }
1380 ref = btrfs_item_ptr(leaf, path->slots[0],
1381 struct btrfs_extent_data_ref);
1382 if (ret == 0) {
1383 btrfs_set_extent_data_ref_root(leaf, ref,
1384 root_objectid);
1385 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1386 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1387 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1388 } else {
1389 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1390 num_refs += refs_to_add;
1391 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1392 }
1393 }
1394 btrfs_mark_buffer_dirty(leaf);
1395 ret = 0;
1396fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001397 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001398 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001399}
1400
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001401static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001402 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001403 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -07001404 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001405{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001406 struct btrfs_key key;
1407 struct btrfs_extent_data_ref *ref1 = NULL;
1408 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001409 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001410 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001411 int ret = 0;
1412
1413 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001414 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1415
1416 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1417 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1418 struct btrfs_extent_data_ref);
1419 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1420 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1421 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1422 struct btrfs_shared_data_ref);
1423 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1424#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1425 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1426 struct btrfs_extent_ref_v0 *ref0;
1427 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1428 struct btrfs_extent_ref_v0);
1429 num_refs = btrfs_ref_count_v0(leaf, ref0);
1430#endif
1431 } else {
1432 BUG();
1433 }
1434
Chris Mason56bec292009-03-13 10:10:06 -04001435 BUG_ON(num_refs < refs_to_drop);
1436 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001437
Zheng Yan31840ae2008-09-23 13:14:14 -04001438 if (num_refs == 0) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001439 ret = btrfs_del_item(trans, fs_info->extent_root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -07001440 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001441 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001442 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1443 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1444 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1445 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1446#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1447 else {
1448 struct btrfs_extent_ref_v0 *ref0;
1449 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1450 struct btrfs_extent_ref_v0);
1451 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1452 }
1453#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001454 btrfs_mark_buffer_dirty(leaf);
1455 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001456 return ret;
1457}
1458
Zhaolei9ed0dea2015-08-06 22:16:24 +08001459static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001460 struct btrfs_extent_inline_ref *iref)
1461{
1462 struct btrfs_key key;
1463 struct extent_buffer *leaf;
1464 struct btrfs_extent_data_ref *ref1;
1465 struct btrfs_shared_data_ref *ref2;
1466 u32 num_refs = 0;
Liu Bo3de28d52017-08-18 15:15:19 -06001467 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001468
1469 leaf = path->nodes[0];
1470 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1471 if (iref) {
Liu Bo3de28d52017-08-18 15:15:19 -06001472 /*
1473 * If type is invalid, we should have bailed out earlier than
1474 * this call.
1475 */
1476 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
1477 ASSERT(type != BTRFS_REF_TYPE_INVALID);
1478 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001479 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1480 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1481 } else {
1482 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1483 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1484 }
1485 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1486 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1487 struct btrfs_extent_data_ref);
1488 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1489 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1490 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1491 struct btrfs_shared_data_ref);
1492 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1493#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1494 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1495 struct btrfs_extent_ref_v0 *ref0;
1496 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1497 struct btrfs_extent_ref_v0);
1498 num_refs = btrfs_ref_count_v0(leaf, ref0);
1499#endif
1500 } else {
1501 WARN_ON(1);
1502 }
1503 return num_refs;
1504}
1505
1506static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001507 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001508 struct btrfs_path *path,
1509 u64 bytenr, u64 parent,
1510 u64 root_objectid)
1511{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001512 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001513 struct btrfs_key key;
1514 int ret;
1515
1516 key.objectid = bytenr;
1517 if (parent) {
1518 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1519 key.offset = parent;
1520 } else {
1521 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1522 key.offset = root_objectid;
1523 }
1524
1525 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1526 if (ret > 0)
1527 ret = -ENOENT;
1528#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1529 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001530 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001531 key.type = BTRFS_EXTENT_REF_V0_KEY;
1532 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1533 if (ret > 0)
1534 ret = -ENOENT;
1535 }
1536#endif
1537 return ret;
1538}
1539
1540static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001541 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001542 struct btrfs_path *path,
1543 u64 bytenr, u64 parent,
1544 u64 root_objectid)
1545{
1546 struct btrfs_key key;
1547 int ret;
1548
1549 key.objectid = bytenr;
1550 if (parent) {
1551 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1552 key.offset = parent;
1553 } else {
1554 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1555 key.offset = root_objectid;
1556 }
1557
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001558 ret = btrfs_insert_empty_item(trans, fs_info->extent_root,
1559 path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001560 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001561 return ret;
1562}
1563
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001564static inline int extent_ref_type(u64 parent, u64 owner)
1565{
1566 int type;
1567 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1568 if (parent > 0)
1569 type = BTRFS_SHARED_BLOCK_REF_KEY;
1570 else
1571 type = BTRFS_TREE_BLOCK_REF_KEY;
1572 } else {
1573 if (parent > 0)
1574 type = BTRFS_SHARED_DATA_REF_KEY;
1575 else
1576 type = BTRFS_EXTENT_DATA_REF_KEY;
1577 }
1578 return type;
1579}
1580
Yan Zheng2c47e6052009-06-27 21:07:35 -04001581static int find_next_key(struct btrfs_path *path, int level,
1582 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001583
1584{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001585 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001586 if (!path->nodes[level])
1587 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001588 if (path->slots[level] + 1 >=
1589 btrfs_header_nritems(path->nodes[level]))
1590 continue;
1591 if (level == 0)
1592 btrfs_item_key_to_cpu(path->nodes[level], key,
1593 path->slots[level] + 1);
1594 else
1595 btrfs_node_key_to_cpu(path->nodes[level], key,
1596 path->slots[level] + 1);
1597 return 0;
1598 }
1599 return 1;
1600}
1601
1602/*
1603 * look for inline back ref. if back ref is found, *ref_ret is set
1604 * to the address of inline back ref, and 0 is returned.
1605 *
1606 * if back ref isn't found, *ref_ret is set to the address where it
1607 * should be inserted, and -ENOENT is returned.
1608 *
1609 * if insert is true and there are too many inline back refs, the path
1610 * points to the extent item, and -EAGAIN is returned.
1611 *
1612 * NOTE: inline back refs are ordered in the same way that back ref
1613 * items in the tree are ordered.
1614 */
1615static noinline_for_stack
1616int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001617 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001618 struct btrfs_path *path,
1619 struct btrfs_extent_inline_ref **ref_ret,
1620 u64 bytenr, u64 num_bytes,
1621 u64 parent, u64 root_objectid,
1622 u64 owner, u64 offset, int insert)
1623{
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001624 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001625 struct btrfs_key key;
1626 struct extent_buffer *leaf;
1627 struct btrfs_extent_item *ei;
1628 struct btrfs_extent_inline_ref *iref;
1629 u64 flags;
1630 u64 item_size;
1631 unsigned long ptr;
1632 unsigned long end;
1633 int extra_size;
1634 int type;
1635 int want;
1636 int ret;
1637 int err = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001638 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Liu Bo3de28d52017-08-18 15:15:19 -06001639 int needed;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001640
1641 key.objectid = bytenr;
1642 key.type = BTRFS_EXTENT_ITEM_KEY;
1643 key.offset = num_bytes;
1644
1645 want = extent_ref_type(parent, owner);
1646 if (insert) {
1647 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001648 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001649 } else
1650 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001651
1652 /*
1653 * Owner is our parent level, so we can just add one to get the level
1654 * for the block we are interested in.
1655 */
1656 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1657 key.type = BTRFS_METADATA_ITEM_KEY;
1658 key.offset = owner;
1659 }
1660
1661again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001662 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1663 if (ret < 0) {
1664 err = ret;
1665 goto out;
1666 }
Josef Bacik3173a182013-03-07 14:22:04 -05001667
1668 /*
1669 * We may be a newly converted file system which still has the old fat
1670 * extent entries for metadata, so try and see if we have one of those.
1671 */
1672 if (ret > 0 && skinny_metadata) {
1673 skinny_metadata = false;
1674 if (path->slots[0]) {
1675 path->slots[0]--;
1676 btrfs_item_key_to_cpu(path->nodes[0], &key,
1677 path->slots[0]);
1678 if (key.objectid == bytenr &&
1679 key.type == BTRFS_EXTENT_ITEM_KEY &&
1680 key.offset == num_bytes)
1681 ret = 0;
1682 }
1683 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +01001684 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05001685 key.type = BTRFS_EXTENT_ITEM_KEY;
1686 key.offset = num_bytes;
1687 btrfs_release_path(path);
1688 goto again;
1689 }
1690 }
1691
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001692 if (ret && !insert) {
1693 err = -ENOENT;
1694 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301695 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -05001696 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -05001697 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001698 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001699
1700 leaf = path->nodes[0];
1701 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1702#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1703 if (item_size < sizeof(*ei)) {
1704 if (!insert) {
1705 err = -ENOENT;
1706 goto out;
1707 }
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001708 ret = convert_extent_item_v0(trans, fs_info, path, owner,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001709 extra_size);
1710 if (ret < 0) {
1711 err = ret;
1712 goto out;
1713 }
1714 leaf = path->nodes[0];
1715 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1716 }
1717#endif
1718 BUG_ON(item_size < sizeof(*ei));
1719
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001720 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1721 flags = btrfs_extent_flags(leaf, ei);
1722
1723 ptr = (unsigned long)(ei + 1);
1724 end = (unsigned long)ei + item_size;
1725
Josef Bacik3173a182013-03-07 14:22:04 -05001726 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001727 ptr += sizeof(struct btrfs_tree_block_info);
1728 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001729 }
1730
Liu Bo3de28d52017-08-18 15:15:19 -06001731 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
1732 needed = BTRFS_REF_TYPE_DATA;
1733 else
1734 needed = BTRFS_REF_TYPE_BLOCK;
1735
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001736 err = -ENOENT;
1737 while (1) {
1738 if (ptr >= end) {
1739 WARN_ON(ptr > end);
1740 break;
1741 }
1742 iref = (struct btrfs_extent_inline_ref *)ptr;
Liu Bo3de28d52017-08-18 15:15:19 -06001743 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
1744 if (type == BTRFS_REF_TYPE_INVALID) {
1745 err = -EINVAL;
1746 goto out;
1747 }
1748
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001749 if (want < type)
1750 break;
1751 if (want > type) {
1752 ptr += btrfs_extent_inline_ref_size(type);
1753 continue;
1754 }
1755
1756 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1757 struct btrfs_extent_data_ref *dref;
1758 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1759 if (match_extent_data_ref(leaf, dref, root_objectid,
1760 owner, offset)) {
1761 err = 0;
1762 break;
1763 }
1764 if (hash_extent_data_ref_item(leaf, dref) <
1765 hash_extent_data_ref(root_objectid, owner, offset))
1766 break;
1767 } else {
1768 u64 ref_offset;
1769 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1770 if (parent > 0) {
1771 if (parent == ref_offset) {
1772 err = 0;
1773 break;
1774 }
1775 if (ref_offset < parent)
1776 break;
1777 } else {
1778 if (root_objectid == ref_offset) {
1779 err = 0;
1780 break;
1781 }
1782 if (ref_offset < root_objectid)
1783 break;
1784 }
1785 }
1786 ptr += btrfs_extent_inline_ref_size(type);
1787 }
1788 if (err == -ENOENT && insert) {
1789 if (item_size + extra_size >=
1790 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1791 err = -EAGAIN;
1792 goto out;
1793 }
1794 /*
1795 * To add new inline back ref, we have to make sure
1796 * there is no corresponding back ref item.
1797 * For simplicity, we just do not add new inline back
1798 * ref if there is any kind of item for this block
1799 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001800 if (find_next_key(path, 0, &key) == 0 &&
1801 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001802 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001803 err = -EAGAIN;
1804 goto out;
1805 }
1806 }
1807 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1808out:
Yan Zheng85d41982009-06-11 08:51:10 -04001809 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001810 path->keep_locks = 0;
1811 btrfs_unlock_up_safe(path, 1);
1812 }
1813 return err;
1814}
1815
1816/*
1817 * helper to add new inline back ref
1818 */
1819static noinline_for_stack
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001820void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001821 struct btrfs_path *path,
1822 struct btrfs_extent_inline_ref *iref,
1823 u64 parent, u64 root_objectid,
1824 u64 owner, u64 offset, int refs_to_add,
1825 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001826{
1827 struct extent_buffer *leaf;
1828 struct btrfs_extent_item *ei;
1829 unsigned long ptr;
1830 unsigned long end;
1831 unsigned long item_offset;
1832 u64 refs;
1833 int size;
1834 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001835
1836 leaf = path->nodes[0];
1837 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1838 item_offset = (unsigned long)iref - (unsigned long)ei;
1839
1840 type = extent_ref_type(parent, owner);
1841 size = btrfs_extent_inline_ref_size(type);
1842
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001843 btrfs_extend_item(fs_info, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001844
1845 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1846 refs = btrfs_extent_refs(leaf, ei);
1847 refs += refs_to_add;
1848 btrfs_set_extent_refs(leaf, ei, refs);
1849 if (extent_op)
1850 __run_delayed_extent_op(extent_op, leaf, ei);
1851
1852 ptr = (unsigned long)ei + item_offset;
1853 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1854 if (ptr < end - size)
1855 memmove_extent_buffer(leaf, ptr + size, ptr,
1856 end - size - ptr);
1857
1858 iref = (struct btrfs_extent_inline_ref *)ptr;
1859 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1860 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1861 struct btrfs_extent_data_ref *dref;
1862 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1863 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1864 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1865 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1866 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1867 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1868 struct btrfs_shared_data_ref *sref;
1869 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1870 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1871 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1872 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1873 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1874 } else {
1875 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1876 }
1877 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878}
1879
1880static int lookup_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001881 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001882 struct btrfs_path *path,
1883 struct btrfs_extent_inline_ref **ref_ret,
1884 u64 bytenr, u64 num_bytes, u64 parent,
1885 u64 root_objectid, u64 owner, u64 offset)
1886{
1887 int ret;
1888
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001889 ret = lookup_inline_extent_backref(trans, fs_info, path, ref_ret,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001890 bytenr, num_bytes, parent,
1891 root_objectid, owner, offset, 0);
1892 if (ret != -ENOENT)
1893 return ret;
1894
David Sterbab3b4aa72011-04-21 01:20:15 +02001895 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001896 *ref_ret = NULL;
1897
1898 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001899 ret = lookup_tree_block_ref(trans, fs_info, path, bytenr,
1900 parent, root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001901 } else {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001902 ret = lookup_extent_data_ref(trans, fs_info, path, bytenr,
1903 parent, root_objectid, owner,
1904 offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001905 }
1906 return ret;
1907}
1908
1909/*
1910 * helper to update/remove inline back ref
1911 */
1912static noinline_for_stack
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001913void update_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001914 struct btrfs_path *path,
1915 struct btrfs_extent_inline_ref *iref,
1916 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001917 struct btrfs_delayed_extent_op *extent_op,
1918 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001919{
1920 struct extent_buffer *leaf;
1921 struct btrfs_extent_item *ei;
1922 struct btrfs_extent_data_ref *dref = NULL;
1923 struct btrfs_shared_data_ref *sref = NULL;
1924 unsigned long ptr;
1925 unsigned long end;
1926 u32 item_size;
1927 int size;
1928 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001929 u64 refs;
1930
1931 leaf = path->nodes[0];
1932 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1933 refs = btrfs_extent_refs(leaf, ei);
1934 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1935 refs += refs_to_mod;
1936 btrfs_set_extent_refs(leaf, ei, refs);
1937 if (extent_op)
1938 __run_delayed_extent_op(extent_op, leaf, ei);
1939
Liu Bo3de28d52017-08-18 15:15:19 -06001940 /*
1941 * If type is invalid, we should have bailed out after
1942 * lookup_inline_extent_backref().
1943 */
1944 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1945 ASSERT(type != BTRFS_REF_TYPE_INVALID);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001946
1947 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1948 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1949 refs = btrfs_extent_data_ref_count(leaf, dref);
1950 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1951 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1952 refs = btrfs_shared_data_ref_count(leaf, sref);
1953 } else {
1954 refs = 1;
1955 BUG_ON(refs_to_mod != -1);
1956 }
1957
1958 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1959 refs += refs_to_mod;
1960
1961 if (refs > 0) {
1962 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1963 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1964 else
1965 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1966 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001967 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001968 size = btrfs_extent_inline_ref_size(type);
1969 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1970 ptr = (unsigned long)iref;
1971 end = (unsigned long)ei + item_size;
1972 if (ptr + size < end)
1973 memmove_extent_buffer(leaf, ptr, ptr + size,
1974 end - ptr - size);
1975 item_size -= size;
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001976 btrfs_truncate_item(fs_info, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001977 }
1978 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001979}
1980
1981static noinline_for_stack
1982int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001983 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001984 struct btrfs_path *path,
1985 u64 bytenr, u64 num_bytes, u64 parent,
1986 u64 root_objectid, u64 owner,
1987 u64 offset, int refs_to_add,
1988 struct btrfs_delayed_extent_op *extent_op)
1989{
1990 struct btrfs_extent_inline_ref *iref;
1991 int ret;
1992
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001993 ret = lookup_inline_extent_backref(trans, fs_info, path, &iref,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001994 bytenr, num_bytes, parent,
1995 root_objectid, owner, offset, 1);
1996 if (ret == 0) {
1997 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05001998 update_inline_extent_backref(fs_info, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001999 refs_to_add, extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002000 } else if (ret == -ENOENT) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002001 setup_inline_extent_backref(fs_info, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002002 root_objectid, owner, offset,
2003 refs_to_add, extent_op);
2004 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002005 }
2006 return ret;
2007}
2008
2009static int insert_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002010 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002011 struct btrfs_path *path,
2012 u64 bytenr, u64 parent, u64 root_objectid,
2013 u64 owner, u64 offset, int refs_to_add)
2014{
2015 int ret;
2016 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
2017 BUG_ON(refs_to_add != 1);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002018 ret = insert_tree_block_ref(trans, fs_info, path, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002019 parent, root_objectid);
2020 } else {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002021 ret = insert_extent_data_ref(trans, fs_info, path, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002022 parent, root_objectid,
2023 owner, offset, refs_to_add);
2024 }
2025 return ret;
2026}
2027
2028static int remove_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002029 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002030 struct btrfs_path *path,
2031 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07002032 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002033{
Jeff Mahoney143bede2012-03-01 14:56:26 +01002034 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002035
2036 BUG_ON(!is_data && refs_to_drop != 1);
2037 if (iref) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002038 update_inline_extent_backref(fs_info, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07002039 -refs_to_drop, NULL, last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002040 } else if (is_data) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002041 ret = remove_extent_data_ref(trans, fs_info, path, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07002042 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002043 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07002044 *last_ref = 1;
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002045 ret = btrfs_del_item(trans, fs_info->extent_root, path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002046 }
2047 return ret;
2048}
2049
Jeff Mahoney86557862015-06-15 09:41:16 -04002050#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002051static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
2052 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05002053{
Jeff Mahoney86557862015-06-15 09:41:16 -04002054 int j, ret = 0;
2055 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002056 u64 aligned_start = ALIGN(start, 1 << 9);
2057
2058 if (WARN_ON(start != aligned_start)) {
2059 len -= aligned_start - start;
2060 len = round_down(len, 1 << 9);
2061 start = aligned_start;
2062 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002063
2064 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04002065
2066 if (!len)
2067 return 0;
2068
2069 end = start + len;
2070 bytes_left = len;
2071
2072 /* Skip any superblocks on this device. */
2073 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
2074 u64 sb_start = btrfs_sb_offset(j);
2075 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
2076 u64 size = sb_start - start;
2077
2078 if (!in_range(sb_start, start, bytes_left) &&
2079 !in_range(sb_end, start, bytes_left) &&
2080 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
2081 continue;
2082
2083 /*
2084 * Superblock spans beginning of range. Adjust start and
2085 * try again.
2086 */
2087 if (sb_start <= start) {
2088 start += sb_end - start;
2089 if (start > end) {
2090 bytes_left = 0;
2091 break;
2092 }
2093 bytes_left = end - start;
2094 continue;
2095 }
2096
2097 if (size) {
2098 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
2099 GFP_NOFS, 0);
2100 if (!ret)
2101 *discarded_bytes += size;
2102 else if (ret != -EOPNOTSUPP)
2103 return ret;
2104 }
2105
2106 start = sb_end;
2107 if (start > end) {
2108 bytes_left = 0;
2109 break;
2110 }
2111 bytes_left = end - start;
2112 }
2113
2114 if (bytes_left) {
2115 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002116 GFP_NOFS, 0);
2117 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04002118 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002119 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002120 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05002121}
Chris Mason15916de2008-11-19 21:17:22 -05002122
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002123int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00002124 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05002125{
Liu Hui1f3c79a2009-01-05 15:57:51 -05002126 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00002127 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02002128 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002129
Christoph Hellwige244a0a2009-10-14 09:24:59 -04002130
Filipe Manana29992412016-05-27 17:42:05 +01002131 /*
2132 * Avoid races with device replace and make sure our bbio has devices
2133 * associated to its stripes that don't go away while we are discarding.
2134 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002135 btrfs_bio_counter_inc_blocked(fs_info);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002136 /* Tell the block device(s) that the sectors can be discarded */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002137 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
2138 &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002139 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05002140 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02002141 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002142 int i;
2143
Liu Hui1f3c79a2009-01-05 15:57:51 -05002144
Jan Schmidta1d3c472011-08-04 17:15:33 +02002145 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002146 u64 bytes;
Josef Bacikd5e20032011-08-04 14:52:27 +00002147 if (!stripe->dev->can_discard)
2148 continue;
2149
Li Dongyang5378e602011-03-24 10:24:27 +00002150 ret = btrfs_issue_discard(stripe->dev->bdev,
2151 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002152 stripe->length,
2153 &bytes);
Li Dongyang5378e602011-03-24 10:24:27 +00002154 if (!ret)
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002155 discarded_bytes += bytes;
Li Dongyang5378e602011-03-24 10:24:27 +00002156 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002157 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00002158
2159 /*
2160 * Just in case we get back EOPNOTSUPP for some reason,
2161 * just ignore the return value so we don't screw up
2162 * people calling discard_extent.
2163 */
2164 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002165 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08002166 btrfs_put_bbio(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002167 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002168 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00002169
2170 if (actual_bytes)
2171 *actual_bytes = discarded_bytes;
2172
Liu Hui1f3c79a2009-01-05 15:57:51 -05002173
David Woodhouse53b381b2013-01-29 18:40:14 -05002174 if (ret == -EOPNOTSUPP)
2175 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002176 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002177}
2178
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002179/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002180int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002181 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002182 u64 bytenr, u64 num_bytes, u64 parent,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002183 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04002184{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002185 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07002186 int old_ref_mod, new_ref_mod;
Zheng Yan31840ae2008-09-23 13:14:14 -04002187 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002188
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002189 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
2190 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04002191
Josef Bacikfd708b82017-09-29 15:43:50 -04002192 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent, root_objectid,
2193 owner, offset, BTRFS_ADD_DELAYED_REF);
2194
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002195 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002196 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07002197 num_bytes, parent,
2198 root_objectid, (int)owner,
2199 BTRFS_ADD_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07002200 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002201 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002202 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07002203 num_bytes, parent,
2204 root_objectid, owner, offset,
Omar Sandovald7eae342017-06-06 16:45:31 -07002205 0, BTRFS_ADD_DELAYED_REF,
2206 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002207 }
Omar Sandovald7eae342017-06-06 16:45:31 -07002208
2209 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0)
2210 add_pinned_bytes(fs_info, -num_bytes, owner, root_objectid);
2211
Zheng Yan31840ae2008-09-23 13:14:14 -04002212 return ret;
2213}
2214
Chris Mason925baed2008-06-25 16:01:30 -04002215static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002216 struct btrfs_fs_info *fs_info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002217 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002218 u64 parent, u64 root_objectid,
2219 u64 owner, u64 offset, int refs_to_add,
2220 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04002221{
Chris Mason5caf2a02007-04-02 11:20:42 -04002222 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002223 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04002224 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07002225 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002226 u64 bytenr = node->bytenr;
2227 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002228 u64 refs;
2229 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05002230
Chris Mason5caf2a02007-04-02 11:20:42 -04002231 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002232 if (!path)
2233 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04002234
David Sterbae4058b52015-11-27 16:31:35 +01002235 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002236 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002237 /* this will setup the path even if it fails to insert the back ref */
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002238 ret = insert_inline_extent_backref(trans, fs_info, path, bytenr,
2239 num_bytes, parent, root_objectid,
2240 owner, offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002241 refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08002242 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002243 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07002244
2245 /*
2246 * Ok we had -EAGAIN which means we didn't have space to insert and
2247 * inline extent ref, so just update the reference count and add a
2248 * normal backref.
2249 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002250 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07002251 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002252 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2253 refs = btrfs_extent_refs(leaf, item);
2254 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2255 if (extent_op)
2256 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002257
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002258 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002259 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002260
David Sterbae4058b52015-11-27 16:31:35 +01002261 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002262 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04002263 /* now insert the actual backref */
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002264 ret = insert_extent_backref(trans, fs_info, path, bytenr, parent,
2265 root_objectid, owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002266 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04002267 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002268out:
Chris Mason74493f72007-12-11 09:25:06 -05002269 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08002270 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002271}
2272
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002273static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002274 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002275 struct btrfs_delayed_ref_node *node,
2276 struct btrfs_delayed_extent_op *extent_op,
2277 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002278{
Chris Mason56bec292009-03-13 10:10:06 -04002279 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002280 struct btrfs_delayed_data_ref *ref;
2281 struct btrfs_key ins;
2282 u64 parent = 0;
2283 u64 ref_root = 0;
2284 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002285
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002286 ins.objectid = node->bytenr;
2287 ins.offset = node->num_bytes;
2288 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002289
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002290 ref = btrfs_delayed_node_to_data_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002291 trace_run_delayed_data_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002292
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002293 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2294 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002295 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002296
2297 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002298 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002299 flags |= extent_op->flags_to_set;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002300 ret = alloc_reserved_file_extent(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002301 parent, ref_root, flags,
2302 ref->objectid, ref->offset,
2303 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002304 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002305 ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002306 ref_root, ref->objectid,
2307 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002308 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002309 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002310 ret = __btrfs_free_extent(trans, fs_info, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002311 ref_root, ref->objectid,
2312 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002313 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002314 } else {
2315 BUG();
2316 }
Chris Mason56bec292009-03-13 10:10:06 -04002317 return ret;
2318}
2319
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002320static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2321 struct extent_buffer *leaf,
2322 struct btrfs_extent_item *ei)
2323{
2324 u64 flags = btrfs_extent_flags(leaf, ei);
2325 if (extent_op->update_flags) {
2326 flags |= extent_op->flags_to_set;
2327 btrfs_set_extent_flags(leaf, ei, flags);
2328 }
2329
2330 if (extent_op->update_key) {
2331 struct btrfs_tree_block_info *bi;
2332 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2333 bi = (struct btrfs_tree_block_info *)(ei + 1);
2334 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2335 }
2336}
2337
2338static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002339 struct btrfs_fs_info *fs_info,
Josef Bacikd2788502017-09-29 15:43:57 -04002340 struct btrfs_delayed_ref_head *head,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002341 struct btrfs_delayed_extent_op *extent_op)
2342{
2343 struct btrfs_key key;
2344 struct btrfs_path *path;
2345 struct btrfs_extent_item *ei;
2346 struct extent_buffer *leaf;
2347 u32 item_size;
2348 int ret;
2349 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002350 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002351
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002352 if (trans->aborted)
2353 return 0;
2354
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002355 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05002356 metadata = 0;
2357
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002358 path = btrfs_alloc_path();
2359 if (!path)
2360 return -ENOMEM;
2361
Josef Bacikd2788502017-09-29 15:43:57 -04002362 key.objectid = head->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002363
Josef Bacik3173a182013-03-07 14:22:04 -05002364 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002365 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002366 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002367 } else {
2368 key.type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacikd2788502017-09-29 15:43:57 -04002369 key.offset = head->num_bytes;
Josef Bacik3173a182013-03-07 14:22:04 -05002370 }
2371
2372again:
David Sterbae4058b52015-11-27 16:31:35 +01002373 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002374 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002375 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002376 if (ret < 0) {
2377 err = ret;
2378 goto out;
2379 }
2380 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002381 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002382 if (path->slots[0] > 0) {
2383 path->slots[0]--;
2384 btrfs_item_key_to_cpu(path->nodes[0], &key,
2385 path->slots[0]);
Josef Bacikd2788502017-09-29 15:43:57 -04002386 if (key.objectid == head->bytenr &&
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002387 key.type == BTRFS_EXTENT_ITEM_KEY &&
Josef Bacikd2788502017-09-29 15:43:57 -04002388 key.offset == head->num_bytes)
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002389 ret = 0;
2390 }
2391 if (ret > 0) {
2392 btrfs_release_path(path);
2393 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002394
Josef Bacikd2788502017-09-29 15:43:57 -04002395 key.objectid = head->bytenr;
2396 key.offset = head->num_bytes;
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002397 key.type = BTRFS_EXTENT_ITEM_KEY;
2398 goto again;
2399 }
2400 } else {
2401 err = -EIO;
2402 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002403 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002404 }
2405
2406 leaf = path->nodes[0];
2407 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2408#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2409 if (item_size < sizeof(*ei)) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05002410 ret = convert_extent_item_v0(trans, fs_info, path, (u64)-1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002411 if (ret < 0) {
2412 err = ret;
2413 goto out;
2414 }
2415 leaf = path->nodes[0];
2416 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2417 }
2418#endif
2419 BUG_ON(item_size < sizeof(*ei));
2420 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2421 __run_delayed_extent_op(extent_op, leaf, ei);
2422
2423 btrfs_mark_buffer_dirty(leaf);
2424out:
2425 btrfs_free_path(path);
2426 return err;
2427}
2428
2429static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002430 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002431 struct btrfs_delayed_ref_node *node,
2432 struct btrfs_delayed_extent_op *extent_op,
2433 int insert_reserved)
2434{
2435 int ret = 0;
2436 struct btrfs_delayed_tree_ref *ref;
2437 struct btrfs_key ins;
2438 u64 parent = 0;
2439 u64 ref_root = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002440 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002441
2442 ref = btrfs_delayed_node_to_tree_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002443 trace_run_delayed_tree_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002444
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002445 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2446 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002447 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002448
Josef Bacik3173a182013-03-07 14:22:04 -05002449 ins.objectid = node->bytenr;
2450 if (skinny_metadata) {
2451 ins.offset = ref->level;
2452 ins.type = BTRFS_METADATA_ITEM_KEY;
2453 } else {
2454 ins.offset = node->num_bytes;
2455 ins.type = BTRFS_EXTENT_ITEM_KEY;
2456 }
2457
Liu Bo02794222016-09-14 19:19:05 -07002458 if (node->ref_mod != 1) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002459 btrfs_err(fs_info,
Liu Bo02794222016-09-14 19:19:05 -07002460 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
2461 node->bytenr, node->ref_mod, node->action, ref_root,
2462 parent);
2463 return -EIO;
2464 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002465 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002466 BUG_ON(!extent_op || !extent_op->update_flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002467 ret = alloc_reserved_tree_block(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002468 parent, ref_root,
2469 extent_op->flags_to_set,
2470 &extent_op->key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002471 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002472 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002473 ret = __btrfs_inc_extent_ref(trans, fs_info, node,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002474 parent, ref_root,
2475 ref->level, 0, 1,
Josef Bacikfcebe452014-05-13 17:30:47 -07002476 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002477 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002478 ret = __btrfs_free_extent(trans, fs_info, node,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002479 parent, ref_root,
2480 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002481 } else {
2482 BUG();
2483 }
2484 return ret;
2485}
2486
Chris Mason56bec292009-03-13 10:10:06 -04002487/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002488static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002489 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002490 struct btrfs_delayed_ref_node *node,
2491 struct btrfs_delayed_extent_op *extent_op,
2492 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002493{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002494 int ret = 0;
2495
Josef Bacik857cc2f2013-10-07 15:21:08 -04002496 if (trans->aborted) {
2497 if (insert_reserved)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002498 btrfs_pin_extent(fs_info, node->bytenr,
Josef Bacik857cc2f2013-10-07 15:21:08 -04002499 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002500 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002501 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002502
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002503 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2504 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002505 ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002506 insert_reserved);
2507 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2508 node->type == BTRFS_SHARED_DATA_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002509 ret = run_delayed_data_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002510 insert_reserved);
2511 else
2512 BUG();
2513 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002514}
2515
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002516static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04002517select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002518{
Filipe Mananacffc3372015-07-09 13:13:44 +01002519 struct btrfs_delayed_ref_node *ref;
2520
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002521 if (list_empty(&head->ref_list))
2522 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002523
Filipe Mananacffc3372015-07-09 13:13:44 +01002524 /*
2525 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2526 * This is to prevent a ref count from going down to zero, which deletes
2527 * the extent item from the extent tree, when there still are references
2528 * to add, which would fail because they would not find the extent item.
2529 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002530 if (!list_empty(&head->ref_add_list))
2531 return list_first_entry(&head->ref_add_list,
2532 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01002533
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002534 ref = list_first_entry(&head->ref_list, struct btrfs_delayed_ref_node,
2535 list);
2536 ASSERT(list_empty(&ref->add_list));
2537 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04002538}
2539
Josef Bacik2eadaa22017-09-29 15:43:52 -04002540static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
2541 struct btrfs_delayed_ref_head *head)
2542{
2543 spin_lock(&delayed_refs->lock);
2544 head->processing = 0;
2545 delayed_refs->num_heads_ready++;
2546 spin_unlock(&delayed_refs->lock);
2547 btrfs_delayed_ref_unlock(head);
2548}
2549
Josef Bacikb00e6252017-09-29 15:43:53 -04002550static int cleanup_extent_op(struct btrfs_trans_handle *trans,
2551 struct btrfs_fs_info *fs_info,
2552 struct btrfs_delayed_ref_head *head)
2553{
2554 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
2555 int ret;
2556
2557 if (!extent_op)
2558 return 0;
2559 head->extent_op = NULL;
2560 if (head->must_insert_reserved) {
2561 btrfs_free_delayed_extent_op(extent_op);
2562 return 0;
2563 }
2564 spin_unlock(&head->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002565 ret = run_delayed_extent_op(trans, fs_info, head, extent_op);
Josef Bacikb00e6252017-09-29 15:43:53 -04002566 btrfs_free_delayed_extent_op(extent_op);
2567 return ret ? ret : 1;
2568}
2569
Josef Bacik194ab0b2017-09-29 15:43:54 -04002570static int cleanup_ref_head(struct btrfs_trans_handle *trans,
2571 struct btrfs_fs_info *fs_info,
2572 struct btrfs_delayed_ref_head *head)
2573{
2574 struct btrfs_delayed_ref_root *delayed_refs;
2575 int ret;
2576
2577 delayed_refs = &trans->transaction->delayed_refs;
2578
2579 ret = cleanup_extent_op(trans, fs_info, head);
2580 if (ret < 0) {
2581 unselect_delayed_ref_head(delayed_refs, head);
2582 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
2583 return ret;
2584 } else if (ret) {
2585 return ret;
2586 }
2587
2588 /*
2589 * Need to drop our head ref lock and re-acquire the delayed ref lock
2590 * and then re-check to make sure nobody got added.
2591 */
2592 spin_unlock(&head->lock);
2593 spin_lock(&delayed_refs->lock);
2594 spin_lock(&head->lock);
2595 if (!list_empty(&head->ref_list) || head->extent_op) {
2596 spin_unlock(&head->lock);
2597 spin_unlock(&delayed_refs->lock);
2598 return 1;
2599 }
Josef Bacik194ab0b2017-09-29 15:43:54 -04002600 delayed_refs->num_heads--;
2601 rb_erase(&head->href_node, &delayed_refs->href_root);
Josef Bacikd2788502017-09-29 15:43:57 -04002602 RB_CLEAR_NODE(&head->href_node);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002603 spin_unlock(&delayed_refs->lock);
Josef Bacikc1103f72017-09-29 15:43:56 -04002604 spin_unlock(&head->lock);
2605 atomic_dec(&delayed_refs->num_entries);
2606
Josef Bacikd2788502017-09-29 15:43:57 -04002607 trace_run_delayed_ref_head(fs_info, head, 0);
Josef Bacikc1103f72017-09-29 15:43:56 -04002608
2609 if (head->total_ref_mod < 0) {
2610 struct btrfs_block_group_cache *cache;
2611
Josef Bacikd2788502017-09-29 15:43:57 -04002612 cache = btrfs_lookup_block_group(fs_info, head->bytenr);
Josef Bacikc1103f72017-09-29 15:43:56 -04002613 ASSERT(cache);
2614 percpu_counter_add(&cache->space_info->total_bytes_pinned,
Josef Bacikd2788502017-09-29 15:43:57 -04002615 -head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04002616 btrfs_put_block_group(cache);
2617
2618 if (head->is_data) {
2619 spin_lock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04002620 delayed_refs->pending_csums -= head->num_bytes;
Josef Bacikc1103f72017-09-29 15:43:56 -04002621 spin_unlock(&delayed_refs->lock);
2622 }
2623 }
2624
2625 if (head->must_insert_reserved) {
Josef Bacikd2788502017-09-29 15:43:57 -04002626 btrfs_pin_extent(fs_info, head->bytenr,
2627 head->num_bytes, 1);
Josef Bacikc1103f72017-09-29 15:43:56 -04002628 if (head->is_data) {
Josef Bacikd2788502017-09-29 15:43:57 -04002629 ret = btrfs_del_csums(trans, fs_info, head->bytenr,
2630 head->num_bytes);
Josef Bacikc1103f72017-09-29 15:43:56 -04002631 }
2632 }
2633
2634 /* Also free its reserved qgroup space */
2635 btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
2636 head->qgroup_reserved);
2637 btrfs_delayed_ref_unlock(head);
Josef Bacikd2788502017-09-29 15:43:57 -04002638 btrfs_put_delayed_ref_head(head);
Josef Bacik194ab0b2017-09-29 15:43:54 -04002639 return 0;
2640}
2641
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002642/*
2643 * Returns 0 on success or if called with an already aborted transaction.
2644 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2645 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002646static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002647 struct btrfs_fs_info *fs_info,
Josef Bacikd7df2c72014-01-23 09:21:38 -05002648 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002649{
Chris Mason56bec292009-03-13 10:10:06 -04002650 struct btrfs_delayed_ref_root *delayed_refs;
2651 struct btrfs_delayed_ref_node *ref;
2652 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002653 struct btrfs_delayed_extent_op *extent_op;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002654 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002655 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002656 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002657 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002658 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002659
2660 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002661 while (1) {
2662 if (!locked_ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002663 if (count >= nr)
Chris Mason56bec292009-03-13 10:10:06 -04002664 break;
Chris Mason56bec292009-03-13 10:10:06 -04002665
Josef Bacikd7df2c72014-01-23 09:21:38 -05002666 spin_lock(&delayed_refs->lock);
2667 locked_ref = btrfs_select_ref_head(trans);
2668 if (!locked_ref) {
2669 spin_unlock(&delayed_refs->lock);
2670 break;
2671 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002672
2673 /* grab the lock that says we are going to process
2674 * all the refs for this head */
2675 ret = btrfs_delayed_ref_lock(trans, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002676 spin_unlock(&delayed_refs->lock);
Chris Masonc3e69d52009-03-13 10:17:05 -04002677 /*
2678 * we may have dropped the spin lock to get the head
2679 * mutex lock, and that might have given someone else
2680 * time to free the head. If that's true, it has been
2681 * removed from our list and we can move on.
2682 */
2683 if (ret == -EAGAIN) {
2684 locked_ref = NULL;
2685 count++;
2686 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002687 }
2688 }
2689
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002690 /*
2691 * We need to try and merge add/drops of the same ref since we
2692 * can run into issues with relocate dropping the implicit ref
2693 * and then it being added back again before the drop can
2694 * finish. If we merged anything we need to re-loop so we can
2695 * get a good ref.
2696 * Or we can get node references of the same type that weren't
2697 * merged when created due to bumps in the tree mod seq, and
2698 * we need to merge them to prevent adding an inline extent
2699 * backref before dropping it (triggering a BUG_ON at
2700 * insert_inline_extent_backref()).
2701 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002702 spin_lock(&locked_ref->lock);
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002703 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2704 locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002705
2706 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002707 * locked_ref is the head node, so we have to go one
2708 * node back for any delayed ref updates
2709 */
2710 ref = select_delayed_ref(locked_ref);
2711
2712 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002713 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002714 spin_unlock(&locked_ref->lock);
Josef Bacik2eadaa22017-09-29 15:43:52 -04002715 unselect_delayed_ref_head(delayed_refs, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002716 locked_ref = NULL;
Arne Jansend1270cd2011-09-13 15:16:43 +02002717 cond_resched();
Josef Bacik27a377d2014-02-07 13:57:59 -05002718 count++;
Arne Jansend1270cd2011-09-13 15:16:43 +02002719 continue;
2720 }
2721
Josef Bacikc1103f72017-09-29 15:43:56 -04002722 /*
2723 * We're done processing refs in this ref_head, clean everything
2724 * up and move on to the next ref_head.
2725 */
Chris Mason56bec292009-03-13 10:10:06 -04002726 if (!ref) {
Josef Bacik194ab0b2017-09-29 15:43:54 -04002727 ret = cleanup_ref_head(trans, fs_info, locked_ref);
2728 if (ret > 0 ) {
Josef Bacikb00e6252017-09-29 15:43:53 -04002729 /* We dropped our lock, we need to loop. */
2730 ret = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002731 continue;
Josef Bacik194ab0b2017-09-29 15:43:54 -04002732 } else if (ret) {
2733 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002734 }
Josef Bacikc1103f72017-09-29 15:43:56 -04002735 locked_ref = NULL;
2736 count++;
2737 continue;
2738 }
Chris Mason56bec292009-03-13 10:10:06 -04002739
Josef Bacikc1103f72017-09-29 15:43:56 -04002740 actual_count++;
2741 ref->in_tree = 0;
2742 list_del(&ref->list);
2743 if (!list_empty(&ref->add_list))
2744 list_del(&ref->add_list);
2745 /*
2746 * When we play the delayed ref, also correct the ref_mod on
2747 * head
2748 */
2749 switch (ref->action) {
2750 case BTRFS_ADD_DELAYED_REF:
2751 case BTRFS_ADD_DELAYED_EXTENT:
Josef Bacikd2788502017-09-29 15:43:57 -04002752 locked_ref->ref_mod -= ref->ref_mod;
Josef Bacikc1103f72017-09-29 15:43:56 -04002753 break;
2754 case BTRFS_DROP_DELAYED_REF:
Josef Bacikd2788502017-09-29 15:43:57 -04002755 locked_ref->ref_mod += ref->ref_mod;
Josef Bacikc1103f72017-09-29 15:43:56 -04002756 break;
2757 default:
2758 WARN_ON(1);
Arne Jansen22cd2e72012-08-09 00:16:53 -06002759 }
Josef Bacik1ce7a5e2017-09-29 15:43:55 -04002760 atomic_dec(&delayed_refs->num_entries);
2761
Josef Bacikb00e6252017-09-29 15:43:53 -04002762 /*
2763 * Record the must-insert_reserved flag before we drop the spin
2764 * lock.
2765 */
2766 must_insert_reserved = locked_ref->must_insert_reserved;
2767 locked_ref->must_insert_reserved = 0;
2768
2769 extent_op = locked_ref->extent_op;
2770 locked_ref->extent_op = NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002771 spin_unlock(&locked_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002772
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002773 ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002774 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002775
Miao Xie78a61842012-11-21 02:21:28 +00002776 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002777 if (ret) {
Josef Bacik2eadaa22017-09-29 15:43:52 -04002778 unselect_delayed_ref_head(delayed_refs, locked_ref);
Miao Xie093486c2012-12-19 08:10:10 +00002779 btrfs_put_delayed_ref(ref);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002780 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
2781 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002782 return ret;
2783 }
2784
Miao Xie093486c2012-12-19 08:10:10 +00002785 btrfs_put_delayed_ref(ref);
2786 count++;
Chris Mason1887be62009-03-13 10:11:24 -04002787 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002788 }
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002789
2790 /*
2791 * We don't want to include ref heads since we can have empty ref heads
2792 * and those will drastically skew our runtime down since we just do
2793 * accounting, no actual extent tree updates.
2794 */
2795 if (actual_count > 0) {
2796 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2797 u64 avg;
2798
2799 /*
2800 * We weigh the current average higher than our current runtime
2801 * to avoid large swings in the average.
2802 */
2803 spin_lock(&delayed_refs->lock);
2804 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002805 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002806 spin_unlock(&delayed_refs->lock);
2807 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002808 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002809}
2810
Arne Jansen709c0482011-09-12 12:22:57 +02002811#ifdef SCRAMBLE_DELAYED_REFS
2812/*
2813 * Normally delayed refs get processed in ascending bytenr order. This
2814 * correlates in most cases to the order added. To expose dependencies on this
2815 * order, we start to process the tree in the middle instead of the beginning
2816 */
2817static u64 find_middle(struct rb_root *root)
2818{
2819 struct rb_node *n = root->rb_node;
2820 struct btrfs_delayed_ref_node *entry;
2821 int alt = 1;
2822 u64 middle;
2823 u64 first = 0, last = 0;
2824
2825 n = rb_first(root);
2826 if (n) {
2827 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2828 first = entry->bytenr;
2829 }
2830 n = rb_last(root);
2831 if (n) {
2832 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2833 last = entry->bytenr;
2834 }
2835 n = root->rb_node;
2836
2837 while (n) {
2838 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2839 WARN_ON(!entry->in_tree);
2840
2841 middle = entry->bytenr;
2842
2843 if (alt)
2844 n = n->rb_left;
2845 else
2846 n = n->rb_right;
2847
2848 alt = 1 - alt;
2849 }
2850 return middle;
2851}
2852#endif
2853
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002854static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002855{
2856 u64 num_bytes;
2857
2858 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2859 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002860 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002861 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2862
2863 /*
2864 * 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 -04002865 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002866 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002867 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002868}
2869
Josef Bacik12621332015-02-03 07:50:16 -08002870/*
2871 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2872 * would require to store the csums for that many bytes.
2873 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002874u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002875{
2876 u64 csum_size;
2877 u64 num_csums_per_leaf;
2878 u64 num_csums;
2879
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002880 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002881 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002882 (u64)btrfs_super_csum_size(fs_info->super_copy));
2883 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002884 num_csums += num_csums_per_leaf - 1;
2885 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2886 return num_csums;
2887}
2888
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002889int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002890 struct btrfs_fs_info *fs_info)
Josef Bacik1be41b72013-06-12 13:56:06 -04002891{
2892 struct btrfs_block_rsv *global_rsv;
2893 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
Josef Bacik12621332015-02-03 07:50:16 -08002894 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
Josef Bacikcb723e42015-02-18 08:06:57 -08002895 u64 num_dirty_bgs = trans->transaction->num_dirty_bgs;
2896 u64 num_bytes, num_dirty_bgs_bytes;
Josef Bacik1be41b72013-06-12 13:56:06 -04002897 int ret = 0;
2898
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002899 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002900 num_heads = heads_to_leaves(fs_info, num_heads);
Josef Bacik1be41b72013-06-12 13:56:06 -04002901 if (num_heads > 1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002902 num_bytes += (num_heads - 1) * fs_info->nodesize;
Josef Bacik1be41b72013-06-12 13:56:06 -04002903 num_bytes <<= 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002904 num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
2905 fs_info->nodesize;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002906 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
Josef Bacikcb723e42015-02-18 08:06:57 -08002907 num_dirty_bgs);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002908 global_rsv = &fs_info->global_block_rsv;
Josef Bacik1be41b72013-06-12 13:56:06 -04002909
2910 /*
2911 * If we can't allocate any more chunks lets make sure we have _lots_ of
2912 * wiggle room since running delayed refs can create more delayed refs.
2913 */
Josef Bacikcb723e42015-02-18 08:06:57 -08002914 if (global_rsv->space_info->full) {
2915 num_dirty_bgs_bytes <<= 1;
Josef Bacik1be41b72013-06-12 13:56:06 -04002916 num_bytes <<= 1;
Josef Bacikcb723e42015-02-18 08:06:57 -08002917 }
Josef Bacik1be41b72013-06-12 13:56:06 -04002918
2919 spin_lock(&global_rsv->lock);
Josef Bacikcb723e42015-02-18 08:06:57 -08002920 if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
Josef Bacik1be41b72013-06-12 13:56:06 -04002921 ret = 1;
2922 spin_unlock(&global_rsv->lock);
2923 return ret;
2924}
2925
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002926int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002927 struct btrfs_fs_info *fs_info)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002928{
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002929 u64 num_entries =
2930 atomic_read(&trans->transaction->delayed_refs.num_entries);
2931 u64 avg_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002932 u64 val;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002933
2934 smp_mb();
2935 avg_runtime = fs_info->avg_delayed_ref_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002936 val = num_entries * avg_runtime;
Wang Xiaoguangdc1a90c2016-10-26 15:23:01 +08002937 if (val >= NSEC_PER_SEC)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002938 return 1;
Chris Masona79b7d42014-05-22 16:18:52 -07002939 if (val >= NSEC_PER_SEC / 2)
2940 return 2;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002941
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002942 return btrfs_check_space_for_delayed_refs(trans, fs_info);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002943}
2944
Chris Masona79b7d42014-05-22 16:18:52 -07002945struct async_delayed_refs {
2946 struct btrfs_root *root;
Josef Bacik31b96552016-04-11 17:37:40 -04002947 u64 transid;
Chris Masona79b7d42014-05-22 16:18:52 -07002948 int count;
2949 int error;
2950 int sync;
2951 struct completion wait;
2952 struct btrfs_work work;
2953};
2954
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002955static inline struct async_delayed_refs *
2956to_async_delayed_refs(struct btrfs_work *work)
2957{
2958 return container_of(work, struct async_delayed_refs, work);
2959}
2960
Chris Masona79b7d42014-05-22 16:18:52 -07002961static void delayed_ref_async_start(struct btrfs_work *work)
2962{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002963 struct async_delayed_refs *async = to_async_delayed_refs(work);
Chris Masona79b7d42014-05-22 16:18:52 -07002964 struct btrfs_trans_handle *trans;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002965 struct btrfs_fs_info *fs_info = async->root->fs_info;
Chris Masona79b7d42014-05-22 16:18:52 -07002966 int ret;
2967
Chris Mason0f873ec2016-04-27 09:59:38 -04002968 /* if the commit is already started, we don't need to wait here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002969 if (btrfs_transaction_blocked(fs_info))
Josef Bacik31b96552016-04-11 17:37:40 -04002970 goto done;
Josef Bacik31b96552016-04-11 17:37:40 -04002971
Chris Mason0f873ec2016-04-27 09:59:38 -04002972 trans = btrfs_join_transaction(async->root);
2973 if (IS_ERR(trans)) {
2974 async->error = PTR_ERR(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07002975 goto done;
2976 }
2977
2978 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002979 * trans->sync means that when we call end_transaction, we won't
Chris Masona79b7d42014-05-22 16:18:52 -07002980 * wait on delayed refs
2981 */
2982 trans->sync = true;
Chris Mason0f873ec2016-04-27 09:59:38 -04002983
2984 /* Don't bother flushing if we got into a different transaction */
2985 if (trans->transid > async->transid)
2986 goto end;
2987
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002988 ret = btrfs_run_delayed_refs(trans, fs_info, async->count);
Chris Masona79b7d42014-05-22 16:18:52 -07002989 if (ret)
2990 async->error = ret;
Chris Mason0f873ec2016-04-27 09:59:38 -04002991end:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002992 ret = btrfs_end_transaction(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07002993 if (ret && !async->error)
2994 async->error = ret;
2995done:
2996 if (async->sync)
2997 complete(&async->wait);
2998 else
2999 kfree(async);
3000}
3001
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003002int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
Josef Bacik31b96552016-04-11 17:37:40 -04003003 unsigned long count, u64 transid, int wait)
Chris Masona79b7d42014-05-22 16:18:52 -07003004{
3005 struct async_delayed_refs *async;
3006 int ret;
3007
3008 async = kmalloc(sizeof(*async), GFP_NOFS);
3009 if (!async)
3010 return -ENOMEM;
3011
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003012 async->root = fs_info->tree_root;
Chris Masona79b7d42014-05-22 16:18:52 -07003013 async->count = count;
3014 async->error = 0;
Josef Bacik31b96552016-04-11 17:37:40 -04003015 async->transid = transid;
Chris Masona79b7d42014-05-22 16:18:52 -07003016 if (wait)
3017 async->sync = 1;
3018 else
3019 async->sync = 0;
3020 init_completion(&async->wait);
3021
Liu Bo9e0af232014-08-15 23:36:53 +08003022 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
3023 delayed_ref_async_start, NULL, NULL);
Chris Masona79b7d42014-05-22 16:18:52 -07003024
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003025 btrfs_queue_work(fs_info->extent_workers, &async->work);
Chris Masona79b7d42014-05-22 16:18:52 -07003026
3027 if (wait) {
3028 wait_for_completion(&async->wait);
3029 ret = async->error;
3030 kfree(async);
3031 return ret;
3032 }
3033 return 0;
3034}
3035
Chris Masonc3e69d52009-03-13 10:17:05 -04003036/*
3037 * this starts processing the delayed reference count updates and
3038 * extent insertions we have queued up so far. count can be
3039 * 0, which means to process everything in the tree at the start
3040 * of the run (but not newly added entries), or it can be some target
3041 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003042 *
3043 * Returns 0 on success or if called with an aborted transaction
3044 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04003045 */
3046int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003047 struct btrfs_fs_info *fs_info, unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04003048{
3049 struct rb_node *node;
3050 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08003051 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04003052 int ret;
3053 int run_all = count == (unsigned long)-1;
Filipe Mananad9a05402015-10-03 13:13:13 +01003054 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Chris Masonc3e69d52009-03-13 10:17:05 -04003055
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003056 /* We'll clean this up in btrfs_cleanup_transaction */
3057 if (trans->aborted)
3058 return 0;
3059
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003060 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08003061 return 0;
3062
Chris Masonc3e69d52009-03-13 10:17:05 -04003063 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08003064 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05003065 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05003066
Chris Masonc3e69d52009-03-13 10:17:05 -04003067again:
Arne Jansen709c0482011-09-12 12:22:57 +02003068#ifdef SCRAMBLE_DELAYED_REFS
3069 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
3070#endif
Filipe Mananad9a05402015-10-03 13:13:13 +01003071 trans->can_flush_pending_bgs = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003072 ret = __btrfs_run_delayed_refs(trans, fs_info, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003073 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003074 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003075 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04003076 }
3077
Chris Mason56bec292009-03-13 10:10:06 -04003078 if (run_all) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05003079 if (!list_empty(&trans->new_bgs))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003080 btrfs_create_pending_block_groups(trans, fs_info);
Josef Bacikea658ba2012-09-11 16:57:25 -04003081
Josef Bacikd7df2c72014-01-23 09:21:38 -05003082 spin_lock(&delayed_refs->lock);
Liu Boc46effa2013-10-14 12:59:45 +08003083 node = rb_first(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003084 if (!node) {
3085 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04003086 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05003087 }
Josef Bacikd2788502017-09-29 15:43:57 -04003088 head = rb_entry(node, struct btrfs_delayed_ref_head,
3089 href_node);
3090 refcount_inc(&head->refs);
Chris Mason56bec292009-03-13 10:10:06 -04003091 spin_unlock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -04003092
3093 /* Mutex was contended, block until it's released and retry. */
3094 mutex_lock(&head->mutex);
3095 mutex_unlock(&head->mutex);
3096
3097 btrfs_put_delayed_ref_head(head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003098 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04003099 goto again;
3100 }
Chris Mason54aa1f42007-06-22 14:16:25 -04003101out:
Filipe Mananad9a05402015-10-03 13:13:13 +01003102 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Chris Masona28ec192007-03-06 20:08:01 -05003103 return 0;
3104}
3105
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003106int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003107 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003108 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04003109 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003110{
3111 struct btrfs_delayed_extent_op *extent_op;
3112 int ret;
3113
Miao Xie78a61842012-11-21 02:21:28 +00003114 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003115 if (!extent_op)
3116 return -ENOMEM;
3117
3118 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01003119 extent_op->update_flags = true;
3120 extent_op->update_key = false;
3121 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04003122 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003123
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003124 ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003125 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003126 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00003127 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003128 return ret;
3129}
3130
Liu Boe4c3b2d2017-01-30 12:25:28 -08003131static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003132 struct btrfs_path *path,
3133 u64 objectid, u64 offset, u64 bytenr)
3134{
3135 struct btrfs_delayed_ref_head *head;
3136 struct btrfs_delayed_ref_node *ref;
3137 struct btrfs_delayed_data_ref *data_ref;
3138 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08003139 struct btrfs_transaction *cur_trans;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003140 int ret = 0;
3141
Liu Boe4c3b2d2017-01-30 12:25:28 -08003142 cur_trans = root->fs_info->running_transaction;
3143 if (!cur_trans)
3144 return 0;
3145
3146 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003147 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003148 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003149 if (!head) {
3150 spin_unlock(&delayed_refs->lock);
3151 return 0;
3152 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003153
3154 if (!mutex_trylock(&head->mutex)) {
Josef Bacikd2788502017-09-29 15:43:57 -04003155 refcount_inc(&head->refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003156 spin_unlock(&delayed_refs->lock);
3157
David Sterbab3b4aa72011-04-21 01:20:15 +02003158 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003159
David Sterba8cc33e52011-05-02 15:29:25 +02003160 /*
3161 * Mutex was contended, block until it's released and let
3162 * caller try again
3163 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003164 mutex_lock(&head->mutex);
3165 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04003166 btrfs_put_delayed_ref_head(head);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003167 return -EAGAIN;
3168 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003169 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003170
3171 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08003172 list_for_each_entry(ref, &head->ref_list, list) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05003173 /* If it's a shared ref we know a cross reference exists */
3174 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
3175 ret = 1;
3176 break;
3177 }
3178
3179 data_ref = btrfs_delayed_node_to_data_ref(ref);
3180
3181 /*
3182 * If our ref doesn't match the one we're currently looking at
3183 * then we have a cross reference.
3184 */
3185 if (data_ref->root != root->root_key.objectid ||
3186 data_ref->objectid != objectid ||
3187 data_ref->offset != offset) {
3188 ret = 1;
3189 break;
3190 }
3191 }
3192 spin_unlock(&head->lock);
3193 mutex_unlock(&head->mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003194 return ret;
3195}
3196
Liu Boe4c3b2d2017-01-30 12:25:28 -08003197static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003198 struct btrfs_path *path,
3199 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05003200{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003201 struct btrfs_fs_info *fs_info = root->fs_info;
3202 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04003203 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003204 struct btrfs_extent_data_ref *ref;
3205 struct btrfs_extent_inline_ref *iref;
3206 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05003207 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003208 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06003209 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04003210 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05003211
Chris Masonbe20aa92007-12-17 20:14:01 -05003212 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04003213 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04003214 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05003215
Chris Masonbe20aa92007-12-17 20:14:01 -05003216 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3217 if (ret < 0)
3218 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003219 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04003220
3221 ret = -ENOENT;
3222 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04003223 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003224
Zheng Yan31840ae2008-09-23 13:14:14 -04003225 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04003226 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003227 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05003228
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003229 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05003230 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003231
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003232 ret = 1;
3233 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3234#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3235 if (item_size < sizeof(*ei)) {
3236 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3237 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003238 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003239#endif
3240 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
3241
3242 if (item_size != sizeof(*ei) +
3243 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
3244 goto out;
3245
3246 if (btrfs_extent_generation(leaf, ei) <=
3247 btrfs_root_last_snapshot(&root->root_item))
3248 goto out;
3249
3250 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
Liu Bo3de28d52017-08-18 15:15:19 -06003251
3252 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
3253 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003254 goto out;
3255
3256 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3257 if (btrfs_extent_refs(leaf, ei) !=
3258 btrfs_extent_data_ref_count(leaf, ref) ||
3259 btrfs_extent_data_ref_root(leaf, ref) !=
3260 root->root_key.objectid ||
3261 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3262 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3263 goto out;
3264
Yan Zhengf321e492008-07-30 09:26:11 -04003265 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05003266out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003267 return ret;
3268}
3269
Liu Boe4c3b2d2017-01-30 12:25:28 -08003270int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
3271 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003272{
3273 struct btrfs_path *path;
3274 int ret;
3275 int ret2;
3276
3277 path = btrfs_alloc_path();
3278 if (!path)
3279 return -ENOENT;
3280
3281 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08003282 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003283 offset, bytenr);
3284 if (ret && ret != -ENOENT)
3285 goto out;
3286
Liu Boe4c3b2d2017-01-30 12:25:28 -08003287 ret2 = check_delayed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003288 offset, bytenr);
3289 } while (ret2 == -EAGAIN);
3290
3291 if (ret2 && ret2 != -ENOENT) {
3292 ret = ret2;
3293 goto out;
3294 }
3295
3296 if (ret != -ENOENT || ret2 != -ENOENT)
3297 ret = 0;
3298out:
Yan Zhengf321e492008-07-30 09:26:11 -04003299 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003300 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3301 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04003302 return ret;
3303}
3304
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003305static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05003306 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003307 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07003308 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04003309{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003310 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04003311 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003312 u64 num_bytes;
3313 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003314 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04003315 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003316 struct btrfs_key key;
3317 struct btrfs_file_extent_item *fi;
3318 int i;
3319 int level;
3320 int ret = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003321 int (*process_func)(struct btrfs_trans_handle *,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003322 struct btrfs_root *,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003323 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04003324
David Sterbafccb84c2014-09-29 23:53:21 +02003325
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003326 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003327 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003328
Zheng Yan31840ae2008-09-23 13:14:14 -04003329 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003330 nritems = btrfs_header_nritems(buf);
3331 level = btrfs_header_level(buf);
3332
Miao Xie27cdeb72014-04-02 19:51:05 +08003333 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003334 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003335
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003336 if (inc)
3337 process_func = btrfs_inc_extent_ref;
3338 else
3339 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003340
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003341 if (full_backref)
3342 parent = buf->start;
3343 else
3344 parent = 0;
3345
Zheng Yan31840ae2008-09-23 13:14:14 -04003346 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003347 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003348 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02003349 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003350 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003351 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003352 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003353 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003354 BTRFS_FILE_EXTENT_INLINE)
3355 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003356 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3357 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003358 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003359
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003360 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3361 key.offset -= btrfs_file_extent_offset(buf, fi);
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003362 ret = process_func(trans, root, bytenr, num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003363 parent, ref_root, key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003364 key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003365 if (ret)
3366 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003367 } else {
3368 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003369 num_bytes = fs_info->nodesize;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003370 ret = process_func(trans, root, bytenr, num_bytes,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003371 parent, ref_root, level - 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003372 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003373 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003374 }
3375 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003376 return 0;
3377fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003378 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003379}
3380
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003381int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003382 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04003383{
Josef Bacike339a6b2014-07-02 10:54:25 -07003384 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003385}
Zheng Yan31840ae2008-09-23 13:14:14 -04003386
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003387int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003388 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003389{
Josef Bacike339a6b2014-07-02 10:54:25 -07003390 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003391}
3392
Chris Mason9078a3e2007-04-26 16:46:15 -04003393static int write_one_cache_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003394 struct btrfs_fs_info *fs_info,
Chris Mason9078a3e2007-04-26 16:46:15 -04003395 struct btrfs_path *path,
3396 struct btrfs_block_group_cache *cache)
3397{
3398 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003399 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003400 unsigned long bi;
3401 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003402
Chris Mason9078a3e2007-04-26 16:46:15 -04003403 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003404 if (ret) {
3405 if (ret > 0)
3406 ret = -ENOENT;
Chris Mason54aa1f42007-06-22 14:16:25 -04003407 goto fail;
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003408 }
Chris Mason5f39d392007-10-15 16:14:19 -04003409
3410 leaf = path->nodes[0];
3411 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3412 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3413 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -04003414fail:
Filipe Manana24b89d02015-04-25 18:31:05 +01003415 btrfs_release_path(path);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003416 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003417
3418}
3419
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003420static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003421next_block_group(struct btrfs_fs_info *fs_info,
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003422 struct btrfs_block_group_cache *cache)
3423{
3424 struct rb_node *node;
Filipe Manana292cbd52014-11-26 15:28:50 +00003425
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003426 spin_lock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003427
3428 /* If our block group was removed, we need a full search. */
3429 if (RB_EMPTY_NODE(&cache->cache_node)) {
3430 const u64 next_bytenr = cache->key.objectid + cache->key.offset;
3431
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003432 spin_unlock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003433 btrfs_put_block_group(cache);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003434 cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
Filipe Manana292cbd52014-11-26 15:28:50 +00003435 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003436 node = rb_next(&cache->cache_node);
3437 btrfs_put_block_group(cache);
3438 if (node) {
3439 cache = rb_entry(node, struct btrfs_block_group_cache,
3440 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003441 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003442 } else
3443 cache = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003444 spin_unlock(&fs_info->block_group_cache_lock);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003445 return cache;
3446}
3447
Josef Bacik0af3d002010-06-21 14:48:16 -04003448static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3449 struct btrfs_trans_handle *trans,
3450 struct btrfs_path *path)
3451{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003452 struct btrfs_fs_info *fs_info = block_group->fs_info;
3453 struct btrfs_root *root = fs_info->tree_root;
Josef Bacik0af3d002010-06-21 14:48:16 -04003454 struct inode *inode = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08003455 struct extent_changeset *data_reserved = NULL;
Josef Bacik0af3d002010-06-21 14:48:16 -04003456 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003457 int dcs = BTRFS_DC_ERROR;
David Sterbaf8c269d2015-01-16 17:21:12 +01003458 u64 num_pages = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003459 int retries = 0;
3460 int ret = 0;
3461
3462 /*
3463 * If this block group is smaller than 100 megs don't bother caching the
3464 * block group.
3465 */
Byongho Leeee221842015-12-15 01:42:10 +09003466 if (block_group->key.offset < (100 * SZ_1M)) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003467 spin_lock(&block_group->lock);
3468 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3469 spin_unlock(&block_group->lock);
3470 return 0;
3471 }
3472
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003473 if (trans->aborted)
3474 return 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003475again:
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003476 inode = lookup_free_space_inode(fs_info, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003477 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3478 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003479 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003480 goto out;
3481 }
3482
3483 if (IS_ERR(inode)) {
3484 BUG_ON(retries);
3485 retries++;
3486
3487 if (block_group->ro)
3488 goto out_free;
3489
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003490 ret = create_free_space_inode(fs_info, trans, block_group,
3491 path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003492 if (ret)
3493 goto out_free;
3494 goto again;
3495 }
3496
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003497 /* We've already setup this transaction, go ahead and exit */
3498 if (block_group->cache_generation == trans->transid &&
3499 i_size_read(inode)) {
3500 dcs = BTRFS_DC_SETUP;
3501 goto out_put;
3502 }
3503
Josef Bacik0af3d002010-06-21 14:48:16 -04003504 /*
3505 * We want to set the generation to 0, that way if anything goes wrong
3506 * from here on out we know not to trust this cache when we load up next
3507 * time.
3508 */
3509 BTRFS_I(inode)->generation = 0;
3510 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003511 if (ret) {
3512 /*
3513 * So theoretically we could recover from this, simply set the
3514 * super cache generation to 0 so we know to invalidate the
3515 * cache, but then we'd have to keep track of the block groups
3516 * that fail this way so we know we _have_ to reset this cache
3517 * before the next commit or risk reading stale cache. So to
3518 * limit our exposure to horrible edge cases lets just abort the
3519 * transaction, this only happens in really bad situations
3520 * anyway.
3521 */
Jeff Mahoney66642832016-06-10 18:19:25 -04003522 btrfs_abort_transaction(trans, ret);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003523 goto out_put;
3524 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003525 WARN_ON(ret);
3526
3527 if (i_size_read(inode) > 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003528 ret = btrfs_check_trunc_cache_free_space(fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003529 &fs_info->global_block_rsv);
Miao Xie7b61cd92013-05-13 13:55:09 +00003530 if (ret)
3531 goto out_put;
3532
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003533 ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003534 if (ret)
3535 goto out_put;
3536 }
3537
3538 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003539 if (block_group->cached != BTRFS_CACHE_FINISHED ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003540 !btrfs_test_opt(fs_info, SPACE_CACHE)) {
Liu Bocf7c1ef2012-07-06 03:31:34 -06003541 /*
3542 * don't bother trying to write stuff out _if_
3543 * a) we're not cached,
Liu Bo1a79c1f2017-03-06 13:49:02 -08003544 * b) we're with nospace_cache mount option,
3545 * c) we're with v2 space_cache (FREE_SPACE_TREE).
Liu Bocf7c1ef2012-07-06 03:31:34 -06003546 */
Josef Bacik2b209822010-12-03 13:17:53 -05003547 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003548 spin_unlock(&block_group->lock);
3549 goto out_put;
3550 }
3551 spin_unlock(&block_group->lock);
3552
Josef Bacik6fc823b2012-08-06 13:46:38 -06003553 /*
Josef Bacik2968b1f2015-10-01 12:55:18 -04003554 * We hit an ENOSPC when setting up the cache in this transaction, just
3555 * skip doing the setup, we've already cleared the cache so we're safe.
3556 */
3557 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
3558 ret = -ENOSPC;
3559 goto out_put;
3560 }
3561
3562 /*
Josef Bacik6fc823b2012-08-06 13:46:38 -06003563 * Try to preallocate enough space based on how big the block group is.
3564 * Keep in mind this has to include any pinned space which could end up
3565 * taking up quite a bit since it's not folded into the other space
3566 * cache.
3567 */
Byongho Leeee221842015-12-15 01:42:10 +09003568 num_pages = div_u64(block_group->key.offset, SZ_256M);
Josef Bacik0af3d002010-06-21 14:48:16 -04003569 if (!num_pages)
3570 num_pages = 1;
3571
Josef Bacik0af3d002010-06-21 14:48:16 -04003572 num_pages *= 16;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003573 num_pages *= PAGE_SIZE;
Josef Bacik0af3d002010-06-21 14:48:16 -04003574
Qu Wenruo364ecf32017-02-27 15:10:38 +08003575 ret = btrfs_check_data_free_space(inode, &data_reserved, 0, num_pages);
Josef Bacik0af3d002010-06-21 14:48:16 -04003576 if (ret)
3577 goto out_put;
3578
3579 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3580 num_pages, num_pages,
3581 &alloc_hint);
Josef Bacik2968b1f2015-10-01 12:55:18 -04003582 /*
3583 * Our cache requires contiguous chunks so that we don't modify a bunch
3584 * of metadata or split extents when writing the cache out, which means
3585 * we can enospc if we are heavily fragmented in addition to just normal
3586 * out of space conditions. So if we hit this just skip setting up any
3587 * other block groups for this transaction, maybe we'll unpin enough
3588 * space the next time around.
3589 */
Josef Bacik2b209822010-12-03 13:17:53 -05003590 if (!ret)
3591 dcs = BTRFS_DC_SETUP;
Josef Bacik2968b1f2015-10-01 12:55:18 -04003592 else if (ret == -ENOSPC)
3593 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
Josef Bacikc09544e2011-08-30 10:19:10 -04003594
Josef Bacik0af3d002010-06-21 14:48:16 -04003595out_put:
3596 iput(inode);
3597out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003598 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003599out:
3600 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003601 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003602 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003603 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003604 spin_unlock(&block_group->lock);
3605
Qu Wenruo364ecf32017-02-27 15:10:38 +08003606 extent_changeset_free(data_reserved);
Josef Bacik0af3d002010-06-21 14:48:16 -04003607 return ret;
3608}
3609
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003610int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003611 struct btrfs_fs_info *fs_info)
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003612{
3613 struct btrfs_block_group_cache *cache, *tmp;
3614 struct btrfs_transaction *cur_trans = trans->transaction;
3615 struct btrfs_path *path;
3616
3617 if (list_empty(&cur_trans->dirty_bgs) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003618 !btrfs_test_opt(fs_info, SPACE_CACHE))
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003619 return 0;
3620
3621 path = btrfs_alloc_path();
3622 if (!path)
3623 return -ENOMEM;
3624
3625 /* Could add new block groups, use _safe just in case */
3626 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
3627 dirty_list) {
3628 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3629 cache_save_setup(cache, trans, path);
3630 }
3631
3632 btrfs_free_path(path);
3633 return 0;
3634}
3635
Chris Mason1bbc6212015-04-06 12:46:08 -07003636/*
3637 * transaction commit does final block group cache writeback during a
3638 * critical section where nothing is allowed to change the FS. This is
3639 * required in order for the cache to actually match the block group,
3640 * but can introduce a lot of latency into the commit.
3641 *
3642 * So, btrfs_start_dirty_block_groups is here to kick off block group
3643 * cache IO. There's a chance we'll have to redo some of it if the
3644 * block group changes again during the commit, but it greatly reduces
3645 * the commit latency by getting rid of the easy block groups while
3646 * we're still allowing others to join the commit.
3647 */
3648int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003649 struct btrfs_fs_info *fs_info)
Chris Mason1bbc6212015-04-06 12:46:08 -07003650{
3651 struct btrfs_block_group_cache *cache;
3652 struct btrfs_transaction *cur_trans = trans->transaction;
3653 int ret = 0;
3654 int should_put;
3655 struct btrfs_path *path = NULL;
3656 LIST_HEAD(dirty);
3657 struct list_head *io = &cur_trans->io_bgs;
3658 int num_started = 0;
3659 int loops = 0;
3660
3661 spin_lock(&cur_trans->dirty_bgs_lock);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003662 if (list_empty(&cur_trans->dirty_bgs)) {
3663 spin_unlock(&cur_trans->dirty_bgs_lock);
3664 return 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003665 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003666 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Chris Mason1bbc6212015-04-06 12:46:08 -07003667 spin_unlock(&cur_trans->dirty_bgs_lock);
3668
3669again:
Chris Mason1bbc6212015-04-06 12:46:08 -07003670 /*
3671 * make sure all the block groups on our dirty list actually
3672 * exist
3673 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003674 btrfs_create_pending_block_groups(trans, fs_info);
Chris Mason1bbc6212015-04-06 12:46:08 -07003675
3676 if (!path) {
3677 path = btrfs_alloc_path();
3678 if (!path)
3679 return -ENOMEM;
3680 }
3681
Filipe Mananab58d1a92015-04-25 18:29:16 +01003682 /*
3683 * cache_write_mutex is here only to save us from balance or automatic
3684 * removal of empty block groups deleting this block group while we are
3685 * writing out the cache
3686 */
3687 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003688 while (!list_empty(&dirty)) {
3689 cache = list_first_entry(&dirty,
3690 struct btrfs_block_group_cache,
3691 dirty_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07003692 /*
3693 * this can happen if something re-dirties a block
3694 * group that is already under IO. Just wait for it to
3695 * finish and then do it all again
3696 */
3697 if (!list_empty(&cache->io_list)) {
3698 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003699 btrfs_wait_cache_io(trans, cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003700 btrfs_put_block_group(cache);
3701 }
3702
3703
3704 /*
3705 * btrfs_wait_cache_io uses the cache->dirty_list to decide
3706 * if it should update the cache_state. Don't delete
3707 * until after we wait.
3708 *
3709 * Since we're not running in the commit critical section
3710 * we need the dirty_bgs_lock to protect from update_block_group
3711 */
3712 spin_lock(&cur_trans->dirty_bgs_lock);
3713 list_del_init(&cache->dirty_list);
3714 spin_unlock(&cur_trans->dirty_bgs_lock);
3715
3716 should_put = 1;
3717
3718 cache_save_setup(cache, trans, path);
3719
3720 if (cache->disk_cache_state == BTRFS_DC_SETUP) {
3721 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003722 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003723 cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003724 if (ret == 0 && cache->io_ctl.inode) {
3725 num_started++;
3726 should_put = 0;
3727
3728 /*
3729 * the cache_write_mutex is protecting
3730 * the io_list
3731 */
3732 list_add_tail(&cache->io_list, io);
3733 } else {
3734 /*
3735 * if we failed to write the cache, the
3736 * generation will be bad and life goes on
3737 */
3738 ret = 0;
3739 }
3740 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003741 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003742 ret = write_one_cache_group(trans, fs_info,
3743 path, cache);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003744 /*
3745 * Our block group might still be attached to the list
3746 * of new block groups in the transaction handle of some
3747 * other task (struct btrfs_trans_handle->new_bgs). This
3748 * means its block group item isn't yet in the extent
3749 * tree. If this happens ignore the error, as we will
3750 * try again later in the critical section of the
3751 * transaction commit.
3752 */
3753 if (ret == -ENOENT) {
3754 ret = 0;
3755 spin_lock(&cur_trans->dirty_bgs_lock);
3756 if (list_empty(&cache->dirty_list)) {
3757 list_add_tail(&cache->dirty_list,
3758 &cur_trans->dirty_bgs);
3759 btrfs_get_block_group(cache);
3760 }
3761 spin_unlock(&cur_trans->dirty_bgs_lock);
3762 } else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003763 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003764 }
3765 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003766
3767 /* if its not on the io list, we need to put the block group */
3768 if (should_put)
3769 btrfs_put_block_group(cache);
3770
3771 if (ret)
3772 break;
Filipe Mananab58d1a92015-04-25 18:29:16 +01003773
3774 /*
3775 * Avoid blocking other tasks for too long. It might even save
3776 * us from writing caches for block groups that are going to be
3777 * removed.
3778 */
3779 mutex_unlock(&trans->transaction->cache_write_mutex);
3780 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003781 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003782 mutex_unlock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003783
3784 /*
3785 * go through delayed refs for all the stuff we've just kicked off
3786 * and then loop back (just once)
3787 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003788 ret = btrfs_run_delayed_refs(trans, fs_info, 0);
Chris Mason1bbc6212015-04-06 12:46:08 -07003789 if (!ret && loops == 0) {
3790 loops++;
3791 spin_lock(&cur_trans->dirty_bgs_lock);
3792 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003793 /*
3794 * dirty_bgs_lock protects us from concurrent block group
3795 * deletes too (not just cache_write_mutex).
3796 */
3797 if (!list_empty(&dirty)) {
3798 spin_unlock(&cur_trans->dirty_bgs_lock);
3799 goto again;
3800 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003801 spin_unlock(&cur_trans->dirty_bgs_lock);
Liu Boc79a1752016-07-20 17:44:12 -07003802 } else if (ret < 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003803 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
Chris Mason1bbc6212015-04-06 12:46:08 -07003804 }
3805
3806 btrfs_free_path(path);
3807 return ret;
3808}
3809
Chris Mason96b51792007-10-15 16:15:19 -04003810int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003811 struct btrfs_fs_info *fs_info)
Chris Mason9078a3e2007-04-26 16:46:15 -04003812{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003813 struct btrfs_block_group_cache *cache;
Josef Bacikce93ec52014-11-17 15:45:48 -05003814 struct btrfs_transaction *cur_trans = trans->transaction;
3815 int ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003816 int should_put;
Chris Mason9078a3e2007-04-26 16:46:15 -04003817 struct btrfs_path *path;
Chris Mason1bbc6212015-04-06 12:46:08 -07003818 struct list_head *io = &cur_trans->io_bgs;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003819 int num_started = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003820
3821 path = btrfs_alloc_path();
3822 if (!path)
3823 return -ENOMEM;
3824
Josef Bacikce93ec52014-11-17 15:45:48 -05003825 /*
Filipe Mananae44081e2015-12-18 03:02:48 +00003826 * Even though we are in the critical section of the transaction commit,
3827 * we can still have concurrent tasks adding elements to this
3828 * transaction's list of dirty block groups. These tasks correspond to
3829 * endio free space workers started when writeback finishes for a
3830 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
3831 * allocate new block groups as a result of COWing nodes of the root
3832 * tree when updating the free space inode. The writeback for the space
3833 * caches is triggered by an earlier call to
3834 * btrfs_start_dirty_block_groups() and iterations of the following
3835 * loop.
3836 * Also we want to do the cache_save_setup first and then run the
Josef Bacikce93ec52014-11-17 15:45:48 -05003837 * delayed refs to make sure we have the best chance at doing this all
3838 * in one shot.
3839 */
Filipe Mananae44081e2015-12-18 03:02:48 +00003840 spin_lock(&cur_trans->dirty_bgs_lock);
Josef Bacikce93ec52014-11-17 15:45:48 -05003841 while (!list_empty(&cur_trans->dirty_bgs)) {
3842 cache = list_first_entry(&cur_trans->dirty_bgs,
3843 struct btrfs_block_group_cache,
3844 dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003845
3846 /*
3847 * this can happen if cache_save_setup re-dirties a block
3848 * group that is already under IO. Just wait for it to
3849 * finish and then do it all again
3850 */
3851 if (!list_empty(&cache->io_list)) {
Filipe Mananae44081e2015-12-18 03:02:48 +00003852 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003853 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003854 btrfs_wait_cache_io(trans, cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003855 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003856 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003857 }
3858
Chris Mason1bbc6212015-04-06 12:46:08 -07003859 /*
3860 * don't remove from the dirty list until after we've waited
3861 * on any pending IO
3862 */
Josef Bacikce93ec52014-11-17 15:45:48 -05003863 list_del_init(&cache->dirty_list);
Filipe Mananae44081e2015-12-18 03:02:48 +00003864 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003865 should_put = 1;
3866
Chris Mason1bbc6212015-04-06 12:46:08 -07003867 cache_save_setup(cache, trans, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003868
Josef Bacikce93ec52014-11-17 15:45:48 -05003869 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003870 ret = btrfs_run_delayed_refs(trans, fs_info,
3871 (unsigned long) -1);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003872
3873 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3874 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003875 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003876 cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003877 if (ret == 0 && cache->io_ctl.inode) {
3878 num_started++;
3879 should_put = 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003880 list_add_tail(&cache->io_list, io);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003881 } else {
3882 /*
3883 * if we failed to write the cache, the
3884 * generation will be bad and life goes on
3885 */
3886 ret = 0;
3887 }
3888 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003889 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003890 ret = write_one_cache_group(trans, fs_info,
3891 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003892 /*
3893 * One of the free space endio workers might have
3894 * created a new block group while updating a free space
3895 * cache's inode (at inode.c:btrfs_finish_ordered_io())
3896 * and hasn't released its transaction handle yet, in
3897 * which case the new block group is still attached to
3898 * its transaction handle and its creation has not
3899 * finished yet (no block group item in the extent tree
3900 * yet, etc). If this is the case, wait for all free
3901 * space endio workers to finish and retry. This is a
3902 * a very rare case so no need for a more efficient and
3903 * complex approach.
3904 */
3905 if (ret == -ENOENT) {
3906 wait_event(cur_trans->writer_wait,
3907 atomic_read(&cur_trans->num_writers) == 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003908 ret = write_one_cache_group(trans, fs_info,
3909 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003910 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003911 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003912 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003913 }
Chris Masonc9dc4c62015-04-04 17:14:42 -07003914
3915 /* if its not on the io list, we need to put the block group */
3916 if (should_put)
3917 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003918 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003919 }
Filipe Mananae44081e2015-12-18 03:02:48 +00003920 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003921
Chris Mason1bbc6212015-04-06 12:46:08 -07003922 while (!list_empty(io)) {
3923 cache = list_first_entry(io, struct btrfs_block_group_cache,
Chris Masonc9dc4c62015-04-04 17:14:42 -07003924 io_list);
3925 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003926 btrfs_wait_cache_io(trans, cache, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003927 btrfs_put_block_group(cache);
3928 }
3929
Chris Mason9078a3e2007-04-26 16:46:15 -04003930 btrfs_free_path(path);
Josef Bacikce93ec52014-11-17 15:45:48 -05003931 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003932}
3933
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003934int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05003935{
3936 struct btrfs_block_group_cache *block_group;
3937 int readonly = 0;
3938
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003939 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003940 if (!block_group || block_group->ro)
3941 readonly = 1;
3942 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003943 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003944 return readonly;
3945}
3946
Filipe Mananaf78c4362016-05-09 13:15:41 +01003947bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3948{
3949 struct btrfs_block_group_cache *bg;
3950 bool ret = true;
3951
3952 bg = btrfs_lookup_block_group(fs_info, bytenr);
3953 if (!bg)
3954 return false;
3955
3956 spin_lock(&bg->lock);
3957 if (bg->ro)
3958 ret = false;
3959 else
3960 atomic_inc(&bg->nocow_writers);
3961 spin_unlock(&bg->lock);
3962
3963 /* no put on block group, done by btrfs_dec_nocow_writers */
3964 if (!ret)
3965 btrfs_put_block_group(bg);
3966
3967 return ret;
3968
3969}
3970
3971void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3972{
3973 struct btrfs_block_group_cache *bg;
3974
3975 bg = btrfs_lookup_block_group(fs_info, bytenr);
3976 ASSERT(bg);
3977 if (atomic_dec_and_test(&bg->nocow_writers))
3978 wake_up_atomic_t(&bg->nocow_writers);
3979 /*
3980 * Once for our lookup and once for the lookup done by a previous call
3981 * to btrfs_inc_nocow_writers()
3982 */
3983 btrfs_put_block_group(bg);
3984 btrfs_put_block_group(bg);
3985}
3986
3987static int btrfs_wait_nocow_writers_atomic_t(atomic_t *a)
3988{
3989 schedule();
3990 return 0;
3991}
3992
3993void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg)
3994{
3995 wait_on_atomic_t(&bg->nocow_writers,
3996 btrfs_wait_nocow_writers_atomic_t,
3997 TASK_UNINTERRUPTIBLE);
3998}
3999
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04004000static const char *alloc_name(u64 flags)
4001{
4002 switch (flags) {
4003 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
4004 return "mixed";
4005 case BTRFS_BLOCK_GROUP_METADATA:
4006 return "metadata";
4007 case BTRFS_BLOCK_GROUP_DATA:
4008 return "data";
4009 case BTRFS_BLOCK_GROUP_SYSTEM:
4010 return "system";
4011 default:
4012 WARN_ON(1);
4013 return "invalid-combination";
4014 };
4015}
4016
Nikolay Borisov2be12ef2017-05-22 09:35:49 +03004017static int create_space_info(struct btrfs_fs_info *info, u64 flags,
4018 struct btrfs_space_info **new)
4019{
4020
4021 struct btrfs_space_info *space_info;
4022 int i;
4023 int ret;
4024
4025 space_info = kzalloc(sizeof(*space_info), GFP_NOFS);
4026 if (!space_info)
4027 return -ENOMEM;
4028
4029 ret = percpu_counter_init(&space_info->total_bytes_pinned, 0,
4030 GFP_KERNEL);
4031 if (ret) {
4032 kfree(space_info);
4033 return ret;
4034 }
4035
4036 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
4037 INIT_LIST_HEAD(&space_info->block_groups[i]);
4038 init_rwsem(&space_info->groups_sem);
4039 spin_lock_init(&space_info->lock);
4040 space_info->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
4041 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
4042 init_waitqueue_head(&space_info->wait);
4043 INIT_LIST_HEAD(&space_info->ro_bgs);
4044 INIT_LIST_HEAD(&space_info->tickets);
4045 INIT_LIST_HEAD(&space_info->priority_tickets);
4046
4047 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
4048 info->space_info_kobj, "%s",
4049 alloc_name(space_info->flags));
4050 if (ret) {
4051 percpu_counter_destroy(&space_info->total_bytes_pinned);
4052 kfree(space_info);
4053 return ret;
4054 }
4055
4056 *new = space_info;
4057 list_add_rcu(&space_info->list, &info->space_info);
4058 if (flags & BTRFS_BLOCK_GROUP_DATA)
4059 info->data_sinfo = space_info;
4060
4061 return ret;
4062}
4063
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +03004064static void update_space_info(struct btrfs_fs_info *info, u64 flags,
Chris Mason593060d2008-03-25 16:50:33 -04004065 u64 total_bytes, u64 bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -04004066 u64 bytes_readonly,
Chris Mason593060d2008-03-25 16:50:33 -04004067 struct btrfs_space_info **space_info)
4068{
4069 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004070 int factor;
4071
4072 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
4073 BTRFS_BLOCK_GROUP_RAID10))
4074 factor = 2;
4075 else
4076 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04004077
4078 found = __find_space_info(info, flags);
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +03004079 ASSERT(found);
4080 spin_lock(&found->lock);
4081 found->total_bytes += total_bytes;
4082 found->disk_total += total_bytes * factor;
4083 found->bytes_used += bytes_used;
4084 found->disk_used += bytes_used * factor;
4085 found->bytes_readonly += bytes_readonly;
4086 if (total_bytes > 0)
4087 found->full = 0;
4088 space_info_add_new_bytes(info, found, total_bytes -
4089 bytes_used - bytes_readonly);
4090 spin_unlock(&found->lock);
4091 *space_info = found;
Chris Mason593060d2008-03-25 16:50:33 -04004092}
4093
Chris Mason8790d502008-04-03 16:29:03 -04004094static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
4095{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03004096 u64 extra_flags = chunk_to_extended(flags) &
4097 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004098
Miao Xiede98ced2013-01-29 10:13:12 +00004099 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004100 if (flags & BTRFS_BLOCK_GROUP_DATA)
4101 fs_info->avail_data_alloc_bits |= extra_flags;
4102 if (flags & BTRFS_BLOCK_GROUP_METADATA)
4103 fs_info->avail_metadata_alloc_bits |= extra_flags;
4104 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
4105 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004106 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04004107}
Chris Mason593060d2008-03-25 16:50:33 -04004108
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004109/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004110 * returns target flags in extended format or 0 if restripe for this
4111 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04004112 *
4113 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004114 */
4115static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
4116{
4117 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4118 u64 target = 0;
4119
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004120 if (!bctl)
4121 return 0;
4122
4123 if (flags & BTRFS_BLOCK_GROUP_DATA &&
4124 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4125 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
4126 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
4127 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4128 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
4129 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
4130 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4131 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
4132 }
4133
4134 return target;
4135}
4136
4137/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004138 * @flags: available profiles in extended format (see ctree.h)
4139 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004140 * Returns reduced profile in chunk format. If profile changing is in
4141 * progress (either running or paused) picks the target profile (if it's
4142 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004143 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004144static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04004145{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004146 u64 num_devices = fs_info->fs_devices->rw_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004147 u64 target;
Zhao Lei9c170b22015-09-15 21:08:08 +08004148 u64 raid_type;
4149 u64 allowed = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004150
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004151 /*
4152 * see if restripe for this chunk_type is in progress, if so
4153 * try to reduce to the target profile
4154 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004155 spin_lock(&fs_info->balance_lock);
4156 target = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004157 if (target) {
4158 /* pick target profile only if it's already available */
4159 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004160 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004161 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004162 }
4163 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004164 spin_unlock(&fs_info->balance_lock);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004165
David Woodhouse53b381b2013-01-29 18:40:14 -05004166 /* First, mask out the RAID levels which aren't possible */
Zhao Lei9c170b22015-09-15 21:08:08 +08004167 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
4168 if (num_devices >= btrfs_raid_array[raid_type].devs_min)
4169 allowed |= btrfs_raid_group[raid_type];
4170 }
4171 allowed &= flags;
Chris Masona061fc82008-05-07 11:43:44 -04004172
Zhao Lei9c170b22015-09-15 21:08:08 +08004173 if (allowed & BTRFS_BLOCK_GROUP_RAID6)
4174 allowed = BTRFS_BLOCK_GROUP_RAID6;
4175 else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
4176 allowed = BTRFS_BLOCK_GROUP_RAID5;
4177 else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
4178 allowed = BTRFS_BLOCK_GROUP_RAID10;
4179 else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
4180 allowed = BTRFS_BLOCK_GROUP_RAID1;
4181 else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
4182 allowed = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04004183
Zhao Lei9c170b22015-09-15 21:08:08 +08004184 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
Chris Masonec44a352008-04-28 15:29:52 -04004185
Zhao Lei9c170b22015-09-15 21:08:08 +08004186 return extended_to_chunk(flags | allowed);
Chris Masonec44a352008-04-28 15:29:52 -04004187}
4188
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004189static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05004190{
Miao Xiede98ced2013-01-29 10:13:12 +00004191 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004192 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004193
4194 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004195 flags = orig_flags;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004196 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00004197
4198 if (flags & BTRFS_BLOCK_GROUP_DATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004199 flags |= fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004200 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004201 flags |= fs_info->avail_system_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004202 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004203 flags |= fs_info->avail_metadata_alloc_bits;
4204 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02004205
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004206 return btrfs_reduce_alloc_profile(fs_info, flags);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004207}
Josef Bacik6a632092009-02-20 11:00:09 -05004208
Jeff Mahoney1b868262017-05-17 11:38:35 -04004209static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04004210{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004211 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004212 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05004213 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004214
Yan, Zhengb742bb82010-05-16 10:46:24 -04004215 if (data)
4216 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004217 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb82010-05-16 10:46:24 -04004218 flags = BTRFS_BLOCK_GROUP_SYSTEM;
4219 else
4220 flags = BTRFS_BLOCK_GROUP_METADATA;
4221
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004222 ret = get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05004223 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004224}
4225
Jeff Mahoney1b868262017-05-17 11:38:35 -04004226u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info)
4227{
4228 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_DATA);
4229}
4230
4231u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info)
4232{
4233 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4234}
4235
4236u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info)
4237{
4238 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4239}
4240
Liu Bo41361352017-02-13 15:42:21 -08004241static u64 btrfs_space_info_used(struct btrfs_space_info *s_info,
4242 bool may_use_included)
4243{
4244 ASSERT(s_info);
4245 return s_info->bytes_used + s_info->bytes_reserved +
4246 s_info->bytes_pinned + s_info->bytes_readonly +
4247 (may_use_included ? s_info->bytes_may_use : 0);
4248}
4249
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004250int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05004251{
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004252 struct btrfs_root *root = inode->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004253 struct btrfs_fs_info *fs_info = root->fs_info;
Nikolay Borisov1174cad2017-07-11 13:47:50 +03004254 struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
Josef Bacikab6e24102010-03-19 14:38:13 +00004255 u64 used;
Zhao Lei94b947b2015-02-14 13:23:45 +08004256 int ret = 0;
Zhao Leic99f1b02015-03-02 19:32:20 +08004257 int need_commit = 2;
4258 int have_pinned_space;
Josef Bacik6a632092009-02-20 11:00:09 -05004259
4260 /* make sure bytes are sectorsize aligned */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004261 bytes = ALIGN(bytes, fs_info->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05004262
Miao Xie9dced182013-10-25 17:33:36 +08004263 if (btrfs_is_free_space_inode(inode)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004264 need_commit = 0;
Miao Xie9dced182013-10-25 17:33:36 +08004265 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04004266 }
4267
Josef Bacik6a632092009-02-20 11:00:09 -05004268again:
4269 /* make sure we have enough space to handle the data first */
4270 spin_lock(&data_sinfo->lock);
Liu Bo41361352017-02-13 15:42:21 -08004271 used = btrfs_space_info_used(data_sinfo, true);
Josef Bacikab6e24102010-03-19 14:38:13 +00004272
4273 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004274 struct btrfs_trans_handle *trans;
4275
Josef Bacik6a632092009-02-20 11:00:09 -05004276 /*
4277 * if we don't have enough free bytes in this space then we need
4278 * to alloc a new chunk.
4279 */
Zhao Leib9fd47c2015-02-09 14:40:20 +08004280 if (!data_sinfo->full) {
Josef Bacik6a632092009-02-20 11:00:09 -05004281 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05004282
Chris Mason0e4f8f82011-04-15 16:05:44 -04004283 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05004284 spin_unlock(&data_sinfo->lock);
Nikolay Borisov1174cad2017-07-11 13:47:50 +03004285
Jeff Mahoney1b868262017-05-17 11:38:35 -04004286 alloc_target = btrfs_data_alloc_profile(fs_info);
Miao Xie9dced182013-10-25 17:33:36 +08004287 /*
4288 * It is ugly that we don't call nolock join
4289 * transaction for the free space inode case here.
4290 * But it is safe because we only do the data space
4291 * reservation for the free space cache in the
4292 * transaction context, the common join transaction
4293 * just increase the counter of the current transaction
4294 * handler, doesn't try to acquire the trans_lock of
4295 * the fs.
4296 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004297 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004298 if (IS_ERR(trans))
4299 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05004300
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004301 ret = do_chunk_alloc(trans, fs_info, alloc_target,
Chris Mason0e4f8f82011-04-15 16:05:44 -04004302 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004303 btrfs_end_transaction(trans);
Miao Xied52a5b52011-01-05 10:07:18 +00004304 if (ret < 0) {
4305 if (ret != -ENOSPC)
4306 return ret;
Zhao Leic99f1b02015-03-02 19:32:20 +08004307 else {
4308 have_pinned_space = 1;
Miao Xied52a5b52011-01-05 10:07:18 +00004309 goto commit_trans;
Zhao Leic99f1b02015-03-02 19:32:20 +08004310 }
Miao Xied52a5b52011-01-05 10:07:18 +00004311 }
Chris Mason33b4d472009-09-22 14:45:50 -04004312
Josef Bacik6a632092009-02-20 11:00:09 -05004313 goto again;
4314 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004315
4316 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04004317 * If we don't have enough pinned space to deal with this
Zhao Lei94b947b2015-02-14 13:23:45 +08004318 * allocation, and no removed chunk in current transaction,
4319 * don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004320 */
Zhao Leic99f1b02015-03-02 19:32:20 +08004321 have_pinned_space = percpu_counter_compare(
4322 &data_sinfo->total_bytes_pinned,
4323 used + bytes - data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05004324 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004325
4326 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00004327commit_trans:
Zhao Leic99f1b02015-03-02 19:32:20 +08004328 if (need_commit &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004329 !atomic_read(&fs_info->open_ioctl_trans)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004330 need_commit--;
Josef Bacikb150a4f2013-06-19 15:00:04 -04004331
Zhao Leie1746e82015-12-01 18:39:40 +08004332 if (need_commit > 0) {
4333 btrfs_start_delalloc_roots(fs_info, 0, -1);
Chris Mason6374e57a2017-06-23 09:48:21 -07004334 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004335 (u64)-1);
Zhao Leie1746e82015-12-01 18:39:40 +08004336 }
Zhao Lei9a4e7272015-04-09 12:34:43 +08004337
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004338 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004339 if (IS_ERR(trans))
4340 return PTR_ERR(trans);
Zhao Leic99f1b02015-03-02 19:32:20 +08004341 if (have_pinned_space >= 0 ||
Josef Bacik3204d332015-09-24 10:46:10 -04004342 test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
4343 &trans->transaction->flags) ||
Zhao Leic99f1b02015-03-02 19:32:20 +08004344 need_commit > 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004345 ret = btrfs_commit_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004346 if (ret)
4347 return ret;
Zhao Leid7c15172015-02-26 10:49:20 +08004348 /*
Filipe Mananac2d6cb12016-01-15 11:05:12 +00004349 * The cleaner kthread might still be doing iput
4350 * operations. Wait for it to finish so that
4351 * more space is released.
Zhao Leid7c15172015-02-26 10:49:20 +08004352 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004353 mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
4354 mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
Zhao Lei94b947b2015-02-14 13:23:45 +08004355 goto again;
4356 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004357 btrfs_end_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004358 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004359 }
4360
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004361 trace_btrfs_space_reservation(fs_info,
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004362 "space_info:enospc",
4363 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004364 return -ENOSPC;
4365 }
4366 data_sinfo->bytes_may_use += bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004367 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004368 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004369 spin_unlock(&data_sinfo->lock);
4370
Dongsheng Yang237c0e92014-12-29 06:23:05 -05004371 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004372}
4373
Qu Wenruo364ecf32017-02-27 15:10:38 +08004374int btrfs_check_data_free_space(struct inode *inode,
4375 struct extent_changeset **reserved, u64 start, u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004376{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004377 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004378 int ret;
4379
4380 /* align the range */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004381 len = round_up(start + len, fs_info->sectorsize) -
4382 round_down(start, fs_info->sectorsize);
4383 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004384
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004385 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004386 if (ret < 0)
4387 return ret;
4388
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004389 /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
Qu Wenruo364ecf32017-02-27 15:10:38 +08004390 ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
Qu Wenruo7bc329c2017-02-27 15:10:36 +08004391 if (ret < 0)
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004392 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004393 else
4394 ret = 0;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004395 return ret;
4396}
4397
4398/*
Qu Wenruo4ceff072015-09-08 17:22:42 +08004399 * Called if we need to clear a data reservation for this inode
4400 * Normally in a error case.
4401 *
Qu Wenruo51773be2015-10-08 18:19:37 +08004402 * This one will *NOT* use accurate qgroup reserved space API, just for case
4403 * which we can't sleep and is sure it won't affect qgroup reserved space.
4404 * Like clear_bit_hook().
Qu Wenruo4ceff072015-09-08 17:22:42 +08004405 */
Qu Wenruo51773be2015-10-08 18:19:37 +08004406void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
4407 u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004410 struct btrfs_space_info *data_sinfo;
4411
4412 /* Make sure the range is aligned to sectorsize */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004413 len = round_up(start + len, fs_info->sectorsize) -
4414 round_down(start, fs_info->sectorsize);
4415 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004416
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004417 data_sinfo = fs_info->data_sinfo;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004418 spin_lock(&data_sinfo->lock);
4419 if (WARN_ON(data_sinfo->bytes_may_use < len))
4420 data_sinfo->bytes_may_use = 0;
4421 else
4422 data_sinfo->bytes_may_use -= len;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004423 trace_btrfs_space_reservation(fs_info, "space_info",
Qu Wenruo4ceff072015-09-08 17:22:42 +08004424 data_sinfo->flags, len, 0);
4425 spin_unlock(&data_sinfo->lock);
4426}
4427
Qu Wenruo51773be2015-10-08 18:19:37 +08004428/*
4429 * Called if we need to clear a data reservation for this inode
4430 * Normally in a error case.
4431 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04004432 * This one will handle the per-inode data rsv map for accurate reserved
Qu Wenruo51773be2015-10-08 18:19:37 +08004433 * space framework.
4434 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08004435void btrfs_free_reserved_data_space(struct inode *inode,
4436 struct extent_changeset *reserved, u64 start, u64 len)
Qu Wenruo51773be2015-10-08 18:19:37 +08004437{
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004438 struct btrfs_root *root = BTRFS_I(inode)->root;
4439
4440 /* Make sure the range is aligned to sectorsize */
Jeff Mahoneyda170662016-06-15 09:22:56 -04004441 len = round_up(start + len, root->fs_info->sectorsize) -
4442 round_down(start, root->fs_info->sectorsize);
4443 start = round_down(start, root->fs_info->sectorsize);
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004444
Qu Wenruo51773be2015-10-08 18:19:37 +08004445 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruobc42bda2017-02-27 15:10:39 +08004446 btrfs_qgroup_free_data(inode, reserved, start, len);
Qu Wenruo51773be2015-10-08 18:19:37 +08004447}
4448
Josef Bacik97e728d2009-04-21 17:40:57 -04004449static void force_metadata_allocation(struct btrfs_fs_info *info)
4450{
4451 struct list_head *head = &info->space_info;
4452 struct btrfs_space_info *found;
4453
4454 rcu_read_lock();
4455 list_for_each_entry_rcu(found, head, list) {
4456 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004457 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04004458 }
4459 rcu_read_unlock();
4460}
4461
Miao Xie3c76cd82013-04-25 10:12:38 +00004462static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
4463{
4464 return (global->size << 1);
4465}
4466
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004467static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
Josef Bacik698d0082012-09-12 14:08:47 -04004468 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04004469{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004470 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004471 u64 bytes_used = btrfs_space_info_used(sinfo, false);
Chris Masone5bc2452010-10-26 13:37:56 -04004472 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04004473
Chris Mason0e4f8f82011-04-15 16:05:44 -04004474 if (force == CHUNK_ALLOC_FORCE)
4475 return 1;
4476
4477 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04004478 * We need to take into account the global rsv because for all intents
4479 * and purposes it's used space. Don't worry about locking the
4480 * global_rsv, it doesn't change except when the transaction commits.
4481 */
Josef Bacik54338b52012-08-14 16:20:52 -04004482 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004483 bytes_used += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04004484
4485 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04004486 * in limited mode, we want to have some free space up to
4487 * about 1% of the FS size.
4488 */
4489 if (force == CHUNK_ALLOC_LIMITED) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004490 thresh = btrfs_super_total_bytes(fs_info->super_copy);
Byongho Leeee221842015-12-15 01:42:10 +09004491 thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
Chris Mason0e4f8f82011-04-15 16:05:44 -04004492
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004493 if (sinfo->total_bytes - bytes_used < thresh)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004494 return 1;
4495 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004496
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004497 if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04004498 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04004499 return 1;
4500}
4501
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004502static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004503{
4504 u64 num_dev;
4505
David Woodhouse53b381b2013-01-29 18:40:14 -05004506 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
4507 BTRFS_BLOCK_GROUP_RAID0 |
4508 BTRFS_BLOCK_GROUP_RAID5 |
4509 BTRFS_BLOCK_GROUP_RAID6))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004510 num_dev = fs_info->fs_devices->rw_devices;
Liu Bo15d1ff82012-03-29 09:57:44 -04004511 else if (type & BTRFS_BLOCK_GROUP_RAID1)
4512 num_dev = 2;
4513 else
4514 num_dev = 1; /* DUP or single */
4515
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004516 return num_dev;
Liu Bo15d1ff82012-03-29 09:57:44 -04004517}
4518
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004519/*
4520 * If @is_allocation is true, reserve space in the system space info necessary
4521 * for allocating a chunk, otherwise if it's false, reserve space necessary for
4522 * removing a chunk.
4523 */
4524void check_system_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004525 struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004526{
4527 struct btrfs_space_info *info;
4528 u64 left;
4529 u64 thresh;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004530 int ret = 0;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004531 u64 num_devs;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004532
4533 /*
4534 * Needed because we can end up allocating a system chunk and for an
4535 * atomic and race free space reservation in the chunk block reserve.
4536 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004537 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
Liu Bo15d1ff82012-03-29 09:57:44 -04004538
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004539 info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
Liu Bo15d1ff82012-03-29 09:57:44 -04004540 spin_lock(&info->lock);
Liu Bo41361352017-02-13 15:42:21 -08004541 left = info->total_bytes - btrfs_space_info_used(info, true);
Liu Bo15d1ff82012-03-29 09:57:44 -04004542 spin_unlock(&info->lock);
4543
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004544 num_devs = get_profile_num_devs(fs_info, type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004545
4546 /* num_devs device items to update and 1 chunk item to add or remove */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004547 thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) +
4548 btrfs_calc_trans_metadata_size(fs_info, 1);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004549
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004550 if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
4551 btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
4552 left, thresh, type);
4553 dump_space_info(fs_info, info, 0, 0);
Liu Bo15d1ff82012-03-29 09:57:44 -04004554 }
4555
4556 if (left < thresh) {
Jeff Mahoney1b868262017-05-17 11:38:35 -04004557 u64 flags = btrfs_system_alloc_profile(fs_info);
Liu Bo15d1ff82012-03-29 09:57:44 -04004558
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004559 /*
4560 * Ignore failure to create system chunk. We might end up not
4561 * needing it, as we might not need to COW all nodes/leafs from
4562 * the paths we visit in the chunk tree (they were already COWed
4563 * or created in the current transaction for example).
4564 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004565 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004566 }
4567
4568 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004569 ret = btrfs_block_rsv_add(fs_info->chunk_root,
4570 &fs_info->chunk_block_rsv,
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004571 thresh, BTRFS_RESERVE_NO_FLUSH);
4572 if (!ret)
4573 trans->chunk_bytes_reserved += thresh;
Liu Bo15d1ff82012-03-29 09:57:44 -04004574 }
4575}
4576
Liu Bo28b737f2016-07-29 11:09:50 -07004577/*
4578 * If force is CHUNK_ALLOC_FORCE:
4579 * - return 1 if it successfully allocates a chunk,
4580 * - return errors including -ENOSPC otherwise.
4581 * If force is NOT CHUNK_ALLOC_FORCE:
4582 * - return 0 if it doesn't need to allocate a new chunk,
4583 * - return 1 if it successfully allocates a chunk,
4584 * - return errors including -ENOSPC otherwise.
4585 */
Chris Mason6324fbf2008-03-24 15:01:59 -04004586static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004587 struct btrfs_fs_info *fs_info, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04004588{
4589 struct btrfs_space_info *space_info;
Josef Bacik6d741192011-04-11 20:20:11 -04004590 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05004591 int ret = 0;
4592
Josef Bacikc6b305a2012-12-18 09:16:16 -05004593 /* Don't re-enter if we're already allocating a chunk */
4594 if (trans->allocating_chunk)
4595 return -ENOSPC;
4596
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004597 space_info = __find_space_info(fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04004598 if (!space_info) {
Nikolay Borisov2be12ef2017-05-22 09:35:49 +03004599 ret = create_space_info(fs_info, flags, &space_info);
4600 if (ret)
4601 return ret;
Chris Mason593060d2008-03-25 16:50:33 -04004602 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004603
Josef Bacik6d741192011-04-11 20:20:11 -04004604again:
Josef Bacik25179202008-10-29 14:49:05 -04004605 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05004606 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004607 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04004608 if (space_info->full) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004609 if (should_alloc_chunk(fs_info, space_info, force))
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004610 ret = -ENOSPC;
4611 else
4612 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04004613 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004614 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04004615 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004616
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004617 if (!should_alloc_chunk(fs_info, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04004618 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04004619 return 0;
4620 } else if (space_info->chunk_alloc) {
4621 wait_for_alloc = 1;
4622 } else {
4623 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04004624 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004625
Josef Bacik25179202008-10-29 14:49:05 -04004626 spin_unlock(&space_info->lock);
4627
Josef Bacik6d741192011-04-11 20:20:11 -04004628 mutex_lock(&fs_info->chunk_mutex);
4629
4630 /*
4631 * The chunk_mutex is held throughout the entirety of a chunk
4632 * allocation, so once we've acquired the chunk_mutex we know that the
4633 * other guy is done and we need to recheck and see if we should
4634 * allocate.
4635 */
4636 if (wait_for_alloc) {
4637 mutex_unlock(&fs_info->chunk_mutex);
4638 wait_for_alloc = 0;
4639 goto again;
4640 }
4641
Josef Bacikc6b305a2012-12-18 09:16:16 -05004642 trans->allocating_chunk = true;
4643
Josef Bacik97e728d2009-04-21 17:40:57 -04004644 /*
Josef Bacik67377732010-09-16 16:19:09 -04004645 * If we have mixed data/metadata chunks we want to make sure we keep
4646 * allocating mixed chunks instead of individual chunks.
4647 */
4648 if (btrfs_mixed_space_info(space_info))
4649 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
4650
4651 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04004652 * if we're doing a data chunk, go ahead and make sure that
4653 * we keep a reasonable number of metadata chunks allocated in the
4654 * FS as well.
4655 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04004656 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04004657 fs_info->data_chunk_allocations++;
4658 if (!(fs_info->data_chunk_allocations %
4659 fs_info->metadata_ratio))
4660 force_metadata_allocation(fs_info);
4661 }
4662
Liu Bo15d1ff82012-03-29 09:57:44 -04004663 /*
4664 * Check if we have enough space in SYSTEM chunk because we may need
4665 * to update devices.
4666 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004667 check_system_chunk(trans, fs_info, flags);
Liu Bo15d1ff82012-03-29 09:57:44 -04004668
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004669 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05004670 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004671
Josef Bacik9ed74f22009-09-11 16:12:44 -04004672 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004673 if (ret < 0 && ret != -ENOSPC)
4674 goto out;
Chris Masond3977122009-01-05 21:25:51 -05004675 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04004676 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04004677 else
4678 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04004679
Chris Mason0e4f8f82011-04-15 16:05:44 -04004680 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004681out:
Josef Bacik6d741192011-04-11 20:20:11 -04004682 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004683 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03004684 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana00d80e32015-07-20 14:56:20 +01004685 /*
4686 * When we allocate a new chunk we reserve space in the chunk block
4687 * reserve to make sure we can COW nodes/leafs in the chunk tree or
4688 * add new nodes/leafs to it if we end up needing to do it when
4689 * inserting the chunk item and updating device items as part of the
4690 * second phase of chunk allocation, performed by
4691 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
4692 * large number of new block groups to create in our transaction
4693 * handle's new_bgs list to avoid exhausting the chunk block reserve
4694 * in extreme cases - like having a single transaction create many new
4695 * block groups when starting to write out the free space caches of all
4696 * the block groups that were made dirty during the lifetime of the
4697 * transaction.
4698 */
Filipe Mananad9a05402015-10-03 13:13:13 +01004699 if (trans->can_flush_pending_bgs &&
Byongho Leeee221842015-12-15 01:42:10 +09004700 trans->chunk_bytes_reserved >= (u64)SZ_2M) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004701 btrfs_create_pending_block_groups(trans, fs_info);
Filipe Manana00d80e32015-07-20 14:56:20 +01004702 btrfs_trans_release_chunk_metadata(trans);
4703 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004704 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04004705}
4706
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004707static int can_overcommit(struct btrfs_fs_info *fs_info,
Josef Bacika80c8dc2012-09-06 16:59:33 -04004708 struct btrfs_space_info *space_info, u64 bytes,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004709 enum btrfs_reserve_flush_enum flush,
4710 bool system_chunk)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004711{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004712 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04004713 u64 profile;
Miao Xie3c76cd82013-04-25 10:12:38 +00004714 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004715 u64 avail;
4716 u64 used;
4717
Josef Bacik957780e2016-05-17 13:30:55 -04004718 /* Don't overcommit when in mixed mode. */
4719 if (space_info->flags & BTRFS_BLOCK_GROUP_DATA)
4720 return 0;
4721
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004722 if (system_chunk)
4723 profile = btrfs_system_alloc_profile(fs_info);
4724 else
4725 profile = btrfs_metadata_alloc_profile(fs_info);
4726
Liu Bo41361352017-02-13 15:42:21 -08004727 used = btrfs_space_info_used(space_info, false);
Josef Bacik96f1bb52013-01-30 17:02:51 -05004728
Josef Bacik96f1bb52013-01-30 17:02:51 -05004729 /*
4730 * We only want to allow over committing if we have lots of actual space
4731 * free, but if we don't have enough space to handle the global reserve
4732 * space then we could end up having a real enospc problem when trying
4733 * to allocate a chunk or some other such important allocation.
4734 */
Miao Xie3c76cd82013-04-25 10:12:38 +00004735 spin_lock(&global_rsv->lock);
4736 space_size = calc_global_rsv_need_space(global_rsv);
4737 spin_unlock(&global_rsv->lock);
4738 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05004739 return 0;
4740
4741 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004742
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004743 avail = atomic64_read(&fs_info->free_chunk_space);
Josef Bacika80c8dc2012-09-06 16:59:33 -04004744
4745 /*
4746 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05004747 * space is actually useable. For raid56, the space info used
4748 * doesn't include the parity drive, so we don't have to
4749 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04004750 */
4751 if (profile & (BTRFS_BLOCK_GROUP_DUP |
4752 BTRFS_BLOCK_GROUP_RAID1 |
4753 BTRFS_BLOCK_GROUP_RAID10))
4754 avail >>= 1;
4755
4756 /*
Miao Xie561c2942012-10-16 11:32:18 +00004757 * If we aren't flushing all things, let us overcommit up to
4758 * 1/2th of the space. If we can flush, don't let us overcommit
4759 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04004760 */
Miao Xie08e007d2012-10-16 11:33:38 +00004761 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04004762 avail >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004763 else
Josef Bacik14575ae2013-09-17 10:48:00 -04004764 avail >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004765
Josef Bacik14575ae2013-09-17 10:48:00 -04004766 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004767 return 1;
4768 return 0;
4769}
4770
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004771static void btrfs_writeback_inodes_sb_nr(struct btrfs_fs_info *fs_info,
Miao Xie6c255e62014-03-06 13:55:01 +08004772 unsigned long nr_pages, int nr_items)
Miao Xieda633a42012-12-20 11:19:09 +00004773{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004774 struct super_block *sb = fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00004775
Josef Bacik925a6ef2013-06-20 12:31:27 -04004776 if (down_read_trylock(&sb->s_umount)) {
4777 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4778 up_read(&sb->s_umount);
4779 } else {
Miao Xieda633a42012-12-20 11:19:09 +00004780 /*
4781 * We needn't worry the filesystem going from r/w to r/o though
4782 * we don't acquire ->s_umount mutex, because the filesystem
4783 * should guarantee the delalloc inodes list be empty after
4784 * the filesystem is readonly(all dirty pages are written to
4785 * the disk).
4786 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004787 btrfs_start_delalloc_roots(fs_info, 0, nr_items);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004788 if (!current->journal_info)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004789 btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1);
Miao Xieda633a42012-12-20 11:19:09 +00004790 }
4791}
4792
Chris Mason6374e57a2017-06-23 09:48:21 -07004793static inline u64 calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004794 u64 to_reclaim)
Miao Xie18cd8ea2013-11-04 23:13:22 +08004795{
4796 u64 bytes;
Chris Mason6374e57a2017-06-23 09:48:21 -07004797 u64 nr;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004798
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004799 bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Chris Mason6374e57a2017-06-23 09:48:21 -07004800 nr = div64_u64(to_reclaim, bytes);
Miao Xie18cd8ea2013-11-04 23:13:22 +08004801 if (!nr)
4802 nr = 1;
4803 return nr;
4804}
4805
Byongho Leeee221842015-12-15 01:42:10 +09004806#define EXTENT_SIZE_PER_ITEM SZ_256K
Miao Xiec61a16a2013-11-04 23:13:23 +08004807
Yan, Zheng5da9d012010-05-16 10:46:25 -04004808/*
4809 * shrink metadata reservation for delalloc
4810 */
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004811static void shrink_delalloc(struct btrfs_fs_info *fs_info, u64 to_reclaim,
4812 u64 orig, bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004813{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004814 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004815 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004816 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004817 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004818 u64 max_reclaim;
Chris Mason6374e57a2017-06-23 09:48:21 -07004819 u64 items;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004820 long time_left;
Miao Xied3ee29e32013-11-04 23:13:20 +08004821 unsigned long nr_pages;
4822 int loops;
Miao Xie08e007d2012-10-16 11:33:38 +00004823 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004824
Miao Xiec61a16a2013-11-04 23:13:23 +08004825 /* Calc the number of the pages we need flush for space reservation */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004826 items = calc_reclaim_items_nr(fs_info, to_reclaim);
Chris Mason6374e57a2017-06-23 09:48:21 -07004827 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004828
Josef Bacik663350a2011-11-03 22:54:25 -04004829 trans = (struct btrfs_trans_handle *)current->journal_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004830 block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004831 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04004832
Miao Xie963d6782013-01-29 10:10:51 +00004833 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004834 &fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004835 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004836 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004837 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004838 if (wait_ordered)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004839 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004840 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004841 }
4842
Miao Xied3ee29e32013-11-04 23:13:20 +08004843 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004844 while (delalloc_bytes && loops < 3) {
4845 max_reclaim = min(delalloc_bytes, to_reclaim);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004846 nr_pages = max_reclaim >> PAGE_SHIFT;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004847 btrfs_writeback_inodes_sb_nr(fs_info, nr_pages, items);
Josef Bacikdea31f52012-09-06 16:47:00 -04004848 /*
4849 * We need to wait for the async pages to actually start before
4850 * we do anything.
4851 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004852 max_reclaim = atomic_read(&fs_info->async_delalloc_pages);
Miao Xie9f3a0742013-11-04 23:13:24 +08004853 if (!max_reclaim)
4854 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004855
Miao Xie9f3a0742013-11-04 23:13:24 +08004856 if (max_reclaim <= nr_pages)
4857 max_reclaim = 0;
4858 else
4859 max_reclaim -= nr_pages;
4860
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004861 wait_event(fs_info->async_submit_wait,
4862 atomic_read(&fs_info->async_delalloc_pages) <=
Miao Xie9f3a0742013-11-04 23:13:24 +08004863 (int)max_reclaim);
4864skip_async:
Miao Xie08e007d2012-10-16 11:33:38 +00004865 if (!trans)
4866 flush = BTRFS_RESERVE_FLUSH_ALL;
4867 else
4868 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04004869 spin_lock(&space_info->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04004870 if (list_empty(&space_info->tickets) &&
4871 list_empty(&space_info->priority_tickets)) {
4872 spin_unlock(&space_info->lock);
4873 break;
4874 }
Josef Bacik0019f102010-10-15 15:18:40 -04004875 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004876
Chris Mason36e39c42011-03-12 07:08:42 -05004877 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004878 if (wait_ordered && !trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004879 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf104d042011-10-14 13:56:58 -04004880 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004881 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004882 if (time_left)
4883 break;
4884 }
Miao Xie963d6782013-01-29 10:10:51 +00004885 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004886 &fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004887 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004888}
4889
Josef Bacik996478c2017-08-22 16:00:39 -04004890struct reserve_ticket {
4891 u64 bytes;
4892 int error;
4893 struct list_head list;
4894 wait_queue_head_t wait;
4895};
4896
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004897/**
Josef Bacik663350a2011-11-03 22:54:25 -04004898 * maybe_commit_transaction - possibly commit the transaction if its ok to
4899 * @root - the root we're allocating for
4900 * @bytes - the number of bytes we want to reserve
4901 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004902 *
Josef Bacik663350a2011-11-03 22:54:25 -04004903 * This will check to make sure that committing the transaction will actually
4904 * get us somewhere and then commit the transaction if it does. Otherwise it
4905 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004906 */
Jeff Mahoney0c9ab342017-02-15 16:28:28 -05004907static int may_commit_transaction(struct btrfs_fs_info *fs_info,
Josef Bacik996478c2017-08-22 16:00:39 -04004908 struct btrfs_space_info *space_info)
Josef Bacik663350a2011-11-03 22:54:25 -04004909{
Josef Bacik996478c2017-08-22 16:00:39 -04004910 struct reserve_ticket *ticket = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004911 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;
Josef Bacik663350a2011-11-03 22:54:25 -04004912 struct btrfs_trans_handle *trans;
Josef Bacik996478c2017-08-22 16:00:39 -04004913 u64 bytes;
Josef Bacik663350a2011-11-03 22:54:25 -04004914
4915 trans = (struct btrfs_trans_handle *)current->journal_info;
4916 if (trans)
4917 return -EAGAIN;
4918
Josef Bacik996478c2017-08-22 16:00:39 -04004919 spin_lock(&space_info->lock);
4920 if (!list_empty(&space_info->priority_tickets))
4921 ticket = list_first_entry(&space_info->priority_tickets,
4922 struct reserve_ticket, list);
4923 else if (!list_empty(&space_info->tickets))
4924 ticket = list_first_entry(&space_info->tickets,
4925 struct reserve_ticket, list);
4926 bytes = (ticket) ? ticket->bytes : 0;
4927 spin_unlock(&space_info->lock);
4928
4929 if (!bytes)
4930 return 0;
Josef Bacik663350a2011-11-03 22:54:25 -04004931
4932 /* See if there is enough pinned space to make this reservation */
Josef Bacikb150a4f2013-06-19 15:00:04 -04004933 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Miao Xie0424c542014-03-06 13:54:59 +08004934 bytes) >= 0)
Josef Bacik663350a2011-11-03 22:54:25 -04004935 goto commit;
Josef Bacik663350a2011-11-03 22:54:25 -04004936
4937 /*
4938 * See if there is some space in the delayed insertion reservation for
4939 * this reservation.
4940 */
4941 if (space_info != delayed_rsv->space_info)
4942 return -ENOSPC;
4943
4944 spin_lock(&delayed_rsv->lock);
Josef Bacik996478c2017-08-22 16:00:39 -04004945 if (delayed_rsv->size > bytes)
4946 bytes = 0;
4947 else
4948 bytes -= delayed_rsv->size;
Josef Bacikb150a4f2013-06-19 15:00:04 -04004949 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Josef Bacik996478c2017-08-22 16:00:39 -04004950 bytes) < 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004951 spin_unlock(&delayed_rsv->lock);
4952 return -ENOSPC;
4953 }
4954 spin_unlock(&delayed_rsv->lock);
4955
4956commit:
Jeff Mahoneya9b33112017-05-17 11:38:34 -04004957 trans = btrfs_join_transaction(fs_info->extent_root);
Josef Bacik663350a2011-11-03 22:54:25 -04004958 if (IS_ERR(trans))
4959 return -ENOSPC;
4960
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004961 return btrfs_commit_transaction(trans);
Josef Bacik663350a2011-11-03 22:54:25 -04004962}
4963
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03004964/*
4965 * Try to flush some data based on policy set by @state. This is only advisory
4966 * and may fail for various reasons. The caller is supposed to examine the
4967 * state of @space_info to detect the outcome.
4968 */
4969static void flush_space(struct btrfs_fs_info *fs_info,
Josef Bacik96c3f432012-06-21 14:05:49 -04004970 struct btrfs_space_info *space_info, u64 num_bytes,
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03004971 int state)
Josef Bacik96c3f432012-06-21 14:05:49 -04004972{
Jeff Mahoneya9b33112017-05-17 11:38:34 -04004973 struct btrfs_root *root = fs_info->extent_root;
Josef Bacik96c3f432012-06-21 14:05:49 -04004974 struct btrfs_trans_handle *trans;
4975 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004976 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004977
4978 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004979 case FLUSH_DELAYED_ITEMS_NR:
4980 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08004981 if (state == FLUSH_DELAYED_ITEMS_NR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004982 nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004983 else
Josef Bacik96c3f432012-06-21 14:05:49 -04004984 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004985
Josef Bacik96c3f432012-06-21 14:05:49 -04004986 trans = btrfs_join_transaction(root);
4987 if (IS_ERR(trans)) {
4988 ret = PTR_ERR(trans);
4989 break;
4990 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004991 ret = btrfs_run_delayed_items_nr(trans, fs_info, nr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004992 btrfs_end_transaction(trans);
Josef Bacik96c3f432012-06-21 14:05:49 -04004993 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004994 case FLUSH_DELALLOC:
4995 case FLUSH_DELALLOC_WAIT:
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03004996 shrink_delalloc(fs_info, num_bytes * 2, num_bytes,
Josef Bacik67b0fd62012-09-24 13:42:00 -04004997 state == FLUSH_DELALLOC_WAIT);
4998 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004999 case ALLOC_CHUNK:
5000 trans = btrfs_join_transaction(root);
5001 if (IS_ERR(trans)) {
5002 ret = PTR_ERR(trans);
5003 break;
5004 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005005 ret = do_chunk_alloc(trans, fs_info,
Jeff Mahoney1b868262017-05-17 11:38:35 -04005006 btrfs_metadata_alloc_profile(fs_info),
Josef Bacikea658ba2012-09-11 16:57:25 -04005007 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005008 btrfs_end_transaction(trans);
Alex Lyakaseecba892015-12-06 12:32:31 +02005009 if (ret > 0 || ret == -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04005010 ret = 0;
5011 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04005012 case COMMIT_TRANS:
Josef Bacik996478c2017-08-22 16:00:39 -04005013 ret = may_commit_transaction(fs_info, space_info);
Josef Bacik96c3f432012-06-21 14:05:49 -04005014 break;
5015 default:
5016 ret = -ENOSPC;
5017 break;
5018 }
5019
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005020 trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, state,
5021 ret);
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03005022 return;
Josef Bacik96c3f432012-06-21 14:05:49 -04005023}
Miao Xie21c7e752014-05-13 17:29:04 -07005024
5025static inline u64
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005026btrfs_calc_reclaim_metadata_size(struct btrfs_fs_info *fs_info,
5027 struct btrfs_space_info *space_info,
5028 bool system_chunk)
Miao Xie21c7e752014-05-13 17:29:04 -07005029{
Josef Bacik957780e2016-05-17 13:30:55 -04005030 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07005031 u64 used;
5032 u64 expected;
Josef Bacik957780e2016-05-17 13:30:55 -04005033 u64 to_reclaim = 0;
Miao Xie21c7e752014-05-13 17:29:04 -07005034
Josef Bacik957780e2016-05-17 13:30:55 -04005035 list_for_each_entry(ticket, &space_info->tickets, list)
5036 to_reclaim += ticket->bytes;
5037 list_for_each_entry(ticket, &space_info->priority_tickets, list)
5038 to_reclaim += ticket->bytes;
5039 if (to_reclaim)
5040 return to_reclaim;
Miao Xie21c7e752014-05-13 17:29:04 -07005041
Wang Xiaoguange0af2482016-08-31 19:46:16 +08005042 to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005043 if (can_overcommit(fs_info, space_info, to_reclaim,
5044 BTRFS_RESERVE_FLUSH_ALL, system_chunk))
Wang Xiaoguange0af2482016-08-31 19:46:16 +08005045 return 0;
5046
Nikolay Borisov0eee8a42017-06-14 11:35:34 +03005047 used = btrfs_space_info_used(space_info, true);
5048
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005049 if (can_overcommit(fs_info, space_info, SZ_1M,
5050 BTRFS_RESERVE_FLUSH_ALL, system_chunk))
Miao Xie21c7e752014-05-13 17:29:04 -07005051 expected = div_factor_fine(space_info->total_bytes, 95);
5052 else
5053 expected = div_factor_fine(space_info->total_bytes, 90);
5054
5055 if (used > expected)
5056 to_reclaim = used - expected;
5057 else
5058 to_reclaim = 0;
5059 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
5060 space_info->bytes_reserved);
Miao Xie21c7e752014-05-13 17:29:04 -07005061 return to_reclaim;
5062}
5063
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005064static inline int need_do_async_reclaim(struct btrfs_fs_info *fs_info,
5065 struct btrfs_space_info *space_info,
5066 u64 used, bool system_chunk)
Miao Xie21c7e752014-05-13 17:29:04 -07005067{
Josef Bacik365c5312015-02-18 13:58:15 -08005068 u64 thresh = div_factor_fine(space_info->total_bytes, 98);
5069
5070 /* If we're just plain full then async reclaim just slows us down. */
Josef Bacikbaee8792016-01-26 09:35:38 -05005071 if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh)
Josef Bacik365c5312015-02-18 13:58:15 -08005072 return 0;
5073
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005074 if (!btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5075 system_chunk))
Liu Bo25ce4592014-09-10 12:58:50 +08005076 return 0;
Liu Bo25ce4592014-09-10 12:58:50 +08005077
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005078 return (used >= thresh && !btrfs_fs_closing(fs_info) &&
5079 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
Miao Xie21c7e752014-05-13 17:29:04 -07005080}
5081
Josef Bacik957780e2016-05-17 13:30:55 -04005082static void wake_all_tickets(struct list_head *head)
Miao Xie21c7e752014-05-13 17:29:04 -07005083{
Josef Bacik957780e2016-05-17 13:30:55 -04005084 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07005085
Josef Bacik957780e2016-05-17 13:30:55 -04005086 while (!list_empty(head)) {
5087 ticket = list_first_entry(head, struct reserve_ticket, list);
5088 list_del_init(&ticket->list);
5089 ticket->error = -ENOSPC;
5090 wake_up(&ticket->wait);
Miao Xie21c7e752014-05-13 17:29:04 -07005091 }
Miao Xie21c7e752014-05-13 17:29:04 -07005092}
5093
Josef Bacik957780e2016-05-17 13:30:55 -04005094/*
5095 * This is for normal flushers, we can wait all goddamned day if we want to. We
5096 * will loop and continuously try to flush as long as we are making progress.
5097 * We count progress as clearing off tickets each time we have to loop.
5098 */
Miao Xie21c7e752014-05-13 17:29:04 -07005099static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
5100{
5101 struct btrfs_fs_info *fs_info;
5102 struct btrfs_space_info *space_info;
5103 u64 to_reclaim;
5104 int flush_state;
Josef Bacik957780e2016-05-17 13:30:55 -04005105 int commit_cycles = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005106 u64 last_tickets_id;
Miao Xie21c7e752014-05-13 17:29:04 -07005107
5108 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
5109 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
5110
Josef Bacik957780e2016-05-17 13:30:55 -04005111 spin_lock(&space_info->lock);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005112 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5113 false);
Josef Bacik957780e2016-05-17 13:30:55 -04005114 if (!to_reclaim) {
5115 space_info->flush = 0;
5116 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005117 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005118 }
Wang Xiaoguangce129652016-09-02 10:58:46 +08005119 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005120 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005121
5122 flush_state = FLUSH_DELAYED_ITEMS_NR;
5123 do {
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03005124 flush_space(fs_info, space_info, to_reclaim, flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005125 spin_lock(&space_info->lock);
5126 if (list_empty(&space_info->tickets)) {
5127 space_info->flush = 0;
5128 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005129 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005130 }
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005131 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info,
5132 space_info,
5133 false);
Wang Xiaoguangce129652016-09-02 10:58:46 +08005134 if (last_tickets_id == space_info->tickets_id) {
Josef Bacik957780e2016-05-17 13:30:55 -04005135 flush_state++;
5136 } else {
Wang Xiaoguangce129652016-09-02 10:58:46 +08005137 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005138 flush_state = FLUSH_DELAYED_ITEMS_NR;
5139 if (commit_cycles)
5140 commit_cycles--;
5141 }
5142
5143 if (flush_state > COMMIT_TRANS) {
5144 commit_cycles++;
5145 if (commit_cycles > 2) {
5146 wake_all_tickets(&space_info->tickets);
5147 space_info->flush = 0;
5148 } else {
5149 flush_state = FLUSH_DELAYED_ITEMS_NR;
5150 }
5151 }
5152 spin_unlock(&space_info->lock);
5153 } while (flush_state <= COMMIT_TRANS);
Miao Xie21c7e752014-05-13 17:29:04 -07005154}
5155
5156void btrfs_init_async_reclaim_work(struct work_struct *work)
5157{
5158 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
5159}
5160
Josef Bacik957780e2016-05-17 13:30:55 -04005161static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
5162 struct btrfs_space_info *space_info,
5163 struct reserve_ticket *ticket)
5164{
5165 u64 to_reclaim;
5166 int flush_state = FLUSH_DELAYED_ITEMS_NR;
5167
5168 spin_lock(&space_info->lock);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005169 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5170 false);
Josef Bacik957780e2016-05-17 13:30:55 -04005171 if (!to_reclaim) {
5172 spin_unlock(&space_info->lock);
5173 return;
5174 }
5175 spin_unlock(&space_info->lock);
5176
5177 do {
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005178 flush_space(fs_info, space_info, to_reclaim, flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005179 flush_state++;
5180 spin_lock(&space_info->lock);
5181 if (ticket->bytes == 0) {
5182 spin_unlock(&space_info->lock);
5183 return;
5184 }
5185 spin_unlock(&space_info->lock);
5186
5187 /*
5188 * Priority flushers can't wait on delalloc without
5189 * deadlocking.
5190 */
5191 if (flush_state == FLUSH_DELALLOC ||
5192 flush_state == FLUSH_DELALLOC_WAIT)
5193 flush_state = ALLOC_CHUNK;
5194 } while (flush_state < COMMIT_TRANS);
5195}
5196
5197static int wait_reserve_ticket(struct btrfs_fs_info *fs_info,
5198 struct btrfs_space_info *space_info,
5199 struct reserve_ticket *ticket, u64 orig_bytes)
5200
5201{
5202 DEFINE_WAIT(wait);
5203 int ret = 0;
5204
5205 spin_lock(&space_info->lock);
5206 while (ticket->bytes > 0 && ticket->error == 0) {
5207 ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE);
5208 if (ret) {
5209 ret = -EINTR;
5210 break;
5211 }
5212 spin_unlock(&space_info->lock);
5213
5214 schedule();
5215
5216 finish_wait(&ticket->wait, &wait);
5217 spin_lock(&space_info->lock);
5218 }
5219 if (!ret)
5220 ret = ticket->error;
5221 if (!list_empty(&ticket->list))
5222 list_del_init(&ticket->list);
5223 if (ticket->bytes && ticket->bytes < orig_bytes) {
5224 u64 num_bytes = orig_bytes - ticket->bytes;
5225 space_info->bytes_may_use -= num_bytes;
5226 trace_btrfs_space_reservation(fs_info, "space_info",
5227 space_info->flags, num_bytes, 0);
5228 }
5229 spin_unlock(&space_info->lock);
5230
5231 return ret;
5232}
5233
Josef Bacik663350a2011-11-03 22:54:25 -04005234/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005235 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5236 * @root - the root we're allocating for
Josef Bacik957780e2016-05-17 13:30:55 -04005237 * @space_info - the space info we want to allocate from
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005238 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04005239 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005240 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04005241 * This will reserve orig_bytes number of bytes from the space info associated
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005242 * with the block_rsv. If there is not enough space it will make an attempt to
5243 * flush out space to make room. It will do this by flushing delalloc if
5244 * possible or committing the transaction. If flush is 0 then no attempts to
5245 * regain reservations will be made and this will fail if there is not enough
5246 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005247 */
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005248static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
Josef Bacik957780e2016-05-17 13:30:55 -04005249 struct btrfs_space_info *space_info,
5250 u64 orig_bytes,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005251 enum btrfs_reserve_flush_enum flush,
5252 bool system_chunk)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005253{
Josef Bacik957780e2016-05-17 13:30:55 -04005254 struct reserve_ticket ticket;
Josef Bacik2bf64752011-09-26 17:12:22 -04005255 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005256 int ret = 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005257
Josef Bacik957780e2016-05-17 13:30:55 -04005258 ASSERT(orig_bytes);
Josef Bacik8ca17f02016-05-27 13:24:13 -04005259 ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL);
5260
Yan, Zhengf0486c62010-05-16 10:46:25 -04005261 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04005262 ret = -ENOSPC;
Liu Bo41361352017-02-13 15:42:21 -08005263 used = btrfs_space_info_used(space_info, true);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005264
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005265 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005266 * If we have enough space then hooray, make our reservation and carry
5267 * on. If not see if we can overcommit, and if we can, hooray carry on.
5268 * If not things get more complicated.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005269 */
Josef Bacik957780e2016-05-17 13:30:55 -04005270 if (used + orig_bytes <= space_info->total_bytes) {
5271 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005272 trace_btrfs_space_reservation(fs_info, "space_info",
5273 space_info->flags, orig_bytes, 1);
Josef Bacik957780e2016-05-17 13:30:55 -04005274 ret = 0;
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005275 } else if (can_overcommit(fs_info, space_info, orig_bytes, flush,
5276 system_chunk)) {
Josef Bacik44734ed2012-09-28 16:04:19 -04005277 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005278 trace_btrfs_space_reservation(fs_info, "space_info",
5279 space_info->flags, orig_bytes, 1);
Josef Bacik44734ed2012-09-28 16:04:19 -04005280 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04005281 }
5282
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005283 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005284 * If we couldn't make a reservation then setup our reservation ticket
5285 * and kick the async worker if it's not already running.
Miao Xie08e007d2012-10-16 11:33:38 +00005286 *
Josef Bacik957780e2016-05-17 13:30:55 -04005287 * If we are a priority flusher then we just need to add our ticket to
5288 * the list and we will do our own flushing further down.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005289 */
Josef Bacik72bcd992012-12-18 15:16:34 -05005290 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacik957780e2016-05-17 13:30:55 -04005291 ticket.bytes = orig_bytes;
5292 ticket.error = 0;
5293 init_waitqueue_head(&ticket.wait);
5294 if (flush == BTRFS_RESERVE_FLUSH_ALL) {
5295 list_add_tail(&ticket.list, &space_info->tickets);
5296 if (!space_info->flush) {
5297 space_info->flush = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005298 trace_btrfs_trigger_flush(fs_info,
Josef Bacikf376df22016-03-25 13:25:56 -04005299 space_info->flags,
5300 orig_bytes, flush,
5301 "enospc");
Josef Bacik957780e2016-05-17 13:30:55 -04005302 queue_work(system_unbound_wq,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005303 &fs_info->async_reclaim_work);
Josef Bacik957780e2016-05-17 13:30:55 -04005304 }
5305 } else {
5306 list_add_tail(&ticket.list,
5307 &space_info->priority_tickets);
5308 }
Miao Xie21c7e752014-05-13 17:29:04 -07005309 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
5310 used += orig_bytes;
Josef Bacikf6acfd52014-09-18 11:27:17 -04005311 /*
5312 * We will do the space reservation dance during log replay,
5313 * which means we won't have fs_info->fs_root set, so don't do
5314 * the async reclaim as we will panic.
5315 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005316 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) &&
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005317 need_do_async_reclaim(fs_info, space_info,
5318 used, system_chunk) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005319 !work_busy(&fs_info->async_reclaim_work)) {
5320 trace_btrfs_trigger_flush(fs_info, space_info->flags,
5321 orig_bytes, flush, "preempt");
Miao Xie21c7e752014-05-13 17:29:04 -07005322 queue_work(system_unbound_wq,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005323 &fs_info->async_reclaim_work);
Josef Bacikf376df22016-03-25 13:25:56 -04005324 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005325 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005326 spin_unlock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00005327 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik957780e2016-05-17 13:30:55 -04005328 return ret;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005329
Josef Bacik957780e2016-05-17 13:30:55 -04005330 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005331 return wait_reserve_ticket(fs_info, space_info, &ticket,
Josef Bacik957780e2016-05-17 13:30:55 -04005332 orig_bytes);
Miao Xie08e007d2012-10-16 11:33:38 +00005333
Josef Bacik957780e2016-05-17 13:30:55 -04005334 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005335 priority_reclaim_metadata_space(fs_info, space_info, &ticket);
Josef Bacik957780e2016-05-17 13:30:55 -04005336 spin_lock(&space_info->lock);
5337 if (ticket.bytes) {
5338 if (ticket.bytes < orig_bytes) {
5339 u64 num_bytes = orig_bytes - ticket.bytes;
5340 space_info->bytes_may_use -= num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005341 trace_btrfs_space_reservation(fs_info, "space_info",
5342 space_info->flags,
5343 num_bytes, 0);
Miao Xie08e007d2012-10-16 11:33:38 +00005344
Josef Bacik957780e2016-05-17 13:30:55 -04005345 }
5346 list_del_init(&ticket.list);
5347 ret = -ENOSPC;
5348 }
5349 spin_unlock(&space_info->lock);
5350 ASSERT(list_empty(&ticket.list));
5351 return ret;
5352}
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005353
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005354/**
5355 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5356 * @root - the root we're allocating for
5357 * @block_rsv - the block_rsv we're allocating for
5358 * @orig_bytes - the number of bytes we want
5359 * @flush - whether or not we can flush to make our reservation
5360 *
Josef Bacik957780e2016-05-17 13:30:55 -04005361 * This will reserve orgi_bytes number of bytes from the space info associated
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005362 * with the block_rsv. If there is not enough space it will make an attempt to
5363 * flush out space to make room. It will do this by flushing delalloc if
5364 * possible or committing the transaction. If flush is 0 then no attempts to
5365 * regain reservations will be made and this will fail if there is not enough
5366 * space already.
5367 */
5368static int reserve_metadata_bytes(struct btrfs_root *root,
5369 struct btrfs_block_rsv *block_rsv,
5370 u64 orig_bytes,
5371 enum btrfs_reserve_flush_enum flush)
5372{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005373 struct btrfs_fs_info *fs_info = root->fs_info;
5374 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04005375 int ret;
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005376 bool system_chunk = (root == fs_info->chunk_root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005377
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005378 ret = __reserve_metadata_bytes(fs_info, block_rsv->space_info,
5379 orig_bytes, flush, system_chunk);
Josef Bacik5d803662013-02-07 16:06:02 -05005380 if (ret == -ENOSPC &&
5381 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
Josef Bacik5d803662013-02-07 16:06:02 -05005382 if (block_rsv != global_rsv &&
5383 !block_rsv_use_bytes(global_rsv, orig_bytes))
5384 ret = 0;
5385 }
Jeff Mahoneycab45e22013-10-16 16:27:01 -04005386 if (ret == -ENOSPC)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005387 trace_btrfs_space_reservation(fs_info, "space_info:enospc",
Josef Bacik957780e2016-05-17 13:30:55 -04005388 block_rsv->space_info->flags,
5389 orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005390 return ret;
5391}
5392
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005393static struct btrfs_block_rsv *get_block_rsv(
5394 const struct btrfs_trans_handle *trans,
5395 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005396{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005397 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik4c13d752011-08-30 11:31:29 -04005398 struct btrfs_block_rsv *block_rsv = NULL;
5399
Alexandru Moisee9cf4392015-09-09 00:18:50 +00005400 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005401 (root == fs_info->csum_root && trans->adding_csums) ||
5402 (root == fs_info->uuid_root))
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02005403 block_rsv = trans->block_rsv;
5404
Josef Bacik4c13d752011-08-30 11:31:29 -04005405 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005406 block_rsv = root->block_rsv;
5407
5408 if (!block_rsv)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005409 block_rsv = &fs_info->empty_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005410
5411 return block_rsv;
5412}
5413
5414static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
5415 u64 num_bytes)
5416{
5417 int ret = -ENOSPC;
5418 spin_lock(&block_rsv->lock);
5419 if (block_rsv->reserved >= num_bytes) {
5420 block_rsv->reserved -= num_bytes;
5421 if (block_rsv->reserved < block_rsv->size)
5422 block_rsv->full = 0;
5423 ret = 0;
5424 }
5425 spin_unlock(&block_rsv->lock);
5426 return ret;
5427}
5428
5429static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
5430 u64 num_bytes, int update_size)
5431{
5432 spin_lock(&block_rsv->lock);
5433 block_rsv->reserved += num_bytes;
5434 if (update_size)
5435 block_rsv->size += num_bytes;
5436 else if (block_rsv->reserved >= block_rsv->size)
5437 block_rsv->full = 1;
5438 spin_unlock(&block_rsv->lock);
5439}
5440
Josef Bacikd52be812013-05-29 14:54:47 -04005441int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
5442 struct btrfs_block_rsv *dest, u64 num_bytes,
5443 int min_factor)
5444{
5445 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5446 u64 min_bytes;
5447
5448 if (global_rsv->space_info != dest->space_info)
5449 return -ENOSPC;
5450
5451 spin_lock(&global_rsv->lock);
5452 min_bytes = div_factor(global_rsv->size, min_factor);
5453 if (global_rsv->reserved < min_bytes + num_bytes) {
5454 spin_unlock(&global_rsv->lock);
5455 return -ENOSPC;
5456 }
5457 global_rsv->reserved -= num_bytes;
5458 if (global_rsv->reserved < global_rsv->size)
5459 global_rsv->full = 0;
5460 spin_unlock(&global_rsv->lock);
5461
5462 block_rsv_add_bytes(dest, num_bytes, 1);
5463 return 0;
5464}
5465
Josef Bacik957780e2016-05-17 13:30:55 -04005466/*
5467 * This is for space we already have accounted in space_info->bytes_may_use, so
5468 * basically when we're returning space from block_rsv's.
5469 */
5470static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
5471 struct btrfs_space_info *space_info,
5472 u64 num_bytes)
5473{
5474 struct reserve_ticket *ticket;
5475 struct list_head *head;
5476 u64 used;
5477 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH;
5478 bool check_overcommit = false;
5479
5480 spin_lock(&space_info->lock);
5481 head = &space_info->priority_tickets;
5482
5483 /*
5484 * If we are over our limit then we need to check and see if we can
5485 * overcommit, and if we can't then we just need to free up our space
5486 * and not satisfy any requests.
5487 */
Nikolay Borisov0eee8a42017-06-14 11:35:34 +03005488 used = btrfs_space_info_used(space_info, true);
Josef Bacik957780e2016-05-17 13:30:55 -04005489 if (used - num_bytes >= space_info->total_bytes)
5490 check_overcommit = true;
5491again:
5492 while (!list_empty(head) && num_bytes) {
5493 ticket = list_first_entry(head, struct reserve_ticket,
5494 list);
5495 /*
5496 * We use 0 bytes because this space is already reserved, so
5497 * adding the ticket space would be a double count.
5498 */
5499 if (check_overcommit &&
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005500 !can_overcommit(fs_info, space_info, 0, flush, false))
Josef Bacik957780e2016-05-17 13:30:55 -04005501 break;
5502 if (num_bytes >= ticket->bytes) {
5503 list_del_init(&ticket->list);
5504 num_bytes -= ticket->bytes;
5505 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005506 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005507 wake_up(&ticket->wait);
5508 } else {
5509 ticket->bytes -= num_bytes;
5510 num_bytes = 0;
5511 }
5512 }
5513
5514 if (num_bytes && head == &space_info->priority_tickets) {
5515 head = &space_info->tickets;
5516 flush = BTRFS_RESERVE_FLUSH_ALL;
5517 goto again;
5518 }
5519 space_info->bytes_may_use -= num_bytes;
5520 trace_btrfs_space_reservation(fs_info, "space_info",
5521 space_info->flags, num_bytes, 0);
5522 spin_unlock(&space_info->lock);
5523}
5524
5525/*
5526 * This is for newly allocated space that isn't accounted in
5527 * space_info->bytes_may_use yet. So if we allocate a chunk or unpin an extent
5528 * we use this helper.
5529 */
5530static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
5531 struct btrfs_space_info *space_info,
5532 u64 num_bytes)
5533{
5534 struct reserve_ticket *ticket;
5535 struct list_head *head = &space_info->priority_tickets;
5536
5537again:
5538 while (!list_empty(head) && num_bytes) {
5539 ticket = list_first_entry(head, struct reserve_ticket,
5540 list);
5541 if (num_bytes >= ticket->bytes) {
5542 trace_btrfs_space_reservation(fs_info, "space_info",
5543 space_info->flags,
5544 ticket->bytes, 1);
5545 list_del_init(&ticket->list);
5546 num_bytes -= ticket->bytes;
5547 space_info->bytes_may_use += ticket->bytes;
5548 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005549 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005550 wake_up(&ticket->wait);
5551 } else {
5552 trace_btrfs_space_reservation(fs_info, "space_info",
5553 space_info->flags,
5554 num_bytes, 1);
5555 space_info->bytes_may_use += num_bytes;
5556 ticket->bytes -= num_bytes;
5557 num_bytes = 0;
5558 }
5559 }
5560
5561 if (num_bytes && head == &space_info->priority_tickets) {
5562 head = &space_info->tickets;
5563 goto again;
5564 }
5565}
5566
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005567static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
5568 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02005569 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005570{
5571 struct btrfs_space_info *space_info = block_rsv->space_info;
5572
5573 spin_lock(&block_rsv->lock);
5574 if (num_bytes == (u64)-1)
5575 num_bytes = block_rsv->size;
5576 block_rsv->size -= num_bytes;
5577 if (block_rsv->reserved >= block_rsv->size) {
5578 num_bytes = block_rsv->reserved - block_rsv->size;
5579 block_rsv->reserved = block_rsv->size;
5580 block_rsv->full = 1;
5581 } else {
5582 num_bytes = 0;
5583 }
5584 spin_unlock(&block_rsv->lock);
5585
5586 if (num_bytes > 0) {
5587 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00005588 spin_lock(&dest->lock);
5589 if (!dest->full) {
5590 u64 bytes_to_add;
5591
5592 bytes_to_add = dest->size - dest->reserved;
5593 bytes_to_add = min(num_bytes, bytes_to_add);
5594 dest->reserved += bytes_to_add;
5595 if (dest->reserved >= dest->size)
5596 dest->full = 1;
5597 num_bytes -= bytes_to_add;
5598 }
5599 spin_unlock(&dest->lock);
5600 }
Josef Bacik957780e2016-05-17 13:30:55 -04005601 if (num_bytes)
5602 space_info_add_old_bytes(fs_info, space_info,
5603 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005604 }
5605}
5606
Josef Bacik25d609f2016-03-25 13:25:48 -04005607int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
5608 struct btrfs_block_rsv *dst, u64 num_bytes,
5609 int update_size)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005610{
5611 int ret;
5612
5613 ret = block_rsv_use_bytes(src, num_bytes);
5614 if (ret)
5615 return ret;
5616
Josef Bacik25d609f2016-03-25 13:25:48 -04005617 block_rsv_add_bytes(dst, num_bytes, update_size);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005618 return 0;
5619}
5620
Miao Xie66d8f3d2012-09-06 04:02:28 -06005621void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005622{
5623 memset(rsv, 0, sizeof(*rsv));
5624 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06005625 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005626}
5627
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005628struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
Miao Xie66d8f3d2012-09-06 04:02:28 -06005629 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005630{
5631 struct btrfs_block_rsv *block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005632
5633 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
5634 if (!block_rsv)
5635 return NULL;
5636
Miao Xie66d8f3d2012-09-06 04:02:28 -06005637 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005638 block_rsv->space_info = __find_space_info(fs_info,
5639 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005640 return block_rsv;
5641}
5642
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005643void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005644 struct btrfs_block_rsv *rsv)
5645{
Josef Bacik2aaa6652012-08-29 14:27:18 -04005646 if (!rsv)
5647 return;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005648 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Josef Bacikdabdb642011-08-08 12:50:18 -04005649 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005650}
5651
Chris Masoncdfb0802015-04-06 18:17:00 -07005652void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
5653{
5654 kfree(rsv);
5655}
5656
Miao Xie08e007d2012-10-16 11:33:38 +00005657int btrfs_block_rsv_add(struct btrfs_root *root,
5658 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
5659 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005660{
5661 int ret;
5662
5663 if (num_bytes == 0)
5664 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005665
Miao Xie61b520a2011-11-10 20:45:05 -05005666 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005667 if (!ret) {
5668 block_rsv_add_bytes(block_rsv, num_bytes, 1);
5669 return 0;
5670 }
5671
Yan, Zhengf0486c62010-05-16 10:46:25 -04005672 return ret;
5673}
5674
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005675int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005676{
5677 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005678 int ret = -ENOSPC;
5679
5680 if (!block_rsv)
5681 return 0;
5682
5683 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04005684 num_bytes = div_factor(block_rsv->size, min_factor);
5685 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005686 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005687 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005688
Josef Bacik36ba0222011-10-18 12:15:48 -04005689 return ret;
5690}
5691
Miao Xie08e007d2012-10-16 11:33:38 +00005692int btrfs_block_rsv_refill(struct btrfs_root *root,
5693 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
5694 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04005695{
5696 u64 num_bytes = 0;
5697 int ret = -ENOSPC;
5698
5699 if (!block_rsv)
5700 return 0;
5701
5702 spin_lock(&block_rsv->lock);
5703 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04005704 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005705 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04005706 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04005707 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005708 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04005709
Yan, Zhengf0486c62010-05-16 10:46:25 -04005710 if (!ret)
5711 return 0;
5712
Miao Xieaa38a712011-11-18 17:43:00 +08005713 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04005714 if (!ret) {
5715 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005716 return 0;
5717 }
5718
Josef Bacik13553e52011-08-08 13:33:21 -04005719 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005720}
5721
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005722void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005723 struct btrfs_block_rsv *block_rsv,
5724 u64 num_bytes)
5725{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005726 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5727
Liu Bo17504582013-12-29 21:44:50 +08005728 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04005729 block_rsv->space_info != global_rsv->space_info)
5730 global_rsv = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005731 block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005732}
5733
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005734static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
5735{
5736 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
5737 struct btrfs_space_info *sinfo = block_rsv->space_info;
5738 u64 num_bytes;
5739
Josef Bacikae2e4722016-05-27 12:58:35 -04005740 /*
5741 * The global block rsv is based on the size of the extent tree, the
5742 * checksum tree and the root tree. If the fs is empty we want to set
5743 * it to a minimal amount for safety.
5744 */
5745 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) +
5746 btrfs_root_used(&fs_info->csum_root->root_item) +
5747 btrfs_root_used(&fs_info->tree_root->root_item);
5748 num_bytes = max_t(u64, num_bytes, SZ_16M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005749
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005750 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005751 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005752
Byongho Leeee221842015-12-15 01:42:10 +09005753 block_rsv->size = min_t(u64, num_bytes, SZ_512M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005754
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005755 if (block_rsv->reserved < block_rsv->size) {
Liu Bo41361352017-02-13 15:42:21 -08005756 num_bytes = btrfs_space_info_used(sinfo, true);
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005757 if (sinfo->total_bytes > num_bytes) {
5758 num_bytes = sinfo->total_bytes - num_bytes;
5759 num_bytes = min(num_bytes,
5760 block_rsv->size - block_rsv->reserved);
5761 block_rsv->reserved += num_bytes;
5762 sinfo->bytes_may_use += num_bytes;
5763 trace_btrfs_space_reservation(fs_info, "space_info",
5764 sinfo->flags, num_bytes,
5765 1);
5766 }
5767 } else if (block_rsv->reserved > block_rsv->size) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005768 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04005769 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005770 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005771 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005772 block_rsv->reserved = block_rsv->size;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005773 }
David Sterba182608c2011-05-05 13:13:16 +02005774
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005775 if (block_rsv->reserved == block_rsv->size)
5776 block_rsv->full = 1;
5777 else
5778 block_rsv->full = 0;
5779
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005780 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005781 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005782}
5783
Yan, Zhengf0486c62010-05-16 10:46:25 -04005784static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
5785{
5786 struct btrfs_space_info *space_info;
5787
5788 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
5789 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005790
5791 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005792 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005793 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005794 fs_info->trans_block_rsv.space_info = space_info;
5795 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04005796 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005797
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005798 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
5799 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
5800 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
5801 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00005802 if (fs_info->quota_root)
5803 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005804 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005805
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005806 update_global_block_rsv(fs_info);
5807}
5808
5809static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
5810{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005811 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
5812 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005813 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
5814 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
5815 WARN_ON(fs_info->trans_block_rsv.size > 0);
5816 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
5817 WARN_ON(fs_info->chunk_block_rsv.size > 0);
5818 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04005819 WARN_ON(fs_info->delayed_block_rsv.size > 0);
5820 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04005821}
5822
Yan, Zhenga22285a2010-05-16 10:48:46 -04005823void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005824 struct btrfs_fs_info *fs_info)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005825{
Josef Bacikbf2681c2017-09-29 15:44:05 -04005826 if (!trans->block_rsv) {
5827 ASSERT(!trans->bytes_reserved);
Josef Bacik0e721102012-06-26 16:13:18 -04005828 return;
Josef Bacikbf2681c2017-09-29 15:44:05 -04005829 }
Josef Bacik0e721102012-06-26 16:13:18 -04005830
Yan, Zhenga22285a2010-05-16 10:48:46 -04005831 if (!trans->bytes_reserved)
5832 return;
5833
Josef Bacikbf2681c2017-09-29 15:44:05 -04005834 ASSERT(trans->block_rsv == &fs_info->trans_block_rsv);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005835 trace_btrfs_space_reservation(fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04005836 trans->transid, trans->bytes_reserved, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005837 btrfs_block_rsv_release(fs_info, trans->block_rsv,
5838 trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005839 trans->bytes_reserved = 0;
5840}
5841
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005842/*
5843 * To be called after all the new block groups attached to the transaction
5844 * handle have been created (btrfs_create_pending_block_groups()).
5845 */
5846void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
5847{
Jeff Mahoney64b63582016-06-20 17:23:41 -04005848 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005849
5850 if (!trans->chunk_bytes_reserved)
5851 return;
5852
5853 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
5854
5855 block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL,
5856 trans->chunk_bytes_reserved);
5857 trans->chunk_bytes_reserved = 0;
5858}
5859
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005860/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04005861int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005862 struct btrfs_inode *inode)
Yan, Zhengd68fc572010-05-16 10:49:58 -04005863{
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005864 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
5865 struct btrfs_root *root = inode->root;
Josef Bacik40acc3ee2016-05-27 13:01:08 -04005866 /*
5867 * We always use trans->block_rsv here as we will have reserved space
5868 * for our orphan when starting the transaction, using get_block_rsv()
5869 * here will sometimes make us choose the wrong block rsv as we could be
5870 * doing a reloc inode for a non refcounted root.
5871 */
5872 struct btrfs_block_rsv *src_rsv = trans->block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005873 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
5874
5875 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04005876 * We need to hold space in order to delete our orphan item once we've
5877 * added it, so this takes the reservation so we can release it later
5878 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04005879 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005880 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5881
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005882 trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode),
5883 num_bytes, 1);
Josef Bacik25d609f2016-03-25 13:25:48 -04005884 return btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005885}
5886
Nikolay Borisov703b3912017-02-20 13:50:40 +02005887void btrfs_orphan_release_metadata(struct btrfs_inode *inode)
Yan, Zhengd68fc572010-05-16 10:49:58 -04005888{
Nikolay Borisov703b3912017-02-20 13:50:40 +02005889 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
5890 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005891 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5892
Nikolay Borisov703b3912017-02-20 13:50:40 +02005893 trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode),
5894 num_bytes, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005895 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, num_bytes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005896}
5897
Miao Xied5c12072013-02-28 10:04:33 +00005898/*
5899 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
5900 * root: the root of the parent directory
5901 * rsv: block reservation
5902 * items: the number of items that we need do reservation
5903 * qgroup_reserved: used to return the reserved size in qgroup
5904 *
5905 * This function is used to reserve the space for snapshot/subvolume
5906 * creation and deletion. Those operations are different with the
5907 * common file/directory operations, they change two fs/file trees
5908 * and root tree, the number of items that the qgroup reserves is
5909 * different with the free space reservation. So we can not use
Nicholas D Steeves01327612016-05-19 21:18:45 -04005910 * the space reservation mechanism in start_transaction().
Miao Xied5c12072013-02-28 10:04:33 +00005911 */
5912int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
5913 struct btrfs_block_rsv *rsv,
5914 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005915 u64 *qgroup_reserved,
5916 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005917{
Miao Xied5c12072013-02-28 10:04:33 +00005918 u64 num_bytes;
5919 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005920 struct btrfs_fs_info *fs_info = root->fs_info;
5921 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00005922
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005923 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Miao Xied5c12072013-02-28 10:04:33 +00005924 /* One for parent inode, two for dir entries */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005925 num_bytes = 3 * fs_info->nodesize;
Jeff Mahoney003d7c52017-01-25 09:50:33 -05005926 ret = btrfs_qgroup_reserve_meta(root, num_bytes, true);
Miao Xied5c12072013-02-28 10:04:33 +00005927 if (ret)
5928 return ret;
5929 } else {
5930 num_bytes = 0;
5931 }
5932
5933 *qgroup_reserved = num_bytes;
5934
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005935 num_bytes = btrfs_calc_trans_metadata_size(fs_info, items);
5936 rsv->space_info = __find_space_info(fs_info,
Miao Xied5c12072013-02-28 10:04:33 +00005937 BTRFS_BLOCK_GROUP_METADATA);
5938 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
5939 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005940
5941 if (ret == -ENOSPC && use_global_rsv)
Josef Bacik25d609f2016-03-25 13:25:48 -04005942 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005943
Qu Wenruo71741092015-09-08 17:22:41 +08005944 if (ret && *qgroup_reserved)
5945 btrfs_qgroup_free_meta(root, *qgroup_reserved);
Miao Xied5c12072013-02-28 10:04:33 +00005946
5947 return ret;
5948}
5949
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005950void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
David Sterba7775c812017-02-10 19:18:18 +01005951 struct btrfs_block_rsv *rsv)
Miao Xied5c12072013-02-28 10:04:33 +00005952{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005953 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005954}
5955
Josef Bacik7709cde2011-08-04 10:25:02 -04005956/**
Josef Bacik8b62f872017-10-19 14:15:55 -04005957 * drop_over_reserved_extents - drop our extra extent reservations
Josef Bacik7709cde2011-08-04 10:25:02 -04005958 * @inode: the inode we're dropping the extent for
5959 *
Josef Bacik8b62f872017-10-19 14:15:55 -04005960 * We reserve extents we may use, but they may have been merged with other
5961 * extents and we may not need the extra reservation.
5962 *
5963 * We also call this when we've completed io to an extent or had an error and
5964 * cleared the outstanding extent, in either case we no longer need our
5965 * reservation and can drop the excess.
Josef Bacik7709cde2011-08-04 10:25:02 -04005966 */
Josef Bacik8b62f872017-10-19 14:15:55 -04005967static unsigned drop_over_reserved_extents(struct btrfs_inode *inode)
Josef Bacik9e0baf62011-07-15 15:16:44 +00005968{
Josef Bacik8b62f872017-10-19 14:15:55 -04005969 unsigned num_extents = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005970
Josef Bacik8b62f872017-10-19 14:15:55 -04005971 if (inode->reserved_extents > inode->outstanding_extents) {
5972 num_extents = inode->reserved_extents -
5973 inode->outstanding_extents;
5974 btrfs_mod_reserved_extents(inode, -num_extents);
5975 }
Josef Bacik9e0baf62011-07-15 15:16:44 +00005976
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02005977 if (inode->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04005978 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02005979 &inode->runtime_flags))
Josef Bacik8b62f872017-10-19 14:15:55 -04005980 num_extents++;
5981 return num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005982}
5983
Josef Bacik7709cde2011-08-04 10:25:02 -04005984/**
Nicholas D Steeves01327612016-05-19 21:18:45 -04005985 * calc_csum_metadata_size - return the amount of metadata space that must be
5986 * reserved/freed for the given bytes.
Josef Bacik7709cde2011-08-04 10:25:02 -04005987 * @inode: the inode we're manipulating
5988 * @num_bytes: the number of bytes in question
5989 * @reserve: 1 if we are reserving space, 0 if we are freeing space
5990 *
5991 * This adjusts the number of csum_bytes in the inode and then returns the
5992 * correct amount of metadata that must either be reserved or freed. We
5993 * calculate how many checksums we can fit into one leaf and then divide the
5994 * number of bytes that will need to be checksumed by this value to figure out
5995 * how many checksums will be required. If we are adding bytes then the number
5996 * may go up and we will return the number of additional bytes that must be
5997 * reserved. If it is going down we will return the number of bytes that must
5998 * be freed.
5999 *
6000 * This must be called with BTRFS_I(inode)->lock held.
6001 */
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02006002static u64 calc_csum_metadata_size(struct btrfs_inode *inode, u64 num_bytes,
Josef Bacik7709cde2011-08-04 10:25:02 -04006003 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006004{
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02006005 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Josef Bacik12621332015-02-03 07:50:16 -08006006 u64 old_csums, num_csums;
Josef Bacik7709cde2011-08-04 10:25:02 -04006007
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02006008 if (inode->flags & BTRFS_INODE_NODATASUM && inode->csum_bytes == 0)
Josef Bacik7709cde2011-08-04 10:25:02 -04006009 return 0;
6010
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02006011 old_csums = btrfs_csum_bytes_to_leaves(fs_info, inode->csum_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04006012 if (reserve)
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02006013 inode->csum_bytes += num_bytes;
Josef Bacik7709cde2011-08-04 10:25:02 -04006014 else
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02006015 inode->csum_bytes -= num_bytes;
6016 num_csums = btrfs_csum_bytes_to_leaves(fs_info, inode->csum_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04006017
6018 /* No change, no need to reserve more */
6019 if (old_csums == num_csums)
6020 return 0;
6021
6022 if (reserve)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006023 return btrfs_calc_trans_metadata_size(fs_info,
Josef Bacik7709cde2011-08-04 10:25:02 -04006024 num_csums - old_csums);
6025
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006026 return btrfs_calc_trans_metadata_size(fs_info, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006027}
6028
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006029int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006030{
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006031 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
6032 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006033 struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006034 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05006035 u64 csum_bytes;
Josef Bacik8b62f872017-10-19 14:15:55 -04006036 unsigned nr_extents, reserve_extents;
Miao Xie08e007d2012-10-16 11:33:38 +00006037 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07006038 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006039 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00006040 u64 to_free = 0;
6041 unsigned dropped;
Josef Bacik48c3d482016-03-25 13:25:49 -04006042 bool release_extra = false;
Josef Bacik8b62f872017-10-19 14:15:55 -04006043 bool underflow = false;
6044 bool did_retry = false;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006045
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006046 /* If we are a free space inode we need to not flush since we will be in
6047 * the middle of a transaction commit. We also don't need the delalloc
6048 * mutex since we won't race with anybody. We need this mostly to make
6049 * lockdep shut its filthy mouth.
Josef Bacikbac357dc2016-07-20 16:48:45 -07006050 *
6051 * If we have a transaction open (can happen if we call truncate_block
6052 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006053 */
6054 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00006055 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006056 delalloc_lock = false;
Josef Bacikbac357dc2016-07-20 16:48:45 -07006057 } else if (current->journal_info) {
6058 flush = BTRFS_RESERVE_FLUSH_LIMIT;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006059 }
Josef Bacikc09544e2011-08-30 10:19:10 -04006060
Miao Xie08e007d2012-10-16 11:33:38 +00006061 if (flush != BTRFS_RESERVE_NO_FLUSH &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006062 btrfs_transaction_in_commit(fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006063 schedule_timeout(1);
6064
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006065 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006066 mutex_lock(&inode->delalloc_mutex);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006067
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006068 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Josef Bacik8b62f872017-10-19 14:15:55 -04006069retry:
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006070 spin_lock(&inode->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04006071 reserve_extents = nr_extents = count_max_extents(num_bytes);
6072 btrfs_mod_outstanding_extents(inode, nr_extents);
Josef Bacik57a45ced2011-01-25 16:30:38 -05006073
Josef Bacik8b62f872017-10-19 14:15:55 -04006074 /*
6075 * Because we add an outstanding extent for ordered before we clear
6076 * delalloc we will double count our outstanding extents slightly. This
6077 * could mean that we transiently over-reserve, which could result in an
6078 * early ENOSPC if our timing is unlucky. Keep track of the case that
6079 * we had a reservation underflow so we can retry if we fail.
6080 *
6081 * Keep in mind we can legitimately have more outstanding extents than
6082 * reserved because of fragmentation, so only allow a retry once.
6083 */
6084 if (inode->outstanding_extents >
6085 inode->reserved_extents + nr_extents) {
6086 reserve_extents = inode->outstanding_extents -
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006087 inode->reserved_extents;
Josef Bacik8b62f872017-10-19 14:15:55 -04006088 underflow = true;
6089 }
Josef Bacik7fd2ae22011-11-08 15:47:34 -05006090
Josef Bacik48c3d482016-03-25 13:25:49 -04006091 /* We always want to reserve a slot for updating the inode. */
Josef Bacik8b62f872017-10-19 14:15:55 -04006092 to_reserve = btrfs_calc_trans_metadata_size(fs_info,
6093 reserve_extents + 1);
Josef Bacik7709cde2011-08-04 10:25:02 -04006094 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006095 csum_bytes = inode->csum_bytes;
6096 spin_unlock(&inode->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05006097
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006098 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Qu Wenruo71741092015-09-08 17:22:41 +08006099 ret = btrfs_qgroup_reserve_meta(root,
Jeff Mahoney003d7c52017-01-25 09:50:33 -05006100 nr_extents * fs_info->nodesize, true);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006101 if (ret)
6102 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00006103 }
Arne Jansenc5567232011-09-14 15:44:05 +02006104
Josef Bacik48c3d482016-03-25 13:25:49 -04006105 ret = btrfs_block_rsv_add(root, block_rsv, to_reserve, flush);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006106 if (unlikely(ret)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04006107 btrfs_qgroup_free_meta(root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006108 nr_extents * fs_info->nodesize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006109 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006110 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006111
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006112 spin_lock(&inode->lock);
Josef Bacik48c3d482016-03-25 13:25:49 -04006113 if (test_and_set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006114 &inode->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006115 to_reserve -= btrfs_calc_trans_metadata_size(fs_info, 1);
Josef Bacik48c3d482016-03-25 13:25:49 -04006116 release_extra = true;
Josef Bacik660d3f62011-12-09 11:18:51 -05006117 }
Josef Bacik8b62f872017-10-19 14:15:55 -04006118 btrfs_mod_reserved_extents(inode, reserve_extents);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006119 spin_unlock(&inode->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006120
6121 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006122 mutex_unlock(&inode->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05006123
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006124 if (to_reserve)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006125 trace_btrfs_space_reservation(fs_info, "delalloc",
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006126 btrfs_ino(inode), to_reserve, 1);
Josef Bacik48c3d482016-03-25 13:25:49 -04006127 if (release_extra)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006128 btrfs_block_rsv_release(fs_info, block_rsv,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006129 btrfs_calc_trans_metadata_size(fs_info, 1));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006130 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00006131
6132out_fail:
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006133 spin_lock(&inode->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04006134 nr_extents = count_max_extents(num_bytes);
6135 btrfs_mod_outstanding_extents(inode, -nr_extents);
6136
6137 dropped = drop_over_reserved_extents(inode);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006138 /*
6139 * If the inodes csum_bytes is the same as the original
6140 * csum_bytes then we know we haven't raced with any free()ers
6141 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00006142 */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006143 if (inode->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00006144 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04006145 } else {
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006146 u64 orig_csum_bytes = inode->csum_bytes;
Josef Bacikf4881bc2013-03-25 16:03:35 -04006147 u64 bytes;
6148
6149 /*
6150 * This is tricky, but first we need to figure out how much we
Nicholas D Steeves01327612016-05-19 21:18:45 -04006151 * freed from any free-ers that occurred during this
Josef Bacikf4881bc2013-03-25 16:03:35 -04006152 * reservation, so we reset ->csum_bytes to the csum_bytes
6153 * before we dropped our lock, and then call the free for the
6154 * number of bytes that were freed while we were trying our
6155 * reservation.
6156 */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006157 bytes = csum_bytes - inode->csum_bytes;
6158 inode->csum_bytes = csum_bytes;
Josef Bacikf4881bc2013-03-25 16:03:35 -04006159 to_free = calc_csum_metadata_size(inode, bytes, 0);
6160
6161
6162 /*
6163 * Now we need to see how much we would have freed had we not
6164 * been making this reservation and our ->csum_bytes were not
6165 * artificially inflated.
6166 */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006167 inode->csum_bytes = csum_bytes - num_bytes;
Josef Bacikf4881bc2013-03-25 16:03:35 -04006168 bytes = csum_bytes - orig_csum_bytes;
6169 bytes = calc_csum_metadata_size(inode, bytes, 0);
6170
6171 /*
6172 * Now reset ->csum_bytes to what it should be. If bytes is
Nicholas D Steeves01327612016-05-19 21:18:45 -04006173 * more than to_free then we would have freed more space had we
Josef Bacikf4881bc2013-03-25 16:03:35 -04006174 * not had an artificially high ->csum_bytes, so we need to free
6175 * the remainder. If bytes is the same or less then we don't
6176 * need to do anything, the other free-ers did the correct
6177 * thing.
6178 */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006179 inode->csum_bytes = orig_csum_bytes - num_bytes;
Josef Bacikf4881bc2013-03-25 16:03:35 -04006180 if (bytes > to_free)
6181 to_free = bytes - to_free;
6182 else
6183 to_free = 0;
6184 }
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006185 spin_unlock(&inode->lock);
Dongsheng Yange2d1f922015-02-06 10:26:52 -05006186 if (dropped)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006187 to_free += btrfs_calc_trans_metadata_size(fs_info, dropped);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006188
6189 if (to_free) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006190 btrfs_block_rsv_release(fs_info, block_rsv, to_free);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006191 trace_btrfs_space_reservation(fs_info, "delalloc",
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006192 btrfs_ino(inode), to_free, 0);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006193 }
Josef Bacik8b62f872017-10-19 14:15:55 -04006194 if (underflow && !did_retry) {
6195 did_retry = true;
6196 underflow = false;
6197 goto retry;
6198 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00006199 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006200 mutex_unlock(&inode->delalloc_mutex);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006201 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006202}
6203
Josef Bacik7709cde2011-08-04 10:25:02 -04006204/**
6205 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
Josef Bacik8b62f872017-10-19 14:15:55 -04006206 * @inode: the inode to release the reservation for.
6207 * @num_bytes: the number of bytes we are releasing.
Josef Bacik7709cde2011-08-04 10:25:02 -04006208 *
6209 * This will release the metadata reservation for an inode. This can be called
6210 * once we complete IO for a given set of bytes to release their metadata
Josef Bacik8b62f872017-10-19 14:15:55 -04006211 * reservations, or on error for the same reason.
Josef Bacik7709cde2011-08-04 10:25:02 -04006212 */
Nikolay Borisov691fa052017-02-20 13:50:42 +02006213void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006214{
Nikolay Borisov691fa052017-02-20 13:50:42 +02006215 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006216 u64 to_free = 0;
6217 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006218
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006219 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006220 spin_lock(&inode->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04006221 dropped = drop_over_reserved_extents(inode);
Miao Xie09348562013-02-07 10:12:07 +00006222 if (num_bytes)
6223 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006224 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006225 if (dropped > 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006226 to_free += btrfs_calc_trans_metadata_size(fs_info, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006227
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006228 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04006229 return;
6230
Nikolay Borisov691fa052017-02-20 13:50:42 +02006231 trace_btrfs_space_reservation(fs_info, "delalloc", btrfs_ino(inode),
6232 to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02006233
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006234 btrfs_block_rsv_release(fs_info, &fs_info->delalloc_block_rsv, to_free);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006235}
6236
Josef Bacik7709cde2011-08-04 10:25:02 -04006237/**
Josef Bacik8b62f872017-10-19 14:15:55 -04006238 * btrfs_delalloc_release_extents - release our outstanding_extents
6239 * @inode: the inode to balance the reservation for.
6240 * @num_bytes: the number of bytes we originally reserved with
6241 *
6242 * When we reserve space we increase outstanding_extents for the extents we may
6243 * add. Once we've set the range as delalloc or created our ordered extents we
6244 * have outstanding_extents to track the real usage, so we use this to free our
6245 * temporarily tracked outstanding_extents. This _must_ be used in conjunction
6246 * with btrfs_delalloc_reserve_metadata.
6247 */
6248void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes)
6249{
6250 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
6251 unsigned num_extents;
6252 u64 to_free;
6253 unsigned dropped;
6254
6255 spin_lock(&inode->lock);
6256 num_extents = count_max_extents(num_bytes);
6257 btrfs_mod_outstanding_extents(inode, -num_extents);
6258 dropped = drop_over_reserved_extents(inode);
6259 spin_unlock(&inode->lock);
6260
6261 if (!dropped)
6262 return;
6263
6264 if (btrfs_is_testing(fs_info))
6265 return;
6266
6267 to_free = btrfs_calc_trans_metadata_size(fs_info, dropped);
6268 trace_btrfs_space_reservation(fs_info, "delalloc", btrfs_ino(inode),
6269 to_free, 0);
6270 btrfs_block_rsv_release(fs_info, &fs_info->delalloc_block_rsv, to_free);
6271}
6272
6273/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006274 * btrfs_delalloc_reserve_space - reserve data and metadata space for
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006275 * delalloc
6276 * @inode: inode we're writing to
6277 * @start: start range we are writing to
6278 * @len: how long the range we are writing to
Qu Wenruo364ecf32017-02-27 15:10:38 +08006279 * @reserved: mandatory parameter, record actually reserved qgroup ranges of
6280 * current reservation.
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006281 *
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006282 * This will do the following things
6283 *
6284 * o reserve space in data space info for num bytes
6285 * and reserve precious corresponding qgroup space
6286 * (Done in check_data_free_space)
6287 *
6288 * o reserve space for metadata space, based on the number of outstanding
6289 * extents and how much csums will be needed
6290 * also reserve metadata space in a per root over-reserve method.
6291 * o add to the inodes->delalloc_bytes
6292 * o add it to the fs_info's delalloc inodes list.
6293 * (Above 3 all done in delalloc_reserve_metadata)
6294 *
6295 * Return 0 for success
6296 * Return <0 for error(-ENOSPC or -EQUOT)
6297 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08006298int btrfs_delalloc_reserve_space(struct inode *inode,
6299 struct extent_changeset **reserved, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006300{
6301 int ret;
6302
Qu Wenruo364ecf32017-02-27 15:10:38 +08006303 ret = btrfs_check_data_free_space(inode, reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006304 if (ret < 0)
6305 return ret;
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006306 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006307 if (ret < 0)
Qu Wenruobc42bda2017-02-27 15:10:39 +08006308 btrfs_free_reserved_data_space(inode, *reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006309 return ret;
6310}
6311
6312/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006313 * btrfs_delalloc_release_space - release data and metadata space for delalloc
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006314 * @inode: inode we're releasing space for
6315 * @start: start position of the space already reserved
6316 * @len: the len of the space already reserved
Josef Bacik8b62f872017-10-19 14:15:55 -04006317 * @release_bytes: the len of the space we consumed or didn't use
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006318 *
6319 * This function will release the metadata space that was not used and will
6320 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
6321 * list if there are no delalloc bytes left.
6322 * Also it will handle the qgroup reserved space.
6323 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08006324void btrfs_delalloc_release_space(struct inode *inode,
Josef Bacik8b62f872017-10-19 14:15:55 -04006325 struct extent_changeset *reserved,
6326 u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006327{
Nikolay Borisov691fa052017-02-20 13:50:42 +02006328 btrfs_delalloc_release_metadata(BTRFS_I(inode), len);
Qu Wenruobc42bda2017-02-27 15:10:39 +08006329 btrfs_free_reserved_data_space(inode, reserved, start, len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006330}
6331
Josef Bacikce93ec52014-11-17 15:45:48 -05006332static int update_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6202df62016-06-22 18:54:22 -04006333 struct btrfs_fs_info *info, u64 bytenr,
Josef Bacikce93ec52014-11-17 15:45:48 -05006334 u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04006335{
Josef Bacik0af3d002010-06-21 14:48:16 -04006336 struct btrfs_block_group_cache *cache = NULL;
Chris Masondb945352007-10-15 16:15:53 -04006337 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006338 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04006339 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04006340 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04006341
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006342 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00006343 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02006344 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006345 if (alloc)
6346 old_val += num_bytes;
6347 else
6348 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02006349 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00006350 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006351
Chris Masond3977122009-01-05 21:25:51 -05006352 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04006353 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006354 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006355 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006356 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
6357 BTRFS_BLOCK_GROUP_RAID1 |
6358 BTRFS_BLOCK_GROUP_RAID10))
6359 factor = 2;
6360 else
6361 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04006362 /*
6363 * If this block group has free space cache written out, we
6364 * need to make sure to load it if we are removing space. This
6365 * is because we need the unpinning stage to actually add the
6366 * space back to the block group, otherwise we will leak space.
6367 */
6368 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00006369 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04006370
Chris Masondb945352007-10-15 16:15:53 -04006371 byte_in_group = bytenr - cache->key.objectid;
6372 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04006373
Josef Bacik25179202008-10-29 14:49:05 -04006374 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006375 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04006376
Jeff Mahoney6202df62016-06-22 18:54:22 -04006377 if (btrfs_test_opt(info, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04006378 cache->disk_cache_state < BTRFS_DC_CLEAR)
6379 cache->disk_cache_state = BTRFS_DC_CLEAR;
6380
Chris Mason9078a3e2007-04-26 16:46:15 -04006381 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04006382 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04006383 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04006384 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006385 btrfs_set_block_group_used(&cache->item, old_val);
6386 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006387 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006388 cache->space_info->bytes_used += num_bytes;
6389 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04006390 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006391 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04006392 } else {
Chris Masondb945352007-10-15 16:15:53 -04006393 old_val -= num_bytes;
Filipe Mananaae0ab002014-11-26 15:28:52 +00006394 btrfs_set_block_group_used(&cache->item, old_val);
6395 cache->pinned += num_bytes;
6396 cache->space_info->bytes_pinned += num_bytes;
6397 cache->space_info->bytes_used -= num_bytes;
6398 cache->space_info->disk_used -= num_bytes * factor;
6399 spin_unlock(&cache->lock);
6400 spin_unlock(&cache->space_info->lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04006401
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006402 trace_btrfs_space_reservation(info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006403 cache->space_info->flags,
6404 num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07006405 percpu_counter_add(&cache->space_info->total_bytes_pinned,
6406 num_bytes);
Filipe Mananaae0ab002014-11-26 15:28:52 +00006407 set_extent_dirty(info->pinned_extents,
6408 bytenr, bytenr + num_bytes - 1,
6409 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04006410 }
Chris Mason1bbc6212015-04-06 12:46:08 -07006411
6412 spin_lock(&trans->transaction->dirty_bgs_lock);
6413 if (list_empty(&cache->dirty_list)) {
6414 list_add_tail(&cache->dirty_list,
6415 &trans->transaction->dirty_bgs);
6416 trans->transaction->num_dirty_bgs++;
6417 btrfs_get_block_group(cache);
6418 }
6419 spin_unlock(&trans->transaction->dirty_bgs_lock);
6420
Filipe Manana036a9342015-11-23 15:25:16 +00006421 /*
6422 * No longer have used bytes in this block group, queue it for
6423 * deletion. We do this after adding the block group to the
6424 * dirty list to avoid races between cleaner kthread and space
6425 * cache writeout.
6426 */
6427 if (!alloc && old_val == 0) {
6428 spin_lock(&info->unused_bgs_lock);
6429 if (list_empty(&cache->bg_list)) {
6430 btrfs_get_block_group(cache);
6431 list_add_tail(&cache->bg_list,
6432 &info->unused_bgs);
6433 }
6434 spin_unlock(&info->unused_bgs_lock);
6435 }
6436
Chris Masonfa9c0d792009-04-03 09:47:43 -04006437 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04006438 total -= num_bytes;
6439 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006440 }
6441 return 0;
6442}
Chris Mason6324fbf2008-03-24 15:01:59 -04006443
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006444static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04006445{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006446 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05006447 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006448
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006449 spin_lock(&fs_info->block_group_cache_lock);
6450 bytenr = fs_info->first_logical_byte;
6451 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00006452
6453 if (bytenr < (u64)-1)
6454 return bytenr;
6455
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006456 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006457 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04006458 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006459
Yan Zhengd2fb3432008-12-11 16:30:39 -05006460 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006461 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006462
6463 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04006464}
6465
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006466static int pin_down_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006467 struct btrfs_block_group_cache *cache,
6468 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05006469{
Yan Zheng11833d62009-09-11 16:11:19 -04006470 spin_lock(&cache->space_info->lock);
6471 spin_lock(&cache->lock);
6472 cache->pinned += num_bytes;
6473 cache->space_info->bytes_pinned += num_bytes;
6474 if (reserved) {
6475 cache->reserved -= num_bytes;
6476 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05006477 }
Yan Zheng11833d62009-09-11 16:11:19 -04006478 spin_unlock(&cache->lock);
6479 spin_unlock(&cache->space_info->lock);
6480
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006481 trace_btrfs_space_reservation(fs_info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006482 cache->space_info->flags, num_bytes, 1);
Omar Sandoval4da8b762017-06-06 16:45:28 -07006483 percpu_counter_add(&cache->space_info->total_bytes_pinned, num_bytes);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006484 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006485 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05006486 return 0;
6487}
Chris Mason9078a3e2007-04-26 16:46:15 -04006488
Yan, Zhengf0486c62010-05-16 10:46:25 -04006489/*
6490 * this function must be called within transaction
6491 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006492int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006493 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04006494{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006495 struct btrfs_block_group_cache *cache;
6496
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006497 cache = btrfs_lookup_block_group(fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006498 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006499
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006500 pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006501
6502 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04006503 return 0;
6504}
Zheng Yane8569812008-09-26 10:05:48 -04006505
Yan, Zhengf0486c62010-05-16 10:46:25 -04006506/*
Chris Masone688b7252011-10-31 20:52:39 -04006507 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04006508 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006509int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04006510 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006511{
Chris Masone688b7252011-10-31 20:52:39 -04006512 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04006513 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04006514
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006515 cache = btrfs_lookup_block_group(fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006516 if (!cache)
6517 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04006518
6519 /*
6520 * pull in the free space cache (if any) so that our pin
6521 * removes the free space from the cache. We have load_only set
6522 * to one because the slow code to read in the free extents does check
6523 * the pinned extents.
6524 */
Liu Bof6373bf2012-12-27 09:01:18 +00006525 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04006526
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006527 pin_down_extent(fs_info, cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04006528
6529 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04006530 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04006531 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006532 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04006533}
6534
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006535static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
6536 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006537{
6538 int ret;
6539 struct btrfs_block_group_cache *block_group;
6540 struct btrfs_caching_control *caching_ctl;
6541
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006542 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006543 if (!block_group)
6544 return -EINVAL;
6545
6546 cache_block_group(block_group, 0);
6547 caching_ctl = get_caching_control(block_group);
6548
6549 if (!caching_ctl) {
6550 /* Logic error */
6551 BUG_ON(!block_group_cache_done(block_group));
6552 ret = btrfs_remove_free_space(block_group, start, num_bytes);
6553 } else {
6554 mutex_lock(&caching_ctl->mutex);
6555
6556 if (start >= caching_ctl->progress) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006557 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006558 } else if (start + num_bytes <= caching_ctl->progress) {
6559 ret = btrfs_remove_free_space(block_group,
6560 start, num_bytes);
6561 } else {
6562 num_bytes = caching_ctl->progress - start;
6563 ret = btrfs_remove_free_space(block_group,
6564 start, num_bytes);
6565 if (ret)
6566 goto out_lock;
6567
6568 num_bytes = (start + num_bytes) -
6569 caching_ctl->progress;
6570 start = caching_ctl->progress;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006571 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006572 }
6573out_lock:
6574 mutex_unlock(&caching_ctl->mutex);
6575 put_caching_control(caching_ctl);
6576 }
6577 btrfs_put_block_group(block_group);
6578 return ret;
6579}
6580
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006581int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info,
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006582 struct extent_buffer *eb)
6583{
6584 struct btrfs_file_extent_item *item;
6585 struct btrfs_key key;
6586 int found_type;
6587 int i;
6588
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006589 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006590 return 0;
6591
6592 for (i = 0; i < btrfs_header_nritems(eb); i++) {
6593 btrfs_item_key_to_cpu(eb, &key, i);
6594 if (key.type != BTRFS_EXTENT_DATA_KEY)
6595 continue;
6596 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
6597 found_type = btrfs_file_extent_type(eb, item);
6598 if (found_type == BTRFS_FILE_EXTENT_INLINE)
6599 continue;
6600 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
6601 continue;
6602 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
6603 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006604 __exclude_logged_extent(fs_info, key.objectid, key.offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006605 }
6606
6607 return 0;
6608}
6609
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006610static void
6611btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg)
6612{
6613 atomic_inc(&bg->reservations);
6614}
6615
6616void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
6617 const u64 start)
6618{
6619 struct btrfs_block_group_cache *bg;
6620
6621 bg = btrfs_lookup_block_group(fs_info, start);
6622 ASSERT(bg);
6623 if (atomic_dec_and_test(&bg->reservations))
6624 wake_up_atomic_t(&bg->reservations);
6625 btrfs_put_block_group(bg);
6626}
6627
6628static int btrfs_wait_bg_reservations_atomic_t(atomic_t *a)
6629{
6630 schedule();
6631 return 0;
6632}
6633
6634void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6635{
6636 struct btrfs_space_info *space_info = bg->space_info;
6637
6638 ASSERT(bg->ro);
6639
6640 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
6641 return;
6642
6643 /*
6644 * Our block group is read only but before we set it to read only,
6645 * some task might have had allocated an extent from it already, but it
6646 * has not yet created a respective ordered extent (and added it to a
6647 * root's list of ordered extents).
6648 * Therefore wait for any task currently allocating extents, since the
6649 * block group's reservations counter is incremented while a read lock
6650 * on the groups' semaphore is held and decremented after releasing
6651 * the read access on that semaphore and creating the ordered extent.
6652 */
6653 down_write(&space_info->groups_sem);
6654 up_write(&space_info->groups_sem);
6655
6656 wait_on_atomic_t(&bg->reservations,
6657 btrfs_wait_bg_reservations_atomic_t,
6658 TASK_UNINTERRUPTIBLE);
6659}
6660
Josef Bacikfb25e912011-07-26 17:00:46 -04006661/**
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006662 * btrfs_add_reserved_bytes - update the block_group and space info counters
Josef Bacikfb25e912011-07-26 17:00:46 -04006663 * @cache: The cache we are manipulating
Wang Xiaoguang18513092016-07-25 15:51:40 +08006664 * @ram_bytes: The number of bytes of file content, and will be same to
6665 * @num_bytes except for the compress path.
Josef Bacikfb25e912011-07-26 17:00:46 -04006666 * @num_bytes: The number of bytes in question
Miao Xiee570fd22014-06-19 10:42:50 +08006667 * @delalloc: The blocks are allocated for the delalloc write
Josef Bacikfb25e912011-07-26 17:00:46 -04006668 *
Xiaoguang Wang745699e2016-09-23 12:38:50 +08006669 * This is called by the allocator when it reserves space. If this is a
6670 * reservation and the block group has become read only we cannot make the
6671 * reservation and return -EAGAIN, otherwise this function always succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04006672 */
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006673static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
Wang Xiaoguang18513092016-07-25 15:51:40 +08006674 u64 ram_bytes, u64 num_bytes, int delalloc)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006675{
Josef Bacikfb25e912011-07-26 17:00:46 -04006676 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006677 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006678
Josef Bacikfb25e912011-07-26 17:00:46 -04006679 spin_lock(&space_info->lock);
6680 spin_lock(&cache->lock);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006681 if (cache->ro) {
6682 ret = -EAGAIN;
Josef Bacikfb25e912011-07-26 17:00:46 -04006683 } else {
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006684 cache->reserved += num_bytes;
6685 space_info->bytes_reserved += num_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006686
Wang Xiaoguang18513092016-07-25 15:51:40 +08006687 trace_btrfs_space_reservation(cache->fs_info,
6688 "space_info", space_info->flags,
6689 ram_bytes, 0);
6690 space_info->bytes_may_use -= ram_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006691 if (delalloc)
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006692 cache->delalloc_bytes += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006693 }
Josef Bacikfb25e912011-07-26 17:00:46 -04006694 spin_unlock(&cache->lock);
6695 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006696 return ret;
6697}
6698
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006699/**
6700 * btrfs_free_reserved_bytes - update the block_group and space info counters
6701 * @cache: The cache we are manipulating
6702 * @num_bytes: The number of bytes in question
6703 * @delalloc: The blocks are allocated for the delalloc write
6704 *
6705 * This is called by somebody who is freeing space that was never actually used
6706 * on disk. For example if you reserve some space for a new leaf in transaction
6707 * A and before transaction A commits you free that leaf, you call this with
6708 * reserve set to 0 in order to clear the reservation.
6709 */
6710
6711static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
6712 u64 num_bytes, int delalloc)
6713{
6714 struct btrfs_space_info *space_info = cache->space_info;
6715 int ret = 0;
6716
6717 spin_lock(&space_info->lock);
6718 spin_lock(&cache->lock);
6719 if (cache->ro)
6720 space_info->bytes_readonly += num_bytes;
6721 cache->reserved -= num_bytes;
6722 space_info->bytes_reserved -= num_bytes;
6723
6724 if (delalloc)
6725 cache->delalloc_bytes -= num_bytes;
6726 spin_unlock(&cache->lock);
6727 spin_unlock(&space_info->lock);
6728 return ret;
6729}
David Sterba8b74c032017-02-10 19:20:56 +01006730void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04006731{
Yan Zheng11833d62009-09-11 16:11:19 -04006732 struct btrfs_caching_control *next;
6733 struct btrfs_caching_control *caching_ctl;
6734 struct btrfs_block_group_cache *cache;
6735
Josef Bacik9e351cc2014-03-13 15:42:13 -04006736 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04006737
6738 list_for_each_entry_safe(caching_ctl, next,
6739 &fs_info->caching_block_groups, list) {
6740 cache = caching_ctl->block_group;
6741 if (block_group_cache_done(cache)) {
6742 cache->last_byte_to_unpin = (u64)-1;
6743 list_del_init(&caching_ctl->list);
6744 put_caching_control(caching_ctl);
6745 } else {
6746 cache->last_byte_to_unpin = caching_ctl->progress;
6747 }
6748 }
6749
6750 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6751 fs_info->pinned_extents = &fs_info->freed_extents[1];
6752 else
6753 fs_info->pinned_extents = &fs_info->freed_extents[0];
6754
Josef Bacik9e351cc2014-03-13 15:42:13 -04006755 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006756
6757 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04006758}
6759
Josef Bacikc759c4e2015-10-02 15:25:10 -04006760/*
6761 * Returns the free cluster for the given space info and sets empty_cluster to
6762 * what it should be based on the mount options.
6763 */
6764static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006765fetch_cluster_info(struct btrfs_fs_info *fs_info,
6766 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04006767{
6768 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006769
6770 *empty_cluster = 0;
6771 if (btrfs_mixed_space_info(space_info))
6772 return ret;
6773
Josef Bacikc759c4e2015-10-02 15:25:10 -04006774 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006775 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006776 if (btrfs_test_opt(fs_info, SSD))
6777 *empty_cluster = SZ_2M;
6778 else
Byongho Leeee221842015-12-15 01:42:10 +09006779 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006780 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
6781 btrfs_test_opt(fs_info, SSD_SPREAD)) {
6782 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006783 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006784 }
6785
6786 return ret;
6787}
6788
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006789static int unpin_extent_range(struct btrfs_fs_info *fs_info,
6790 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00006791 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04006792{
Yan Zheng11833d62009-09-11 16:11:19 -04006793 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04006794 struct btrfs_space_info *space_info;
6795 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006796 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04006797 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006798 u64 total_unpinned = 0;
6799 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04006800 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04006801
6802 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04006803 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04006804 if (!cache ||
6805 start >= cache->key.objectid + cache->key.offset) {
6806 if (cache)
6807 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04006808 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006809 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006810 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04006811
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006812 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04006813 cache->space_info,
6814 &empty_cluster);
6815 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04006816 }
6817
6818 len = cache->key.objectid + cache->key.offset - start;
6819 len = min(len, end + 1 - start);
6820
6821 if (start < cache->last_byte_to_unpin) {
6822 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00006823 if (return_free_space)
6824 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04006825 }
Josef Bacik25179202008-10-29 14:49:05 -04006826
Yan, Zhengf0486c62010-05-16 10:46:25 -04006827 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006828 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006829 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006830
Josef Bacikc759c4e2015-10-02 15:25:10 -04006831 /*
6832 * If this space cluster has been marked as fragmented and we've
6833 * unpinned enough in this block group to potentially allow a
6834 * cluster to be created inside of it go ahead and clear the
6835 * fragmented check.
6836 */
6837 if (cluster && cluster->fragmented &&
6838 total_unpinned > empty_cluster) {
6839 spin_lock(&cluster->lock);
6840 cluster->fragmented = 0;
6841 spin_unlock(&cluster->lock);
6842 }
6843
Josef Bacik7b398f82012-10-22 15:52:28 -04006844 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006845 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006846 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006847 space_info->bytes_pinned -= len;
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006848
6849 trace_btrfs_space_reservation(fs_info, "pinned",
6850 space_info->flags, len, 0);
Josef Bacik4f4db212015-09-29 11:40:47 -04006851 space_info->max_extent_size = 0;
Liu Bod288db52014-07-02 16:58:01 +08006852 percpu_counter_add(&space_info->total_bytes_pinned, -len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006853 if (cache->ro) {
6854 space_info->bytes_readonly += len;
6855 readonly = true;
6856 }
Josef Bacik25179202008-10-29 14:49:05 -04006857 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006858 if (!readonly && return_free_space &&
6859 global_rsv->space_info == space_info) {
6860 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03006861
Josef Bacik7b398f82012-10-22 15:52:28 -04006862 spin_lock(&global_rsv->lock);
6863 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04006864 to_add = min(len, global_rsv->size -
6865 global_rsv->reserved);
6866 global_rsv->reserved += to_add;
6867 space_info->bytes_may_use += to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006868 if (global_rsv->reserved >= global_rsv->size)
6869 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04006870 trace_btrfs_space_reservation(fs_info,
6871 "space_info",
6872 space_info->flags,
6873 to_add, 1);
6874 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006875 }
6876 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006877 /* Add to any tickets we may have */
6878 if (len)
6879 space_info_add_new_bytes(fs_info, space_info,
6880 len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006881 }
6882 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006883 }
6884
6885 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006886 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04006887 return 0;
6888}
6889
6890int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006891 struct btrfs_fs_info *fs_info)
Chris Masona28ec192007-03-06 20:08:01 -05006892{
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006893 struct btrfs_block_group_cache *block_group, *tmp;
6894 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04006895 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04006896 u64 start;
6897 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05006898 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05006899
Yan Zheng11833d62009-09-11 16:11:19 -04006900 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6901 unpin = &fs_info->freed_extents[1];
6902 else
6903 unpin = &fs_info->freed_extents[0];
6904
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006905 while (!trans->aborted) {
Filipe Mananad4b450c2015-01-29 19:18:25 +00006906 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04006907 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04006908 EXTENT_DIRTY, NULL);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006909 if (ret) {
6910 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05006911 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00006912 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006913
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006914 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006915 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00006916 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006917
David Sterbaaf6f8f62016-04-26 23:54:39 +02006918 clear_extent_dirty(unpin, start, end);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006919 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006920 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masonb9473432009-03-13 11:00:37 -04006921 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05006922 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006923
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006924 /*
6925 * Transaction is finished. We don't need the lock anymore. We
6926 * do need to clean up the block groups in case of a transaction
6927 * abort.
6928 */
6929 deleted_bgs = &trans->transaction->deleted_bgs;
6930 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
6931 u64 trimmed = 0;
6932
6933 ret = -EROFS;
6934 if (!trans->aborted)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006935 ret = btrfs_discard_extent(fs_info,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006936 block_group->key.objectid,
6937 block_group->key.offset,
6938 &trimmed);
6939
6940 list_del_init(&block_group->bg_list);
6941 btrfs_put_block_group_trimming(block_group);
6942 btrfs_put_block_group(block_group);
6943
6944 if (ret) {
6945 const char *errstr = btrfs_decode_error(ret);
6946 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02006947 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006948 ret, errstr);
6949 }
6950 }
6951
Chris Masone20d96d2007-03-22 12:13:20 -04006952 return 0;
6953}
6954
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006955static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006956 struct btrfs_fs_info *info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006957 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006958 u64 root_objectid, u64 owner_objectid,
6959 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006960 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05006961{
Chris Masone2fa7222007-03-12 16:22:34 -04006962 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006963 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04006964 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04006965 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006966 struct btrfs_extent_item *ei;
6967 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05006968 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006969 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05006970 int extent_slot = 0;
6971 int found_extent = 0;
6972 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006973 u32 item_size;
6974 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006975 u64 bytenr = node->bytenr;
6976 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07006977 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006978 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05006979
Chris Mason5caf2a02007-04-02 11:20:42 -04006980 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04006981 if (!path)
6982 return -ENOMEM;
6983
David Sterbae4058b52015-11-27 16:31:35 +01006984 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04006985 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006986
6987 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
6988 BUG_ON(!is_data && refs_to_drop != 1);
6989
Josef Bacik3173a182013-03-07 14:22:04 -05006990 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02006991 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05006992
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05006993 ret = lookup_extent_backref(trans, info, path, &iref,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006994 bytenr, num_bytes, parent,
6995 root_objectid, owner_objectid,
6996 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05006997 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05006998 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006999 while (extent_slot >= 0) {
7000 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05007001 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007002 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05007003 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007004 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
7005 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05007006 found_extent = 1;
7007 break;
7008 }
Josef Bacik3173a182013-03-07 14:22:04 -05007009 if (key.type == BTRFS_METADATA_ITEM_KEY &&
7010 key.offset == owner_objectid) {
7011 found_extent = 1;
7012 break;
7013 }
Chris Mason952fcca2008-02-18 16:33:44 -05007014 if (path->slots[0] - extent_slot > 5)
7015 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007016 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05007017 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007018#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
7019 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
7020 if (found_extent && item_size < sizeof(*ei))
7021 found_extent = 0;
7022#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04007023 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007024 BUG_ON(iref);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05007025 ret = remove_extent_backref(trans, info, path, NULL,
7026 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07007027 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06007028 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007029 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007030 goto out;
7031 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007032 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04007033 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007034
7035 key.objectid = bytenr;
7036 key.type = BTRFS_EXTENT_ITEM_KEY;
7037 key.offset = num_bytes;
7038
Josef Bacik3173a182013-03-07 14:22:04 -05007039 if (!is_data && skinny_metadata) {
7040 key.type = BTRFS_METADATA_ITEM_KEY;
7041 key.offset = owner_objectid;
7042 }
7043
Zheng Yan31840ae2008-09-23 13:14:14 -04007044 ret = btrfs_search_slot(trans, extent_root,
7045 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05007046 if (ret > 0 && skinny_metadata && path->slots[0]) {
7047 /*
7048 * Couldn't find our skinny metadata item,
7049 * see if we have ye olde extent item.
7050 */
7051 path->slots[0]--;
7052 btrfs_item_key_to_cpu(path->nodes[0], &key,
7053 path->slots[0]);
7054 if (key.objectid == bytenr &&
7055 key.type == BTRFS_EXTENT_ITEM_KEY &&
7056 key.offset == num_bytes)
7057 ret = 0;
7058 }
7059
7060 if (ret > 0 && skinny_metadata) {
7061 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01007062 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05007063 key.type = BTRFS_EXTENT_ITEM_KEY;
7064 key.offset = num_bytes;
7065 btrfs_release_path(path);
7066 ret = btrfs_search_slot(trans, extent_root,
7067 &key, path, -1, 1);
7068 }
7069
Josef Bacikf3465ca2008-11-12 14:19:50 -05007070 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007071 btrfs_err(info,
7072 "umm, got %d back from search, was looking for %llu",
7073 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00007074 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02007075 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05007076 }
David Sterba005d6422012-09-18 07:52:32 -06007077 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007078 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007079 goto out;
7080 }
Zheng Yan31840ae2008-09-23 13:14:14 -04007081 extent_slot = path->slots[0];
7082 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05307083 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02007084 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007085 btrfs_err(info,
7086 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007087 bytenr, parent, root_objectid, owner_objectid,
7088 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04007089 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04007090 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007091 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04007092 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007093 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05007094 }
Chris Mason5f39d392007-10-15 16:14:19 -04007095
7096 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007097 item_size = btrfs_item_size_nr(leaf, extent_slot);
7098#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
7099 if (item_size < sizeof(*ei)) {
7100 BUG_ON(found_extent || extent_slot != path->slots[0]);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05007101 ret = convert_extent_item_v0(trans, info, path, owner_objectid,
7102 0);
David Sterba005d6422012-09-18 07:52:32 -06007103 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007104 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007105 goto out;
7106 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007107
David Sterbab3b4aa72011-04-21 01:20:15 +02007108 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007109 path->leave_spinning = 1;
7110
7111 key.objectid = bytenr;
7112 key.type = BTRFS_EXTENT_ITEM_KEY;
7113 key.offset = num_bytes;
7114
7115 ret = btrfs_search_slot(trans, extent_root, &key, path,
7116 -1, 1);
7117 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007118 btrfs_err(info,
7119 "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007120 ret, bytenr);
David Sterbaa4f78752017-06-29 18:37:49 +02007121 btrfs_print_leaf(path->nodes[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007122 }
David Sterba005d6422012-09-18 07:52:32 -06007123 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007124 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007125 goto out;
7126 }
7127
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007128 extent_slot = path->slots[0];
7129 leaf = path->nodes[0];
7130 item_size = btrfs_item_size_nr(leaf, extent_slot);
7131 }
7132#endif
7133 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05007134 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04007135 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05007136 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
7137 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007138 struct btrfs_tree_block_info *bi;
7139 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
7140 bi = (struct btrfs_tree_block_info *)(ei + 1);
7141 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05007142 }
7143
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007144 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04007145 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007146 btrfs_err(info,
7147 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
7148 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04007149 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04007150 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04007151 goto out;
7152 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007153 refs -= refs_to_drop;
7154
7155 if (refs > 0) {
7156 if (extent_op)
7157 __run_delayed_extent_op(extent_op, leaf, ei);
7158 /*
7159 * In the case of inline back ref, reference count will
7160 * be updated by remove_extent_backref
7161 */
7162 if (iref) {
7163 BUG_ON(!found_extent);
7164 } else {
7165 btrfs_set_extent_refs(leaf, ei, refs);
7166 btrfs_mark_buffer_dirty(leaf);
7167 }
7168 if (found_extent) {
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05007169 ret = remove_extent_backref(trans, info, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007170 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07007171 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06007172 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007173 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007174 goto out;
7175 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007176 }
7177 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007178 if (found_extent) {
7179 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08007180 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007181 if (iref) {
7182 BUG_ON(path->slots[0] != extent_slot);
7183 } else {
7184 BUG_ON(path->slots[0] != extent_slot + 1);
7185 path->slots[0] = extent_slot;
7186 num_to_del = 2;
7187 }
Chris Mason78fae272007-03-25 11:35:08 -04007188 }
Chris Masonb9473432009-03-13 11:00:37 -04007189
Josef Bacikfcebe452014-05-13 17:30:47 -07007190 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05007191 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
7192 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06007193 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007194 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007195 goto out;
7196 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007197 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01007198
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007199 if (is_data) {
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007200 ret = btrfs_del_csums(trans, info, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06007201 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007202 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007203 goto out;
7204 }
Chris Mason459931e2008-12-10 09:10:46 -05007205 }
7206
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007207 ret = add_to_free_space_tree(trans, info, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007208 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007209 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007210 goto out;
7211 }
7212
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007213 ret = update_block_group(trans, info, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06007214 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007215 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007216 goto out;
7217 }
Chris Masona28ec192007-03-06 20:08:01 -05007218 }
Josef Bacikfcebe452014-05-13 17:30:47 -07007219 btrfs_release_path(path);
7220
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007221out:
Chris Mason5caf2a02007-04-02 11:20:42 -04007222 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05007223 return ret;
7224}
7225
7226/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007227 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04007228 * delayed ref for that extent as well. This searches the delayed ref tree for
7229 * a given extent, and if there are no other delayed refs to be processed, it
7230 * removes it from the tree.
7231 */
7232static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007233 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04007234{
7235 struct btrfs_delayed_ref_head *head;
7236 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007237 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04007238
7239 delayed_refs = &trans->transaction->delayed_refs;
7240 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08007241 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04007242 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08007243 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04007244
Josef Bacikd7df2c72014-01-23 09:21:38 -05007245 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08007246 if (!list_empty(&head->ref_list))
Chris Mason1887be62009-03-13 10:11:24 -04007247 goto out;
7248
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007249 if (head->extent_op) {
7250 if (!head->must_insert_reserved)
7251 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00007252 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007253 head->extent_op = NULL;
7254 }
7255
Chris Mason1887be62009-03-13 10:11:24 -04007256 /*
7257 * waiting for the lock here would deadlock. If someone else has it
7258 * locked they are already in the process of dropping it anyway
7259 */
7260 if (!mutex_trylock(&head->mutex))
7261 goto out;
7262
7263 /*
7264 * at this point we have a head with no other entries. Go
7265 * ahead and process it.
7266 */
Liu Boc46effa2013-10-14 12:59:45 +08007267 rb_erase(&head->href_node, &delayed_refs->href_root);
Josef Bacikd2788502017-09-29 15:43:57 -04007268 RB_CLEAR_NODE(&head->href_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05007269 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04007270
7271 /*
7272 * we don't take a ref on the node because we're removing it from the
7273 * tree, so we just steal the ref the tree was holding.
7274 */
Chris Masonc3e69d52009-03-13 10:17:05 -04007275 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007276 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04007277 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007278 head->processing = 0;
7279 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04007280 spin_unlock(&delayed_refs->lock);
7281
Yan, Zhengf0486c62010-05-16 10:46:25 -04007282 BUG_ON(head->extent_op);
7283 if (head->must_insert_reserved)
7284 ret = 1;
7285
7286 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -04007287 btrfs_put_delayed_ref_head(head);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007288 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04007289out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05007290 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08007291
7292out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04007293 spin_unlock(&delayed_refs->lock);
7294 return 0;
7295}
7296
Yan, Zhengf0486c62010-05-16 10:46:25 -04007297void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7298 struct btrfs_root *root,
7299 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02007300 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007301{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007302 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb150a4f2013-06-19 15:00:04 -04007303 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007304 int ret;
7305
7306 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07007307 int old_ref_mod, new_ref_mod;
7308
Josef Bacikfd708b82017-09-29 15:43:50 -04007309 btrfs_ref_tree_mod(root, buf->start, buf->len, parent,
7310 root->root_key.objectid,
7311 btrfs_header_level(buf), 0,
7312 BTRFS_DROP_DELAYED_REF);
Omar Sandoval7be07912017-06-06 16:45:30 -07007313 ret = btrfs_add_delayed_tree_ref(fs_info, trans, buf->start,
7314 buf->len, parent,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007315 root->root_key.objectid,
7316 btrfs_header_level(buf),
Omar Sandoval7be07912017-06-06 16:45:30 -07007317 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007318 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007319 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07007320 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007321 }
7322
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007323 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
Filipe Manana62198722015-01-06 20:18:45 +00007324 struct btrfs_block_group_cache *cache;
7325
Yan, Zhengf0486c62010-05-16 10:46:25 -04007326 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007327 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007328 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04007329 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007330 }
7331
Omar Sandoval4da8b762017-06-06 16:45:28 -07007332 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007333 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00007334
Yan, Zhengf0486c62010-05-16 10:46:25 -04007335 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007336 pin_down_extent(fs_info, cache, buf->start,
7337 buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00007338 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04007339 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007340 }
7341
7342 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
7343
7344 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08007345 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00007346 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007347 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007348 }
7349out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04007350 if (pin)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007351 add_pinned_bytes(fs_info, buf->len, btrfs_header_level(buf),
Josef Bacikb150a4f2013-06-19 15:00:04 -04007352 root->root_key.objectid);
7353
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007354 if (last_ref) {
7355 /*
7356 * Deleting the buffer, clear the corrupt flag since it doesn't
7357 * matter anymore.
7358 */
7359 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
7360 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007361}
7362
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007363/* Can return -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007364int btrfs_free_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007365 struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007366 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01007367 u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04007368{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007369 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07007370 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04007371 int ret;
7372
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04007373 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007374 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02007375
Josef Bacikfd708b82017-09-29 15:43:50 -04007376 if (root_objectid != BTRFS_TREE_LOG_OBJECTID)
7377 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent,
7378 root_objectid, owner, offset,
7379 BTRFS_DROP_DELAYED_REF);
7380
Chris Mason56bec292009-03-13 10:10:06 -04007381 /*
7382 * tree log blocks never actually go into the extent allocation
7383 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04007384 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007385 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
7386 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04007387 /* unlocks the pinned mutex */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007388 btrfs_pin_extent(fs_info, bytenr, num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07007389 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04007390 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007391 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007392 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007393 num_bytes, parent,
7394 root_objectid, (int)owner,
7395 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007396 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007397 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007398 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007399 num_bytes, parent,
7400 root_objectid, owner, offset,
7401 0, BTRFS_DROP_DELAYED_REF,
Omar Sandovald7eae342017-06-06 16:45:31 -07007402 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04007403 }
Omar Sandovald7eae342017-06-06 16:45:31 -07007404
7405 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
7406 add_pinned_bytes(fs_info, num_bytes, owner, root_objectid);
7407
Chris Mason925baed2008-06-25 16:01:30 -04007408 return ret;
7409}
7410
Chris Masonfec577f2007-02-26 10:40:21 -05007411/*
Josef Bacik817d52f2009-07-13 21:29:25 -04007412 * when we wait for progress in the block group caching, its because
7413 * our allocation attempt failed at least once. So, we must sleep
7414 * and let some progress happen before we try again.
7415 *
7416 * This function will sleep at least once waiting for new free space to
7417 * show up, and then it will check the block group free space numbers
7418 * for our min num_bytes. Another option is to have it go ahead
7419 * and look in the rbtree for a free extent of a given size, but this
7420 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04007421 *
7422 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
7423 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04007424 */
Josef Bacik36cce922013-08-05 11:15:21 -04007425static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04007426wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
7427 u64 num_bytes)
7428{
Yan Zheng11833d62009-09-11 16:11:19 -04007429 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04007430
Yan Zheng11833d62009-09-11 16:11:19 -04007431 caching_ctl = get_caching_control(cache);
7432 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007433 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04007434
Yan Zheng11833d62009-09-11 16:11:19 -04007435 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08007436 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04007437
7438 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04007439}
7440
7441static noinline int
7442wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
7443{
7444 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04007445 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007446
7447 caching_ctl = get_caching_control(cache);
7448 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007449 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007450
7451 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04007452 if (cache->cached == BTRFS_CACHE_ERROR)
7453 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04007454 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04007455 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04007456}
7457
Liu Bo31e50222012-11-21 14:18:10 +00007458int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04007459{
Ilya Dryomov7738a532012-03-27 17:09:17 +03007460 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00007461 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007462 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00007463 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007464 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00007465 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007466 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00007467 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05007468 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05007469 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05007470 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05007471 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007472
Chris Masone942f882013-02-20 14:06:05 -05007473 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04007474}
7475
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007476int get_block_group_index(struct btrfs_block_group_cache *cache)
Ilya Dryomov7738a532012-03-27 17:09:17 +03007477{
Liu Bo31e50222012-11-21 14:18:10 +00007478 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03007479}
7480
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007481static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
7482 [BTRFS_RAID_RAID10] = "raid10",
7483 [BTRFS_RAID_RAID1] = "raid1",
7484 [BTRFS_RAID_DUP] = "dup",
7485 [BTRFS_RAID_RAID0] = "raid0",
7486 [BTRFS_RAID_SINGLE] = "single",
7487 [BTRFS_RAID_RAID5] = "raid5",
7488 [BTRFS_RAID_RAID6] = "raid6",
7489};
7490
Jeff Mahoney1b8e5df2013-11-20 16:50:23 -05007491static const char *get_raid_name(enum btrfs_raid_types type)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007492{
7493 if (type >= BTRFS_NR_RAID_TYPES)
7494 return NULL;
7495
7496 return btrfs_raid_type_names[type];
7497}
7498
Josef Bacik817d52f2009-07-13 21:29:25 -04007499enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05007500 LOOP_CACHING_NOWAIT = 0,
7501 LOOP_CACHING_WAIT = 1,
7502 LOOP_ALLOC_CHUNK = 2,
7503 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04007504};
7505
Miao Xiee570fd22014-06-19 10:42:50 +08007506static inline void
7507btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
7508 int delalloc)
7509{
7510 if (delalloc)
7511 down_read(&cache->data_rwsem);
7512}
7513
7514static inline void
7515btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
7516 int delalloc)
7517{
7518 btrfs_get_block_group(cache);
7519 if (delalloc)
7520 down_read(&cache->data_rwsem);
7521}
7522
7523static struct btrfs_block_group_cache *
7524btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
7525 struct btrfs_free_cluster *cluster,
7526 int delalloc)
7527{
Sudip Mukherjee89771cc2016-02-16 13:32:47 +05307528 struct btrfs_block_group_cache *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007529
Miao Xiee570fd22014-06-19 10:42:50 +08007530 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007531 while (1) {
7532 used_bg = cluster->block_group;
7533 if (!used_bg)
7534 return NULL;
7535
7536 if (used_bg == block_group)
7537 return used_bg;
7538
7539 btrfs_get_block_group(used_bg);
7540
7541 if (!delalloc)
7542 return used_bg;
7543
7544 if (down_read_trylock(&used_bg->data_rwsem))
7545 return used_bg;
7546
7547 spin_unlock(&cluster->refill_lock);
7548
Liu Boe321f8a2016-11-30 16:11:04 -08007549 /* We should only have one-level nested. */
7550 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007551
7552 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007553 if (used_bg == cluster->block_group)
7554 return used_bg;
7555
7556 up_read(&used_bg->data_rwsem);
7557 btrfs_put_block_group(used_bg);
7558 }
Miao Xiee570fd22014-06-19 10:42:50 +08007559}
7560
7561static inline void
7562btrfs_release_block_group(struct btrfs_block_group_cache *cache,
7563 int delalloc)
7564{
7565 if (delalloc)
7566 up_read(&cache->data_rwsem);
7567 btrfs_put_block_group(cache);
7568}
7569
Josef Bacik817d52f2009-07-13 21:29:25 -04007570/*
Chris Masonfec577f2007-02-26 10:40:21 -05007571 * walks the btree of allocated extents and find a hole of a given size.
7572 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08007573 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04007574 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08007575 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05007576 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08007577 *
7578 * If there is no suitable free space, we will record the max size of
7579 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05007580 */
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05007581static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08007582 u64 ram_bytes, u64 num_bytes, u64 empty_size,
7583 u64 hint_byte, struct btrfs_key *ins,
7584 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007585{
Josef Bacik80eb2342008-10-29 14:49:05 -04007586 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007587 struct btrfs_root *root = fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007588 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04007589 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05007590 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08007591 u64 max_extent_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007592 u64 empty_cluster = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04007593 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007594 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00007595 int index = __get_raid_index(flags);
Josef Bacik0a243252009-09-11 16:11:20 -04007596 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007597 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04007598 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08007599 bool have_caching_bg = false;
chandan13a0db52015-11-02 13:59:46 +05307600 bool orig_have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007601 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05007602
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007603 WARN_ON(num_bytes < fs_info->sectorsize);
David Sterba962a2982014-06-04 18:41:45 +02007604 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04007605 ins->objectid = 0;
7606 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04007607
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007608 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05007609
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007610 space_info = __find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007611 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007612 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007613 return -ENOSPC;
7614 }
Josef Bacik2552d172009-04-03 10:14:19 -04007615
Josef Bacik67377732010-09-16 16:19:09 -04007616 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04007617 * If our free space is heavily fragmented we may not be able to make
7618 * big contiguous allocations, so instead of doing the expensive search
7619 * for free space, simply return ENOSPC with our max_extent_size so we
7620 * can go ahead and search for a more manageable chunk.
7621 *
7622 * If our max_extent_size is large enough for our allocation simply
7623 * disable clustering since we will likely not be able to find enough
7624 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04007625 */
Josef Bacik4f4db212015-09-29 11:40:47 -04007626 if (unlikely(space_info->max_extent_size)) {
7627 spin_lock(&space_info->lock);
7628 if (space_info->max_extent_size &&
7629 num_bytes > space_info->max_extent_size) {
7630 ins->offset = space_info->max_extent_size;
7631 spin_unlock(&space_info->lock);
7632 return -ENOSPC;
7633 } else if (space_info->max_extent_size) {
7634 use_cluster = false;
7635 }
7636 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007637 }
7638
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007639 last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04007640 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007641 spin_lock(&last_ptr->lock);
7642 if (last_ptr->block_group)
7643 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007644 if (last_ptr->fragmented) {
7645 /*
7646 * We still set window_start so we can keep track of the
7647 * last place we found an allocation to try and save
7648 * some time.
7649 */
7650 hint_byte = last_ptr->window_start;
7651 use_cluster = false;
7652 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007653 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007654 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007655
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007656 search_start = max(search_start, first_logical_byte(fs_info, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04007657 search_start = max(search_start, hint_byte);
Josef Bacik2552d172009-04-03 10:14:19 -04007658 if (search_start == hint_byte) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007659 block_group = btrfs_lookup_block_group(fs_info, search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04007660 /*
7661 * we don't want to use the block group if it doesn't match our
7662 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05007663 *
7664 * However if we are re-searching with an ideal block group
7665 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04007666 */
David Sterbab6919a52013-04-29 13:39:40 +00007667 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05007668 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04007669 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007670 if (list_empty(&block_group->list) ||
7671 block_group->ro) {
7672 /*
7673 * someone is removing this block group,
7674 * we can't jump into the have_block_group
7675 * target because our list pointers are not
7676 * valid
7677 */
7678 btrfs_put_block_group(block_group);
7679 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05007680 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04007681 index = get_block_group_index(block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007682 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04007683 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05007684 }
Josef Bacik2552d172009-04-03 10:14:19 -04007685 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007686 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04007687 }
Chris Mason42e70e72008-11-07 18:17:11 -05007688 }
Josef Bacik2552d172009-04-03 10:14:19 -04007689search:
Miao Xie60d2adb2011-09-09 17:34:35 +08007690 have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007691 if (index == 0 || index == __get_raid_index(flags))
7692 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04007693 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007694 list_for_each_entry(block_group, &space_info->block_groups[index],
7695 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04007696 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04007697 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05007698
Jeff Mahoney14443932017-07-19 23:25:51 -04007699 /* If the block group is read-only, we can skip it entirely. */
7700 if (unlikely(block_group->ro))
7701 continue;
7702
Miao Xiee570fd22014-06-19 10:42:50 +08007703 btrfs_grab_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007704 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05007705
Chris Mason83a50de2010-12-13 15:06:46 -05007706 /*
7707 * this can happen if we end up cycling through all the
7708 * raid types, but we want to make sure we only allocate
7709 * for the proper type.
7710 */
David Sterbab6919a52013-04-29 13:39:40 +00007711 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05007712 u64 extra = BTRFS_BLOCK_GROUP_DUP |
7713 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007714 BTRFS_BLOCK_GROUP_RAID5 |
7715 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05007716 BTRFS_BLOCK_GROUP_RAID10;
7717
7718 /*
7719 * if they asked for extra copies and this block group
7720 * doesn't provide them, bail. This does allow us to
7721 * fill raid0 from raid1.
7722 */
David Sterbab6919a52013-04-29 13:39:40 +00007723 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05007724 goto loop;
7725 }
7726
Josef Bacik2552d172009-04-03 10:14:19 -04007727have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05007728 cached = block_group_cache_done(block_group);
7729 if (unlikely(!cached)) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007730 have_caching_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00007731 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04007732 BUG_ON(ret < 0);
7733 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05007734 }
7735
Josef Bacik36cce922013-08-05 11:15:21 -04007736 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
7737 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007738
Josef Bacik0a243252009-09-11 16:11:20 -04007739 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007740 * Ok we want to try and use the cluster allocator, so
7741 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04007742 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04007743 if (last_ptr && use_cluster) {
Miao Xie215a63d2014-01-15 20:00:56 +08007744 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05007745 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007746 /*
7747 * the refill lock keeps out other
7748 * people trying to start a new cluster
7749 */
Miao Xiee570fd22014-06-19 10:42:50 +08007750 used_block_group = btrfs_lock_cluster(block_group,
7751 last_ptr,
7752 delalloc);
7753 if (!used_block_group)
Chris Mason44fb5512009-06-04 15:34:51 -04007754 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04007755
Miao Xiee570fd22014-06-19 10:42:50 +08007756 if (used_block_group != block_group &&
7757 (used_block_group->ro ||
7758 !block_group_bits(used_block_group, flags)))
7759 goto release_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007760
7761 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007762 last_ptr,
7763 num_bytes,
7764 used_block_group->key.objectid,
7765 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007766 if (offset) {
7767 /* we have a block, we're done */
7768 spin_unlock(&last_ptr->refill_lock);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007769 trace_btrfs_reserve_extent_cluster(fs_info,
Miao Xie89d43462014-01-15 20:00:57 +08007770 used_block_group,
7771 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08007772 if (used_block_group != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08007773 btrfs_release_block_group(block_group,
7774 delalloc);
Miao Xie215a63d2014-01-15 20:00:56 +08007775 block_group = used_block_group;
7776 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007777 goto checks;
7778 }
7779
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007780 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007781release_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007782 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7783 * set up a new clusters, so lets just skip it
7784 * and let the allocator find whatever block
7785 * it can find. If we reach this point, we
7786 * will have tried the cluster allocator
7787 * plenty of times and not have found
7788 * anything, so we are likely way too
7789 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007790 * anything.
7791 *
7792 * However, if the cluster is taken from the
7793 * current block group, release the cluster
7794 * first, so that we stand a better chance of
7795 * succeeding in the unclustered
7796 * allocation. */
7797 if (loop >= LOOP_NO_EMPTY_SIZE &&
Miao Xiee570fd22014-06-19 10:42:50 +08007798 used_block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007799 spin_unlock(&last_ptr->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007800 btrfs_release_block_group(used_block_group,
7801 delalloc);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007802 goto unclustered_alloc;
7803 }
7804
Chris Masonfa9c0d792009-04-03 09:47:43 -04007805 /*
7806 * this cluster didn't work out, free it and
7807 * start over
7808 */
7809 btrfs_return_cluster_to_free_space(NULL, last_ptr);
7810
Miao Xiee570fd22014-06-19 10:42:50 +08007811 if (used_block_group != block_group)
7812 btrfs_release_block_group(used_block_group,
7813 delalloc);
7814refill_cluster:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007815 if (loop >= LOOP_NO_EMPTY_SIZE) {
7816 spin_unlock(&last_ptr->refill_lock);
7817 goto unclustered_alloc;
7818 }
7819
Chris Mason8de972b2013-01-04 15:39:43 -05007820 aligned_cluster = max_t(unsigned long,
7821 empty_cluster + empty_size,
7822 block_group->full_stripe_len);
7823
Chris Masonfa9c0d792009-04-03 09:47:43 -04007824 /* allocate a cluster in this block group */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007825 ret = btrfs_find_space_cluster(fs_info, block_group,
Josef Bacik00361582013-08-14 14:02:47 -04007826 last_ptr, search_start,
7827 num_bytes,
7828 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007829 if (ret == 0) {
7830 /*
7831 * now pull our allocation out of this
7832 * cluster
7833 */
7834 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007835 last_ptr,
7836 num_bytes,
7837 search_start,
7838 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007839 if (offset) {
7840 /* we found one, proceed */
7841 spin_unlock(&last_ptr->refill_lock);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007842 trace_btrfs_reserve_extent_cluster(fs_info,
Josef Bacik3f7de032011-11-10 08:29:20 -05007843 block_group, search_start,
7844 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007845 goto checks;
7846 }
Josef Bacik0a243252009-09-11 16:11:20 -04007847 } else if (!cached && loop > LOOP_CACHING_NOWAIT
7848 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007849 spin_unlock(&last_ptr->refill_lock);
7850
Josef Bacik0a243252009-09-11 16:11:20 -04007851 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007852 wait_block_group_cache_progress(block_group,
7853 num_bytes + empty_cluster + empty_size);
7854 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007855 }
Josef Bacik817d52f2009-07-13 21:29:25 -04007856
Chris Masonfa9c0d792009-04-03 09:47:43 -04007857 /*
7858 * at this point we either didn't find a cluster
7859 * or we weren't able to allocate a block from our
7860 * cluster. Free the cluster we've been trying
7861 * to use, and go to the next block group
7862 */
Josef Bacik0a243252009-09-11 16:11:20 -04007863 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007864 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04007865 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007866 }
7867
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007868unclustered_alloc:
Josef Bacikc759c4e2015-10-02 15:25:10 -04007869 /*
7870 * We are doing an unclustered alloc, set the fragmented flag so
7871 * we don't bother trying to setup a cluster again until we get
7872 * more space.
7873 */
7874 if (unlikely(last_ptr)) {
7875 spin_lock(&last_ptr->lock);
7876 last_ptr->fragmented = 1;
7877 spin_unlock(&last_ptr->lock);
7878 }
Liu Bo0c9b36e2017-02-13 15:42:30 -08007879 if (cached) {
7880 struct btrfs_free_space_ctl *ctl =
7881 block_group->free_space_ctl;
7882
7883 spin_lock(&ctl->tree_lock);
7884 if (ctl->free_space <
7885 num_bytes + empty_cluster + empty_size) {
7886 if (ctl->free_space > max_extent_size)
7887 max_extent_size = ctl->free_space;
7888 spin_unlock(&ctl->tree_lock);
7889 goto loop;
7890 }
7891 spin_unlock(&ctl->tree_lock);
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007892 }
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007893
Josef Bacik6226cb02009-04-03 10:14:18 -04007894 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08007895 num_bytes, empty_size,
7896 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007897 /*
7898 * If we didn't find a chunk, and we haven't failed on this
7899 * block group before, and this block group is in the middle of
7900 * caching and we are ok with waiting, then go ahead and wait
7901 * for progress to be made, and set failed_alloc to true.
7902 *
7903 * If failed_alloc is true then we've already waited on this
7904 * block group once and should move on to the next block group.
7905 */
7906 if (!offset && !failed_alloc && !cached &&
7907 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007908 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007909 num_bytes + empty_size);
7910 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007911 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007912 } else if (!offset) {
7913 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04007914 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007915checks:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007916 search_start = ALIGN(offset, fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04007917
Josef Bacik2552d172009-04-03 10:14:19 -04007918 /* move on to the next group */
7919 if (search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08007920 block_group->key.objectid + block_group->key.offset) {
7921 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04007922 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04007923 }
Josef Bacik80eb2342008-10-29 14:49:05 -04007924
Josef Bacik6226cb02009-04-03 10:14:18 -04007925 if (offset < search_start)
Miao Xie215a63d2014-01-15 20:00:56 +08007926 btrfs_add_free_space(block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04007927 search_start - offset);
7928 BUG_ON(offset > search_start);
7929
Wang Xiaoguang18513092016-07-25 15:51:40 +08007930 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
7931 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007932 if (ret == -EAGAIN) {
Miao Xie215a63d2014-01-15 20:00:56 +08007933 btrfs_add_free_space(block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007934 goto loop;
7935 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007936 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04007937
Josef Bacik2552d172009-04-03 10:14:19 -04007938 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007939 ins->objectid = search_start;
7940 ins->offset = num_bytes;
7941
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007942 trace_btrfs_reserve_extent(fs_info, block_group,
Josef Bacik3f7de032011-11-10 08:29:20 -05007943 search_start, num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08007944 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007945 break;
7946loop:
Josef Bacik0a243252009-09-11 16:11:20 -04007947 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007948 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04007949 BUG_ON(index != get_block_group_index(block_group));
Miao Xiee570fd22014-06-19 10:42:50 +08007950 btrfs_release_block_group(block_group, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04007951 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04007952 }
7953 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05007954
chandan13a0db52015-11-02 13:59:46 +05307955 if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg
7956 && !orig_have_caching_bg)
7957 orig_have_caching_bg = true;
7958
Miao Xie60d2adb2011-09-09 17:34:35 +08007959 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
7960 goto search;
7961
Yan, Zhengb742bb82010-05-16 10:46:24 -04007962 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
7963 goto search;
7964
Josef Bacik285ff5a2012-01-13 15:27:45 -05007965 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05007966 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
7967 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04007968 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
7969 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
7970 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
7971 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04007972 */
Josef Bacik723bda22011-05-27 16:11:38 -04007973 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04007974 index = 0;
Josef Bacika5e681d2015-10-01 14:54:10 -04007975 if (loop == LOOP_CACHING_NOWAIT) {
7976 /*
7977 * We want to skip the LOOP_CACHING_WAIT step if we
Nicholas D Steeves01327612016-05-19 21:18:45 -04007978 * don't have any uncached bgs and we've already done a
Josef Bacika5e681d2015-10-01 14:54:10 -04007979 * full search through.
7980 */
chandan13a0db52015-11-02 13:59:46 +05307981 if (orig_have_caching_bg || !full_search)
Josef Bacika5e681d2015-10-01 14:54:10 -04007982 loop = LOOP_CACHING_WAIT;
7983 else
7984 loop = LOOP_ALLOC_CHUNK;
7985 } else {
7986 loop++;
7987 }
7988
Josef Bacik817d52f2009-07-13 21:29:25 -04007989 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04007990 struct btrfs_trans_handle *trans;
Wang Shilongf017f152014-03-13 13:19:47 +08007991 int exist = 0;
Josef Bacik00361582013-08-14 14:02:47 -04007992
Wang Shilongf017f152014-03-13 13:19:47 +08007993 trans = current->journal_info;
7994 if (trans)
7995 exist = 1;
7996 else
7997 trans = btrfs_join_transaction(root);
7998
Josef Bacik00361582013-08-14 14:02:47 -04007999 if (IS_ERR(trans)) {
8000 ret = PTR_ERR(trans);
8001 goto out;
8002 }
8003
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008004 ret = do_chunk_alloc(trans, fs_info, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04008005 CHUNK_ALLOC_FORCE);
Josef Bacika5e681d2015-10-01 14:54:10 -04008006
8007 /*
8008 * If we can't allocate a new chunk we've already looped
8009 * through at least once, move on to the NO_EMPTY_SIZE
8010 * case.
8011 */
8012 if (ret == -ENOSPC)
8013 loop = LOOP_NO_EMPTY_SIZE;
8014
Josef Bacikea658ba2012-09-11 16:57:25 -04008015 /*
8016 * Do not bail out on ENOSPC since we
8017 * can do more things.
8018 */
Josef Bacik00361582013-08-14 14:02:47 -04008019 if (ret < 0 && ret != -ENOSPC)
Jeff Mahoney66642832016-06-10 18:19:25 -04008020 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -04008021 else
8022 ret = 0;
Wang Shilongf017f152014-03-13 13:19:47 +08008023 if (!exist)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008024 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04008025 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04008026 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04008027 }
8028
8029 if (loop == LOOP_NO_EMPTY_SIZE) {
Josef Bacika5e681d2015-10-01 14:54:10 -04008030 /*
8031 * Don't loop again if we already have no empty_size and
8032 * no empty_cluster.
8033 */
8034 if (empty_size == 0 &&
8035 empty_cluster == 0) {
8036 ret = -ENOSPC;
8037 goto out;
8038 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04008039 empty_size = 0;
8040 empty_cluster = 0;
8041 }
Chris Mason42e70e72008-11-07 18:17:11 -05008042
Josef Bacik723bda22011-05-27 16:11:38 -04008043 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04008044 } else if (!ins->objectid) {
8045 ret = -ENOSPC;
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008046 } else if (ins->objectid) {
Josef Bacikc759c4e2015-10-02 15:25:10 -04008047 if (!use_cluster && last_ptr) {
8048 spin_lock(&last_ptr->lock);
8049 last_ptr->window_start = ins->objectid;
8050 spin_unlock(&last_ptr->lock);
8051 }
Josef Bacik2552d172009-04-03 10:14:19 -04008052 ret = 0;
8053 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008054out:
Josef Bacik4f4db212015-09-29 11:40:47 -04008055 if (ret == -ENOSPC) {
8056 spin_lock(&space_info->lock);
8057 space_info->max_extent_size = max_extent_size;
8058 spin_unlock(&space_info->lock);
Miao Xiea4820392013-09-09 13:19:42 +08008059 ins->offset = max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04008060 }
Chris Mason0f70abe2007-02-28 16:46:22 -05008061 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05008062}
Chris Masonec44a352008-04-28 15:29:52 -04008063
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008064static void dump_space_info(struct btrfs_fs_info *fs_info,
8065 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008066 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04008067{
8068 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04008069 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008070
Josef Bacik9ed74f22009-09-11 16:12:44 -04008071 spin_lock(&info->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008072 btrfs_info(fs_info, "space_info %llu has %llu free, is %sfull",
8073 info->flags,
Liu Bo41361352017-02-13 15:42:21 -08008074 info->total_bytes - btrfs_space_info_used(info, true),
8075 info->full ? "" : "not ");
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008076 btrfs_info(fs_info,
8077 "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu",
8078 info->total_bytes, info->bytes_used, info->bytes_pinned,
8079 info->bytes_reserved, info->bytes_may_use,
8080 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008081 spin_unlock(&info->lock);
8082
8083 if (!dump_block_groups)
8084 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008085
Josef Bacik80eb2342008-10-29 14:49:05 -04008086 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008087again:
8088 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04008089 spin_lock(&cache->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008090 btrfs_info(fs_info,
8091 "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s",
8092 cache->key.objectid, cache->key.offset,
8093 btrfs_block_group_used(&cache->item), cache->pinned,
8094 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04008095 btrfs_dump_free_space(cache, bytes);
8096 spin_unlock(&cache->lock);
8097 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008098 if (++index < BTRFS_NR_RAID_TYPES)
8099 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04008100 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008101}
Zheng Yane8569812008-09-26 10:05:48 -04008102
Wang Xiaoguang18513092016-07-25 15:51:40 +08008103int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04008104 u64 num_bytes, u64 min_alloc_size,
8105 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08008106 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05008107{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008108 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04008109 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00008110 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05008111 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04008112
Jeff Mahoney1b868262017-05-17 11:38:35 -04008113 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04008114again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008115 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3cd2017-02-15 16:28:27 -05008116 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08008117 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008118 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008119 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008120 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08008121 if (!final_tried && ins->offset) {
8122 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04008123 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008124 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05008125 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08008126 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05008127 if (num_bytes == min_alloc_size)
8128 final_tried = true;
8129 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008130 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05008131 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008132
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008133 sinfo = __find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008134 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008135 "allocation failed flags %llu, wanted %llu",
8136 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01008137 if (sinfo)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008138 dump_space_info(fs_info, sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05008139 }
Chris Mason925baed2008-06-25 16:01:30 -04008140 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008141
8142 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008143}
8144
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008145static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008146 u64 start, u64 len,
8147 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04008148{
Josef Bacik0f9dd462008-09-23 13:14:11 -04008149 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05008150 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008151
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008152 cache = btrfs_lookup_block_group(fs_info, start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008153 if (!cache) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008154 btrfs_err(fs_info, "Unable to find block group for %llu",
8155 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008156 return -ENOSPC;
8157 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05008158
Chris Masone688b7252011-10-31 20:52:39 -04008159 if (pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008160 pin_down_extent(fs_info, cache, start, len, 1);
Chris Masone688b7252011-10-31 20:52:39 -04008161 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008162 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008163 ret = btrfs_discard_extent(fs_info, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04008164 btrfs_add_free_space(cache, start, len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08008165 btrfs_free_reserved_bytes(cache, len, delalloc);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008166 trace_btrfs_reserved_extent_free(fs_info, start, len);
Chris Masone688b7252011-10-31 20:52:39 -04008167 }
Dongsheng Yang31193212014-12-12 16:44:35 +08008168
Chris Masonfa9c0d792009-04-03 09:47:43 -04008169 btrfs_put_block_group(cache);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008170 return ret;
8171}
8172
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008173int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008174 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04008175{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008176 return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04008177}
8178
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008179int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04008180 u64 start, u64 len)
8181{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008182 return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04008183}
8184
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008185static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008186 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008187 u64 parent, u64 root_objectid,
8188 u64 flags, u64 owner, u64 offset,
8189 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008190{
8191 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008192 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008193 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008194 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008195 struct extent_buffer *leaf;
8196 int type;
8197 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04008198
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008199 if (parent > 0)
8200 type = BTRFS_SHARED_DATA_REF_KEY;
8201 else
8202 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04008203
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008204 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05008205
8206 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00008207 if (!path)
8208 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05008209
Chris Masonb9473432009-03-13 11:00:37 -04008210 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008211 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8212 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008213 if (ret) {
8214 btrfs_free_path(path);
8215 return ret;
8216 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008217
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008218 leaf = path->nodes[0];
8219 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05008220 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008221 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
8222 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8223 btrfs_set_extent_flags(leaf, extent_item,
8224 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05008225
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008226 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
8227 btrfs_set_extent_inline_ref_type(leaf, iref, type);
8228 if (parent > 0) {
8229 struct btrfs_shared_data_ref *ref;
8230 ref = (struct btrfs_shared_data_ref *)(iref + 1);
8231 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8232 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
8233 } else {
8234 struct btrfs_extent_data_ref *ref;
8235 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
8236 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
8237 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
8238 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
8239 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
8240 }
Chris Mason47e4bb92008-02-01 14:51:59 -05008241
8242 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05008243 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04008244
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008245 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
8246 ins->offset);
8247 if (ret)
8248 return ret;
8249
Jeff Mahoney6202df62016-06-22 18:54:22 -04008250 ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008251 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008252 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008253 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05008254 BUG();
8255 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008256 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008257 return ret;
8258}
8259
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008260static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008261 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008262 u64 parent, u64 root_objectid,
8263 u64 flags, struct btrfs_disk_key *key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01008264 int level, struct btrfs_key *ins)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008265{
8266 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008267 struct btrfs_extent_item *extent_item;
8268 struct btrfs_tree_block_info *block_info;
8269 struct btrfs_extent_inline_ref *iref;
8270 struct btrfs_path *path;
8271 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05008272 u32 size = sizeof(*extent_item) + sizeof(*iref);
Josef Bacikfcebe452014-05-13 17:30:47 -07008273 u64 num_bytes = ins->offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008274 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05008275
8276 if (!skinny_metadata)
8277 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008278
8279 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04008280 if (!path) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008281 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008282 fs_info->nodesize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07008283 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04008284 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008285
8286 path->leave_spinning = 1;
8287 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8288 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008289 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07008290 btrfs_free_path(path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008291 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008292 fs_info->nodesize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008293 return ret;
8294 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008295
8296 leaf = path->nodes[0];
8297 extent_item = btrfs_item_ptr(leaf, path->slots[0],
8298 struct btrfs_extent_item);
8299 btrfs_set_extent_refs(leaf, extent_item, 1);
8300 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8301 btrfs_set_extent_flags(leaf, extent_item,
8302 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008303
Josef Bacik3173a182013-03-07 14:22:04 -05008304 if (skinny_metadata) {
8305 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008306 num_bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05008307 } else {
8308 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
8309 btrfs_set_tree_block_key(leaf, block_info, key);
8310 btrfs_set_tree_block_level(leaf, block_info, level);
8311 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
8312 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008313
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008314 if (parent > 0) {
8315 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
8316 btrfs_set_extent_inline_ref_type(leaf, iref,
8317 BTRFS_SHARED_BLOCK_REF_KEY);
8318 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8319 } else {
8320 btrfs_set_extent_inline_ref_type(leaf, iref,
8321 BTRFS_TREE_BLOCK_REF_KEY);
8322 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
8323 }
8324
8325 btrfs_mark_buffer_dirty(leaf);
8326 btrfs_free_path(path);
8327
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008328 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
8329 num_bytes);
8330 if (ret)
8331 return ret;
8332
Jeff Mahoney6202df62016-06-22 18:54:22 -04008333 ret = update_block_group(trans, fs_info, ins->objectid,
8334 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008335 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008336 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008337 ins->objectid, ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008338 BUG();
8339 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04008340
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008341 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008342 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008343 return ret;
8344}
8345
8346int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008347 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08008348 u64 offset, u64 ram_bytes,
8349 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008350{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008351 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008352 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04008353
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008354 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04008355
Josef Bacikfd708b82017-09-29 15:43:50 -04008356 btrfs_ref_tree_mod(root, ins->objectid, ins->offset, 0,
8357 root->root_key.objectid, owner, offset,
8358 BTRFS_ADD_DELAYED_EXTENT);
8359
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008360 ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008361 ins->offset, 0,
8362 root->root_key.objectid, owner,
Omar Sandoval7be07912017-06-06 16:45:30 -07008363 offset, ram_bytes,
8364 BTRFS_ADD_DELAYED_EXTENT, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008365 return ret;
8366}
Chris Masone02119d2008-09-05 16:13:11 -04008367
8368/*
8369 * this is used by the tree logging recovery code. It records that
8370 * an extent has been allocated and makes sure to clear the free
8371 * space cache bits as well
8372 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008373int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008374 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008375 u64 root_objectid, u64 owner, u64 offset,
8376 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04008377{
8378 int ret;
8379 struct btrfs_block_group_cache *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008380 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008381
8382 /*
8383 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04008384 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008385 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008386 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008387 ret = __exclude_logged_extent(fs_info, ins->objectid,
8388 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008389 if (ret)
8390 return ret;
8391 }
Chris Masone02119d2008-09-05 16:13:11 -04008392
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008393 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008394 if (!block_group)
8395 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04008396
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008397 space_info = block_group->space_info;
8398 spin_lock(&space_info->lock);
8399 spin_lock(&block_group->lock);
8400 space_info->bytes_reserved += ins->offset;
8401 block_group->reserved += ins->offset;
8402 spin_unlock(&block_group->lock);
8403 spin_unlock(&space_info->lock);
8404
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008405 ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008406 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04008407 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04008408 return ret;
8409}
8410
Eric Sandeen48a3b632013-04-25 20:41:01 +00008411static struct extent_buffer *
8412btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
David Sterbafe864572014-06-15 02:28:42 +02008413 u64 bytenr, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04008414{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008415 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04008416 struct extent_buffer *buf;
8417
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008418 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008419 if (IS_ERR(buf))
8420 return buf;
8421
Chris Mason65b51a02008-08-01 15:11:20 -04008422 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04008423 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04008424 btrfs_tree_lock(buf);
David Sterba7c302b42017-02-10 18:47:57 +01008425 clean_tree_block(fs_info, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05008426 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008427
8428 btrfs_set_lock_blocking(buf);
David Sterba4db8c522015-12-03 13:06:46 +01008429 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008430
Chris Masond0c803c2008-09-11 16:17:57 -04008431 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01008432 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008433 /*
8434 * we allow two log transactions at a time, use different
8435 * EXENT bit to differentiate dirty pages.
8436 */
Filipe Manana656f30d2014-09-26 12:25:56 +01008437 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008438 set_extent_dirty(&root->dirty_log_pages, buf->start,
8439 buf->start + buf->len - 1, GFP_NOFS);
8440 else
8441 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02008442 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04008443 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01008444 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04008445 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
8446 buf->start + buf->len - 1, GFP_NOFS);
8447 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04008448 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05008449 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04008450 return buf;
8451}
8452
Yan, Zhengf0486c62010-05-16 10:46:25 -04008453static struct btrfs_block_rsv *
8454use_block_rsv(struct btrfs_trans_handle *trans,
8455 struct btrfs_root *root, u32 blocksize)
8456{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008457 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008458 struct btrfs_block_rsv *block_rsv;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008459 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008460 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00008461 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008462
8463 block_rsv = get_block_rsv(trans, root);
8464
Miao Xieb586b322013-05-13 13:55:10 +00008465 if (unlikely(block_rsv->size == 0))
8466 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00008467again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04008468 ret = block_rsv_use_bytes(block_rsv, blocksize);
8469 if (!ret)
8470 return block_rsv;
8471
Miao Xieb586b322013-05-13 13:55:10 +00008472 if (block_rsv->failfast)
8473 return ERR_PTR(ret);
8474
Miao Xied88033d2013-05-13 13:55:12 +00008475 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
8476 global_updated = true;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008477 update_global_block_rsv(fs_info);
Miao Xied88033d2013-05-13 13:55:12 +00008478 goto again;
8479 }
8480
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008481 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xieb586b322013-05-13 13:55:10 +00008482 static DEFINE_RATELIMIT_STATE(_rs,
8483 DEFAULT_RATELIMIT_INTERVAL * 10,
8484 /*DEFAULT_RATELIMIT_BURST*/ 1);
8485 if (__ratelimit(&_rs))
8486 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05008487 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00008488 }
8489try_reserve:
8490 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
8491 BTRFS_RESERVE_NO_FLUSH);
8492 if (!ret)
8493 return block_rsv;
8494 /*
8495 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00008496 * the global reserve if its space type is the same as the global
8497 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00008498 */
Miao Xie5881cfc2013-05-13 13:55:11 +00008499 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
8500 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00008501 ret = block_rsv_use_bytes(global_rsv, blocksize);
8502 if (!ret)
8503 return global_rsv;
8504 }
8505 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008506}
8507
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008508static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
8509 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008510{
8511 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008512 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008513}
8514
Chris Masonfec577f2007-02-26 10:40:21 -05008515/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04008516 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08008517 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05008518 */
David Sterba4d75f8a2014-06-15 01:54:12 +02008519struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08008520 struct btrfs_root *root,
8521 u64 parent, u64 root_objectid,
8522 const struct btrfs_disk_key *key,
8523 int level, u64 hint,
8524 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05008525{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008526 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04008527 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008528 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04008529 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008530 struct btrfs_delayed_extent_op *extent_op;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008531 u64 flags = 0;
8532 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008533 u32 blocksize = fs_info->nodesize;
8534 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008535
David Sterba05653ef2016-07-15 15:23:37 +02008536#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008537 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008538 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
David Sterbafe864572014-06-15 02:28:42 +02008539 level);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008540 if (!IS_ERR(buf))
8541 root->alloc_bytenr += blocksize;
8542 return buf;
8543 }
David Sterba05653ef2016-07-15 15:23:37 +02008544#endif
David Sterbafccb84c2014-09-29 23:53:21 +02008545
Yan, Zhengf0486c62010-05-16 10:46:25 -04008546 block_rsv = use_block_rsv(trans, root, blocksize);
8547 if (IS_ERR(block_rsv))
8548 return ERR_CAST(block_rsv);
8549
Wang Xiaoguang18513092016-07-25 15:51:40 +08008550 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08008551 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008552 if (ret)
8553 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05008554
David Sterbafe864572014-06-15 02:28:42 +02008555 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
Omar Sandoval67b78592015-02-24 02:47:04 -08008556 if (IS_ERR(buf)) {
8557 ret = PTR_ERR(buf);
8558 goto out_free_reserved;
8559 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008560
8561 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
8562 if (parent == 0)
8563 parent = ins.objectid;
8564 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8565 } else
8566 BUG_ON(parent > 0);
8567
8568 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00008569 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08008570 if (!extent_op) {
8571 ret = -ENOMEM;
8572 goto out_free_buf;
8573 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008574 if (key)
8575 memcpy(&extent_op->key, key, sizeof(extent_op->key));
8576 else
8577 memset(&extent_op->key, 0, sizeof(extent_op->key));
8578 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01008579 extent_op->update_key = skinny_metadata ? false : true;
8580 extent_op->update_flags = true;
8581 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04008582 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008583
Josef Bacikfd708b82017-09-29 15:43:50 -04008584 btrfs_ref_tree_mod(root, ins.objectid, ins.offset, parent,
8585 root_objectid, level, 0,
8586 BTRFS_ADD_DELAYED_EXTENT);
Omar Sandoval7be07912017-06-06 16:45:30 -07008587 ret = btrfs_add_delayed_tree_ref(fs_info, trans, ins.objectid,
8588 ins.offset, parent,
8589 root_objectid, level,
Omar Sandoval67b78592015-02-24 02:47:04 -08008590 BTRFS_ADD_DELAYED_EXTENT,
Omar Sandoval7be07912017-06-06 16:45:30 -07008591 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08008592 if (ret)
8593 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008594 }
Chris Masonfec577f2007-02-26 10:40:21 -05008595 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008596
8597out_free_delayed:
8598 btrfs_free_delayed_extent_op(extent_op);
8599out_free_buf:
8600 free_extent_buffer(buf);
8601out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008602 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008603out_unuse:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008604 unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08008605 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05008606}
Chris Masona28ec192007-03-06 20:08:01 -05008607
Yan Zheng2c47e6052009-06-27 21:07:35 -04008608struct walk_control {
8609 u64 refs[BTRFS_MAX_LEVEL];
8610 u64 flags[BTRFS_MAX_LEVEL];
8611 struct btrfs_key update_progress;
8612 int stage;
8613 int level;
8614 int shared_level;
8615 int update_ref;
8616 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008617 int reada_slot;
8618 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008619 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008620};
8621
8622#define DROP_REFERENCE 1
8623#define UPDATE_BACKREF 2
8624
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008625static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8626 struct btrfs_root *root,
8627 struct walk_control *wc,
8628 struct btrfs_path *path)
8629{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008630 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008631 u64 bytenr;
8632 u64 generation;
8633 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008634 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008635 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008636 struct btrfs_key key;
8637 struct extent_buffer *eb;
8638 int ret;
8639 int slot;
8640 int nread = 0;
8641
8642 if (path->slots[wc->level] < wc->reada_slot) {
8643 wc->reada_count = wc->reada_count * 2 / 3;
8644 wc->reada_count = max(wc->reada_count, 2);
8645 } else {
8646 wc->reada_count = wc->reada_count * 3 / 2;
8647 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008648 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008649 }
8650
8651 eb = path->nodes[wc->level];
8652 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008653
8654 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
8655 if (nread >= wc->reada_count)
8656 break;
8657
8658 cond_resched();
8659 bytenr = btrfs_node_blockptr(eb, slot);
8660 generation = btrfs_node_ptr_generation(eb, slot);
8661
8662 if (slot == path->slots[wc->level])
8663 goto reada;
8664
8665 if (wc->stage == UPDATE_BACKREF &&
8666 generation <= root->root_key.offset)
8667 continue;
8668
Yan, Zheng94fcca92009-10-09 09:25:16 -04008669 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008670 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05008671 wc->level - 1, 1, &refs,
8672 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008673 /* We don't care about errors in readahead. */
8674 if (ret < 0)
8675 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008676 BUG_ON(refs == 0);
8677
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008678 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008679 if (refs == 1)
8680 goto reada;
8681
Yan, Zheng94fcca92009-10-09 09:25:16 -04008682 if (wc->level == 1 &&
8683 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8684 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008685 if (!wc->update_ref ||
8686 generation <= root->root_key.offset)
8687 continue;
8688 btrfs_node_key_to_cpu(eb, &key, slot);
8689 ret = btrfs_comp_cpu_keys(&key,
8690 &wc->update_progress);
8691 if (ret < 0)
8692 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008693 } else {
8694 if (wc->level == 1 &&
8695 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8696 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008697 }
8698reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008699 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008700 nread++;
8701 }
8702 wc->reada_slot = slot;
8703}
8704
Chris Mason9aca1d52007-03-13 11:09:37 -04008705/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008706 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008707 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04008708 * when wc->stage == UPDATE_BACKREF, this function updates
8709 * back refs for pointers in the block.
8710 *
8711 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04008712 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008713static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8714 struct btrfs_root *root,
8715 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008716 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04008717{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008718 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008719 int level = wc->level;
8720 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04008721 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8722 int ret;
8723
8724 if (wc->stage == UPDATE_BACKREF &&
8725 btrfs_header_owner(eb) != root->root_key.objectid)
8726 return 1;
8727
8728 /*
8729 * when reference count of tree block is 1, it won't increase
8730 * again. once full backref flag is set, we never clear it.
8731 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04008732 if (lookup_info &&
8733 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
8734 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008735 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008736 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008737 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008738 &wc->refs[level],
8739 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008740 BUG_ON(ret == -ENOMEM);
8741 if (ret)
8742 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008743 BUG_ON(wc->refs[level] == 0);
8744 }
8745
Yan Zheng2c47e6052009-06-27 21:07:35 -04008746 if (wc->stage == DROP_REFERENCE) {
8747 if (wc->refs[level] > 1)
8748 return 1;
8749
8750 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04008751 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008752 path->locks[level] = 0;
8753 }
8754 return 0;
8755 }
8756
8757 /* wc->stage == UPDATE_BACKREF */
8758 if (!(wc->flags[level] & flag)) {
8759 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07008760 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008761 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07008762 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008763 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008764 ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04008765 eb->len, flag,
8766 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008767 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008768 wc->flags[level] |= flag;
8769 }
8770
8771 /*
8772 * the block is shared by multiple trees, so it's not good to
8773 * keep the tree lock
8774 */
8775 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04008776 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008777 path->locks[level] = 0;
8778 }
8779 return 0;
8780}
8781
8782/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008783 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008784 *
8785 * when wc->stage == DROP_REFERENCE, this function checks
8786 * reference count of the block pointed to. if the block
8787 * is shared and we need update back refs for the subtree
8788 * rooted at the block, this function changes wc->stage to
8789 * UPDATE_BACKREF. if the block is shared and there is no
8790 * need to update back, this function drops the reference
8791 * to the block.
8792 *
8793 * NOTE: return value 1 means we should stop walking down.
8794 */
8795static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8796 struct btrfs_root *root,
8797 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008798 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008799{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008800 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008801 u64 bytenr;
8802 u64 generation;
8803 u64 parent;
8804 u32 blocksize;
8805 struct btrfs_key key;
8806 struct extent_buffer *next;
8807 int level = wc->level;
8808 int reada = 0;
8809 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07008810 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008811
8812 generation = btrfs_node_ptr_generation(path->nodes[level],
8813 path->slots[level]);
8814 /*
8815 * if the lower level block was created before the snapshot
8816 * was created, we know there is no need to update back refs
8817 * for the subtree
8818 */
8819 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04008820 generation <= root->root_key.offset) {
8821 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008822 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008823 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008824
8825 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008826 blocksize = fs_info->nodesize;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008827
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008828 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008829 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008830 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008831 if (IS_ERR(next))
8832 return PTR_ERR(next);
8833
Josef Bacikb2aaaa32013-07-05 17:05:38 -04008834 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
8835 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008836 reada = 1;
8837 }
8838 btrfs_tree_lock(next);
8839 btrfs_set_lock_blocking(next);
8840
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008841 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008842 &wc->refs[level - 1],
8843 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02008844 if (ret < 0)
8845 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008846
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008847 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008848 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02008849 ret = -EIO;
8850 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008851 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008852 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008853
Yan, Zheng94fcca92009-10-09 09:25:16 -04008854 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008855 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07008856 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008857 if (level == 1 &&
8858 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8859 goto skip;
8860
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008861 if (!wc->update_ref ||
8862 generation <= root->root_key.offset)
8863 goto skip;
8864
8865 btrfs_node_key_to_cpu(path->nodes[level], &key,
8866 path->slots[level]);
8867 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
8868 if (ret < 0)
8869 goto skip;
8870
8871 wc->stage = UPDATE_BACKREF;
8872 wc->shared_level = level - 1;
8873 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008874 } else {
8875 if (level == 1 &&
8876 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8877 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008878 }
8879
Chris Masonb9fab912012-05-06 07:23:47 -04008880 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008881 btrfs_tree_unlock(next);
8882 free_extent_buffer(next);
8883 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008884 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008885 }
8886
8887 if (!next) {
8888 if (reada && level == 1)
8889 reada_walk_down(trans, root, wc, path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008890 next = read_tree_block(fs_info, bytenr, generation);
Liu Bo64c043d2015-05-25 17:30:15 +08008891 if (IS_ERR(next)) {
8892 return PTR_ERR(next);
8893 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04008894 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008895 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04008896 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008897 btrfs_tree_lock(next);
8898 btrfs_set_lock_blocking(next);
8899 }
8900
8901 level--;
Josef Bacik48672682016-09-23 13:23:28 +02008902 ASSERT(level == btrfs_header_level(next));
8903 if (level != btrfs_header_level(next)) {
8904 btrfs_err(root->fs_info, "mismatched level");
8905 ret = -EIO;
8906 goto out_unlock;
8907 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008908 path->nodes[level] = next;
8909 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008910 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008911 wc->level = level;
8912 if (wc->level == 1)
8913 wc->reada_slot = 0;
8914 return 0;
8915skip:
8916 wc->refs[level - 1] = 0;
8917 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008918 if (wc->stage == DROP_REFERENCE) {
8919 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
8920 parent = path->nodes[level]->start;
8921 } else {
Josef Bacik48672682016-09-23 13:23:28 +02008922 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04008923 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02008924 if (root->root_key.objectid !=
8925 btrfs_header_owner(path->nodes[level])) {
8926 btrfs_err(root->fs_info,
8927 "mismatched block owner");
8928 ret = -EIO;
8929 goto out_unlock;
8930 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008931 parent = 0;
8932 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008933
Mark Fasheh11526512014-07-17 12:39:01 -07008934 if (need_account) {
Qu Wenruo33d1f052016-10-18 09:31:28 +08008935 ret = btrfs_qgroup_trace_subtree(trans, root, next,
8936 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07008937 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008938 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008939 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
8940 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008941 }
8942 }
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008943 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008944 parent, root->root_key.objectid,
8945 level - 1, 0);
Josef Bacik48672682016-09-23 13:23:28 +02008946 if (ret)
8947 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008948 }
Josef Bacik48672682016-09-23 13:23:28 +02008949
8950 *lookup_info = 1;
8951 ret = 1;
8952
8953out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008954 btrfs_tree_unlock(next);
8955 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02008956
8957 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008958}
8959
8960/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008961 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008962 *
8963 * when wc->stage == DROP_REFERENCE, this function drops
8964 * reference count on the block.
8965 *
8966 * when wc->stage == UPDATE_BACKREF, this function changes
8967 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8968 * to UPDATE_BACKREF previously while processing the block.
8969 *
8970 * NOTE: return value 1 means we should stop walking up.
8971 */
8972static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8973 struct btrfs_root *root,
8974 struct btrfs_path *path,
8975 struct walk_control *wc)
8976{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008977 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008978 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008979 int level = wc->level;
8980 struct extent_buffer *eb = path->nodes[level];
8981 u64 parent = 0;
8982
8983 if (wc->stage == UPDATE_BACKREF) {
8984 BUG_ON(wc->shared_level < level);
8985 if (level < wc->shared_level)
8986 goto out;
8987
Yan Zheng2c47e6052009-06-27 21:07:35 -04008988 ret = find_next_key(path, level + 1, &wc->update_progress);
8989 if (ret > 0)
8990 wc->update_ref = 0;
8991
8992 wc->stage = DROP_REFERENCE;
8993 wc->shared_level = -1;
8994 path->slots[level] = 0;
8995
8996 /*
8997 * check reference count again if the block isn't locked.
8998 * we should start walking down the tree again if reference
8999 * count is one.
9000 */
9001 if (!path->locks[level]) {
9002 BUG_ON(level == 0);
9003 btrfs_tree_lock(eb);
9004 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04009005 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009006
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009007 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05009008 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009009 &wc->refs[level],
9010 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009011 if (ret < 0) {
9012 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00009013 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009014 return ret;
9015 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009016 BUG_ON(wc->refs[level] == 0);
9017 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04009018 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00009019 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009020 return 1;
9021 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009022 }
9023 }
9024
9025 /* wc->stage == DROP_REFERENCE */
9026 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
9027
9028 if (wc->refs[level] == 1) {
9029 if (level == 0) {
9030 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07009031 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009032 else
Josef Bacike339a6b2014-07-02 10:54:25 -07009033 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009034 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009035 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb);
Mark Fasheh11526512014-07-17 12:39:01 -07009036 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009037 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009038 "error %d accounting leaf items. Quota is out of sync, rescan required.",
9039 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07009040 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009041 }
9042 /* make block locked assertion in clean_tree_block happy */
9043 if (!path->locks[level] &&
9044 btrfs_header_generation(eb) == trans->transid) {
9045 btrfs_tree_lock(eb);
9046 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04009047 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009048 }
David Sterba7c302b42017-02-10 18:47:57 +01009049 clean_tree_block(fs_info, eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009050 }
9051
9052 if (eb == root->node) {
9053 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
9054 parent = eb->start;
9055 else
9056 BUG_ON(root->root_key.objectid !=
9057 btrfs_header_owner(eb));
9058 } else {
9059 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
9060 parent = path->nodes[level + 1]->start;
9061 else
9062 BUG_ON(root->root_key.objectid !=
9063 btrfs_header_owner(path->nodes[level + 1]));
9064 }
9065
Jan Schmidt5581a512012-05-16 17:04:52 +02009066 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009067out:
9068 wc->refs[level] = 0;
9069 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009070 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009071}
9072
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009073static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
9074 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009075 struct btrfs_path *path,
9076 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04009077{
Yan Zheng2c47e6052009-06-27 21:07:35 -04009078 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04009079 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009080 int ret;
9081
Yan Zheng2c47e6052009-06-27 21:07:35 -04009082 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04009083 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009084 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04009085 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009086
Yan Zheng2c47e6052009-06-27 21:07:35 -04009087 if (level == 0)
9088 break;
9089
Yan, Zheng7a7965f2010-02-01 02:41:17 +00009090 if (path->slots[level] >=
9091 btrfs_header_nritems(path->nodes[level]))
9092 break;
9093
Yan, Zheng94fcca92009-10-09 09:25:16 -04009094 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009095 if (ret > 0) {
9096 path->slots[level]++;
9097 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00009098 } else if (ret < 0)
9099 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009100 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009101 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009102 return 0;
9103}
9104
Chris Masond3977122009-01-05 21:25:51 -05009105static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05009106 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04009107 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009108 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05009109{
Yan Zheng2c47e6052009-06-27 21:07:35 -04009110 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05009111 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04009112
Yan Zheng2c47e6052009-06-27 21:07:35 -04009113 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
9114 while (level < max_level && path->nodes[level]) {
9115 wc->level = level;
9116 if (path->slots[level] + 1 <
9117 btrfs_header_nritems(path->nodes[level])) {
9118 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05009119 return 0;
9120 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009121 ret = walk_up_proc(trans, root, path, wc);
9122 if (ret > 0)
9123 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05009124
Yan Zheng2c47e6052009-06-27 21:07:35 -04009125 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04009126 btrfs_tree_unlock_rw(path->nodes[level],
9127 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009128 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009129 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009130 free_extent_buffer(path->nodes[level]);
9131 path->nodes[level] = NULL;
9132 level++;
Chris Mason20524f02007-03-10 06:35:47 -05009133 }
9134 }
9135 return 1;
9136}
9137
Chris Mason9aca1d52007-03-13 11:09:37 -04009138/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04009139 * drop a subvolume tree.
9140 *
9141 * this function traverses the tree freeing any blocks that only
9142 * referenced by the tree.
9143 *
9144 * when a shared tree block is found. this function decreases its
9145 * reference count by one. if update_ref is true, this function
9146 * also make sure backrefs for the shared block and all lower level
9147 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00009148 *
9149 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04009150 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04009151int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009152 struct btrfs_block_rsv *block_rsv, int update_ref,
9153 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05009154{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009155 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04009156 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009157 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009158 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04009159 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009160 struct walk_control *wc;
9161 struct btrfs_key key;
9162 int err = 0;
9163 int ret;
9164 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04009165 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05009166
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009167 btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07009168
Chris Mason5caf2a02007-04-02 11:20:42 -04009169 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009170 if (!path) {
9171 err = -ENOMEM;
9172 goto out;
9173 }
Chris Mason20524f02007-03-10 06:35:47 -05009174
Yan Zheng2c47e6052009-06-27 21:07:35 -04009175 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07009176 if (!wc) {
9177 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009178 err = -ENOMEM;
9179 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07009180 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009181
Yan, Zhenga22285a2010-05-16 10:48:46 -04009182 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009183 if (IS_ERR(trans)) {
9184 err = PTR_ERR(trans);
9185 goto out_free;
9186 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00009187
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009188 if (block_rsv)
9189 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009190
Chris Mason9f3a7422007-08-07 15:52:19 -04009191 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009192 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009193 path->nodes[level] = btrfs_lock_root_node(root);
9194 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04009195 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009196 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009197 memset(&wc->update_progress, 0,
9198 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04009199 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04009200 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009201 memcpy(&wc->update_progress, &key,
9202 sizeof(wc->update_progress));
9203
Chris Mason6702ed42007-08-07 16:15:09 -04009204 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009205 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04009206 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009207 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
9208 path->lowest_level = 0;
9209 if (ret < 0) {
9210 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009211 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04009212 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009213 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009214
Chris Mason7d9eb122008-07-08 14:19:17 -04009215 /*
9216 * unlock our path, this is safe because only this
9217 * function is allowed to delete this snapshot
9218 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009219 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04009220
Yan Zheng2c47e6052009-06-27 21:07:35 -04009221 level = btrfs_header_level(root->node);
9222 while (1) {
9223 btrfs_tree_lock(path->nodes[level]);
9224 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009225 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009226
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009227 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009228 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05009229 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04009230 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009231 if (ret < 0) {
9232 err = ret;
9233 goto out_end_trans;
9234 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009235 BUG_ON(wc->refs[level] == 0);
9236
9237 if (level == root_item->drop_level)
9238 break;
9239
9240 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009241 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009242 WARN_ON(wc->refs[level] != 1);
9243 level--;
9244 }
9245 }
9246
9247 wc->level = level;
9248 wc->shared_level = -1;
9249 wc->stage = DROP_REFERENCE;
9250 wc->update_ref = update_ref;
9251 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009252 wc->for_reloc = for_reloc;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009253 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009254
9255 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00009256
Yan Zheng2c47e6052009-06-27 21:07:35 -04009257 ret = walk_down_tree(trans, root, path, wc);
9258 if (ret < 0) {
9259 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04009260 break;
9261 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009262
9263 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
9264 if (ret < 0) {
9265 err = ret;
9266 break;
9267 }
9268
9269 if (ret > 0) {
9270 BUG_ON(wc->stage != DROP_REFERENCE);
9271 break;
9272 }
9273
9274 if (wc->stage == DROP_REFERENCE) {
9275 level = wc->level;
9276 btrfs_node_key(path->nodes[level],
9277 &root_item->drop_progress,
9278 path->slots[level]);
9279 root_item->drop_level = level;
9280 }
9281
9282 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009283 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009284 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009285 ret = btrfs_update_root(trans, tree_root,
9286 &root->root_key,
9287 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009288 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009289 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009290 err = ret;
9291 goto out_end_trans;
9292 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009293
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009294 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009295 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009296 btrfs_debug(fs_info,
9297 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04009298 err = -EAGAIN;
9299 goto out_free;
9300 }
9301
Yan, Zhenga22285a2010-05-16 10:48:46 -04009302 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009303 if (IS_ERR(trans)) {
9304 err = PTR_ERR(trans);
9305 goto out_free;
9306 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009307 if (block_rsv)
9308 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04009309 }
Chris Mason20524f02007-03-10 06:35:47 -05009310 }
David Sterbab3b4aa72011-04-21 01:20:15 +02009311 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009312 if (err)
9313 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009314
Jeff Mahoney1cd54472017-08-17 10:25:11 -04009315 ret = btrfs_del_root(trans, fs_info, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009316 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009317 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009318 goto out_end_trans;
9319 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009320
Yan, Zheng76dda932009-09-21 16:00:26 -04009321 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00009322 ret = btrfs_find_root(tree_root, &root->root_key, path,
9323 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009324 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009325 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009326 err = ret;
9327 goto out_end_trans;
9328 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05009329 /* if we fail to delete the orphan item this time
9330 * around, it'll get picked up the next time.
9331 *
9332 * The most common failure here is just -ENOENT.
9333 */
9334 btrfs_del_orphan_item(trans, tree_root,
9335 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04009336 }
9337 }
9338
Miao Xie27cdeb72014-04-02 19:51:05 +08009339 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04009340 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009341 } else {
9342 free_extent_buffer(root->node);
9343 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00009344 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009345 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04009346 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009347out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009348 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009349out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04009350 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04009351 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009352out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04009353 /*
9354 * So if we need to stop dropping the snapshot for whatever reason we
9355 * need to make sure to add it back to the dead root list so that we
9356 * keep trying to do the work later. This also cleans up roots if we
9357 * don't have it in the radix (like when we recover after a power fail
9358 * or unmount) so we don't leak memory.
9359 */
Thomas Meyer897ca812017-10-07 16:02:21 +02009360 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04009361 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08009362 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009363 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04009364 return err;
Chris Mason20524f02007-03-10 06:35:47 -05009365}
Chris Mason9078a3e2007-04-26 16:46:15 -04009366
Yan Zheng2c47e6052009-06-27 21:07:35 -04009367/*
9368 * drop subtree rooted at tree block 'node'.
9369 *
9370 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009371 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04009372 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04009373int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
9374 struct btrfs_root *root,
9375 struct extent_buffer *node,
9376 struct extent_buffer *parent)
9377{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009378 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009379 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009380 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009381 int level;
9382 int parent_level;
9383 int ret = 0;
9384 int wret;
9385
Yan Zheng2c47e6052009-06-27 21:07:35 -04009386 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
9387
Yan Zhengf82d02d2008-10-29 14:49:05 -04009388 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009389 if (!path)
9390 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009391
Yan Zheng2c47e6052009-06-27 21:07:35 -04009392 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009393 if (!wc) {
9394 btrfs_free_path(path);
9395 return -ENOMEM;
9396 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009397
Chris Masonb9447ef2009-03-09 11:45:38 -04009398 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009399 parent_level = btrfs_header_level(parent);
9400 extent_buffer_get(parent);
9401 path->nodes[parent_level] = parent;
9402 path->slots[parent_level] = btrfs_header_nritems(parent);
9403
Chris Masonb9447ef2009-03-09 11:45:38 -04009404 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009405 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009406 path->nodes[level] = node;
9407 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009408 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009409
9410 wc->refs[parent_level] = 1;
9411 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
9412 wc->level = level;
9413 wc->shared_level = -1;
9414 wc->stage = DROP_REFERENCE;
9415 wc->update_ref = 0;
9416 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009417 wc->for_reloc = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009418 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009419
9420 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009421 wret = walk_down_tree(trans, root, path, wc);
9422 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04009423 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009424 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009425 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009426
Yan Zheng2c47e6052009-06-27 21:07:35 -04009427 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009428 if (wret < 0)
9429 ret = wret;
9430 if (wret != 0)
9431 break;
9432 }
9433
Yan Zheng2c47e6052009-06-27 21:07:35 -04009434 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009435 btrfs_free_path(path);
9436 return ret;
9437}
9438
Jeff Mahoney6202df62016-06-22 18:54:22 -04009439static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04009440{
9441 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009442 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04009443
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009444 /*
9445 * if restripe for this chunk_type is on pick target profile and
9446 * return, otherwise do the usual balance
9447 */
Jeff Mahoney6202df62016-06-22 18:54:22 -04009448 stripped = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009449 if (stripped)
9450 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02009451
Jeff Mahoney6202df62016-06-22 18:54:22 -04009452 num_devices = fs_info->fs_devices->rw_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05009453
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009454 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05009455 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009456 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
9457
Chris Masonec44a352008-04-28 15:29:52 -04009458 if (num_devices == 1) {
9459 stripped |= BTRFS_BLOCK_GROUP_DUP;
9460 stripped = flags & ~stripped;
9461
9462 /* turn raid0 into single device chunks */
9463 if (flags & BTRFS_BLOCK_GROUP_RAID0)
9464 return stripped;
9465
9466 /* turn mirroring into duplication */
9467 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
9468 BTRFS_BLOCK_GROUP_RAID10))
9469 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04009470 } else {
9471 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04009472 if (flags & stripped)
9473 return flags;
9474
9475 stripped |= BTRFS_BLOCK_GROUP_DUP;
9476 stripped = flags & ~stripped;
9477
9478 /* switch duplicated blocks with raid1 */
9479 if (flags & BTRFS_BLOCK_GROUP_DUP)
9480 return stripped | BTRFS_BLOCK_GROUP_RAID1;
9481
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009482 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04009483 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009484
Chris Masonec44a352008-04-28 15:29:52 -04009485 return flags;
9486}
9487
Zhaolei868f4012015-08-05 16:43:27 +08009488static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04009489{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009490 struct btrfs_space_info *sinfo = cache->space_info;
9491 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00009492 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009493 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04009494
Miao Xie199c36e2011-07-15 10:34:36 +00009495 /*
9496 * We need some metadata space and system metadata space for
9497 * allocating chunks in some corner cases until we force to set
9498 * it to be readonly.
9499 */
9500 if ((sinfo->flags &
9501 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
9502 !force)
Byongho Leeee221842015-12-15 01:42:10 +09009503 min_allocable_bytes = SZ_1M;
Miao Xie199c36e2011-07-15 10:34:36 +00009504 else
9505 min_allocable_bytes = 0;
9506
Yan, Zhengf0486c62010-05-16 10:46:25 -04009507 spin_lock(&sinfo->lock);
9508 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00009509
9510 if (cache->ro) {
Zhaolei868f4012015-08-05 16:43:27 +08009511 cache->ro++;
WuBo61cfea92011-07-26 03:30:11 +00009512 ret = 0;
9513 goto out;
9514 }
9515
Yan, Zhengf0486c62010-05-16 10:46:25 -04009516 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
9517 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04009518
Liu Bo41361352017-02-13 15:42:21 -08009519 if (btrfs_space_info_used(sinfo, true) + num_bytes +
Josef Bacik37be25b2011-08-05 10:25:38 -04009520 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04009521 sinfo->bytes_readonly += num_bytes;
Zhaolei868f4012015-08-05 16:43:27 +08009522 cache->ro++;
Josef Bacik633c0aa2014-10-31 09:49:34 -04009523 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009524 ret = 0;
9525 }
WuBo61cfea92011-07-26 03:30:11 +00009526out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04009527 spin_unlock(&cache->lock);
9528 spin_unlock(&sinfo->lock);
9529 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04009530}
9531
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009532int btrfs_inc_block_group_ro(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04009533 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009534
9535{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009536 struct btrfs_trans_handle *trans;
9537 u64 alloc_flags;
9538 int ret;
9539
Chris Mason1bbc6212015-04-06 12:46:08 -07009540again:
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009541 trans = btrfs_join_transaction(fs_info->extent_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009542 if (IS_ERR(trans))
9543 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009544
Chris Mason1bbc6212015-04-06 12:46:08 -07009545 /*
9546 * we're not allowed to set block groups readonly after the dirty
9547 * block groups cache has started writing. If it already started,
9548 * back off and let this transaction commit
9549 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009550 mutex_lock(&fs_info->ro_block_group_mutex);
Josef Bacik3204d332015-09-24 10:46:10 -04009551 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
Chris Mason1bbc6212015-04-06 12:46:08 -07009552 u64 transid = trans->transid;
9553
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009554 mutex_unlock(&fs_info->ro_block_group_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009555 btrfs_end_transaction(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07009556
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009557 ret = btrfs_wait_for_commit(fs_info, transid);
Chris Mason1bbc6212015-04-06 12:46:08 -07009558 if (ret)
9559 return ret;
9560 goto again;
9561 }
9562
Chris Mason153c35b2015-05-19 18:54:41 -07009563 /*
9564 * if we are changing raid levels, try to allocate a corresponding
9565 * block group with the new raid level.
9566 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009567 alloc_flags = update_block_group_flags(fs_info, cache->flags);
Chris Mason153c35b2015-05-19 18:54:41 -07009568 if (alloc_flags != cache->flags) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009569 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason153c35b2015-05-19 18:54:41 -07009570 CHUNK_ALLOC_FORCE);
9571 /*
9572 * ENOSPC is allowed here, we may have enough space
9573 * already allocated at the new raid level to
9574 * carry on
9575 */
9576 if (ret == -ENOSPC)
9577 ret = 0;
9578 if (ret < 0)
9579 goto out;
9580 }
Chris Mason1bbc6212015-04-06 12:46:08 -07009581
Zhaolei868f4012015-08-05 16:43:27 +08009582 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009583 if (!ret)
9584 goto out;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009585 alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags);
9586 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04009587 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009588 if (ret < 0)
9589 goto out;
Zhaolei868f4012015-08-05 16:43:27 +08009590 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009591out:
Shaohua Li2f081082015-01-09 10:40:15 -08009592 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009593 alloc_flags = update_block_group_flags(fs_info, cache->flags);
David Sterba34441362016-10-04 19:34:27 +02009594 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009595 check_system_chunk(trans, fs_info, alloc_flags);
David Sterba34441362016-10-04 19:34:27 +02009596 mutex_unlock(&fs_info->chunk_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009597 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009598 mutex_unlock(&fs_info->ro_block_group_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009599
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009600 btrfs_end_transaction(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009601 return ret;
9602}
9603
Chris Masonc87f08c2011-02-16 13:57:04 -05009604int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009605 struct btrfs_fs_info *fs_info, u64 type)
Chris Masonc87f08c2011-02-16 13:57:04 -05009606{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009607 u64 alloc_flags = get_alloc_profile(fs_info, type);
9608
9609 return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05009610}
9611
Miao Xie6d07bce2011-01-05 10:07:31 +00009612/*
9613 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04009614 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00009615 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009616u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00009617{
9618 struct btrfs_block_group_cache *block_group;
9619 u64 free_bytes = 0;
9620 int factor;
9621
Nicholas D Steeves01327612016-05-19 21:18:45 -04009622 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009623 if (list_empty(&sinfo->ro_bgs))
9624 return 0;
9625
9626 spin_lock(&sinfo->lock);
9627 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00009628 spin_lock(&block_group->lock);
9629
9630 if (!block_group->ro) {
9631 spin_unlock(&block_group->lock);
9632 continue;
9633 }
9634
9635 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
9636 BTRFS_BLOCK_GROUP_RAID10 |
9637 BTRFS_BLOCK_GROUP_DUP))
9638 factor = 2;
9639 else
9640 factor = 1;
9641
9642 free_bytes += (block_group->key.offset -
9643 btrfs_block_group_used(&block_group->item)) *
9644 factor;
9645
9646 spin_unlock(&block_group->lock);
9647 }
Miao Xie6d07bce2011-01-05 10:07:31 +00009648 spin_unlock(&sinfo->lock);
9649
9650 return free_bytes;
9651}
9652
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009653void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache)
Yan, Zhengf0486c62010-05-16 10:46:25 -04009654{
9655 struct btrfs_space_info *sinfo = cache->space_info;
9656 u64 num_bytes;
9657
9658 BUG_ON(!cache->ro);
9659
9660 spin_lock(&sinfo->lock);
9661 spin_lock(&cache->lock);
Zhaolei868f4012015-08-05 16:43:27 +08009662 if (!--cache->ro) {
9663 num_bytes = cache->key.offset - cache->reserved -
9664 cache->pinned - cache->bytes_super -
9665 btrfs_block_group_used(&cache->item);
9666 sinfo->bytes_readonly -= num_bytes;
9667 list_del_init(&cache->ro_list);
9668 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009669 spin_unlock(&cache->lock);
9670 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009671}
9672
Josef Bacikba1bf482009-09-11 16:11:19 -04009673/*
9674 * checks to see if its even possible to relocate this block group.
9675 *
9676 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9677 * ok to go ahead and try.
9678 */
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009679int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04009680{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009681 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04009682 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04009683 struct btrfs_space_info *space_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009684 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacikba1bf482009-09-11 16:11:19 -04009685 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04009686 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00009687 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04009688 u64 dev_min = 1;
9689 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009690 u64 target;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009691 int debug;
liubocdcb7252011-08-03 10:15:25 +00009692 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04009693 int full = 0;
9694 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05009695
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009696 debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG);
Qu Wenruo0305bc22016-03-23 11:38:17 +08009697
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009698 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04009699
Josef Bacikba1bf482009-09-11 16:11:19 -04009700 /* odd, couldn't find the block group, leave it alone */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009701 if (!block_group) {
9702 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009703 btrfs_warn(fs_info,
Qu Wenruo0305bc22016-03-23 11:38:17 +08009704 "can't find block group for bytenr %llu",
9705 bytenr);
Josef Bacikba1bf482009-09-11 16:11:19 -04009706 return -1;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009707 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009708
liubocdcb7252011-08-03 10:15:25 +00009709 min_free = btrfs_block_group_used(&block_group->item);
9710
Josef Bacikba1bf482009-09-11 16:11:19 -04009711 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00009712 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04009713 goto out;
Chris Mason323da792008-05-09 11:46:48 -04009714
Josef Bacikba1bf482009-09-11 16:11:19 -04009715 space_info = block_group->space_info;
9716 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04009717
Josef Bacikba1bf482009-09-11 16:11:19 -04009718 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04009719
Josef Bacikba1bf482009-09-11 16:11:19 -04009720 /*
9721 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04009722 * relocate it unless we're able to allocate a new chunk below.
9723 *
9724 * Otherwise, we need to make sure we have room in the space to handle
9725 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04009726 */
Chris Mason7ce618d2009-09-22 14:48:44 -04009727 if ((space_info->total_bytes != block_group->key.offset) &&
Liu Bo41361352017-02-13 15:42:21 -08009728 (btrfs_space_info_used(space_info, false) + min_free <
9729 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04009730 spin_unlock(&space_info->lock);
9731 goto out;
9732 }
9733 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009734
Josef Bacikba1bf482009-09-11 16:11:19 -04009735 /*
9736 * ok we don't have enough space, but maybe we have free space on our
9737 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009738 * alloc devices and guess if we have enough space. if this block
9739 * group is going to be restriped, run checks against the target
9740 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04009741 */
9742 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05009743
liubocdcb7252011-08-03 10:15:25 +00009744 /*
9745 * index:
9746 * 0: raid10
9747 * 1: raid1
9748 * 2: dup
9749 * 3: raid0
9750 * 4: single
9751 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009752 target = get_restripe_target(fs_info, block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009753 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00009754 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009755 } else {
9756 /*
9757 * this is just a balance, so if we were marked as full
9758 * we know there is no space for a new chunk
9759 */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009760 if (full) {
9761 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009762 btrfs_warn(fs_info,
9763 "no space to alloc new chunk for block group %llu",
9764 block_group->key.objectid);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009765 goto out;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009766 }
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009767
9768 index = get_block_group_index(block_group);
9769 }
9770
Miao Xiee6ec7162013-01-17 05:38:51 +00009771 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00009772 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04009773 /* Divide by 2 */
9774 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009775 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00009776 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00009777 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04009778 /* Multiply by 2 */
9779 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009780 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00009781 dev_min = fs_devices->rw_devices;
David Sterba47c57132015-02-20 18:43:47 +01009782 min_free = div64_u64(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00009783 }
9784
Josef Bacik6df9a952013-06-27 13:22:46 -04009785 /* We need to do this so that we can look at pending chunks */
9786 trans = btrfs_join_transaction(root);
9787 if (IS_ERR(trans)) {
9788 ret = PTR_ERR(trans);
9789 goto out;
9790 }
9791
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009792 mutex_lock(&fs_info->chunk_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04009793 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00009794 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04009795
Josef Bacikba1bf482009-09-11 16:11:19 -04009796 /*
9797 * check to make sure we can actually find a chunk with enough
9798 * space to fit our block group in.
9799 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01009800 if (device->total_bytes > device->bytes_used + min_free &&
9801 !device->is_tgtdev_for_dev_replace) {
Josef Bacik6df9a952013-06-27 13:22:46 -04009802 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00009803 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04009804 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00009805 dev_nr++;
9806
9807 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05009808 break;
liubocdcb7252011-08-03 10:15:25 +00009809
Josef Bacikba1bf482009-09-11 16:11:19 -04009810 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05009811 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009812 }
Qu Wenruo0305bc22016-03-23 11:38:17 +08009813 if (debug && ret == -1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009814 btrfs_warn(fs_info,
9815 "no space to allocate a new chunk for block group %llu",
9816 block_group->key.objectid);
9817 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009818 btrfs_end_transaction(trans);
Chris Masonedbd8d42007-12-21 16:27:24 -05009819out:
Josef Bacikba1bf482009-09-11 16:11:19 -04009820 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05009821 return ret;
9822}
9823
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009824static int find_first_block_group(struct btrfs_fs_info *fs_info,
9825 struct btrfs_path *path,
9826 struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04009827{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009828 struct btrfs_root *root = fs_info->extent_root;
Chris Mason925baed2008-06-25 16:01:30 -04009829 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009830 struct btrfs_key found_key;
9831 struct extent_buffer *leaf;
9832 int slot;
9833
9834 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
9835 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009836 goto out;
9837
Chris Masond3977122009-01-05 21:25:51 -05009838 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009839 slot = path->slots[0];
9840 leaf = path->nodes[0];
9841 if (slot >= btrfs_header_nritems(leaf)) {
9842 ret = btrfs_next_leaf(root, path);
9843 if (ret == 0)
9844 continue;
9845 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009846 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04009847 break;
9848 }
9849 btrfs_item_key_to_cpu(leaf, &found_key, slot);
9850
9851 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04009852 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
Liu Bo6fb37b72016-06-22 18:31:27 -07009853 struct extent_map_tree *em_tree;
9854 struct extent_map *em;
9855
9856 em_tree = &root->fs_info->mapping_tree.map_tree;
9857 read_lock(&em_tree->lock);
9858 em = lookup_extent_mapping(em_tree, found_key.objectid,
9859 found_key.offset);
9860 read_unlock(&em_tree->lock);
9861 if (!em) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009862 btrfs_err(fs_info,
Liu Bo6fb37b72016-06-22 18:31:27 -07009863 "logical %llu len %llu found bg but no related chunk",
9864 found_key.objectid, found_key.offset);
9865 ret = -ENOENT;
9866 } else {
9867 ret = 0;
9868 }
Josef Bacik187ee582016-08-18 15:30:06 -04009869 free_extent_map(em);
Chris Mason925baed2008-06-25 16:01:30 -04009870 goto out;
9871 }
Chris Mason0b86a832008-03-24 15:01:56 -04009872 path->slots[0]++;
9873 }
Chris Mason925baed2008-06-25 16:01:30 -04009874out:
Chris Mason0b86a832008-03-24 15:01:56 -04009875 return ret;
9876}
9877
Josef Bacik0af3d002010-06-21 14:48:16 -04009878void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9879{
9880 struct btrfs_block_group_cache *block_group;
9881 u64 last = 0;
9882
9883 while (1) {
9884 struct inode *inode;
9885
9886 block_group = btrfs_lookup_first_block_group(info, last);
9887 while (block_group) {
9888 spin_lock(&block_group->lock);
9889 if (block_group->iref)
9890 break;
9891 spin_unlock(&block_group->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009892 block_group = next_block_group(info, block_group);
Josef Bacik0af3d002010-06-21 14:48:16 -04009893 }
9894 if (!block_group) {
9895 if (last == 0)
9896 break;
9897 last = 0;
9898 continue;
9899 }
9900
9901 inode = block_group->inode;
9902 block_group->iref = 0;
9903 block_group->inode = NULL;
9904 spin_unlock(&block_group->lock);
Liu Bof3bca802016-07-20 17:33:44 -07009905 ASSERT(block_group->io_ctl.inode == NULL);
Josef Bacik0af3d002010-06-21 14:48:16 -04009906 iput(inode);
9907 last = block_group->key.objectid + block_group->key.offset;
9908 btrfs_put_block_group(block_group);
9909 }
9910}
9911
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009912/*
9913 * Must be called only after stopping all workers, since we could have block
9914 * group caching kthreads running, and therefore they could race with us if we
9915 * freed the block groups before stopping them.
9916 */
Zheng Yan1a40e232008-09-26 10:09:34 -04009917int btrfs_free_block_groups(struct btrfs_fs_info *info)
9918{
9919 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04009920 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04009921 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04009922 struct rb_node *n;
9923
Josef Bacik9e351cc2014-03-13 15:42:13 -04009924 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009925 while (!list_empty(&info->caching_block_groups)) {
9926 caching_ctl = list_entry(info->caching_block_groups.next,
9927 struct btrfs_caching_control, list);
9928 list_del(&caching_ctl->list);
9929 put_caching_control(caching_ctl);
9930 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04009931 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009932
Josef Bacik47ab2a62014-09-18 11:20:02 -04009933 spin_lock(&info->unused_bgs_lock);
9934 while (!list_empty(&info->unused_bgs)) {
9935 block_group = list_first_entry(&info->unused_bgs,
9936 struct btrfs_block_group_cache,
9937 bg_list);
9938 list_del_init(&block_group->bg_list);
9939 btrfs_put_block_group(block_group);
9940 }
9941 spin_unlock(&info->unused_bgs_lock);
9942
Zheng Yan1a40e232008-09-26 10:09:34 -04009943 spin_lock(&info->block_group_cache_lock);
9944 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
9945 block_group = rb_entry(n, struct btrfs_block_group_cache,
9946 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04009947 rb_erase(&block_group->cache_node,
9948 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009949 RB_CLEAR_NODE(&block_group->cache_node);
Yan Zhengd899e052008-10-30 14:25:28 -04009950 spin_unlock(&info->block_group_cache_lock);
9951
Josef Bacik80eb2342008-10-29 14:49:05 -04009952 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04009953 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04009954 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05009955
Josef Bacik3c148742011-02-02 15:53:47 +00009956 /*
9957 * We haven't cached this block group, which means we could
9958 * possibly have excluded extents on this block group.
9959 */
Josef Bacik36cce922013-08-05 11:15:21 -04009960 if (block_group->cached == BTRFS_CACHE_NO ||
9961 block_group->cached == BTRFS_CACHE_ERROR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009962 free_excluded_extents(info, block_group);
Josef Bacik3c148742011-02-02 15:53:47 +00009963
Josef Bacik817d52f2009-07-13 21:29:25 -04009964 btrfs_remove_free_space_cache(block_group);
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009965 ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
Liu Bof3bca802016-07-20 17:33:44 -07009966 ASSERT(list_empty(&block_group->dirty_list));
9967 ASSERT(list_empty(&block_group->io_list));
9968 ASSERT(list_empty(&block_group->bg_list));
9969 ASSERT(atomic_read(&block_group->count) == 1);
Josef Bacik11dfe352009-11-13 20:12:59 +00009970 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04009971
9972 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009973 }
9974 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04009975
9976 /* now that all the block groups are freed, go through and
9977 * free all the space_info structs. This is only called during
9978 * the final stages of unmount, and so we know nobody is
9979 * using them. We call synchronize_rcu() once before we start,
9980 * just to be on the safe side.
9981 */
9982 synchronize_rcu();
9983
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04009984 release_global_block_rsv(info);
9985
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309986 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009987 int i;
9988
Chris Mason4184ea72009-03-10 12:39:20 -04009989 space_info = list_entry(info->space_info.next,
9990 struct btrfs_space_info,
9991 list);
Josef Bacikd555b6c2016-03-25 13:25:51 -04009992
9993 /*
9994 * Do not hide this behind enospc_debug, this is actually
9995 * important and indicates a real bug if this happens.
9996 */
9997 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00009998 space_info->bytes_reserved > 0 ||
Josef Bacikd555b6c2016-03-25 13:25:51 -04009999 space_info->bytes_may_use > 0))
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -040010000 dump_space_info(info, space_info, 0, 0);
Chris Mason4184ea72009-03-10 12:39:20 -040010001 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010002 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
10003 struct kobject *kobj;
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010004 kobj = space_info->block_group_kobjs[i];
10005 space_info->block_group_kobjs[i] = NULL;
10006 if (kobj) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010007 kobject_del(kobj);
10008 kobject_put(kobj);
10009 }
10010 }
10011 kobject_del(&space_info->kobj);
10012 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -040010013 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010014 return 0;
10015}
10016
Nikolay Borisovc434d212017-08-21 12:43:50 +030010017static void link_block_group(struct btrfs_block_group_cache *cache)
Yan, Zhengb742bb82010-05-16 10:46:24 -040010018{
Nikolay Borisovc434d212017-08-21 12:43:50 +030010019 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengb742bb82010-05-16 10:46:24 -040010020 int index = get_block_group_index(cache);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -040010021 bool first = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -040010022
10023 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -040010024 if (list_empty(&space_info->block_groups[index]))
10025 first = true;
10026 list_add_tail(&cache->list, &space_info->block_groups[index]);
10027 up_write(&space_info->groups_sem);
10028
10029 if (first) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010030 struct raid_kobject *rkobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010031 int ret;
10032
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010033 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
10034 if (!rkobj)
10035 goto out_err;
10036 rkobj->raid_type = index;
10037 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
10038 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
10039 "%s", get_raid_name(index));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010040 if (ret) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010041 kobject_put(&rkobj->kobj);
10042 goto out_err;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010043 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010044 space_info->block_group_kobjs[index] = &rkobj->kobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010045 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010046
10047 return;
10048out_err:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -040010049 btrfs_warn(cache->fs_info,
10050 "failed to add kobject for block cache, ignoring");
Yan, Zhengb742bb82010-05-16 10:46:24 -040010051}
10052
Miao Xie920e4a52014-01-15 20:00:55 +080010053static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010054btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info,
10055 u64 start, u64 size)
Miao Xie920e4a52014-01-15 20:00:55 +080010056{
10057 struct btrfs_block_group_cache *cache;
10058
10059 cache = kzalloc(sizeof(*cache), GFP_NOFS);
10060 if (!cache)
10061 return NULL;
10062
10063 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
10064 GFP_NOFS);
10065 if (!cache->free_space_ctl) {
10066 kfree(cache);
10067 return NULL;
10068 }
10069
10070 cache->key.objectid = start;
10071 cache->key.offset = size;
10072 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
10073
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010074 cache->fs_info = fs_info;
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +030010075 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010076 set_free_space_tree_thresholds(cache);
10077
Miao Xie920e4a52014-01-15 20:00:55 +080010078 atomic_set(&cache->count, 1);
10079 spin_lock_init(&cache->lock);
Miao Xiee570fd22014-06-19 10:42:50 +080010080 init_rwsem(&cache->data_rwsem);
Miao Xie920e4a52014-01-15 20:00:55 +080010081 INIT_LIST_HEAD(&cache->list);
10082 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010083 INIT_LIST_HEAD(&cache->bg_list);
Josef Bacik633c0aa2014-10-31 09:49:34 -040010084 INIT_LIST_HEAD(&cache->ro_list);
Josef Bacikce93ec52014-11-17 15:45:48 -050010085 INIT_LIST_HEAD(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -070010086 INIT_LIST_HEAD(&cache->io_list);
Miao Xie920e4a52014-01-15 20:00:55 +080010087 btrfs_init_free_space_ctl(cache);
Filipe Manana04216822014-11-27 21:14:15 +000010088 atomic_set(&cache->trimming, 0);
Omar Sandovala5ed9182015-09-29 20:50:35 -070010089 mutex_init(&cache->free_space_lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +080010090 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
Miao Xie920e4a52014-01-15 20:00:55 +080010091
10092 return cache;
10093}
10094
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010095int btrfs_read_block_groups(struct btrfs_fs_info *info)
Chris Mason9078a3e2007-04-26 16:46:15 -040010096{
10097 struct btrfs_path *path;
10098 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010099 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -040010100 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -040010101 struct btrfs_key key;
10102 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -040010103 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -040010104 int need_clear = 0;
10105 u64 cache_gen;
Liu Bo49303382016-08-25 18:08:27 -070010106 u64 feature;
10107 int mixed;
10108
10109 feature = btrfs_super_incompat_flags(info->super_copy);
10110 mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS);
Chris Mason96b51792007-10-15 16:15:19 -040010111
Chris Mason9078a3e2007-04-26 16:46:15 -040010112 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -040010113 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010114 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Chris Mason9078a3e2007-04-26 16:46:15 -040010115 path = btrfs_alloc_path();
10116 if (!path)
10117 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +010010118 path->reada = READA_FORWARD;
Chris Mason9078a3e2007-04-26 16:46:15 -040010119
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010120 cache_gen = btrfs_super_cache_generation(info->super_copy);
10121 if (btrfs_test_opt(info, SPACE_CACHE) &&
10122 btrfs_super_generation(info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -040010123 need_clear = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010124 if (btrfs_test_opt(info, CLEAR_CACHE))
Josef Bacik88c2ba32010-09-21 14:21:34 -040010125 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -040010126
Chris Masond3977122009-01-05 21:25:51 -050010127 while (1) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010128 ret = find_first_block_group(info, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -040010129 if (ret > 0)
10130 break;
Chris Mason0b86a832008-03-24 15:01:56 -040010131 if (ret != 0)
10132 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +080010133
Chris Mason5f39d392007-10-15 16:14:19 -040010134 leaf = path->nodes[0];
10135 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +080010136
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010137 cache = btrfs_create_block_group_cache(info, found_key.objectid,
Miao Xie920e4a52014-01-15 20:00:55 +080010138 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -040010139 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -040010140 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -040010141 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -040010142 }
Josef Bacik96303082009-07-13 21:29:25 -040010143
Liu Bocf7c1ef2012-07-06 03:31:34 -060010144 if (need_clear) {
10145 /*
10146 * When we mount with old space cache, we need to
10147 * set BTRFS_DC_CLEAR and set dirty flag.
10148 *
10149 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
10150 * truncate the old free space cache inode and
10151 * setup a new one.
10152 * b) Setting 'dirty flag' makes sure that we flush
10153 * the new space cache info onto disk.
10154 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010155 if (btrfs_test_opt(info, SPACE_CACHE))
Josef Bacikce93ec52014-11-17 15:45:48 -050010156 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -060010157 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010158
Chris Mason5f39d392007-10-15 16:14:19 -040010159 read_extent_buffer(leaf, &cache->item,
10160 btrfs_item_ptr_offset(leaf, path->slots[0]),
10161 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +080010162 cache->flags = btrfs_block_group_flags(&cache->item);
Liu Bo49303382016-08-25 18:08:27 -070010163 if (!mixed &&
10164 ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
10165 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
10166 btrfs_err(info,
10167"bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
10168 cache->key.objectid);
10169 ret = -EINVAL;
10170 goto error;
10171 }
Chris Mason0b86a832008-03-24 15:01:56 -040010172
Chris Mason9078a3e2007-04-26 16:46:15 -040010173 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +020010174 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +080010175
Josef Bacik817d52f2009-07-13 21:29:25 -040010176 /*
Josef Bacik3c148742011-02-02 15:53:47 +000010177 * We need to exclude the super stripes now so that the space
10178 * info has super bytes accounted for, otherwise we'll think
10179 * we have more space than we actually do.
10180 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010181 ret = exclude_super_stripes(info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010182 if (ret) {
10183 /*
10184 * We may have excluded something, so call this just in
10185 * case.
10186 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010187 free_excluded_extents(info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010188 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010189 goto error;
10190 }
Josef Bacik3c148742011-02-02 15:53:47 +000010191
10192 /*
Josef Bacik817d52f2009-07-13 21:29:25 -040010193 * check for two cases, either we are full, and therefore
10194 * don't need to bother with the caching work since we won't
10195 * find any space, or we are empty, and we can just add all
10196 * the space in and be done with it. This saves us _alot_ of
10197 * time, particularly in the full case.
10198 */
10199 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -040010200 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010201 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010202 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010203 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -040010204 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010205 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010206 add_new_free_space(cache, info,
Josef Bacik817d52f2009-07-13 21:29:25 -040010207 found_key.objectid,
10208 found_key.objectid +
10209 found_key.offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010210 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010211 }
Chris Mason96b51792007-10-15 16:15:19 -040010212
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010213 ret = btrfs_add_block_group_cache(info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010214 if (ret) {
10215 btrfs_remove_free_space_cache(cache);
10216 btrfs_put_block_group(cache);
10217 goto error;
10218 }
10219
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010220 trace_btrfs_add_block_group(info, cache, 0);
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +030010221 update_space_info(info, cache->flags, found_key.offset,
10222 btrfs_block_group_used(&cache->item),
10223 cache->bytes_super, &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010224
Chris Mason6324fbf2008-03-24 15:01:59 -040010225 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -040010226
Nikolay Borisovc434d212017-08-21 12:43:50 +030010227 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010228
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010229 set_avail_alloc_bits(info, cache->flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010230 if (btrfs_chunk_readonly(info, cache->key.objectid)) {
Zhaolei868f4012015-08-05 16:43:27 +080010231 inc_block_group_ro(cache, 1);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010232 } else if (btrfs_block_group_used(&cache->item) == 0) {
10233 spin_lock(&info->unused_bgs_lock);
10234 /* Should always be true but just in case. */
10235 if (list_empty(&cache->bg_list)) {
10236 btrfs_get_block_group(cache);
10237 list_add_tail(&cache->bg_list,
10238 &info->unused_bgs);
10239 }
10240 spin_unlock(&info->unused_bgs_lock);
10241 }
Chris Mason9078a3e2007-04-26 16:46:15 -040010242 }
Yan, Zhengb742bb82010-05-16 10:46:24 -040010243
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010244 list_for_each_entry_rcu(space_info, &info->space_info, list) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010245 if (!(get_alloc_profile(info, space_info->flags) &
Yan, Zhengb742bb82010-05-16 10:46:24 -040010246 (BTRFS_BLOCK_GROUP_RAID10 |
10247 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -050010248 BTRFS_BLOCK_GROUP_RAID5 |
10249 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -040010250 BTRFS_BLOCK_GROUP_DUP)))
10251 continue;
10252 /*
10253 * avoid allocating from un-mirrored block group if there are
10254 * mirrored block groups.
10255 */
chandan1095cc02013-07-16 12:28:56 +053010256 list_for_each_entry(cache,
10257 &space_info->block_groups[BTRFS_RAID_RAID0],
10258 list)
Zhaolei868f4012015-08-05 16:43:27 +080010259 inc_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +053010260 list_for_each_entry(cache,
10261 &space_info->block_groups[BTRFS_RAID_SINGLE],
10262 list)
Zhaolei868f4012015-08-05 16:43:27 +080010263 inc_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -040010264 }
Yan, Zhengf0486c62010-05-16 10:46:25 -040010265
10266 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -040010267 ret = 0;
10268error:
Chris Mason9078a3e2007-04-26 16:46:15 -040010269 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -040010270 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010271}
Chris Mason6324fbf2008-03-24 15:01:59 -040010272
Josef Bacikea658ba2012-09-11 16:57:25 -040010273void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010274 struct btrfs_fs_info *fs_info)
Josef Bacikea658ba2012-09-11 16:57:25 -040010275{
10276 struct btrfs_block_group_cache *block_group, *tmp;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010277 struct btrfs_root *extent_root = fs_info->extent_root;
Josef Bacikea658ba2012-09-11 16:57:25 -040010278 struct btrfs_block_group_item item;
10279 struct btrfs_key key;
10280 int ret = 0;
Filipe Mananad9a05402015-10-03 13:13:13 +010010281 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010282
Filipe Mananad9a05402015-10-03 13:13:13 +010010283 trans->can_flush_pending_bgs = false;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010284 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
Josef Bacikea658ba2012-09-11 16:57:25 -040010285 if (ret)
Filipe Mananac92f6be2014-11-26 15:28:55 +000010286 goto next;
Josef Bacikea658ba2012-09-11 16:57:25 -040010287
10288 spin_lock(&block_group->lock);
10289 memcpy(&item, &block_group->item, sizeof(item));
10290 memcpy(&key, &block_group->key, sizeof(key));
10291 spin_unlock(&block_group->lock);
10292
10293 ret = btrfs_insert_item(trans, extent_root, &key, &item,
10294 sizeof(item));
10295 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010296 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010297 ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid,
10298 key.offset);
Josef Bacik6df9a952013-06-27 13:22:46 -040010299 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010300 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010301 add_block_group_free_space(trans, fs_info, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010302 /* already aborted the transaction if it failed. */
Filipe Mananac92f6be2014-11-26 15:28:55 +000010303next:
10304 list_del_init(&block_group->bg_list);
Josef Bacikea658ba2012-09-11 16:57:25 -040010305 }
Filipe Mananad9a05402015-10-03 13:13:13 +010010306 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010307}
10308
Chris Mason6324fbf2008-03-24 15:01:59 -040010309int btrfs_make_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010310 struct btrfs_fs_info *fs_info, u64 bytes_used,
Nikolay Borisov01744842017-07-27 14:22:11 +030010311 u64 type, u64 chunk_offset, u64 size)
Chris Mason6324fbf2008-03-24 15:01:59 -040010312{
Chris Mason6324fbf2008-03-24 15:01:59 -040010313 struct btrfs_block_group_cache *cache;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010314 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -040010315
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010316 btrfs_set_log_full_commit(fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -040010317
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010318 cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -040010319 if (!cache)
10320 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +080010321
Chris Mason6324fbf2008-03-24 15:01:59 -040010322 btrfs_set_block_group_used(&cache->item, bytes_used);
Nikolay Borisov01744842017-07-27 14:22:11 +030010323 btrfs_set_block_group_chunk_objectid(&cache->item,
10324 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Mason6324fbf2008-03-24 15:01:59 -040010325 btrfs_set_block_group_flags(&cache->item, type);
10326
Miao Xie920e4a52014-01-15 20:00:55 +080010327 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -040010328 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010329 cache->cached = BTRFS_CACHE_FINISHED;
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010330 cache->needs_free_space = 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010331 ret = exclude_super_stripes(fs_info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010332 if (ret) {
10333 /*
10334 * We may have excluded something, so call this just in
10335 * case.
10336 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010337 free_excluded_extents(fs_info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010338 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010339 return ret;
10340 }
Josef Bacik96303082009-07-13 21:29:25 -040010341
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010342 add_new_free_space(cache, fs_info, chunk_offset, chunk_offset + size);
Josef Bacik817d52f2009-07-13 21:29:25 -040010343
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010344 free_excluded_extents(fs_info, cache);
Yan Zheng11833d62009-09-11 16:11:19 -040010345
Josef Bacikd0bd4562015-09-23 14:54:14 -040010346#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010347 if (btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -040010348 u64 new_bytes_used = size - bytes_used;
10349
10350 bytes_used += new_bytes_used >> 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010351 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -040010352 }
10353#endif
Filipe Manana2e6e5182015-05-12 00:28:11 +010010354 /*
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010355 * Ensure the corresponding space_info object is created and
10356 * assigned to our block group. We want our bg to be added to the rbtree
10357 * with its ->space_info set.
Filipe Manana2e6e5182015-05-12 00:28:11 +010010358 */
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010359 cache->space_info = __find_space_info(fs_info, cache->flags);
10360 if (!cache->space_info) {
10361 ret = create_space_info(fs_info, cache->flags,
10362 &cache->space_info);
10363 if (ret) {
10364 btrfs_remove_free_space_cache(cache);
10365 btrfs_put_block_group(cache);
10366 return ret;
10367 }
Filipe Manana2e6e5182015-05-12 00:28:11 +010010368 }
10369
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010370 ret = btrfs_add_block_group_cache(fs_info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010371 if (ret) {
10372 btrfs_remove_free_space_cache(cache);
10373 btrfs_put_block_group(cache);
10374 return ret;
10375 }
10376
Filipe Manana2e6e5182015-05-12 00:28:11 +010010377 /*
10378 * Now that our block group has its ->space_info set and is inserted in
10379 * the rbtree, update the space info's counters.
10380 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010381 trace_btrfs_add_block_group(fs_info, cache, 1);
Nikolay Borisovd2006e6d2017-05-22 09:35:50 +030010382 update_space_info(fs_info, cache->flags, size, bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -040010383 cache->bytes_super, &cache->space_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010384 update_global_block_rsv(fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -040010385
Nikolay Borisovc434d212017-08-21 12:43:50 +030010386 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010387
Josef Bacik47ab2a62014-09-18 11:20:02 -040010388 list_add_tail(&cache->bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -040010389
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010390 set_avail_alloc_bits(fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -040010391 return 0;
10392}
Zheng Yan1a40e232008-09-26 10:09:34 -040010393
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010394static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
10395{
Ilya Dryomov899c81e2012-03-27 17:09:16 +030010396 u64 extra_flags = chunk_to_extended(flags) &
10397 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010398
Miao Xiede98ced2013-01-29 10:13:12 +000010399 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010400 if (flags & BTRFS_BLOCK_GROUP_DATA)
10401 fs_info->avail_data_alloc_bits &= ~extra_flags;
10402 if (flags & BTRFS_BLOCK_GROUP_METADATA)
10403 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
10404 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
10405 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +000010406 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010407}
10408
Zheng Yan1a40e232008-09-26 10:09:34 -040010409int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010410 struct btrfs_fs_info *fs_info, u64 group_start,
Filipe Manana04216822014-11-27 21:14:15 +000010411 struct extent_map *em)
Zheng Yan1a40e232008-09-26 10:09:34 -040010412{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010413 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010414 struct btrfs_path *path;
10415 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -040010416 struct btrfs_free_cluster *cluster;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010417 struct btrfs_root *tree_root = fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010418 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -040010419 struct inode *inode;
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010420 struct kobject *kobj = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -040010421 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010422 int index;
Josef Bacik89a55892010-10-14 14:52:27 -040010423 int factor;
Filipe Manana4f69cb92014-11-26 15:28:51 +000010424 struct btrfs_caching_control *caching_ctl = NULL;
Filipe Manana04216822014-11-27 21:14:15 +000010425 bool remove_em;
Zheng Yan1a40e232008-09-26 10:09:34 -040010426
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010427 block_group = btrfs_lookup_block_group(fs_info, group_start);
Zheng Yan1a40e232008-09-26 10:09:34 -040010428 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -050010429 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -040010430
liubo9f7c43c2011-03-07 02:13:33 +000010431 /*
10432 * Free the reserved super bytes from this block group before
10433 * remove it.
10434 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010435 free_excluded_extents(fs_info, block_group);
Josef Bacikfd708b82017-09-29 15:43:50 -040010436 btrfs_free_ref_tree_range(fs_info, block_group->key.objectid,
10437 block_group->key.offset);
liubo9f7c43c2011-03-07 02:13:33 +000010438
Zheng Yan1a40e232008-09-26 10:09:34 -040010439 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010440 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -040010441 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
10442 BTRFS_BLOCK_GROUP_RAID1 |
10443 BTRFS_BLOCK_GROUP_RAID10))
10444 factor = 2;
10445 else
10446 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -040010447
Chris Mason44fb5512009-06-04 15:34:51 -040010448 /* make sure this block group isn't part of an allocation cluster */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010449 cluster = &fs_info->data_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010450 spin_lock(&cluster->refill_lock);
10451 btrfs_return_cluster_to_free_space(block_group, cluster);
10452 spin_unlock(&cluster->refill_lock);
10453
10454 /*
10455 * make sure this block group isn't part of a metadata
10456 * allocation cluster
10457 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010458 cluster = &fs_info->meta_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010459 spin_lock(&cluster->refill_lock);
10460 btrfs_return_cluster_to_free_space(block_group, cluster);
10461 spin_unlock(&cluster->refill_lock);
10462
Zheng Yan1a40e232008-09-26 10:09:34 -040010463 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010464 if (!path) {
10465 ret = -ENOMEM;
10466 goto out;
10467 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010468
Chris Mason1bbc6212015-04-06 12:46:08 -070010469 /*
10470 * get the inode first so any iput calls done for the io_list
10471 * aren't the final iput (no unlinks allowed now)
10472 */
Jeff Mahoney77ab86b2017-02-15 16:28:30 -050010473 inode = lookup_free_space_inode(fs_info, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010474
10475 mutex_lock(&trans->transaction->cache_write_mutex);
10476 /*
10477 * make sure our free spache cache IO is done before remove the
10478 * free space inode
10479 */
10480 spin_lock(&trans->transaction->dirty_bgs_lock);
10481 if (!list_empty(&block_group->io_list)) {
10482 list_del_init(&block_group->io_list);
10483
10484 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
10485
10486 spin_unlock(&trans->transaction->dirty_bgs_lock);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040010487 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010488 btrfs_put_block_group(block_group);
10489 spin_lock(&trans->transaction->dirty_bgs_lock);
10490 }
10491
10492 if (!list_empty(&block_group->dirty_list)) {
10493 list_del_init(&block_group->dirty_list);
10494 btrfs_put_block_group(block_group);
10495 }
10496 spin_unlock(&trans->transaction->dirty_bgs_lock);
10497 mutex_unlock(&trans->transaction->cache_write_mutex);
10498
Josef Bacik0af3d002010-06-21 14:48:16 -040010499 if (!IS_ERR(inode)) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010500 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010501 if (ret) {
10502 btrfs_add_delayed_iput(inode);
10503 goto out;
10504 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010505 clear_nlink(inode);
10506 /* One for the block groups ref */
10507 spin_lock(&block_group->lock);
10508 if (block_group->iref) {
10509 block_group->iref = 0;
10510 block_group->inode = NULL;
10511 spin_unlock(&block_group->lock);
10512 iput(inode);
10513 } else {
10514 spin_unlock(&block_group->lock);
10515 }
10516 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -040010517 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -040010518 }
10519
10520 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
10521 key.offset = block_group->key.objectid;
10522 key.type = 0;
10523
10524 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
10525 if (ret < 0)
10526 goto out;
10527 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +020010528 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010529 if (ret == 0) {
10530 ret = btrfs_del_item(trans, tree_root, path);
10531 if (ret)
10532 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +020010533 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010534 }
10535
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010536 spin_lock(&fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -040010537 rb_erase(&block_group->cache_node,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010538 &fs_info->block_group_cache_tree);
Filipe Manana292cbd52014-11-26 15:28:50 +000010539 RB_CLEAR_NODE(&block_group->cache_node);
Liu Boa1897fd2012-12-27 09:01:23 +000010540
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010541 if (fs_info->first_logical_byte == block_group->key.objectid)
10542 fs_info->first_logical_byte = (u64)-1;
10543 spin_unlock(&fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010544
Josef Bacik80eb2342008-10-29 14:49:05 -040010545 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -040010546 /*
10547 * we must use list_del_init so people can check to see if they
10548 * are still on the list after taking the semaphore
10549 */
10550 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010551 if (list_empty(&block_group->space_info->block_groups[index])) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010552 kobj = block_group->space_info->block_group_kobjs[index];
10553 block_group->space_info->block_group_kobjs[index] = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010554 clear_avail_alloc_bits(fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010555 }
Josef Bacik80eb2342008-10-29 14:49:05 -040010556 up_write(&block_group->space_info->groups_sem);
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010557 if (kobj) {
10558 kobject_del(kobj);
10559 kobject_put(kobj);
10560 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010561
Filipe Manana4f69cb92014-11-26 15:28:51 +000010562 if (block_group->has_caching_ctl)
10563 caching_ctl = get_caching_control(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -040010564 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -040010565 wait_block_group_cache_done(block_group);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010566 if (block_group->has_caching_ctl) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010567 down_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010568 if (!caching_ctl) {
10569 struct btrfs_caching_control *ctl;
10570
10571 list_for_each_entry(ctl,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010572 &fs_info->caching_block_groups, list)
Filipe Manana4f69cb92014-11-26 15:28:51 +000010573 if (ctl->block_group == block_group) {
10574 caching_ctl = ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +020010575 refcount_inc(&caching_ctl->count);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010576 break;
10577 }
10578 }
10579 if (caching_ctl)
10580 list_del_init(&caching_ctl->list);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010581 up_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010582 if (caching_ctl) {
10583 /* Once for the caching bgs list and once for us. */
10584 put_caching_control(caching_ctl);
10585 put_caching_control(caching_ctl);
10586 }
10587 }
Josef Bacik817d52f2009-07-13 21:29:25 -040010588
Josef Bacikce93ec52014-11-17 15:45:48 -050010589 spin_lock(&trans->transaction->dirty_bgs_lock);
10590 if (!list_empty(&block_group->dirty_list)) {
Chris Mason1bbc6212015-04-06 12:46:08 -070010591 WARN_ON(1);
10592 }
10593 if (!list_empty(&block_group->io_list)) {
10594 WARN_ON(1);
Josef Bacikce93ec52014-11-17 15:45:48 -050010595 }
10596 spin_unlock(&trans->transaction->dirty_bgs_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010597 btrfs_remove_free_space_cache(block_group);
10598
Yan Zhengc146afa2008-11-12 14:34:12 -050010599 spin_lock(&block_group->space_info->lock);
Filipe Manana75c68e92015-01-16 13:24:40 +000010600 list_del_init(&block_group->ro_list);
Zhao Lei18d018a2015-02-24 20:07:44 +080010601
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010602 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Zhao Lei18d018a2015-02-24 20:07:44 +080010603 WARN_ON(block_group->space_info->total_bytes
10604 < block_group->key.offset);
10605 WARN_ON(block_group->space_info->bytes_readonly
10606 < block_group->key.offset);
10607 WARN_ON(block_group->space_info->disk_total
10608 < block_group->key.offset * factor);
10609 }
Yan Zhengc146afa2008-11-12 14:34:12 -050010610 block_group->space_info->total_bytes -= block_group->key.offset;
10611 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -040010612 block_group->space_info->disk_total -= block_group->key.offset * factor;
Zhao Lei18d018a2015-02-24 20:07:44 +080010613
Yan Zhengc146afa2008-11-12 14:34:12 -050010614 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -040010615
Josef Bacik0af3d002010-06-21 14:48:16 -040010616 memcpy(&key, &block_group->key, sizeof(key));
10617
David Sterba34441362016-10-04 19:34:27 +020010618 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana495e64f2014-12-02 18:07:30 +000010619 if (!list_empty(&em->list)) {
10620 /* We're in the transaction->pending_chunks list. */
10621 free_extent_map(em);
10622 }
Filipe Manana04216822014-11-27 21:14:15 +000010623 spin_lock(&block_group->lock);
10624 block_group->removed = 1;
10625 /*
10626 * At this point trimming can't start on this block group, because we
10627 * removed the block group from the tree fs_info->block_group_cache_tree
10628 * so no one can't find it anymore and even if someone already got this
10629 * block group before we removed it from the rbtree, they have already
10630 * incremented block_group->trimming - if they didn't, they won't find
10631 * any free space entries because we already removed them all when we
10632 * called btrfs_remove_free_space_cache().
10633 *
10634 * And we must not remove the extent map from the fs_info->mapping_tree
10635 * to prevent the same logical address range and physical device space
10636 * ranges from being reused for a new block group. This is because our
10637 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
10638 * completely transactionless, so while it is trimming a range the
10639 * currently running transaction might finish and a new one start,
10640 * allowing for new block groups to be created that can reuse the same
10641 * physical device locations unless we take this special care.
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010642 *
10643 * There may also be an implicit trim operation if the file system
10644 * is mounted with -odiscard. The same protections must remain
10645 * in place until the extents have been discarded completely when
10646 * the transaction commit has completed.
Filipe Manana04216822014-11-27 21:14:15 +000010647 */
10648 remove_em = (atomic_read(&block_group->trimming) == 0);
10649 /*
10650 * Make sure a trimmer task always sees the em in the pinned_chunks list
10651 * if it sees block_group->removed == 1 (needs to lock block_group->lock
10652 * before checking block_group->removed).
10653 */
10654 if (!remove_em) {
10655 /*
10656 * Our em might be in trans->transaction->pending_chunks which
10657 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10658 * and so is the fs_info->pinned_chunks list.
10659 *
10660 * So at this point we must be holding the chunk_mutex to avoid
10661 * any races with chunk allocation (more specifically at
10662 * volumes.c:contains_pending_extent()), to ensure it always
10663 * sees the em, either in the pending_chunks list or in the
10664 * pinned_chunks list.
10665 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010666 list_move_tail(&em->list, &fs_info->pinned_chunks);
Filipe Manana04216822014-11-27 21:14:15 +000010667 }
10668 spin_unlock(&block_group->lock);
Filipe Manana04216822014-11-27 21:14:15 +000010669
10670 if (remove_em) {
10671 struct extent_map_tree *em_tree;
10672
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010673 em_tree = &fs_info->mapping_tree.map_tree;
Filipe Manana04216822014-11-27 21:14:15 +000010674 write_lock(&em_tree->lock);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010675 /*
10676 * The em might be in the pending_chunks list, so make sure the
10677 * chunk mutex is locked, since remove_extent_mapping() will
10678 * delete us from that list.
10679 */
Filipe Manana04216822014-11-27 21:14:15 +000010680 remove_extent_mapping(em_tree, em);
10681 write_unlock(&em_tree->lock);
10682 /* once for the tree */
10683 free_extent_map(em);
10684 }
10685
David Sterba34441362016-10-04 19:34:27 +020010686 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010687
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010688 ret = remove_block_group_free_space(trans, fs_info, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010689 if (ret)
10690 goto out;
10691
Chris Masonfa9c0d792009-04-03 09:47:43 -040010692 btrfs_put_block_group(block_group);
10693 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -040010694
10695 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
10696 if (ret > 0)
10697 ret = -EIO;
10698 if (ret < 0)
10699 goto out;
10700
10701 ret = btrfs_del_item(trans, root, path);
10702out:
10703 btrfs_free_path(path);
10704 return ret;
10705}
liuboacce9522011-01-06 19:30:25 +080010706
Filipe Manana8eab77f2015-11-13 23:57:16 +000010707struct btrfs_trans_handle *
Filipe Manana7fd01182015-11-13 23:57:17 +000010708btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info,
10709 const u64 chunk_offset)
Filipe Manana8eab77f2015-11-13 23:57:16 +000010710{
Filipe Manana7fd01182015-11-13 23:57:17 +000010711 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
10712 struct extent_map *em;
10713 struct map_lookup *map;
10714 unsigned int num_items;
10715
10716 read_lock(&em_tree->lock);
10717 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
10718 read_unlock(&em_tree->lock);
10719 ASSERT(em && em->start == chunk_offset);
10720
Filipe Manana8eab77f2015-11-13 23:57:16 +000010721 /*
Filipe Manana7fd01182015-11-13 23:57:17 +000010722 * We need to reserve 3 + N units from the metadata space info in order
10723 * to remove a block group (done at btrfs_remove_chunk() and at
10724 * btrfs_remove_block_group()), which are used for:
10725 *
Filipe Manana8eab77f2015-11-13 23:57:16 +000010726 * 1 unit for adding the free space inode's orphan (located in the tree
10727 * of tree roots).
Filipe Manana7fd01182015-11-13 23:57:17 +000010728 * 1 unit for deleting the block group item (located in the extent
10729 * tree).
10730 * 1 unit for deleting the free space item (located in tree of tree
10731 * roots).
10732 * N units for deleting N device extent items corresponding to each
10733 * stripe (located in the device tree).
10734 *
10735 * In order to remove a block group we also need to reserve units in the
10736 * system space info in order to update the chunk tree (update one or
10737 * more device items and remove one chunk item), but this is done at
10738 * btrfs_remove_chunk() through a call to check_system_chunk().
Filipe Manana8eab77f2015-11-13 23:57:16 +000010739 */
Jeff Mahoney95617d62015-06-03 10:55:48 -040010740 map = em->map_lookup;
Filipe Manana7fd01182015-11-13 23:57:17 +000010741 num_items = 3 + map->num_stripes;
10742 free_extent_map(em);
10743
Filipe Manana8eab77f2015-11-13 23:57:16 +000010744 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
Filipe Manana7fd01182015-11-13 23:57:17 +000010745 num_items, 1);
Filipe Manana8eab77f2015-11-13 23:57:16 +000010746}
10747
Josef Bacik47ab2a62014-09-18 11:20:02 -040010748/*
10749 * Process the unused_bgs list and remove any that don't have any allocated
10750 * space inside of them.
10751 */
10752void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10753{
10754 struct btrfs_block_group_cache *block_group;
10755 struct btrfs_space_info *space_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010756 struct btrfs_trans_handle *trans;
10757 int ret = 0;
10758
Josef Bacikafcdd122016-09-02 15:40:02 -040010759 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
Josef Bacik47ab2a62014-09-18 11:20:02 -040010760 return;
10761
10762 spin_lock(&fs_info->unused_bgs_lock);
10763 while (!list_empty(&fs_info->unused_bgs)) {
10764 u64 start, end;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010765 int trimming;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010766
10767 block_group = list_first_entry(&fs_info->unused_bgs,
10768 struct btrfs_block_group_cache,
10769 bg_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010770 list_del_init(&block_group->bg_list);
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010771
10772 space_info = block_group->space_info;
10773
Josef Bacik47ab2a62014-09-18 11:20:02 -040010774 if (ret || btrfs_mixed_space_info(space_info)) {
10775 btrfs_put_block_group(block_group);
10776 continue;
10777 }
10778 spin_unlock(&fs_info->unused_bgs_lock);
10779
Zhao Leid5f2e332015-10-08 18:46:44 +080010780 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +010010781
Josef Bacik47ab2a62014-09-18 11:20:02 -040010782 /* Don't want to race with allocators so take the groups_sem */
10783 down_write(&space_info->groups_sem);
10784 spin_lock(&block_group->lock);
10785 if (block_group->reserved ||
10786 btrfs_block_group_used(&block_group->item) ||
Chris Mason19c4d2f2016-10-10 13:43:31 -070010787 block_group->ro ||
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010788 list_is_singular(&block_group->list)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -040010789 /*
10790 * We want to bail if we made new allocations or have
10791 * outstanding allocations in this block group. We do
10792 * the ro check in case balance is currently acting on
10793 * this block group.
10794 */
10795 spin_unlock(&block_group->lock);
10796 up_write(&space_info->groups_sem);
10797 goto next;
10798 }
10799 spin_unlock(&block_group->lock);
10800
10801 /* We don't want to force the issue, only flip if it's ok. */
Zhaolei868f4012015-08-05 16:43:27 +080010802 ret = inc_block_group_ro(block_group, 0);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010803 up_write(&space_info->groups_sem);
10804 if (ret < 0) {
10805 ret = 0;
10806 goto next;
10807 }
10808
10809 /*
10810 * Want to do this before we do anything else so we can recover
10811 * properly if we fail to join the transaction.
10812 */
Filipe Manana7fd01182015-11-13 23:57:17 +000010813 trans = btrfs_start_trans_remove_block_group(fs_info,
10814 block_group->key.objectid);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010815 if (IS_ERR(trans)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010816 btrfs_dec_block_group_ro(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010817 ret = PTR_ERR(trans);
10818 goto next;
10819 }
10820
10821 /*
10822 * We could have pending pinned extents for this block group,
10823 * just delete them, we don't care about them anymore.
10824 */
10825 start = block_group->key.objectid;
10826 end = start + block_group->key.offset - 1;
Filipe Mananad4b450c2015-01-29 19:18:25 +000010827 /*
10828 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10829 * btrfs_finish_extent_commit(). If we are at transaction N,
10830 * another task might be running finish_extent_commit() for the
10831 * previous transaction N - 1, and have seen a range belonging
10832 * to the block group in freed_extents[] before we were able to
10833 * clear the whole block group range from freed_extents[]. This
10834 * means that task can lookup for the block group after we
10835 * unpinned it from freed_extents[] and removed it, leading to
10836 * a BUG_ON() at btrfs_unpin_extent_range().
10837 */
10838 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana758eb512014-11-03 14:08:39 +000010839 ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
David Sterba91166212016-04-26 23:54:39 +020010840 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010841 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010842 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010843 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010844 goto end_trans;
10845 }
10846 ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
David Sterba91166212016-04-26 23:54:39 +020010847 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010848 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010849 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010850 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010851 goto end_trans;
10852 }
Filipe Mananad4b450c2015-01-29 19:18:25 +000010853 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010854
10855 /* Reset pinned so btrfs_put_block_group doesn't complain */
Zhao Leic30666d2015-02-25 14:17:20 +080010856 spin_lock(&space_info->lock);
10857 spin_lock(&block_group->lock);
10858
10859 space_info->bytes_pinned -= block_group->pinned;
10860 space_info->bytes_readonly += block_group->pinned;
10861 percpu_counter_add(&space_info->total_bytes_pinned,
10862 -block_group->pinned);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010863 block_group->pinned = 0;
10864
Zhao Leic30666d2015-02-25 14:17:20 +080010865 spin_unlock(&block_group->lock);
10866 spin_unlock(&space_info->lock);
10867
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010868 /* DISCARD can flip during remount */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010869 trimming = btrfs_test_opt(fs_info, DISCARD);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010870
10871 /* Implicit trim during transaction commit. */
10872 if (trimming)
10873 btrfs_get_block_group_trimming(block_group);
10874
Josef Bacik47ab2a62014-09-18 11:20:02 -040010875 /*
10876 * Btrfs_remove_chunk will abort the transaction if things go
10877 * horribly wrong.
10878 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010879 ret = btrfs_remove_chunk(trans, fs_info,
Josef Bacik47ab2a62014-09-18 11:20:02 -040010880 block_group->key.objectid);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010881
10882 if (ret) {
10883 if (trimming)
10884 btrfs_put_block_group_trimming(block_group);
10885 goto end_trans;
10886 }
10887
10888 /*
10889 * If we're not mounted with -odiscard, we can just forget
10890 * about this block group. Otherwise we'll need to wait
10891 * until transaction commit to do the actual discard.
10892 */
10893 if (trimming) {
Filipe Manana348a0012015-11-27 12:16:16 +000010894 spin_lock(&fs_info->unused_bgs_lock);
10895 /*
10896 * A concurrent scrub might have added us to the list
10897 * fs_info->unused_bgs, so use a list_move operation
10898 * to add the block group to the deleted_bgs list.
10899 */
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010900 list_move(&block_group->bg_list,
10901 &trans->transaction->deleted_bgs);
Filipe Manana348a0012015-11-27 12:16:16 +000010902 spin_unlock(&fs_info->unused_bgs_lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010903 btrfs_get_block_group(block_group);
10904 }
Filipe Manana758eb512014-11-03 14:08:39 +000010905end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010906 btrfs_end_transaction(trans);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010907next:
Zhao Leid5f2e332015-10-08 18:46:44 +080010908 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010909 btrfs_put_block_group(block_group);
10910 spin_lock(&fs_info->unused_bgs_lock);
10911 }
10912 spin_unlock(&fs_info->unused_bgs_lock);
10913}
10914
liuboc59021f2011-03-07 02:13:14 +000010915int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
10916{
10917 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +000010918 struct btrfs_super_block *disk_super;
10919 u64 features;
10920 u64 flags;
10921 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +000010922 int ret;
10923
David Sterba6c417612011-04-13 15:41:04 +020010924 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +000010925 if (!btrfs_super_root(disk_super))
Dan Carpenter0dc924c52016-01-13 15:21:17 +030010926 return -EINVAL;
liuboc59021f2011-03-07 02:13:14 +000010927
liubo1aba86d2011-04-08 08:44:37 +000010928 features = btrfs_super_incompat_flags(disk_super);
10929 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
10930 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +000010931
liubo1aba86d2011-04-08 08:44:37 +000010932 flags = BTRFS_BLOCK_GROUP_SYSTEM;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010933 ret = create_space_info(fs_info, flags, &space_info);
liuboc59021f2011-03-07 02:13:14 +000010934 if (ret)
liubo1aba86d2011-04-08 08:44:37 +000010935 goto out;
liuboc59021f2011-03-07 02:13:14 +000010936
liubo1aba86d2011-04-08 08:44:37 +000010937 if (mixed) {
10938 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010939 ret = create_space_info(fs_info, flags, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010940 } else {
10941 flags = BTRFS_BLOCK_GROUP_METADATA;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010942 ret = create_space_info(fs_info, flags, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010943 if (ret)
10944 goto out;
10945
10946 flags = BTRFS_BLOCK_GROUP_DATA;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010947 ret = create_space_info(fs_info, flags, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010948 }
10949out:
liuboc59021f2011-03-07 02:13:14 +000010950 return ret;
10951}
10952
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010953int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
10954 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +080010955{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010956 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +080010957}
10958
Jeff Mahoney499f3772015-06-15 09:41:17 -040010959/*
10960 * It used to be that old block groups would be left around forever.
10961 * Iterating over them would be enough to trim unused space. Since we
10962 * now automatically remove them, we also need to iterate over unallocated
10963 * space.
10964 *
10965 * We don't want a transaction for this since the discard may take a
10966 * substantial amount of time. We don't require that a transaction be
10967 * running, but we do need to take a running transaction into account
10968 * to ensure that we're not discarding chunks that were released in
10969 * the current transaction.
10970 *
10971 * Holding the chunks lock will prevent other threads from allocating
10972 * or releasing chunks, but it won't prevent a running transaction
10973 * from committing and releasing the memory that the pending chunks
10974 * list head uses. For that, we need to take a reference to the
10975 * transaction.
10976 */
10977static int btrfs_trim_free_extents(struct btrfs_device *device,
10978 u64 minlen, u64 *trimmed)
10979{
10980 u64 start = 0, len = 0;
10981 int ret;
10982
10983 *trimmed = 0;
10984
10985 /* Not writeable = nothing to do. */
10986 if (!device->writeable)
10987 return 0;
10988
10989 /* No free space = nothing to do. */
10990 if (device->total_bytes <= device->bytes_used)
10991 return 0;
10992
10993 ret = 0;
10994
10995 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -040010996 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010997 struct btrfs_transaction *trans;
10998 u64 bytes;
10999
11000 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
11001 if (ret)
11002 return ret;
11003
11004 down_read(&fs_info->commit_root_sem);
11005
11006 spin_lock(&fs_info->trans_lock);
11007 trans = fs_info->running_transaction;
11008 if (trans)
Elena Reshetova9b64f572017-03-03 10:55:11 +020011009 refcount_inc(&trans->use_count);
Jeff Mahoney499f3772015-06-15 09:41:17 -040011010 spin_unlock(&fs_info->trans_lock);
11011
11012 ret = find_free_dev_extent_start(trans, device, minlen, start,
11013 &start, &len);
11014 if (trans)
11015 btrfs_put_transaction(trans);
11016
11017 if (ret) {
11018 up_read(&fs_info->commit_root_sem);
11019 mutex_unlock(&fs_info->chunk_mutex);
11020 if (ret == -ENOSPC)
11021 ret = 0;
11022 break;
11023 }
11024
11025 ret = btrfs_issue_discard(device->bdev, start, len, &bytes);
11026 up_read(&fs_info->commit_root_sem);
11027 mutex_unlock(&fs_info->chunk_mutex);
11028
11029 if (ret)
11030 break;
11031
11032 start += len;
11033 *trimmed += bytes;
11034
11035 if (fatal_signal_pending(current)) {
11036 ret = -ERESTARTSYS;
11037 break;
11038 }
11039
11040 cond_resched();
11041 }
11042
11043 return ret;
11044}
11045
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040011046int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +000011047{
Li Dongyangf7039b12011-03-24 10:24:28 +000011048 struct btrfs_block_group_cache *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -040011049 struct btrfs_device *device;
11050 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +000011051 u64 group_trimmed;
11052 u64 start;
11053 u64 end;
11054 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +080011055 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +000011056 int ret = 0;
11057
Liu Bo2cac13e2012-02-09 18:17:41 +080011058 /*
11059 * try to trim all FS space, our block group may start from non-zero.
11060 */
11061 if (range->len == total_bytes)
11062 cache = btrfs_lookup_first_block_group(fs_info, range->start);
11063 else
11064 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +000011065
11066 while (cache) {
11067 if (cache->key.objectid >= (range->start + range->len)) {
11068 btrfs_put_block_group(cache);
11069 break;
11070 }
11071
11072 start = max(range->start, cache->key.objectid);
11073 end = min(range->start + range->len,
11074 cache->key.objectid + cache->key.offset);
11075
11076 if (end - start >= range->minlen) {
11077 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +000011078 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -040011079 if (ret) {
11080 btrfs_put_block_group(cache);
11081 break;
11082 }
11083 ret = wait_block_group_cache_done(cache);
11084 if (ret) {
11085 btrfs_put_block_group(cache);
11086 break;
11087 }
Li Dongyangf7039b12011-03-24 10:24:28 +000011088 }
11089 ret = btrfs_trim_block_group(cache,
11090 &group_trimmed,
11091 start,
11092 end,
11093 range->minlen);
11094
11095 trimmed += group_trimmed;
11096 if (ret) {
11097 btrfs_put_block_group(cache);
11098 break;
11099 }
11100 }
11101
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040011102 cache = next_block_group(fs_info, cache);
Li Dongyangf7039b12011-03-24 10:24:28 +000011103 }
11104
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011105 mutex_lock(&fs_info->fs_devices->device_list_mutex);
11106 devices = &fs_info->fs_devices->alloc_list;
Jeff Mahoney499f3772015-06-15 09:41:17 -040011107 list_for_each_entry(device, devices, dev_alloc_list) {
11108 ret = btrfs_trim_free_extents(device, range->minlen,
11109 &group_trimmed);
11110 if (ret)
11111 break;
11112
11113 trimmed += group_trimmed;
11114 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011115 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -040011116
Li Dongyangf7039b12011-03-24 10:24:28 +000011117 range->len = trimmed;
11118 return ret;
11119}
Miao Xie8257b2d2014-03-06 13:38:19 +080011120
11121/*
David Sterbaea14b57f2017-06-22 02:19:11 +020011122 * btrfs_{start,end}_write_no_snapshotting() are similar to
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011123 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
11124 * data into the page cache through nocow before the subvolume is snapshoted,
11125 * but flush the data into disk after the snapshot creation, or to prevent
David Sterbaea14b57f2017-06-22 02:19:11 +020011126 * operations while snapshotting is ongoing and that cause the snapshot to be
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011127 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +080011128 */
David Sterbaea14b57f2017-06-22 02:19:11 +020011129void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011130{
11131 percpu_counter_dec(&root->subv_writers->counter);
11132 /*
David Sterbaa83342a2015-02-16 19:36:47 +010011133 * Make sure counter is updated before we wake up waiters.
Miao Xie8257b2d2014-03-06 13:38:19 +080011134 */
11135 smp_mb();
11136 if (waitqueue_active(&root->subv_writers->wait))
11137 wake_up(&root->subv_writers->wait);
11138}
11139
David Sterbaea14b57f2017-06-22 02:19:11 +020011140int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011141{
David Sterbaea14b57f2017-06-22 02:19:11 +020011142 if (atomic_read(&root->will_be_snapshotted))
Miao Xie8257b2d2014-03-06 13:38:19 +080011143 return 0;
11144
11145 percpu_counter_inc(&root->subv_writers->counter);
11146 /*
11147 * Make sure counter is updated before we check for snapshot creation.
11148 */
11149 smp_mb();
David Sterbaea14b57f2017-06-22 02:19:11 +020011150 if (atomic_read(&root->will_be_snapshotted)) {
11151 btrfs_end_write_no_snapshotting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +080011152 return 0;
11153 }
11154 return 1;
11155}
Zhao Lei0bc19f902016-01-06 18:56:36 +080011156
David Sterbaea14b57f2017-06-22 02:19:11 +020011157static int wait_snapshotting_atomic_t(atomic_t *a)
Zhao Lei0bc19f902016-01-06 18:56:36 +080011158{
11159 schedule();
11160 return 0;
11161}
11162
11163void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
11164{
11165 while (true) {
11166 int ret;
11167
David Sterbaea14b57f2017-06-22 02:19:11 +020011168 ret = btrfs_start_write_no_snapshotting(root);
Zhao Lei0bc19f902016-01-06 18:56:36 +080011169 if (ret)
11170 break;
David Sterbaea14b57f2017-06-22 02:19:11 +020011171 wait_on_atomic_t(&root->will_be_snapshotted,
11172 wait_snapshotting_atomic_t,
Zhao Lei0bc19f902016-01-06 18:56:36 +080011173 TASK_UNINTERRUPTIBLE);
11174 }
11175}