blob: 3e4a6a4b0505955a0ccfaba67efecb8df6bd39b8 [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, Zhengb742bb822010-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)) {
Elena Reshetova6df8cdf2017-03-03 10:55:15 +0200915 refcount_inc(&head->node.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);
926 btrfs_put_delayed_ref(&head->node);
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
935 num_refs += head->node.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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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 Mahoney87bde3c2017-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,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002340 struct btrfs_delayed_ref_node *node,
2341 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
2362 key.objectid = node->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;
2369 key.offset = node->num_bytes;
2370 }
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]);
2386 if (key.objectid == node->bytenr &&
2387 key.type == BTRFS_EXTENT_ITEM_KEY &&
2388 key.offset == node->num_bytes)
2389 ret = 0;
2390 }
2391 if (ret > 0) {
2392 btrfs_release_path(path);
2393 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002394
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002395 key.objectid = node->bytenr;
2396 key.offset = node->num_bytes;
2397 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 Mahoney87bde3c2017-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 (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002504 struct btrfs_delayed_ref_head *head;
2505 /*
2506 * we've hit the end of the chain and we were supposed
2507 * to insert this extent into the tree. But, it got
2508 * deleted before we ever needed to insert it, so all
2509 * we have to do is clean up the accounting
2510 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002511 BUG_ON(extent_op);
2512 head = btrfs_delayed_node_to_head(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002513 trace_run_delayed_ref_head(fs_info, node, head, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002514
Omar Sandovald7eae342017-06-06 16:45:31 -07002515 if (head->total_ref_mod < 0) {
2516 struct btrfs_block_group_cache *cache;
2517
2518 cache = btrfs_lookup_block_group(fs_info, node->bytenr);
2519 ASSERT(cache);
2520 percpu_counter_add(&cache->space_info->total_bytes_pinned,
2521 -node->num_bytes);
2522 btrfs_put_block_group(cache);
2523 }
2524
Chris Mason56bec292009-03-13 10:10:06 -04002525 if (insert_reserved) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002526 btrfs_pin_extent(fs_info, node->bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002527 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002528 if (head->is_data) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002529 ret = btrfs_del_csums(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002530 node->bytenr,
2531 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002532 }
Chris Mason56bec292009-03-13 10:10:06 -04002533 }
Qu Wenruo297d7502015-09-08 17:08:37 +08002534
2535 /* Also free its reserved qgroup space */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002536 btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
Qu Wenruo297d7502015-09-08 17:08:37 +08002537 head->qgroup_reserved);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002538 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002539 }
Josef Bacikeb099672009-02-12 09:27:38 -05002540
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002541 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2542 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002543 ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002544 insert_reserved);
2545 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2546 node->type == BTRFS_SHARED_DATA_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002547 ret = run_delayed_data_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002548 insert_reserved);
2549 else
2550 BUG();
2551 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002552}
2553
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002554static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04002555select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002556{
Filipe Mananacffc3372015-07-09 13:13:44 +01002557 struct btrfs_delayed_ref_node *ref;
2558
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002559 if (list_empty(&head->ref_list))
2560 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002561
Filipe Mananacffc3372015-07-09 13:13:44 +01002562 /*
2563 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2564 * This is to prevent a ref count from going down to zero, which deletes
2565 * the extent item from the extent tree, when there still are references
2566 * to add, which would fail because they would not find the extent item.
2567 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002568 if (!list_empty(&head->ref_add_list))
2569 return list_first_entry(&head->ref_add_list,
2570 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01002571
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002572 ref = list_first_entry(&head->ref_list, struct btrfs_delayed_ref_node,
2573 list);
2574 ASSERT(list_empty(&ref->add_list));
2575 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04002576}
2577
Josef Bacik2eadaa22017-09-29 15:43:52 -04002578static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
2579 struct btrfs_delayed_ref_head *head)
2580{
2581 spin_lock(&delayed_refs->lock);
2582 head->processing = 0;
2583 delayed_refs->num_heads_ready++;
2584 spin_unlock(&delayed_refs->lock);
2585 btrfs_delayed_ref_unlock(head);
2586}
2587
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002588/*
2589 * Returns 0 on success or if called with an already aborted transaction.
2590 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2591 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002592static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002593 struct btrfs_fs_info *fs_info,
Josef Bacikd7df2c72014-01-23 09:21:38 -05002594 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002595{
Chris Mason56bec292009-03-13 10:10:06 -04002596 struct btrfs_delayed_ref_root *delayed_refs;
2597 struct btrfs_delayed_ref_node *ref;
2598 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002599 struct btrfs_delayed_extent_op *extent_op;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002600 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002601 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002602 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002603 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002604 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002605
2606 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002607 while (1) {
2608 if (!locked_ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002609 if (count >= nr)
Chris Mason56bec292009-03-13 10:10:06 -04002610 break;
Chris Mason56bec292009-03-13 10:10:06 -04002611
Josef Bacikd7df2c72014-01-23 09:21:38 -05002612 spin_lock(&delayed_refs->lock);
2613 locked_ref = btrfs_select_ref_head(trans);
2614 if (!locked_ref) {
2615 spin_unlock(&delayed_refs->lock);
2616 break;
2617 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002618
2619 /* grab the lock that says we are going to process
2620 * all the refs for this head */
2621 ret = btrfs_delayed_ref_lock(trans, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002622 spin_unlock(&delayed_refs->lock);
Chris Masonc3e69d52009-03-13 10:17:05 -04002623 /*
2624 * we may have dropped the spin lock to get the head
2625 * mutex lock, and that might have given someone else
2626 * time to free the head. If that's true, it has been
2627 * removed from our list and we can move on.
2628 */
2629 if (ret == -EAGAIN) {
2630 locked_ref = NULL;
2631 count++;
2632 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002633 }
2634 }
2635
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002636 /*
2637 * We need to try and merge add/drops of the same ref since we
2638 * can run into issues with relocate dropping the implicit ref
2639 * and then it being added back again before the drop can
2640 * finish. If we merged anything we need to re-loop so we can
2641 * get a good ref.
2642 * Or we can get node references of the same type that weren't
2643 * merged when created due to bumps in the tree mod seq, and
2644 * we need to merge them to prevent adding an inline extent
2645 * backref before dropping it (triggering a BUG_ON at
2646 * insert_inline_extent_backref()).
2647 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002648 spin_lock(&locked_ref->lock);
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002649 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2650 locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002651
2652 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002653 * locked_ref is the head node, so we have to go one
2654 * node back for any delayed ref updates
2655 */
2656 ref = select_delayed_ref(locked_ref);
2657
2658 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002659 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002660 spin_unlock(&locked_ref->lock);
Josef Bacik2eadaa22017-09-29 15:43:52 -04002661 unselect_delayed_ref_head(delayed_refs, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002662 locked_ref = NULL;
Arne Jansend1270cd2011-09-13 15:16:43 +02002663 cond_resched();
Josef Bacik27a377d2014-02-07 13:57:59 -05002664 count++;
Arne Jansend1270cd2011-09-13 15:16:43 +02002665 continue;
2666 }
2667
2668 /*
Chris Mason56bec292009-03-13 10:10:06 -04002669 * record the must insert reserved flag before we
2670 * drop the spin lock.
2671 */
2672 must_insert_reserved = locked_ref->must_insert_reserved;
2673 locked_ref->must_insert_reserved = 0;
2674
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002675 extent_op = locked_ref->extent_op;
2676 locked_ref->extent_op = NULL;
2677
Chris Mason56bec292009-03-13 10:10:06 -04002678 if (!ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002679
2680
Chris Mason56bec292009-03-13 10:10:06 -04002681 /* All delayed refs have been processed, Go ahead
2682 * and send the head node to run_one_delayed_ref,
2683 * so that any accounting fixes can happen
2684 */
2685 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002686
2687 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002688 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002689 extent_op = NULL;
2690 }
2691
2692 if (extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002693 spin_unlock(&locked_ref->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002694 ret = run_delayed_extent_op(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002695 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002696 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002697
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002698 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04002699 /*
2700 * Need to reset must_insert_reserved if
2701 * there was an error so the abort stuff
2702 * can cleanup the reserved space
2703 * properly.
2704 */
2705 if (must_insert_reserved)
2706 locked_ref->must_insert_reserved = 1;
Josef Bacik2eadaa22017-09-29 15:43:52 -04002707 unselect_delayed_ref_head(delayed_refs,
2708 locked_ref);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002709 btrfs_debug(fs_info,
2710 "run_delayed_extent_op returned %d",
2711 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002712 return ret;
2713 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002714 continue;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002715 }
Chris Mason56bec292009-03-13 10:10:06 -04002716
Josef Bacikd7df2c72014-01-23 09:21:38 -05002717 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002718 * Need to drop our head ref lock and re-acquire the
Josef Bacikd7df2c72014-01-23 09:21:38 -05002719 * delayed ref lock and then re-check to make sure
2720 * nobody got added.
2721 */
2722 spin_unlock(&locked_ref->lock);
2723 spin_lock(&delayed_refs->lock);
2724 spin_lock(&locked_ref->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002725 if (!list_empty(&locked_ref->ref_list) ||
Josef Bacik573a0752014-03-27 19:41:34 -04002726 locked_ref->extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002727 spin_unlock(&locked_ref->lock);
2728 spin_unlock(&delayed_refs->lock);
2729 continue;
2730 }
2731 ref->in_tree = 0;
2732 delayed_refs->num_heads--;
Liu Boc46effa2013-10-14 12:59:45 +08002733 rb_erase(&locked_ref->href_node,
2734 &delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002735 spin_unlock(&delayed_refs->lock);
2736 } else {
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002737 actual_count++;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002738 ref->in_tree = 0;
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002739 list_del(&ref->list);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002740 if (!list_empty(&ref->add_list))
2741 list_del(&ref->add_list);
Liu Boc46effa2013-10-14 12:59:45 +08002742 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002743 atomic_dec(&delayed_refs->num_entries);
2744
Miao Xie093486c2012-12-19 08:10:10 +00002745 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002746 /*
2747 * when we play the delayed ref, also correct the
2748 * ref_mod on head
2749 */
2750 switch (ref->action) {
2751 case BTRFS_ADD_DELAYED_REF:
2752 case BTRFS_ADD_DELAYED_EXTENT:
2753 locked_ref->node.ref_mod -= ref->ref_mod;
2754 break;
2755 case BTRFS_DROP_DELAYED_REF:
2756 locked_ref->node.ref_mod += ref->ref_mod;
2757 break;
2758 default:
2759 WARN_ON(1);
2760 }
2761 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002762 spin_unlock(&locked_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002763
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002764 ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002765 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002766
Miao Xie78a61842012-11-21 02:21:28 +00002767 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002768 if (ret) {
Josef Bacik2eadaa22017-09-29 15:43:52 -04002769 unselect_delayed_ref_head(delayed_refs, locked_ref);
Miao Xie093486c2012-12-19 08:10:10 +00002770 btrfs_put_delayed_ref(ref);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002771 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
2772 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002773 return ret;
2774 }
2775
Miao Xie093486c2012-12-19 08:10:10 +00002776 /*
2777 * If this node is a head, that means all the refs in this head
2778 * have been dealt with, and we will pick the next head to deal
2779 * with, so we must unlock the head and drop it from the cluster
2780 * list before we release it.
2781 */
2782 if (btrfs_delayed_ref_is_head(ref)) {
Josef Bacik12621332015-02-03 07:50:16 -08002783 if (locked_ref->is_data &&
2784 locked_ref->total_ref_mod < 0) {
2785 spin_lock(&delayed_refs->lock);
2786 delayed_refs->pending_csums -= ref->num_bytes;
2787 spin_unlock(&delayed_refs->lock);
2788 }
Miao Xie093486c2012-12-19 08:10:10 +00002789 btrfs_delayed_ref_unlock(locked_ref);
2790 locked_ref = NULL;
2791 }
2792 btrfs_put_delayed_ref(ref);
2793 count++;
Chris Mason1887be62009-03-13 10:11:24 -04002794 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002795 }
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002796
2797 /*
2798 * We don't want to include ref heads since we can have empty ref heads
2799 * and those will drastically skew our runtime down since we just do
2800 * accounting, no actual extent tree updates.
2801 */
2802 if (actual_count > 0) {
2803 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2804 u64 avg;
2805
2806 /*
2807 * We weigh the current average higher than our current runtime
2808 * to avoid large swings in the average.
2809 */
2810 spin_lock(&delayed_refs->lock);
2811 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002812 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002813 spin_unlock(&delayed_refs->lock);
2814 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002815 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002816}
2817
Arne Jansen709c0482011-09-12 12:22:57 +02002818#ifdef SCRAMBLE_DELAYED_REFS
2819/*
2820 * Normally delayed refs get processed in ascending bytenr order. This
2821 * correlates in most cases to the order added. To expose dependencies on this
2822 * order, we start to process the tree in the middle instead of the beginning
2823 */
2824static u64 find_middle(struct rb_root *root)
2825{
2826 struct rb_node *n = root->rb_node;
2827 struct btrfs_delayed_ref_node *entry;
2828 int alt = 1;
2829 u64 middle;
2830 u64 first = 0, last = 0;
2831
2832 n = rb_first(root);
2833 if (n) {
2834 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2835 first = entry->bytenr;
2836 }
2837 n = rb_last(root);
2838 if (n) {
2839 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2840 last = entry->bytenr;
2841 }
2842 n = root->rb_node;
2843
2844 while (n) {
2845 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2846 WARN_ON(!entry->in_tree);
2847
2848 middle = entry->bytenr;
2849
2850 if (alt)
2851 n = n->rb_left;
2852 else
2853 n = n->rb_right;
2854
2855 alt = 1 - alt;
2856 }
2857 return middle;
2858}
2859#endif
2860
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002861static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002862{
2863 u64 num_bytes;
2864
2865 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2866 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002867 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002868 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2869
2870 /*
2871 * We don't ever fill up leaves all the way so multiply by 2 just to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04002872 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002873 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002874 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002875}
2876
Josef Bacik12621332015-02-03 07:50:16 -08002877/*
2878 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2879 * would require to store the csums for that many bytes.
2880 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002881u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002882{
2883 u64 csum_size;
2884 u64 num_csums_per_leaf;
2885 u64 num_csums;
2886
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002887 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002888 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002889 (u64)btrfs_super_csum_size(fs_info->super_copy));
2890 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002891 num_csums += num_csums_per_leaf - 1;
2892 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2893 return num_csums;
2894}
2895
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002896int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002897 struct btrfs_fs_info *fs_info)
Josef Bacik1be41b72013-06-12 13:56:06 -04002898{
2899 struct btrfs_block_rsv *global_rsv;
2900 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
Josef Bacik12621332015-02-03 07:50:16 -08002901 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
Josef Bacikcb723e42015-02-18 08:06:57 -08002902 u64 num_dirty_bgs = trans->transaction->num_dirty_bgs;
2903 u64 num_bytes, num_dirty_bgs_bytes;
Josef Bacik1be41b72013-06-12 13:56:06 -04002904 int ret = 0;
2905
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002906 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002907 num_heads = heads_to_leaves(fs_info, num_heads);
Josef Bacik1be41b72013-06-12 13:56:06 -04002908 if (num_heads > 1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002909 num_bytes += (num_heads - 1) * fs_info->nodesize;
Josef Bacik1be41b72013-06-12 13:56:06 -04002910 num_bytes <<= 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002911 num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
2912 fs_info->nodesize;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002913 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
Josef Bacikcb723e42015-02-18 08:06:57 -08002914 num_dirty_bgs);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002915 global_rsv = &fs_info->global_block_rsv;
Josef Bacik1be41b72013-06-12 13:56:06 -04002916
2917 /*
2918 * If we can't allocate any more chunks lets make sure we have _lots_ of
2919 * wiggle room since running delayed refs can create more delayed refs.
2920 */
Josef Bacikcb723e42015-02-18 08:06:57 -08002921 if (global_rsv->space_info->full) {
2922 num_dirty_bgs_bytes <<= 1;
Josef Bacik1be41b72013-06-12 13:56:06 -04002923 num_bytes <<= 1;
Josef Bacikcb723e42015-02-18 08:06:57 -08002924 }
Josef Bacik1be41b72013-06-12 13:56:06 -04002925
2926 spin_lock(&global_rsv->lock);
Josef Bacikcb723e42015-02-18 08:06:57 -08002927 if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
Josef Bacik1be41b72013-06-12 13:56:06 -04002928 ret = 1;
2929 spin_unlock(&global_rsv->lock);
2930 return ret;
2931}
2932
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002933int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002934 struct btrfs_fs_info *fs_info)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002935{
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002936 u64 num_entries =
2937 atomic_read(&trans->transaction->delayed_refs.num_entries);
2938 u64 avg_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002939 u64 val;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002940
2941 smp_mb();
2942 avg_runtime = fs_info->avg_delayed_ref_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002943 val = num_entries * avg_runtime;
Wang Xiaoguangdc1a90c2016-10-26 15:23:01 +08002944 if (val >= NSEC_PER_SEC)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002945 return 1;
Chris Masona79b7d42014-05-22 16:18:52 -07002946 if (val >= NSEC_PER_SEC / 2)
2947 return 2;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002948
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002949 return btrfs_check_space_for_delayed_refs(trans, fs_info);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002950}
2951
Chris Masona79b7d42014-05-22 16:18:52 -07002952struct async_delayed_refs {
2953 struct btrfs_root *root;
Josef Bacik31b96552016-04-11 17:37:40 -04002954 u64 transid;
Chris Masona79b7d42014-05-22 16:18:52 -07002955 int count;
2956 int error;
2957 int sync;
2958 struct completion wait;
2959 struct btrfs_work work;
2960};
2961
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002962static inline struct async_delayed_refs *
2963to_async_delayed_refs(struct btrfs_work *work)
2964{
2965 return container_of(work, struct async_delayed_refs, work);
2966}
2967
Chris Masona79b7d42014-05-22 16:18:52 -07002968static void delayed_ref_async_start(struct btrfs_work *work)
2969{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002970 struct async_delayed_refs *async = to_async_delayed_refs(work);
Chris Masona79b7d42014-05-22 16:18:52 -07002971 struct btrfs_trans_handle *trans;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002972 struct btrfs_fs_info *fs_info = async->root->fs_info;
Chris Masona79b7d42014-05-22 16:18:52 -07002973 int ret;
2974
Chris Mason0f873ec2016-04-27 09:59:38 -04002975 /* if the commit is already started, we don't need to wait here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002976 if (btrfs_transaction_blocked(fs_info))
Josef Bacik31b96552016-04-11 17:37:40 -04002977 goto done;
Josef Bacik31b96552016-04-11 17:37:40 -04002978
Chris Mason0f873ec2016-04-27 09:59:38 -04002979 trans = btrfs_join_transaction(async->root);
2980 if (IS_ERR(trans)) {
2981 async->error = PTR_ERR(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07002982 goto done;
2983 }
2984
2985 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002986 * trans->sync means that when we call end_transaction, we won't
Chris Masona79b7d42014-05-22 16:18:52 -07002987 * wait on delayed refs
2988 */
2989 trans->sync = true;
Chris Mason0f873ec2016-04-27 09:59:38 -04002990
2991 /* Don't bother flushing if we got into a different transaction */
2992 if (trans->transid > async->transid)
2993 goto end;
2994
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002995 ret = btrfs_run_delayed_refs(trans, fs_info, async->count);
Chris Masona79b7d42014-05-22 16:18:52 -07002996 if (ret)
2997 async->error = ret;
Chris Mason0f873ec2016-04-27 09:59:38 -04002998end:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002999 ret = btrfs_end_transaction(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07003000 if (ret && !async->error)
3001 async->error = ret;
3002done:
3003 if (async->sync)
3004 complete(&async->wait);
3005 else
3006 kfree(async);
3007}
3008
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003009int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
Josef Bacik31b96552016-04-11 17:37:40 -04003010 unsigned long count, u64 transid, int wait)
Chris Masona79b7d42014-05-22 16:18:52 -07003011{
3012 struct async_delayed_refs *async;
3013 int ret;
3014
3015 async = kmalloc(sizeof(*async), GFP_NOFS);
3016 if (!async)
3017 return -ENOMEM;
3018
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003019 async->root = fs_info->tree_root;
Chris Masona79b7d42014-05-22 16:18:52 -07003020 async->count = count;
3021 async->error = 0;
Josef Bacik31b96552016-04-11 17:37:40 -04003022 async->transid = transid;
Chris Masona79b7d42014-05-22 16:18:52 -07003023 if (wait)
3024 async->sync = 1;
3025 else
3026 async->sync = 0;
3027 init_completion(&async->wait);
3028
Liu Bo9e0af232014-08-15 23:36:53 +08003029 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
3030 delayed_ref_async_start, NULL, NULL);
Chris Masona79b7d42014-05-22 16:18:52 -07003031
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003032 btrfs_queue_work(fs_info->extent_workers, &async->work);
Chris Masona79b7d42014-05-22 16:18:52 -07003033
3034 if (wait) {
3035 wait_for_completion(&async->wait);
3036 ret = async->error;
3037 kfree(async);
3038 return ret;
3039 }
3040 return 0;
3041}
3042
Chris Masonc3e69d52009-03-13 10:17:05 -04003043/*
3044 * this starts processing the delayed reference count updates and
3045 * extent insertions we have queued up so far. count can be
3046 * 0, which means to process everything in the tree at the start
3047 * of the run (but not newly added entries), or it can be some target
3048 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 *
3050 * Returns 0 on success or if called with an aborted transaction
3051 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04003052 */
3053int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003054 struct btrfs_fs_info *fs_info, unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04003055{
3056 struct rb_node *node;
3057 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08003058 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04003059 int ret;
3060 int run_all = count == (unsigned long)-1;
Filipe Mananad9a05402015-10-03 13:13:13 +01003061 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Chris Masonc3e69d52009-03-13 10:17:05 -04003062
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003063 /* We'll clean this up in btrfs_cleanup_transaction */
3064 if (trans->aborted)
3065 return 0;
3066
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003067 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08003068 return 0;
3069
Chris Masonc3e69d52009-03-13 10:17:05 -04003070 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08003071 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05003072 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05003073
Chris Masonc3e69d52009-03-13 10:17:05 -04003074again:
Arne Jansen709c0482011-09-12 12:22:57 +02003075#ifdef SCRAMBLE_DELAYED_REFS
3076 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
3077#endif
Filipe Mananad9a05402015-10-03 13:13:13 +01003078 trans->can_flush_pending_bgs = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003079 ret = __btrfs_run_delayed_refs(trans, fs_info, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003080 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003081 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003082 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04003083 }
3084
Chris Mason56bec292009-03-13 10:10:06 -04003085 if (run_all) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05003086 if (!list_empty(&trans->new_bgs))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003087 btrfs_create_pending_block_groups(trans, fs_info);
Josef Bacikea658ba2012-09-11 16:57:25 -04003088
Josef Bacikd7df2c72014-01-23 09:21:38 -05003089 spin_lock(&delayed_refs->lock);
Liu Boc46effa2013-10-14 12:59:45 +08003090 node = rb_first(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003091 if (!node) {
3092 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04003093 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05003094 }
Chris Mason56bec292009-03-13 10:10:06 -04003095
3096 while (node) {
Liu Boc46effa2013-10-14 12:59:45 +08003097 head = rb_entry(node, struct btrfs_delayed_ref_head,
3098 href_node);
3099 if (btrfs_delayed_ref_is_head(&head->node)) {
3100 struct btrfs_delayed_ref_node *ref;
Chris Mason56bec292009-03-13 10:10:06 -04003101
Liu Boc46effa2013-10-14 12:59:45 +08003102 ref = &head->node;
Elena Reshetova6df8cdf2017-03-03 10:55:15 +02003103 refcount_inc(&ref->refs);
Chris Mason56bec292009-03-13 10:10:06 -04003104
3105 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02003106 /*
3107 * Mutex was contended, block until it's
3108 * released and try again
3109 */
Chris Mason56bec292009-03-13 10:10:06 -04003110 mutex_lock(&head->mutex);
3111 mutex_unlock(&head->mutex);
3112
3113 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04003114 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04003115 goto again;
Liu Boc46effa2013-10-14 12:59:45 +08003116 } else {
3117 WARN_ON(1);
Chris Mason56bec292009-03-13 10:10:06 -04003118 }
3119 node = rb_next(node);
3120 }
3121 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003122 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04003123 goto again;
3124 }
Chris Mason54aa1f42007-06-22 14:16:25 -04003125out:
Filipe Mananad9a05402015-10-03 13:13:13 +01003126 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Chris Masona28ec192007-03-06 20:08:01 -05003127 return 0;
3128}
3129
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003130int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003131 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003132 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04003133 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003134{
3135 struct btrfs_delayed_extent_op *extent_op;
3136 int ret;
3137
Miao Xie78a61842012-11-21 02:21:28 +00003138 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003139 if (!extent_op)
3140 return -ENOMEM;
3141
3142 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01003143 extent_op->update_flags = true;
3144 extent_op->update_key = false;
3145 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04003146 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003147
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003148 ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003149 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003150 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00003151 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003152 return ret;
3153}
3154
Liu Boe4c3b2d2017-01-30 12:25:28 -08003155static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003156 struct btrfs_path *path,
3157 u64 objectid, u64 offset, u64 bytenr)
3158{
3159 struct btrfs_delayed_ref_head *head;
3160 struct btrfs_delayed_ref_node *ref;
3161 struct btrfs_delayed_data_ref *data_ref;
3162 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08003163 struct btrfs_transaction *cur_trans;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003164 int ret = 0;
3165
Liu Boe4c3b2d2017-01-30 12:25:28 -08003166 cur_trans = root->fs_info->running_transaction;
3167 if (!cur_trans)
3168 return 0;
3169
3170 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003171 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003172 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003173 if (!head) {
3174 spin_unlock(&delayed_refs->lock);
3175 return 0;
3176 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003177
3178 if (!mutex_trylock(&head->mutex)) {
Elena Reshetova6df8cdf2017-03-03 10:55:15 +02003179 refcount_inc(&head->node.refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003180 spin_unlock(&delayed_refs->lock);
3181
David Sterbab3b4aa72011-04-21 01:20:15 +02003182 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003183
David Sterba8cc33e52011-05-02 15:29:25 +02003184 /*
3185 * Mutex was contended, block until it's released and let
3186 * caller try again
3187 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003188 mutex_lock(&head->mutex);
3189 mutex_unlock(&head->mutex);
3190 btrfs_put_delayed_ref(&head->node);
3191 return -EAGAIN;
3192 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003193 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003194
3195 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08003196 list_for_each_entry(ref, &head->ref_list, list) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05003197 /* If it's a shared ref we know a cross reference exists */
3198 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
3199 ret = 1;
3200 break;
3201 }
3202
3203 data_ref = btrfs_delayed_node_to_data_ref(ref);
3204
3205 /*
3206 * If our ref doesn't match the one we're currently looking at
3207 * then we have a cross reference.
3208 */
3209 if (data_ref->root != root->root_key.objectid ||
3210 data_ref->objectid != objectid ||
3211 data_ref->offset != offset) {
3212 ret = 1;
3213 break;
3214 }
3215 }
3216 spin_unlock(&head->lock);
3217 mutex_unlock(&head->mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003218 return ret;
3219}
3220
Liu Boe4c3b2d2017-01-30 12:25:28 -08003221static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003222 struct btrfs_path *path,
3223 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05003224{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003225 struct btrfs_fs_info *fs_info = root->fs_info;
3226 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04003227 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003228 struct btrfs_extent_data_ref *ref;
3229 struct btrfs_extent_inline_ref *iref;
3230 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05003231 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003232 u32 item_size;
Liu Bo3de28d52017-08-18 15:15:19 -06003233 int type;
Yan Zhengf321e492008-07-30 09:26:11 -04003234 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05003235
Chris Masonbe20aa92007-12-17 20:14:01 -05003236 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04003237 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04003238 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05003239
Chris Masonbe20aa92007-12-17 20:14:01 -05003240 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3241 if (ret < 0)
3242 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003243 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04003244
3245 ret = -ENOENT;
3246 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04003247 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003248
Zheng Yan31840ae2008-09-23 13:14:14 -04003249 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04003250 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003251 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05003252
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003253 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05003254 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003255
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003256 ret = 1;
3257 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3258#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3259 if (item_size < sizeof(*ei)) {
3260 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3261 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003262 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003263#endif
3264 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
3265
3266 if (item_size != sizeof(*ei) +
3267 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
3268 goto out;
3269
3270 if (btrfs_extent_generation(leaf, ei) <=
3271 btrfs_root_last_snapshot(&root->root_item))
3272 goto out;
3273
3274 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
Liu Bo3de28d52017-08-18 15:15:19 -06003275
3276 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
3277 if (type != BTRFS_EXTENT_DATA_REF_KEY)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003278 goto out;
3279
3280 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3281 if (btrfs_extent_refs(leaf, ei) !=
3282 btrfs_extent_data_ref_count(leaf, ref) ||
3283 btrfs_extent_data_ref_root(leaf, ref) !=
3284 root->root_key.objectid ||
3285 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3286 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3287 goto out;
3288
Yan Zhengf321e492008-07-30 09:26:11 -04003289 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05003290out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003291 return ret;
3292}
3293
Liu Boe4c3b2d2017-01-30 12:25:28 -08003294int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
3295 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003296{
3297 struct btrfs_path *path;
3298 int ret;
3299 int ret2;
3300
3301 path = btrfs_alloc_path();
3302 if (!path)
3303 return -ENOENT;
3304
3305 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08003306 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003307 offset, bytenr);
3308 if (ret && ret != -ENOENT)
3309 goto out;
3310
Liu Boe4c3b2d2017-01-30 12:25:28 -08003311 ret2 = check_delayed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003312 offset, bytenr);
3313 } while (ret2 == -EAGAIN);
3314
3315 if (ret2 && ret2 != -ENOENT) {
3316 ret = ret2;
3317 goto out;
3318 }
3319
3320 if (ret != -ENOENT || ret2 != -ENOENT)
3321 ret = 0;
3322out:
Yan Zhengf321e492008-07-30 09:26:11 -04003323 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003324 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3325 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04003326 return ret;
3327}
3328
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003329static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05003330 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003331 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07003332 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04003333{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003334 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04003335 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003336 u64 num_bytes;
3337 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003338 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04003339 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003340 struct btrfs_key key;
3341 struct btrfs_file_extent_item *fi;
3342 int i;
3343 int level;
3344 int ret = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003345 int (*process_func)(struct btrfs_trans_handle *,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003346 struct btrfs_root *,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003347 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04003348
David Sterbafccb84c2014-09-29 23:53:21 +02003349
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003350 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003351 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003352
Zheng Yan31840ae2008-09-23 13:14:14 -04003353 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003354 nritems = btrfs_header_nritems(buf);
3355 level = btrfs_header_level(buf);
3356
Miao Xie27cdeb72014-04-02 19:51:05 +08003357 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003358 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003359
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003360 if (inc)
3361 process_func = btrfs_inc_extent_ref;
3362 else
3363 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003364
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003365 if (full_backref)
3366 parent = buf->start;
3367 else
3368 parent = 0;
3369
Zheng Yan31840ae2008-09-23 13:14:14 -04003370 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003371 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003372 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02003373 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003374 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003375 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003376 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003377 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003378 BTRFS_FILE_EXTENT_INLINE)
3379 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003380 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3381 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003382 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003383
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003384 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3385 key.offset -= btrfs_file_extent_offset(buf, fi);
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003386 ret = process_func(trans, root, bytenr, num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003387 parent, ref_root, key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003388 key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003389 if (ret)
3390 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003391 } else {
3392 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003393 num_bytes = fs_info->nodesize;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04003394 ret = process_func(trans, root, bytenr, num_bytes,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003395 parent, ref_root, level - 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003396 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003397 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003398 }
3399 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003400 return 0;
3401fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003402 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003403}
3404
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003405int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003406 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04003407{
Josef Bacike339a6b2014-07-02 10:54:25 -07003408 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003409}
Zheng Yan31840ae2008-09-23 13:14:14 -04003410
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003411int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003412 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003413{
Josef Bacike339a6b2014-07-02 10:54:25 -07003414 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003415}
3416
Chris Mason9078a3e2007-04-26 16:46:15 -04003417static int write_one_cache_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003418 struct btrfs_fs_info *fs_info,
Chris Mason9078a3e2007-04-26 16:46:15 -04003419 struct btrfs_path *path,
3420 struct btrfs_block_group_cache *cache)
3421{
3422 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003423 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003424 unsigned long bi;
3425 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003426
Chris Mason9078a3e2007-04-26 16:46:15 -04003427 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003428 if (ret) {
3429 if (ret > 0)
3430 ret = -ENOENT;
Chris Mason54aa1f42007-06-22 14:16:25 -04003431 goto fail;
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003432 }
Chris Mason5f39d392007-10-15 16:14:19 -04003433
3434 leaf = path->nodes[0];
3435 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3436 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3437 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -04003438fail:
Filipe Manana24b89d02015-04-25 18:31:05 +01003439 btrfs_release_path(path);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003440 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003441
3442}
3443
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003444static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003445next_block_group(struct btrfs_fs_info *fs_info,
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003446 struct btrfs_block_group_cache *cache)
3447{
3448 struct rb_node *node;
Filipe Manana292cbd52014-11-26 15:28:50 +00003449
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003450 spin_lock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003451
3452 /* If our block group was removed, we need a full search. */
3453 if (RB_EMPTY_NODE(&cache->cache_node)) {
3454 const u64 next_bytenr = cache->key.objectid + cache->key.offset;
3455
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003456 spin_unlock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003457 btrfs_put_block_group(cache);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003458 cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
Filipe Manana292cbd52014-11-26 15:28:50 +00003459 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003460 node = rb_next(&cache->cache_node);
3461 btrfs_put_block_group(cache);
3462 if (node) {
3463 cache = rb_entry(node, struct btrfs_block_group_cache,
3464 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003465 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003466 } else
3467 cache = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003468 spin_unlock(&fs_info->block_group_cache_lock);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003469 return cache;
3470}
3471
Josef Bacik0af3d002010-06-21 14:48:16 -04003472static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3473 struct btrfs_trans_handle *trans,
3474 struct btrfs_path *path)
3475{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003476 struct btrfs_fs_info *fs_info = block_group->fs_info;
3477 struct btrfs_root *root = fs_info->tree_root;
Josef Bacik0af3d002010-06-21 14:48:16 -04003478 struct inode *inode = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08003479 struct extent_changeset *data_reserved = NULL;
Josef Bacik0af3d002010-06-21 14:48:16 -04003480 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003481 int dcs = BTRFS_DC_ERROR;
David Sterbaf8c269d2015-01-16 17:21:12 +01003482 u64 num_pages = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003483 int retries = 0;
3484 int ret = 0;
3485
3486 /*
3487 * If this block group is smaller than 100 megs don't bother caching the
3488 * block group.
3489 */
Byongho Leeee221842015-12-15 01:42:10 +09003490 if (block_group->key.offset < (100 * SZ_1M)) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003491 spin_lock(&block_group->lock);
3492 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3493 spin_unlock(&block_group->lock);
3494 return 0;
3495 }
3496
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003497 if (trans->aborted)
3498 return 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003499again:
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003500 inode = lookup_free_space_inode(fs_info, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003501 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3502 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003503 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003504 goto out;
3505 }
3506
3507 if (IS_ERR(inode)) {
3508 BUG_ON(retries);
3509 retries++;
3510
3511 if (block_group->ro)
3512 goto out_free;
3513
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003514 ret = create_free_space_inode(fs_info, trans, block_group,
3515 path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003516 if (ret)
3517 goto out_free;
3518 goto again;
3519 }
3520
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003521 /* We've already setup this transaction, go ahead and exit */
3522 if (block_group->cache_generation == trans->transid &&
3523 i_size_read(inode)) {
3524 dcs = BTRFS_DC_SETUP;
3525 goto out_put;
3526 }
3527
Josef Bacik0af3d002010-06-21 14:48:16 -04003528 /*
3529 * We want to set the generation to 0, that way if anything goes wrong
3530 * from here on out we know not to trust this cache when we load up next
3531 * time.
3532 */
3533 BTRFS_I(inode)->generation = 0;
3534 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003535 if (ret) {
3536 /*
3537 * So theoretically we could recover from this, simply set the
3538 * super cache generation to 0 so we know to invalidate the
3539 * cache, but then we'd have to keep track of the block groups
3540 * that fail this way so we know we _have_ to reset this cache
3541 * before the next commit or risk reading stale cache. So to
3542 * limit our exposure to horrible edge cases lets just abort the
3543 * transaction, this only happens in really bad situations
3544 * anyway.
3545 */
Jeff Mahoney66642832016-06-10 18:19:25 -04003546 btrfs_abort_transaction(trans, ret);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003547 goto out_put;
3548 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003549 WARN_ON(ret);
3550
3551 if (i_size_read(inode) > 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003552 ret = btrfs_check_trunc_cache_free_space(fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003553 &fs_info->global_block_rsv);
Miao Xie7b61cd92013-05-13 13:55:09 +00003554 if (ret)
3555 goto out_put;
3556
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003557 ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003558 if (ret)
3559 goto out_put;
3560 }
3561
3562 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003563 if (block_group->cached != BTRFS_CACHE_FINISHED ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003564 !btrfs_test_opt(fs_info, SPACE_CACHE)) {
Liu Bocf7c1ef2012-07-06 03:31:34 -06003565 /*
3566 * don't bother trying to write stuff out _if_
3567 * a) we're not cached,
Liu Bo1a79c1f2017-03-06 13:49:02 -08003568 * b) we're with nospace_cache mount option,
3569 * c) we're with v2 space_cache (FREE_SPACE_TREE).
Liu Bocf7c1ef2012-07-06 03:31:34 -06003570 */
Josef Bacik2b209822010-12-03 13:17:53 -05003571 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003572 spin_unlock(&block_group->lock);
3573 goto out_put;
3574 }
3575 spin_unlock(&block_group->lock);
3576
Josef Bacik6fc823b2012-08-06 13:46:38 -06003577 /*
Josef Bacik2968b1f2015-10-01 12:55:18 -04003578 * We hit an ENOSPC when setting up the cache in this transaction, just
3579 * skip doing the setup, we've already cleared the cache so we're safe.
3580 */
3581 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
3582 ret = -ENOSPC;
3583 goto out_put;
3584 }
3585
3586 /*
Josef Bacik6fc823b2012-08-06 13:46:38 -06003587 * Try to preallocate enough space based on how big the block group is.
3588 * Keep in mind this has to include any pinned space which could end up
3589 * taking up quite a bit since it's not folded into the other space
3590 * cache.
3591 */
Byongho Leeee221842015-12-15 01:42:10 +09003592 num_pages = div_u64(block_group->key.offset, SZ_256M);
Josef Bacik0af3d002010-06-21 14:48:16 -04003593 if (!num_pages)
3594 num_pages = 1;
3595
Josef Bacik0af3d002010-06-21 14:48:16 -04003596 num_pages *= 16;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003597 num_pages *= PAGE_SIZE;
Josef Bacik0af3d002010-06-21 14:48:16 -04003598
Qu Wenruo364ecf32017-02-27 15:10:38 +08003599 ret = btrfs_check_data_free_space(inode, &data_reserved, 0, num_pages);
Josef Bacik0af3d002010-06-21 14:48:16 -04003600 if (ret)
3601 goto out_put;
3602
3603 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3604 num_pages, num_pages,
3605 &alloc_hint);
Josef Bacik2968b1f2015-10-01 12:55:18 -04003606 /*
3607 * Our cache requires contiguous chunks so that we don't modify a bunch
3608 * of metadata or split extents when writing the cache out, which means
3609 * we can enospc if we are heavily fragmented in addition to just normal
3610 * out of space conditions. So if we hit this just skip setting up any
3611 * other block groups for this transaction, maybe we'll unpin enough
3612 * space the next time around.
3613 */
Josef Bacik2b209822010-12-03 13:17:53 -05003614 if (!ret)
3615 dcs = BTRFS_DC_SETUP;
Josef Bacik2968b1f2015-10-01 12:55:18 -04003616 else if (ret == -ENOSPC)
3617 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
Josef Bacikc09544e2011-08-30 10:19:10 -04003618
Josef Bacik0af3d002010-06-21 14:48:16 -04003619out_put:
3620 iput(inode);
3621out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003622 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003623out:
3624 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003625 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003626 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003627 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003628 spin_unlock(&block_group->lock);
3629
Qu Wenruo364ecf32017-02-27 15:10:38 +08003630 extent_changeset_free(data_reserved);
Josef Bacik0af3d002010-06-21 14:48:16 -04003631 return ret;
3632}
3633
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003634int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003635 struct btrfs_fs_info *fs_info)
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003636{
3637 struct btrfs_block_group_cache *cache, *tmp;
3638 struct btrfs_transaction *cur_trans = trans->transaction;
3639 struct btrfs_path *path;
3640
3641 if (list_empty(&cur_trans->dirty_bgs) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003642 !btrfs_test_opt(fs_info, SPACE_CACHE))
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003643 return 0;
3644
3645 path = btrfs_alloc_path();
3646 if (!path)
3647 return -ENOMEM;
3648
3649 /* Could add new block groups, use _safe just in case */
3650 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
3651 dirty_list) {
3652 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3653 cache_save_setup(cache, trans, path);
3654 }
3655
3656 btrfs_free_path(path);
3657 return 0;
3658}
3659
Chris Mason1bbc6212015-04-06 12:46:08 -07003660/*
3661 * transaction commit does final block group cache writeback during a
3662 * critical section where nothing is allowed to change the FS. This is
3663 * required in order for the cache to actually match the block group,
3664 * but can introduce a lot of latency into the commit.
3665 *
3666 * So, btrfs_start_dirty_block_groups is here to kick off block group
3667 * cache IO. There's a chance we'll have to redo some of it if the
3668 * block group changes again during the commit, but it greatly reduces
3669 * the commit latency by getting rid of the easy block groups while
3670 * we're still allowing others to join the commit.
3671 */
3672int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003673 struct btrfs_fs_info *fs_info)
Chris Mason1bbc6212015-04-06 12:46:08 -07003674{
3675 struct btrfs_block_group_cache *cache;
3676 struct btrfs_transaction *cur_trans = trans->transaction;
3677 int ret = 0;
3678 int should_put;
3679 struct btrfs_path *path = NULL;
3680 LIST_HEAD(dirty);
3681 struct list_head *io = &cur_trans->io_bgs;
3682 int num_started = 0;
3683 int loops = 0;
3684
3685 spin_lock(&cur_trans->dirty_bgs_lock);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003686 if (list_empty(&cur_trans->dirty_bgs)) {
3687 spin_unlock(&cur_trans->dirty_bgs_lock);
3688 return 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003689 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003690 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Chris Mason1bbc6212015-04-06 12:46:08 -07003691 spin_unlock(&cur_trans->dirty_bgs_lock);
3692
3693again:
Chris Mason1bbc6212015-04-06 12:46:08 -07003694 /*
3695 * make sure all the block groups on our dirty list actually
3696 * exist
3697 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003698 btrfs_create_pending_block_groups(trans, fs_info);
Chris Mason1bbc6212015-04-06 12:46:08 -07003699
3700 if (!path) {
3701 path = btrfs_alloc_path();
3702 if (!path)
3703 return -ENOMEM;
3704 }
3705
Filipe Mananab58d1a92015-04-25 18:29:16 +01003706 /*
3707 * cache_write_mutex is here only to save us from balance or automatic
3708 * removal of empty block groups deleting this block group while we are
3709 * writing out the cache
3710 */
3711 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003712 while (!list_empty(&dirty)) {
3713 cache = list_first_entry(&dirty,
3714 struct btrfs_block_group_cache,
3715 dirty_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07003716 /*
3717 * this can happen if something re-dirties a block
3718 * group that is already under IO. Just wait for it to
3719 * finish and then do it all again
3720 */
3721 if (!list_empty(&cache->io_list)) {
3722 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003723 btrfs_wait_cache_io(trans, cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003724 btrfs_put_block_group(cache);
3725 }
3726
3727
3728 /*
3729 * btrfs_wait_cache_io uses the cache->dirty_list to decide
3730 * if it should update the cache_state. Don't delete
3731 * until after we wait.
3732 *
3733 * Since we're not running in the commit critical section
3734 * we need the dirty_bgs_lock to protect from update_block_group
3735 */
3736 spin_lock(&cur_trans->dirty_bgs_lock);
3737 list_del_init(&cache->dirty_list);
3738 spin_unlock(&cur_trans->dirty_bgs_lock);
3739
3740 should_put = 1;
3741
3742 cache_save_setup(cache, trans, path);
3743
3744 if (cache->disk_cache_state == BTRFS_DC_SETUP) {
3745 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003746 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003747 cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003748 if (ret == 0 && cache->io_ctl.inode) {
3749 num_started++;
3750 should_put = 0;
3751
3752 /*
3753 * the cache_write_mutex is protecting
3754 * the io_list
3755 */
3756 list_add_tail(&cache->io_list, io);
3757 } else {
3758 /*
3759 * if we failed to write the cache, the
3760 * generation will be bad and life goes on
3761 */
3762 ret = 0;
3763 }
3764 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003765 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003766 ret = write_one_cache_group(trans, fs_info,
3767 path, cache);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003768 /*
3769 * Our block group might still be attached to the list
3770 * of new block groups in the transaction handle of some
3771 * other task (struct btrfs_trans_handle->new_bgs). This
3772 * means its block group item isn't yet in the extent
3773 * tree. If this happens ignore the error, as we will
3774 * try again later in the critical section of the
3775 * transaction commit.
3776 */
3777 if (ret == -ENOENT) {
3778 ret = 0;
3779 spin_lock(&cur_trans->dirty_bgs_lock);
3780 if (list_empty(&cache->dirty_list)) {
3781 list_add_tail(&cache->dirty_list,
3782 &cur_trans->dirty_bgs);
3783 btrfs_get_block_group(cache);
3784 }
3785 spin_unlock(&cur_trans->dirty_bgs_lock);
3786 } else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003787 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003788 }
3789 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003790
3791 /* if its not on the io list, we need to put the block group */
3792 if (should_put)
3793 btrfs_put_block_group(cache);
3794
3795 if (ret)
3796 break;
Filipe Mananab58d1a92015-04-25 18:29:16 +01003797
3798 /*
3799 * Avoid blocking other tasks for too long. It might even save
3800 * us from writing caches for block groups that are going to be
3801 * removed.
3802 */
3803 mutex_unlock(&trans->transaction->cache_write_mutex);
3804 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003805 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003806 mutex_unlock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003807
3808 /*
3809 * go through delayed refs for all the stuff we've just kicked off
3810 * and then loop back (just once)
3811 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003812 ret = btrfs_run_delayed_refs(trans, fs_info, 0);
Chris Mason1bbc6212015-04-06 12:46:08 -07003813 if (!ret && loops == 0) {
3814 loops++;
3815 spin_lock(&cur_trans->dirty_bgs_lock);
3816 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003817 /*
3818 * dirty_bgs_lock protects us from concurrent block group
3819 * deletes too (not just cache_write_mutex).
3820 */
3821 if (!list_empty(&dirty)) {
3822 spin_unlock(&cur_trans->dirty_bgs_lock);
3823 goto again;
3824 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003825 spin_unlock(&cur_trans->dirty_bgs_lock);
Liu Boc79a1752016-07-20 17:44:12 -07003826 } else if (ret < 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003827 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
Chris Mason1bbc6212015-04-06 12:46:08 -07003828 }
3829
3830 btrfs_free_path(path);
3831 return ret;
3832}
3833
Chris Mason96b51792007-10-15 16:15:19 -04003834int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003835 struct btrfs_fs_info *fs_info)
Chris Mason9078a3e2007-04-26 16:46:15 -04003836{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003837 struct btrfs_block_group_cache *cache;
Josef Bacikce93ec52014-11-17 15:45:48 -05003838 struct btrfs_transaction *cur_trans = trans->transaction;
3839 int ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003840 int should_put;
Chris Mason9078a3e2007-04-26 16:46:15 -04003841 struct btrfs_path *path;
Chris Mason1bbc6212015-04-06 12:46:08 -07003842 struct list_head *io = &cur_trans->io_bgs;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003843 int num_started = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003844
3845 path = btrfs_alloc_path();
3846 if (!path)
3847 return -ENOMEM;
3848
Josef Bacikce93ec52014-11-17 15:45:48 -05003849 /*
Filipe Mananae44081e2015-12-18 03:02:48 +00003850 * Even though we are in the critical section of the transaction commit,
3851 * we can still have concurrent tasks adding elements to this
3852 * transaction's list of dirty block groups. These tasks correspond to
3853 * endio free space workers started when writeback finishes for a
3854 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
3855 * allocate new block groups as a result of COWing nodes of the root
3856 * tree when updating the free space inode. The writeback for the space
3857 * caches is triggered by an earlier call to
3858 * btrfs_start_dirty_block_groups() and iterations of the following
3859 * loop.
3860 * Also we want to do the cache_save_setup first and then run the
Josef Bacikce93ec52014-11-17 15:45:48 -05003861 * delayed refs to make sure we have the best chance at doing this all
3862 * in one shot.
3863 */
Filipe Mananae44081e2015-12-18 03:02:48 +00003864 spin_lock(&cur_trans->dirty_bgs_lock);
Josef Bacikce93ec52014-11-17 15:45:48 -05003865 while (!list_empty(&cur_trans->dirty_bgs)) {
3866 cache = list_first_entry(&cur_trans->dirty_bgs,
3867 struct btrfs_block_group_cache,
3868 dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003869
3870 /*
3871 * this can happen if cache_save_setup re-dirties a block
3872 * group that is already under IO. Just wait for it to
3873 * finish and then do it all again
3874 */
3875 if (!list_empty(&cache->io_list)) {
Filipe Mananae44081e2015-12-18 03:02:48 +00003876 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003877 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003878 btrfs_wait_cache_io(trans, cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003879 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003880 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003881 }
3882
Chris Mason1bbc6212015-04-06 12:46:08 -07003883 /*
3884 * don't remove from the dirty list until after we've waited
3885 * on any pending IO
3886 */
Josef Bacikce93ec52014-11-17 15:45:48 -05003887 list_del_init(&cache->dirty_list);
Filipe Mananae44081e2015-12-18 03:02:48 +00003888 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003889 should_put = 1;
3890
Chris Mason1bbc6212015-04-06 12:46:08 -07003891 cache_save_setup(cache, trans, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003892
Josef Bacikce93ec52014-11-17 15:45:48 -05003893 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003894 ret = btrfs_run_delayed_refs(trans, fs_info,
3895 (unsigned long) -1);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003896
3897 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3898 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003899 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003900 cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003901 if (ret == 0 && cache->io_ctl.inode) {
3902 num_started++;
3903 should_put = 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003904 list_add_tail(&cache->io_list, io);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003905 } else {
3906 /*
3907 * if we failed to write the cache, the
3908 * generation will be bad and life goes on
3909 */
3910 ret = 0;
3911 }
3912 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003913 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003914 ret = write_one_cache_group(trans, fs_info,
3915 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003916 /*
3917 * One of the free space endio workers might have
3918 * created a new block group while updating a free space
3919 * cache's inode (at inode.c:btrfs_finish_ordered_io())
3920 * and hasn't released its transaction handle yet, in
3921 * which case the new block group is still attached to
3922 * its transaction handle and its creation has not
3923 * finished yet (no block group item in the extent tree
3924 * yet, etc). If this is the case, wait for all free
3925 * space endio workers to finish and retry. This is a
3926 * a very rare case so no need for a more efficient and
3927 * complex approach.
3928 */
3929 if (ret == -ENOENT) {
3930 wait_event(cur_trans->writer_wait,
3931 atomic_read(&cur_trans->num_writers) == 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003932 ret = write_one_cache_group(trans, fs_info,
3933 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003934 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003935 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003936 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003937 }
Chris Masonc9dc4c62015-04-04 17:14:42 -07003938
3939 /* if its not on the io list, we need to put the block group */
3940 if (should_put)
3941 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003942 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003943 }
Filipe Mananae44081e2015-12-18 03:02:48 +00003944 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003945
Chris Mason1bbc6212015-04-06 12:46:08 -07003946 while (!list_empty(io)) {
3947 cache = list_first_entry(io, struct btrfs_block_group_cache,
Chris Masonc9dc4c62015-04-04 17:14:42 -07003948 io_list);
3949 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003950 btrfs_wait_cache_io(trans, cache, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003951 btrfs_put_block_group(cache);
3952 }
3953
Chris Mason9078a3e2007-04-26 16:46:15 -04003954 btrfs_free_path(path);
Josef Bacikce93ec52014-11-17 15:45:48 -05003955 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003956}
3957
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003958int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05003959{
3960 struct btrfs_block_group_cache *block_group;
3961 int readonly = 0;
3962
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003963 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003964 if (!block_group || block_group->ro)
3965 readonly = 1;
3966 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003967 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003968 return readonly;
3969}
3970
Filipe Mananaf78c4362016-05-09 13:15:41 +01003971bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3972{
3973 struct btrfs_block_group_cache *bg;
3974 bool ret = true;
3975
3976 bg = btrfs_lookup_block_group(fs_info, bytenr);
3977 if (!bg)
3978 return false;
3979
3980 spin_lock(&bg->lock);
3981 if (bg->ro)
3982 ret = false;
3983 else
3984 atomic_inc(&bg->nocow_writers);
3985 spin_unlock(&bg->lock);
3986
3987 /* no put on block group, done by btrfs_dec_nocow_writers */
3988 if (!ret)
3989 btrfs_put_block_group(bg);
3990
3991 return ret;
3992
3993}
3994
3995void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3996{
3997 struct btrfs_block_group_cache *bg;
3998
3999 bg = btrfs_lookup_block_group(fs_info, bytenr);
4000 ASSERT(bg);
4001 if (atomic_dec_and_test(&bg->nocow_writers))
4002 wake_up_atomic_t(&bg->nocow_writers);
4003 /*
4004 * Once for our lookup and once for the lookup done by a previous call
4005 * to btrfs_inc_nocow_writers()
4006 */
4007 btrfs_put_block_group(bg);
4008 btrfs_put_block_group(bg);
4009}
4010
4011static int btrfs_wait_nocow_writers_atomic_t(atomic_t *a)
4012{
4013 schedule();
4014 return 0;
4015}
4016
4017void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg)
4018{
4019 wait_on_atomic_t(&bg->nocow_writers,
4020 btrfs_wait_nocow_writers_atomic_t,
4021 TASK_UNINTERRUPTIBLE);
4022}
4023
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04004024static const char *alloc_name(u64 flags)
4025{
4026 switch (flags) {
4027 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
4028 return "mixed";
4029 case BTRFS_BLOCK_GROUP_METADATA:
4030 return "metadata";
4031 case BTRFS_BLOCK_GROUP_DATA:
4032 return "data";
4033 case BTRFS_BLOCK_GROUP_SYSTEM:
4034 return "system";
4035 default:
4036 WARN_ON(1);
4037 return "invalid-combination";
4038 };
4039}
4040
Nikolay Borisov2be12ef2017-05-22 09:35:49 +03004041static int create_space_info(struct btrfs_fs_info *info, u64 flags,
4042 struct btrfs_space_info **new)
4043{
4044
4045 struct btrfs_space_info *space_info;
4046 int i;
4047 int ret;
4048
4049 space_info = kzalloc(sizeof(*space_info), GFP_NOFS);
4050 if (!space_info)
4051 return -ENOMEM;
4052
4053 ret = percpu_counter_init(&space_info->total_bytes_pinned, 0,
4054 GFP_KERNEL);
4055 if (ret) {
4056 kfree(space_info);
4057 return ret;
4058 }
4059
4060 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
4061 INIT_LIST_HEAD(&space_info->block_groups[i]);
4062 init_rwsem(&space_info->groups_sem);
4063 spin_lock_init(&space_info->lock);
4064 space_info->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
4065 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
4066 init_waitqueue_head(&space_info->wait);
4067 INIT_LIST_HEAD(&space_info->ro_bgs);
4068 INIT_LIST_HEAD(&space_info->tickets);
4069 INIT_LIST_HEAD(&space_info->priority_tickets);
4070
4071 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
4072 info->space_info_kobj, "%s",
4073 alloc_name(space_info->flags));
4074 if (ret) {
4075 percpu_counter_destroy(&space_info->total_bytes_pinned);
4076 kfree(space_info);
4077 return ret;
4078 }
4079
4080 *new = space_info;
4081 list_add_rcu(&space_info->list, &info->space_info);
4082 if (flags & BTRFS_BLOCK_GROUP_DATA)
4083 info->data_sinfo = space_info;
4084
4085 return ret;
4086}
4087
Nikolay Borisovd2006e62017-05-22 09:35:50 +03004088static void update_space_info(struct btrfs_fs_info *info, u64 flags,
Chris Mason593060d2008-03-25 16:50:33 -04004089 u64 total_bytes, u64 bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -04004090 u64 bytes_readonly,
Chris Mason593060d2008-03-25 16:50:33 -04004091 struct btrfs_space_info **space_info)
4092{
4093 struct btrfs_space_info *found;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004094 int factor;
4095
4096 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
4097 BTRFS_BLOCK_GROUP_RAID10))
4098 factor = 2;
4099 else
4100 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04004101
4102 found = __find_space_info(info, flags);
Nikolay Borisovd2006e62017-05-22 09:35:50 +03004103 ASSERT(found);
4104 spin_lock(&found->lock);
4105 found->total_bytes += total_bytes;
4106 found->disk_total += total_bytes * factor;
4107 found->bytes_used += bytes_used;
4108 found->disk_used += bytes_used * factor;
4109 found->bytes_readonly += bytes_readonly;
4110 if (total_bytes > 0)
4111 found->full = 0;
4112 space_info_add_new_bytes(info, found, total_bytes -
4113 bytes_used - bytes_readonly);
4114 spin_unlock(&found->lock);
4115 *space_info = found;
Chris Mason593060d2008-03-25 16:50:33 -04004116}
4117
Chris Mason8790d502008-04-03 16:29:03 -04004118static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
4119{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03004120 u64 extra_flags = chunk_to_extended(flags) &
4121 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004122
Miao Xiede98ced2013-01-29 10:13:12 +00004123 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004124 if (flags & BTRFS_BLOCK_GROUP_DATA)
4125 fs_info->avail_data_alloc_bits |= extra_flags;
4126 if (flags & BTRFS_BLOCK_GROUP_METADATA)
4127 fs_info->avail_metadata_alloc_bits |= extra_flags;
4128 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
4129 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004130 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04004131}
Chris Mason593060d2008-03-25 16:50:33 -04004132
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004133/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004134 * returns target flags in extended format or 0 if restripe for this
4135 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04004136 *
4137 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004138 */
4139static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
4140{
4141 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4142 u64 target = 0;
4143
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004144 if (!bctl)
4145 return 0;
4146
4147 if (flags & BTRFS_BLOCK_GROUP_DATA &&
4148 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4149 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
4150 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
4151 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4152 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
4153 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
4154 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4155 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
4156 }
4157
4158 return target;
4159}
4160
4161/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004162 * @flags: available profiles in extended format (see ctree.h)
4163 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004164 * Returns reduced profile in chunk format. If profile changing is in
4165 * progress (either running or paused) picks the target profile (if it's
4166 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004167 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004168static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04004169{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004170 u64 num_devices = fs_info->fs_devices->rw_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004171 u64 target;
Zhao Lei9c170b22015-09-15 21:08:08 +08004172 u64 raid_type;
4173 u64 allowed = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004174
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004175 /*
4176 * see if restripe for this chunk_type is in progress, if so
4177 * try to reduce to the target profile
4178 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004179 spin_lock(&fs_info->balance_lock);
4180 target = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004181 if (target) {
4182 /* pick target profile only if it's already available */
4183 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004184 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004185 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004186 }
4187 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004188 spin_unlock(&fs_info->balance_lock);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004189
David Woodhouse53b381b2013-01-29 18:40:14 -05004190 /* First, mask out the RAID levels which aren't possible */
Zhao Lei9c170b22015-09-15 21:08:08 +08004191 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
4192 if (num_devices >= btrfs_raid_array[raid_type].devs_min)
4193 allowed |= btrfs_raid_group[raid_type];
4194 }
4195 allowed &= flags;
Chris Masona061fc82008-05-07 11:43:44 -04004196
Zhao Lei9c170b22015-09-15 21:08:08 +08004197 if (allowed & BTRFS_BLOCK_GROUP_RAID6)
4198 allowed = BTRFS_BLOCK_GROUP_RAID6;
4199 else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
4200 allowed = BTRFS_BLOCK_GROUP_RAID5;
4201 else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
4202 allowed = BTRFS_BLOCK_GROUP_RAID10;
4203 else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
4204 allowed = BTRFS_BLOCK_GROUP_RAID1;
4205 else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
4206 allowed = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04004207
Zhao Lei9c170b22015-09-15 21:08:08 +08004208 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
Chris Masonec44a352008-04-28 15:29:52 -04004209
Zhao Lei9c170b22015-09-15 21:08:08 +08004210 return extended_to_chunk(flags | allowed);
Chris Masonec44a352008-04-28 15:29:52 -04004211}
4212
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004213static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05004214{
Miao Xiede98ced2013-01-29 10:13:12 +00004215 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004216 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004217
4218 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004219 flags = orig_flags;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004220 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00004221
4222 if (flags & BTRFS_BLOCK_GROUP_DATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004223 flags |= fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004224 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004225 flags |= fs_info->avail_system_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004226 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004227 flags |= fs_info->avail_metadata_alloc_bits;
4228 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02004229
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004230 return btrfs_reduce_alloc_profile(fs_info, flags);
Yan, Zhengb742bb822010-05-16 10:46:24 -04004231}
Josef Bacik6a632092009-02-20 11:00:09 -05004232
Jeff Mahoney1b868262017-05-17 11:38:35 -04004233static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04004234{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004235 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004236 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05004237 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004238
Yan, Zhengb742bb822010-05-16 10:46:24 -04004239 if (data)
4240 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004241 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb822010-05-16 10:46:24 -04004242 flags = BTRFS_BLOCK_GROUP_SYSTEM;
4243 else
4244 flags = BTRFS_BLOCK_GROUP_METADATA;
4245
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004246 ret = get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05004247 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004248}
4249
Jeff Mahoney1b868262017-05-17 11:38:35 -04004250u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info)
4251{
4252 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_DATA);
4253}
4254
4255u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info)
4256{
4257 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4258}
4259
4260u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info)
4261{
4262 return get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4263}
4264
Liu Bo41361352017-02-13 15:42:21 -08004265static u64 btrfs_space_info_used(struct btrfs_space_info *s_info,
4266 bool may_use_included)
4267{
4268 ASSERT(s_info);
4269 return s_info->bytes_used + s_info->bytes_reserved +
4270 s_info->bytes_pinned + s_info->bytes_readonly +
4271 (may_use_included ? s_info->bytes_may_use : 0);
4272}
4273
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004274int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05004275{
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004276 struct btrfs_root *root = inode->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004277 struct btrfs_fs_info *fs_info = root->fs_info;
Nikolay Borisov1174cad2017-07-11 13:47:50 +03004278 struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
Josef Bacikab6e24102010-03-19 14:38:13 +00004279 u64 used;
Zhao Lei94b947b2015-02-14 13:23:45 +08004280 int ret = 0;
Zhao Leic99f1b02015-03-02 19:32:20 +08004281 int need_commit = 2;
4282 int have_pinned_space;
Josef Bacik6a632092009-02-20 11:00:09 -05004283
4284 /* make sure bytes are sectorsize aligned */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004285 bytes = ALIGN(bytes, fs_info->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05004286
Miao Xie9dced182013-10-25 17:33:36 +08004287 if (btrfs_is_free_space_inode(inode)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004288 need_commit = 0;
Miao Xie9dced182013-10-25 17:33:36 +08004289 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04004290 }
4291
Josef Bacik6a632092009-02-20 11:00:09 -05004292again:
4293 /* make sure we have enough space to handle the data first */
4294 spin_lock(&data_sinfo->lock);
Liu Bo41361352017-02-13 15:42:21 -08004295 used = btrfs_space_info_used(data_sinfo, true);
Josef Bacikab6e24102010-03-19 14:38:13 +00004296
4297 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004298 struct btrfs_trans_handle *trans;
4299
Josef Bacik6a632092009-02-20 11:00:09 -05004300 /*
4301 * if we don't have enough free bytes in this space then we need
4302 * to alloc a new chunk.
4303 */
Zhao Leib9fd47c2015-02-09 14:40:20 +08004304 if (!data_sinfo->full) {
Josef Bacik6a632092009-02-20 11:00:09 -05004305 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05004306
Chris Mason0e4f8f82011-04-15 16:05:44 -04004307 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05004308 spin_unlock(&data_sinfo->lock);
Nikolay Borisov1174cad2017-07-11 13:47:50 +03004309
Jeff Mahoney1b868262017-05-17 11:38:35 -04004310 alloc_target = btrfs_data_alloc_profile(fs_info);
Miao Xie9dced182013-10-25 17:33:36 +08004311 /*
4312 * It is ugly that we don't call nolock join
4313 * transaction for the free space inode case here.
4314 * But it is safe because we only do the data space
4315 * reservation for the free space cache in the
4316 * transaction context, the common join transaction
4317 * just increase the counter of the current transaction
4318 * handler, doesn't try to acquire the trans_lock of
4319 * the fs.
4320 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004321 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004322 if (IS_ERR(trans))
4323 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05004324
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004325 ret = do_chunk_alloc(trans, fs_info, alloc_target,
Chris Mason0e4f8f82011-04-15 16:05:44 -04004326 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004327 btrfs_end_transaction(trans);
Miao Xied52a5b52011-01-05 10:07:18 +00004328 if (ret < 0) {
4329 if (ret != -ENOSPC)
4330 return ret;
Zhao Leic99f1b02015-03-02 19:32:20 +08004331 else {
4332 have_pinned_space = 1;
Miao Xied52a5b52011-01-05 10:07:18 +00004333 goto commit_trans;
Zhao Leic99f1b02015-03-02 19:32:20 +08004334 }
Miao Xied52a5b52011-01-05 10:07:18 +00004335 }
Chris Mason33b4d472009-09-22 14:45:50 -04004336
Josef Bacik6a632092009-02-20 11:00:09 -05004337 goto again;
4338 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004339
4340 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04004341 * If we don't have enough pinned space to deal with this
Zhao Lei94b947b2015-02-14 13:23:45 +08004342 * allocation, and no removed chunk in current transaction,
4343 * don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004344 */
Zhao Leic99f1b02015-03-02 19:32:20 +08004345 have_pinned_space = percpu_counter_compare(
4346 &data_sinfo->total_bytes_pinned,
4347 used + bytes - data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05004348 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004349
4350 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00004351commit_trans:
Zhao Leic99f1b02015-03-02 19:32:20 +08004352 if (need_commit &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004353 !atomic_read(&fs_info->open_ioctl_trans)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004354 need_commit--;
Josef Bacikb150a4f2013-06-19 15:00:04 -04004355
Zhao Leie1746e82015-12-01 18:39:40 +08004356 if (need_commit > 0) {
4357 btrfs_start_delalloc_roots(fs_info, 0, -1);
Chris Mason6374e57a2017-06-23 09:48:21 -07004358 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004359 (u64)-1);
Zhao Leie1746e82015-12-01 18:39:40 +08004360 }
Zhao Lei9a4e7272015-04-09 12:34:43 +08004361
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004362 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004363 if (IS_ERR(trans))
4364 return PTR_ERR(trans);
Zhao Leic99f1b02015-03-02 19:32:20 +08004365 if (have_pinned_space >= 0 ||
Josef Bacik3204d332015-09-24 10:46:10 -04004366 test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
4367 &trans->transaction->flags) ||
Zhao Leic99f1b02015-03-02 19:32:20 +08004368 need_commit > 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004369 ret = btrfs_commit_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004370 if (ret)
4371 return ret;
Zhao Leid7c15172015-02-26 10:49:20 +08004372 /*
Filipe Mananac2d6cb12016-01-15 11:05:12 +00004373 * The cleaner kthread might still be doing iput
4374 * operations. Wait for it to finish so that
4375 * more space is released.
Zhao Leid7c15172015-02-26 10:49:20 +08004376 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004377 mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
4378 mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
Zhao Lei94b947b2015-02-14 13:23:45 +08004379 goto again;
4380 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004381 btrfs_end_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004382 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004383 }
4384
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004385 trace_btrfs_space_reservation(fs_info,
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004386 "space_info:enospc",
4387 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004388 return -ENOSPC;
4389 }
4390 data_sinfo->bytes_may_use += bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004391 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004392 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004393 spin_unlock(&data_sinfo->lock);
4394
Dongsheng Yang237c0e92014-12-29 06:23:05 -05004395 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004396}
4397
Qu Wenruo364ecf32017-02-27 15:10:38 +08004398int btrfs_check_data_free_space(struct inode *inode,
4399 struct extent_changeset **reserved, u64 start, u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004400{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004401 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004402 int ret;
4403
4404 /* align the range */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004405 len = round_up(start + len, fs_info->sectorsize) -
4406 round_down(start, fs_info->sectorsize);
4407 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004408
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004409 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004410 if (ret < 0)
4411 return ret;
4412
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004413 /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
Qu Wenruo364ecf32017-02-27 15:10:38 +08004414 ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
Qu Wenruo7bc329c2017-02-27 15:10:36 +08004415 if (ret < 0)
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004416 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004417 else
4418 ret = 0;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004419 return ret;
4420}
4421
4422/*
Qu Wenruo4ceff072015-09-08 17:22:42 +08004423 * Called if we need to clear a data reservation for this inode
4424 * Normally in a error case.
4425 *
Qu Wenruo51773be2015-10-08 18:19:37 +08004426 * This one will *NOT* use accurate qgroup reserved space API, just for case
4427 * which we can't sleep and is sure it won't affect qgroup reserved space.
4428 * Like clear_bit_hook().
Qu Wenruo4ceff072015-09-08 17:22:42 +08004429 */
Qu Wenruo51773be2015-10-08 18:19:37 +08004430void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
4431 u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004432{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004433 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004434 struct btrfs_space_info *data_sinfo;
4435
4436 /* Make sure the range is aligned to sectorsize */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004437 len = round_up(start + len, fs_info->sectorsize) -
4438 round_down(start, fs_info->sectorsize);
4439 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004440
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004441 data_sinfo = fs_info->data_sinfo;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004442 spin_lock(&data_sinfo->lock);
4443 if (WARN_ON(data_sinfo->bytes_may_use < len))
4444 data_sinfo->bytes_may_use = 0;
4445 else
4446 data_sinfo->bytes_may_use -= len;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004447 trace_btrfs_space_reservation(fs_info, "space_info",
Qu Wenruo4ceff072015-09-08 17:22:42 +08004448 data_sinfo->flags, len, 0);
4449 spin_unlock(&data_sinfo->lock);
4450}
4451
Qu Wenruo51773be2015-10-08 18:19:37 +08004452/*
4453 * Called if we need to clear a data reservation for this inode
4454 * Normally in a error case.
4455 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04004456 * This one will handle the per-inode data rsv map for accurate reserved
Qu Wenruo51773be2015-10-08 18:19:37 +08004457 * space framework.
4458 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08004459void btrfs_free_reserved_data_space(struct inode *inode,
4460 struct extent_changeset *reserved, u64 start, u64 len)
Qu Wenruo51773be2015-10-08 18:19:37 +08004461{
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004462 struct btrfs_root *root = BTRFS_I(inode)->root;
4463
4464 /* Make sure the range is aligned to sectorsize */
Jeff Mahoneyda170662016-06-15 09:22:56 -04004465 len = round_up(start + len, root->fs_info->sectorsize) -
4466 round_down(start, root->fs_info->sectorsize);
4467 start = round_down(start, root->fs_info->sectorsize);
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004468
Qu Wenruo51773be2015-10-08 18:19:37 +08004469 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruobc42bda2017-02-27 15:10:39 +08004470 btrfs_qgroup_free_data(inode, reserved, start, len);
Qu Wenruo51773be2015-10-08 18:19:37 +08004471}
4472
Josef Bacik97e728d2009-04-21 17:40:57 -04004473static void force_metadata_allocation(struct btrfs_fs_info *info)
4474{
4475 struct list_head *head = &info->space_info;
4476 struct btrfs_space_info *found;
4477
4478 rcu_read_lock();
4479 list_for_each_entry_rcu(found, head, list) {
4480 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004481 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04004482 }
4483 rcu_read_unlock();
4484}
4485
Miao Xie3c76cd82013-04-25 10:12:38 +00004486static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
4487{
4488 return (global->size << 1);
4489}
4490
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004491static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
Josef Bacik698d0082012-09-12 14:08:47 -04004492 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04004493{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004494 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004495 u64 bytes_used = btrfs_space_info_used(sinfo, false);
Chris Masone5bc2452010-10-26 13:37:56 -04004496 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04004497
Chris Mason0e4f8f82011-04-15 16:05:44 -04004498 if (force == CHUNK_ALLOC_FORCE)
4499 return 1;
4500
4501 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04004502 * We need to take into account the global rsv because for all intents
4503 * and purposes it's used space. Don't worry about locking the
4504 * global_rsv, it doesn't change except when the transaction commits.
4505 */
Josef Bacik54338b52012-08-14 16:20:52 -04004506 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004507 bytes_used += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04004508
4509 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04004510 * in limited mode, we want to have some free space up to
4511 * about 1% of the FS size.
4512 */
4513 if (force == CHUNK_ALLOC_LIMITED) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004514 thresh = btrfs_super_total_bytes(fs_info->super_copy);
Byongho Leeee221842015-12-15 01:42:10 +09004515 thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
Chris Mason0e4f8f82011-04-15 16:05:44 -04004516
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004517 if (sinfo->total_bytes - bytes_used < thresh)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004518 return 1;
4519 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004520
Nikolay Borisov8d8aafe2017-06-22 09:51:48 -04004521 if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04004522 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04004523 return 1;
4524}
4525
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004526static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004527{
4528 u64 num_dev;
4529
David Woodhouse53b381b2013-01-29 18:40:14 -05004530 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
4531 BTRFS_BLOCK_GROUP_RAID0 |
4532 BTRFS_BLOCK_GROUP_RAID5 |
4533 BTRFS_BLOCK_GROUP_RAID6))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004534 num_dev = fs_info->fs_devices->rw_devices;
Liu Bo15d1ff82012-03-29 09:57:44 -04004535 else if (type & BTRFS_BLOCK_GROUP_RAID1)
4536 num_dev = 2;
4537 else
4538 num_dev = 1; /* DUP or single */
4539
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004540 return num_dev;
Liu Bo15d1ff82012-03-29 09:57:44 -04004541}
4542
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004543/*
4544 * If @is_allocation is true, reserve space in the system space info necessary
4545 * for allocating a chunk, otherwise if it's false, reserve space necessary for
4546 * removing a chunk.
4547 */
4548void check_system_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004549 struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004550{
4551 struct btrfs_space_info *info;
4552 u64 left;
4553 u64 thresh;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004554 int ret = 0;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004555 u64 num_devs;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004556
4557 /*
4558 * Needed because we can end up allocating a system chunk and for an
4559 * atomic and race free space reservation in the chunk block reserve.
4560 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004561 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
Liu Bo15d1ff82012-03-29 09:57:44 -04004562
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004563 info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
Liu Bo15d1ff82012-03-29 09:57:44 -04004564 spin_lock(&info->lock);
Liu Bo41361352017-02-13 15:42:21 -08004565 left = info->total_bytes - btrfs_space_info_used(info, true);
Liu Bo15d1ff82012-03-29 09:57:44 -04004566 spin_unlock(&info->lock);
4567
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004568 num_devs = get_profile_num_devs(fs_info, type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004569
4570 /* num_devs device items to update and 1 chunk item to add or remove */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004571 thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) +
4572 btrfs_calc_trans_metadata_size(fs_info, 1);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004573
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004574 if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
4575 btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
4576 left, thresh, type);
4577 dump_space_info(fs_info, info, 0, 0);
Liu Bo15d1ff82012-03-29 09:57:44 -04004578 }
4579
4580 if (left < thresh) {
Jeff Mahoney1b868262017-05-17 11:38:35 -04004581 u64 flags = btrfs_system_alloc_profile(fs_info);
Liu Bo15d1ff82012-03-29 09:57:44 -04004582
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004583 /*
4584 * Ignore failure to create system chunk. We might end up not
4585 * needing it, as we might not need to COW all nodes/leafs from
4586 * the paths we visit in the chunk tree (they were already COWed
4587 * or created in the current transaction for example).
4588 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004589 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004590 }
4591
4592 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004593 ret = btrfs_block_rsv_add(fs_info->chunk_root,
4594 &fs_info->chunk_block_rsv,
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004595 thresh, BTRFS_RESERVE_NO_FLUSH);
4596 if (!ret)
4597 trans->chunk_bytes_reserved += thresh;
Liu Bo15d1ff82012-03-29 09:57:44 -04004598 }
4599}
4600
Liu Bo28b737f2016-07-29 11:09:50 -07004601/*
4602 * If force is CHUNK_ALLOC_FORCE:
4603 * - return 1 if it successfully allocates a chunk,
4604 * - return errors including -ENOSPC otherwise.
4605 * If force is NOT CHUNK_ALLOC_FORCE:
4606 * - return 0 if it doesn't need to allocate a new chunk,
4607 * - return 1 if it successfully allocates a chunk,
4608 * - return errors including -ENOSPC otherwise.
4609 */
Chris Mason6324fbf2008-03-24 15:01:59 -04004610static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004611 struct btrfs_fs_info *fs_info, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04004612{
4613 struct btrfs_space_info *space_info;
Josef Bacik6d741192011-04-11 20:20:11 -04004614 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05004615 int ret = 0;
4616
Josef Bacikc6b305a2012-12-18 09:16:16 -05004617 /* Don't re-enter if we're already allocating a chunk */
4618 if (trans->allocating_chunk)
4619 return -ENOSPC;
4620
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004621 space_info = __find_space_info(fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04004622 if (!space_info) {
Nikolay Borisov2be12ef2017-05-22 09:35:49 +03004623 ret = create_space_info(fs_info, flags, &space_info);
4624 if (ret)
4625 return ret;
Chris Mason593060d2008-03-25 16:50:33 -04004626 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004627
Josef Bacik6d741192011-04-11 20:20:11 -04004628again:
Josef Bacik25179202008-10-29 14:49:05 -04004629 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05004630 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004631 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04004632 if (space_info->full) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004633 if (should_alloc_chunk(fs_info, space_info, force))
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004634 ret = -ENOSPC;
4635 else
4636 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04004637 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004638 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04004639 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004640
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004641 if (!should_alloc_chunk(fs_info, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04004642 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04004643 return 0;
4644 } else if (space_info->chunk_alloc) {
4645 wait_for_alloc = 1;
4646 } else {
4647 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04004648 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004649
Josef Bacik25179202008-10-29 14:49:05 -04004650 spin_unlock(&space_info->lock);
4651
Josef Bacik6d741192011-04-11 20:20:11 -04004652 mutex_lock(&fs_info->chunk_mutex);
4653
4654 /*
4655 * The chunk_mutex is held throughout the entirety of a chunk
4656 * allocation, so once we've acquired the chunk_mutex we know that the
4657 * other guy is done and we need to recheck and see if we should
4658 * allocate.
4659 */
4660 if (wait_for_alloc) {
4661 mutex_unlock(&fs_info->chunk_mutex);
4662 wait_for_alloc = 0;
4663 goto again;
4664 }
4665
Josef Bacikc6b305a2012-12-18 09:16:16 -05004666 trans->allocating_chunk = true;
4667
Josef Bacik97e728d2009-04-21 17:40:57 -04004668 /*
Josef Bacik67377732010-09-16 16:19:09 -04004669 * If we have mixed data/metadata chunks we want to make sure we keep
4670 * allocating mixed chunks instead of individual chunks.
4671 */
4672 if (btrfs_mixed_space_info(space_info))
4673 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
4674
4675 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04004676 * if we're doing a data chunk, go ahead and make sure that
4677 * we keep a reasonable number of metadata chunks allocated in the
4678 * FS as well.
4679 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04004680 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04004681 fs_info->data_chunk_allocations++;
4682 if (!(fs_info->data_chunk_allocations %
4683 fs_info->metadata_ratio))
4684 force_metadata_allocation(fs_info);
4685 }
4686
Liu Bo15d1ff82012-03-29 09:57:44 -04004687 /*
4688 * Check if we have enough space in SYSTEM chunk because we may need
4689 * to update devices.
4690 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004691 check_system_chunk(trans, fs_info, flags);
Liu Bo15d1ff82012-03-29 09:57:44 -04004692
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004693 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05004694 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004695
Josef Bacik9ed74f22009-09-11 16:12:44 -04004696 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004697 if (ret < 0 && ret != -ENOSPC)
4698 goto out;
Chris Masond3977122009-01-05 21:25:51 -05004699 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04004700 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04004701 else
4702 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04004703
Chris Mason0e4f8f82011-04-15 16:05:44 -04004704 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004705out:
Josef Bacik6d741192011-04-11 20:20:11 -04004706 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004707 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03004708 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana00d80e32015-07-20 14:56:20 +01004709 /*
4710 * When we allocate a new chunk we reserve space in the chunk block
4711 * reserve to make sure we can COW nodes/leafs in the chunk tree or
4712 * add new nodes/leafs to it if we end up needing to do it when
4713 * inserting the chunk item and updating device items as part of the
4714 * second phase of chunk allocation, performed by
4715 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
4716 * large number of new block groups to create in our transaction
4717 * handle's new_bgs list to avoid exhausting the chunk block reserve
4718 * in extreme cases - like having a single transaction create many new
4719 * block groups when starting to write out the free space caches of all
4720 * the block groups that were made dirty during the lifetime of the
4721 * transaction.
4722 */
Filipe Mananad9a05402015-10-03 13:13:13 +01004723 if (trans->can_flush_pending_bgs &&
Byongho Leeee221842015-12-15 01:42:10 +09004724 trans->chunk_bytes_reserved >= (u64)SZ_2M) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004725 btrfs_create_pending_block_groups(trans, fs_info);
Filipe Manana00d80e32015-07-20 14:56:20 +01004726 btrfs_trans_release_chunk_metadata(trans);
4727 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004728 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04004729}
4730
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004731static int can_overcommit(struct btrfs_fs_info *fs_info,
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004732 struct btrfs_space_info *space_info, u64 bytes,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004733 enum btrfs_reserve_flush_enum flush,
4734 bool system_chunk)
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004735{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004736 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04004737 u64 profile;
Miao Xie3c76cd82013-04-25 10:12:38 +00004738 u64 space_size;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004739 u64 avail;
4740 u64 used;
4741
Josef Bacik957780e2016-05-17 13:30:55 -04004742 /* Don't overcommit when in mixed mode. */
4743 if (space_info->flags & BTRFS_BLOCK_GROUP_DATA)
4744 return 0;
4745
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004746 if (system_chunk)
4747 profile = btrfs_system_alloc_profile(fs_info);
4748 else
4749 profile = btrfs_metadata_alloc_profile(fs_info);
4750
Liu Bo41361352017-02-13 15:42:21 -08004751 used = btrfs_space_info_used(space_info, false);
Josef Bacik96f1bb52013-01-30 17:02:51 -05004752
Josef Bacik96f1bb52013-01-30 17:02:51 -05004753 /*
4754 * We only want to allow over committing if we have lots of actual space
4755 * free, but if we don't have enough space to handle the global reserve
4756 * space then we could end up having a real enospc problem when trying
4757 * to allocate a chunk or some other such important allocation.
4758 */
Miao Xie3c76cd82013-04-25 10:12:38 +00004759 spin_lock(&global_rsv->lock);
4760 space_size = calc_global_rsv_need_space(global_rsv);
4761 spin_unlock(&global_rsv->lock);
4762 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05004763 return 0;
4764
4765 used += space_info->bytes_may_use;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004766
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004767 avail = atomic64_read(&fs_info->free_chunk_space);
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004768
4769 /*
4770 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05004771 * space is actually useable. For raid56, the space info used
4772 * doesn't include the parity drive, so we don't have to
4773 * change the math
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004774 */
4775 if (profile & (BTRFS_BLOCK_GROUP_DUP |
4776 BTRFS_BLOCK_GROUP_RAID1 |
4777 BTRFS_BLOCK_GROUP_RAID10))
4778 avail >>= 1;
4779
4780 /*
Miao Xie561c2942012-10-16 11:32:18 +00004781 * If we aren't flushing all things, let us overcommit up to
4782 * 1/2th of the space. If we can flush, don't let us overcommit
4783 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004784 */
Miao Xie08e007d2012-10-16 11:33:38 +00004785 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04004786 avail >>= 3;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004787 else
Josef Bacik14575ae2013-09-17 10:48:00 -04004788 avail >>= 1;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004789
Josef Bacik14575ae2013-09-17 10:48:00 -04004790 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004791 return 1;
4792 return 0;
4793}
4794
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004795static void btrfs_writeback_inodes_sb_nr(struct btrfs_fs_info *fs_info,
Miao Xie6c255e62014-03-06 13:55:01 +08004796 unsigned long nr_pages, int nr_items)
Miao Xieda633a42012-12-20 11:19:09 +00004797{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004798 struct super_block *sb = fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00004799
Josef Bacik925a6ef2013-06-20 12:31:27 -04004800 if (down_read_trylock(&sb->s_umount)) {
4801 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4802 up_read(&sb->s_umount);
4803 } else {
Miao Xieda633a42012-12-20 11:19:09 +00004804 /*
4805 * We needn't worry the filesystem going from r/w to r/o though
4806 * we don't acquire ->s_umount mutex, because the filesystem
4807 * should guarantee the delalloc inodes list be empty after
4808 * the filesystem is readonly(all dirty pages are written to
4809 * the disk).
4810 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004811 btrfs_start_delalloc_roots(fs_info, 0, nr_items);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004812 if (!current->journal_info)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004813 btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1);
Miao Xieda633a42012-12-20 11:19:09 +00004814 }
4815}
4816
Chris Mason6374e57a2017-06-23 09:48:21 -07004817static inline u64 calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004818 u64 to_reclaim)
Miao Xie18cd8ea2013-11-04 23:13:22 +08004819{
4820 u64 bytes;
Chris Mason6374e57a2017-06-23 09:48:21 -07004821 u64 nr;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004822
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004823 bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Chris Mason6374e57a2017-06-23 09:48:21 -07004824 nr = div64_u64(to_reclaim, bytes);
Miao Xie18cd8ea2013-11-04 23:13:22 +08004825 if (!nr)
4826 nr = 1;
4827 return nr;
4828}
4829
Byongho Leeee221842015-12-15 01:42:10 +09004830#define EXTENT_SIZE_PER_ITEM SZ_256K
Miao Xiec61a16a2013-11-04 23:13:23 +08004831
Yan, Zheng5da9d012010-05-16 10:46:25 -04004832/*
4833 * shrink metadata reservation for delalloc
4834 */
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04004835static void shrink_delalloc(struct btrfs_fs_info *fs_info, u64 to_reclaim,
4836 u64 orig, bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004837{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004838 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004839 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004840 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004841 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004842 u64 max_reclaim;
Chris Mason6374e57a2017-06-23 09:48:21 -07004843 u64 items;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004844 long time_left;
Miao Xied3ee29e32013-11-04 23:13:20 +08004845 unsigned long nr_pages;
4846 int loops;
Miao Xie08e007d2012-10-16 11:33:38 +00004847 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004848
Miao Xiec61a16a2013-11-04 23:13:23 +08004849 /* Calc the number of the pages we need flush for space reservation */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004850 items = calc_reclaim_items_nr(fs_info, to_reclaim);
Chris Mason6374e57a2017-06-23 09:48:21 -07004851 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004852
Josef Bacik663350a2011-11-03 22:54:25 -04004853 trans = (struct btrfs_trans_handle *)current->journal_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004854 block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004855 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04004856
Miao Xie963d6782013-01-29 10:10:51 +00004857 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004858 &fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004859 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004860 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004861 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004862 if (wait_ordered)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004863 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004864 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004865 }
4866
Miao Xied3ee29e32013-11-04 23:13:20 +08004867 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004868 while (delalloc_bytes && loops < 3) {
4869 max_reclaim = min(delalloc_bytes, to_reclaim);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004870 nr_pages = max_reclaim >> PAGE_SHIFT;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004871 btrfs_writeback_inodes_sb_nr(fs_info, nr_pages, items);
Josef Bacikdea31f52012-09-06 16:47:00 -04004872 /*
4873 * We need to wait for the async pages to actually start before
4874 * we do anything.
4875 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004876 max_reclaim = atomic_read(&fs_info->async_delalloc_pages);
Miao Xie9f3a0742013-11-04 23:13:24 +08004877 if (!max_reclaim)
4878 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004879
Miao Xie9f3a0742013-11-04 23:13:24 +08004880 if (max_reclaim <= nr_pages)
4881 max_reclaim = 0;
4882 else
4883 max_reclaim -= nr_pages;
4884
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004885 wait_event(fs_info->async_submit_wait,
4886 atomic_read(&fs_info->async_delalloc_pages) <=
Miao Xie9f3a0742013-11-04 23:13:24 +08004887 (int)max_reclaim);
4888skip_async:
Miao Xie08e007d2012-10-16 11:33:38 +00004889 if (!trans)
4890 flush = BTRFS_RESERVE_FLUSH_ALL;
4891 else
4892 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04004893 spin_lock(&space_info->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04004894 if (list_empty(&space_info->tickets) &&
4895 list_empty(&space_info->priority_tickets)) {
4896 spin_unlock(&space_info->lock);
4897 break;
4898 }
Josef Bacik0019f102010-10-15 15:18:40 -04004899 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004900
Chris Mason36e39c42011-03-12 07:08:42 -05004901 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004902 if (wait_ordered && !trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004903 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf104d042011-10-14 13:56:58 -04004904 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004905 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004906 if (time_left)
4907 break;
4908 }
Miao Xie963d6782013-01-29 10:10:51 +00004909 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004910 &fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004911 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004912}
4913
Josef Bacik996478c2017-08-22 16:00:39 -04004914struct reserve_ticket {
4915 u64 bytes;
4916 int error;
4917 struct list_head list;
4918 wait_queue_head_t wait;
4919};
4920
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004921/**
Josef Bacik663350a2011-11-03 22:54:25 -04004922 * maybe_commit_transaction - possibly commit the transaction if its ok to
4923 * @root - the root we're allocating for
4924 * @bytes - the number of bytes we want to reserve
4925 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004926 *
Josef Bacik663350a2011-11-03 22:54:25 -04004927 * This will check to make sure that committing the transaction will actually
4928 * get us somewhere and then commit the transaction if it does. Otherwise it
4929 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004930 */
Jeff Mahoney0c9ab342017-02-15 16:28:28 -05004931static int may_commit_transaction(struct btrfs_fs_info *fs_info,
Josef Bacik996478c2017-08-22 16:00:39 -04004932 struct btrfs_space_info *space_info)
Josef Bacik663350a2011-11-03 22:54:25 -04004933{
Josef Bacik996478c2017-08-22 16:00:39 -04004934 struct reserve_ticket *ticket = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004935 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;
Josef Bacik663350a2011-11-03 22:54:25 -04004936 struct btrfs_trans_handle *trans;
Josef Bacik996478c2017-08-22 16:00:39 -04004937 u64 bytes;
Josef Bacik663350a2011-11-03 22:54:25 -04004938
4939 trans = (struct btrfs_trans_handle *)current->journal_info;
4940 if (trans)
4941 return -EAGAIN;
4942
Josef Bacik996478c2017-08-22 16:00:39 -04004943 spin_lock(&space_info->lock);
4944 if (!list_empty(&space_info->priority_tickets))
4945 ticket = list_first_entry(&space_info->priority_tickets,
4946 struct reserve_ticket, list);
4947 else if (!list_empty(&space_info->tickets))
4948 ticket = list_first_entry(&space_info->tickets,
4949 struct reserve_ticket, list);
4950 bytes = (ticket) ? ticket->bytes : 0;
4951 spin_unlock(&space_info->lock);
4952
4953 if (!bytes)
4954 return 0;
Josef Bacik663350a2011-11-03 22:54:25 -04004955
4956 /* See if there is enough pinned space to make this reservation */
Josef Bacikb150a4f2013-06-19 15:00:04 -04004957 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Miao Xie0424c542014-03-06 13:54:59 +08004958 bytes) >= 0)
Josef Bacik663350a2011-11-03 22:54:25 -04004959 goto commit;
Josef Bacik663350a2011-11-03 22:54:25 -04004960
4961 /*
4962 * See if there is some space in the delayed insertion reservation for
4963 * this reservation.
4964 */
4965 if (space_info != delayed_rsv->space_info)
4966 return -ENOSPC;
4967
4968 spin_lock(&delayed_rsv->lock);
Josef Bacik996478c2017-08-22 16:00:39 -04004969 if (delayed_rsv->size > bytes)
4970 bytes = 0;
4971 else
4972 bytes -= delayed_rsv->size;
Josef Bacikb150a4f2013-06-19 15:00:04 -04004973 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Josef Bacik996478c2017-08-22 16:00:39 -04004974 bytes) < 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004975 spin_unlock(&delayed_rsv->lock);
4976 return -ENOSPC;
4977 }
4978 spin_unlock(&delayed_rsv->lock);
4979
4980commit:
Jeff Mahoneya9b33112017-05-17 11:38:34 -04004981 trans = btrfs_join_transaction(fs_info->extent_root);
Josef Bacik663350a2011-11-03 22:54:25 -04004982 if (IS_ERR(trans))
4983 return -ENOSPC;
4984
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004985 return btrfs_commit_transaction(trans);
Josef Bacik663350a2011-11-03 22:54:25 -04004986}
4987
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03004988/*
4989 * Try to flush some data based on policy set by @state. This is only advisory
4990 * and may fail for various reasons. The caller is supposed to examine the
4991 * state of @space_info to detect the outcome.
4992 */
4993static void flush_space(struct btrfs_fs_info *fs_info,
Josef Bacik96c3f432012-06-21 14:05:49 -04004994 struct btrfs_space_info *space_info, u64 num_bytes,
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03004995 int state)
Josef Bacik96c3f432012-06-21 14:05:49 -04004996{
Jeff Mahoneya9b33112017-05-17 11:38:34 -04004997 struct btrfs_root *root = fs_info->extent_root;
Josef Bacik96c3f432012-06-21 14:05:49 -04004998 struct btrfs_trans_handle *trans;
4999 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04005000 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04005001
5002 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04005003 case FLUSH_DELAYED_ITEMS_NR:
5004 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08005005 if (state == FLUSH_DELAYED_ITEMS_NR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005006 nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
Miao Xie18cd8ea2013-11-04 23:13:22 +08005007 else
Josef Bacik96c3f432012-06-21 14:05:49 -04005008 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08005009
Josef Bacik96c3f432012-06-21 14:05:49 -04005010 trans = btrfs_join_transaction(root);
5011 if (IS_ERR(trans)) {
5012 ret = PTR_ERR(trans);
5013 break;
5014 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005015 ret = btrfs_run_delayed_items_nr(trans, fs_info, nr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005016 btrfs_end_transaction(trans);
Josef Bacik96c3f432012-06-21 14:05:49 -04005017 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04005018 case FLUSH_DELALLOC:
5019 case FLUSH_DELALLOC_WAIT:
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005020 shrink_delalloc(fs_info, num_bytes * 2, num_bytes,
Josef Bacik67b0fd62012-09-24 13:42:00 -04005021 state == FLUSH_DELALLOC_WAIT);
5022 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04005023 case ALLOC_CHUNK:
5024 trans = btrfs_join_transaction(root);
5025 if (IS_ERR(trans)) {
5026 ret = PTR_ERR(trans);
5027 break;
5028 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005029 ret = do_chunk_alloc(trans, fs_info,
Jeff Mahoney1b868262017-05-17 11:38:35 -04005030 btrfs_metadata_alloc_profile(fs_info),
Josef Bacikea658ba2012-09-11 16:57:25 -04005031 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005032 btrfs_end_transaction(trans);
Alex Lyakaseecba892015-12-06 12:32:31 +02005033 if (ret > 0 || ret == -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04005034 ret = 0;
5035 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04005036 case COMMIT_TRANS:
Josef Bacik996478c2017-08-22 16:00:39 -04005037 ret = may_commit_transaction(fs_info, space_info);
Josef Bacik96c3f432012-06-21 14:05:49 -04005038 break;
5039 default:
5040 ret = -ENOSPC;
5041 break;
5042 }
5043
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005044 trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, state,
5045 ret);
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03005046 return;
Josef Bacik96c3f432012-06-21 14:05:49 -04005047}
Miao Xie21c7e752014-05-13 17:29:04 -07005048
5049static inline u64
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005050btrfs_calc_reclaim_metadata_size(struct btrfs_fs_info *fs_info,
5051 struct btrfs_space_info *space_info,
5052 bool system_chunk)
Miao Xie21c7e752014-05-13 17:29:04 -07005053{
Josef Bacik957780e2016-05-17 13:30:55 -04005054 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07005055 u64 used;
5056 u64 expected;
Josef Bacik957780e2016-05-17 13:30:55 -04005057 u64 to_reclaim = 0;
Miao Xie21c7e752014-05-13 17:29:04 -07005058
Josef Bacik957780e2016-05-17 13:30:55 -04005059 list_for_each_entry(ticket, &space_info->tickets, list)
5060 to_reclaim += ticket->bytes;
5061 list_for_each_entry(ticket, &space_info->priority_tickets, list)
5062 to_reclaim += ticket->bytes;
5063 if (to_reclaim)
5064 return to_reclaim;
Miao Xie21c7e752014-05-13 17:29:04 -07005065
Wang Xiaoguange0af2482016-08-31 19:46:16 +08005066 to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005067 if (can_overcommit(fs_info, space_info, to_reclaim,
5068 BTRFS_RESERVE_FLUSH_ALL, system_chunk))
Wang Xiaoguange0af2482016-08-31 19:46:16 +08005069 return 0;
5070
Nikolay Borisov0eee8a42017-06-14 11:35:34 +03005071 used = btrfs_space_info_used(space_info, true);
5072
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005073 if (can_overcommit(fs_info, space_info, SZ_1M,
5074 BTRFS_RESERVE_FLUSH_ALL, system_chunk))
Miao Xie21c7e752014-05-13 17:29:04 -07005075 expected = div_factor_fine(space_info->total_bytes, 95);
5076 else
5077 expected = div_factor_fine(space_info->total_bytes, 90);
5078
5079 if (used > expected)
5080 to_reclaim = used - expected;
5081 else
5082 to_reclaim = 0;
5083 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
5084 space_info->bytes_reserved);
Miao Xie21c7e752014-05-13 17:29:04 -07005085 return to_reclaim;
5086}
5087
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005088static inline int need_do_async_reclaim(struct btrfs_fs_info *fs_info,
5089 struct btrfs_space_info *space_info,
5090 u64 used, bool system_chunk)
Miao Xie21c7e752014-05-13 17:29:04 -07005091{
Josef Bacik365c5312015-02-18 13:58:15 -08005092 u64 thresh = div_factor_fine(space_info->total_bytes, 98);
5093
5094 /* If we're just plain full then async reclaim just slows us down. */
Josef Bacikbaee8792016-01-26 09:35:38 -05005095 if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh)
Josef Bacik365c5312015-02-18 13:58:15 -08005096 return 0;
5097
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005098 if (!btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5099 system_chunk))
Liu Bo25ce4592014-09-10 12:58:50 +08005100 return 0;
Liu Bo25ce4592014-09-10 12:58:50 +08005101
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005102 return (used >= thresh && !btrfs_fs_closing(fs_info) &&
5103 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
Miao Xie21c7e752014-05-13 17:29:04 -07005104}
5105
Josef Bacik957780e2016-05-17 13:30:55 -04005106static void wake_all_tickets(struct list_head *head)
Miao Xie21c7e752014-05-13 17:29:04 -07005107{
Josef Bacik957780e2016-05-17 13:30:55 -04005108 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07005109
Josef Bacik957780e2016-05-17 13:30:55 -04005110 while (!list_empty(head)) {
5111 ticket = list_first_entry(head, struct reserve_ticket, list);
5112 list_del_init(&ticket->list);
5113 ticket->error = -ENOSPC;
5114 wake_up(&ticket->wait);
Miao Xie21c7e752014-05-13 17:29:04 -07005115 }
Miao Xie21c7e752014-05-13 17:29:04 -07005116}
5117
Josef Bacik957780e2016-05-17 13:30:55 -04005118/*
5119 * This is for normal flushers, we can wait all goddamned day if we want to. We
5120 * will loop and continuously try to flush as long as we are making progress.
5121 * We count progress as clearing off tickets each time we have to loop.
5122 */
Miao Xie21c7e752014-05-13 17:29:04 -07005123static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
5124{
5125 struct btrfs_fs_info *fs_info;
5126 struct btrfs_space_info *space_info;
5127 u64 to_reclaim;
5128 int flush_state;
Josef Bacik957780e2016-05-17 13:30:55 -04005129 int commit_cycles = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005130 u64 last_tickets_id;
Miao Xie21c7e752014-05-13 17:29:04 -07005131
5132 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
5133 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
5134
Josef Bacik957780e2016-05-17 13:30:55 -04005135 spin_lock(&space_info->lock);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005136 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5137 false);
Josef Bacik957780e2016-05-17 13:30:55 -04005138 if (!to_reclaim) {
5139 space_info->flush = 0;
5140 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005141 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005142 }
Wang Xiaoguangce129652016-09-02 10:58:46 +08005143 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005144 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005145
5146 flush_state = FLUSH_DELAYED_ITEMS_NR;
5147 do {
Nikolay Borisove38ae7a2017-07-25 17:48:28 +03005148 flush_space(fs_info, space_info, to_reclaim, flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005149 spin_lock(&space_info->lock);
5150 if (list_empty(&space_info->tickets)) {
5151 space_info->flush = 0;
5152 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005153 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005154 }
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005155 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info,
5156 space_info,
5157 false);
Wang Xiaoguangce129652016-09-02 10:58:46 +08005158 if (last_tickets_id == space_info->tickets_id) {
Josef Bacik957780e2016-05-17 13:30:55 -04005159 flush_state++;
5160 } else {
Wang Xiaoguangce129652016-09-02 10:58:46 +08005161 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005162 flush_state = FLUSH_DELAYED_ITEMS_NR;
5163 if (commit_cycles)
5164 commit_cycles--;
5165 }
5166
5167 if (flush_state > COMMIT_TRANS) {
5168 commit_cycles++;
5169 if (commit_cycles > 2) {
5170 wake_all_tickets(&space_info->tickets);
5171 space_info->flush = 0;
5172 } else {
5173 flush_state = FLUSH_DELAYED_ITEMS_NR;
5174 }
5175 }
5176 spin_unlock(&space_info->lock);
5177 } while (flush_state <= COMMIT_TRANS);
Miao Xie21c7e752014-05-13 17:29:04 -07005178}
5179
5180void btrfs_init_async_reclaim_work(struct work_struct *work)
5181{
5182 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
5183}
5184
Josef Bacik957780e2016-05-17 13:30:55 -04005185static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
5186 struct btrfs_space_info *space_info,
5187 struct reserve_ticket *ticket)
5188{
5189 u64 to_reclaim;
5190 int flush_state = FLUSH_DELAYED_ITEMS_NR;
5191
5192 spin_lock(&space_info->lock);
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005193 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info, space_info,
5194 false);
Josef Bacik957780e2016-05-17 13:30:55 -04005195 if (!to_reclaim) {
5196 spin_unlock(&space_info->lock);
5197 return;
5198 }
5199 spin_unlock(&space_info->lock);
5200
5201 do {
Nikolay Borisov7bdd6272017-07-11 13:25:13 +03005202 flush_space(fs_info, space_info, to_reclaim, flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005203 flush_state++;
5204 spin_lock(&space_info->lock);
5205 if (ticket->bytes == 0) {
5206 spin_unlock(&space_info->lock);
5207 return;
5208 }
5209 spin_unlock(&space_info->lock);
5210
5211 /*
5212 * Priority flushers can't wait on delalloc without
5213 * deadlocking.
5214 */
5215 if (flush_state == FLUSH_DELALLOC ||
5216 flush_state == FLUSH_DELALLOC_WAIT)
5217 flush_state = ALLOC_CHUNK;
5218 } while (flush_state < COMMIT_TRANS);
5219}
5220
5221static int wait_reserve_ticket(struct btrfs_fs_info *fs_info,
5222 struct btrfs_space_info *space_info,
5223 struct reserve_ticket *ticket, u64 orig_bytes)
5224
5225{
5226 DEFINE_WAIT(wait);
5227 int ret = 0;
5228
5229 spin_lock(&space_info->lock);
5230 while (ticket->bytes > 0 && ticket->error == 0) {
5231 ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE);
5232 if (ret) {
5233 ret = -EINTR;
5234 break;
5235 }
5236 spin_unlock(&space_info->lock);
5237
5238 schedule();
5239
5240 finish_wait(&ticket->wait, &wait);
5241 spin_lock(&space_info->lock);
5242 }
5243 if (!ret)
5244 ret = ticket->error;
5245 if (!list_empty(&ticket->list))
5246 list_del_init(&ticket->list);
5247 if (ticket->bytes && ticket->bytes < orig_bytes) {
5248 u64 num_bytes = orig_bytes - ticket->bytes;
5249 space_info->bytes_may_use -= num_bytes;
5250 trace_btrfs_space_reservation(fs_info, "space_info",
5251 space_info->flags, num_bytes, 0);
5252 }
5253 spin_unlock(&space_info->lock);
5254
5255 return ret;
5256}
5257
Josef Bacik663350a2011-11-03 22:54:25 -04005258/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005259 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5260 * @root - the root we're allocating for
Josef Bacik957780e2016-05-17 13:30:55 -04005261 * @space_info - the space info we want to allocate from
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005262 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04005263 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005264 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04005265 * This will reserve orig_bytes number of bytes from the space info associated
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005266 * with the block_rsv. If there is not enough space it will make an attempt to
5267 * flush out space to make room. It will do this by flushing delalloc if
5268 * possible or committing the transaction. If flush is 0 then no attempts to
5269 * regain reservations will be made and this will fail if there is not enough
5270 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005271 */
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005272static int __reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
Josef Bacik957780e2016-05-17 13:30:55 -04005273 struct btrfs_space_info *space_info,
5274 u64 orig_bytes,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005275 enum btrfs_reserve_flush_enum flush,
5276 bool system_chunk)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005277{
Josef Bacik957780e2016-05-17 13:30:55 -04005278 struct reserve_ticket ticket;
Josef Bacik2bf64752011-09-26 17:12:22 -04005279 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005280 int ret = 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005281
Josef Bacik957780e2016-05-17 13:30:55 -04005282 ASSERT(orig_bytes);
Josef Bacik8ca17f02016-05-27 13:24:13 -04005283 ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL);
5284
Yan, Zhengf0486c62010-05-16 10:46:25 -04005285 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04005286 ret = -ENOSPC;
Liu Bo41361352017-02-13 15:42:21 -08005287 used = btrfs_space_info_used(space_info, true);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005288
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005289 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005290 * If we have enough space then hooray, make our reservation and carry
5291 * on. If not see if we can overcommit, and if we can, hooray carry on.
5292 * If not things get more complicated.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005293 */
Josef Bacik957780e2016-05-17 13:30:55 -04005294 if (used + orig_bytes <= space_info->total_bytes) {
5295 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005296 trace_btrfs_space_reservation(fs_info, "space_info",
5297 space_info->flags, orig_bytes, 1);
Josef Bacik957780e2016-05-17 13:30:55 -04005298 ret = 0;
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005299 } else if (can_overcommit(fs_info, space_info, orig_bytes, flush,
5300 system_chunk)) {
Josef Bacik44734ed2012-09-28 16:04:19 -04005301 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005302 trace_btrfs_space_reservation(fs_info, "space_info",
5303 space_info->flags, orig_bytes, 1);
Josef Bacik44734ed2012-09-28 16:04:19 -04005304 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04005305 }
5306
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005307 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005308 * If we couldn't make a reservation then setup our reservation ticket
5309 * and kick the async worker if it's not already running.
Miao Xie08e007d2012-10-16 11:33:38 +00005310 *
Josef Bacik957780e2016-05-17 13:30:55 -04005311 * If we are a priority flusher then we just need to add our ticket to
5312 * the list and we will do our own flushing further down.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005313 */
Josef Bacik72bcd992012-12-18 15:16:34 -05005314 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacik957780e2016-05-17 13:30:55 -04005315 ticket.bytes = orig_bytes;
5316 ticket.error = 0;
5317 init_waitqueue_head(&ticket.wait);
5318 if (flush == BTRFS_RESERVE_FLUSH_ALL) {
5319 list_add_tail(&ticket.list, &space_info->tickets);
5320 if (!space_info->flush) {
5321 space_info->flush = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005322 trace_btrfs_trigger_flush(fs_info,
Josef Bacikf376df22016-03-25 13:25:56 -04005323 space_info->flags,
5324 orig_bytes, flush,
5325 "enospc");
Josef Bacik957780e2016-05-17 13:30:55 -04005326 queue_work(system_unbound_wq,
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005327 &fs_info->async_reclaim_work);
Josef Bacik957780e2016-05-17 13:30:55 -04005328 }
5329 } else {
5330 list_add_tail(&ticket.list,
5331 &space_info->priority_tickets);
5332 }
Miao Xie21c7e752014-05-13 17:29:04 -07005333 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
5334 used += orig_bytes;
Josef Bacikf6acfd52014-09-18 11:27:17 -04005335 /*
5336 * We will do the space reservation dance during log replay,
5337 * which means we won't have fs_info->fs_root set, so don't do
5338 * the async reclaim as we will panic.
5339 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005340 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) &&
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005341 need_do_async_reclaim(fs_info, space_info,
5342 used, system_chunk) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005343 !work_busy(&fs_info->async_reclaim_work)) {
5344 trace_btrfs_trigger_flush(fs_info, space_info->flags,
5345 orig_bytes, flush, "preempt");
Miao Xie21c7e752014-05-13 17:29:04 -07005346 queue_work(system_unbound_wq,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005347 &fs_info->async_reclaim_work);
Josef Bacikf376df22016-03-25 13:25:56 -04005348 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005349 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005350 spin_unlock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00005351 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik957780e2016-05-17 13:30:55 -04005352 return ret;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005353
Josef Bacik957780e2016-05-17 13:30:55 -04005354 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005355 return wait_reserve_ticket(fs_info, space_info, &ticket,
Josef Bacik957780e2016-05-17 13:30:55 -04005356 orig_bytes);
Miao Xie08e007d2012-10-16 11:33:38 +00005357
Josef Bacik957780e2016-05-17 13:30:55 -04005358 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005359 priority_reclaim_metadata_space(fs_info, space_info, &ticket);
Josef Bacik957780e2016-05-17 13:30:55 -04005360 spin_lock(&space_info->lock);
5361 if (ticket.bytes) {
5362 if (ticket.bytes < orig_bytes) {
5363 u64 num_bytes = orig_bytes - ticket.bytes;
5364 space_info->bytes_may_use -= num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005365 trace_btrfs_space_reservation(fs_info, "space_info",
5366 space_info->flags,
5367 num_bytes, 0);
Miao Xie08e007d2012-10-16 11:33:38 +00005368
Josef Bacik957780e2016-05-17 13:30:55 -04005369 }
5370 list_del_init(&ticket.list);
5371 ret = -ENOSPC;
5372 }
5373 spin_unlock(&space_info->lock);
5374 ASSERT(list_empty(&ticket.list));
5375 return ret;
5376}
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005377
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005378/**
5379 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5380 * @root - the root we're allocating for
5381 * @block_rsv - the block_rsv we're allocating for
5382 * @orig_bytes - the number of bytes we want
5383 * @flush - whether or not we can flush to make our reservation
5384 *
Josef Bacik957780e2016-05-17 13:30:55 -04005385 * This will reserve orgi_bytes number of bytes from the space info associated
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005386 * with the block_rsv. If there is not enough space it will make an attempt to
5387 * flush out space to make room. It will do this by flushing delalloc if
5388 * possible or committing the transaction. If flush is 0 then no attempts to
5389 * regain reservations will be made and this will fail if there is not enough
5390 * space already.
5391 */
5392static int reserve_metadata_bytes(struct btrfs_root *root,
5393 struct btrfs_block_rsv *block_rsv,
5394 u64 orig_bytes,
5395 enum btrfs_reserve_flush_enum flush)
5396{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005397 struct btrfs_fs_info *fs_info = root->fs_info;
5398 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04005399 int ret;
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005400 bool system_chunk = (root == fs_info->chunk_root);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005401
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005402 ret = __reserve_metadata_bytes(fs_info, block_rsv->space_info,
5403 orig_bytes, flush, system_chunk);
Josef Bacik5d803662013-02-07 16:06:02 -05005404 if (ret == -ENOSPC &&
5405 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
Josef Bacik5d803662013-02-07 16:06:02 -05005406 if (block_rsv != global_rsv &&
5407 !block_rsv_use_bytes(global_rsv, orig_bytes))
5408 ret = 0;
5409 }
Jeff Mahoneycab45e22013-10-16 16:27:01 -04005410 if (ret == -ENOSPC)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005411 trace_btrfs_space_reservation(fs_info, "space_info:enospc",
Josef Bacik957780e2016-05-17 13:30:55 -04005412 block_rsv->space_info->flags,
5413 orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005414 return ret;
5415}
5416
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005417static struct btrfs_block_rsv *get_block_rsv(
5418 const struct btrfs_trans_handle *trans,
5419 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005420{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005421 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik4c13d752011-08-30 11:31:29 -04005422 struct btrfs_block_rsv *block_rsv = NULL;
5423
Alexandru Moisee9cf4392015-09-09 00:18:50 +00005424 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005425 (root == fs_info->csum_root && trans->adding_csums) ||
5426 (root == fs_info->uuid_root))
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02005427 block_rsv = trans->block_rsv;
5428
Josef Bacik4c13d752011-08-30 11:31:29 -04005429 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005430 block_rsv = root->block_rsv;
5431
5432 if (!block_rsv)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005433 block_rsv = &fs_info->empty_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005434
5435 return block_rsv;
5436}
5437
5438static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
5439 u64 num_bytes)
5440{
5441 int ret = -ENOSPC;
5442 spin_lock(&block_rsv->lock);
5443 if (block_rsv->reserved >= num_bytes) {
5444 block_rsv->reserved -= num_bytes;
5445 if (block_rsv->reserved < block_rsv->size)
5446 block_rsv->full = 0;
5447 ret = 0;
5448 }
5449 spin_unlock(&block_rsv->lock);
5450 return ret;
5451}
5452
5453static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
5454 u64 num_bytes, int update_size)
5455{
5456 spin_lock(&block_rsv->lock);
5457 block_rsv->reserved += num_bytes;
5458 if (update_size)
5459 block_rsv->size += num_bytes;
5460 else if (block_rsv->reserved >= block_rsv->size)
5461 block_rsv->full = 1;
5462 spin_unlock(&block_rsv->lock);
5463}
5464
Josef Bacikd52be812013-05-29 14:54:47 -04005465int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
5466 struct btrfs_block_rsv *dest, u64 num_bytes,
5467 int min_factor)
5468{
5469 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5470 u64 min_bytes;
5471
5472 if (global_rsv->space_info != dest->space_info)
5473 return -ENOSPC;
5474
5475 spin_lock(&global_rsv->lock);
5476 min_bytes = div_factor(global_rsv->size, min_factor);
5477 if (global_rsv->reserved < min_bytes + num_bytes) {
5478 spin_unlock(&global_rsv->lock);
5479 return -ENOSPC;
5480 }
5481 global_rsv->reserved -= num_bytes;
5482 if (global_rsv->reserved < global_rsv->size)
5483 global_rsv->full = 0;
5484 spin_unlock(&global_rsv->lock);
5485
5486 block_rsv_add_bytes(dest, num_bytes, 1);
5487 return 0;
5488}
5489
Josef Bacik957780e2016-05-17 13:30:55 -04005490/*
5491 * This is for space we already have accounted in space_info->bytes_may_use, so
5492 * basically when we're returning space from block_rsv's.
5493 */
5494static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
5495 struct btrfs_space_info *space_info,
5496 u64 num_bytes)
5497{
5498 struct reserve_ticket *ticket;
5499 struct list_head *head;
5500 u64 used;
5501 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH;
5502 bool check_overcommit = false;
5503
5504 spin_lock(&space_info->lock);
5505 head = &space_info->priority_tickets;
5506
5507 /*
5508 * If we are over our limit then we need to check and see if we can
5509 * overcommit, and if we can't then we just need to free up our space
5510 * and not satisfy any requests.
5511 */
Nikolay Borisov0eee8a42017-06-14 11:35:34 +03005512 used = btrfs_space_info_used(space_info, true);
Josef Bacik957780e2016-05-17 13:30:55 -04005513 if (used - num_bytes >= space_info->total_bytes)
5514 check_overcommit = true;
5515again:
5516 while (!list_empty(head) && num_bytes) {
5517 ticket = list_first_entry(head, struct reserve_ticket,
5518 list);
5519 /*
5520 * We use 0 bytes because this space is already reserved, so
5521 * adding the ticket space would be a double count.
5522 */
5523 if (check_overcommit &&
Jeff Mahoneyc1c49192017-05-17 11:38:36 -04005524 !can_overcommit(fs_info, space_info, 0, flush, false))
Josef Bacik957780e2016-05-17 13:30:55 -04005525 break;
5526 if (num_bytes >= ticket->bytes) {
5527 list_del_init(&ticket->list);
5528 num_bytes -= ticket->bytes;
5529 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005530 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005531 wake_up(&ticket->wait);
5532 } else {
5533 ticket->bytes -= num_bytes;
5534 num_bytes = 0;
5535 }
5536 }
5537
5538 if (num_bytes && head == &space_info->priority_tickets) {
5539 head = &space_info->tickets;
5540 flush = BTRFS_RESERVE_FLUSH_ALL;
5541 goto again;
5542 }
5543 space_info->bytes_may_use -= num_bytes;
5544 trace_btrfs_space_reservation(fs_info, "space_info",
5545 space_info->flags, num_bytes, 0);
5546 spin_unlock(&space_info->lock);
5547}
5548
5549/*
5550 * This is for newly allocated space that isn't accounted in
5551 * space_info->bytes_may_use yet. So if we allocate a chunk or unpin an extent
5552 * we use this helper.
5553 */
5554static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
5555 struct btrfs_space_info *space_info,
5556 u64 num_bytes)
5557{
5558 struct reserve_ticket *ticket;
5559 struct list_head *head = &space_info->priority_tickets;
5560
5561again:
5562 while (!list_empty(head) && num_bytes) {
5563 ticket = list_first_entry(head, struct reserve_ticket,
5564 list);
5565 if (num_bytes >= ticket->bytes) {
5566 trace_btrfs_space_reservation(fs_info, "space_info",
5567 space_info->flags,
5568 ticket->bytes, 1);
5569 list_del_init(&ticket->list);
5570 num_bytes -= ticket->bytes;
5571 space_info->bytes_may_use += ticket->bytes;
5572 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005573 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005574 wake_up(&ticket->wait);
5575 } else {
5576 trace_btrfs_space_reservation(fs_info, "space_info",
5577 space_info->flags,
5578 num_bytes, 1);
5579 space_info->bytes_may_use += num_bytes;
5580 ticket->bytes -= num_bytes;
5581 num_bytes = 0;
5582 }
5583 }
5584
5585 if (num_bytes && head == &space_info->priority_tickets) {
5586 head = &space_info->tickets;
5587 goto again;
5588 }
5589}
5590
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005591static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
5592 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02005593 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005594{
5595 struct btrfs_space_info *space_info = block_rsv->space_info;
5596
5597 spin_lock(&block_rsv->lock);
5598 if (num_bytes == (u64)-1)
5599 num_bytes = block_rsv->size;
5600 block_rsv->size -= num_bytes;
5601 if (block_rsv->reserved >= block_rsv->size) {
5602 num_bytes = block_rsv->reserved - block_rsv->size;
5603 block_rsv->reserved = block_rsv->size;
5604 block_rsv->full = 1;
5605 } else {
5606 num_bytes = 0;
5607 }
5608 spin_unlock(&block_rsv->lock);
5609
5610 if (num_bytes > 0) {
5611 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00005612 spin_lock(&dest->lock);
5613 if (!dest->full) {
5614 u64 bytes_to_add;
5615
5616 bytes_to_add = dest->size - dest->reserved;
5617 bytes_to_add = min(num_bytes, bytes_to_add);
5618 dest->reserved += bytes_to_add;
5619 if (dest->reserved >= dest->size)
5620 dest->full = 1;
5621 num_bytes -= bytes_to_add;
5622 }
5623 spin_unlock(&dest->lock);
5624 }
Josef Bacik957780e2016-05-17 13:30:55 -04005625 if (num_bytes)
5626 space_info_add_old_bytes(fs_info, space_info,
5627 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005628 }
5629}
5630
Josef Bacik25d609f2016-03-25 13:25:48 -04005631int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
5632 struct btrfs_block_rsv *dst, u64 num_bytes,
5633 int update_size)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005634{
5635 int ret;
5636
5637 ret = block_rsv_use_bytes(src, num_bytes);
5638 if (ret)
5639 return ret;
5640
Josef Bacik25d609f2016-03-25 13:25:48 -04005641 block_rsv_add_bytes(dst, num_bytes, update_size);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005642 return 0;
5643}
5644
Miao Xie66d8f3d2012-09-06 04:02:28 -06005645void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005646{
5647 memset(rsv, 0, sizeof(*rsv));
5648 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06005649 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005650}
5651
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005652struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
Miao Xie66d8f3d2012-09-06 04:02:28 -06005653 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005654{
5655 struct btrfs_block_rsv *block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005656
5657 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
5658 if (!block_rsv)
5659 return NULL;
5660
Miao Xie66d8f3d2012-09-06 04:02:28 -06005661 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005662 block_rsv->space_info = __find_space_info(fs_info,
5663 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005664 return block_rsv;
5665}
5666
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005667void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005668 struct btrfs_block_rsv *rsv)
5669{
Josef Bacik2aaa6652012-08-29 14:27:18 -04005670 if (!rsv)
5671 return;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005672 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Josef Bacikdabdb642011-08-08 12:50:18 -04005673 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005674}
5675
Chris Masoncdfb0802015-04-06 18:17:00 -07005676void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
5677{
5678 kfree(rsv);
5679}
5680
Miao Xie08e007d2012-10-16 11:33:38 +00005681int btrfs_block_rsv_add(struct btrfs_root *root,
5682 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
5683 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005684{
5685 int ret;
5686
5687 if (num_bytes == 0)
5688 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005689
Miao Xie61b520a2011-11-10 20:45:05 -05005690 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005691 if (!ret) {
5692 block_rsv_add_bytes(block_rsv, num_bytes, 1);
5693 return 0;
5694 }
5695
Yan, Zhengf0486c62010-05-16 10:46:25 -04005696 return ret;
5697}
5698
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005699int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005700{
5701 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005702 int ret = -ENOSPC;
5703
5704 if (!block_rsv)
5705 return 0;
5706
5707 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04005708 num_bytes = div_factor(block_rsv->size, min_factor);
5709 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005710 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005711 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005712
Josef Bacik36ba0222011-10-18 12:15:48 -04005713 return ret;
5714}
5715
Miao Xie08e007d2012-10-16 11:33:38 +00005716int btrfs_block_rsv_refill(struct btrfs_root *root,
5717 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
5718 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04005719{
5720 u64 num_bytes = 0;
5721 int ret = -ENOSPC;
5722
5723 if (!block_rsv)
5724 return 0;
5725
5726 spin_lock(&block_rsv->lock);
5727 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04005728 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005729 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04005730 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04005731 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005732 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04005733
Yan, Zhengf0486c62010-05-16 10:46:25 -04005734 if (!ret)
5735 return 0;
5736
Miao Xieaa38a712011-11-18 17:43:00 +08005737 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04005738 if (!ret) {
5739 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005740 return 0;
5741 }
5742
Josef Bacik13553e52011-08-08 13:33:21 -04005743 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005744}
5745
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005746void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005747 struct btrfs_block_rsv *block_rsv,
5748 u64 num_bytes)
5749{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005750 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5751
Liu Bo17504582013-12-29 21:44:50 +08005752 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04005753 block_rsv->space_info != global_rsv->space_info)
5754 global_rsv = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005755 block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005756}
5757
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005758static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
5759{
5760 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
5761 struct btrfs_space_info *sinfo = block_rsv->space_info;
5762 u64 num_bytes;
5763
Josef Bacikae2e4722016-05-27 12:58:35 -04005764 /*
5765 * The global block rsv is based on the size of the extent tree, the
5766 * checksum tree and the root tree. If the fs is empty we want to set
5767 * it to a minimal amount for safety.
5768 */
5769 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) +
5770 btrfs_root_used(&fs_info->csum_root->root_item) +
5771 btrfs_root_used(&fs_info->tree_root->root_item);
5772 num_bytes = max_t(u64, num_bytes, SZ_16M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005773
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005774 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005775 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005776
Byongho Leeee221842015-12-15 01:42:10 +09005777 block_rsv->size = min_t(u64, num_bytes, SZ_512M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005778
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005779 if (block_rsv->reserved < block_rsv->size) {
Liu Bo41361352017-02-13 15:42:21 -08005780 num_bytes = btrfs_space_info_used(sinfo, true);
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005781 if (sinfo->total_bytes > num_bytes) {
5782 num_bytes = sinfo->total_bytes - num_bytes;
5783 num_bytes = min(num_bytes,
5784 block_rsv->size - block_rsv->reserved);
5785 block_rsv->reserved += num_bytes;
5786 sinfo->bytes_may_use += num_bytes;
5787 trace_btrfs_space_reservation(fs_info, "space_info",
5788 sinfo->flags, num_bytes,
5789 1);
5790 }
5791 } else if (block_rsv->reserved > block_rsv->size) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005792 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04005793 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005794 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005795 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005796 block_rsv->reserved = block_rsv->size;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005797 }
David Sterba182608c2011-05-05 13:13:16 +02005798
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005799 if (block_rsv->reserved == block_rsv->size)
5800 block_rsv->full = 1;
5801 else
5802 block_rsv->full = 0;
5803
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005804 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005805 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005806}
5807
Yan, Zhengf0486c62010-05-16 10:46:25 -04005808static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
5809{
5810 struct btrfs_space_info *space_info;
5811
5812 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
5813 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005814
5815 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005816 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005817 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005818 fs_info->trans_block_rsv.space_info = space_info;
5819 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04005820 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005821
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005822 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
5823 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
5824 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
5825 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00005826 if (fs_info->quota_root)
5827 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005828 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005829
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005830 update_global_block_rsv(fs_info);
5831}
5832
5833static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
5834{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005835 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
5836 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005837 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
5838 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
5839 WARN_ON(fs_info->trans_block_rsv.size > 0);
5840 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
5841 WARN_ON(fs_info->chunk_block_rsv.size > 0);
5842 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04005843 WARN_ON(fs_info->delayed_block_rsv.size > 0);
5844 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04005845}
5846
Yan, Zhenga22285a2010-05-16 10:48:46 -04005847void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005848 struct btrfs_fs_info *fs_info)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005849{
Josef Bacik0e721102012-06-26 16:13:18 -04005850 if (!trans->block_rsv)
5851 return;
5852
Yan, Zhenga22285a2010-05-16 10:48:46 -04005853 if (!trans->bytes_reserved)
5854 return;
5855
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005856 trace_btrfs_space_reservation(fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04005857 trans->transid, trans->bytes_reserved, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005858 btrfs_block_rsv_release(fs_info, trans->block_rsv,
5859 trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005860 trans->bytes_reserved = 0;
5861}
5862
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005863/*
5864 * To be called after all the new block groups attached to the transaction
5865 * handle have been created (btrfs_create_pending_block_groups()).
5866 */
5867void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
5868{
Jeff Mahoney64b63582016-06-20 17:23:41 -04005869 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005870
5871 if (!trans->chunk_bytes_reserved)
5872 return;
5873
5874 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
5875
5876 block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL,
5877 trans->chunk_bytes_reserved);
5878 trans->chunk_bytes_reserved = 0;
5879}
5880
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005881/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04005882int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005883 struct btrfs_inode *inode)
Yan, Zhengd68fc572010-05-16 10:49:58 -04005884{
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005885 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
5886 struct btrfs_root *root = inode->root;
Josef Bacik40acc3e2016-05-27 13:01:08 -04005887 /*
5888 * We always use trans->block_rsv here as we will have reserved space
5889 * for our orphan when starting the transaction, using get_block_rsv()
5890 * here will sometimes make us choose the wrong block rsv as we could be
5891 * doing a reloc inode for a non refcounted root.
5892 */
5893 struct btrfs_block_rsv *src_rsv = trans->block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005894 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
5895
5896 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04005897 * We need to hold space in order to delete our orphan item once we've
5898 * added it, so this takes the reservation so we can release it later
5899 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04005900 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005901 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5902
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005903 trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode),
5904 num_bytes, 1);
Josef Bacik25d609f2016-03-25 13:25:48 -04005905 return btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005906}
5907
Nikolay Borisov703b3912017-02-20 13:50:40 +02005908void btrfs_orphan_release_metadata(struct btrfs_inode *inode)
Yan, Zhengd68fc572010-05-16 10:49:58 -04005909{
Nikolay Borisov703b3912017-02-20 13:50:40 +02005910 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
5911 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005912 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5913
Nikolay Borisov703b3912017-02-20 13:50:40 +02005914 trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode),
5915 num_bytes, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005916 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, num_bytes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005917}
5918
Miao Xied5c12072013-02-28 10:04:33 +00005919/*
5920 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
5921 * root: the root of the parent directory
5922 * rsv: block reservation
5923 * items: the number of items that we need do reservation
5924 * qgroup_reserved: used to return the reserved size in qgroup
5925 *
5926 * This function is used to reserve the space for snapshot/subvolume
5927 * creation and deletion. Those operations are different with the
5928 * common file/directory operations, they change two fs/file trees
5929 * and root tree, the number of items that the qgroup reserves is
5930 * different with the free space reservation. So we can not use
Nicholas D Steeves01327612016-05-19 21:18:45 -04005931 * the space reservation mechanism in start_transaction().
Miao Xied5c12072013-02-28 10:04:33 +00005932 */
5933int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
5934 struct btrfs_block_rsv *rsv,
5935 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005936 u64 *qgroup_reserved,
5937 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005938{
Miao Xied5c12072013-02-28 10:04:33 +00005939 u64 num_bytes;
5940 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005941 struct btrfs_fs_info *fs_info = root->fs_info;
5942 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00005943
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005944 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Miao Xied5c12072013-02-28 10:04:33 +00005945 /* One for parent inode, two for dir entries */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005946 num_bytes = 3 * fs_info->nodesize;
Jeff Mahoney003d7c52017-01-25 09:50:33 -05005947 ret = btrfs_qgroup_reserve_meta(root, num_bytes, true);
Miao Xied5c12072013-02-28 10:04:33 +00005948 if (ret)
5949 return ret;
5950 } else {
5951 num_bytes = 0;
5952 }
5953
5954 *qgroup_reserved = num_bytes;
5955
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005956 num_bytes = btrfs_calc_trans_metadata_size(fs_info, items);
5957 rsv->space_info = __find_space_info(fs_info,
Miao Xied5c12072013-02-28 10:04:33 +00005958 BTRFS_BLOCK_GROUP_METADATA);
5959 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
5960 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005961
5962 if (ret == -ENOSPC && use_global_rsv)
Josef Bacik25d609f2016-03-25 13:25:48 -04005963 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005964
Qu Wenruo71741092015-09-08 17:22:41 +08005965 if (ret && *qgroup_reserved)
5966 btrfs_qgroup_free_meta(root, *qgroup_reserved);
Miao Xied5c12072013-02-28 10:04:33 +00005967
5968 return ret;
5969}
5970
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005971void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
David Sterba7775c812017-02-10 19:18:18 +01005972 struct btrfs_block_rsv *rsv)
Miao Xied5c12072013-02-28 10:04:33 +00005973{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005974 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005975}
5976
Josef Bacik7709cde2011-08-04 10:25:02 -04005977/**
5978 * drop_outstanding_extent - drop an outstanding extent
5979 * @inode: the inode we're dropping the extent for
Nicholas D Steeves01327612016-05-19 21:18:45 -04005980 * @num_bytes: the number of bytes we're releasing.
Josef Bacik7709cde2011-08-04 10:25:02 -04005981 *
5982 * This is called when we are freeing up an outstanding extent, either called
5983 * after an error or after an extent is written. This will return the number of
5984 * reserved extents that need to be freed. This must be called with
5985 * BTRFS_I(inode)->lock held.
5986 */
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02005987static unsigned drop_outstanding_extent(struct btrfs_inode *inode,
5988 u64 num_bytes)
Josef Bacik9e0baf62011-07-15 15:16:44 +00005989{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005990 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005991 unsigned dropped_extents = 0;
David Sterba823bb202017-01-04 11:09:51 +01005992 unsigned num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005993
David Sterba823bb202017-01-04 11:09:51 +01005994 num_extents = count_max_extents(num_bytes);
Josef Bacikdcab6a32015-02-11 15:08:59 -05005995 ASSERT(num_extents);
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02005996 ASSERT(inode->outstanding_extents >= num_extents);
5997 inode->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005998
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02005999 if (inode->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04006000 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02006001 &inode->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05006002 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05006003
Josef Bacik9e0baf62011-07-15 15:16:44 +00006004 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006005 * If we have more or the same amount of outstanding extents than we have
Josef Bacik9e0baf62011-07-15 15:16:44 +00006006 * reserved then we need to leave the reserved extents count alone.
6007 */
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02006008 if (inode->outstanding_extents >= inode->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05006009 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006010
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02006011 dropped_extents = inode->reserved_extents - inode->outstanding_extents;
6012 inode->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05006013 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006014}
6015
Josef Bacik7709cde2011-08-04 10:25:02 -04006016/**
Nicholas D Steeves01327612016-05-19 21:18:45 -04006017 * calc_csum_metadata_size - return the amount of metadata space that must be
6018 * reserved/freed for the given bytes.
Josef Bacik7709cde2011-08-04 10:25:02 -04006019 * @inode: the inode we're manipulating
6020 * @num_bytes: the number of bytes in question
6021 * @reserve: 1 if we are reserving space, 0 if we are freeing space
6022 *
6023 * This adjusts the number of csum_bytes in the inode and then returns the
6024 * correct amount of metadata that must either be reserved or freed. We
6025 * calculate how many checksums we can fit into one leaf and then divide the
6026 * number of bytes that will need to be checksumed by this value to figure out
6027 * how many checksums will be required. If we are adding bytes then the number
6028 * may go up and we will return the number of additional bytes that must be
6029 * reserved. If it is going down we will return the number of bytes that must
6030 * be freed.
6031 *
6032 * This must be called with BTRFS_I(inode)->lock held.
6033 */
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02006034static u64 calc_csum_metadata_size(struct btrfs_inode *inode, u64 num_bytes,
Josef Bacik7709cde2011-08-04 10:25:02 -04006035 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006036{
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02006037 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Josef Bacik12621332015-02-03 07:50:16 -08006038 u64 old_csums, num_csums;
Josef Bacik7709cde2011-08-04 10:25:02 -04006039
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02006040 if (inode->flags & BTRFS_INODE_NODATASUM && inode->csum_bytes == 0)
Josef Bacik7709cde2011-08-04 10:25:02 -04006041 return 0;
6042
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02006043 old_csums = btrfs_csum_bytes_to_leaves(fs_info, inode->csum_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04006044 if (reserve)
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02006045 inode->csum_bytes += num_bytes;
Josef Bacik7709cde2011-08-04 10:25:02 -04006046 else
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02006047 inode->csum_bytes -= num_bytes;
6048 num_csums = btrfs_csum_bytes_to_leaves(fs_info, inode->csum_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04006049
6050 /* No change, no need to reserve more */
6051 if (old_csums == num_csums)
6052 return 0;
6053
6054 if (reserve)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006055 return btrfs_calc_trans_metadata_size(fs_info,
Josef Bacik7709cde2011-08-04 10:25:02 -04006056 num_csums - old_csums);
6057
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006058 return btrfs_calc_trans_metadata_size(fs_info, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006059}
6060
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006061int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006062{
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006063 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
6064 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006065 struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006066 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05006067 u64 csum_bytes;
David Sterba823bb202017-01-04 11:09:51 +01006068 unsigned nr_extents;
Miao Xie08e007d2012-10-16 11:33:38 +00006069 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07006070 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006071 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00006072 u64 to_free = 0;
6073 unsigned dropped;
Josef Bacik48c3d482016-03-25 13:25:49 -04006074 bool release_extra = false;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006075
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006076 /* If we are a free space inode we need to not flush since we will be in
6077 * the middle of a transaction commit. We also don't need the delalloc
6078 * mutex since we won't race with anybody. We need this mostly to make
6079 * lockdep shut its filthy mouth.
Josef Bacikbac357dc2016-07-20 16:48:45 -07006080 *
6081 * If we have a transaction open (can happen if we call truncate_block
6082 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006083 */
6084 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00006085 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006086 delalloc_lock = false;
Josef Bacikbac357dc2016-07-20 16:48:45 -07006087 } else if (current->journal_info) {
6088 flush = BTRFS_RESERVE_FLUSH_LIMIT;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006089 }
Josef Bacikc09544e2011-08-30 10:19:10 -04006090
Miao Xie08e007d2012-10-16 11:33:38 +00006091 if (flush != BTRFS_RESERVE_NO_FLUSH &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006092 btrfs_transaction_in_commit(fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006093 schedule_timeout(1);
6094
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006095 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006096 mutex_lock(&inode->delalloc_mutex);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006097
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006098 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04006099
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006100 spin_lock(&inode->lock);
David Sterba823bb202017-01-04 11:09:51 +01006101 nr_extents = count_max_extents(num_bytes);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006102 inode->outstanding_extents += nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05006103
Josef Bacik48c3d482016-03-25 13:25:49 -04006104 nr_extents = 0;
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006105 if (inode->outstanding_extents > inode->reserved_extents)
6106 nr_extents += inode->outstanding_extents -
6107 inode->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05006108
Josef Bacik48c3d482016-03-25 13:25:49 -04006109 /* We always want to reserve a slot for updating the inode. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006110 to_reserve = btrfs_calc_trans_metadata_size(fs_info, nr_extents + 1);
Josef Bacik7709cde2011-08-04 10:25:02 -04006111 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006112 csum_bytes = inode->csum_bytes;
6113 spin_unlock(&inode->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05006114
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006115 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Qu Wenruo71741092015-09-08 17:22:41 +08006116 ret = btrfs_qgroup_reserve_meta(root,
Jeff Mahoney003d7c52017-01-25 09:50:33 -05006117 nr_extents * fs_info->nodesize, true);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006118 if (ret)
6119 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00006120 }
Arne Jansenc5567232011-09-14 15:44:05 +02006121
Josef Bacik48c3d482016-03-25 13:25:49 -04006122 ret = btrfs_block_rsv_add(root, block_rsv, to_reserve, flush);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006123 if (unlikely(ret)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04006124 btrfs_qgroup_free_meta(root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006125 nr_extents * fs_info->nodesize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006126 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00006127 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006128
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006129 spin_lock(&inode->lock);
Josef Bacik48c3d482016-03-25 13:25:49 -04006130 if (test_and_set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006131 &inode->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006132 to_reserve -= btrfs_calc_trans_metadata_size(fs_info, 1);
Josef Bacik48c3d482016-03-25 13:25:49 -04006133 release_extra = true;
Josef Bacik660d3f62011-12-09 11:18:51 -05006134 }
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006135 inode->reserved_extents += nr_extents;
6136 spin_unlock(&inode->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006137
6138 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006139 mutex_unlock(&inode->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05006140
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006141 if (to_reserve)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006142 trace_btrfs_space_reservation(fs_info, "delalloc",
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006143 btrfs_ino(inode), to_reserve, 1);
Josef Bacik48c3d482016-03-25 13:25:49 -04006144 if (release_extra)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006145 btrfs_block_rsv_release(fs_info, block_rsv,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006146 btrfs_calc_trans_metadata_size(fs_info, 1));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006147 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00006148
6149out_fail:
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006150 spin_lock(&inode->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05006151 dropped = drop_outstanding_extent(inode, num_bytes);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006152 /*
6153 * If the inodes csum_bytes is the same as the original
6154 * csum_bytes then we know we haven't raced with any free()ers
6155 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00006156 */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006157 if (inode->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00006158 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04006159 } else {
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006160 u64 orig_csum_bytes = inode->csum_bytes;
Josef Bacikf4881bc2013-03-25 16:03:35 -04006161 u64 bytes;
6162
6163 /*
6164 * This is tricky, but first we need to figure out how much we
Nicholas D Steeves01327612016-05-19 21:18:45 -04006165 * freed from any free-ers that occurred during this
Josef Bacikf4881bc2013-03-25 16:03:35 -04006166 * reservation, so we reset ->csum_bytes to the csum_bytes
6167 * before we dropped our lock, and then call the free for the
6168 * number of bytes that were freed while we were trying our
6169 * reservation.
6170 */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006171 bytes = csum_bytes - inode->csum_bytes;
6172 inode->csum_bytes = csum_bytes;
Josef Bacikf4881bc2013-03-25 16:03:35 -04006173 to_free = calc_csum_metadata_size(inode, bytes, 0);
6174
6175
6176 /*
6177 * Now we need to see how much we would have freed had we not
6178 * been making this reservation and our ->csum_bytes were not
6179 * artificially inflated.
6180 */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006181 inode->csum_bytes = csum_bytes - num_bytes;
Josef Bacikf4881bc2013-03-25 16:03:35 -04006182 bytes = csum_bytes - orig_csum_bytes;
6183 bytes = calc_csum_metadata_size(inode, bytes, 0);
6184
6185 /*
6186 * Now reset ->csum_bytes to what it should be. If bytes is
Nicholas D Steeves01327612016-05-19 21:18:45 -04006187 * more than to_free then we would have freed more space had we
Josef Bacikf4881bc2013-03-25 16:03:35 -04006188 * not had an artificially high ->csum_bytes, so we need to free
6189 * the remainder. If bytes is the same or less then we don't
6190 * need to do anything, the other free-ers did the correct
6191 * thing.
6192 */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006193 inode->csum_bytes = orig_csum_bytes - num_bytes;
Josef Bacikf4881bc2013-03-25 16:03:35 -04006194 if (bytes > to_free)
6195 to_free = bytes - to_free;
6196 else
6197 to_free = 0;
6198 }
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006199 spin_unlock(&inode->lock);
Dongsheng Yange2d1f922015-02-06 10:26:52 -05006200 if (dropped)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006201 to_free += btrfs_calc_trans_metadata_size(fs_info, dropped);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006202
6203 if (to_free) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006204 btrfs_block_rsv_release(fs_info, block_rsv, to_free);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006205 trace_btrfs_space_reservation(fs_info, "delalloc",
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006206 btrfs_ino(inode), to_free, 0);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006207 }
6208 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006209 mutex_unlock(&inode->delalloc_mutex);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006210 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006211}
6212
Josef Bacik7709cde2011-08-04 10:25:02 -04006213/**
6214 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
6215 * @inode: the inode to release the reservation for
6216 * @num_bytes: the number of bytes we're releasing
6217 *
6218 * This will release the metadata reservation for an inode. This can be called
6219 * once we complete IO for a given set of bytes to release their metadata
6220 * reservations.
6221 */
Nikolay Borisov691fa052017-02-20 13:50:42 +02006222void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006223{
Nikolay Borisov691fa052017-02-20 13:50:42 +02006224 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006225 u64 to_free = 0;
6226 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006227
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006228 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006229 spin_lock(&inode->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05006230 dropped = drop_outstanding_extent(inode, num_bytes);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006231
Miao Xie09348562013-02-07 10:12:07 +00006232 if (num_bytes)
6233 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006234 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006235 if (dropped > 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006236 to_free += btrfs_calc_trans_metadata_size(fs_info, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006237
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006238 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04006239 return;
6240
Nikolay Borisov691fa052017-02-20 13:50:42 +02006241 trace_btrfs_space_reservation(fs_info, "delalloc", btrfs_ino(inode),
6242 to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02006243
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006244 btrfs_block_rsv_release(fs_info, &fs_info->delalloc_block_rsv, to_free);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006245}
6246
Josef Bacik7709cde2011-08-04 10:25:02 -04006247/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006248 * btrfs_delalloc_reserve_space - reserve data and metadata space for
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006249 * delalloc
6250 * @inode: inode we're writing to
6251 * @start: start range we are writing to
6252 * @len: how long the range we are writing to
Qu Wenruo364ecf32017-02-27 15:10:38 +08006253 * @reserved: mandatory parameter, record actually reserved qgroup ranges of
6254 * current reservation.
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006255 *
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006256 * This will do the following things
6257 *
6258 * o reserve space in data space info for num bytes
6259 * and reserve precious corresponding qgroup space
6260 * (Done in check_data_free_space)
6261 *
6262 * o reserve space for metadata space, based on the number of outstanding
6263 * extents and how much csums will be needed
6264 * also reserve metadata space in a per root over-reserve method.
6265 * o add to the inodes->delalloc_bytes
6266 * o add it to the fs_info's delalloc inodes list.
6267 * (Above 3 all done in delalloc_reserve_metadata)
6268 *
6269 * Return 0 for success
6270 * Return <0 for error(-ENOSPC or -EQUOT)
6271 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08006272int btrfs_delalloc_reserve_space(struct inode *inode,
6273 struct extent_changeset **reserved, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006274{
6275 int ret;
6276
Qu Wenruo364ecf32017-02-27 15:10:38 +08006277 ret = btrfs_check_data_free_space(inode, reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006278 if (ret < 0)
6279 return ret;
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006280 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006281 if (ret < 0)
Qu Wenruobc42bda2017-02-27 15:10:39 +08006282 btrfs_free_reserved_data_space(inode, *reserved, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006283 return ret;
6284}
6285
6286/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006287 * btrfs_delalloc_release_space - release data and metadata space for delalloc
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006288 * @inode: inode we're releasing space for
6289 * @start: start position of the space already reserved
6290 * @len: the len of the space already reserved
6291 *
6292 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
6293 * called in the case that we don't need the metadata AND data reservations
6294 * anymore. So if there is an error or we insert an inline extent.
6295 *
6296 * This function will release the metadata space that was not used and will
6297 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
6298 * list if there are no delalloc bytes left.
6299 * Also it will handle the qgroup reserved space.
6300 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08006301void btrfs_delalloc_release_space(struct inode *inode,
6302 struct extent_changeset *reserved, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006303{
Nikolay Borisov691fa052017-02-20 13:50:42 +02006304 btrfs_delalloc_release_metadata(BTRFS_I(inode), len);
Qu Wenruobc42bda2017-02-27 15:10:39 +08006305 btrfs_free_reserved_data_space(inode, reserved, start, len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006306}
6307
Josef Bacikce93ec52014-11-17 15:45:48 -05006308static int update_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6202df62016-06-22 18:54:22 -04006309 struct btrfs_fs_info *info, u64 bytenr,
Josef Bacikce93ec52014-11-17 15:45:48 -05006310 u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04006311{
Josef Bacik0af3d002010-06-21 14:48:16 -04006312 struct btrfs_block_group_cache *cache = NULL;
Chris Masondb945352007-10-15 16:15:53 -04006313 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006314 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04006315 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04006316 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04006317
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006318 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00006319 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02006320 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006321 if (alloc)
6322 old_val += num_bytes;
6323 else
6324 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02006325 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00006326 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006327
Chris Masond3977122009-01-05 21:25:51 -05006328 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04006329 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006330 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006331 return -ENOENT;
Yan, Zhengb742bb822010-05-16 10:46:24 -04006332 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
6333 BTRFS_BLOCK_GROUP_RAID1 |
6334 BTRFS_BLOCK_GROUP_RAID10))
6335 factor = 2;
6336 else
6337 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04006338 /*
6339 * If this block group has free space cache written out, we
6340 * need to make sure to load it if we are removing space. This
6341 * is because we need the unpinning stage to actually add the
6342 * space back to the block group, otherwise we will leak space.
6343 */
6344 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00006345 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04006346
Chris Masondb945352007-10-15 16:15:53 -04006347 byte_in_group = bytenr - cache->key.objectid;
6348 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04006349
Josef Bacik25179202008-10-29 14:49:05 -04006350 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006351 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04006352
Jeff Mahoney6202df62016-06-22 18:54:22 -04006353 if (btrfs_test_opt(info, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04006354 cache->disk_cache_state < BTRFS_DC_CLEAR)
6355 cache->disk_cache_state = BTRFS_DC_CLEAR;
6356
Chris Mason9078a3e2007-04-26 16:46:15 -04006357 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04006358 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04006359 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04006360 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006361 btrfs_set_block_group_used(&cache->item, old_val);
6362 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006363 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb822010-05-16 10:46:24 -04006364 cache->space_info->bytes_used += num_bytes;
6365 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04006366 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006367 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04006368 } else {
Chris Masondb945352007-10-15 16:15:53 -04006369 old_val -= num_bytes;
Filipe Mananaae0ab002014-11-26 15:28:52 +00006370 btrfs_set_block_group_used(&cache->item, old_val);
6371 cache->pinned += num_bytes;
6372 cache->space_info->bytes_pinned += num_bytes;
6373 cache->space_info->bytes_used -= num_bytes;
6374 cache->space_info->disk_used -= num_bytes * factor;
6375 spin_unlock(&cache->lock);
6376 spin_unlock(&cache->space_info->lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04006377
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006378 trace_btrfs_space_reservation(info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006379 cache->space_info->flags,
6380 num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07006381 percpu_counter_add(&cache->space_info->total_bytes_pinned,
6382 num_bytes);
Filipe Mananaae0ab002014-11-26 15:28:52 +00006383 set_extent_dirty(info->pinned_extents,
6384 bytenr, bytenr + num_bytes - 1,
6385 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04006386 }
Chris Mason1bbc6212015-04-06 12:46:08 -07006387
6388 spin_lock(&trans->transaction->dirty_bgs_lock);
6389 if (list_empty(&cache->dirty_list)) {
6390 list_add_tail(&cache->dirty_list,
6391 &trans->transaction->dirty_bgs);
6392 trans->transaction->num_dirty_bgs++;
6393 btrfs_get_block_group(cache);
6394 }
6395 spin_unlock(&trans->transaction->dirty_bgs_lock);
6396
Filipe Manana036a9342015-11-23 15:25:16 +00006397 /*
6398 * No longer have used bytes in this block group, queue it for
6399 * deletion. We do this after adding the block group to the
6400 * dirty list to avoid races between cleaner kthread and space
6401 * cache writeout.
6402 */
6403 if (!alloc && old_val == 0) {
6404 spin_lock(&info->unused_bgs_lock);
6405 if (list_empty(&cache->bg_list)) {
6406 btrfs_get_block_group(cache);
6407 list_add_tail(&cache->bg_list,
6408 &info->unused_bgs);
6409 }
6410 spin_unlock(&info->unused_bgs_lock);
6411 }
6412
Chris Masonfa9c0d792009-04-03 09:47:43 -04006413 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04006414 total -= num_bytes;
6415 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006416 }
6417 return 0;
6418}
Chris Mason6324fbf2008-03-24 15:01:59 -04006419
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006420static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04006421{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006422 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05006423 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006424
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006425 spin_lock(&fs_info->block_group_cache_lock);
6426 bytenr = fs_info->first_logical_byte;
6427 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00006428
6429 if (bytenr < (u64)-1)
6430 return bytenr;
6431
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006432 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006433 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04006434 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006435
Yan Zhengd2fb3432008-12-11 16:30:39 -05006436 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006437 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006438
6439 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04006440}
6441
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006442static int pin_down_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006443 struct btrfs_block_group_cache *cache,
6444 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05006445{
Yan Zheng11833d62009-09-11 16:11:19 -04006446 spin_lock(&cache->space_info->lock);
6447 spin_lock(&cache->lock);
6448 cache->pinned += num_bytes;
6449 cache->space_info->bytes_pinned += num_bytes;
6450 if (reserved) {
6451 cache->reserved -= num_bytes;
6452 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05006453 }
Yan Zheng11833d62009-09-11 16:11:19 -04006454 spin_unlock(&cache->lock);
6455 spin_unlock(&cache->space_info->lock);
6456
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006457 trace_btrfs_space_reservation(fs_info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006458 cache->space_info->flags, num_bytes, 1);
Omar Sandoval4da8b762017-06-06 16:45:28 -07006459 percpu_counter_add(&cache->space_info->total_bytes_pinned, num_bytes);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006460 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006461 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05006462 return 0;
6463}
Chris Mason9078a3e2007-04-26 16:46:15 -04006464
Yan, Zhengf0486c62010-05-16 10:46:25 -04006465/*
6466 * this function must be called within transaction
6467 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006468int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006469 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04006470{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006471 struct btrfs_block_group_cache *cache;
6472
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006473 cache = btrfs_lookup_block_group(fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006474 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006475
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006476 pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006477
6478 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04006479 return 0;
6480}
Zheng Yane8569812008-09-26 10:05:48 -04006481
Yan, Zhengf0486c62010-05-16 10:46:25 -04006482/*
Chris Masone688b7252011-10-31 20:52:39 -04006483 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04006484 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006485int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04006486 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006487{
Chris Masone688b7252011-10-31 20:52:39 -04006488 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04006489 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04006490
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006491 cache = btrfs_lookup_block_group(fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006492 if (!cache)
6493 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04006494
6495 /*
6496 * pull in the free space cache (if any) so that our pin
6497 * removes the free space from the cache. We have load_only set
6498 * to one because the slow code to read in the free extents does check
6499 * the pinned extents.
6500 */
Liu Bof6373bf2012-12-27 09:01:18 +00006501 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04006502
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006503 pin_down_extent(fs_info, cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04006504
6505 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04006506 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04006507 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006508 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04006509}
6510
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006511static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
6512 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006513{
6514 int ret;
6515 struct btrfs_block_group_cache *block_group;
6516 struct btrfs_caching_control *caching_ctl;
6517
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006518 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006519 if (!block_group)
6520 return -EINVAL;
6521
6522 cache_block_group(block_group, 0);
6523 caching_ctl = get_caching_control(block_group);
6524
6525 if (!caching_ctl) {
6526 /* Logic error */
6527 BUG_ON(!block_group_cache_done(block_group));
6528 ret = btrfs_remove_free_space(block_group, start, num_bytes);
6529 } else {
6530 mutex_lock(&caching_ctl->mutex);
6531
6532 if (start >= caching_ctl->progress) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006533 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006534 } else if (start + num_bytes <= caching_ctl->progress) {
6535 ret = btrfs_remove_free_space(block_group,
6536 start, num_bytes);
6537 } else {
6538 num_bytes = caching_ctl->progress - start;
6539 ret = btrfs_remove_free_space(block_group,
6540 start, num_bytes);
6541 if (ret)
6542 goto out_lock;
6543
6544 num_bytes = (start + num_bytes) -
6545 caching_ctl->progress;
6546 start = caching_ctl->progress;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006547 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006548 }
6549out_lock:
6550 mutex_unlock(&caching_ctl->mutex);
6551 put_caching_control(caching_ctl);
6552 }
6553 btrfs_put_block_group(block_group);
6554 return ret;
6555}
6556
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006557int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info,
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006558 struct extent_buffer *eb)
6559{
6560 struct btrfs_file_extent_item *item;
6561 struct btrfs_key key;
6562 int found_type;
6563 int i;
6564
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006565 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006566 return 0;
6567
6568 for (i = 0; i < btrfs_header_nritems(eb); i++) {
6569 btrfs_item_key_to_cpu(eb, &key, i);
6570 if (key.type != BTRFS_EXTENT_DATA_KEY)
6571 continue;
6572 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
6573 found_type = btrfs_file_extent_type(eb, item);
6574 if (found_type == BTRFS_FILE_EXTENT_INLINE)
6575 continue;
6576 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
6577 continue;
6578 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
6579 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006580 __exclude_logged_extent(fs_info, key.objectid, key.offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006581 }
6582
6583 return 0;
6584}
6585
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006586static void
6587btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg)
6588{
6589 atomic_inc(&bg->reservations);
6590}
6591
6592void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
6593 const u64 start)
6594{
6595 struct btrfs_block_group_cache *bg;
6596
6597 bg = btrfs_lookup_block_group(fs_info, start);
6598 ASSERT(bg);
6599 if (atomic_dec_and_test(&bg->reservations))
6600 wake_up_atomic_t(&bg->reservations);
6601 btrfs_put_block_group(bg);
6602}
6603
6604static int btrfs_wait_bg_reservations_atomic_t(atomic_t *a)
6605{
6606 schedule();
6607 return 0;
6608}
6609
6610void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6611{
6612 struct btrfs_space_info *space_info = bg->space_info;
6613
6614 ASSERT(bg->ro);
6615
6616 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
6617 return;
6618
6619 /*
6620 * Our block group is read only but before we set it to read only,
6621 * some task might have had allocated an extent from it already, but it
6622 * has not yet created a respective ordered extent (and added it to a
6623 * root's list of ordered extents).
6624 * Therefore wait for any task currently allocating extents, since the
6625 * block group's reservations counter is incremented while a read lock
6626 * on the groups' semaphore is held and decremented after releasing
6627 * the read access on that semaphore and creating the ordered extent.
6628 */
6629 down_write(&space_info->groups_sem);
6630 up_write(&space_info->groups_sem);
6631
6632 wait_on_atomic_t(&bg->reservations,
6633 btrfs_wait_bg_reservations_atomic_t,
6634 TASK_UNINTERRUPTIBLE);
6635}
6636
Josef Bacikfb25e912011-07-26 17:00:46 -04006637/**
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006638 * btrfs_add_reserved_bytes - update the block_group and space info counters
Josef Bacikfb25e912011-07-26 17:00:46 -04006639 * @cache: The cache we are manipulating
Wang Xiaoguang18513092016-07-25 15:51:40 +08006640 * @ram_bytes: The number of bytes of file content, and will be same to
6641 * @num_bytes except for the compress path.
Josef Bacikfb25e912011-07-26 17:00:46 -04006642 * @num_bytes: The number of bytes in question
Miao Xiee570fd22014-06-19 10:42:50 +08006643 * @delalloc: The blocks are allocated for the delalloc write
Josef Bacikfb25e912011-07-26 17:00:46 -04006644 *
Xiaoguang Wang745699e2016-09-23 12:38:50 +08006645 * This is called by the allocator when it reserves space. If this is a
6646 * reservation and the block group has become read only we cannot make the
6647 * reservation and return -EAGAIN, otherwise this function always succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04006648 */
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006649static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
Wang Xiaoguang18513092016-07-25 15:51:40 +08006650 u64 ram_bytes, u64 num_bytes, int delalloc)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006651{
Josef Bacikfb25e912011-07-26 17:00:46 -04006652 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006653 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006654
Josef Bacikfb25e912011-07-26 17:00:46 -04006655 spin_lock(&space_info->lock);
6656 spin_lock(&cache->lock);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006657 if (cache->ro) {
6658 ret = -EAGAIN;
Josef Bacikfb25e912011-07-26 17:00:46 -04006659 } else {
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006660 cache->reserved += num_bytes;
6661 space_info->bytes_reserved += num_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006662
Wang Xiaoguang18513092016-07-25 15:51:40 +08006663 trace_btrfs_space_reservation(cache->fs_info,
6664 "space_info", space_info->flags,
6665 ram_bytes, 0);
6666 space_info->bytes_may_use -= ram_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006667 if (delalloc)
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006668 cache->delalloc_bytes += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006669 }
Josef Bacikfb25e912011-07-26 17:00:46 -04006670 spin_unlock(&cache->lock);
6671 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006672 return ret;
6673}
6674
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006675/**
6676 * btrfs_free_reserved_bytes - update the block_group and space info counters
6677 * @cache: The cache we are manipulating
6678 * @num_bytes: The number of bytes in question
6679 * @delalloc: The blocks are allocated for the delalloc write
6680 *
6681 * This is called by somebody who is freeing space that was never actually used
6682 * on disk. For example if you reserve some space for a new leaf in transaction
6683 * A and before transaction A commits you free that leaf, you call this with
6684 * reserve set to 0 in order to clear the reservation.
6685 */
6686
6687static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
6688 u64 num_bytes, int delalloc)
6689{
6690 struct btrfs_space_info *space_info = cache->space_info;
6691 int ret = 0;
6692
6693 spin_lock(&space_info->lock);
6694 spin_lock(&cache->lock);
6695 if (cache->ro)
6696 space_info->bytes_readonly += num_bytes;
6697 cache->reserved -= num_bytes;
6698 space_info->bytes_reserved -= num_bytes;
6699
6700 if (delalloc)
6701 cache->delalloc_bytes -= num_bytes;
6702 spin_unlock(&cache->lock);
6703 spin_unlock(&space_info->lock);
6704 return ret;
6705}
David Sterba8b74c032017-02-10 19:20:56 +01006706void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04006707{
Yan Zheng11833d62009-09-11 16:11:19 -04006708 struct btrfs_caching_control *next;
6709 struct btrfs_caching_control *caching_ctl;
6710 struct btrfs_block_group_cache *cache;
6711
Josef Bacik9e351cc2014-03-13 15:42:13 -04006712 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04006713
6714 list_for_each_entry_safe(caching_ctl, next,
6715 &fs_info->caching_block_groups, list) {
6716 cache = caching_ctl->block_group;
6717 if (block_group_cache_done(cache)) {
6718 cache->last_byte_to_unpin = (u64)-1;
6719 list_del_init(&caching_ctl->list);
6720 put_caching_control(caching_ctl);
6721 } else {
6722 cache->last_byte_to_unpin = caching_ctl->progress;
6723 }
6724 }
6725
6726 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6727 fs_info->pinned_extents = &fs_info->freed_extents[1];
6728 else
6729 fs_info->pinned_extents = &fs_info->freed_extents[0];
6730
Josef Bacik9e351cc2014-03-13 15:42:13 -04006731 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006732
6733 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04006734}
6735
Josef Bacikc759c4e2015-10-02 15:25:10 -04006736/*
6737 * Returns the free cluster for the given space info and sets empty_cluster to
6738 * what it should be based on the mount options.
6739 */
6740static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006741fetch_cluster_info(struct btrfs_fs_info *fs_info,
6742 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04006743{
6744 struct btrfs_free_cluster *ret = NULL;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006745
6746 *empty_cluster = 0;
6747 if (btrfs_mixed_space_info(space_info))
6748 return ret;
6749
Josef Bacikc759c4e2015-10-02 15:25:10 -04006750 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006751 ret = &fs_info->meta_alloc_cluster;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006752 if (btrfs_test_opt(fs_info, SSD))
6753 *empty_cluster = SZ_2M;
6754 else
Byongho Leeee221842015-12-15 01:42:10 +09006755 *empty_cluster = SZ_64K;
Hans van Kranenburg583b7232017-07-28 08:31:28 +02006756 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
6757 btrfs_test_opt(fs_info, SSD_SPREAD)) {
6758 *empty_cluster = SZ_2M;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006759 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006760 }
6761
6762 return ret;
6763}
6764
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006765static int unpin_extent_range(struct btrfs_fs_info *fs_info,
6766 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00006767 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04006768{
Yan Zheng11833d62009-09-11 16:11:19 -04006769 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04006770 struct btrfs_space_info *space_info;
6771 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006772 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04006773 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006774 u64 total_unpinned = 0;
6775 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04006776 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04006777
6778 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04006779 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04006780 if (!cache ||
6781 start >= cache->key.objectid + cache->key.offset) {
6782 if (cache)
6783 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04006784 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006785 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006786 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04006787
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006788 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04006789 cache->space_info,
6790 &empty_cluster);
6791 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04006792 }
6793
6794 len = cache->key.objectid + cache->key.offset - start;
6795 len = min(len, end + 1 - start);
6796
6797 if (start < cache->last_byte_to_unpin) {
6798 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00006799 if (return_free_space)
6800 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04006801 }
Josef Bacik25179202008-10-29 14:49:05 -04006802
Yan, Zhengf0486c62010-05-16 10:46:25 -04006803 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006804 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006805 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006806
Josef Bacikc759c4e2015-10-02 15:25:10 -04006807 /*
6808 * If this space cluster has been marked as fragmented and we've
6809 * unpinned enough in this block group to potentially allow a
6810 * cluster to be created inside of it go ahead and clear the
6811 * fragmented check.
6812 */
6813 if (cluster && cluster->fragmented &&
6814 total_unpinned > empty_cluster) {
6815 spin_lock(&cluster->lock);
6816 cluster->fragmented = 0;
6817 spin_unlock(&cluster->lock);
6818 }
6819
Josef Bacik7b398f82012-10-22 15:52:28 -04006820 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006821 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006822 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006823 space_info->bytes_pinned -= len;
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006824
6825 trace_btrfs_space_reservation(fs_info, "pinned",
6826 space_info->flags, len, 0);
Josef Bacik4f4db212015-09-29 11:40:47 -04006827 space_info->max_extent_size = 0;
Liu Bod288db52014-07-02 16:58:01 +08006828 percpu_counter_add(&space_info->total_bytes_pinned, -len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006829 if (cache->ro) {
6830 space_info->bytes_readonly += len;
6831 readonly = true;
6832 }
Josef Bacik25179202008-10-29 14:49:05 -04006833 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006834 if (!readonly && return_free_space &&
6835 global_rsv->space_info == space_info) {
6836 u64 to_add = len;
Nikolay Borisov92ac58e2017-08-17 10:52:28 +03006837
Josef Bacik7b398f82012-10-22 15:52:28 -04006838 spin_lock(&global_rsv->lock);
6839 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04006840 to_add = min(len, global_rsv->size -
6841 global_rsv->reserved);
6842 global_rsv->reserved += to_add;
6843 space_info->bytes_may_use += to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006844 if (global_rsv->reserved >= global_rsv->size)
6845 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04006846 trace_btrfs_space_reservation(fs_info,
6847 "space_info",
6848 space_info->flags,
6849 to_add, 1);
6850 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006851 }
6852 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006853 /* Add to any tickets we may have */
6854 if (len)
6855 space_info_add_new_bytes(fs_info, space_info,
6856 len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006857 }
6858 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006859 }
6860
6861 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006862 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04006863 return 0;
6864}
6865
6866int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006867 struct btrfs_fs_info *fs_info)
Chris Masona28ec192007-03-06 20:08:01 -05006868{
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006869 struct btrfs_block_group_cache *block_group, *tmp;
6870 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04006871 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04006872 u64 start;
6873 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05006874 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05006875
Yan Zheng11833d62009-09-11 16:11:19 -04006876 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6877 unpin = &fs_info->freed_extents[1];
6878 else
6879 unpin = &fs_info->freed_extents[0];
6880
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006881 while (!trans->aborted) {
Filipe Mananad4b450c2015-01-29 19:18:25 +00006882 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04006883 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04006884 EXTENT_DIRTY, NULL);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006885 if (ret) {
6886 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05006887 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00006888 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006889
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006890 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006891 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00006892 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006893
David Sterbaaf6f8f62016-04-26 23:54:39 +02006894 clear_extent_dirty(unpin, start, end);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006895 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006896 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masonb9473432009-03-13 11:00:37 -04006897 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05006898 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006899
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006900 /*
6901 * Transaction is finished. We don't need the lock anymore. We
6902 * do need to clean up the block groups in case of a transaction
6903 * abort.
6904 */
6905 deleted_bgs = &trans->transaction->deleted_bgs;
6906 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
6907 u64 trimmed = 0;
6908
6909 ret = -EROFS;
6910 if (!trans->aborted)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006911 ret = btrfs_discard_extent(fs_info,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006912 block_group->key.objectid,
6913 block_group->key.offset,
6914 &trimmed);
6915
6916 list_del_init(&block_group->bg_list);
6917 btrfs_put_block_group_trimming(block_group);
6918 btrfs_put_block_group(block_group);
6919
6920 if (ret) {
6921 const char *errstr = btrfs_decode_error(ret);
6922 btrfs_warn(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02006923 "discard failed while removing blockgroup: errno=%d %s",
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006924 ret, errstr);
6925 }
6926 }
6927
Chris Masone20d96d2007-03-22 12:13:20 -04006928 return 0;
6929}
6930
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006931static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006932 struct btrfs_fs_info *info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006933 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006934 u64 root_objectid, u64 owner_objectid,
6935 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006936 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05006937{
Chris Masone2fa7222007-03-12 16:22:34 -04006938 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006939 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04006940 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04006941 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006942 struct btrfs_extent_item *ei;
6943 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05006944 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006945 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05006946 int extent_slot = 0;
6947 int found_extent = 0;
6948 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006949 u32 item_size;
6950 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006951 u64 bytenr = node->bytenr;
6952 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07006953 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006954 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05006955
Chris Mason5caf2a02007-04-02 11:20:42 -04006956 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04006957 if (!path)
6958 return -ENOMEM;
6959
David Sterbae4058b52015-11-27 16:31:35 +01006960 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04006961 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006962
6963 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
6964 BUG_ON(!is_data && refs_to_drop != 1);
6965
Josef Bacik3173a182013-03-07 14:22:04 -05006966 if (is_data)
Thomas Meyer897ca812017-10-07 16:02:21 +02006967 skinny_metadata = false;
Josef Bacik3173a182013-03-07 14:22:04 -05006968
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05006969 ret = lookup_extent_backref(trans, info, path, &iref,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006970 bytenr, num_bytes, parent,
6971 root_objectid, owner_objectid,
6972 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05006973 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05006974 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006975 while (extent_slot >= 0) {
6976 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05006977 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006978 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05006979 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006980 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
6981 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05006982 found_extent = 1;
6983 break;
6984 }
Josef Bacik3173a182013-03-07 14:22:04 -05006985 if (key.type == BTRFS_METADATA_ITEM_KEY &&
6986 key.offset == owner_objectid) {
6987 found_extent = 1;
6988 break;
6989 }
Chris Mason952fcca2008-02-18 16:33:44 -05006990 if (path->slots[0] - extent_slot > 5)
6991 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006992 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05006993 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006994#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6995 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
6996 if (found_extent && item_size < sizeof(*ei))
6997 found_extent = 0;
6998#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04006999 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007000 BUG_ON(iref);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05007001 ret = remove_extent_backref(trans, info, path, NULL,
7002 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07007003 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06007004 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007005 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007006 goto out;
7007 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007008 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04007009 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007010
7011 key.objectid = bytenr;
7012 key.type = BTRFS_EXTENT_ITEM_KEY;
7013 key.offset = num_bytes;
7014
Josef Bacik3173a182013-03-07 14:22:04 -05007015 if (!is_data && skinny_metadata) {
7016 key.type = BTRFS_METADATA_ITEM_KEY;
7017 key.offset = owner_objectid;
7018 }
7019
Zheng Yan31840ae2008-09-23 13:14:14 -04007020 ret = btrfs_search_slot(trans, extent_root,
7021 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05007022 if (ret > 0 && skinny_metadata && path->slots[0]) {
7023 /*
7024 * Couldn't find our skinny metadata item,
7025 * see if we have ye olde extent item.
7026 */
7027 path->slots[0]--;
7028 btrfs_item_key_to_cpu(path->nodes[0], &key,
7029 path->slots[0]);
7030 if (key.objectid == bytenr &&
7031 key.type == BTRFS_EXTENT_ITEM_KEY &&
7032 key.offset == num_bytes)
7033 ret = 0;
7034 }
7035
7036 if (ret > 0 && skinny_metadata) {
7037 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01007038 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05007039 key.type = BTRFS_EXTENT_ITEM_KEY;
7040 key.offset = num_bytes;
7041 btrfs_release_path(path);
7042 ret = btrfs_search_slot(trans, extent_root,
7043 &key, path, -1, 1);
7044 }
7045
Josef Bacikf3465ca2008-11-12 14:19:50 -05007046 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007047 btrfs_err(info,
7048 "umm, got %d back from search, was looking for %llu",
7049 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00007050 if (ret > 0)
David Sterbaa4f78752017-06-29 18:37:49 +02007051 btrfs_print_leaf(path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05007052 }
David Sterba005d6422012-09-18 07:52:32 -06007053 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007054 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007055 goto out;
7056 }
Zheng Yan31840ae2008-09-23 13:14:14 -04007057 extent_slot = path->slots[0];
7058 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05307059 } else if (WARN_ON(ret == -ENOENT)) {
David Sterbaa4f78752017-06-29 18:37:49 +02007060 btrfs_print_leaf(path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007061 btrfs_err(info,
7062 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007063 bytenr, parent, root_objectid, owner_objectid,
7064 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04007065 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04007066 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007067 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04007068 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007069 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05007070 }
Chris Mason5f39d392007-10-15 16:14:19 -04007071
7072 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007073 item_size = btrfs_item_size_nr(leaf, extent_slot);
7074#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
7075 if (item_size < sizeof(*ei)) {
7076 BUG_ON(found_extent || extent_slot != path->slots[0]);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05007077 ret = convert_extent_item_v0(trans, info, path, owner_objectid,
7078 0);
David Sterba005d6422012-09-18 07:52:32 -06007079 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007080 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007081 goto out;
7082 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007083
David Sterbab3b4aa72011-04-21 01:20:15 +02007084 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007085 path->leave_spinning = 1;
7086
7087 key.objectid = bytenr;
7088 key.type = BTRFS_EXTENT_ITEM_KEY;
7089 key.offset = num_bytes;
7090
7091 ret = btrfs_search_slot(trans, extent_root, &key, path,
7092 -1, 1);
7093 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007094 btrfs_err(info,
7095 "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007096 ret, bytenr);
David Sterbaa4f78752017-06-29 18:37:49 +02007097 btrfs_print_leaf(path->nodes[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007098 }
David Sterba005d6422012-09-18 07:52:32 -06007099 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007100 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007101 goto out;
7102 }
7103
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007104 extent_slot = path->slots[0];
7105 leaf = path->nodes[0];
7106 item_size = btrfs_item_size_nr(leaf, extent_slot);
7107 }
7108#endif
7109 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05007110 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04007111 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05007112 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
7113 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007114 struct btrfs_tree_block_info *bi;
7115 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
7116 bi = (struct btrfs_tree_block_info *)(ei + 1);
7117 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05007118 }
7119
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007120 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04007121 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007122 btrfs_err(info,
7123 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
7124 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04007125 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04007126 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04007127 goto out;
7128 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007129 refs -= refs_to_drop;
7130
7131 if (refs > 0) {
7132 if (extent_op)
7133 __run_delayed_extent_op(extent_op, leaf, ei);
7134 /*
7135 * In the case of inline back ref, reference count will
7136 * be updated by remove_extent_backref
7137 */
7138 if (iref) {
7139 BUG_ON(!found_extent);
7140 } else {
7141 btrfs_set_extent_refs(leaf, ei, refs);
7142 btrfs_mark_buffer_dirty(leaf);
7143 }
7144 if (found_extent) {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05007145 ret = remove_extent_backref(trans, info, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007146 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07007147 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06007148 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007149 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007150 goto out;
7151 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007152 }
7153 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007154 if (found_extent) {
7155 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08007156 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007157 if (iref) {
7158 BUG_ON(path->slots[0] != extent_slot);
7159 } else {
7160 BUG_ON(path->slots[0] != extent_slot + 1);
7161 path->slots[0] = extent_slot;
7162 num_to_del = 2;
7163 }
Chris Mason78fae272007-03-25 11:35:08 -04007164 }
Chris Masonb9473432009-03-13 11:00:37 -04007165
Josef Bacikfcebe452014-05-13 17:30:47 -07007166 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05007167 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
7168 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06007169 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007170 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007171 goto out;
7172 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007173 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01007174
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007175 if (is_data) {
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007176 ret = btrfs_del_csums(trans, info, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06007177 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007178 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007179 goto out;
7180 }
Chris Mason459931e2008-12-10 09:10:46 -05007181 }
7182
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007183 ret = add_to_free_space_tree(trans, info, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007184 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007185 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007186 goto out;
7187 }
7188
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007189 ret = update_block_group(trans, info, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06007190 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007191 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007192 goto out;
7193 }
Chris Masona28ec192007-03-06 20:08:01 -05007194 }
Josef Bacikfcebe452014-05-13 17:30:47 -07007195 btrfs_release_path(path);
7196
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007197out:
Chris Mason5caf2a02007-04-02 11:20:42 -04007198 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05007199 return ret;
7200}
7201
7202/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007203 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04007204 * delayed ref for that extent as well. This searches the delayed ref tree for
7205 * a given extent, and if there are no other delayed refs to be processed, it
7206 * removes it from the tree.
7207 */
7208static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007209 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04007210{
7211 struct btrfs_delayed_ref_head *head;
7212 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007213 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04007214
7215 delayed_refs = &trans->transaction->delayed_refs;
7216 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08007217 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04007218 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08007219 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04007220
Josef Bacikd7df2c72014-01-23 09:21:38 -05007221 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08007222 if (!list_empty(&head->ref_list))
Chris Mason1887be62009-03-13 10:11:24 -04007223 goto out;
7224
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007225 if (head->extent_op) {
7226 if (!head->must_insert_reserved)
7227 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00007228 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007229 head->extent_op = NULL;
7230 }
7231
Chris Mason1887be62009-03-13 10:11:24 -04007232 /*
7233 * waiting for the lock here would deadlock. If someone else has it
7234 * locked they are already in the process of dropping it anyway
7235 */
7236 if (!mutex_trylock(&head->mutex))
7237 goto out;
7238
7239 /*
7240 * at this point we have a head with no other entries. Go
7241 * ahead and process it.
7242 */
7243 head->node.in_tree = 0;
Liu Boc46effa2013-10-14 12:59:45 +08007244 rb_erase(&head->href_node, &delayed_refs->href_root);
Chris Masonc3e69d52009-03-13 10:17:05 -04007245
Josef Bacikd7df2c72014-01-23 09:21:38 -05007246 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04007247
7248 /*
7249 * we don't take a ref on the node because we're removing it from the
7250 * tree, so we just steal the ref the tree was holding.
7251 */
Chris Masonc3e69d52009-03-13 10:17:05 -04007252 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007253 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04007254 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007255 head->processing = 0;
7256 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04007257 spin_unlock(&delayed_refs->lock);
7258
Yan, Zhengf0486c62010-05-16 10:46:25 -04007259 BUG_ON(head->extent_op);
7260 if (head->must_insert_reserved)
7261 ret = 1;
7262
7263 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04007264 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007265 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04007266out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05007267 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08007268
7269out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04007270 spin_unlock(&delayed_refs->lock);
7271 return 0;
7272}
7273
Yan, Zhengf0486c62010-05-16 10:46:25 -04007274void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7275 struct btrfs_root *root,
7276 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02007277 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007278{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007279 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb150a4f2013-06-19 15:00:04 -04007280 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007281 int ret;
7282
7283 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Omar Sandovald7eae342017-06-06 16:45:31 -07007284 int old_ref_mod, new_ref_mod;
7285
Josef Bacikfd708b82017-09-29 15:43:50 -04007286 btrfs_ref_tree_mod(root, buf->start, buf->len, parent,
7287 root->root_key.objectid,
7288 btrfs_header_level(buf), 0,
7289 BTRFS_DROP_DELAYED_REF);
Omar Sandoval7be07912017-06-06 16:45:30 -07007290 ret = btrfs_add_delayed_tree_ref(fs_info, trans, buf->start,
7291 buf->len, parent,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007292 root->root_key.objectid,
7293 btrfs_header_level(buf),
Omar Sandoval7be07912017-06-06 16:45:30 -07007294 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007295 &old_ref_mod, &new_ref_mod);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007296 BUG_ON(ret); /* -ENOMEM */
Omar Sandovald7eae342017-06-06 16:45:31 -07007297 pin = old_ref_mod >= 0 && new_ref_mod < 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007298 }
7299
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007300 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
Filipe Manana62198722015-01-06 20:18:45 +00007301 struct btrfs_block_group_cache *cache;
7302
Yan, Zhengf0486c62010-05-16 10:46:25 -04007303 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007304 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007305 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04007306 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007307 }
7308
Omar Sandoval4da8b762017-06-06 16:45:28 -07007309 pin = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007310 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00007311
Yan, Zhengf0486c62010-05-16 10:46:25 -04007312 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007313 pin_down_extent(fs_info, cache, buf->start,
7314 buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00007315 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04007316 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007317 }
7318
7319 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
7320
7321 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08007322 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00007323 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007324 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007325 }
7326out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04007327 if (pin)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007328 add_pinned_bytes(fs_info, buf->len, btrfs_header_level(buf),
Josef Bacikb150a4f2013-06-19 15:00:04 -04007329 root->root_key.objectid);
7330
Omar Sandoval0a16c7d2017-06-06 16:45:29 -07007331 if (last_ref) {
7332 /*
7333 * Deleting the buffer, clear the corrupt flag since it doesn't
7334 * matter anymore.
7335 */
7336 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
7337 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007338}
7339
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007340/* Can return -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007341int btrfs_free_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007342 struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007343 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01007344 u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04007345{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04007346 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovald7eae342017-06-06 16:45:31 -07007347 int old_ref_mod, new_ref_mod;
Chris Mason925baed2008-06-25 16:01:30 -04007348 int ret;
7349
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04007350 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007351 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02007352
Josef Bacikfd708b82017-09-29 15:43:50 -04007353 if (root_objectid != BTRFS_TREE_LOG_OBJECTID)
7354 btrfs_ref_tree_mod(root, bytenr, num_bytes, parent,
7355 root_objectid, owner, offset,
7356 BTRFS_DROP_DELAYED_REF);
7357
Chris Mason56bec292009-03-13 10:10:06 -04007358 /*
7359 * tree log blocks never actually go into the extent allocation
7360 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04007361 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007362 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
7363 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04007364 /* unlocks the pinned mutex */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007365 btrfs_pin_extent(fs_info, bytenr, num_bytes, 1);
Omar Sandovald7eae342017-06-06 16:45:31 -07007366 old_ref_mod = new_ref_mod = 0;
Chris Mason56bec292009-03-13 10:10:06 -04007367 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007368 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007369 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007370 num_bytes, parent,
7371 root_objectid, (int)owner,
7372 BTRFS_DROP_DELAYED_REF, NULL,
Omar Sandovald7eae342017-06-06 16:45:31 -07007373 &old_ref_mod, &new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007374 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007375 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
Omar Sandoval7be07912017-06-06 16:45:30 -07007376 num_bytes, parent,
7377 root_objectid, owner, offset,
7378 0, BTRFS_DROP_DELAYED_REF,
Omar Sandovald7eae342017-06-06 16:45:31 -07007379 &old_ref_mod, &new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -04007380 }
Omar Sandovald7eae342017-06-06 16:45:31 -07007381
7382 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
7383 add_pinned_bytes(fs_info, num_bytes, owner, root_objectid);
7384
Chris Mason925baed2008-06-25 16:01:30 -04007385 return ret;
7386}
7387
Chris Masonfec577f2007-02-26 10:40:21 -05007388/*
Josef Bacik817d52f2009-07-13 21:29:25 -04007389 * when we wait for progress in the block group caching, its because
7390 * our allocation attempt failed at least once. So, we must sleep
7391 * and let some progress happen before we try again.
7392 *
7393 * This function will sleep at least once waiting for new free space to
7394 * show up, and then it will check the block group free space numbers
7395 * for our min num_bytes. Another option is to have it go ahead
7396 * and look in the rbtree for a free extent of a given size, but this
7397 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04007398 *
7399 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
7400 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04007401 */
Josef Bacik36cce922013-08-05 11:15:21 -04007402static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04007403wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
7404 u64 num_bytes)
7405{
Yan Zheng11833d62009-09-11 16:11:19 -04007406 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04007407
Yan Zheng11833d62009-09-11 16:11:19 -04007408 caching_ctl = get_caching_control(cache);
7409 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007410 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04007411
Yan Zheng11833d62009-09-11 16:11:19 -04007412 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08007413 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04007414
7415 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04007416}
7417
7418static noinline int
7419wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
7420{
7421 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04007422 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007423
7424 caching_ctl = get_caching_control(cache);
7425 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007426 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007427
7428 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04007429 if (cache->cached == BTRFS_CACHE_ERROR)
7430 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04007431 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04007432 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04007433}
7434
Liu Bo31e50222012-11-21 14:18:10 +00007435int __get_raid_index(u64 flags)
Yan, Zhengb742bb822010-05-16 10:46:24 -04007436{
Ilya Dryomov7738a532012-03-27 17:09:17 +03007437 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00007438 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007439 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00007440 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007441 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00007442 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007443 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00007444 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05007445 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05007446 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05007447 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05007448 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007449
Chris Masone942f882013-02-20 14:06:05 -05007450 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb822010-05-16 10:46:24 -04007451}
7452
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007453int get_block_group_index(struct btrfs_block_group_cache *cache)
Ilya Dryomov7738a532012-03-27 17:09:17 +03007454{
Liu Bo31e50222012-11-21 14:18:10 +00007455 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03007456}
7457
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007458static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
7459 [BTRFS_RAID_RAID10] = "raid10",
7460 [BTRFS_RAID_RAID1] = "raid1",
7461 [BTRFS_RAID_DUP] = "dup",
7462 [BTRFS_RAID_RAID0] = "raid0",
7463 [BTRFS_RAID_SINGLE] = "single",
7464 [BTRFS_RAID_RAID5] = "raid5",
7465 [BTRFS_RAID_RAID6] = "raid6",
7466};
7467
Jeff Mahoney1b8e5df2013-11-20 16:50:23 -05007468static const char *get_raid_name(enum btrfs_raid_types type)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007469{
7470 if (type >= BTRFS_NR_RAID_TYPES)
7471 return NULL;
7472
7473 return btrfs_raid_type_names[type];
7474}
7475
Josef Bacik817d52f2009-07-13 21:29:25 -04007476enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05007477 LOOP_CACHING_NOWAIT = 0,
7478 LOOP_CACHING_WAIT = 1,
7479 LOOP_ALLOC_CHUNK = 2,
7480 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04007481};
7482
Miao Xiee570fd22014-06-19 10:42:50 +08007483static inline void
7484btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
7485 int delalloc)
7486{
7487 if (delalloc)
7488 down_read(&cache->data_rwsem);
7489}
7490
7491static inline void
7492btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
7493 int delalloc)
7494{
7495 btrfs_get_block_group(cache);
7496 if (delalloc)
7497 down_read(&cache->data_rwsem);
7498}
7499
7500static struct btrfs_block_group_cache *
7501btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
7502 struct btrfs_free_cluster *cluster,
7503 int delalloc)
7504{
Sudip Mukherjee89771cc2016-02-16 13:32:47 +05307505 struct btrfs_block_group_cache *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007506
Miao Xiee570fd22014-06-19 10:42:50 +08007507 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007508 while (1) {
7509 used_bg = cluster->block_group;
7510 if (!used_bg)
7511 return NULL;
7512
7513 if (used_bg == block_group)
7514 return used_bg;
7515
7516 btrfs_get_block_group(used_bg);
7517
7518 if (!delalloc)
7519 return used_bg;
7520
7521 if (down_read_trylock(&used_bg->data_rwsem))
7522 return used_bg;
7523
7524 spin_unlock(&cluster->refill_lock);
7525
Liu Boe321f8a2016-11-30 16:11:04 -08007526 /* We should only have one-level nested. */
7527 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007528
7529 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007530 if (used_bg == cluster->block_group)
7531 return used_bg;
7532
7533 up_read(&used_bg->data_rwsem);
7534 btrfs_put_block_group(used_bg);
7535 }
Miao Xiee570fd22014-06-19 10:42:50 +08007536}
7537
7538static inline void
7539btrfs_release_block_group(struct btrfs_block_group_cache *cache,
7540 int delalloc)
7541{
7542 if (delalloc)
7543 up_read(&cache->data_rwsem);
7544 btrfs_put_block_group(cache);
7545}
7546
Josef Bacik817d52f2009-07-13 21:29:25 -04007547/*
Chris Masonfec577f2007-02-26 10:40:21 -05007548 * walks the btree of allocated extents and find a hole of a given size.
7549 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08007550 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04007551 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08007552 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05007553 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08007554 *
7555 * If there is no suitable free space, we will record the max size of
7556 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05007557 */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05007558static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08007559 u64 ram_bytes, u64 num_bytes, u64 empty_size,
7560 u64 hint_byte, struct btrfs_key *ins,
7561 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007562{
Josef Bacik80eb2342008-10-29 14:49:05 -04007563 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007564 struct btrfs_root *root = fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007565 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04007566 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05007567 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08007568 u64 max_extent_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007569 u64 empty_cluster = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04007570 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007571 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00007572 int index = __get_raid_index(flags);
Josef Bacik0a243252009-09-11 16:11:20 -04007573 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007574 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04007575 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08007576 bool have_caching_bg = false;
chandan13a0db52015-11-02 13:59:46 +05307577 bool orig_have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007578 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05007579
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007580 WARN_ON(num_bytes < fs_info->sectorsize);
David Sterba962a2982014-06-04 18:41:45 +02007581 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04007582 ins->objectid = 0;
7583 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04007584
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007585 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05007586
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007587 space_info = __find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007588 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007589 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007590 return -ENOSPC;
7591 }
Josef Bacik2552d172009-04-03 10:14:19 -04007592
Josef Bacik67377732010-09-16 16:19:09 -04007593 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04007594 * If our free space is heavily fragmented we may not be able to make
7595 * big contiguous allocations, so instead of doing the expensive search
7596 * for free space, simply return ENOSPC with our max_extent_size so we
7597 * can go ahead and search for a more manageable chunk.
7598 *
7599 * If our max_extent_size is large enough for our allocation simply
7600 * disable clustering since we will likely not be able to find enough
7601 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04007602 */
Josef Bacik4f4db212015-09-29 11:40:47 -04007603 if (unlikely(space_info->max_extent_size)) {
7604 spin_lock(&space_info->lock);
7605 if (space_info->max_extent_size &&
7606 num_bytes > space_info->max_extent_size) {
7607 ins->offset = space_info->max_extent_size;
7608 spin_unlock(&space_info->lock);
7609 return -ENOSPC;
7610 } else if (space_info->max_extent_size) {
7611 use_cluster = false;
7612 }
7613 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007614 }
7615
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007616 last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04007617 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007618 spin_lock(&last_ptr->lock);
7619 if (last_ptr->block_group)
7620 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007621 if (last_ptr->fragmented) {
7622 /*
7623 * We still set window_start so we can keep track of the
7624 * last place we found an allocation to try and save
7625 * some time.
7626 */
7627 hint_byte = last_ptr->window_start;
7628 use_cluster = false;
7629 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007630 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007631 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007632
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007633 search_start = max(search_start, first_logical_byte(fs_info, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04007634 search_start = max(search_start, hint_byte);
Josef Bacik2552d172009-04-03 10:14:19 -04007635 if (search_start == hint_byte) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007636 block_group = btrfs_lookup_block_group(fs_info, search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04007637 /*
7638 * we don't want to use the block group if it doesn't match our
7639 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05007640 *
7641 * However if we are re-searching with an ideal block group
7642 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04007643 */
David Sterbab6919a52013-04-29 13:39:40 +00007644 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05007645 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04007646 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007647 if (list_empty(&block_group->list) ||
7648 block_group->ro) {
7649 /*
7650 * someone is removing this block group,
7651 * we can't jump into the have_block_group
7652 * target because our list pointers are not
7653 * valid
7654 */
7655 btrfs_put_block_group(block_group);
7656 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05007657 } else {
Yan, Zhengb742bb822010-05-16 10:46:24 -04007658 index = get_block_group_index(block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007659 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04007660 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05007661 }
Josef Bacik2552d172009-04-03 10:14:19 -04007662 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007663 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04007664 }
Chris Mason42e70e72008-11-07 18:17:11 -05007665 }
Josef Bacik2552d172009-04-03 10:14:19 -04007666search:
Miao Xie60d2adb2011-09-09 17:34:35 +08007667 have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007668 if (index == 0 || index == __get_raid_index(flags))
7669 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04007670 down_read(&space_info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007671 list_for_each_entry(block_group, &space_info->block_groups[index],
7672 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04007673 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04007674 int cached;
Chris Mason8a1413a22008-11-10 16:13:54 -05007675
Jeff Mahoney14443932017-07-19 23:25:51 -04007676 /* If the block group is read-only, we can skip it entirely. */
7677 if (unlikely(block_group->ro))
7678 continue;
7679
Miao Xiee570fd22014-06-19 10:42:50 +08007680 btrfs_grab_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007681 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05007682
Chris Mason83a50de2010-12-13 15:06:46 -05007683 /*
7684 * this can happen if we end up cycling through all the
7685 * raid types, but we want to make sure we only allocate
7686 * for the proper type.
7687 */
David Sterbab6919a52013-04-29 13:39:40 +00007688 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05007689 u64 extra = BTRFS_BLOCK_GROUP_DUP |
7690 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007691 BTRFS_BLOCK_GROUP_RAID5 |
7692 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05007693 BTRFS_BLOCK_GROUP_RAID10;
7694
7695 /*
7696 * if they asked for extra copies and this block group
7697 * doesn't provide them, bail. This does allow us to
7698 * fill raid0 from raid1.
7699 */
David Sterbab6919a52013-04-29 13:39:40 +00007700 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05007701 goto loop;
7702 }
7703
Josef Bacik2552d172009-04-03 10:14:19 -04007704have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05007705 cached = block_group_cache_done(block_group);
7706 if (unlikely(!cached)) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007707 have_caching_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00007708 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04007709 BUG_ON(ret < 0);
7710 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05007711 }
7712
Josef Bacik36cce922013-08-05 11:15:21 -04007713 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
7714 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007715
Josef Bacik0a243252009-09-11 16:11:20 -04007716 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007717 * Ok we want to try and use the cluster allocator, so
7718 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04007719 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04007720 if (last_ptr && use_cluster) {
Miao Xie215a63d2014-01-15 20:00:56 +08007721 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05007722 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007723 /*
7724 * the refill lock keeps out other
7725 * people trying to start a new cluster
7726 */
Miao Xiee570fd22014-06-19 10:42:50 +08007727 used_block_group = btrfs_lock_cluster(block_group,
7728 last_ptr,
7729 delalloc);
7730 if (!used_block_group)
Chris Mason44fb5512009-06-04 15:34:51 -04007731 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04007732
Miao Xiee570fd22014-06-19 10:42:50 +08007733 if (used_block_group != block_group &&
7734 (used_block_group->ro ||
7735 !block_group_bits(used_block_group, flags)))
7736 goto release_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007737
7738 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007739 last_ptr,
7740 num_bytes,
7741 used_block_group->key.objectid,
7742 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007743 if (offset) {
7744 /* we have a block, we're done */
7745 spin_unlock(&last_ptr->refill_lock);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007746 trace_btrfs_reserve_extent_cluster(fs_info,
Miao Xie89d43462014-01-15 20:00:57 +08007747 used_block_group,
7748 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08007749 if (used_block_group != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08007750 btrfs_release_block_group(block_group,
7751 delalloc);
Miao Xie215a63d2014-01-15 20:00:56 +08007752 block_group = used_block_group;
7753 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007754 goto checks;
7755 }
7756
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007757 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007758release_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007759 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7760 * set up a new clusters, so lets just skip it
7761 * and let the allocator find whatever block
7762 * it can find. If we reach this point, we
7763 * will have tried the cluster allocator
7764 * plenty of times and not have found
7765 * anything, so we are likely way too
7766 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007767 * anything.
7768 *
7769 * However, if the cluster is taken from the
7770 * current block group, release the cluster
7771 * first, so that we stand a better chance of
7772 * succeeding in the unclustered
7773 * allocation. */
7774 if (loop >= LOOP_NO_EMPTY_SIZE &&
Miao Xiee570fd22014-06-19 10:42:50 +08007775 used_block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007776 spin_unlock(&last_ptr->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007777 btrfs_release_block_group(used_block_group,
7778 delalloc);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007779 goto unclustered_alloc;
7780 }
7781
Chris Masonfa9c0d792009-04-03 09:47:43 -04007782 /*
7783 * this cluster didn't work out, free it and
7784 * start over
7785 */
7786 btrfs_return_cluster_to_free_space(NULL, last_ptr);
7787
Miao Xiee570fd22014-06-19 10:42:50 +08007788 if (used_block_group != block_group)
7789 btrfs_release_block_group(used_block_group,
7790 delalloc);
7791refill_cluster:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007792 if (loop >= LOOP_NO_EMPTY_SIZE) {
7793 spin_unlock(&last_ptr->refill_lock);
7794 goto unclustered_alloc;
7795 }
7796
Chris Mason8de972b2013-01-04 15:39:43 -05007797 aligned_cluster = max_t(unsigned long,
7798 empty_cluster + empty_size,
7799 block_group->full_stripe_len);
7800
Chris Masonfa9c0d792009-04-03 09:47:43 -04007801 /* allocate a cluster in this block group */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007802 ret = btrfs_find_space_cluster(fs_info, block_group,
Josef Bacik00361582013-08-14 14:02:47 -04007803 last_ptr, search_start,
7804 num_bytes,
7805 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007806 if (ret == 0) {
7807 /*
7808 * now pull our allocation out of this
7809 * cluster
7810 */
7811 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007812 last_ptr,
7813 num_bytes,
7814 search_start,
7815 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007816 if (offset) {
7817 /* we found one, proceed */
7818 spin_unlock(&last_ptr->refill_lock);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007819 trace_btrfs_reserve_extent_cluster(fs_info,
Josef Bacik3f7de032011-11-10 08:29:20 -05007820 block_group, search_start,
7821 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007822 goto checks;
7823 }
Josef Bacik0a243252009-09-11 16:11:20 -04007824 } else if (!cached && loop > LOOP_CACHING_NOWAIT
7825 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007826 spin_unlock(&last_ptr->refill_lock);
7827
Josef Bacik0a243252009-09-11 16:11:20 -04007828 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007829 wait_block_group_cache_progress(block_group,
7830 num_bytes + empty_cluster + empty_size);
7831 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007832 }
Josef Bacik817d52f2009-07-13 21:29:25 -04007833
Chris Masonfa9c0d792009-04-03 09:47:43 -04007834 /*
7835 * at this point we either didn't find a cluster
7836 * or we weren't able to allocate a block from our
7837 * cluster. Free the cluster we've been trying
7838 * to use, and go to the next block group
7839 */
Josef Bacik0a243252009-09-11 16:11:20 -04007840 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007841 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04007842 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007843 }
7844
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007845unclustered_alloc:
Josef Bacikc759c4e2015-10-02 15:25:10 -04007846 /*
7847 * We are doing an unclustered alloc, set the fragmented flag so
7848 * we don't bother trying to setup a cluster again until we get
7849 * more space.
7850 */
7851 if (unlikely(last_ptr)) {
7852 spin_lock(&last_ptr->lock);
7853 last_ptr->fragmented = 1;
7854 spin_unlock(&last_ptr->lock);
7855 }
Liu Bo0c9b36e2017-02-13 15:42:30 -08007856 if (cached) {
7857 struct btrfs_free_space_ctl *ctl =
7858 block_group->free_space_ctl;
7859
7860 spin_lock(&ctl->tree_lock);
7861 if (ctl->free_space <
7862 num_bytes + empty_cluster + empty_size) {
7863 if (ctl->free_space > max_extent_size)
7864 max_extent_size = ctl->free_space;
7865 spin_unlock(&ctl->tree_lock);
7866 goto loop;
7867 }
7868 spin_unlock(&ctl->tree_lock);
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007869 }
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007870
Josef Bacik6226cb02009-04-03 10:14:18 -04007871 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08007872 num_bytes, empty_size,
7873 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007874 /*
7875 * If we didn't find a chunk, and we haven't failed on this
7876 * block group before, and this block group is in the middle of
7877 * caching and we are ok with waiting, then go ahead and wait
7878 * for progress to be made, and set failed_alloc to true.
7879 *
7880 * If failed_alloc is true then we've already waited on this
7881 * block group once and should move on to the next block group.
7882 */
7883 if (!offset && !failed_alloc && !cached &&
7884 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007885 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007886 num_bytes + empty_size);
7887 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007888 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007889 } else if (!offset) {
7890 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04007891 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007892checks:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007893 search_start = ALIGN(offset, fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04007894
Josef Bacik2552d172009-04-03 10:14:19 -04007895 /* move on to the next group */
7896 if (search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08007897 block_group->key.objectid + block_group->key.offset) {
7898 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04007899 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04007900 }
Josef Bacik80eb2342008-10-29 14:49:05 -04007901
Josef Bacik6226cb02009-04-03 10:14:18 -04007902 if (offset < search_start)
Miao Xie215a63d2014-01-15 20:00:56 +08007903 btrfs_add_free_space(block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04007904 search_start - offset);
7905 BUG_ON(offset > search_start);
7906
Wang Xiaoguang18513092016-07-25 15:51:40 +08007907 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
7908 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007909 if (ret == -EAGAIN) {
Miao Xie215a63d2014-01-15 20:00:56 +08007910 btrfs_add_free_space(block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007911 goto loop;
7912 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007913 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04007914
Josef Bacik2552d172009-04-03 10:14:19 -04007915 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007916 ins->objectid = search_start;
7917 ins->offset = num_bytes;
7918
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007919 trace_btrfs_reserve_extent(fs_info, block_group,
Josef Bacik3f7de032011-11-10 08:29:20 -05007920 search_start, num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08007921 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007922 break;
7923loop:
Josef Bacik0a243252009-09-11 16:11:20 -04007924 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007925 failed_alloc = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04007926 BUG_ON(index != get_block_group_index(block_group));
Miao Xiee570fd22014-06-19 10:42:50 +08007927 btrfs_release_block_group(block_group, delalloc);
Jeff Mahoney14443932017-07-19 23:25:51 -04007928 cond_resched();
Josef Bacik2552d172009-04-03 10:14:19 -04007929 }
7930 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05007931
chandan13a0db52015-11-02 13:59:46 +05307932 if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg
7933 && !orig_have_caching_bg)
7934 orig_have_caching_bg = true;
7935
Miao Xie60d2adb2011-09-09 17:34:35 +08007936 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
7937 goto search;
7938
Yan, Zhengb742bb822010-05-16 10:46:24 -04007939 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
7940 goto search;
7941
Josef Bacik285ff5a2012-01-13 15:27:45 -05007942 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05007943 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
7944 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04007945 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
7946 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
7947 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
7948 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04007949 */
Josef Bacik723bda22011-05-27 16:11:38 -04007950 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb822010-05-16 10:46:24 -04007951 index = 0;
Josef Bacika5e681d2015-10-01 14:54:10 -04007952 if (loop == LOOP_CACHING_NOWAIT) {
7953 /*
7954 * We want to skip the LOOP_CACHING_WAIT step if we
Nicholas D Steeves01327612016-05-19 21:18:45 -04007955 * don't have any uncached bgs and we've already done a
Josef Bacika5e681d2015-10-01 14:54:10 -04007956 * full search through.
7957 */
chandan13a0db52015-11-02 13:59:46 +05307958 if (orig_have_caching_bg || !full_search)
Josef Bacika5e681d2015-10-01 14:54:10 -04007959 loop = LOOP_CACHING_WAIT;
7960 else
7961 loop = LOOP_ALLOC_CHUNK;
7962 } else {
7963 loop++;
7964 }
7965
Josef Bacik817d52f2009-07-13 21:29:25 -04007966 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04007967 struct btrfs_trans_handle *trans;
Wang Shilongf017f152014-03-13 13:19:47 +08007968 int exist = 0;
Josef Bacik00361582013-08-14 14:02:47 -04007969
Wang Shilongf017f152014-03-13 13:19:47 +08007970 trans = current->journal_info;
7971 if (trans)
7972 exist = 1;
7973 else
7974 trans = btrfs_join_transaction(root);
7975
Josef Bacik00361582013-08-14 14:02:47 -04007976 if (IS_ERR(trans)) {
7977 ret = PTR_ERR(trans);
7978 goto out;
7979 }
7980
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007981 ret = do_chunk_alloc(trans, fs_info, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04007982 CHUNK_ALLOC_FORCE);
Josef Bacika5e681d2015-10-01 14:54:10 -04007983
7984 /*
7985 * If we can't allocate a new chunk we've already looped
7986 * through at least once, move on to the NO_EMPTY_SIZE
7987 * case.
7988 */
7989 if (ret == -ENOSPC)
7990 loop = LOOP_NO_EMPTY_SIZE;
7991
Josef Bacikea658ba2012-09-11 16:57:25 -04007992 /*
7993 * Do not bail out on ENOSPC since we
7994 * can do more things.
7995 */
Josef Bacik00361582013-08-14 14:02:47 -04007996 if (ret < 0 && ret != -ENOSPC)
Jeff Mahoney66642832016-06-10 18:19:25 -04007997 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -04007998 else
7999 ret = 0;
Wang Shilongf017f152014-03-13 13:19:47 +08008000 if (!exist)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008001 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04008002 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04008003 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04008004 }
8005
8006 if (loop == LOOP_NO_EMPTY_SIZE) {
Josef Bacika5e681d2015-10-01 14:54:10 -04008007 /*
8008 * Don't loop again if we already have no empty_size and
8009 * no empty_cluster.
8010 */
8011 if (empty_size == 0 &&
8012 empty_cluster == 0) {
8013 ret = -ENOSPC;
8014 goto out;
8015 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04008016 empty_size = 0;
8017 empty_cluster = 0;
8018 }
Chris Mason42e70e72008-11-07 18:17:11 -05008019
Josef Bacik723bda22011-05-27 16:11:38 -04008020 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04008021 } else if (!ins->objectid) {
8022 ret = -ENOSPC;
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008023 } else if (ins->objectid) {
Josef Bacikc759c4e2015-10-02 15:25:10 -04008024 if (!use_cluster && last_ptr) {
8025 spin_lock(&last_ptr->lock);
8026 last_ptr->window_start = ins->objectid;
8027 spin_unlock(&last_ptr->lock);
8028 }
Josef Bacik2552d172009-04-03 10:14:19 -04008029 ret = 0;
8030 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008031out:
Josef Bacik4f4db212015-09-29 11:40:47 -04008032 if (ret == -ENOSPC) {
8033 spin_lock(&space_info->lock);
8034 space_info->max_extent_size = max_extent_size;
8035 spin_unlock(&space_info->lock);
Miao Xiea4820392013-09-09 13:19:42 +08008036 ins->offset = max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04008037 }
Chris Mason0f70abe2007-02-28 16:46:22 -05008038 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05008039}
Chris Masonec44a352008-04-28 15:29:52 -04008040
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008041static void dump_space_info(struct btrfs_fs_info *fs_info,
8042 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008043 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04008044{
8045 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb822010-05-16 10:46:24 -04008046 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008047
Josef Bacik9ed74f22009-09-11 16:12:44 -04008048 spin_lock(&info->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008049 btrfs_info(fs_info, "space_info %llu has %llu free, is %sfull",
8050 info->flags,
Liu Bo41361352017-02-13 15:42:21 -08008051 info->total_bytes - btrfs_space_info_used(info, true),
8052 info->full ? "" : "not ");
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008053 btrfs_info(fs_info,
8054 "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu",
8055 info->total_bytes, info->bytes_used, info->bytes_pinned,
8056 info->bytes_reserved, info->bytes_may_use,
8057 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008058 spin_unlock(&info->lock);
8059
8060 if (!dump_block_groups)
8061 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008062
Josef Bacik80eb2342008-10-29 14:49:05 -04008063 down_read(&info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04008064again:
8065 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04008066 spin_lock(&cache->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008067 btrfs_info(fs_info,
8068 "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s",
8069 cache->key.objectid, cache->key.offset,
8070 btrfs_block_group_used(&cache->item), cache->pinned,
8071 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04008072 btrfs_dump_free_space(cache, bytes);
8073 spin_unlock(&cache->lock);
8074 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04008075 if (++index < BTRFS_NR_RAID_TYPES)
8076 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04008077 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008078}
Zheng Yane8569812008-09-26 10:05:48 -04008079
Wang Xiaoguang18513092016-07-25 15:51:40 +08008080int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04008081 u64 num_bytes, u64 min_alloc_size,
8082 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08008083 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05008084{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008085 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04008086 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00008087 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05008088 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04008089
Jeff Mahoney1b868262017-05-17 11:38:35 -04008090 flags = get_alloc_profile_by_root(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04008091again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008092 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05008093 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08008094 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008095 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008096 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01008097 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08008098 if (!final_tried && ins->offset) {
8099 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04008100 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008101 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05008102 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08008103 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05008104 if (num_bytes == min_alloc_size)
8105 final_tried = true;
8106 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008107 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05008108 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008109
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008110 sinfo = __find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008111 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008112 "allocation failed flags %llu, wanted %llu",
8113 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01008114 if (sinfo)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008115 dump_space_info(fs_info, sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05008116 }
Chris Mason925baed2008-06-25 16:01:30 -04008117 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008118
8119 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008120}
8121
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008122static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008123 u64 start, u64 len,
8124 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04008125{
Josef Bacik0f9dd462008-09-23 13:14:11 -04008126 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05008127 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008128
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008129 cache = btrfs_lookup_block_group(fs_info, start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008130 if (!cache) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008131 btrfs_err(fs_info, "Unable to find block group for %llu",
8132 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008133 return -ENOSPC;
8134 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05008135
Chris Masone688b7252011-10-31 20:52:39 -04008136 if (pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008137 pin_down_extent(fs_info, cache, start, len, 1);
Chris Masone688b7252011-10-31 20:52:39 -04008138 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008139 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008140 ret = btrfs_discard_extent(fs_info, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04008141 btrfs_add_free_space(cache, start, len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08008142 btrfs_free_reserved_bytes(cache, len, delalloc);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008143 trace_btrfs_reserved_extent_free(fs_info, start, len);
Chris Masone688b7252011-10-31 20:52:39 -04008144 }
Dongsheng Yang31193212014-12-12 16:44:35 +08008145
Chris Masonfa9c0d792009-04-03 09:47:43 -04008146 btrfs_put_block_group(cache);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008147 return ret;
8148}
8149
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008150int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008151 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04008152{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008153 return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04008154}
8155
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008156int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04008157 u64 start, u64 len)
8158{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008159 return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04008160}
8161
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008162static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008163 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008164 u64 parent, u64 root_objectid,
8165 u64 flags, u64 owner, u64 offset,
8166 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008167{
8168 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008169 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008170 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008171 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008172 struct extent_buffer *leaf;
8173 int type;
8174 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04008175
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008176 if (parent > 0)
8177 type = BTRFS_SHARED_DATA_REF_KEY;
8178 else
8179 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04008180
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008181 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05008182
8183 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00008184 if (!path)
8185 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05008186
Chris Masonb9473432009-03-13 11:00:37 -04008187 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008188 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8189 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008190 if (ret) {
8191 btrfs_free_path(path);
8192 return ret;
8193 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008194
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008195 leaf = path->nodes[0];
8196 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05008197 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008198 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
8199 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8200 btrfs_set_extent_flags(leaf, extent_item,
8201 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05008202
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008203 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
8204 btrfs_set_extent_inline_ref_type(leaf, iref, type);
8205 if (parent > 0) {
8206 struct btrfs_shared_data_ref *ref;
8207 ref = (struct btrfs_shared_data_ref *)(iref + 1);
8208 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8209 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
8210 } else {
8211 struct btrfs_extent_data_ref *ref;
8212 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
8213 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
8214 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
8215 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
8216 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
8217 }
Chris Mason47e4bb92008-02-01 14:51:59 -05008218
8219 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05008220 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04008221
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008222 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
8223 ins->offset);
8224 if (ret)
8225 return ret;
8226
Jeff Mahoney6202df62016-06-22 18:54:22 -04008227 ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008228 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008229 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008230 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05008231 BUG();
8232 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008233 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008234 return ret;
8235}
8236
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008237static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008238 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008239 u64 parent, u64 root_objectid,
8240 u64 flags, struct btrfs_disk_key *key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01008241 int level, struct btrfs_key *ins)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008242{
8243 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008244 struct btrfs_extent_item *extent_item;
8245 struct btrfs_tree_block_info *block_info;
8246 struct btrfs_extent_inline_ref *iref;
8247 struct btrfs_path *path;
8248 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05008249 u32 size = sizeof(*extent_item) + sizeof(*iref);
Josef Bacikfcebe452014-05-13 17:30:47 -07008250 u64 num_bytes = ins->offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008251 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05008252
8253 if (!skinny_metadata)
8254 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008255
8256 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04008257 if (!path) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008258 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008259 fs_info->nodesize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07008260 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04008261 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008262
8263 path->leave_spinning = 1;
8264 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8265 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008266 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07008267 btrfs_free_path(path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008268 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008269 fs_info->nodesize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008270 return ret;
8271 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008272
8273 leaf = path->nodes[0];
8274 extent_item = btrfs_item_ptr(leaf, path->slots[0],
8275 struct btrfs_extent_item);
8276 btrfs_set_extent_refs(leaf, extent_item, 1);
8277 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8278 btrfs_set_extent_flags(leaf, extent_item,
8279 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008280
Josef Bacik3173a182013-03-07 14:22:04 -05008281 if (skinny_metadata) {
8282 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008283 num_bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05008284 } else {
8285 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
8286 btrfs_set_tree_block_key(leaf, block_info, key);
8287 btrfs_set_tree_block_level(leaf, block_info, level);
8288 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
8289 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008290
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008291 if (parent > 0) {
8292 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
8293 btrfs_set_extent_inline_ref_type(leaf, iref,
8294 BTRFS_SHARED_BLOCK_REF_KEY);
8295 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8296 } else {
8297 btrfs_set_extent_inline_ref_type(leaf, iref,
8298 BTRFS_TREE_BLOCK_REF_KEY);
8299 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
8300 }
8301
8302 btrfs_mark_buffer_dirty(leaf);
8303 btrfs_free_path(path);
8304
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008305 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
8306 num_bytes);
8307 if (ret)
8308 return ret;
8309
Jeff Mahoney6202df62016-06-22 18:54:22 -04008310 ret = update_block_group(trans, fs_info, ins->objectid,
8311 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008312 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008313 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008314 ins->objectid, ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008315 BUG();
8316 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04008317
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008318 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008319 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008320 return ret;
8321}
8322
8323int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008324 struct btrfs_root *root, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08008325 u64 offset, u64 ram_bytes,
8326 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008327{
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008328 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008329 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04008330
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008331 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04008332
Josef Bacikfd708b82017-09-29 15:43:50 -04008333 btrfs_ref_tree_mod(root, ins->objectid, ins->offset, 0,
8334 root->root_key.objectid, owner, offset,
8335 BTRFS_ADD_DELAYED_EXTENT);
8336
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008337 ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid,
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008338 ins->offset, 0,
8339 root->root_key.objectid, owner,
Omar Sandoval7be07912017-06-06 16:45:30 -07008340 offset, ram_bytes,
8341 BTRFS_ADD_DELAYED_EXTENT, NULL, NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008342 return ret;
8343}
Chris Masone02119d2008-09-05 16:13:11 -04008344
8345/*
8346 * this is used by the tree logging recovery code. It records that
8347 * an extent has been allocated and makes sure to clear the free
8348 * space cache bits as well
8349 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008350int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008351 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008352 u64 root_objectid, u64 owner, u64 offset,
8353 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04008354{
8355 int ret;
8356 struct btrfs_block_group_cache *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008357 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008358
8359 /*
8360 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04008361 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008362 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008363 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008364 ret = __exclude_logged_extent(fs_info, ins->objectid,
8365 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008366 if (ret)
8367 return ret;
8368 }
Chris Masone02119d2008-09-05 16:13:11 -04008369
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008370 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008371 if (!block_group)
8372 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04008373
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008374 space_info = block_group->space_info;
8375 spin_lock(&space_info->lock);
8376 spin_lock(&block_group->lock);
8377 space_info->bytes_reserved += ins->offset;
8378 block_group->reserved += ins->offset;
8379 spin_unlock(&block_group->lock);
8380 spin_unlock(&space_info->lock);
8381
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008382 ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008383 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04008384 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04008385 return ret;
8386}
8387
Eric Sandeen48a3b632013-04-25 20:41:01 +00008388static struct extent_buffer *
8389btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
David Sterbafe864572014-06-15 02:28:42 +02008390 u64 bytenr, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04008391{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008392 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04008393 struct extent_buffer *buf;
8394
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008395 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008396 if (IS_ERR(buf))
8397 return buf;
8398
Chris Mason65b51a02008-08-01 15:11:20 -04008399 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04008400 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04008401 btrfs_tree_lock(buf);
David Sterba7c302b42017-02-10 18:47:57 +01008402 clean_tree_block(fs_info, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05008403 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008404
8405 btrfs_set_lock_blocking(buf);
David Sterba4db8c522015-12-03 13:06:46 +01008406 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008407
Chris Masond0c803c2008-09-11 16:17:57 -04008408 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01008409 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008410 /*
8411 * we allow two log transactions at a time, use different
8412 * EXENT bit to differentiate dirty pages.
8413 */
Filipe Manana656f30d2014-09-26 12:25:56 +01008414 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008415 set_extent_dirty(&root->dirty_log_pages, buf->start,
8416 buf->start + buf->len - 1, GFP_NOFS);
8417 else
8418 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02008419 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04008420 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01008421 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04008422 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
8423 buf->start + buf->len - 1, GFP_NOFS);
8424 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04008425 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05008426 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04008427 return buf;
8428}
8429
Yan, Zhengf0486c62010-05-16 10:46:25 -04008430static struct btrfs_block_rsv *
8431use_block_rsv(struct btrfs_trans_handle *trans,
8432 struct btrfs_root *root, u32 blocksize)
8433{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008434 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008435 struct btrfs_block_rsv *block_rsv;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008436 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008437 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00008438 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008439
8440 block_rsv = get_block_rsv(trans, root);
8441
Miao Xieb586b322013-05-13 13:55:10 +00008442 if (unlikely(block_rsv->size == 0))
8443 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00008444again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04008445 ret = block_rsv_use_bytes(block_rsv, blocksize);
8446 if (!ret)
8447 return block_rsv;
8448
Miao Xieb586b322013-05-13 13:55:10 +00008449 if (block_rsv->failfast)
8450 return ERR_PTR(ret);
8451
Miao Xied88033d2013-05-13 13:55:12 +00008452 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
8453 global_updated = true;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008454 update_global_block_rsv(fs_info);
Miao Xied88033d2013-05-13 13:55:12 +00008455 goto again;
8456 }
8457
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008458 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xieb586b322013-05-13 13:55:10 +00008459 static DEFINE_RATELIMIT_STATE(_rs,
8460 DEFAULT_RATELIMIT_INTERVAL * 10,
8461 /*DEFAULT_RATELIMIT_BURST*/ 1);
8462 if (__ratelimit(&_rs))
8463 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05008464 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00008465 }
8466try_reserve:
8467 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
8468 BTRFS_RESERVE_NO_FLUSH);
8469 if (!ret)
8470 return block_rsv;
8471 /*
8472 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00008473 * the global reserve if its space type is the same as the global
8474 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00008475 */
Miao Xie5881cfc2013-05-13 13:55:11 +00008476 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
8477 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00008478 ret = block_rsv_use_bytes(global_rsv, blocksize);
8479 if (!ret)
8480 return global_rsv;
8481 }
8482 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008483}
8484
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008485static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
8486 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008487{
8488 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008489 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008490}
8491
Chris Masonfec577f2007-02-26 10:40:21 -05008492/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04008493 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08008494 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05008495 */
David Sterba4d75f8a2014-06-15 01:54:12 +02008496struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08008497 struct btrfs_root *root,
8498 u64 parent, u64 root_objectid,
8499 const struct btrfs_disk_key *key,
8500 int level, u64 hint,
8501 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05008502{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008503 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04008504 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008505 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04008506 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008507 struct btrfs_delayed_extent_op *extent_op;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008508 u64 flags = 0;
8509 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008510 u32 blocksize = fs_info->nodesize;
8511 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008512
David Sterba05653ef2016-07-15 15:23:37 +02008513#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008514 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008515 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
David Sterbafe864572014-06-15 02:28:42 +02008516 level);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008517 if (!IS_ERR(buf))
8518 root->alloc_bytenr += blocksize;
8519 return buf;
8520 }
David Sterba05653ef2016-07-15 15:23:37 +02008521#endif
David Sterbafccb84c2014-09-29 23:53:21 +02008522
Yan, Zhengf0486c62010-05-16 10:46:25 -04008523 block_rsv = use_block_rsv(trans, root, blocksize);
8524 if (IS_ERR(block_rsv))
8525 return ERR_CAST(block_rsv);
8526
Wang Xiaoguang18513092016-07-25 15:51:40 +08008527 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08008528 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008529 if (ret)
8530 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05008531
David Sterbafe864572014-06-15 02:28:42 +02008532 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
Omar Sandoval67b78592015-02-24 02:47:04 -08008533 if (IS_ERR(buf)) {
8534 ret = PTR_ERR(buf);
8535 goto out_free_reserved;
8536 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008537
8538 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
8539 if (parent == 0)
8540 parent = ins.objectid;
8541 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8542 } else
8543 BUG_ON(parent > 0);
8544
8545 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00008546 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08008547 if (!extent_op) {
8548 ret = -ENOMEM;
8549 goto out_free_buf;
8550 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008551 if (key)
8552 memcpy(&extent_op->key, key, sizeof(extent_op->key));
8553 else
8554 memset(&extent_op->key, 0, sizeof(extent_op->key));
8555 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01008556 extent_op->update_key = skinny_metadata ? false : true;
8557 extent_op->update_flags = true;
8558 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04008559 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008560
Josef Bacikfd708b82017-09-29 15:43:50 -04008561 btrfs_ref_tree_mod(root, ins.objectid, ins.offset, parent,
8562 root_objectid, level, 0,
8563 BTRFS_ADD_DELAYED_EXTENT);
Omar Sandoval7be07912017-06-06 16:45:30 -07008564 ret = btrfs_add_delayed_tree_ref(fs_info, trans, ins.objectid,
8565 ins.offset, parent,
8566 root_objectid, level,
Omar Sandoval67b78592015-02-24 02:47:04 -08008567 BTRFS_ADD_DELAYED_EXTENT,
Omar Sandoval7be07912017-06-06 16:45:30 -07008568 extent_op, NULL, NULL);
Omar Sandoval67b78592015-02-24 02:47:04 -08008569 if (ret)
8570 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008571 }
Chris Masonfec577f2007-02-26 10:40:21 -05008572 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008573
8574out_free_delayed:
8575 btrfs_free_delayed_extent_op(extent_op);
8576out_free_buf:
8577 free_extent_buffer(buf);
8578out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008579 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008580out_unuse:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008581 unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08008582 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05008583}
Chris Masona28ec192007-03-06 20:08:01 -05008584
Yan Zheng2c47e6052009-06-27 21:07:35 -04008585struct walk_control {
8586 u64 refs[BTRFS_MAX_LEVEL];
8587 u64 flags[BTRFS_MAX_LEVEL];
8588 struct btrfs_key update_progress;
8589 int stage;
8590 int level;
8591 int shared_level;
8592 int update_ref;
8593 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008594 int reada_slot;
8595 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008596 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008597};
8598
8599#define DROP_REFERENCE 1
8600#define UPDATE_BACKREF 2
8601
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008602static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8603 struct btrfs_root *root,
8604 struct walk_control *wc,
8605 struct btrfs_path *path)
8606{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008607 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008608 u64 bytenr;
8609 u64 generation;
8610 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008611 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008612 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008613 struct btrfs_key key;
8614 struct extent_buffer *eb;
8615 int ret;
8616 int slot;
8617 int nread = 0;
8618
8619 if (path->slots[wc->level] < wc->reada_slot) {
8620 wc->reada_count = wc->reada_count * 2 / 3;
8621 wc->reada_count = max(wc->reada_count, 2);
8622 } else {
8623 wc->reada_count = wc->reada_count * 3 / 2;
8624 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008625 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008626 }
8627
8628 eb = path->nodes[wc->level];
8629 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008630
8631 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
8632 if (nread >= wc->reada_count)
8633 break;
8634
8635 cond_resched();
8636 bytenr = btrfs_node_blockptr(eb, slot);
8637 generation = btrfs_node_ptr_generation(eb, slot);
8638
8639 if (slot == path->slots[wc->level])
8640 goto reada;
8641
8642 if (wc->stage == UPDATE_BACKREF &&
8643 generation <= root->root_key.offset)
8644 continue;
8645
Yan, Zheng94fcca92009-10-09 09:25:16 -04008646 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008647 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05008648 wc->level - 1, 1, &refs,
8649 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008650 /* We don't care about errors in readahead. */
8651 if (ret < 0)
8652 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008653 BUG_ON(refs == 0);
8654
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008655 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008656 if (refs == 1)
8657 goto reada;
8658
Yan, Zheng94fcca92009-10-09 09:25:16 -04008659 if (wc->level == 1 &&
8660 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8661 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008662 if (!wc->update_ref ||
8663 generation <= root->root_key.offset)
8664 continue;
8665 btrfs_node_key_to_cpu(eb, &key, slot);
8666 ret = btrfs_comp_cpu_keys(&key,
8667 &wc->update_progress);
8668 if (ret < 0)
8669 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008670 } else {
8671 if (wc->level == 1 &&
8672 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8673 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008674 }
8675reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008676 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008677 nread++;
8678 }
8679 wc->reada_slot = slot;
8680}
8681
Chris Mason9aca1d52007-03-13 11:09:37 -04008682/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008683 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008684 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04008685 * when wc->stage == UPDATE_BACKREF, this function updates
8686 * back refs for pointers in the block.
8687 *
8688 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04008689 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008690static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8691 struct btrfs_root *root,
8692 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008693 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04008694{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008695 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008696 int level = wc->level;
8697 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04008698 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8699 int ret;
8700
8701 if (wc->stage == UPDATE_BACKREF &&
8702 btrfs_header_owner(eb) != root->root_key.objectid)
8703 return 1;
8704
8705 /*
8706 * when reference count of tree block is 1, it won't increase
8707 * again. once full backref flag is set, we never clear it.
8708 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04008709 if (lookup_info &&
8710 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
8711 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008712 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008713 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008714 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008715 &wc->refs[level],
8716 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008717 BUG_ON(ret == -ENOMEM);
8718 if (ret)
8719 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008720 BUG_ON(wc->refs[level] == 0);
8721 }
8722
Yan Zheng2c47e6052009-06-27 21:07:35 -04008723 if (wc->stage == DROP_REFERENCE) {
8724 if (wc->refs[level] > 1)
8725 return 1;
8726
8727 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04008728 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008729 path->locks[level] = 0;
8730 }
8731 return 0;
8732 }
8733
8734 /* wc->stage == UPDATE_BACKREF */
8735 if (!(wc->flags[level] & flag)) {
8736 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07008737 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008738 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07008739 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008740 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008741 ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04008742 eb->len, flag,
8743 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008744 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008745 wc->flags[level] |= flag;
8746 }
8747
8748 /*
8749 * the block is shared by multiple trees, so it's not good to
8750 * keep the tree lock
8751 */
8752 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04008753 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008754 path->locks[level] = 0;
8755 }
8756 return 0;
8757}
8758
8759/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008760 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008761 *
8762 * when wc->stage == DROP_REFERENCE, this function checks
8763 * reference count of the block pointed to. if the block
8764 * is shared and we need update back refs for the subtree
8765 * rooted at the block, this function changes wc->stage to
8766 * UPDATE_BACKREF. if the block is shared and there is no
8767 * need to update back, this function drops the reference
8768 * to the block.
8769 *
8770 * NOTE: return value 1 means we should stop walking down.
8771 */
8772static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8773 struct btrfs_root *root,
8774 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008775 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008776{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008777 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008778 u64 bytenr;
8779 u64 generation;
8780 u64 parent;
8781 u32 blocksize;
8782 struct btrfs_key key;
8783 struct extent_buffer *next;
8784 int level = wc->level;
8785 int reada = 0;
8786 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07008787 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008788
8789 generation = btrfs_node_ptr_generation(path->nodes[level],
8790 path->slots[level]);
8791 /*
8792 * if the lower level block was created before the snapshot
8793 * was created, we know there is no need to update back refs
8794 * for the subtree
8795 */
8796 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04008797 generation <= root->root_key.offset) {
8798 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008799 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008800 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008801
8802 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008803 blocksize = fs_info->nodesize;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008804
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008805 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008806 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008807 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008808 if (IS_ERR(next))
8809 return PTR_ERR(next);
8810
Josef Bacikb2aaaa32013-07-05 17:05:38 -04008811 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
8812 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008813 reada = 1;
8814 }
8815 btrfs_tree_lock(next);
8816 btrfs_set_lock_blocking(next);
8817
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008818 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008819 &wc->refs[level - 1],
8820 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02008821 if (ret < 0)
8822 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008823
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008824 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008825 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02008826 ret = -EIO;
8827 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008828 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008829 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008830
Yan, Zheng94fcca92009-10-09 09:25:16 -04008831 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008832 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07008833 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008834 if (level == 1 &&
8835 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8836 goto skip;
8837
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008838 if (!wc->update_ref ||
8839 generation <= root->root_key.offset)
8840 goto skip;
8841
8842 btrfs_node_key_to_cpu(path->nodes[level], &key,
8843 path->slots[level]);
8844 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
8845 if (ret < 0)
8846 goto skip;
8847
8848 wc->stage = UPDATE_BACKREF;
8849 wc->shared_level = level - 1;
8850 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008851 } else {
8852 if (level == 1 &&
8853 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8854 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008855 }
8856
Chris Masonb9fab912012-05-06 07:23:47 -04008857 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008858 btrfs_tree_unlock(next);
8859 free_extent_buffer(next);
8860 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008861 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008862 }
8863
8864 if (!next) {
8865 if (reada && level == 1)
8866 reada_walk_down(trans, root, wc, path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008867 next = read_tree_block(fs_info, bytenr, generation);
Liu Bo64c043d2015-05-25 17:30:15 +08008868 if (IS_ERR(next)) {
8869 return PTR_ERR(next);
8870 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04008871 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008872 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04008873 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008874 btrfs_tree_lock(next);
8875 btrfs_set_lock_blocking(next);
8876 }
8877
8878 level--;
Josef Bacik48672682016-09-23 13:23:28 +02008879 ASSERT(level == btrfs_header_level(next));
8880 if (level != btrfs_header_level(next)) {
8881 btrfs_err(root->fs_info, "mismatched level");
8882 ret = -EIO;
8883 goto out_unlock;
8884 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008885 path->nodes[level] = next;
8886 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008887 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008888 wc->level = level;
8889 if (wc->level == 1)
8890 wc->reada_slot = 0;
8891 return 0;
8892skip:
8893 wc->refs[level - 1] = 0;
8894 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008895 if (wc->stage == DROP_REFERENCE) {
8896 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
8897 parent = path->nodes[level]->start;
8898 } else {
Josef Bacik48672682016-09-23 13:23:28 +02008899 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04008900 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02008901 if (root->root_key.objectid !=
8902 btrfs_header_owner(path->nodes[level])) {
8903 btrfs_err(root->fs_info,
8904 "mismatched block owner");
8905 ret = -EIO;
8906 goto out_unlock;
8907 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008908 parent = 0;
8909 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008910
Mark Fasheh11526512014-07-17 12:39:01 -07008911 if (need_account) {
Qu Wenruo33d1f052016-10-18 09:31:28 +08008912 ret = btrfs_qgroup_trace_subtree(trans, root, next,
8913 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07008914 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008915 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008916 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
8917 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008918 }
8919 }
Josef Bacik84f7d8e2017-09-29 15:43:49 -04008920 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008921 parent, root->root_key.objectid,
8922 level - 1, 0);
Josef Bacik48672682016-09-23 13:23:28 +02008923 if (ret)
8924 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008925 }
Josef Bacik48672682016-09-23 13:23:28 +02008926
8927 *lookup_info = 1;
8928 ret = 1;
8929
8930out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008931 btrfs_tree_unlock(next);
8932 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02008933
8934 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008935}
8936
8937/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008938 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008939 *
8940 * when wc->stage == DROP_REFERENCE, this function drops
8941 * reference count on the block.
8942 *
8943 * when wc->stage == UPDATE_BACKREF, this function changes
8944 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8945 * to UPDATE_BACKREF previously while processing the block.
8946 *
8947 * NOTE: return value 1 means we should stop walking up.
8948 */
8949static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8950 struct btrfs_root *root,
8951 struct btrfs_path *path,
8952 struct walk_control *wc)
8953{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008954 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008955 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008956 int level = wc->level;
8957 struct extent_buffer *eb = path->nodes[level];
8958 u64 parent = 0;
8959
8960 if (wc->stage == UPDATE_BACKREF) {
8961 BUG_ON(wc->shared_level < level);
8962 if (level < wc->shared_level)
8963 goto out;
8964
Yan Zheng2c47e6052009-06-27 21:07:35 -04008965 ret = find_next_key(path, level + 1, &wc->update_progress);
8966 if (ret > 0)
8967 wc->update_ref = 0;
8968
8969 wc->stage = DROP_REFERENCE;
8970 wc->shared_level = -1;
8971 path->slots[level] = 0;
8972
8973 /*
8974 * check reference count again if the block isn't locked.
8975 * we should start walking down the tree again if reference
8976 * count is one.
8977 */
8978 if (!path->locks[level]) {
8979 BUG_ON(level == 0);
8980 btrfs_tree_lock(eb);
8981 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008982 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008983
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008984 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008985 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008986 &wc->refs[level],
8987 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008988 if (ret < 0) {
8989 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008990 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008991 return ret;
8992 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008993 BUG_ON(wc->refs[level] == 0);
8994 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04008995 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008996 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008997 return 1;
8998 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008999 }
9000 }
9001
9002 /* wc->stage == DROP_REFERENCE */
9003 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
9004
9005 if (wc->refs[level] == 1) {
9006 if (level == 0) {
9007 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07009008 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009009 else
Josef Bacike339a6b2014-07-02 10:54:25 -07009010 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009011 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009012 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb);
Mark Fasheh11526512014-07-17 12:39:01 -07009013 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009014 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009015 "error %d accounting leaf items. Quota is out of sync, rescan required.",
9016 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07009017 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009018 }
9019 /* make block locked assertion in clean_tree_block happy */
9020 if (!path->locks[level] &&
9021 btrfs_header_generation(eb) == trans->transid) {
9022 btrfs_tree_lock(eb);
9023 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04009024 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009025 }
David Sterba7c302b42017-02-10 18:47:57 +01009026 clean_tree_block(fs_info, eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009027 }
9028
9029 if (eb == root->node) {
9030 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
9031 parent = eb->start;
9032 else
9033 BUG_ON(root->root_key.objectid !=
9034 btrfs_header_owner(eb));
9035 } else {
9036 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
9037 parent = path->nodes[level + 1]->start;
9038 else
9039 BUG_ON(root->root_key.objectid !=
9040 btrfs_header_owner(path->nodes[level + 1]));
9041 }
9042
Jan Schmidt5581a512012-05-16 17:04:52 +02009043 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009044out:
9045 wc->refs[level] = 0;
9046 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009047 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009048}
9049
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009050static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
9051 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009052 struct btrfs_path *path,
9053 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04009054{
Yan Zheng2c47e6052009-06-27 21:07:35 -04009055 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04009056 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009057 int ret;
9058
Yan Zheng2c47e6052009-06-27 21:07:35 -04009059 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04009060 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009061 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04009062 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009063
Yan Zheng2c47e6052009-06-27 21:07:35 -04009064 if (level == 0)
9065 break;
9066
Yan, Zheng7a7965f2010-02-01 02:41:17 +00009067 if (path->slots[level] >=
9068 btrfs_header_nritems(path->nodes[level]))
9069 break;
9070
Yan, Zheng94fcca92009-10-09 09:25:16 -04009071 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009072 if (ret > 0) {
9073 path->slots[level]++;
9074 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00009075 } else if (ret < 0)
9076 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009077 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009078 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009079 return 0;
9080}
9081
Chris Masond3977122009-01-05 21:25:51 -05009082static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05009083 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04009084 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009085 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05009086{
Yan Zheng2c47e6052009-06-27 21:07:35 -04009087 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05009088 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04009089
Yan Zheng2c47e6052009-06-27 21:07:35 -04009090 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
9091 while (level < max_level && path->nodes[level]) {
9092 wc->level = level;
9093 if (path->slots[level] + 1 <
9094 btrfs_header_nritems(path->nodes[level])) {
9095 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05009096 return 0;
9097 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009098 ret = walk_up_proc(trans, root, path, wc);
9099 if (ret > 0)
9100 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05009101
Yan Zheng2c47e6052009-06-27 21:07:35 -04009102 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04009103 btrfs_tree_unlock_rw(path->nodes[level],
9104 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009105 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009106 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009107 free_extent_buffer(path->nodes[level]);
9108 path->nodes[level] = NULL;
9109 level++;
Chris Mason20524f02007-03-10 06:35:47 -05009110 }
9111 }
9112 return 1;
9113}
9114
Chris Mason9aca1d52007-03-13 11:09:37 -04009115/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04009116 * drop a subvolume tree.
9117 *
9118 * this function traverses the tree freeing any blocks that only
9119 * referenced by the tree.
9120 *
9121 * when a shared tree block is found. this function decreases its
9122 * reference count by one. if update_ref is true, this function
9123 * also make sure backrefs for the shared block and all lower level
9124 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00009125 *
9126 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04009127 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04009128int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009129 struct btrfs_block_rsv *block_rsv, int update_ref,
9130 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05009131{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009132 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04009133 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009134 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009135 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04009136 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009137 struct walk_control *wc;
9138 struct btrfs_key key;
9139 int err = 0;
9140 int ret;
9141 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04009142 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05009143
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009144 btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07009145
Chris Mason5caf2a02007-04-02 11:20:42 -04009146 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009147 if (!path) {
9148 err = -ENOMEM;
9149 goto out;
9150 }
Chris Mason20524f02007-03-10 06:35:47 -05009151
Yan Zheng2c47e6052009-06-27 21:07:35 -04009152 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07009153 if (!wc) {
9154 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009155 err = -ENOMEM;
9156 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07009157 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009158
Yan, Zhenga22285a2010-05-16 10:48:46 -04009159 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009160 if (IS_ERR(trans)) {
9161 err = PTR_ERR(trans);
9162 goto out_free;
9163 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00009164
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009165 if (block_rsv)
9166 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009167
Chris Mason9f3a7422007-08-07 15:52:19 -04009168 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009169 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009170 path->nodes[level] = btrfs_lock_root_node(root);
9171 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04009172 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009173 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009174 memset(&wc->update_progress, 0,
9175 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04009176 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04009177 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009178 memcpy(&wc->update_progress, &key,
9179 sizeof(wc->update_progress));
9180
Chris Mason6702ed42007-08-07 16:15:09 -04009181 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009182 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04009183 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009184 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
9185 path->lowest_level = 0;
9186 if (ret < 0) {
9187 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009188 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04009189 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009190 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009191
Chris Mason7d9eb122008-07-08 14:19:17 -04009192 /*
9193 * unlock our path, this is safe because only this
9194 * function is allowed to delete this snapshot
9195 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009196 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04009197
Yan Zheng2c47e6052009-06-27 21:07:35 -04009198 level = btrfs_header_level(root->node);
9199 while (1) {
9200 btrfs_tree_lock(path->nodes[level]);
9201 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009202 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009203
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009204 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009205 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05009206 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04009207 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009208 if (ret < 0) {
9209 err = ret;
9210 goto out_end_trans;
9211 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009212 BUG_ON(wc->refs[level] == 0);
9213
9214 if (level == root_item->drop_level)
9215 break;
9216
9217 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009218 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009219 WARN_ON(wc->refs[level] != 1);
9220 level--;
9221 }
9222 }
9223
9224 wc->level = level;
9225 wc->shared_level = -1;
9226 wc->stage = DROP_REFERENCE;
9227 wc->update_ref = update_ref;
9228 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009229 wc->for_reloc = for_reloc;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009230 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009231
9232 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00009233
Yan Zheng2c47e6052009-06-27 21:07:35 -04009234 ret = walk_down_tree(trans, root, path, wc);
9235 if (ret < 0) {
9236 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04009237 break;
9238 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009239
9240 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
9241 if (ret < 0) {
9242 err = ret;
9243 break;
9244 }
9245
9246 if (ret > 0) {
9247 BUG_ON(wc->stage != DROP_REFERENCE);
9248 break;
9249 }
9250
9251 if (wc->stage == DROP_REFERENCE) {
9252 level = wc->level;
9253 btrfs_node_key(path->nodes[level],
9254 &root_item->drop_progress,
9255 path->slots[level]);
9256 root_item->drop_level = level;
9257 }
9258
9259 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009260 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009261 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009262 ret = btrfs_update_root(trans, tree_root,
9263 &root->root_key,
9264 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009265 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009266 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009267 err = ret;
9268 goto out_end_trans;
9269 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009270
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009271 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009272 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009273 btrfs_debug(fs_info,
9274 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04009275 err = -EAGAIN;
9276 goto out_free;
9277 }
9278
Yan, Zhenga22285a2010-05-16 10:48:46 -04009279 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009280 if (IS_ERR(trans)) {
9281 err = PTR_ERR(trans);
9282 goto out_free;
9283 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009284 if (block_rsv)
9285 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04009286 }
Chris Mason20524f02007-03-10 06:35:47 -05009287 }
David Sterbab3b4aa72011-04-21 01:20:15 +02009288 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009289 if (err)
9290 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009291
Jeff Mahoney1cd54472017-08-17 10:25:11 -04009292 ret = btrfs_del_root(trans, fs_info, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009293 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009294 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009295 goto out_end_trans;
9296 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009297
Yan, Zheng76dda932009-09-21 16:00:26 -04009298 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00009299 ret = btrfs_find_root(tree_root, &root->root_key, path,
9300 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009301 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009302 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009303 err = ret;
9304 goto out_end_trans;
9305 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05009306 /* if we fail to delete the orphan item this time
9307 * around, it'll get picked up the next time.
9308 *
9309 * The most common failure here is just -ENOENT.
9310 */
9311 btrfs_del_orphan_item(trans, tree_root,
9312 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04009313 }
9314 }
9315
Miao Xie27cdeb72014-04-02 19:51:05 +08009316 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04009317 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009318 } else {
9319 free_extent_buffer(root->node);
9320 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00009321 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009322 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04009323 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009324out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009325 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009326out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04009327 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04009328 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009329out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04009330 /*
9331 * So if we need to stop dropping the snapshot for whatever reason we
9332 * need to make sure to add it back to the dead root list so that we
9333 * keep trying to do the work later. This also cleans up roots if we
9334 * don't have it in the radix (like when we recover after a power fail
9335 * or unmount) so we don't leak memory.
9336 */
Thomas Meyer897ca812017-10-07 16:02:21 +02009337 if (!for_reloc && !root_dropped)
Josef Bacikd29a9f62013-07-17 19:30:20 -04009338 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08009339 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009340 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04009341 return err;
Chris Mason20524f02007-03-10 06:35:47 -05009342}
Chris Mason9078a3e2007-04-26 16:46:15 -04009343
Yan Zheng2c47e6052009-06-27 21:07:35 -04009344/*
9345 * drop subtree rooted at tree block 'node'.
9346 *
9347 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009348 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04009349 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04009350int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
9351 struct btrfs_root *root,
9352 struct extent_buffer *node,
9353 struct extent_buffer *parent)
9354{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009355 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009356 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009357 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009358 int level;
9359 int parent_level;
9360 int ret = 0;
9361 int wret;
9362
Yan Zheng2c47e6052009-06-27 21:07:35 -04009363 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
9364
Yan Zhengf82d02d2008-10-29 14:49:05 -04009365 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009366 if (!path)
9367 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009368
Yan Zheng2c47e6052009-06-27 21:07:35 -04009369 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009370 if (!wc) {
9371 btrfs_free_path(path);
9372 return -ENOMEM;
9373 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009374
Chris Masonb9447ef82009-03-09 11:45:38 -04009375 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009376 parent_level = btrfs_header_level(parent);
9377 extent_buffer_get(parent);
9378 path->nodes[parent_level] = parent;
9379 path->slots[parent_level] = btrfs_header_nritems(parent);
9380
Chris Masonb9447ef82009-03-09 11:45:38 -04009381 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009382 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009383 path->nodes[level] = node;
9384 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009385 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009386
9387 wc->refs[parent_level] = 1;
9388 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
9389 wc->level = level;
9390 wc->shared_level = -1;
9391 wc->stage = DROP_REFERENCE;
9392 wc->update_ref = 0;
9393 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009394 wc->for_reloc = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009395 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009396
9397 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009398 wret = walk_down_tree(trans, root, path, wc);
9399 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04009400 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009401 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009402 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009403
Yan Zheng2c47e6052009-06-27 21:07:35 -04009404 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009405 if (wret < 0)
9406 ret = wret;
9407 if (wret != 0)
9408 break;
9409 }
9410
Yan Zheng2c47e6052009-06-27 21:07:35 -04009411 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009412 btrfs_free_path(path);
9413 return ret;
9414}
9415
Jeff Mahoney6202df62016-06-22 18:54:22 -04009416static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04009417{
9418 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009419 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04009420
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009421 /*
9422 * if restripe for this chunk_type is on pick target profile and
9423 * return, otherwise do the usual balance
9424 */
Jeff Mahoney6202df62016-06-22 18:54:22 -04009425 stripped = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009426 if (stripped)
9427 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02009428
Jeff Mahoney6202df62016-06-22 18:54:22 -04009429 num_devices = fs_info->fs_devices->rw_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05009430
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009431 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05009432 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009433 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
9434
Chris Masonec44a352008-04-28 15:29:52 -04009435 if (num_devices == 1) {
9436 stripped |= BTRFS_BLOCK_GROUP_DUP;
9437 stripped = flags & ~stripped;
9438
9439 /* turn raid0 into single device chunks */
9440 if (flags & BTRFS_BLOCK_GROUP_RAID0)
9441 return stripped;
9442
9443 /* turn mirroring into duplication */
9444 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
9445 BTRFS_BLOCK_GROUP_RAID10))
9446 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04009447 } else {
9448 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04009449 if (flags & stripped)
9450 return flags;
9451
9452 stripped |= BTRFS_BLOCK_GROUP_DUP;
9453 stripped = flags & ~stripped;
9454
9455 /* switch duplicated blocks with raid1 */
9456 if (flags & BTRFS_BLOCK_GROUP_DUP)
9457 return stripped | BTRFS_BLOCK_GROUP_RAID1;
9458
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009459 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04009460 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009461
Chris Masonec44a352008-04-28 15:29:52 -04009462 return flags;
9463}
9464
Zhaolei868f4012015-08-05 16:43:27 +08009465static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04009466{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009467 struct btrfs_space_info *sinfo = cache->space_info;
9468 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00009469 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009470 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04009471
Miao Xie199c36e2011-07-15 10:34:36 +00009472 /*
9473 * We need some metadata space and system metadata space for
9474 * allocating chunks in some corner cases until we force to set
9475 * it to be readonly.
9476 */
9477 if ((sinfo->flags &
9478 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
9479 !force)
Byongho Leeee221842015-12-15 01:42:10 +09009480 min_allocable_bytes = SZ_1M;
Miao Xie199c36e2011-07-15 10:34:36 +00009481 else
9482 min_allocable_bytes = 0;
9483
Yan, Zhengf0486c62010-05-16 10:46:25 -04009484 spin_lock(&sinfo->lock);
9485 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00009486
9487 if (cache->ro) {
Zhaolei868f4012015-08-05 16:43:27 +08009488 cache->ro++;
WuBo61cfea92011-07-26 03:30:11 +00009489 ret = 0;
9490 goto out;
9491 }
9492
Yan, Zhengf0486c62010-05-16 10:46:25 -04009493 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
9494 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04009495
Liu Bo41361352017-02-13 15:42:21 -08009496 if (btrfs_space_info_used(sinfo, true) + num_bytes +
Josef Bacik37be25b2011-08-05 10:25:38 -04009497 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04009498 sinfo->bytes_readonly += num_bytes;
Zhaolei868f4012015-08-05 16:43:27 +08009499 cache->ro++;
Josef Bacik633c0aa2014-10-31 09:49:34 -04009500 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009501 ret = 0;
9502 }
WuBo61cfea92011-07-26 03:30:11 +00009503out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04009504 spin_unlock(&cache->lock);
9505 spin_unlock(&sinfo->lock);
9506 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04009507}
9508
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009509int btrfs_inc_block_group_ro(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04009510 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009511
9512{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009513 struct btrfs_trans_handle *trans;
9514 u64 alloc_flags;
9515 int ret;
9516
Chris Mason1bbc6212015-04-06 12:46:08 -07009517again:
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009518 trans = btrfs_join_transaction(fs_info->extent_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009519 if (IS_ERR(trans))
9520 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009521
Chris Mason1bbc6212015-04-06 12:46:08 -07009522 /*
9523 * we're not allowed to set block groups readonly after the dirty
9524 * block groups cache has started writing. If it already started,
9525 * back off and let this transaction commit
9526 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009527 mutex_lock(&fs_info->ro_block_group_mutex);
Josef Bacik3204d332015-09-24 10:46:10 -04009528 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
Chris Mason1bbc6212015-04-06 12:46:08 -07009529 u64 transid = trans->transid;
9530
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009531 mutex_unlock(&fs_info->ro_block_group_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009532 btrfs_end_transaction(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07009533
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009534 ret = btrfs_wait_for_commit(fs_info, transid);
Chris Mason1bbc6212015-04-06 12:46:08 -07009535 if (ret)
9536 return ret;
9537 goto again;
9538 }
9539
Chris Mason153c35b2015-05-19 18:54:41 -07009540 /*
9541 * if we are changing raid levels, try to allocate a corresponding
9542 * block group with the new raid level.
9543 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009544 alloc_flags = update_block_group_flags(fs_info, cache->flags);
Chris Mason153c35b2015-05-19 18:54:41 -07009545 if (alloc_flags != cache->flags) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009546 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason153c35b2015-05-19 18:54:41 -07009547 CHUNK_ALLOC_FORCE);
9548 /*
9549 * ENOSPC is allowed here, we may have enough space
9550 * already allocated at the new raid level to
9551 * carry on
9552 */
9553 if (ret == -ENOSPC)
9554 ret = 0;
9555 if (ret < 0)
9556 goto out;
9557 }
Chris Mason1bbc6212015-04-06 12:46:08 -07009558
Zhaolei868f4012015-08-05 16:43:27 +08009559 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009560 if (!ret)
9561 goto out;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009562 alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags);
9563 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04009564 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009565 if (ret < 0)
9566 goto out;
Zhaolei868f4012015-08-05 16:43:27 +08009567 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009568out:
Shaohua Li2f081082015-01-09 10:40:15 -08009569 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009570 alloc_flags = update_block_group_flags(fs_info, cache->flags);
David Sterba34441362016-10-04 19:34:27 +02009571 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009572 check_system_chunk(trans, fs_info, alloc_flags);
David Sterba34441362016-10-04 19:34:27 +02009573 mutex_unlock(&fs_info->chunk_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009574 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009575 mutex_unlock(&fs_info->ro_block_group_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009576
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009577 btrfs_end_transaction(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009578 return ret;
9579}
9580
Chris Masonc87f08c2011-02-16 13:57:04 -05009581int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009582 struct btrfs_fs_info *fs_info, u64 type)
Chris Masonc87f08c2011-02-16 13:57:04 -05009583{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009584 u64 alloc_flags = get_alloc_profile(fs_info, type);
9585
9586 return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05009587}
9588
Miao Xie6d07bce2011-01-05 10:07:31 +00009589/*
9590 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04009591 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00009592 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009593u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00009594{
9595 struct btrfs_block_group_cache *block_group;
9596 u64 free_bytes = 0;
9597 int factor;
9598
Nicholas D Steeves01327612016-05-19 21:18:45 -04009599 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009600 if (list_empty(&sinfo->ro_bgs))
9601 return 0;
9602
9603 spin_lock(&sinfo->lock);
9604 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00009605 spin_lock(&block_group->lock);
9606
9607 if (!block_group->ro) {
9608 spin_unlock(&block_group->lock);
9609 continue;
9610 }
9611
9612 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
9613 BTRFS_BLOCK_GROUP_RAID10 |
9614 BTRFS_BLOCK_GROUP_DUP))
9615 factor = 2;
9616 else
9617 factor = 1;
9618
9619 free_bytes += (block_group->key.offset -
9620 btrfs_block_group_used(&block_group->item)) *
9621 factor;
9622
9623 spin_unlock(&block_group->lock);
9624 }
Miao Xie6d07bce2011-01-05 10:07:31 +00009625 spin_unlock(&sinfo->lock);
9626
9627 return free_bytes;
9628}
9629
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009630void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache)
Yan, Zhengf0486c62010-05-16 10:46:25 -04009631{
9632 struct btrfs_space_info *sinfo = cache->space_info;
9633 u64 num_bytes;
9634
9635 BUG_ON(!cache->ro);
9636
9637 spin_lock(&sinfo->lock);
9638 spin_lock(&cache->lock);
Zhaolei868f4012015-08-05 16:43:27 +08009639 if (!--cache->ro) {
9640 num_bytes = cache->key.offset - cache->reserved -
9641 cache->pinned - cache->bytes_super -
9642 btrfs_block_group_used(&cache->item);
9643 sinfo->bytes_readonly -= num_bytes;
9644 list_del_init(&cache->ro_list);
9645 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009646 spin_unlock(&cache->lock);
9647 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009648}
9649
Josef Bacikba1bf482009-09-11 16:11:19 -04009650/*
9651 * checks to see if its even possible to relocate this block group.
9652 *
9653 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9654 * ok to go ahead and try.
9655 */
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009656int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04009657{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009658 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04009659 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04009660 struct btrfs_space_info *space_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009661 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacikba1bf482009-09-11 16:11:19 -04009662 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04009663 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00009664 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04009665 u64 dev_min = 1;
9666 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009667 u64 target;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009668 int debug;
liubocdcb7252011-08-03 10:15:25 +00009669 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04009670 int full = 0;
9671 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05009672
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009673 debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG);
Qu Wenruo0305bc22016-03-23 11:38:17 +08009674
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009675 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04009676
Josef Bacikba1bf482009-09-11 16:11:19 -04009677 /* odd, couldn't find the block group, leave it alone */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009678 if (!block_group) {
9679 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009680 btrfs_warn(fs_info,
Qu Wenruo0305bc22016-03-23 11:38:17 +08009681 "can't find block group for bytenr %llu",
9682 bytenr);
Josef Bacikba1bf482009-09-11 16:11:19 -04009683 return -1;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009684 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009685
liubocdcb7252011-08-03 10:15:25 +00009686 min_free = btrfs_block_group_used(&block_group->item);
9687
Josef Bacikba1bf482009-09-11 16:11:19 -04009688 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00009689 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04009690 goto out;
Chris Mason323da792008-05-09 11:46:48 -04009691
Josef Bacikba1bf482009-09-11 16:11:19 -04009692 space_info = block_group->space_info;
9693 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04009694
Josef Bacikba1bf482009-09-11 16:11:19 -04009695 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04009696
Josef Bacikba1bf482009-09-11 16:11:19 -04009697 /*
9698 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04009699 * relocate it unless we're able to allocate a new chunk below.
9700 *
9701 * Otherwise, we need to make sure we have room in the space to handle
9702 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04009703 */
Chris Mason7ce618d2009-09-22 14:48:44 -04009704 if ((space_info->total_bytes != block_group->key.offset) &&
Liu Bo41361352017-02-13 15:42:21 -08009705 (btrfs_space_info_used(space_info, false) + min_free <
9706 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04009707 spin_unlock(&space_info->lock);
9708 goto out;
9709 }
9710 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009711
Josef Bacikba1bf482009-09-11 16:11:19 -04009712 /*
9713 * ok we don't have enough space, but maybe we have free space on our
9714 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009715 * alloc devices and guess if we have enough space. if this block
9716 * group is going to be restriped, run checks against the target
9717 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04009718 */
9719 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05009720
liubocdcb7252011-08-03 10:15:25 +00009721 /*
9722 * index:
9723 * 0: raid10
9724 * 1: raid1
9725 * 2: dup
9726 * 3: raid0
9727 * 4: single
9728 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009729 target = get_restripe_target(fs_info, block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009730 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00009731 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009732 } else {
9733 /*
9734 * this is just a balance, so if we were marked as full
9735 * we know there is no space for a new chunk
9736 */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009737 if (full) {
9738 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009739 btrfs_warn(fs_info,
9740 "no space to alloc new chunk for block group %llu",
9741 block_group->key.objectid);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009742 goto out;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009743 }
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009744
9745 index = get_block_group_index(block_group);
9746 }
9747
Miao Xiee6ec7162013-01-17 05:38:51 +00009748 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00009749 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04009750 /* Divide by 2 */
9751 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009752 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00009753 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00009754 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04009755 /* Multiply by 2 */
9756 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009757 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00009758 dev_min = fs_devices->rw_devices;
David Sterba47c57132015-02-20 18:43:47 +01009759 min_free = div64_u64(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00009760 }
9761
Josef Bacik6df9a952013-06-27 13:22:46 -04009762 /* We need to do this so that we can look at pending chunks */
9763 trans = btrfs_join_transaction(root);
9764 if (IS_ERR(trans)) {
9765 ret = PTR_ERR(trans);
9766 goto out;
9767 }
9768
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009769 mutex_lock(&fs_info->chunk_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04009770 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00009771 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04009772
Josef Bacikba1bf482009-09-11 16:11:19 -04009773 /*
9774 * check to make sure we can actually find a chunk with enough
9775 * space to fit our block group in.
9776 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01009777 if (device->total_bytes > device->bytes_used + min_free &&
9778 !device->is_tgtdev_for_dev_replace) {
Josef Bacik6df9a952013-06-27 13:22:46 -04009779 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00009780 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04009781 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00009782 dev_nr++;
9783
9784 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05009785 break;
liubocdcb7252011-08-03 10:15:25 +00009786
Josef Bacikba1bf482009-09-11 16:11:19 -04009787 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05009788 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009789 }
Qu Wenruo0305bc22016-03-23 11:38:17 +08009790 if (debug && ret == -1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009791 btrfs_warn(fs_info,
9792 "no space to allocate a new chunk for block group %llu",
9793 block_group->key.objectid);
9794 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009795 btrfs_end_transaction(trans);
Chris Masonedbd8d42007-12-21 16:27:24 -05009796out:
Josef Bacikba1bf482009-09-11 16:11:19 -04009797 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05009798 return ret;
9799}
9800
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009801static int find_first_block_group(struct btrfs_fs_info *fs_info,
9802 struct btrfs_path *path,
9803 struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04009804{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009805 struct btrfs_root *root = fs_info->extent_root;
Chris Mason925baed2008-06-25 16:01:30 -04009806 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009807 struct btrfs_key found_key;
9808 struct extent_buffer *leaf;
9809 int slot;
9810
9811 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
9812 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009813 goto out;
9814
Chris Masond3977122009-01-05 21:25:51 -05009815 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009816 slot = path->slots[0];
9817 leaf = path->nodes[0];
9818 if (slot >= btrfs_header_nritems(leaf)) {
9819 ret = btrfs_next_leaf(root, path);
9820 if (ret == 0)
9821 continue;
9822 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009823 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04009824 break;
9825 }
9826 btrfs_item_key_to_cpu(leaf, &found_key, slot);
9827
9828 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04009829 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
Liu Bo6fb37b72016-06-22 18:31:27 -07009830 struct extent_map_tree *em_tree;
9831 struct extent_map *em;
9832
9833 em_tree = &root->fs_info->mapping_tree.map_tree;
9834 read_lock(&em_tree->lock);
9835 em = lookup_extent_mapping(em_tree, found_key.objectid,
9836 found_key.offset);
9837 read_unlock(&em_tree->lock);
9838 if (!em) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009839 btrfs_err(fs_info,
Liu Bo6fb37b72016-06-22 18:31:27 -07009840 "logical %llu len %llu found bg but no related chunk",
9841 found_key.objectid, found_key.offset);
9842 ret = -ENOENT;
9843 } else {
9844 ret = 0;
9845 }
Josef Bacik187ee582016-08-18 15:30:06 -04009846 free_extent_map(em);
Chris Mason925baed2008-06-25 16:01:30 -04009847 goto out;
9848 }
Chris Mason0b86a832008-03-24 15:01:56 -04009849 path->slots[0]++;
9850 }
Chris Mason925baed2008-06-25 16:01:30 -04009851out:
Chris Mason0b86a832008-03-24 15:01:56 -04009852 return ret;
9853}
9854
Josef Bacik0af3d002010-06-21 14:48:16 -04009855void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9856{
9857 struct btrfs_block_group_cache *block_group;
9858 u64 last = 0;
9859
9860 while (1) {
9861 struct inode *inode;
9862
9863 block_group = btrfs_lookup_first_block_group(info, last);
9864 while (block_group) {
9865 spin_lock(&block_group->lock);
9866 if (block_group->iref)
9867 break;
9868 spin_unlock(&block_group->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009869 block_group = next_block_group(info, block_group);
Josef Bacik0af3d002010-06-21 14:48:16 -04009870 }
9871 if (!block_group) {
9872 if (last == 0)
9873 break;
9874 last = 0;
9875 continue;
9876 }
9877
9878 inode = block_group->inode;
9879 block_group->iref = 0;
9880 block_group->inode = NULL;
9881 spin_unlock(&block_group->lock);
Liu Bof3bca802016-07-20 17:33:44 -07009882 ASSERT(block_group->io_ctl.inode == NULL);
Josef Bacik0af3d002010-06-21 14:48:16 -04009883 iput(inode);
9884 last = block_group->key.objectid + block_group->key.offset;
9885 btrfs_put_block_group(block_group);
9886 }
9887}
9888
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009889/*
9890 * Must be called only after stopping all workers, since we could have block
9891 * group caching kthreads running, and therefore they could race with us if we
9892 * freed the block groups before stopping them.
9893 */
Zheng Yan1a40e232008-09-26 10:09:34 -04009894int btrfs_free_block_groups(struct btrfs_fs_info *info)
9895{
9896 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04009897 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04009898 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04009899 struct rb_node *n;
9900
Josef Bacik9e351cc2014-03-13 15:42:13 -04009901 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009902 while (!list_empty(&info->caching_block_groups)) {
9903 caching_ctl = list_entry(info->caching_block_groups.next,
9904 struct btrfs_caching_control, list);
9905 list_del(&caching_ctl->list);
9906 put_caching_control(caching_ctl);
9907 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04009908 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009909
Josef Bacik47ab2a62014-09-18 11:20:02 -04009910 spin_lock(&info->unused_bgs_lock);
9911 while (!list_empty(&info->unused_bgs)) {
9912 block_group = list_first_entry(&info->unused_bgs,
9913 struct btrfs_block_group_cache,
9914 bg_list);
9915 list_del_init(&block_group->bg_list);
9916 btrfs_put_block_group(block_group);
9917 }
9918 spin_unlock(&info->unused_bgs_lock);
9919
Zheng Yan1a40e232008-09-26 10:09:34 -04009920 spin_lock(&info->block_group_cache_lock);
9921 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
9922 block_group = rb_entry(n, struct btrfs_block_group_cache,
9923 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04009924 rb_erase(&block_group->cache_node,
9925 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009926 RB_CLEAR_NODE(&block_group->cache_node);
Yan Zhengd899e052008-10-30 14:25:28 -04009927 spin_unlock(&info->block_group_cache_lock);
9928
Josef Bacik80eb2342008-10-29 14:49:05 -04009929 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04009930 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04009931 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05009932
Josef Bacik3c148742011-02-02 15:53:47 +00009933 /*
9934 * We haven't cached this block group, which means we could
9935 * possibly have excluded extents on this block group.
9936 */
Josef Bacik36cce922013-08-05 11:15:21 -04009937 if (block_group->cached == BTRFS_CACHE_NO ||
9938 block_group->cached == BTRFS_CACHE_ERROR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009939 free_excluded_extents(info, block_group);
Josef Bacik3c148742011-02-02 15:53:47 +00009940
Josef Bacik817d52f2009-07-13 21:29:25 -04009941 btrfs_remove_free_space_cache(block_group);
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009942 ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
Liu Bof3bca802016-07-20 17:33:44 -07009943 ASSERT(list_empty(&block_group->dirty_list));
9944 ASSERT(list_empty(&block_group->io_list));
9945 ASSERT(list_empty(&block_group->bg_list));
9946 ASSERT(atomic_read(&block_group->count) == 1);
Josef Bacik11dfe352009-11-13 20:12:59 +00009947 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04009948
9949 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009950 }
9951 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04009952
9953 /* now that all the block groups are freed, go through and
9954 * free all the space_info structs. This is only called during
9955 * the final stages of unmount, and so we know nobody is
9956 * using them. We call synchronize_rcu() once before we start,
9957 * just to be on the safe side.
9958 */
9959 synchronize_rcu();
9960
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04009961 release_global_block_rsv(info);
9962
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309963 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009964 int i;
9965
Chris Mason4184ea72009-03-10 12:39:20 -04009966 space_info = list_entry(info->space_info.next,
9967 struct btrfs_space_info,
9968 list);
Josef Bacikd555b6c32016-03-25 13:25:51 -04009969
9970 /*
9971 * Do not hide this behind enospc_debug, this is actually
9972 * important and indicates a real bug if this happens.
9973 */
9974 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00009975 space_info->bytes_reserved > 0 ||
Josef Bacikd555b6c32016-03-25 13:25:51 -04009976 space_info->bytes_may_use > 0))
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009977 dump_space_info(info, space_info, 0, 0);
Chris Mason4184ea72009-03-10 12:39:20 -04009978 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009979 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
9980 struct kobject *kobj;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009981 kobj = space_info->block_group_kobjs[i];
9982 space_info->block_group_kobjs[i] = NULL;
9983 if (kobj) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009984 kobject_del(kobj);
9985 kobject_put(kobj);
9986 }
9987 }
9988 kobject_del(&space_info->kobj);
9989 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04009990 }
Zheng Yan1a40e232008-09-26 10:09:34 -04009991 return 0;
9992}
9993
Nikolay Borisovc434d212017-08-21 12:43:50 +03009994static void link_block_group(struct btrfs_block_group_cache *cache)
Yan, Zhengb742bb822010-05-16 10:46:24 -04009995{
Nikolay Borisovc434d212017-08-21 12:43:50 +03009996 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengb742bb822010-05-16 10:46:24 -04009997 int index = get_block_group_index(cache);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009998 bool first = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04009999
10000 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -040010001 if (list_empty(&space_info->block_groups[index]))
10002 first = true;
10003 list_add_tail(&cache->list, &space_info->block_groups[index]);
10004 up_write(&space_info->groups_sem);
10005
10006 if (first) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010007 struct raid_kobject *rkobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010008 int ret;
10009
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010010 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
10011 if (!rkobj)
10012 goto out_err;
10013 rkobj->raid_type = index;
10014 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
10015 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
10016 "%s", get_raid_name(index));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010017 if (ret) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010018 kobject_put(&rkobj->kobj);
10019 goto out_err;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010020 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010021 space_info->block_group_kobjs[index] = &rkobj->kobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010022 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010023
10024 return;
10025out_err:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -040010026 btrfs_warn(cache->fs_info,
10027 "failed to add kobject for block cache, ignoring");
Yan, Zhengb742bb822010-05-16 10:46:24 -040010028}
10029
Miao Xie920e4a52014-01-15 20:00:55 +080010030static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010031btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info,
10032 u64 start, u64 size)
Miao Xie920e4a52014-01-15 20:00:55 +080010033{
10034 struct btrfs_block_group_cache *cache;
10035
10036 cache = kzalloc(sizeof(*cache), GFP_NOFS);
10037 if (!cache)
10038 return NULL;
10039
10040 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
10041 GFP_NOFS);
10042 if (!cache->free_space_ctl) {
10043 kfree(cache);
10044 return NULL;
10045 }
10046
10047 cache->key.objectid = start;
10048 cache->key.offset = size;
10049 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
10050
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010051 cache->fs_info = fs_info;
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +030010052 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010053 set_free_space_tree_thresholds(cache);
10054
Miao Xie920e4a52014-01-15 20:00:55 +080010055 atomic_set(&cache->count, 1);
10056 spin_lock_init(&cache->lock);
Miao Xiee570fd22014-06-19 10:42:50 +080010057 init_rwsem(&cache->data_rwsem);
Miao Xie920e4a52014-01-15 20:00:55 +080010058 INIT_LIST_HEAD(&cache->list);
10059 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010060 INIT_LIST_HEAD(&cache->bg_list);
Josef Bacik633c0aa2014-10-31 09:49:34 -040010061 INIT_LIST_HEAD(&cache->ro_list);
Josef Bacikce93ec52014-11-17 15:45:48 -050010062 INIT_LIST_HEAD(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -070010063 INIT_LIST_HEAD(&cache->io_list);
Miao Xie920e4a52014-01-15 20:00:55 +080010064 btrfs_init_free_space_ctl(cache);
Filipe Manana04216822014-11-27 21:14:15 +000010065 atomic_set(&cache->trimming, 0);
Omar Sandovala5ed9182015-09-29 20:50:35 -070010066 mutex_init(&cache->free_space_lock);
Qu Wenruo0966a7b2017-04-14 08:35:54 +080010067 btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root);
Miao Xie920e4a52014-01-15 20:00:55 +080010068
10069 return cache;
10070}
10071
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010072int btrfs_read_block_groups(struct btrfs_fs_info *info)
Chris Mason9078a3e2007-04-26 16:46:15 -040010073{
10074 struct btrfs_path *path;
10075 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010076 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -040010077 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -040010078 struct btrfs_key key;
10079 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -040010080 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -040010081 int need_clear = 0;
10082 u64 cache_gen;
Liu Bo49303382016-08-25 18:08:27 -070010083 u64 feature;
10084 int mixed;
10085
10086 feature = btrfs_super_incompat_flags(info->super_copy);
10087 mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS);
Chris Mason96b51792007-10-15 16:15:19 -040010088
Chris Mason9078a3e2007-04-26 16:46:15 -040010089 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -040010090 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010091 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Chris Mason9078a3e2007-04-26 16:46:15 -040010092 path = btrfs_alloc_path();
10093 if (!path)
10094 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +010010095 path->reada = READA_FORWARD;
Chris Mason9078a3e2007-04-26 16:46:15 -040010096
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010097 cache_gen = btrfs_super_cache_generation(info->super_copy);
10098 if (btrfs_test_opt(info, SPACE_CACHE) &&
10099 btrfs_super_generation(info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -040010100 need_clear = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010101 if (btrfs_test_opt(info, CLEAR_CACHE))
Josef Bacik88c2ba32010-09-21 14:21:34 -040010102 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -040010103
Chris Masond3977122009-01-05 21:25:51 -050010104 while (1) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010105 ret = find_first_block_group(info, path, &key);
Yan, Zhengb742bb822010-05-16 10:46:24 -040010106 if (ret > 0)
10107 break;
Chris Mason0b86a832008-03-24 15:01:56 -040010108 if (ret != 0)
10109 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +080010110
Chris Mason5f39d392007-10-15 16:14:19 -040010111 leaf = path->nodes[0];
10112 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +080010113
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010114 cache = btrfs_create_block_group_cache(info, found_key.objectid,
Miao Xie920e4a52014-01-15 20:00:55 +080010115 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -040010116 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -040010117 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -040010118 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -040010119 }
Josef Bacik96303082009-07-13 21:29:25 -040010120
Liu Bocf7c1ef2012-07-06 03:31:34 -060010121 if (need_clear) {
10122 /*
10123 * When we mount with old space cache, we need to
10124 * set BTRFS_DC_CLEAR and set dirty flag.
10125 *
10126 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
10127 * truncate the old free space cache inode and
10128 * setup a new one.
10129 * b) Setting 'dirty flag' makes sure that we flush
10130 * the new space cache info onto disk.
10131 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010132 if (btrfs_test_opt(info, SPACE_CACHE))
Josef Bacikce93ec52014-11-17 15:45:48 -050010133 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -060010134 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010135
Chris Mason5f39d392007-10-15 16:14:19 -040010136 read_extent_buffer(leaf, &cache->item,
10137 btrfs_item_ptr_offset(leaf, path->slots[0]),
10138 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +080010139 cache->flags = btrfs_block_group_flags(&cache->item);
Liu Bo49303382016-08-25 18:08:27 -070010140 if (!mixed &&
10141 ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
10142 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
10143 btrfs_err(info,
10144"bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
10145 cache->key.objectid);
10146 ret = -EINVAL;
10147 goto error;
10148 }
Chris Mason0b86a832008-03-24 15:01:56 -040010149
Chris Mason9078a3e2007-04-26 16:46:15 -040010150 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +020010151 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +080010152
Josef Bacik817d52f2009-07-13 21:29:25 -040010153 /*
Josef Bacik3c148742011-02-02 15:53:47 +000010154 * We need to exclude the super stripes now so that the space
10155 * info has super bytes accounted for, otherwise we'll think
10156 * we have more space than we actually do.
10157 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010158 ret = exclude_super_stripes(info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010159 if (ret) {
10160 /*
10161 * We may have excluded something, so call this just in
10162 * case.
10163 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010164 free_excluded_extents(info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010165 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010166 goto error;
10167 }
Josef Bacik3c148742011-02-02 15:53:47 +000010168
10169 /*
Josef Bacik817d52f2009-07-13 21:29:25 -040010170 * check for two cases, either we are full, and therefore
10171 * don't need to bother with the caching work since we won't
10172 * find any space, or we are empty, and we can just add all
10173 * the space in and be done with it. This saves us _alot_ of
10174 * time, particularly in the full case.
10175 */
10176 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -040010177 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010178 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010179 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010180 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -040010181 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010182 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010183 add_new_free_space(cache, info,
Josef Bacik817d52f2009-07-13 21:29:25 -040010184 found_key.objectid,
10185 found_key.objectid +
10186 found_key.offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010187 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010188 }
Chris Mason96b51792007-10-15 16:15:19 -040010189
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010190 ret = btrfs_add_block_group_cache(info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010191 if (ret) {
10192 btrfs_remove_free_space_cache(cache);
10193 btrfs_put_block_group(cache);
10194 goto error;
10195 }
10196
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010197 trace_btrfs_add_block_group(info, cache, 0);
Nikolay Borisovd2006e62017-05-22 09:35:50 +030010198 update_space_info(info, cache->flags, found_key.offset,
10199 btrfs_block_group_used(&cache->item),
10200 cache->bytes_super, &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010201
Chris Mason6324fbf2008-03-24 15:01:59 -040010202 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -040010203
Nikolay Borisovc434d212017-08-21 12:43:50 +030010204 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010205
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010206 set_avail_alloc_bits(info, cache->flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010207 if (btrfs_chunk_readonly(info, cache->key.objectid)) {
Zhaolei868f4012015-08-05 16:43:27 +080010208 inc_block_group_ro(cache, 1);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010209 } else if (btrfs_block_group_used(&cache->item) == 0) {
10210 spin_lock(&info->unused_bgs_lock);
10211 /* Should always be true but just in case. */
10212 if (list_empty(&cache->bg_list)) {
10213 btrfs_get_block_group(cache);
10214 list_add_tail(&cache->bg_list,
10215 &info->unused_bgs);
10216 }
10217 spin_unlock(&info->unused_bgs_lock);
10218 }
Chris Mason9078a3e2007-04-26 16:46:15 -040010219 }
Yan, Zhengb742bb822010-05-16 10:46:24 -040010220
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010221 list_for_each_entry_rcu(space_info, &info->space_info, list) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010222 if (!(get_alloc_profile(info, space_info->flags) &
Yan, Zhengb742bb822010-05-16 10:46:24 -040010223 (BTRFS_BLOCK_GROUP_RAID10 |
10224 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -050010225 BTRFS_BLOCK_GROUP_RAID5 |
10226 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb822010-05-16 10:46:24 -040010227 BTRFS_BLOCK_GROUP_DUP)))
10228 continue;
10229 /*
10230 * avoid allocating from un-mirrored block group if there are
10231 * mirrored block groups.
10232 */
chandan1095cc02013-07-16 12:28:56 +053010233 list_for_each_entry(cache,
10234 &space_info->block_groups[BTRFS_RAID_RAID0],
10235 list)
Zhaolei868f4012015-08-05 16:43:27 +080010236 inc_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +053010237 list_for_each_entry(cache,
10238 &space_info->block_groups[BTRFS_RAID_SINGLE],
10239 list)
Zhaolei868f4012015-08-05 16:43:27 +080010240 inc_block_group_ro(cache, 1);
Yan, Zhengb742bb822010-05-16 10:46:24 -040010241 }
Yan, Zhengf0486c62010-05-16 10:46:25 -040010242
10243 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -040010244 ret = 0;
10245error:
Chris Mason9078a3e2007-04-26 16:46:15 -040010246 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -040010247 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010248}
Chris Mason6324fbf2008-03-24 15:01:59 -040010249
Josef Bacikea658ba2012-09-11 16:57:25 -040010250void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010251 struct btrfs_fs_info *fs_info)
Josef Bacikea658ba2012-09-11 16:57:25 -040010252{
10253 struct btrfs_block_group_cache *block_group, *tmp;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010254 struct btrfs_root *extent_root = fs_info->extent_root;
Josef Bacikea658ba2012-09-11 16:57:25 -040010255 struct btrfs_block_group_item item;
10256 struct btrfs_key key;
10257 int ret = 0;
Filipe Mananad9a05402015-10-03 13:13:13 +010010258 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010259
Filipe Mananad9a05402015-10-03 13:13:13 +010010260 trans->can_flush_pending_bgs = false;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010261 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
Josef Bacikea658ba2012-09-11 16:57:25 -040010262 if (ret)
Filipe Mananac92f6be2014-11-26 15:28:55 +000010263 goto next;
Josef Bacikea658ba2012-09-11 16:57:25 -040010264
10265 spin_lock(&block_group->lock);
10266 memcpy(&item, &block_group->item, sizeof(item));
10267 memcpy(&key, &block_group->key, sizeof(key));
10268 spin_unlock(&block_group->lock);
10269
10270 ret = btrfs_insert_item(trans, extent_root, &key, &item,
10271 sizeof(item));
10272 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010273 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010274 ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid,
10275 key.offset);
Josef Bacik6df9a952013-06-27 13:22:46 -040010276 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010277 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010278 add_block_group_free_space(trans, fs_info, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010279 /* already aborted the transaction if it failed. */
Filipe Mananac92f6be2014-11-26 15:28:55 +000010280next:
10281 list_del_init(&block_group->bg_list);
Josef Bacikea658ba2012-09-11 16:57:25 -040010282 }
Filipe Mananad9a05402015-10-03 13:13:13 +010010283 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010284}
10285
Chris Mason6324fbf2008-03-24 15:01:59 -040010286int btrfs_make_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010287 struct btrfs_fs_info *fs_info, u64 bytes_used,
Nikolay Borisov01744842017-07-27 14:22:11 +030010288 u64 type, u64 chunk_offset, u64 size)
Chris Mason6324fbf2008-03-24 15:01:59 -040010289{
Chris Mason6324fbf2008-03-24 15:01:59 -040010290 struct btrfs_block_group_cache *cache;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010291 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -040010292
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010293 btrfs_set_log_full_commit(fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -040010294
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010295 cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -040010296 if (!cache)
10297 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +080010298
Chris Mason6324fbf2008-03-24 15:01:59 -040010299 btrfs_set_block_group_used(&cache->item, bytes_used);
Nikolay Borisov01744842017-07-27 14:22:11 +030010300 btrfs_set_block_group_chunk_objectid(&cache->item,
10301 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Mason6324fbf2008-03-24 15:01:59 -040010302 btrfs_set_block_group_flags(&cache->item, type);
10303
Miao Xie920e4a52014-01-15 20:00:55 +080010304 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -040010305 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010306 cache->cached = BTRFS_CACHE_FINISHED;
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010307 cache->needs_free_space = 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010308 ret = exclude_super_stripes(fs_info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010309 if (ret) {
10310 /*
10311 * We may have excluded something, so call this just in
10312 * case.
10313 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010314 free_excluded_extents(fs_info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010315 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010316 return ret;
10317 }
Josef Bacik96303082009-07-13 21:29:25 -040010318
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010319 add_new_free_space(cache, fs_info, chunk_offset, chunk_offset + size);
Josef Bacik817d52f2009-07-13 21:29:25 -040010320
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010321 free_excluded_extents(fs_info, cache);
Yan Zheng11833d62009-09-11 16:11:19 -040010322
Josef Bacikd0bd4562015-09-23 14:54:14 -040010323#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010324 if (btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -040010325 u64 new_bytes_used = size - bytes_used;
10326
10327 bytes_used += new_bytes_used >> 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010328 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -040010329 }
10330#endif
Filipe Manana2e6e5182015-05-12 00:28:11 +010010331 /*
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010332 * Ensure the corresponding space_info object is created and
10333 * assigned to our block group. We want our bg to be added to the rbtree
10334 * with its ->space_info set.
Filipe Manana2e6e5182015-05-12 00:28:11 +010010335 */
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010336 cache->space_info = __find_space_info(fs_info, cache->flags);
10337 if (!cache->space_info) {
10338 ret = create_space_info(fs_info, cache->flags,
10339 &cache->space_info);
10340 if (ret) {
10341 btrfs_remove_free_space_cache(cache);
10342 btrfs_put_block_group(cache);
10343 return ret;
10344 }
Filipe Manana2e6e5182015-05-12 00:28:11 +010010345 }
10346
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010347 ret = btrfs_add_block_group_cache(fs_info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010348 if (ret) {
10349 btrfs_remove_free_space_cache(cache);
10350 btrfs_put_block_group(cache);
10351 return ret;
10352 }
10353
Filipe Manana2e6e5182015-05-12 00:28:11 +010010354 /*
10355 * Now that our block group has its ->space_info set and is inserted in
10356 * the rbtree, update the space info's counters.
10357 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010358 trace_btrfs_add_block_group(fs_info, cache, 1);
Nikolay Borisovd2006e62017-05-22 09:35:50 +030010359 update_space_info(fs_info, cache->flags, size, bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -040010360 cache->bytes_super, &cache->space_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010361 update_global_block_rsv(fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -040010362
Nikolay Borisovc434d212017-08-21 12:43:50 +030010363 link_block_group(cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010364
Josef Bacik47ab2a62014-09-18 11:20:02 -040010365 list_add_tail(&cache->bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -040010366
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010367 set_avail_alloc_bits(fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -040010368 return 0;
10369}
Zheng Yan1a40e232008-09-26 10:09:34 -040010370
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010371static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
10372{
Ilya Dryomov899c81e2012-03-27 17:09:16 +030010373 u64 extra_flags = chunk_to_extended(flags) &
10374 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010375
Miao Xiede98ced2013-01-29 10:13:12 +000010376 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010377 if (flags & BTRFS_BLOCK_GROUP_DATA)
10378 fs_info->avail_data_alloc_bits &= ~extra_flags;
10379 if (flags & BTRFS_BLOCK_GROUP_METADATA)
10380 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
10381 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
10382 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +000010383 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010384}
10385
Zheng Yan1a40e232008-09-26 10:09:34 -040010386int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010387 struct btrfs_fs_info *fs_info, u64 group_start,
Filipe Manana04216822014-11-27 21:14:15 +000010388 struct extent_map *em)
Zheng Yan1a40e232008-09-26 10:09:34 -040010389{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010390 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010391 struct btrfs_path *path;
10392 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -040010393 struct btrfs_free_cluster *cluster;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010394 struct btrfs_root *tree_root = fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010395 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -040010396 struct inode *inode;
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010397 struct kobject *kobj = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -040010398 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010399 int index;
Josef Bacik89a55892010-10-14 14:52:27 -040010400 int factor;
Filipe Manana4f69cb92014-11-26 15:28:51 +000010401 struct btrfs_caching_control *caching_ctl = NULL;
Filipe Manana04216822014-11-27 21:14:15 +000010402 bool remove_em;
Zheng Yan1a40e232008-09-26 10:09:34 -040010403
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010404 block_group = btrfs_lookup_block_group(fs_info, group_start);
Zheng Yan1a40e232008-09-26 10:09:34 -040010405 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -050010406 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -040010407
liubo9f7c43c2011-03-07 02:13:33 +000010408 /*
10409 * Free the reserved super bytes from this block group before
10410 * remove it.
10411 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010412 free_excluded_extents(fs_info, block_group);
Josef Bacikfd708b82017-09-29 15:43:50 -040010413 btrfs_free_ref_tree_range(fs_info, block_group->key.objectid,
10414 block_group->key.offset);
liubo9f7c43c2011-03-07 02:13:33 +000010415
Zheng Yan1a40e232008-09-26 10:09:34 -040010416 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010417 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -040010418 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
10419 BTRFS_BLOCK_GROUP_RAID1 |
10420 BTRFS_BLOCK_GROUP_RAID10))
10421 factor = 2;
10422 else
10423 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -040010424
Chris Mason44fb5512009-06-04 15:34:51 -040010425 /* make sure this block group isn't part of an allocation cluster */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010426 cluster = &fs_info->data_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010427 spin_lock(&cluster->refill_lock);
10428 btrfs_return_cluster_to_free_space(block_group, cluster);
10429 spin_unlock(&cluster->refill_lock);
10430
10431 /*
10432 * make sure this block group isn't part of a metadata
10433 * allocation cluster
10434 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010435 cluster = &fs_info->meta_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010436 spin_lock(&cluster->refill_lock);
10437 btrfs_return_cluster_to_free_space(block_group, cluster);
10438 spin_unlock(&cluster->refill_lock);
10439
Zheng Yan1a40e232008-09-26 10:09:34 -040010440 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010441 if (!path) {
10442 ret = -ENOMEM;
10443 goto out;
10444 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010445
Chris Mason1bbc6212015-04-06 12:46:08 -070010446 /*
10447 * get the inode first so any iput calls done for the io_list
10448 * aren't the final iput (no unlinks allowed now)
10449 */
Jeff Mahoney77ab86b2017-02-15 16:28:30 -050010450 inode = lookup_free_space_inode(fs_info, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010451
10452 mutex_lock(&trans->transaction->cache_write_mutex);
10453 /*
10454 * make sure our free spache cache IO is done before remove the
10455 * free space inode
10456 */
10457 spin_lock(&trans->transaction->dirty_bgs_lock);
10458 if (!list_empty(&block_group->io_list)) {
10459 list_del_init(&block_group->io_list);
10460
10461 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
10462
10463 spin_unlock(&trans->transaction->dirty_bgs_lock);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040010464 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010465 btrfs_put_block_group(block_group);
10466 spin_lock(&trans->transaction->dirty_bgs_lock);
10467 }
10468
10469 if (!list_empty(&block_group->dirty_list)) {
10470 list_del_init(&block_group->dirty_list);
10471 btrfs_put_block_group(block_group);
10472 }
10473 spin_unlock(&trans->transaction->dirty_bgs_lock);
10474 mutex_unlock(&trans->transaction->cache_write_mutex);
10475
Josef Bacik0af3d002010-06-21 14:48:16 -040010476 if (!IS_ERR(inode)) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010477 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010478 if (ret) {
10479 btrfs_add_delayed_iput(inode);
10480 goto out;
10481 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010482 clear_nlink(inode);
10483 /* One for the block groups ref */
10484 spin_lock(&block_group->lock);
10485 if (block_group->iref) {
10486 block_group->iref = 0;
10487 block_group->inode = NULL;
10488 spin_unlock(&block_group->lock);
10489 iput(inode);
10490 } else {
10491 spin_unlock(&block_group->lock);
10492 }
10493 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -040010494 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -040010495 }
10496
10497 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
10498 key.offset = block_group->key.objectid;
10499 key.type = 0;
10500
10501 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
10502 if (ret < 0)
10503 goto out;
10504 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +020010505 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010506 if (ret == 0) {
10507 ret = btrfs_del_item(trans, tree_root, path);
10508 if (ret)
10509 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +020010510 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010511 }
10512
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010513 spin_lock(&fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -040010514 rb_erase(&block_group->cache_node,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010515 &fs_info->block_group_cache_tree);
Filipe Manana292cbd52014-11-26 15:28:50 +000010516 RB_CLEAR_NODE(&block_group->cache_node);
Liu Boa1897fd2012-12-27 09:01:23 +000010517
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010518 if (fs_info->first_logical_byte == block_group->key.objectid)
10519 fs_info->first_logical_byte = (u64)-1;
10520 spin_unlock(&fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010521
Josef Bacik80eb2342008-10-29 14:49:05 -040010522 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -040010523 /*
10524 * we must use list_del_init so people can check to see if they
10525 * are still on the list after taking the semaphore
10526 */
10527 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010528 if (list_empty(&block_group->space_info->block_groups[index])) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010529 kobj = block_group->space_info->block_group_kobjs[index];
10530 block_group->space_info->block_group_kobjs[index] = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010531 clear_avail_alloc_bits(fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010532 }
Josef Bacik80eb2342008-10-29 14:49:05 -040010533 up_write(&block_group->space_info->groups_sem);
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010534 if (kobj) {
10535 kobject_del(kobj);
10536 kobject_put(kobj);
10537 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010538
Filipe Manana4f69cb92014-11-26 15:28:51 +000010539 if (block_group->has_caching_ctl)
10540 caching_ctl = get_caching_control(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -040010541 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -040010542 wait_block_group_cache_done(block_group);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010543 if (block_group->has_caching_ctl) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010544 down_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010545 if (!caching_ctl) {
10546 struct btrfs_caching_control *ctl;
10547
10548 list_for_each_entry(ctl,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010549 &fs_info->caching_block_groups, list)
Filipe Manana4f69cb92014-11-26 15:28:51 +000010550 if (ctl->block_group == block_group) {
10551 caching_ctl = ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +020010552 refcount_inc(&caching_ctl->count);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010553 break;
10554 }
10555 }
10556 if (caching_ctl)
10557 list_del_init(&caching_ctl->list);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010558 up_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010559 if (caching_ctl) {
10560 /* Once for the caching bgs list and once for us. */
10561 put_caching_control(caching_ctl);
10562 put_caching_control(caching_ctl);
10563 }
10564 }
Josef Bacik817d52f2009-07-13 21:29:25 -040010565
Josef Bacikce93ec52014-11-17 15:45:48 -050010566 spin_lock(&trans->transaction->dirty_bgs_lock);
10567 if (!list_empty(&block_group->dirty_list)) {
Chris Mason1bbc6212015-04-06 12:46:08 -070010568 WARN_ON(1);
10569 }
10570 if (!list_empty(&block_group->io_list)) {
10571 WARN_ON(1);
Josef Bacikce93ec52014-11-17 15:45:48 -050010572 }
10573 spin_unlock(&trans->transaction->dirty_bgs_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010574 btrfs_remove_free_space_cache(block_group);
10575
Yan Zhengc146afa2008-11-12 14:34:12 -050010576 spin_lock(&block_group->space_info->lock);
Filipe Manana75c68e92015-01-16 13:24:40 +000010577 list_del_init(&block_group->ro_list);
Zhao Lei18d018a2015-02-24 20:07:44 +080010578
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010579 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Zhao Lei18d018a2015-02-24 20:07:44 +080010580 WARN_ON(block_group->space_info->total_bytes
10581 < block_group->key.offset);
10582 WARN_ON(block_group->space_info->bytes_readonly
10583 < block_group->key.offset);
10584 WARN_ON(block_group->space_info->disk_total
10585 < block_group->key.offset * factor);
10586 }
Yan Zhengc146afa2008-11-12 14:34:12 -050010587 block_group->space_info->total_bytes -= block_group->key.offset;
10588 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -040010589 block_group->space_info->disk_total -= block_group->key.offset * factor;
Zhao Lei18d018a2015-02-24 20:07:44 +080010590
Yan Zhengc146afa2008-11-12 14:34:12 -050010591 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -040010592
Josef Bacik0af3d002010-06-21 14:48:16 -040010593 memcpy(&key, &block_group->key, sizeof(key));
10594
David Sterba34441362016-10-04 19:34:27 +020010595 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana495e64f2014-12-02 18:07:30 +000010596 if (!list_empty(&em->list)) {
10597 /* We're in the transaction->pending_chunks list. */
10598 free_extent_map(em);
10599 }
Filipe Manana04216822014-11-27 21:14:15 +000010600 spin_lock(&block_group->lock);
10601 block_group->removed = 1;
10602 /*
10603 * At this point trimming can't start on this block group, because we
10604 * removed the block group from the tree fs_info->block_group_cache_tree
10605 * so no one can't find it anymore and even if someone already got this
10606 * block group before we removed it from the rbtree, they have already
10607 * incremented block_group->trimming - if they didn't, they won't find
10608 * any free space entries because we already removed them all when we
10609 * called btrfs_remove_free_space_cache().
10610 *
10611 * And we must not remove the extent map from the fs_info->mapping_tree
10612 * to prevent the same logical address range and physical device space
10613 * ranges from being reused for a new block group. This is because our
10614 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
10615 * completely transactionless, so while it is trimming a range the
10616 * currently running transaction might finish and a new one start,
10617 * allowing for new block groups to be created that can reuse the same
10618 * physical device locations unless we take this special care.
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010619 *
10620 * There may also be an implicit trim operation if the file system
10621 * is mounted with -odiscard. The same protections must remain
10622 * in place until the extents have been discarded completely when
10623 * the transaction commit has completed.
Filipe Manana04216822014-11-27 21:14:15 +000010624 */
10625 remove_em = (atomic_read(&block_group->trimming) == 0);
10626 /*
10627 * Make sure a trimmer task always sees the em in the pinned_chunks list
10628 * if it sees block_group->removed == 1 (needs to lock block_group->lock
10629 * before checking block_group->removed).
10630 */
10631 if (!remove_em) {
10632 /*
10633 * Our em might be in trans->transaction->pending_chunks which
10634 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10635 * and so is the fs_info->pinned_chunks list.
10636 *
10637 * So at this point we must be holding the chunk_mutex to avoid
10638 * any races with chunk allocation (more specifically at
10639 * volumes.c:contains_pending_extent()), to ensure it always
10640 * sees the em, either in the pending_chunks list or in the
10641 * pinned_chunks list.
10642 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010643 list_move_tail(&em->list, &fs_info->pinned_chunks);
Filipe Manana04216822014-11-27 21:14:15 +000010644 }
10645 spin_unlock(&block_group->lock);
Filipe Manana04216822014-11-27 21:14:15 +000010646
10647 if (remove_em) {
10648 struct extent_map_tree *em_tree;
10649
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010650 em_tree = &fs_info->mapping_tree.map_tree;
Filipe Manana04216822014-11-27 21:14:15 +000010651 write_lock(&em_tree->lock);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010652 /*
10653 * The em might be in the pending_chunks list, so make sure the
10654 * chunk mutex is locked, since remove_extent_mapping() will
10655 * delete us from that list.
10656 */
Filipe Manana04216822014-11-27 21:14:15 +000010657 remove_extent_mapping(em_tree, em);
10658 write_unlock(&em_tree->lock);
10659 /* once for the tree */
10660 free_extent_map(em);
10661 }
10662
David Sterba34441362016-10-04 19:34:27 +020010663 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010664
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010665 ret = remove_block_group_free_space(trans, fs_info, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010666 if (ret)
10667 goto out;
10668
Chris Masonfa9c0d792009-04-03 09:47:43 -040010669 btrfs_put_block_group(block_group);
10670 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -040010671
10672 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
10673 if (ret > 0)
10674 ret = -EIO;
10675 if (ret < 0)
10676 goto out;
10677
10678 ret = btrfs_del_item(trans, root, path);
10679out:
10680 btrfs_free_path(path);
10681 return ret;
10682}
liuboacce9522011-01-06 19:30:25 +080010683
Filipe Manana8eab77f2015-11-13 23:57:16 +000010684struct btrfs_trans_handle *
Filipe Manana7fd01182015-11-13 23:57:17 +000010685btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info,
10686 const u64 chunk_offset)
Filipe Manana8eab77f2015-11-13 23:57:16 +000010687{
Filipe Manana7fd01182015-11-13 23:57:17 +000010688 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
10689 struct extent_map *em;
10690 struct map_lookup *map;
10691 unsigned int num_items;
10692
10693 read_lock(&em_tree->lock);
10694 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
10695 read_unlock(&em_tree->lock);
10696 ASSERT(em && em->start == chunk_offset);
10697
Filipe Manana8eab77f2015-11-13 23:57:16 +000010698 /*
Filipe Manana7fd01182015-11-13 23:57:17 +000010699 * We need to reserve 3 + N units from the metadata space info in order
10700 * to remove a block group (done at btrfs_remove_chunk() and at
10701 * btrfs_remove_block_group()), which are used for:
10702 *
Filipe Manana8eab77f2015-11-13 23:57:16 +000010703 * 1 unit for adding the free space inode's orphan (located in the tree
10704 * of tree roots).
Filipe Manana7fd01182015-11-13 23:57:17 +000010705 * 1 unit for deleting the block group item (located in the extent
10706 * tree).
10707 * 1 unit for deleting the free space item (located in tree of tree
10708 * roots).
10709 * N units for deleting N device extent items corresponding to each
10710 * stripe (located in the device tree).
10711 *
10712 * In order to remove a block group we also need to reserve units in the
10713 * system space info in order to update the chunk tree (update one or
10714 * more device items and remove one chunk item), but this is done at
10715 * btrfs_remove_chunk() through a call to check_system_chunk().
Filipe Manana8eab77f2015-11-13 23:57:16 +000010716 */
Jeff Mahoney95617d62015-06-03 10:55:48 -040010717 map = em->map_lookup;
Filipe Manana7fd01182015-11-13 23:57:17 +000010718 num_items = 3 + map->num_stripes;
10719 free_extent_map(em);
10720
Filipe Manana8eab77f2015-11-13 23:57:16 +000010721 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
Filipe Manana7fd01182015-11-13 23:57:17 +000010722 num_items, 1);
Filipe Manana8eab77f2015-11-13 23:57:16 +000010723}
10724
Josef Bacik47ab2a62014-09-18 11:20:02 -040010725/*
10726 * Process the unused_bgs list and remove any that don't have any allocated
10727 * space inside of them.
10728 */
10729void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10730{
10731 struct btrfs_block_group_cache *block_group;
10732 struct btrfs_space_info *space_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010733 struct btrfs_trans_handle *trans;
10734 int ret = 0;
10735
Josef Bacikafcdd122016-09-02 15:40:02 -040010736 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
Josef Bacik47ab2a62014-09-18 11:20:02 -040010737 return;
10738
10739 spin_lock(&fs_info->unused_bgs_lock);
10740 while (!list_empty(&fs_info->unused_bgs)) {
10741 u64 start, end;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010742 int trimming;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010743
10744 block_group = list_first_entry(&fs_info->unused_bgs,
10745 struct btrfs_block_group_cache,
10746 bg_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010747 list_del_init(&block_group->bg_list);
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010748
10749 space_info = block_group->space_info;
10750
Josef Bacik47ab2a62014-09-18 11:20:02 -040010751 if (ret || btrfs_mixed_space_info(space_info)) {
10752 btrfs_put_block_group(block_group);
10753 continue;
10754 }
10755 spin_unlock(&fs_info->unused_bgs_lock);
10756
Zhao Leid5f2e332015-10-08 18:46:44 +080010757 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +010010758
Josef Bacik47ab2a62014-09-18 11:20:02 -040010759 /* Don't want to race with allocators so take the groups_sem */
10760 down_write(&space_info->groups_sem);
10761 spin_lock(&block_group->lock);
10762 if (block_group->reserved ||
10763 btrfs_block_group_used(&block_group->item) ||
Chris Mason19c4d2f2016-10-10 13:43:31 -070010764 block_group->ro ||
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010765 list_is_singular(&block_group->list)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -040010766 /*
10767 * We want to bail if we made new allocations or have
10768 * outstanding allocations in this block group. We do
10769 * the ro check in case balance is currently acting on
10770 * this block group.
10771 */
10772 spin_unlock(&block_group->lock);
10773 up_write(&space_info->groups_sem);
10774 goto next;
10775 }
10776 spin_unlock(&block_group->lock);
10777
10778 /* We don't want to force the issue, only flip if it's ok. */
Zhaolei868f4012015-08-05 16:43:27 +080010779 ret = inc_block_group_ro(block_group, 0);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010780 up_write(&space_info->groups_sem);
10781 if (ret < 0) {
10782 ret = 0;
10783 goto next;
10784 }
10785
10786 /*
10787 * Want to do this before we do anything else so we can recover
10788 * properly if we fail to join the transaction.
10789 */
Filipe Manana7fd01182015-11-13 23:57:17 +000010790 trans = btrfs_start_trans_remove_block_group(fs_info,
10791 block_group->key.objectid);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010792 if (IS_ERR(trans)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010793 btrfs_dec_block_group_ro(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010794 ret = PTR_ERR(trans);
10795 goto next;
10796 }
10797
10798 /*
10799 * We could have pending pinned extents for this block group,
10800 * just delete them, we don't care about them anymore.
10801 */
10802 start = block_group->key.objectid;
10803 end = start + block_group->key.offset - 1;
Filipe Mananad4b450c2015-01-29 19:18:25 +000010804 /*
10805 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10806 * btrfs_finish_extent_commit(). If we are at transaction N,
10807 * another task might be running finish_extent_commit() for the
10808 * previous transaction N - 1, and have seen a range belonging
10809 * to the block group in freed_extents[] before we were able to
10810 * clear the whole block group range from freed_extents[]. This
10811 * means that task can lookup for the block group after we
10812 * unpinned it from freed_extents[] and removed it, leading to
10813 * a BUG_ON() at btrfs_unpin_extent_range().
10814 */
10815 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana758eb512014-11-03 14:08:39 +000010816 ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
David Sterba91166212016-04-26 23:54:39 +020010817 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010818 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010819 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010820 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010821 goto end_trans;
10822 }
10823 ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
David Sterba91166212016-04-26 23:54:39 +020010824 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010825 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010826 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010827 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010828 goto end_trans;
10829 }
Filipe Mananad4b450c2015-01-29 19:18:25 +000010830 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010831
10832 /* Reset pinned so btrfs_put_block_group doesn't complain */
Zhao Leic30666d2015-02-25 14:17:20 +080010833 spin_lock(&space_info->lock);
10834 spin_lock(&block_group->lock);
10835
10836 space_info->bytes_pinned -= block_group->pinned;
10837 space_info->bytes_readonly += block_group->pinned;
10838 percpu_counter_add(&space_info->total_bytes_pinned,
10839 -block_group->pinned);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010840 block_group->pinned = 0;
10841
Zhao Leic30666d2015-02-25 14:17:20 +080010842 spin_unlock(&block_group->lock);
10843 spin_unlock(&space_info->lock);
10844
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010845 /* DISCARD can flip during remount */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010846 trimming = btrfs_test_opt(fs_info, DISCARD);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010847
10848 /* Implicit trim during transaction commit. */
10849 if (trimming)
10850 btrfs_get_block_group_trimming(block_group);
10851
Josef Bacik47ab2a62014-09-18 11:20:02 -040010852 /*
10853 * Btrfs_remove_chunk will abort the transaction if things go
10854 * horribly wrong.
10855 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010856 ret = btrfs_remove_chunk(trans, fs_info,
Josef Bacik47ab2a62014-09-18 11:20:02 -040010857 block_group->key.objectid);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010858
10859 if (ret) {
10860 if (trimming)
10861 btrfs_put_block_group_trimming(block_group);
10862 goto end_trans;
10863 }
10864
10865 /*
10866 * If we're not mounted with -odiscard, we can just forget
10867 * about this block group. Otherwise we'll need to wait
10868 * until transaction commit to do the actual discard.
10869 */
10870 if (trimming) {
Filipe Manana348a0012015-11-27 12:16:16 +000010871 spin_lock(&fs_info->unused_bgs_lock);
10872 /*
10873 * A concurrent scrub might have added us to the list
10874 * fs_info->unused_bgs, so use a list_move operation
10875 * to add the block group to the deleted_bgs list.
10876 */
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010877 list_move(&block_group->bg_list,
10878 &trans->transaction->deleted_bgs);
Filipe Manana348a0012015-11-27 12:16:16 +000010879 spin_unlock(&fs_info->unused_bgs_lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010880 btrfs_get_block_group(block_group);
10881 }
Filipe Manana758eb512014-11-03 14:08:39 +000010882end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010883 btrfs_end_transaction(trans);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010884next:
Zhao Leid5f2e332015-10-08 18:46:44 +080010885 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010886 btrfs_put_block_group(block_group);
10887 spin_lock(&fs_info->unused_bgs_lock);
10888 }
10889 spin_unlock(&fs_info->unused_bgs_lock);
10890}
10891
liuboc59021f2011-03-07 02:13:14 +000010892int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
10893{
10894 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +000010895 struct btrfs_super_block *disk_super;
10896 u64 features;
10897 u64 flags;
10898 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +000010899 int ret;
10900
David Sterba6c417612011-04-13 15:41:04 +020010901 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +000010902 if (!btrfs_super_root(disk_super))
Dan Carpenter0dc924c52016-01-13 15:21:17 +030010903 return -EINVAL;
liuboc59021f2011-03-07 02:13:14 +000010904
liubo1aba86d2011-04-08 08:44:37 +000010905 features = btrfs_super_incompat_flags(disk_super);
10906 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
10907 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +000010908
liubo1aba86d2011-04-08 08:44:37 +000010909 flags = BTRFS_BLOCK_GROUP_SYSTEM;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010910 ret = create_space_info(fs_info, flags, &space_info);
liuboc59021f2011-03-07 02:13:14 +000010911 if (ret)
liubo1aba86d2011-04-08 08:44:37 +000010912 goto out;
liuboc59021f2011-03-07 02:13:14 +000010913
liubo1aba86d2011-04-08 08:44:37 +000010914 if (mixed) {
10915 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010916 ret = create_space_info(fs_info, flags, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010917 } else {
10918 flags = BTRFS_BLOCK_GROUP_METADATA;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010919 ret = create_space_info(fs_info, flags, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010920 if (ret)
10921 goto out;
10922
10923 flags = BTRFS_BLOCK_GROUP_DATA;
Nikolay Borisov2be12ef2017-05-22 09:35:49 +030010924 ret = create_space_info(fs_info, flags, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010925 }
10926out:
liuboc59021f2011-03-07 02:13:14 +000010927 return ret;
10928}
10929
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010930int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
10931 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +080010932{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010933 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +080010934}
10935
Jeff Mahoney499f3772015-06-15 09:41:17 -040010936/*
10937 * It used to be that old block groups would be left around forever.
10938 * Iterating over them would be enough to trim unused space. Since we
10939 * now automatically remove them, we also need to iterate over unallocated
10940 * space.
10941 *
10942 * We don't want a transaction for this since the discard may take a
10943 * substantial amount of time. We don't require that a transaction be
10944 * running, but we do need to take a running transaction into account
10945 * to ensure that we're not discarding chunks that were released in
10946 * the current transaction.
10947 *
10948 * Holding the chunks lock will prevent other threads from allocating
10949 * or releasing chunks, but it won't prevent a running transaction
10950 * from committing and releasing the memory that the pending chunks
10951 * list head uses. For that, we need to take a reference to the
10952 * transaction.
10953 */
10954static int btrfs_trim_free_extents(struct btrfs_device *device,
10955 u64 minlen, u64 *trimmed)
10956{
10957 u64 start = 0, len = 0;
10958 int ret;
10959
10960 *trimmed = 0;
10961
10962 /* Not writeable = nothing to do. */
10963 if (!device->writeable)
10964 return 0;
10965
10966 /* No free space = nothing to do. */
10967 if (device->total_bytes <= device->bytes_used)
10968 return 0;
10969
10970 ret = 0;
10971
10972 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -040010973 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010974 struct btrfs_transaction *trans;
10975 u64 bytes;
10976
10977 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
10978 if (ret)
10979 return ret;
10980
10981 down_read(&fs_info->commit_root_sem);
10982
10983 spin_lock(&fs_info->trans_lock);
10984 trans = fs_info->running_transaction;
10985 if (trans)
Elena Reshetova9b64f572017-03-03 10:55:11 +020010986 refcount_inc(&trans->use_count);
Jeff Mahoney499f3772015-06-15 09:41:17 -040010987 spin_unlock(&fs_info->trans_lock);
10988
10989 ret = find_free_dev_extent_start(trans, device, minlen, start,
10990 &start, &len);
10991 if (trans)
10992 btrfs_put_transaction(trans);
10993
10994 if (ret) {
10995 up_read(&fs_info->commit_root_sem);
10996 mutex_unlock(&fs_info->chunk_mutex);
10997 if (ret == -ENOSPC)
10998 ret = 0;
10999 break;
11000 }
11001
11002 ret = btrfs_issue_discard(device->bdev, start, len, &bytes);
11003 up_read(&fs_info->commit_root_sem);
11004 mutex_unlock(&fs_info->chunk_mutex);
11005
11006 if (ret)
11007 break;
11008
11009 start += len;
11010 *trimmed += bytes;
11011
11012 if (fatal_signal_pending(current)) {
11013 ret = -ERESTARTSYS;
11014 break;
11015 }
11016
11017 cond_resched();
11018 }
11019
11020 return ret;
11021}
11022
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040011023int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +000011024{
Li Dongyangf7039b12011-03-24 10:24:28 +000011025 struct btrfs_block_group_cache *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -040011026 struct btrfs_device *device;
11027 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +000011028 u64 group_trimmed;
11029 u64 start;
11030 u64 end;
11031 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +080011032 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +000011033 int ret = 0;
11034
Liu Bo2cac13e2012-02-09 18:17:41 +080011035 /*
11036 * try to trim all FS space, our block group may start from non-zero.
11037 */
11038 if (range->len == total_bytes)
11039 cache = btrfs_lookup_first_block_group(fs_info, range->start);
11040 else
11041 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +000011042
11043 while (cache) {
11044 if (cache->key.objectid >= (range->start + range->len)) {
11045 btrfs_put_block_group(cache);
11046 break;
11047 }
11048
11049 start = max(range->start, cache->key.objectid);
11050 end = min(range->start + range->len,
11051 cache->key.objectid + cache->key.offset);
11052
11053 if (end - start >= range->minlen) {
11054 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +000011055 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -040011056 if (ret) {
11057 btrfs_put_block_group(cache);
11058 break;
11059 }
11060 ret = wait_block_group_cache_done(cache);
11061 if (ret) {
11062 btrfs_put_block_group(cache);
11063 break;
11064 }
Li Dongyangf7039b12011-03-24 10:24:28 +000011065 }
11066 ret = btrfs_trim_block_group(cache,
11067 &group_trimmed,
11068 start,
11069 end,
11070 range->minlen);
11071
11072 trimmed += group_trimmed;
11073 if (ret) {
11074 btrfs_put_block_group(cache);
11075 break;
11076 }
11077 }
11078
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040011079 cache = next_block_group(fs_info, cache);
Li Dongyangf7039b12011-03-24 10:24:28 +000011080 }
11081
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011082 mutex_lock(&fs_info->fs_devices->device_list_mutex);
11083 devices = &fs_info->fs_devices->alloc_list;
Jeff Mahoney499f3772015-06-15 09:41:17 -040011084 list_for_each_entry(device, devices, dev_alloc_list) {
11085 ret = btrfs_trim_free_extents(device, range->minlen,
11086 &group_trimmed);
11087 if (ret)
11088 break;
11089
11090 trimmed += group_trimmed;
11091 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040011092 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -040011093
Li Dongyangf7039b12011-03-24 10:24:28 +000011094 range->len = trimmed;
11095 return ret;
11096}
Miao Xie8257b2d2014-03-06 13:38:19 +080011097
11098/*
David Sterbaea14b57f2017-06-22 02:19:11 +020011099 * btrfs_{start,end}_write_no_snapshotting() are similar to
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011100 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
11101 * data into the page cache through nocow before the subvolume is snapshoted,
11102 * but flush the data into disk after the snapshot creation, or to prevent
David Sterbaea14b57f2017-06-22 02:19:11 +020011103 * operations while snapshotting is ongoing and that cause the snapshot to be
Filipe Manana9ea24bb2014-10-29 11:57:59 +000011104 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +080011105 */
David Sterbaea14b57f2017-06-22 02:19:11 +020011106void btrfs_end_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011107{
11108 percpu_counter_dec(&root->subv_writers->counter);
11109 /*
David Sterbaa83342a2015-02-16 19:36:47 +010011110 * Make sure counter is updated before we wake up waiters.
Miao Xie8257b2d2014-03-06 13:38:19 +080011111 */
11112 smp_mb();
11113 if (waitqueue_active(&root->subv_writers->wait))
11114 wake_up(&root->subv_writers->wait);
11115}
11116
David Sterbaea14b57f2017-06-22 02:19:11 +020011117int btrfs_start_write_no_snapshotting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080011118{
David Sterbaea14b57f2017-06-22 02:19:11 +020011119 if (atomic_read(&root->will_be_snapshotted))
Miao Xie8257b2d2014-03-06 13:38:19 +080011120 return 0;
11121
11122 percpu_counter_inc(&root->subv_writers->counter);
11123 /*
11124 * Make sure counter is updated before we check for snapshot creation.
11125 */
11126 smp_mb();
David Sterbaea14b57f2017-06-22 02:19:11 +020011127 if (atomic_read(&root->will_be_snapshotted)) {
11128 btrfs_end_write_no_snapshotting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +080011129 return 0;
11130 }
11131 return 1;
11132}
Zhao Lei0bc19f902016-01-06 18:56:36 +080011133
David Sterbaea14b57f2017-06-22 02:19:11 +020011134static int wait_snapshotting_atomic_t(atomic_t *a)
Zhao Lei0bc19f902016-01-06 18:56:36 +080011135{
11136 schedule();
11137 return 0;
11138}
11139
11140void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
11141{
11142 while (true) {
11143 int ret;
11144
David Sterbaea14b57f2017-06-22 02:19:11 +020011145 ret = btrfs_start_write_no_snapshotting(root);
Zhao Lei0bc19f902016-01-06 18:56:36 +080011146 if (ret)
11147 break;
David Sterbaea14b57f2017-06-22 02:19:11 +020011148 wait_on_atomic_t(&root->will_be_snapshotted,
11149 wait_snapshotting_atomic_t,
Zhao Lei0bc19f902016-01-06 18:56:36 +080011150 TASK_UNINTERRUPTIBLE);
11151 }
11152}