blob: 1b9ede123ffb7bf356e5ba70eddc3ff6f1c6bd92 [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"
Chris Masonfec577f2007-02-26 10:40:21 -050041
Arne Jansen709c0482011-09-12 12:22:57 +020042#undef SCRAMBLE_DELAYED_REFS
43
Miao Xie9e622d62012-01-26 15:01:12 -050044/*
45 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040046 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
47 * if we really need one.
48 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040049 * CHUNK_ALLOC_LIMITED means to only try and allocate one
50 * if we have very few chunks already allocated. This is
51 * used as part of the clustering code to help make sure
52 * we have a good pool of storage to cluster in, without
53 * filling the FS with empty chunks
54 *
Miao Xie9e622d62012-01-26 15:01:12 -050055 * CHUNK_ALLOC_FORCE means it must try to allocate one
56 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040057 */
58enum {
59 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050060 CHUNK_ALLOC_LIMITED = 1,
61 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040062};
63
Josef Bacikce93ec52014-11-17 15:45:48 -050064static int update_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6202df62016-06-22 18:54:22 -040065 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacikce93ec52014-11-17 15:45:48 -050066 u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040067static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040068 struct btrfs_fs_info *fs_info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080069 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040070 u64 root_objectid, u64 owner_objectid,
71 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080072 struct btrfs_delayed_extent_op *extra_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040073static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
74 struct extent_buffer *leaf,
75 struct btrfs_extent_item *ei);
76static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040077 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040078 u64 parent, u64 root_objectid,
79 u64 flags, u64 owner, u64 offset,
80 struct btrfs_key *ins, int ref_mod);
81static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040082 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040083 u64 parent, u64 root_objectid,
84 u64 flags, struct btrfs_disk_key *key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +010085 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050086static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040087 struct btrfs_fs_info *fs_info, u64 flags,
Josef Bacik698d0082012-09-12 14:08:47 -040088 int force);
Yan Zheng11833d62009-09-11 16:11:19 -040089static int find_next_key(struct btrfs_path *path, int level,
90 struct btrfs_key *key);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -040091static void dump_space_info(struct btrfs_fs_info *fs_info,
92 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -040093 int dump_block_groups);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +080094static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
Wang Xiaoguang18513092016-07-25 15:51:40 +080095 u64 ram_bytes, u64 num_bytes, int delalloc);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +080096static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
97 u64 num_bytes, int delalloc);
Josef Bacik5d803662013-02-07 16:06:02 -050098static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
99 u64 num_bytes);
Josef Bacik957780e2016-05-17 13:30:55 -0400100static int __reserve_metadata_bytes(struct btrfs_root *root,
101 struct btrfs_space_info *space_info,
102 u64 orig_bytes,
103 enum btrfs_reserve_flush_enum flush);
104static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
105 struct btrfs_space_info *space_info,
106 u64 num_bytes);
107static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
108 struct btrfs_space_info *space_info,
109 u64 num_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -0500110
Josef Bacik817d52f2009-07-13 21:29:25 -0400111static noinline int
112block_group_cache_done(struct btrfs_block_group_cache *cache)
113{
114 smp_mb();
Josef Bacik36cce922013-08-05 11:15:21 -0400115 return cache->cached == BTRFS_CACHE_FINISHED ||
116 cache->cached == BTRFS_CACHE_ERROR;
Josef Bacik817d52f2009-07-13 21:29:25 -0400117}
118
Josef Bacik0f9dd462008-09-23 13:14:11 -0400119static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
120{
121 return (cache->flags & bits) == bits;
122}
123
Filipe Manana758f2df2015-11-19 11:45:48 +0000124void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000125{
126 atomic_inc(&cache->count);
127}
128
129void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
130{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400131 if (atomic_dec_and_test(&cache->count)) {
132 WARN_ON(cache->pinned > 0);
133 WARN_ON(cache->reserved > 0);
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
Josef Bacikd53ba472012-04-12 16:03:57 -0400644 if (fs_info->mount_opt & BTRFS_MOUNT_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
Chris Mason4184ea72009-03-10 12:39:20 -0400759/*
760 * after adding space to the filesystem, we need to clear the full flags
761 * on all the space infos.
762 */
763void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
764{
765 struct list_head *head = &info->space_info;
766 struct btrfs_space_info *found;
767
768 rcu_read_lock();
769 list_for_each_entry_rcu(found, head, list)
770 found->full = 0;
771 rcu_read_unlock();
772}
773
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000774/* simple helper to search for an existing data extent at a given offset */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400775int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400776{
777 int ret;
778 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400779 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400780
Zheng Yan31840ae2008-09-23 13:14:14 -0400781 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700782 if (!path)
783 return -ENOMEM;
784
Chris Masone02119d2008-09-05 16:13:11 -0400785 key.objectid = start;
786 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500787 key.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400788 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400789 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500790 return ret;
791}
792
Chris Masond8d5f3e2007-12-11 12:42:00 -0500793/*
Josef Bacik3173a182013-03-07 14:22:04 -0500794 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400795 *
796 * the head node for delayed ref is used to store the sum of all the
797 * reference count modifications queued up in the rbtree. the head
798 * node may also store the extent flags to set. This way you can check
799 * to see what the reference count and extent flags would be if all of
800 * the delayed refs are not processed.
801 */
802int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400803 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500804 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400805{
806 struct btrfs_delayed_ref_head *head;
807 struct btrfs_delayed_ref_root *delayed_refs;
808 struct btrfs_path *path;
809 struct btrfs_extent_item *ei;
810 struct extent_buffer *leaf;
811 struct btrfs_key key;
812 u32 item_size;
813 u64 num_refs;
814 u64 extent_flags;
815 int ret;
816
Josef Bacik3173a182013-03-07 14:22:04 -0500817 /*
818 * If we don't have skinny metadata, don't bother doing anything
819 * different
820 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400821 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
822 offset = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500823 metadata = 0;
824 }
825
Yan, Zhenga22285a2010-05-16 10:48:46 -0400826 path = btrfs_alloc_path();
827 if (!path)
828 return -ENOMEM;
829
Yan, Zhenga22285a2010-05-16 10:48:46 -0400830 if (!trans) {
831 path->skip_locking = 1;
832 path->search_commit_root = 1;
833 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000834
835search_again:
836 key.objectid = bytenr;
837 key.offset = offset;
838 if (metadata)
839 key.type = BTRFS_METADATA_ITEM_KEY;
840 else
841 key.type = BTRFS_EXTENT_ITEM_KEY;
842
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400843 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400844 if (ret < 0)
845 goto out_free;
846
Josef Bacik3173a182013-03-07 14:22:04 -0500847 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100848 if (path->slots[0]) {
849 path->slots[0]--;
850 btrfs_item_key_to_cpu(path->nodes[0], &key,
851 path->slots[0]);
852 if (key.objectid == bytenr &&
853 key.type == BTRFS_EXTENT_ITEM_KEY &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400854 key.offset == fs_info->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100855 ret = 0;
856 }
Josef Bacik3173a182013-03-07 14:22:04 -0500857 }
858
Yan, Zhenga22285a2010-05-16 10:48:46 -0400859 if (ret == 0) {
860 leaf = path->nodes[0];
861 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
862 if (item_size >= sizeof(*ei)) {
863 ei = btrfs_item_ptr(leaf, path->slots[0],
864 struct btrfs_extent_item);
865 num_refs = btrfs_extent_refs(leaf, ei);
866 extent_flags = btrfs_extent_flags(leaf, ei);
867 } else {
868#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
869 struct btrfs_extent_item_v0 *ei0;
870 BUG_ON(item_size != sizeof(*ei0));
871 ei0 = btrfs_item_ptr(leaf, path->slots[0],
872 struct btrfs_extent_item_v0);
873 num_refs = btrfs_extent_refs_v0(leaf, ei0);
874 /* FIXME: this isn't correct for data */
875 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
876#else
877 BUG();
878#endif
879 }
880 BUG_ON(num_refs == 0);
881 } else {
882 num_refs = 0;
883 extent_flags = 0;
884 ret = 0;
885 }
886
887 if (!trans)
888 goto out;
889
890 delayed_refs = &trans->transaction->delayed_refs;
891 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -0800892 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400893 if (head) {
894 if (!mutex_trylock(&head->mutex)) {
Elena Reshetova6df8cdf2017-03-03 10:55:15 +0200895 refcount_inc(&head->node.refs);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400896 spin_unlock(&delayed_refs->lock);
897
David Sterbab3b4aa72011-04-21 01:20:15 +0200898 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400899
David Sterba8cc33e52011-05-02 15:29:25 +0200900 /*
901 * Mutex was contended, block until it's released and try
902 * again
903 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400904 mutex_lock(&head->mutex);
905 mutex_unlock(&head->mutex);
906 btrfs_put_delayed_ref(&head->node);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000907 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400908 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500909 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400910 if (head->extent_op && head->extent_op->update_flags)
911 extent_flags |= head->extent_op->flags_to_set;
912 else
913 BUG_ON(num_refs == 0);
914
915 num_refs += head->node.ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500916 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400917 mutex_unlock(&head->mutex);
918 }
919 spin_unlock(&delayed_refs->lock);
920out:
921 WARN_ON(num_refs == 0);
922 if (refs)
923 *refs = num_refs;
924 if (flags)
925 *flags = extent_flags;
926out_free:
927 btrfs_free_path(path);
928 return ret;
929}
930
931/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500932 * Back reference rules. Back refs have three main goals:
933 *
934 * 1) differentiate between all holders of references to an extent so that
935 * when a reference is dropped we can make sure it was a valid reference
936 * before freeing the extent.
937 *
938 * 2) Provide enough information to quickly find the holders of an extent
939 * if we notice a given block is corrupted or bad.
940 *
941 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
942 * maintenance. This is actually the same as #2, but with a slightly
943 * different use case.
944 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945 * There are two kinds of back refs. The implicit back refs is optimized
946 * for pointers in non-shared tree blocks. For a given pointer in a block,
947 * back refs of this kind provide information about the block's owner tree
948 * and the pointer's key. These information allow us to find the block by
949 * b-tree searching. The full back refs is for pointers in tree blocks not
950 * referenced by their owner trees. The location of tree block is recorded
951 * in the back refs. Actually the full back refs is generic, and can be
952 * used in all cases the implicit back refs is used. The major shortcoming
953 * of the full back refs is its overhead. Every time a tree block gets
954 * COWed, we have to update back refs entry for all pointers in it.
955 *
956 * For a newly allocated tree block, we use implicit back refs for
957 * pointers in it. This means most tree related operations only involve
958 * implicit back refs. For a tree block created in old transaction, the
959 * only way to drop a reference to it is COW it. So we can detect the
960 * event that tree block loses its owner tree's reference and do the
961 * back refs conversion.
962 *
Nicholas D Steeves01327612016-05-19 21:18:45 -0400963 * When a tree block is COWed through a tree, there are four cases:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400964 *
965 * The reference count of the block is one and the tree is the block's
966 * owner tree. Nothing to do in this case.
967 *
968 * The reference count of the block is one and the tree is not the
969 * block's owner tree. In this case, full back refs is used for pointers
970 * in the block. Remove these full back refs, add implicit back refs for
971 * every pointers in the new block.
972 *
973 * The reference count of the block is greater than one and the tree is
974 * the block's owner tree. In this case, implicit back refs is used for
975 * pointers in the block. Add full back refs for every pointers in the
976 * block, increase lower level extents' reference counts. The original
977 * implicit back refs are entailed to the new block.
978 *
979 * The reference count of the block is greater than one and the tree is
980 * not the block's owner tree. Add implicit back refs for every pointer in
981 * the new block, increase lower level extents' reference count.
982 *
983 * Back Reference Key composing:
984 *
985 * The key objectid corresponds to the first byte in the extent,
986 * The key type is used to differentiate between types of back refs.
987 * There are different meanings of the key offset for different types
988 * of back refs.
989 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500990 * File extents can be referenced by:
991 *
992 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400993 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500994 * - different offsets inside a file (bookend extents in file.c)
995 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400996 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500997 *
998 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500999 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001000 * - original offset in the file
1001 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -04001002 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001003 * The key offset for the implicit back refs is hash of the first
1004 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001005 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001006 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001007 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001008 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001009 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001010 * The key offset for the implicit back refs is the first byte of
1011 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001012 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001013 * When a file extent is allocated, The implicit back refs is used.
1014 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001015 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001016 * (root_key.objectid, inode objectid, offset in file, 1)
1017 *
1018 * When a file extent is removed file truncation, we find the
1019 * corresponding implicit back refs and check the following fields:
1020 *
1021 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -05001022 *
1023 * Btree extents can be referenced by:
1024 *
1025 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -05001026 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001027 * Both the implicit back refs and the full back refs for tree blocks
1028 * only consist of key. The key offset for the implicit back refs is
1029 * objectid of block's owner tree. The key offset for the full back refs
1030 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001031 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001032 * When implicit back refs is used, information about the lowest key and
1033 * level of the tree block are required. These information are stored in
1034 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001035 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001036
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1038static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001039 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001040 struct btrfs_path *path,
1041 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -05001042{
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001043 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001044 struct btrfs_extent_item *item;
1045 struct btrfs_extent_item_v0 *ei0;
1046 struct btrfs_extent_ref_v0 *ref0;
1047 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -04001048 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001049 struct btrfs_key key;
1050 struct btrfs_key found_key;
1051 u32 new_size = sizeof(*item);
1052 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -05001053 int ret;
1054
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001055 leaf = path->nodes[0];
1056 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -05001057
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001058 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1059 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1060 struct btrfs_extent_item_v0);
1061 refs = btrfs_extent_refs_v0(leaf, ei0);
1062
1063 if (owner == (u64)-1) {
1064 while (1) {
1065 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1066 ret = btrfs_next_leaf(root, path);
1067 if (ret < 0)
1068 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001069 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001070 leaf = path->nodes[0];
1071 }
1072 btrfs_item_key_to_cpu(leaf, &found_key,
1073 path->slots[0]);
1074 BUG_ON(key.objectid != found_key.objectid);
1075 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1076 path->slots[0]++;
1077 continue;
1078 }
1079 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1080 struct btrfs_extent_ref_v0);
1081 owner = btrfs_ref_objectid_v0(leaf, ref0);
1082 break;
1083 }
1084 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001085 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001086
1087 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1088 new_size += sizeof(*bi);
1089
1090 new_size -= sizeof(*ei0);
1091 ret = btrfs_search_slot(trans, root, &key, path,
1092 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001093 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001094 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001095 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001096
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001097 btrfs_extend_item(fs_info, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001098
1099 leaf = path->nodes[0];
1100 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1101 btrfs_set_extent_refs(leaf, item, refs);
1102 /* FIXME: get real generation */
1103 btrfs_set_extent_generation(leaf, item, 0);
1104 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1105 btrfs_set_extent_flags(leaf, item,
1106 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1107 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1108 bi = (struct btrfs_tree_block_info *)(item + 1);
1109 /* FIXME: get first key of the block */
David Sterbab159fa22016-11-08 18:09:03 +01001110 memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001111 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1112 } else {
1113 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1114 }
1115 btrfs_mark_buffer_dirty(leaf);
1116 return 0;
1117}
1118#endif
1119
1120static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1121{
1122 u32 high_crc = ~(u32)0;
1123 u32 low_crc = ~(u32)0;
1124 __le64 lenum;
1125
1126 lenum = cpu_to_le64(root_objectid);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001127 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001128 lenum = cpu_to_le64(owner);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001129 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001130 lenum = cpu_to_le64(offset);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001131 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001132
1133 return ((u64)high_crc << 31) ^ (u64)low_crc;
1134}
1135
1136static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1137 struct btrfs_extent_data_ref *ref)
1138{
1139 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1140 btrfs_extent_data_ref_objectid(leaf, ref),
1141 btrfs_extent_data_ref_offset(leaf, ref));
1142}
1143
1144static int match_extent_data_ref(struct extent_buffer *leaf,
1145 struct btrfs_extent_data_ref *ref,
1146 u64 root_objectid, u64 owner, u64 offset)
1147{
1148 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1149 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1150 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1151 return 0;
1152 return 1;
1153}
1154
1155static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001156 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001157 struct btrfs_path *path,
1158 u64 bytenr, u64 parent,
1159 u64 root_objectid,
1160 u64 owner, u64 offset)
1161{
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001162 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001163 struct btrfs_key key;
1164 struct btrfs_extent_data_ref *ref;
1165 struct extent_buffer *leaf;
1166 u32 nritems;
1167 int ret;
1168 int recow;
1169 int err = -ENOENT;
1170
1171 key.objectid = bytenr;
1172 if (parent) {
1173 key.type = BTRFS_SHARED_DATA_REF_KEY;
1174 key.offset = parent;
1175 } else {
1176 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1177 key.offset = hash_extent_data_ref(root_objectid,
1178 owner, offset);
1179 }
1180again:
1181 recow = 0;
1182 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1183 if (ret < 0) {
1184 err = ret;
1185 goto fail;
1186 }
1187
1188 if (parent) {
1189 if (!ret)
1190 return 0;
1191#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1192 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001193 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001194 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1195 if (ret < 0) {
1196 err = ret;
1197 goto fail;
1198 }
1199 if (!ret)
1200 return 0;
1201#endif
1202 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001203 }
1204
1205 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001206 nritems = btrfs_header_nritems(leaf);
1207 while (1) {
1208 if (path->slots[0] >= nritems) {
1209 ret = btrfs_next_leaf(root, path);
1210 if (ret < 0)
1211 err = ret;
1212 if (ret)
1213 goto fail;
1214
1215 leaf = path->nodes[0];
1216 nritems = btrfs_header_nritems(leaf);
1217 recow = 1;
1218 }
1219
1220 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1221 if (key.objectid != bytenr ||
1222 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1223 goto fail;
1224
1225 ref = btrfs_item_ptr(leaf, path->slots[0],
1226 struct btrfs_extent_data_ref);
1227
1228 if (match_extent_data_ref(leaf, ref, root_objectid,
1229 owner, offset)) {
1230 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001231 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001232 goto again;
1233 }
1234 err = 0;
1235 break;
1236 }
1237 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001238 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001239fail:
1240 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001241}
1242
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001243static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001244 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001245 struct btrfs_path *path,
1246 u64 bytenr, u64 parent,
1247 u64 root_objectid, u64 owner,
1248 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001249{
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001250 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04001251 struct btrfs_key key;
1252 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001253 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001254 u32 num_refs;
1255 int ret;
1256
1257 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001258 if (parent) {
1259 key.type = BTRFS_SHARED_DATA_REF_KEY;
1260 key.offset = parent;
1261 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001262 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001263 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1264 key.offset = hash_extent_data_ref(root_objectid,
1265 owner, offset);
1266 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001267 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001268
1269 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1270 if (ret && ret != -EEXIST)
1271 goto fail;
1272
1273 leaf = path->nodes[0];
1274 if (parent) {
1275 struct btrfs_shared_data_ref *ref;
1276 ref = btrfs_item_ptr(leaf, path->slots[0],
1277 struct btrfs_shared_data_ref);
1278 if (ret == 0) {
1279 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1280 } else {
1281 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1282 num_refs += refs_to_add;
1283 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1284 }
1285 } else {
1286 struct btrfs_extent_data_ref *ref;
1287 while (ret == -EEXIST) {
1288 ref = btrfs_item_ptr(leaf, path->slots[0],
1289 struct btrfs_extent_data_ref);
1290 if (match_extent_data_ref(leaf, ref, root_objectid,
1291 owner, offset))
1292 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001293 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001294 key.offset++;
1295 ret = btrfs_insert_empty_item(trans, root, path, &key,
1296 size);
1297 if (ret && ret != -EEXIST)
1298 goto fail;
1299
1300 leaf = path->nodes[0];
1301 }
1302 ref = btrfs_item_ptr(leaf, path->slots[0],
1303 struct btrfs_extent_data_ref);
1304 if (ret == 0) {
1305 btrfs_set_extent_data_ref_root(leaf, ref,
1306 root_objectid);
1307 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1308 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1309 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1310 } else {
1311 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1312 num_refs += refs_to_add;
1313 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1314 }
1315 }
1316 btrfs_mark_buffer_dirty(leaf);
1317 ret = 0;
1318fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001319 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001320 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001321}
1322
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001323static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001324 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001325 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -07001326 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001327{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001328 struct btrfs_key key;
1329 struct btrfs_extent_data_ref *ref1 = NULL;
1330 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001331 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001332 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001333 int ret = 0;
1334
1335 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001336 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1337
1338 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1339 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1340 struct btrfs_extent_data_ref);
1341 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1342 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1343 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1344 struct btrfs_shared_data_ref);
1345 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1346#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1347 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1348 struct btrfs_extent_ref_v0 *ref0;
1349 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1350 struct btrfs_extent_ref_v0);
1351 num_refs = btrfs_ref_count_v0(leaf, ref0);
1352#endif
1353 } else {
1354 BUG();
1355 }
1356
Chris Mason56bec292009-03-13 10:10:06 -04001357 BUG_ON(num_refs < refs_to_drop);
1358 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001359
Zheng Yan31840ae2008-09-23 13:14:14 -04001360 if (num_refs == 0) {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001361 ret = btrfs_del_item(trans, fs_info->extent_root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -07001362 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001363 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001364 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1365 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1366 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1367 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1368#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1369 else {
1370 struct btrfs_extent_ref_v0 *ref0;
1371 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1372 struct btrfs_extent_ref_v0);
1373 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1374 }
1375#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001376 btrfs_mark_buffer_dirty(leaf);
1377 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001378 return ret;
1379}
1380
Zhaolei9ed0dea2015-08-06 22:16:24 +08001381static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001382 struct btrfs_extent_inline_ref *iref)
1383{
1384 struct btrfs_key key;
1385 struct extent_buffer *leaf;
1386 struct btrfs_extent_data_ref *ref1;
1387 struct btrfs_shared_data_ref *ref2;
1388 u32 num_refs = 0;
1389
1390 leaf = path->nodes[0];
1391 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1392 if (iref) {
1393 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1394 BTRFS_EXTENT_DATA_REF_KEY) {
1395 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1396 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1397 } else {
1398 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1399 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1400 }
1401 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1402 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1403 struct btrfs_extent_data_ref);
1404 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1405 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1406 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1407 struct btrfs_shared_data_ref);
1408 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1409#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1410 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1411 struct btrfs_extent_ref_v0 *ref0;
1412 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1413 struct btrfs_extent_ref_v0);
1414 num_refs = btrfs_ref_count_v0(leaf, ref0);
1415#endif
1416 } else {
1417 WARN_ON(1);
1418 }
1419 return num_refs;
1420}
1421
1422static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001423 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001424 struct btrfs_path *path,
1425 u64 bytenr, u64 parent,
1426 u64 root_objectid)
1427{
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001428 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001429 struct btrfs_key key;
1430 int ret;
1431
1432 key.objectid = bytenr;
1433 if (parent) {
1434 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1435 key.offset = parent;
1436 } else {
1437 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1438 key.offset = root_objectid;
1439 }
1440
1441 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1442 if (ret > 0)
1443 ret = -ENOENT;
1444#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1445 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001446 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001447 key.type = BTRFS_EXTENT_REF_V0_KEY;
1448 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1449 if (ret > 0)
1450 ret = -ENOENT;
1451 }
1452#endif
1453 return ret;
1454}
1455
1456static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001457 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001458 struct btrfs_path *path,
1459 u64 bytenr, u64 parent,
1460 u64 root_objectid)
1461{
1462 struct btrfs_key key;
1463 int ret;
1464
1465 key.objectid = bytenr;
1466 if (parent) {
1467 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1468 key.offset = parent;
1469 } else {
1470 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1471 key.offset = root_objectid;
1472 }
1473
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001474 ret = btrfs_insert_empty_item(trans, fs_info->extent_root,
1475 path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001476 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001477 return ret;
1478}
1479
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001480static inline int extent_ref_type(u64 parent, u64 owner)
1481{
1482 int type;
1483 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1484 if (parent > 0)
1485 type = BTRFS_SHARED_BLOCK_REF_KEY;
1486 else
1487 type = BTRFS_TREE_BLOCK_REF_KEY;
1488 } else {
1489 if (parent > 0)
1490 type = BTRFS_SHARED_DATA_REF_KEY;
1491 else
1492 type = BTRFS_EXTENT_DATA_REF_KEY;
1493 }
1494 return type;
1495}
1496
Yan Zheng2c47e6052009-06-27 21:07:35 -04001497static int find_next_key(struct btrfs_path *path, int level,
1498 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001499
1500{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001501 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001502 if (!path->nodes[level])
1503 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001504 if (path->slots[level] + 1 >=
1505 btrfs_header_nritems(path->nodes[level]))
1506 continue;
1507 if (level == 0)
1508 btrfs_item_key_to_cpu(path->nodes[level], key,
1509 path->slots[level] + 1);
1510 else
1511 btrfs_node_key_to_cpu(path->nodes[level], key,
1512 path->slots[level] + 1);
1513 return 0;
1514 }
1515 return 1;
1516}
1517
1518/*
1519 * look for inline back ref. if back ref is found, *ref_ret is set
1520 * to the address of inline back ref, and 0 is returned.
1521 *
1522 * if back ref isn't found, *ref_ret is set to the address where it
1523 * should be inserted, and -ENOENT is returned.
1524 *
1525 * if insert is true and there are too many inline back refs, the path
1526 * points to the extent item, and -EAGAIN is returned.
1527 *
1528 * NOTE: inline back refs are ordered in the same way that back ref
1529 * items in the tree are ordered.
1530 */
1531static noinline_for_stack
1532int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001533 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001534 struct btrfs_path *path,
1535 struct btrfs_extent_inline_ref **ref_ret,
1536 u64 bytenr, u64 num_bytes,
1537 u64 parent, u64 root_objectid,
1538 u64 owner, u64 offset, int insert)
1539{
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001540 struct btrfs_root *root = fs_info->extent_root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001541 struct btrfs_key key;
1542 struct extent_buffer *leaf;
1543 struct btrfs_extent_item *ei;
1544 struct btrfs_extent_inline_ref *iref;
1545 u64 flags;
1546 u64 item_size;
1547 unsigned long ptr;
1548 unsigned long end;
1549 int extra_size;
1550 int type;
1551 int want;
1552 int ret;
1553 int err = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001554 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001555
1556 key.objectid = bytenr;
1557 key.type = BTRFS_EXTENT_ITEM_KEY;
1558 key.offset = num_bytes;
1559
1560 want = extent_ref_type(parent, owner);
1561 if (insert) {
1562 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001563 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001564 } else
1565 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001566
1567 /*
1568 * Owner is our parent level, so we can just add one to get the level
1569 * for the block we are interested in.
1570 */
1571 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1572 key.type = BTRFS_METADATA_ITEM_KEY;
1573 key.offset = owner;
1574 }
1575
1576again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001577 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1578 if (ret < 0) {
1579 err = ret;
1580 goto out;
1581 }
Josef Bacik3173a182013-03-07 14:22:04 -05001582
1583 /*
1584 * We may be a newly converted file system which still has the old fat
1585 * extent entries for metadata, so try and see if we have one of those.
1586 */
1587 if (ret > 0 && skinny_metadata) {
1588 skinny_metadata = false;
1589 if (path->slots[0]) {
1590 path->slots[0]--;
1591 btrfs_item_key_to_cpu(path->nodes[0], &key,
1592 path->slots[0]);
1593 if (key.objectid == bytenr &&
1594 key.type == BTRFS_EXTENT_ITEM_KEY &&
1595 key.offset == num_bytes)
1596 ret = 0;
1597 }
1598 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +01001599 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05001600 key.type = BTRFS_EXTENT_ITEM_KEY;
1601 key.offset = num_bytes;
1602 btrfs_release_path(path);
1603 goto again;
1604 }
1605 }
1606
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001607 if (ret && !insert) {
1608 err = -ENOENT;
1609 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301610 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -05001611 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -05001612 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001613 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001614
1615 leaf = path->nodes[0];
1616 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1617#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1618 if (item_size < sizeof(*ei)) {
1619 if (!insert) {
1620 err = -ENOENT;
1621 goto out;
1622 }
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001623 ret = convert_extent_item_v0(trans, fs_info, path, owner,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001624 extra_size);
1625 if (ret < 0) {
1626 err = ret;
1627 goto out;
1628 }
1629 leaf = path->nodes[0];
1630 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1631 }
1632#endif
1633 BUG_ON(item_size < sizeof(*ei));
1634
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001635 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1636 flags = btrfs_extent_flags(leaf, ei);
1637
1638 ptr = (unsigned long)(ei + 1);
1639 end = (unsigned long)ei + item_size;
1640
Josef Bacik3173a182013-03-07 14:22:04 -05001641 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001642 ptr += sizeof(struct btrfs_tree_block_info);
1643 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001644 }
1645
1646 err = -ENOENT;
1647 while (1) {
1648 if (ptr >= end) {
1649 WARN_ON(ptr > end);
1650 break;
1651 }
1652 iref = (struct btrfs_extent_inline_ref *)ptr;
1653 type = btrfs_extent_inline_ref_type(leaf, iref);
1654 if (want < type)
1655 break;
1656 if (want > type) {
1657 ptr += btrfs_extent_inline_ref_size(type);
1658 continue;
1659 }
1660
1661 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1662 struct btrfs_extent_data_ref *dref;
1663 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1664 if (match_extent_data_ref(leaf, dref, root_objectid,
1665 owner, offset)) {
1666 err = 0;
1667 break;
1668 }
1669 if (hash_extent_data_ref_item(leaf, dref) <
1670 hash_extent_data_ref(root_objectid, owner, offset))
1671 break;
1672 } else {
1673 u64 ref_offset;
1674 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1675 if (parent > 0) {
1676 if (parent == ref_offset) {
1677 err = 0;
1678 break;
1679 }
1680 if (ref_offset < parent)
1681 break;
1682 } else {
1683 if (root_objectid == ref_offset) {
1684 err = 0;
1685 break;
1686 }
1687 if (ref_offset < root_objectid)
1688 break;
1689 }
1690 }
1691 ptr += btrfs_extent_inline_ref_size(type);
1692 }
1693 if (err == -ENOENT && insert) {
1694 if (item_size + extra_size >=
1695 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1696 err = -EAGAIN;
1697 goto out;
1698 }
1699 /*
1700 * To add new inline back ref, we have to make sure
1701 * there is no corresponding back ref item.
1702 * For simplicity, we just do not add new inline back
1703 * ref if there is any kind of item for this block
1704 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001705 if (find_next_key(path, 0, &key) == 0 &&
1706 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001707 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001708 err = -EAGAIN;
1709 goto out;
1710 }
1711 }
1712 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1713out:
Yan Zheng85d41982009-06-11 08:51:10 -04001714 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001715 path->keep_locks = 0;
1716 btrfs_unlock_up_safe(path, 1);
1717 }
1718 return err;
1719}
1720
1721/*
1722 * helper to add new inline back ref
1723 */
1724static noinline_for_stack
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001725void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001726 struct btrfs_path *path,
1727 struct btrfs_extent_inline_ref *iref,
1728 u64 parent, u64 root_objectid,
1729 u64 owner, u64 offset, int refs_to_add,
1730 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001731{
1732 struct extent_buffer *leaf;
1733 struct btrfs_extent_item *ei;
1734 unsigned long ptr;
1735 unsigned long end;
1736 unsigned long item_offset;
1737 u64 refs;
1738 int size;
1739 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001740
1741 leaf = path->nodes[0];
1742 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1743 item_offset = (unsigned long)iref - (unsigned long)ei;
1744
1745 type = extent_ref_type(parent, owner);
1746 size = btrfs_extent_inline_ref_size(type);
1747
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001748 btrfs_extend_item(fs_info, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001749
1750 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1751 refs = btrfs_extent_refs(leaf, ei);
1752 refs += refs_to_add;
1753 btrfs_set_extent_refs(leaf, ei, refs);
1754 if (extent_op)
1755 __run_delayed_extent_op(extent_op, leaf, ei);
1756
1757 ptr = (unsigned long)ei + item_offset;
1758 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1759 if (ptr < end - size)
1760 memmove_extent_buffer(leaf, ptr + size, ptr,
1761 end - size - ptr);
1762
1763 iref = (struct btrfs_extent_inline_ref *)ptr;
1764 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1765 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1766 struct btrfs_extent_data_ref *dref;
1767 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1768 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1769 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1770 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1771 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1772 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1773 struct btrfs_shared_data_ref *sref;
1774 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1775 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1776 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1777 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1778 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1779 } else {
1780 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1781 }
1782 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001783}
1784
1785static int lookup_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001786 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001787 struct btrfs_path *path,
1788 struct btrfs_extent_inline_ref **ref_ret,
1789 u64 bytenr, u64 num_bytes, u64 parent,
1790 u64 root_objectid, u64 owner, u64 offset)
1791{
1792 int ret;
1793
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001794 ret = lookup_inline_extent_backref(trans, fs_info, path, ref_ret,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001795 bytenr, num_bytes, parent,
1796 root_objectid, owner, offset, 0);
1797 if (ret != -ENOENT)
1798 return ret;
1799
David Sterbab3b4aa72011-04-21 01:20:15 +02001800 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001801 *ref_ret = NULL;
1802
1803 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001804 ret = lookup_tree_block_ref(trans, fs_info, path, bytenr,
1805 parent, root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001806 } else {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001807 ret = lookup_extent_data_ref(trans, fs_info, path, bytenr,
1808 parent, root_objectid, owner,
1809 offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001810 }
1811 return ret;
1812}
1813
1814/*
1815 * helper to update/remove inline back ref
1816 */
1817static noinline_for_stack
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001818void update_inline_extent_backref(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001819 struct btrfs_path *path,
1820 struct btrfs_extent_inline_ref *iref,
1821 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001822 struct btrfs_delayed_extent_op *extent_op,
1823 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001824{
1825 struct extent_buffer *leaf;
1826 struct btrfs_extent_item *ei;
1827 struct btrfs_extent_data_ref *dref = NULL;
1828 struct btrfs_shared_data_ref *sref = NULL;
1829 unsigned long ptr;
1830 unsigned long end;
1831 u32 item_size;
1832 int size;
1833 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001834 u64 refs;
1835
1836 leaf = path->nodes[0];
1837 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1838 refs = btrfs_extent_refs(leaf, ei);
1839 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1840 refs += refs_to_mod;
1841 btrfs_set_extent_refs(leaf, ei, refs);
1842 if (extent_op)
1843 __run_delayed_extent_op(extent_op, leaf, ei);
1844
1845 type = btrfs_extent_inline_ref_type(leaf, iref);
1846
1847 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1848 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1849 refs = btrfs_extent_data_ref_count(leaf, dref);
1850 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1851 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1852 refs = btrfs_shared_data_ref_count(leaf, sref);
1853 } else {
1854 refs = 1;
1855 BUG_ON(refs_to_mod != -1);
1856 }
1857
1858 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1859 refs += refs_to_mod;
1860
1861 if (refs > 0) {
1862 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1863 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1864 else
1865 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1866 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001867 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001868 size = btrfs_extent_inline_ref_size(type);
1869 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1870 ptr = (unsigned long)iref;
1871 end = (unsigned long)ei + item_size;
1872 if (ptr + size < end)
1873 memmove_extent_buffer(leaf, ptr, ptr + size,
1874 end - ptr - size);
1875 item_size -= size;
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001876 btrfs_truncate_item(fs_info, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001877 }
1878 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001879}
1880
1881static noinline_for_stack
1882int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001883 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001884 struct btrfs_path *path,
1885 u64 bytenr, u64 num_bytes, u64 parent,
1886 u64 root_objectid, u64 owner,
1887 u64 offset, int refs_to_add,
1888 struct btrfs_delayed_extent_op *extent_op)
1889{
1890 struct btrfs_extent_inline_ref *iref;
1891 int ret;
1892
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001893 ret = lookup_inline_extent_backref(trans, fs_info, path, &iref,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001894 bytenr, num_bytes, parent,
1895 root_objectid, owner, offset, 1);
1896 if (ret == 0) {
1897 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001898 update_inline_extent_backref(fs_info, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001899 refs_to_add, extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001900 } else if (ret == -ENOENT) {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001901 setup_inline_extent_backref(fs_info, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001902 root_objectid, owner, offset,
1903 refs_to_add, extent_op);
1904 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001905 }
1906 return ret;
1907}
1908
1909static int insert_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001910 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001911 struct btrfs_path *path,
1912 u64 bytenr, u64 parent, u64 root_objectid,
1913 u64 owner, u64 offset, int refs_to_add)
1914{
1915 int ret;
1916 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1917 BUG_ON(refs_to_add != 1);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001918 ret = insert_tree_block_ref(trans, fs_info, path, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001919 parent, root_objectid);
1920 } else {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001921 ret = insert_extent_data_ref(trans, fs_info, path, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001922 parent, root_objectid,
1923 owner, offset, refs_to_add);
1924 }
1925 return ret;
1926}
1927
1928static int remove_extent_backref(struct btrfs_trans_handle *trans,
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001929 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001930 struct btrfs_path *path,
1931 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001932 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001933{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001934 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001935
1936 BUG_ON(!is_data && refs_to_drop != 1);
1937 if (iref) {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001938 update_inline_extent_backref(fs_info, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001939 -refs_to_drop, NULL, last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001940 } else if (is_data) {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001941 ret = remove_extent_data_ref(trans, fs_info, path, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07001942 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001943 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001944 *last_ref = 1;
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05001945 ret = btrfs_del_item(trans, fs_info->extent_root, path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001946 }
1947 return ret;
1948}
1949
Jeff Mahoney86557862015-06-15 09:41:16 -04001950#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001951static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
1952 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05001953{
Jeff Mahoney86557862015-06-15 09:41:16 -04001954 int j, ret = 0;
1955 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001956 u64 aligned_start = ALIGN(start, 1 << 9);
1957
1958 if (WARN_ON(start != aligned_start)) {
1959 len -= aligned_start - start;
1960 len = round_down(len, 1 << 9);
1961 start = aligned_start;
1962 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001963
1964 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04001965
1966 if (!len)
1967 return 0;
1968
1969 end = start + len;
1970 bytes_left = len;
1971
1972 /* Skip any superblocks on this device. */
1973 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
1974 u64 sb_start = btrfs_sb_offset(j);
1975 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
1976 u64 size = sb_start - start;
1977
1978 if (!in_range(sb_start, start, bytes_left) &&
1979 !in_range(sb_end, start, bytes_left) &&
1980 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
1981 continue;
1982
1983 /*
1984 * Superblock spans beginning of range. Adjust start and
1985 * try again.
1986 */
1987 if (sb_start <= start) {
1988 start += sb_end - start;
1989 if (start > end) {
1990 bytes_left = 0;
1991 break;
1992 }
1993 bytes_left = end - start;
1994 continue;
1995 }
1996
1997 if (size) {
1998 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
1999 GFP_NOFS, 0);
2000 if (!ret)
2001 *discarded_bytes += size;
2002 else if (ret != -EOPNOTSUPP)
2003 return ret;
2004 }
2005
2006 start = sb_end;
2007 if (start > end) {
2008 bytes_left = 0;
2009 break;
2010 }
2011 bytes_left = end - start;
2012 }
2013
2014 if (bytes_left) {
2015 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002016 GFP_NOFS, 0);
2017 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04002018 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002019 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002020 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05002021}
Chris Mason15916de2008-11-19 21:17:22 -05002022
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002023int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00002024 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05002025{
Liu Hui1f3c79a2009-01-05 15:57:51 -05002026 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00002027 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02002028 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002029
Christoph Hellwige244a0a2009-10-14 09:24:59 -04002030
Filipe Manana29992412016-05-27 17:42:05 +01002031 /*
2032 * Avoid races with device replace and make sure our bbio has devices
2033 * associated to its stripes that don't go away while we are discarding.
2034 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002035 btrfs_bio_counter_inc_blocked(fs_info);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002036 /* Tell the block device(s) that the sectors can be discarded */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002037 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
2038 &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002039 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05002040 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02002041 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002042 int i;
2043
Liu Hui1f3c79a2009-01-05 15:57:51 -05002044
Jan Schmidta1d3c472011-08-04 17:15:33 +02002045 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002046 u64 bytes;
Josef Bacikd5e20032011-08-04 14:52:27 +00002047 if (!stripe->dev->can_discard)
2048 continue;
2049
Li Dongyang5378e602011-03-24 10:24:27 +00002050 ret = btrfs_issue_discard(stripe->dev->bdev,
2051 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002052 stripe->length,
2053 &bytes);
Li Dongyang5378e602011-03-24 10:24:27 +00002054 if (!ret)
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002055 discarded_bytes += bytes;
Li Dongyang5378e602011-03-24 10:24:27 +00002056 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002057 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00002058
2059 /*
2060 * Just in case we get back EOPNOTSUPP for some reason,
2061 * just ignore the return value so we don't screw up
2062 * people calling discard_extent.
2063 */
2064 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002065 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08002066 btrfs_put_bbio(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002067 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002068 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00002069
2070 if (actual_bytes)
2071 *actual_bytes = discarded_bytes;
2072
Liu Hui1f3c79a2009-01-05 15:57:51 -05002073
David Woodhouse53b381b2013-01-29 18:40:14 -05002074 if (ret == -EOPNOTSUPP)
2075 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002076 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002077}
2078
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002079/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002080int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002081 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002082 u64 bytenr, u64 num_bytes, u64 parent,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002083 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04002084{
2085 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002086
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002087 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
2088 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04002089
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002090 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002091 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
2092 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002093 parent, root_objectid, (int)owner,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002094 BTRFS_ADD_DELAYED_REF, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002095 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002096 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002097 num_bytes, parent, root_objectid,
2098 owner, offset, 0,
Jeff Mahoneyfef394f2016-12-13 14:39:34 -05002099 BTRFS_ADD_DELAYED_REF);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002100 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002101 return ret;
2102}
2103
Chris Mason925baed2008-06-25 16:01:30 -04002104static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002105 struct btrfs_fs_info *fs_info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002106 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002107 u64 parent, u64 root_objectid,
2108 u64 owner, u64 offset, int refs_to_add,
2109 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04002110{
Chris Mason5caf2a02007-04-02 11:20:42 -04002111 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002112 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04002113 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07002114 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002115 u64 bytenr = node->bytenr;
2116 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002117 u64 refs;
2118 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05002119
Chris Mason5caf2a02007-04-02 11:20:42 -04002120 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002121 if (!path)
2122 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04002123
David Sterbae4058b52015-11-27 16:31:35 +01002124 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002125 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002126 /* this will setup the path even if it fails to insert the back ref */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05002127 ret = insert_inline_extent_backref(trans, fs_info, path, bytenr,
2128 num_bytes, parent, root_objectid,
2129 owner, offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002130 refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08002131 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002132 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07002133
2134 /*
2135 * Ok we had -EAGAIN which means we didn't have space to insert and
2136 * inline extent ref, so just update the reference count and add a
2137 * normal backref.
2138 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002139 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07002140 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002141 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2142 refs = btrfs_extent_refs(leaf, item);
2143 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2144 if (extent_op)
2145 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002146
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002147 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002148 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002149
David Sterbae4058b52015-11-27 16:31:35 +01002150 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002151 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04002152 /* now insert the actual backref */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05002153 ret = insert_extent_backref(trans, fs_info, path, bytenr, parent,
2154 root_objectid, owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002155 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04002156 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002157out:
Chris Mason74493f72007-12-11 09:25:06 -05002158 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08002159 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002160}
2161
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002162static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002163 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002164 struct btrfs_delayed_ref_node *node,
2165 struct btrfs_delayed_extent_op *extent_op,
2166 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002167{
Chris Mason56bec292009-03-13 10:10:06 -04002168 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002169 struct btrfs_delayed_data_ref *ref;
2170 struct btrfs_key ins;
2171 u64 parent = 0;
2172 u64 ref_root = 0;
2173 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002174
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002175 ins.objectid = node->bytenr;
2176 ins.offset = node->num_bytes;
2177 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002178
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002179 ref = btrfs_delayed_node_to_data_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002180 trace_run_delayed_data_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002181
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002182 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2183 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002184 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002185
2186 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002187 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002188 flags |= extent_op->flags_to_set;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002189 ret = alloc_reserved_file_extent(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002190 parent, ref_root, flags,
2191 ref->objectid, ref->offset,
2192 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002193 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002194 ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002195 ref_root, ref->objectid,
2196 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002197 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002198 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002199 ret = __btrfs_free_extent(trans, fs_info, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002200 ref_root, ref->objectid,
2201 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002202 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002203 } else {
2204 BUG();
2205 }
Chris Mason56bec292009-03-13 10:10:06 -04002206 return ret;
2207}
2208
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002209static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2210 struct extent_buffer *leaf,
2211 struct btrfs_extent_item *ei)
2212{
2213 u64 flags = btrfs_extent_flags(leaf, ei);
2214 if (extent_op->update_flags) {
2215 flags |= extent_op->flags_to_set;
2216 btrfs_set_extent_flags(leaf, ei, flags);
2217 }
2218
2219 if (extent_op->update_key) {
2220 struct btrfs_tree_block_info *bi;
2221 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2222 bi = (struct btrfs_tree_block_info *)(ei + 1);
2223 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2224 }
2225}
2226
2227static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002228 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002229 struct btrfs_delayed_ref_node *node,
2230 struct btrfs_delayed_extent_op *extent_op)
2231{
2232 struct btrfs_key key;
2233 struct btrfs_path *path;
2234 struct btrfs_extent_item *ei;
2235 struct extent_buffer *leaf;
2236 u32 item_size;
2237 int ret;
2238 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002239 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002240
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002241 if (trans->aborted)
2242 return 0;
2243
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002244 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05002245 metadata = 0;
2246
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002247 path = btrfs_alloc_path();
2248 if (!path)
2249 return -ENOMEM;
2250
2251 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002252
Josef Bacik3173a182013-03-07 14:22:04 -05002253 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002254 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002255 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002256 } else {
2257 key.type = BTRFS_EXTENT_ITEM_KEY;
2258 key.offset = node->num_bytes;
2259 }
2260
2261again:
David Sterbae4058b52015-11-27 16:31:35 +01002262 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002263 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002264 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002265 if (ret < 0) {
2266 err = ret;
2267 goto out;
2268 }
2269 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002270 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002271 if (path->slots[0] > 0) {
2272 path->slots[0]--;
2273 btrfs_item_key_to_cpu(path->nodes[0], &key,
2274 path->slots[0]);
2275 if (key.objectid == node->bytenr &&
2276 key.type == BTRFS_EXTENT_ITEM_KEY &&
2277 key.offset == node->num_bytes)
2278 ret = 0;
2279 }
2280 if (ret > 0) {
2281 btrfs_release_path(path);
2282 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002283
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002284 key.objectid = node->bytenr;
2285 key.offset = node->num_bytes;
2286 key.type = BTRFS_EXTENT_ITEM_KEY;
2287 goto again;
2288 }
2289 } else {
2290 err = -EIO;
2291 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002292 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002293 }
2294
2295 leaf = path->nodes[0];
2296 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2297#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2298 if (item_size < sizeof(*ei)) {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05002299 ret = convert_extent_item_v0(trans, fs_info, path, (u64)-1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002300 if (ret < 0) {
2301 err = ret;
2302 goto out;
2303 }
2304 leaf = path->nodes[0];
2305 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2306 }
2307#endif
2308 BUG_ON(item_size < sizeof(*ei));
2309 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2310 __run_delayed_extent_op(extent_op, leaf, ei);
2311
2312 btrfs_mark_buffer_dirty(leaf);
2313out:
2314 btrfs_free_path(path);
2315 return err;
2316}
2317
2318static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002319 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002320 struct btrfs_delayed_ref_node *node,
2321 struct btrfs_delayed_extent_op *extent_op,
2322 int insert_reserved)
2323{
2324 int ret = 0;
2325 struct btrfs_delayed_tree_ref *ref;
2326 struct btrfs_key ins;
2327 u64 parent = 0;
2328 u64 ref_root = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002329 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002330
2331 ref = btrfs_delayed_node_to_tree_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002332 trace_run_delayed_tree_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002333
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002334 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2335 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002336 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002337
Josef Bacik3173a182013-03-07 14:22:04 -05002338 ins.objectid = node->bytenr;
2339 if (skinny_metadata) {
2340 ins.offset = ref->level;
2341 ins.type = BTRFS_METADATA_ITEM_KEY;
2342 } else {
2343 ins.offset = node->num_bytes;
2344 ins.type = BTRFS_EXTENT_ITEM_KEY;
2345 }
2346
Liu Bo02794222016-09-14 19:19:05 -07002347 if (node->ref_mod != 1) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002348 btrfs_err(fs_info,
Liu Bo02794222016-09-14 19:19:05 -07002349 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
2350 node->bytenr, node->ref_mod, node->action, ref_root,
2351 parent);
2352 return -EIO;
2353 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002354 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002355 BUG_ON(!extent_op || !extent_op->update_flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002356 ret = alloc_reserved_tree_block(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002357 parent, ref_root,
2358 extent_op->flags_to_set,
2359 &extent_op->key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002360 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002361 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002362 ret = __btrfs_inc_extent_ref(trans, fs_info, node,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002363 parent, ref_root,
2364 ref->level, 0, 1,
Josef Bacikfcebe452014-05-13 17:30:47 -07002365 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002366 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002367 ret = __btrfs_free_extent(trans, fs_info, node,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002368 parent, ref_root,
2369 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002370 } else {
2371 BUG();
2372 }
2373 return ret;
2374}
2375
Chris Mason56bec292009-03-13 10:10:06 -04002376/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002377static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002378 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002379 struct btrfs_delayed_ref_node *node,
2380 struct btrfs_delayed_extent_op *extent_op,
2381 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002382{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002383 int ret = 0;
2384
Josef Bacik857cc2f2013-10-07 15:21:08 -04002385 if (trans->aborted) {
2386 if (insert_reserved)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002387 btrfs_pin_extent(fs_info, node->bytenr,
Josef Bacik857cc2f2013-10-07 15:21:08 -04002388 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002389 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002390 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002391
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002392 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002393 struct btrfs_delayed_ref_head *head;
2394 /*
2395 * we've hit the end of the chain and we were supposed
2396 * to insert this extent into the tree. But, it got
2397 * deleted before we ever needed to insert it, so all
2398 * we have to do is clean up the accounting
2399 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002400 BUG_ON(extent_op);
2401 head = btrfs_delayed_node_to_head(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002402 trace_run_delayed_ref_head(fs_info, node, head, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002403
Chris Mason56bec292009-03-13 10:10:06 -04002404 if (insert_reserved) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002405 btrfs_pin_extent(fs_info, node->bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002406 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002407 if (head->is_data) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002408 ret = btrfs_del_csums(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002409 node->bytenr,
2410 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002411 }
Chris Mason56bec292009-03-13 10:10:06 -04002412 }
Qu Wenruo297d7502015-09-08 17:08:37 +08002413
2414 /* Also free its reserved qgroup space */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002415 btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
Qu Wenruo297d7502015-09-08 17:08:37 +08002416 head->qgroup_reserved);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002417 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002418 }
Josef Bacikeb099672009-02-12 09:27:38 -05002419
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002420 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2421 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002422 ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002423 insert_reserved);
2424 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2425 node->type == BTRFS_SHARED_DATA_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002426 ret = run_delayed_data_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002427 insert_reserved);
2428 else
2429 BUG();
2430 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002431}
2432
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002433static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04002434select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002435{
Filipe Mananacffc3372015-07-09 13:13:44 +01002436 struct btrfs_delayed_ref_node *ref;
2437
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002438 if (list_empty(&head->ref_list))
2439 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002440
Filipe Mananacffc3372015-07-09 13:13:44 +01002441 /*
2442 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2443 * This is to prevent a ref count from going down to zero, which deletes
2444 * the extent item from the extent tree, when there still are references
2445 * to add, which would fail because they would not find the extent item.
2446 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002447 if (!list_empty(&head->ref_add_list))
2448 return list_first_entry(&head->ref_add_list,
2449 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01002450
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002451 ref = list_first_entry(&head->ref_list, struct btrfs_delayed_ref_node,
2452 list);
2453 ASSERT(list_empty(&ref->add_list));
2454 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04002455}
2456
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002457/*
2458 * Returns 0 on success or if called with an already aborted transaction.
2459 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2460 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002461static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002462 struct btrfs_fs_info *fs_info,
Josef Bacikd7df2c72014-01-23 09:21:38 -05002463 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002464{
Chris Mason56bec292009-03-13 10:10:06 -04002465 struct btrfs_delayed_ref_root *delayed_refs;
2466 struct btrfs_delayed_ref_node *ref;
2467 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002468 struct btrfs_delayed_extent_op *extent_op;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002469 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002470 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002471 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002472 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002473 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002474
2475 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002476 while (1) {
2477 if (!locked_ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002478 if (count >= nr)
Chris Mason56bec292009-03-13 10:10:06 -04002479 break;
Chris Mason56bec292009-03-13 10:10:06 -04002480
Josef Bacikd7df2c72014-01-23 09:21:38 -05002481 spin_lock(&delayed_refs->lock);
2482 locked_ref = btrfs_select_ref_head(trans);
2483 if (!locked_ref) {
2484 spin_unlock(&delayed_refs->lock);
2485 break;
2486 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002487
2488 /* grab the lock that says we are going to process
2489 * all the refs for this head */
2490 ret = btrfs_delayed_ref_lock(trans, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002491 spin_unlock(&delayed_refs->lock);
Chris Masonc3e69d52009-03-13 10:17:05 -04002492 /*
2493 * we may have dropped the spin lock to get the head
2494 * mutex lock, and that might have given someone else
2495 * time to free the head. If that's true, it has been
2496 * removed from our list and we can move on.
2497 */
2498 if (ret == -EAGAIN) {
2499 locked_ref = NULL;
2500 count++;
2501 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002502 }
2503 }
2504
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002505 /*
2506 * We need to try and merge add/drops of the same ref since we
2507 * can run into issues with relocate dropping the implicit ref
2508 * and then it being added back again before the drop can
2509 * finish. If we merged anything we need to re-loop so we can
2510 * get a good ref.
2511 * Or we can get node references of the same type that weren't
2512 * merged when created due to bumps in the tree mod seq, and
2513 * we need to merge them to prevent adding an inline extent
2514 * backref before dropping it (triggering a BUG_ON at
2515 * insert_inline_extent_backref()).
2516 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002517 spin_lock(&locked_ref->lock);
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002518 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2519 locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002520
2521 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002522 * locked_ref is the head node, so we have to go one
2523 * node back for any delayed ref updates
2524 */
2525 ref = select_delayed_ref(locked_ref);
2526
2527 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002528 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002529 spin_unlock(&locked_ref->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002530 spin_lock(&delayed_refs->lock);
2531 locked_ref->processing = 0;
Arne Jansend1270cd2011-09-13 15:16:43 +02002532 delayed_refs->num_heads_ready++;
2533 spin_unlock(&delayed_refs->lock);
Jeff Mahoneyd0280992016-12-20 13:28:28 -05002534 btrfs_delayed_ref_unlock(locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002535 locked_ref = NULL;
Arne Jansend1270cd2011-09-13 15:16:43 +02002536 cond_resched();
Josef Bacik27a377d2014-02-07 13:57:59 -05002537 count++;
Arne Jansend1270cd2011-09-13 15:16:43 +02002538 continue;
2539 }
2540
2541 /*
Chris Mason56bec292009-03-13 10:10:06 -04002542 * record the must insert reserved flag before we
2543 * drop the spin lock.
2544 */
2545 must_insert_reserved = locked_ref->must_insert_reserved;
2546 locked_ref->must_insert_reserved = 0;
2547
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002548 extent_op = locked_ref->extent_op;
2549 locked_ref->extent_op = NULL;
2550
Chris Mason56bec292009-03-13 10:10:06 -04002551 if (!ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002552
2553
Chris Mason56bec292009-03-13 10:10:06 -04002554 /* All delayed refs have been processed, Go ahead
2555 * and send the head node to run_one_delayed_ref,
2556 * so that any accounting fixes can happen
2557 */
2558 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002559
2560 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002561 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002562 extent_op = NULL;
2563 }
2564
2565 if (extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002566 spin_unlock(&locked_ref->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002567 ret = run_delayed_extent_op(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002568 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002569 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002570
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002571 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04002572 /*
2573 * Need to reset must_insert_reserved if
2574 * there was an error so the abort stuff
2575 * can cleanup the reserved space
2576 * properly.
2577 */
2578 if (must_insert_reserved)
2579 locked_ref->must_insert_reserved = 1;
Jeff Mahoneyaa7c8da2016-12-20 13:28:27 -05002580 spin_lock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002581 locked_ref->processing = 0;
Jeff Mahoneyaa7c8da2016-12-20 13:28:27 -05002582 delayed_refs->num_heads_ready++;
2583 spin_unlock(&delayed_refs->lock);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002584 btrfs_debug(fs_info,
2585 "run_delayed_extent_op returned %d",
2586 ret);
Miao Xie093486c2012-12-19 08:10:10 +00002587 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002588 return ret;
2589 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002590 continue;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002591 }
Chris Mason56bec292009-03-13 10:10:06 -04002592
Josef Bacikd7df2c72014-01-23 09:21:38 -05002593 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002594 * Need to drop our head ref lock and re-acquire the
Josef Bacikd7df2c72014-01-23 09:21:38 -05002595 * delayed ref lock and then re-check to make sure
2596 * nobody got added.
2597 */
2598 spin_unlock(&locked_ref->lock);
2599 spin_lock(&delayed_refs->lock);
2600 spin_lock(&locked_ref->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002601 if (!list_empty(&locked_ref->ref_list) ||
Josef Bacik573a0752014-03-27 19:41:34 -04002602 locked_ref->extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002603 spin_unlock(&locked_ref->lock);
2604 spin_unlock(&delayed_refs->lock);
2605 continue;
2606 }
2607 ref->in_tree = 0;
2608 delayed_refs->num_heads--;
Liu Boc46effa2013-10-14 12:59:45 +08002609 rb_erase(&locked_ref->href_node,
2610 &delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002611 spin_unlock(&delayed_refs->lock);
2612 } else {
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002613 actual_count++;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002614 ref->in_tree = 0;
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002615 list_del(&ref->list);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002616 if (!list_empty(&ref->add_list))
2617 list_del(&ref->add_list);
Liu Boc46effa2013-10-14 12:59:45 +08002618 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002619 atomic_dec(&delayed_refs->num_entries);
2620
Miao Xie093486c2012-12-19 08:10:10 +00002621 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002622 /*
2623 * when we play the delayed ref, also correct the
2624 * ref_mod on head
2625 */
2626 switch (ref->action) {
2627 case BTRFS_ADD_DELAYED_REF:
2628 case BTRFS_ADD_DELAYED_EXTENT:
2629 locked_ref->node.ref_mod -= ref->ref_mod;
2630 break;
2631 case BTRFS_DROP_DELAYED_REF:
2632 locked_ref->node.ref_mod += ref->ref_mod;
2633 break;
2634 default:
2635 WARN_ON(1);
2636 }
2637 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002638 spin_unlock(&locked_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002639
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002640 ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002641 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002642
Miao Xie78a61842012-11-21 02:21:28 +00002643 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002644 if (ret) {
Wang Xiaoguang9d1032c2016-06-20 09:18:52 +08002645 spin_lock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002646 locked_ref->processing = 0;
Wang Xiaoguang9d1032c2016-06-20 09:18:52 +08002647 delayed_refs->num_heads_ready++;
2648 spin_unlock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002649 btrfs_delayed_ref_unlock(locked_ref);
2650 btrfs_put_delayed_ref(ref);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002651 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
2652 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002653 return ret;
2654 }
2655
Miao Xie093486c2012-12-19 08:10:10 +00002656 /*
2657 * If this node is a head, that means all the refs in this head
2658 * have been dealt with, and we will pick the next head to deal
2659 * with, so we must unlock the head and drop it from the cluster
2660 * list before we release it.
2661 */
2662 if (btrfs_delayed_ref_is_head(ref)) {
Josef Bacik12621332015-02-03 07:50:16 -08002663 if (locked_ref->is_data &&
2664 locked_ref->total_ref_mod < 0) {
2665 spin_lock(&delayed_refs->lock);
2666 delayed_refs->pending_csums -= ref->num_bytes;
2667 spin_unlock(&delayed_refs->lock);
2668 }
Miao Xie093486c2012-12-19 08:10:10 +00002669 btrfs_delayed_ref_unlock(locked_ref);
2670 locked_ref = NULL;
2671 }
2672 btrfs_put_delayed_ref(ref);
2673 count++;
Chris Mason1887be62009-03-13 10:11:24 -04002674 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002675 }
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002676
2677 /*
2678 * We don't want to include ref heads since we can have empty ref heads
2679 * and those will drastically skew our runtime down since we just do
2680 * accounting, no actual extent tree updates.
2681 */
2682 if (actual_count > 0) {
2683 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2684 u64 avg;
2685
2686 /*
2687 * We weigh the current average higher than our current runtime
2688 * to avoid large swings in the average.
2689 */
2690 spin_lock(&delayed_refs->lock);
2691 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002692 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002693 spin_unlock(&delayed_refs->lock);
2694 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002695 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002696}
2697
Arne Jansen709c0482011-09-12 12:22:57 +02002698#ifdef SCRAMBLE_DELAYED_REFS
2699/*
2700 * Normally delayed refs get processed in ascending bytenr order. This
2701 * correlates in most cases to the order added. To expose dependencies on this
2702 * order, we start to process the tree in the middle instead of the beginning
2703 */
2704static u64 find_middle(struct rb_root *root)
2705{
2706 struct rb_node *n = root->rb_node;
2707 struct btrfs_delayed_ref_node *entry;
2708 int alt = 1;
2709 u64 middle;
2710 u64 first = 0, last = 0;
2711
2712 n = rb_first(root);
2713 if (n) {
2714 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2715 first = entry->bytenr;
2716 }
2717 n = rb_last(root);
2718 if (n) {
2719 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2720 last = entry->bytenr;
2721 }
2722 n = root->rb_node;
2723
2724 while (n) {
2725 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2726 WARN_ON(!entry->in_tree);
2727
2728 middle = entry->bytenr;
2729
2730 if (alt)
2731 n = n->rb_left;
2732 else
2733 n = n->rb_right;
2734
2735 alt = 1 - alt;
2736 }
2737 return middle;
2738}
2739#endif
2740
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002741static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002742{
2743 u64 num_bytes;
2744
2745 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2746 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002747 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002748 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2749
2750 /*
2751 * 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 -04002752 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002753 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002754 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002755}
2756
Josef Bacik12621332015-02-03 07:50:16 -08002757/*
2758 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2759 * would require to store the csums for that many bytes.
2760 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002761u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002762{
2763 u64 csum_size;
2764 u64 num_csums_per_leaf;
2765 u64 num_csums;
2766
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002767 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002768 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002769 (u64)btrfs_super_csum_size(fs_info->super_copy));
2770 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002771 num_csums += num_csums_per_leaf - 1;
2772 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2773 return num_csums;
2774}
2775
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002776int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002777 struct btrfs_fs_info *fs_info)
Josef Bacik1be41b72013-06-12 13:56:06 -04002778{
2779 struct btrfs_block_rsv *global_rsv;
2780 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
Josef Bacik12621332015-02-03 07:50:16 -08002781 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
Josef Bacikcb723e42015-02-18 08:06:57 -08002782 u64 num_dirty_bgs = trans->transaction->num_dirty_bgs;
2783 u64 num_bytes, num_dirty_bgs_bytes;
Josef Bacik1be41b72013-06-12 13:56:06 -04002784 int ret = 0;
2785
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002786 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002787 num_heads = heads_to_leaves(fs_info, num_heads);
Josef Bacik1be41b72013-06-12 13:56:06 -04002788 if (num_heads > 1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002789 num_bytes += (num_heads - 1) * fs_info->nodesize;
Josef Bacik1be41b72013-06-12 13:56:06 -04002790 num_bytes <<= 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002791 num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
2792 fs_info->nodesize;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002793 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
Josef Bacikcb723e42015-02-18 08:06:57 -08002794 num_dirty_bgs);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002795 global_rsv = &fs_info->global_block_rsv;
Josef Bacik1be41b72013-06-12 13:56:06 -04002796
2797 /*
2798 * If we can't allocate any more chunks lets make sure we have _lots_ of
2799 * wiggle room since running delayed refs can create more delayed refs.
2800 */
Josef Bacikcb723e42015-02-18 08:06:57 -08002801 if (global_rsv->space_info->full) {
2802 num_dirty_bgs_bytes <<= 1;
Josef Bacik1be41b72013-06-12 13:56:06 -04002803 num_bytes <<= 1;
Josef Bacikcb723e42015-02-18 08:06:57 -08002804 }
Josef Bacik1be41b72013-06-12 13:56:06 -04002805
2806 spin_lock(&global_rsv->lock);
Josef Bacikcb723e42015-02-18 08:06:57 -08002807 if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
Josef Bacik1be41b72013-06-12 13:56:06 -04002808 ret = 1;
2809 spin_unlock(&global_rsv->lock);
2810 return ret;
2811}
2812
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002813int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002814 struct btrfs_fs_info *fs_info)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002815{
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002816 u64 num_entries =
2817 atomic_read(&trans->transaction->delayed_refs.num_entries);
2818 u64 avg_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002819 u64 val;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002820
2821 smp_mb();
2822 avg_runtime = fs_info->avg_delayed_ref_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002823 val = num_entries * avg_runtime;
Wang Xiaoguangdc1a90c2016-10-26 15:23:01 +08002824 if (val >= NSEC_PER_SEC)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002825 return 1;
Chris Masona79b7d42014-05-22 16:18:52 -07002826 if (val >= NSEC_PER_SEC / 2)
2827 return 2;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002828
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002829 return btrfs_check_space_for_delayed_refs(trans, fs_info);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002830}
2831
Chris Masona79b7d42014-05-22 16:18:52 -07002832struct async_delayed_refs {
2833 struct btrfs_root *root;
Josef Bacik31b96552016-04-11 17:37:40 -04002834 u64 transid;
Chris Masona79b7d42014-05-22 16:18:52 -07002835 int count;
2836 int error;
2837 int sync;
2838 struct completion wait;
2839 struct btrfs_work work;
2840};
2841
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002842static inline struct async_delayed_refs *
2843to_async_delayed_refs(struct btrfs_work *work)
2844{
2845 return container_of(work, struct async_delayed_refs, work);
2846}
2847
Chris Masona79b7d42014-05-22 16:18:52 -07002848static void delayed_ref_async_start(struct btrfs_work *work)
2849{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002850 struct async_delayed_refs *async = to_async_delayed_refs(work);
Chris Masona79b7d42014-05-22 16:18:52 -07002851 struct btrfs_trans_handle *trans;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002852 struct btrfs_fs_info *fs_info = async->root->fs_info;
Chris Masona79b7d42014-05-22 16:18:52 -07002853 int ret;
2854
Chris Mason0f873ec2016-04-27 09:59:38 -04002855 /* if the commit is already started, we don't need to wait here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002856 if (btrfs_transaction_blocked(fs_info))
Josef Bacik31b96552016-04-11 17:37:40 -04002857 goto done;
Josef Bacik31b96552016-04-11 17:37:40 -04002858
Chris Mason0f873ec2016-04-27 09:59:38 -04002859 trans = btrfs_join_transaction(async->root);
2860 if (IS_ERR(trans)) {
2861 async->error = PTR_ERR(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07002862 goto done;
2863 }
2864
2865 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002866 * trans->sync means that when we call end_transaction, we won't
Chris Masona79b7d42014-05-22 16:18:52 -07002867 * wait on delayed refs
2868 */
2869 trans->sync = true;
Chris Mason0f873ec2016-04-27 09:59:38 -04002870
2871 /* Don't bother flushing if we got into a different transaction */
2872 if (trans->transid > async->transid)
2873 goto end;
2874
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002875 ret = btrfs_run_delayed_refs(trans, fs_info, async->count);
Chris Masona79b7d42014-05-22 16:18:52 -07002876 if (ret)
2877 async->error = ret;
Chris Mason0f873ec2016-04-27 09:59:38 -04002878end:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002879 ret = btrfs_end_transaction(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07002880 if (ret && !async->error)
2881 async->error = ret;
2882done:
2883 if (async->sync)
2884 complete(&async->wait);
2885 else
2886 kfree(async);
2887}
2888
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002889int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
Josef Bacik31b96552016-04-11 17:37:40 -04002890 unsigned long count, u64 transid, int wait)
Chris Masona79b7d42014-05-22 16:18:52 -07002891{
2892 struct async_delayed_refs *async;
2893 int ret;
2894
2895 async = kmalloc(sizeof(*async), GFP_NOFS);
2896 if (!async)
2897 return -ENOMEM;
2898
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002899 async->root = fs_info->tree_root;
Chris Masona79b7d42014-05-22 16:18:52 -07002900 async->count = count;
2901 async->error = 0;
Josef Bacik31b96552016-04-11 17:37:40 -04002902 async->transid = transid;
Chris Masona79b7d42014-05-22 16:18:52 -07002903 if (wait)
2904 async->sync = 1;
2905 else
2906 async->sync = 0;
2907 init_completion(&async->wait);
2908
Liu Bo9e0af232014-08-15 23:36:53 +08002909 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
2910 delayed_ref_async_start, NULL, NULL);
Chris Masona79b7d42014-05-22 16:18:52 -07002911
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002912 btrfs_queue_work(fs_info->extent_workers, &async->work);
Chris Masona79b7d42014-05-22 16:18:52 -07002913
2914 if (wait) {
2915 wait_for_completion(&async->wait);
2916 ret = async->error;
2917 kfree(async);
2918 return ret;
2919 }
2920 return 0;
2921}
2922
Chris Masonc3e69d52009-03-13 10:17:05 -04002923/*
2924 * this starts processing the delayed reference count updates and
2925 * extent insertions we have queued up so far. count can be
2926 * 0, which means to process everything in the tree at the start
2927 * of the run (but not newly added entries), or it can be some target
2928 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002929 *
2930 * Returns 0 on success or if called with an aborted transaction
2931 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002932 */
2933int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002934 struct btrfs_fs_info *fs_info, unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04002935{
2936 struct rb_node *node;
2937 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002938 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002939 int ret;
2940 int run_all = count == (unsigned long)-1;
Filipe Mananad9a05402015-10-03 13:13:13 +01002941 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Chris Masonc3e69d52009-03-13 10:17:05 -04002942
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002943 /* We'll clean this up in btrfs_cleanup_transaction */
2944 if (trans->aborted)
2945 return 0;
2946
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002947 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08002948 return 0;
2949
Chris Masonc3e69d52009-03-13 10:17:05 -04002950 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08002951 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05002952 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002953
Chris Masonc3e69d52009-03-13 10:17:05 -04002954again:
Arne Jansen709c0482011-09-12 12:22:57 +02002955#ifdef SCRAMBLE_DELAYED_REFS
2956 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2957#endif
Filipe Mananad9a05402015-10-03 13:13:13 +01002958 trans->can_flush_pending_bgs = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002959 ret = __btrfs_run_delayed_refs(trans, fs_info, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002960 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002961 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002962 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002963 }
2964
Chris Mason56bec292009-03-13 10:10:06 -04002965 if (run_all) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002966 if (!list_empty(&trans->new_bgs))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002967 btrfs_create_pending_block_groups(trans, fs_info);
Josef Bacikea658ba2012-09-11 16:57:25 -04002968
Josef Bacikd7df2c72014-01-23 09:21:38 -05002969 spin_lock(&delayed_refs->lock);
Liu Boc46effa2013-10-14 12:59:45 +08002970 node = rb_first(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002971 if (!node) {
2972 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002973 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002974 }
Chris Mason56bec292009-03-13 10:10:06 -04002975
2976 while (node) {
Liu Boc46effa2013-10-14 12:59:45 +08002977 head = rb_entry(node, struct btrfs_delayed_ref_head,
2978 href_node);
2979 if (btrfs_delayed_ref_is_head(&head->node)) {
2980 struct btrfs_delayed_ref_node *ref;
Chris Mason56bec292009-03-13 10:10:06 -04002981
Liu Boc46effa2013-10-14 12:59:45 +08002982 ref = &head->node;
Elena Reshetova6df8cdf2017-03-03 10:55:15 +02002983 refcount_inc(&ref->refs);
Chris Mason56bec292009-03-13 10:10:06 -04002984
2985 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002986 /*
2987 * Mutex was contended, block until it's
2988 * released and try again
2989 */
Chris Mason56bec292009-03-13 10:10:06 -04002990 mutex_lock(&head->mutex);
2991 mutex_unlock(&head->mutex);
2992
2993 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002994 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002995 goto again;
Liu Boc46effa2013-10-14 12:59:45 +08002996 } else {
2997 WARN_ON(1);
Chris Mason56bec292009-03-13 10:10:06 -04002998 }
2999 node = rb_next(node);
3000 }
3001 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003002 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04003003 goto again;
3004 }
Chris Mason54aa1f42007-06-22 14:16:25 -04003005out:
Jan Schmidtedf39272012-06-28 18:04:55 +02003006 assert_qgroups_uptodate(trans);
Filipe Mananad9a05402015-10-03 13:13:13 +01003007 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Chris Masona28ec192007-03-06 20:08:01 -05003008 return 0;
3009}
3010
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003011int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003012 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003013 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04003014 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003015{
3016 struct btrfs_delayed_extent_op *extent_op;
3017 int ret;
3018
Miao Xie78a61842012-11-21 02:21:28 +00003019 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003020 if (!extent_op)
3021 return -ENOMEM;
3022
3023 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01003024 extent_op->update_flags = true;
3025 extent_op->update_key = false;
3026 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04003027 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003028
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003029 ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003030 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003031 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00003032 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003033 return ret;
3034}
3035
Liu Boe4c3b2d2017-01-30 12:25:28 -08003036static noinline int check_delayed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003037 struct btrfs_path *path,
3038 u64 objectid, u64 offset, u64 bytenr)
3039{
3040 struct btrfs_delayed_ref_head *head;
3041 struct btrfs_delayed_ref_node *ref;
3042 struct btrfs_delayed_data_ref *data_ref;
3043 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boe4c3b2d2017-01-30 12:25:28 -08003044 struct btrfs_transaction *cur_trans;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003045 int ret = 0;
3046
Liu Boe4c3b2d2017-01-30 12:25:28 -08003047 cur_trans = root->fs_info->running_transaction;
3048 if (!cur_trans)
3049 return 0;
3050
3051 delayed_refs = &cur_trans->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003052 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08003053 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003054 if (!head) {
3055 spin_unlock(&delayed_refs->lock);
3056 return 0;
3057 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003058
3059 if (!mutex_trylock(&head->mutex)) {
Elena Reshetova6df8cdf2017-03-03 10:55:15 +02003060 refcount_inc(&head->node.refs);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003061 spin_unlock(&delayed_refs->lock);
3062
David Sterbab3b4aa72011-04-21 01:20:15 +02003063 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003064
David Sterba8cc33e52011-05-02 15:29:25 +02003065 /*
3066 * Mutex was contended, block until it's released and let
3067 * caller try again
3068 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003069 mutex_lock(&head->mutex);
3070 mutex_unlock(&head->mutex);
3071 btrfs_put_delayed_ref(&head->node);
3072 return -EAGAIN;
3073 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003074 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003075
3076 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08003077 list_for_each_entry(ref, &head->ref_list, list) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05003078 /* If it's a shared ref we know a cross reference exists */
3079 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
3080 ret = 1;
3081 break;
3082 }
3083
3084 data_ref = btrfs_delayed_node_to_data_ref(ref);
3085
3086 /*
3087 * If our ref doesn't match the one we're currently looking at
3088 * then we have a cross reference.
3089 */
3090 if (data_ref->root != root->root_key.objectid ||
3091 data_ref->objectid != objectid ||
3092 data_ref->offset != offset) {
3093 ret = 1;
3094 break;
3095 }
3096 }
3097 spin_unlock(&head->lock);
3098 mutex_unlock(&head->mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003099 return ret;
3100}
3101
Liu Boe4c3b2d2017-01-30 12:25:28 -08003102static noinline int check_committed_ref(struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003103 struct btrfs_path *path,
3104 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05003105{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003106 struct btrfs_fs_info *fs_info = root->fs_info;
3107 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04003108 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003109 struct btrfs_extent_data_ref *ref;
3110 struct btrfs_extent_inline_ref *iref;
3111 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05003112 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003113 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04003114 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05003115
Chris Masonbe20aa92007-12-17 20:14:01 -05003116 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04003117 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04003118 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05003119
Chris Masonbe20aa92007-12-17 20:14:01 -05003120 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3121 if (ret < 0)
3122 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003123 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04003124
3125 ret = -ENOENT;
3126 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04003127 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003128
Zheng Yan31840ae2008-09-23 13:14:14 -04003129 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04003130 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003131 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05003132
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003133 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05003134 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003135
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003136 ret = 1;
3137 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3138#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3139 if (item_size < sizeof(*ei)) {
3140 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3141 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003142 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003143#endif
3144 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
3145
3146 if (item_size != sizeof(*ei) +
3147 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
3148 goto out;
3149
3150 if (btrfs_extent_generation(leaf, ei) <=
3151 btrfs_root_last_snapshot(&root->root_item))
3152 goto out;
3153
3154 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
3155 if (btrfs_extent_inline_ref_type(leaf, iref) !=
3156 BTRFS_EXTENT_DATA_REF_KEY)
3157 goto out;
3158
3159 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3160 if (btrfs_extent_refs(leaf, ei) !=
3161 btrfs_extent_data_ref_count(leaf, ref) ||
3162 btrfs_extent_data_ref_root(leaf, ref) !=
3163 root->root_key.objectid ||
3164 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3165 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3166 goto out;
3167
Yan Zhengf321e492008-07-30 09:26:11 -04003168 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05003169out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003170 return ret;
3171}
3172
Liu Boe4c3b2d2017-01-30 12:25:28 -08003173int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
3174 u64 bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003175{
3176 struct btrfs_path *path;
3177 int ret;
3178 int ret2;
3179
3180 path = btrfs_alloc_path();
3181 if (!path)
3182 return -ENOENT;
3183
3184 do {
Liu Boe4c3b2d2017-01-30 12:25:28 -08003185 ret = check_committed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003186 offset, bytenr);
3187 if (ret && ret != -ENOENT)
3188 goto out;
3189
Liu Boe4c3b2d2017-01-30 12:25:28 -08003190 ret2 = check_delayed_ref(root, path, objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003191 offset, bytenr);
3192 } while (ret2 == -EAGAIN);
3193
3194 if (ret2 && ret2 != -ENOENT) {
3195 ret = ret2;
3196 goto out;
3197 }
3198
3199 if (ret != -ENOENT || ret2 != -ENOENT)
3200 ret = 0;
3201out:
Yan Zhengf321e492008-07-30 09:26:11 -04003202 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003203 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3204 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04003205 return ret;
3206}
3207
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003208static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05003209 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003210 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07003211 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04003212{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003213 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04003214 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003215 u64 num_bytes;
3216 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003217 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04003218 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003219 struct btrfs_key key;
3220 struct btrfs_file_extent_item *fi;
3221 int i;
3222 int level;
3223 int ret = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003224 int (*process_func)(struct btrfs_trans_handle *,
3225 struct btrfs_fs_info *,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003226 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04003227
David Sterbafccb84c2014-09-29 23:53:21 +02003228
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003229 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003230 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003231
Zheng Yan31840ae2008-09-23 13:14:14 -04003232 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003233 nritems = btrfs_header_nritems(buf);
3234 level = btrfs_header_level(buf);
3235
Miao Xie27cdeb72014-04-02 19:51:05 +08003236 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003237 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003238
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003239 if (inc)
3240 process_func = btrfs_inc_extent_ref;
3241 else
3242 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003243
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003244 if (full_backref)
3245 parent = buf->start;
3246 else
3247 parent = 0;
3248
Zheng Yan31840ae2008-09-23 13:14:14 -04003249 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003250 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003251 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02003252 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003253 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003254 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003255 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003256 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003257 BTRFS_FILE_EXTENT_INLINE)
3258 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003259 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3260 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003261 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003262
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003263 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3264 key.offset -= btrfs_file_extent_offset(buf, fi);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003265 ret = process_func(trans, fs_info, bytenr, num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003266 parent, ref_root, key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003267 key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003268 if (ret)
3269 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003270 } else {
3271 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003272 num_bytes = fs_info->nodesize;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003273 ret = process_func(trans, fs_info, bytenr, num_bytes,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003274 parent, ref_root, level - 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003275 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003276 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003277 }
3278 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003279 return 0;
3280fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003281 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003282}
3283
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003284int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003285 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04003286{
Josef Bacike339a6b2014-07-02 10:54:25 -07003287 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003288}
Zheng Yan31840ae2008-09-23 13:14:14 -04003289
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003290int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003291 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003292{
Josef Bacike339a6b2014-07-02 10:54:25 -07003293 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003294}
3295
Chris Mason9078a3e2007-04-26 16:46:15 -04003296static int write_one_cache_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003297 struct btrfs_fs_info *fs_info,
Chris Mason9078a3e2007-04-26 16:46:15 -04003298 struct btrfs_path *path,
3299 struct btrfs_block_group_cache *cache)
3300{
3301 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003302 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003303 unsigned long bi;
3304 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003305
Chris Mason9078a3e2007-04-26 16:46:15 -04003306 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003307 if (ret) {
3308 if (ret > 0)
3309 ret = -ENOENT;
Chris Mason54aa1f42007-06-22 14:16:25 -04003310 goto fail;
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003311 }
Chris Mason5f39d392007-10-15 16:14:19 -04003312
3313 leaf = path->nodes[0];
3314 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3315 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3316 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -04003317fail:
Filipe Manana24b89d02015-04-25 18:31:05 +01003318 btrfs_release_path(path);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003319 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003320
3321}
3322
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003323static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003324next_block_group(struct btrfs_fs_info *fs_info,
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003325 struct btrfs_block_group_cache *cache)
3326{
3327 struct rb_node *node;
Filipe Manana292cbd52014-11-26 15:28:50 +00003328
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003329 spin_lock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003330
3331 /* If our block group was removed, we need a full search. */
3332 if (RB_EMPTY_NODE(&cache->cache_node)) {
3333 const u64 next_bytenr = cache->key.objectid + cache->key.offset;
3334
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003335 spin_unlock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003336 btrfs_put_block_group(cache);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003337 cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
Filipe Manana292cbd52014-11-26 15:28:50 +00003338 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003339 node = rb_next(&cache->cache_node);
3340 btrfs_put_block_group(cache);
3341 if (node) {
3342 cache = rb_entry(node, struct btrfs_block_group_cache,
3343 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003344 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003345 } else
3346 cache = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003347 spin_unlock(&fs_info->block_group_cache_lock);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003348 return cache;
3349}
3350
Josef Bacik0af3d002010-06-21 14:48:16 -04003351static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3352 struct btrfs_trans_handle *trans,
3353 struct btrfs_path *path)
3354{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003355 struct btrfs_fs_info *fs_info = block_group->fs_info;
3356 struct btrfs_root *root = fs_info->tree_root;
Josef Bacik0af3d002010-06-21 14:48:16 -04003357 struct inode *inode = NULL;
3358 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003359 int dcs = BTRFS_DC_ERROR;
David Sterbaf8c269d2015-01-16 17:21:12 +01003360 u64 num_pages = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003361 int retries = 0;
3362 int ret = 0;
3363
3364 /*
3365 * If this block group is smaller than 100 megs don't bother caching the
3366 * block group.
3367 */
Byongho Leeee221842015-12-15 01:42:10 +09003368 if (block_group->key.offset < (100 * SZ_1M)) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003369 spin_lock(&block_group->lock);
3370 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3371 spin_unlock(&block_group->lock);
3372 return 0;
3373 }
3374
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003375 if (trans->aborted)
3376 return 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003377again:
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003378 inode = lookup_free_space_inode(fs_info, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003379 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3380 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003381 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003382 goto out;
3383 }
3384
3385 if (IS_ERR(inode)) {
3386 BUG_ON(retries);
3387 retries++;
3388
3389 if (block_group->ro)
3390 goto out_free;
3391
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003392 ret = create_free_space_inode(fs_info, trans, block_group,
3393 path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003394 if (ret)
3395 goto out_free;
3396 goto again;
3397 }
3398
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003399 /* We've already setup this transaction, go ahead and exit */
3400 if (block_group->cache_generation == trans->transid &&
3401 i_size_read(inode)) {
3402 dcs = BTRFS_DC_SETUP;
3403 goto out_put;
3404 }
3405
Josef Bacik0af3d002010-06-21 14:48:16 -04003406 /*
3407 * We want to set the generation to 0, that way if anything goes wrong
3408 * from here on out we know not to trust this cache when we load up next
3409 * time.
3410 */
3411 BTRFS_I(inode)->generation = 0;
3412 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003413 if (ret) {
3414 /*
3415 * So theoretically we could recover from this, simply set the
3416 * super cache generation to 0 so we know to invalidate the
3417 * cache, but then we'd have to keep track of the block groups
3418 * that fail this way so we know we _have_ to reset this cache
3419 * before the next commit or risk reading stale cache. So to
3420 * limit our exposure to horrible edge cases lets just abort the
3421 * transaction, this only happens in really bad situations
3422 * anyway.
3423 */
Jeff Mahoney66642832016-06-10 18:19:25 -04003424 btrfs_abort_transaction(trans, ret);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003425 goto out_put;
3426 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003427 WARN_ON(ret);
3428
3429 if (i_size_read(inode) > 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003430 ret = btrfs_check_trunc_cache_free_space(fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003431 &fs_info->global_block_rsv);
Miao Xie7b61cd92013-05-13 13:55:09 +00003432 if (ret)
3433 goto out_put;
3434
Jeff Mahoney77ab86b2017-02-15 16:28:30 -05003435 ret = btrfs_truncate_free_space_cache(trans, NULL, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003436 if (ret)
3437 goto out_put;
3438 }
3439
3440 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003441 if (block_group->cached != BTRFS_CACHE_FINISHED ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003442 !btrfs_test_opt(fs_info, SPACE_CACHE)) {
Liu Bocf7c1ef2012-07-06 03:31:34 -06003443 /*
3444 * don't bother trying to write stuff out _if_
3445 * a) we're not cached,
Liu Bo1a79c1f2017-03-06 13:49:02 -08003446 * b) we're with nospace_cache mount option,
3447 * c) we're with v2 space_cache (FREE_SPACE_TREE).
Liu Bocf7c1ef2012-07-06 03:31:34 -06003448 */
Josef Bacik2b209822010-12-03 13:17:53 -05003449 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003450 spin_unlock(&block_group->lock);
3451 goto out_put;
3452 }
3453 spin_unlock(&block_group->lock);
3454
Josef Bacik6fc823b2012-08-06 13:46:38 -06003455 /*
Josef Bacik2968b1f2015-10-01 12:55:18 -04003456 * We hit an ENOSPC when setting up the cache in this transaction, just
3457 * skip doing the setup, we've already cleared the cache so we're safe.
3458 */
3459 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
3460 ret = -ENOSPC;
3461 goto out_put;
3462 }
3463
3464 /*
Josef Bacik6fc823b2012-08-06 13:46:38 -06003465 * Try to preallocate enough space based on how big the block group is.
3466 * Keep in mind this has to include any pinned space which could end up
3467 * taking up quite a bit since it's not folded into the other space
3468 * cache.
3469 */
Byongho Leeee221842015-12-15 01:42:10 +09003470 num_pages = div_u64(block_group->key.offset, SZ_256M);
Josef Bacik0af3d002010-06-21 14:48:16 -04003471 if (!num_pages)
3472 num_pages = 1;
3473
Josef Bacik0af3d002010-06-21 14:48:16 -04003474 num_pages *= 16;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003475 num_pages *= PAGE_SIZE;
Josef Bacik0af3d002010-06-21 14:48:16 -04003476
Qu Wenruo7cf5b972015-09-08 17:25:55 +08003477 ret = btrfs_check_data_free_space(inode, 0, num_pages);
Josef Bacik0af3d002010-06-21 14:48:16 -04003478 if (ret)
3479 goto out_put;
3480
3481 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3482 num_pages, num_pages,
3483 &alloc_hint);
Josef Bacik2968b1f2015-10-01 12:55:18 -04003484 /*
3485 * Our cache requires contiguous chunks so that we don't modify a bunch
3486 * of metadata or split extents when writing the cache out, which means
3487 * we can enospc if we are heavily fragmented in addition to just normal
3488 * out of space conditions. So if we hit this just skip setting up any
3489 * other block groups for this transaction, maybe we'll unpin enough
3490 * space the next time around.
3491 */
Josef Bacik2b209822010-12-03 13:17:53 -05003492 if (!ret)
3493 dcs = BTRFS_DC_SETUP;
Josef Bacik2968b1f2015-10-01 12:55:18 -04003494 else if (ret == -ENOSPC)
3495 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
Josef Bacikc09544e2011-08-30 10:19:10 -04003496
Josef Bacik0af3d002010-06-21 14:48:16 -04003497out_put:
3498 iput(inode);
3499out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003500 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003501out:
3502 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003503 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003504 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003505 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003506 spin_unlock(&block_group->lock);
3507
3508 return ret;
3509}
3510
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003511int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003512 struct btrfs_fs_info *fs_info)
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003513{
3514 struct btrfs_block_group_cache *cache, *tmp;
3515 struct btrfs_transaction *cur_trans = trans->transaction;
3516 struct btrfs_path *path;
3517
3518 if (list_empty(&cur_trans->dirty_bgs) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003519 !btrfs_test_opt(fs_info, SPACE_CACHE))
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003520 return 0;
3521
3522 path = btrfs_alloc_path();
3523 if (!path)
3524 return -ENOMEM;
3525
3526 /* Could add new block groups, use _safe just in case */
3527 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
3528 dirty_list) {
3529 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3530 cache_save_setup(cache, trans, path);
3531 }
3532
3533 btrfs_free_path(path);
3534 return 0;
3535}
3536
Chris Mason1bbc6212015-04-06 12:46:08 -07003537/*
3538 * transaction commit does final block group cache writeback during a
3539 * critical section where nothing is allowed to change the FS. This is
3540 * required in order for the cache to actually match the block group,
3541 * but can introduce a lot of latency into the commit.
3542 *
3543 * So, btrfs_start_dirty_block_groups is here to kick off block group
3544 * cache IO. There's a chance we'll have to redo some of it if the
3545 * block group changes again during the commit, but it greatly reduces
3546 * the commit latency by getting rid of the easy block groups while
3547 * we're still allowing others to join the commit.
3548 */
3549int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003550 struct btrfs_fs_info *fs_info)
Chris Mason1bbc6212015-04-06 12:46:08 -07003551{
3552 struct btrfs_block_group_cache *cache;
3553 struct btrfs_transaction *cur_trans = trans->transaction;
3554 int ret = 0;
3555 int should_put;
3556 struct btrfs_path *path = NULL;
3557 LIST_HEAD(dirty);
3558 struct list_head *io = &cur_trans->io_bgs;
3559 int num_started = 0;
3560 int loops = 0;
3561
3562 spin_lock(&cur_trans->dirty_bgs_lock);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003563 if (list_empty(&cur_trans->dirty_bgs)) {
3564 spin_unlock(&cur_trans->dirty_bgs_lock);
3565 return 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003566 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003567 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Chris Mason1bbc6212015-04-06 12:46:08 -07003568 spin_unlock(&cur_trans->dirty_bgs_lock);
3569
3570again:
Chris Mason1bbc6212015-04-06 12:46:08 -07003571 /*
3572 * make sure all the block groups on our dirty list actually
3573 * exist
3574 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003575 btrfs_create_pending_block_groups(trans, fs_info);
Chris Mason1bbc6212015-04-06 12:46:08 -07003576
3577 if (!path) {
3578 path = btrfs_alloc_path();
3579 if (!path)
3580 return -ENOMEM;
3581 }
3582
Filipe Mananab58d1a92015-04-25 18:29:16 +01003583 /*
3584 * cache_write_mutex is here only to save us from balance or automatic
3585 * removal of empty block groups deleting this block group while we are
3586 * writing out the cache
3587 */
3588 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003589 while (!list_empty(&dirty)) {
3590 cache = list_first_entry(&dirty,
3591 struct btrfs_block_group_cache,
3592 dirty_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07003593 /*
3594 * this can happen if something re-dirties a block
3595 * group that is already under IO. Just wait for it to
3596 * finish and then do it all again
3597 */
3598 if (!list_empty(&cache->io_list)) {
3599 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003600 btrfs_wait_cache_io(trans, cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003601 btrfs_put_block_group(cache);
3602 }
3603
3604
3605 /*
3606 * btrfs_wait_cache_io uses the cache->dirty_list to decide
3607 * if it should update the cache_state. Don't delete
3608 * until after we wait.
3609 *
3610 * Since we're not running in the commit critical section
3611 * we need the dirty_bgs_lock to protect from update_block_group
3612 */
3613 spin_lock(&cur_trans->dirty_bgs_lock);
3614 list_del_init(&cache->dirty_list);
3615 spin_unlock(&cur_trans->dirty_bgs_lock);
3616
3617 should_put = 1;
3618
3619 cache_save_setup(cache, trans, path);
3620
3621 if (cache->disk_cache_state == BTRFS_DC_SETUP) {
3622 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003623 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003624 cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003625 if (ret == 0 && cache->io_ctl.inode) {
3626 num_started++;
3627 should_put = 0;
3628
3629 /*
3630 * the cache_write_mutex is protecting
3631 * the io_list
3632 */
3633 list_add_tail(&cache->io_list, io);
3634 } else {
3635 /*
3636 * if we failed to write the cache, the
3637 * generation will be bad and life goes on
3638 */
3639 ret = 0;
3640 }
3641 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003642 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003643 ret = write_one_cache_group(trans, fs_info,
3644 path, cache);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003645 /*
3646 * Our block group might still be attached to the list
3647 * of new block groups in the transaction handle of some
3648 * other task (struct btrfs_trans_handle->new_bgs). This
3649 * means its block group item isn't yet in the extent
3650 * tree. If this happens ignore the error, as we will
3651 * try again later in the critical section of the
3652 * transaction commit.
3653 */
3654 if (ret == -ENOENT) {
3655 ret = 0;
3656 spin_lock(&cur_trans->dirty_bgs_lock);
3657 if (list_empty(&cache->dirty_list)) {
3658 list_add_tail(&cache->dirty_list,
3659 &cur_trans->dirty_bgs);
3660 btrfs_get_block_group(cache);
3661 }
3662 spin_unlock(&cur_trans->dirty_bgs_lock);
3663 } else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003664 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003665 }
3666 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003667
3668 /* if its not on the io list, we need to put the block group */
3669 if (should_put)
3670 btrfs_put_block_group(cache);
3671
3672 if (ret)
3673 break;
Filipe Mananab58d1a92015-04-25 18:29:16 +01003674
3675 /*
3676 * Avoid blocking other tasks for too long. It might even save
3677 * us from writing caches for block groups that are going to be
3678 * removed.
3679 */
3680 mutex_unlock(&trans->transaction->cache_write_mutex);
3681 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003682 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003683 mutex_unlock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003684
3685 /*
3686 * go through delayed refs for all the stuff we've just kicked off
3687 * and then loop back (just once)
3688 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003689 ret = btrfs_run_delayed_refs(trans, fs_info, 0);
Chris Mason1bbc6212015-04-06 12:46:08 -07003690 if (!ret && loops == 0) {
3691 loops++;
3692 spin_lock(&cur_trans->dirty_bgs_lock);
3693 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003694 /*
3695 * dirty_bgs_lock protects us from concurrent block group
3696 * deletes too (not just cache_write_mutex).
3697 */
3698 if (!list_empty(&dirty)) {
3699 spin_unlock(&cur_trans->dirty_bgs_lock);
3700 goto again;
3701 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003702 spin_unlock(&cur_trans->dirty_bgs_lock);
Liu Boc79a1752016-07-20 17:44:12 -07003703 } else if (ret < 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003704 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
Chris Mason1bbc6212015-04-06 12:46:08 -07003705 }
3706
3707 btrfs_free_path(path);
3708 return ret;
3709}
3710
Chris Mason96b51792007-10-15 16:15:19 -04003711int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003712 struct btrfs_fs_info *fs_info)
Chris Mason9078a3e2007-04-26 16:46:15 -04003713{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003714 struct btrfs_block_group_cache *cache;
Josef Bacikce93ec52014-11-17 15:45:48 -05003715 struct btrfs_transaction *cur_trans = trans->transaction;
3716 int ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003717 int should_put;
Chris Mason9078a3e2007-04-26 16:46:15 -04003718 struct btrfs_path *path;
Chris Mason1bbc6212015-04-06 12:46:08 -07003719 struct list_head *io = &cur_trans->io_bgs;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003720 int num_started = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003721
3722 path = btrfs_alloc_path();
3723 if (!path)
3724 return -ENOMEM;
3725
Josef Bacikce93ec52014-11-17 15:45:48 -05003726 /*
Filipe Mananae44081e2015-12-18 03:02:48 +00003727 * Even though we are in the critical section of the transaction commit,
3728 * we can still have concurrent tasks adding elements to this
3729 * transaction's list of dirty block groups. These tasks correspond to
3730 * endio free space workers started when writeback finishes for a
3731 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
3732 * allocate new block groups as a result of COWing nodes of the root
3733 * tree when updating the free space inode. The writeback for the space
3734 * caches is triggered by an earlier call to
3735 * btrfs_start_dirty_block_groups() and iterations of the following
3736 * loop.
3737 * Also we want to do the cache_save_setup first and then run the
Josef Bacikce93ec52014-11-17 15:45:48 -05003738 * delayed refs to make sure we have the best chance at doing this all
3739 * in one shot.
3740 */
Filipe Mananae44081e2015-12-18 03:02:48 +00003741 spin_lock(&cur_trans->dirty_bgs_lock);
Josef Bacikce93ec52014-11-17 15:45:48 -05003742 while (!list_empty(&cur_trans->dirty_bgs)) {
3743 cache = list_first_entry(&cur_trans->dirty_bgs,
3744 struct btrfs_block_group_cache,
3745 dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003746
3747 /*
3748 * this can happen if cache_save_setup re-dirties a block
3749 * group that is already under IO. Just wait for it to
3750 * finish and then do it all again
3751 */
3752 if (!list_empty(&cache->io_list)) {
Filipe Mananae44081e2015-12-18 03:02:48 +00003753 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003754 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003755 btrfs_wait_cache_io(trans, cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003756 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003757 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003758 }
3759
Chris Mason1bbc6212015-04-06 12:46:08 -07003760 /*
3761 * don't remove from the dirty list until after we've waited
3762 * on any pending IO
3763 */
Josef Bacikce93ec52014-11-17 15:45:48 -05003764 list_del_init(&cache->dirty_list);
Filipe Mananae44081e2015-12-18 03:02:48 +00003765 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003766 should_put = 1;
3767
Chris Mason1bbc6212015-04-06 12:46:08 -07003768 cache_save_setup(cache, trans, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003769
Josef Bacikce93ec52014-11-17 15:45:48 -05003770 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003771 ret = btrfs_run_delayed_refs(trans, fs_info,
3772 (unsigned long) -1);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003773
3774 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3775 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003776 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003777 cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003778 if (ret == 0 && cache->io_ctl.inode) {
3779 num_started++;
3780 should_put = 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003781 list_add_tail(&cache->io_list, io);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003782 } else {
3783 /*
3784 * if we failed to write the cache, the
3785 * generation will be bad and life goes on
3786 */
3787 ret = 0;
3788 }
3789 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003790 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003791 ret = write_one_cache_group(trans, fs_info,
3792 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003793 /*
3794 * One of the free space endio workers might have
3795 * created a new block group while updating a free space
3796 * cache's inode (at inode.c:btrfs_finish_ordered_io())
3797 * and hasn't released its transaction handle yet, in
3798 * which case the new block group is still attached to
3799 * its transaction handle and its creation has not
3800 * finished yet (no block group item in the extent tree
3801 * yet, etc). If this is the case, wait for all free
3802 * space endio workers to finish and retry. This is a
3803 * a very rare case so no need for a more efficient and
3804 * complex approach.
3805 */
3806 if (ret == -ENOENT) {
3807 wait_event(cur_trans->writer_wait,
3808 atomic_read(&cur_trans->num_writers) == 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003809 ret = write_one_cache_group(trans, fs_info,
3810 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003811 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003812 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003813 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003814 }
Chris Masonc9dc4c62015-04-04 17:14:42 -07003815
3816 /* if its not on the io list, we need to put the block group */
3817 if (should_put)
3818 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003819 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003820 }
Filipe Mananae44081e2015-12-18 03:02:48 +00003821 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003822
Chris Mason1bbc6212015-04-06 12:46:08 -07003823 while (!list_empty(io)) {
3824 cache = list_first_entry(io, struct btrfs_block_group_cache,
Chris Masonc9dc4c62015-04-04 17:14:42 -07003825 io_list);
3826 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003827 btrfs_wait_cache_io(trans, cache, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003828 btrfs_put_block_group(cache);
3829 }
3830
Chris Mason9078a3e2007-04-26 16:46:15 -04003831 btrfs_free_path(path);
Josef Bacikce93ec52014-11-17 15:45:48 -05003832 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003833}
3834
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003835int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05003836{
3837 struct btrfs_block_group_cache *block_group;
3838 int readonly = 0;
3839
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003840 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003841 if (!block_group || block_group->ro)
3842 readonly = 1;
3843 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003844 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003845 return readonly;
3846}
3847
Filipe Mananaf78c4362016-05-09 13:15:41 +01003848bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3849{
3850 struct btrfs_block_group_cache *bg;
3851 bool ret = true;
3852
3853 bg = btrfs_lookup_block_group(fs_info, bytenr);
3854 if (!bg)
3855 return false;
3856
3857 spin_lock(&bg->lock);
3858 if (bg->ro)
3859 ret = false;
3860 else
3861 atomic_inc(&bg->nocow_writers);
3862 spin_unlock(&bg->lock);
3863
3864 /* no put on block group, done by btrfs_dec_nocow_writers */
3865 if (!ret)
3866 btrfs_put_block_group(bg);
3867
3868 return ret;
3869
3870}
3871
3872void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3873{
3874 struct btrfs_block_group_cache *bg;
3875
3876 bg = btrfs_lookup_block_group(fs_info, bytenr);
3877 ASSERT(bg);
3878 if (atomic_dec_and_test(&bg->nocow_writers))
3879 wake_up_atomic_t(&bg->nocow_writers);
3880 /*
3881 * Once for our lookup and once for the lookup done by a previous call
3882 * to btrfs_inc_nocow_writers()
3883 */
3884 btrfs_put_block_group(bg);
3885 btrfs_put_block_group(bg);
3886}
3887
3888static int btrfs_wait_nocow_writers_atomic_t(atomic_t *a)
3889{
3890 schedule();
3891 return 0;
3892}
3893
3894void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg)
3895{
3896 wait_on_atomic_t(&bg->nocow_writers,
3897 btrfs_wait_nocow_writers_atomic_t,
3898 TASK_UNINTERRUPTIBLE);
3899}
3900
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003901static const char *alloc_name(u64 flags)
3902{
3903 switch (flags) {
3904 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
3905 return "mixed";
3906 case BTRFS_BLOCK_GROUP_METADATA:
3907 return "metadata";
3908 case BTRFS_BLOCK_GROUP_DATA:
3909 return "data";
3910 case BTRFS_BLOCK_GROUP_SYSTEM:
3911 return "system";
3912 default:
3913 WARN_ON(1);
3914 return "invalid-combination";
3915 };
3916}
3917
Chris Mason593060d2008-03-25 16:50:33 -04003918static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3919 u64 total_bytes, u64 bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -04003920 u64 bytes_readonly,
Chris Mason593060d2008-03-25 16:50:33 -04003921 struct btrfs_space_info **space_info)
3922{
3923 struct btrfs_space_info *found;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003924 int i;
3925 int factor;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003926 int ret;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003927
3928 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3929 BTRFS_BLOCK_GROUP_RAID10))
3930 factor = 2;
3931 else
3932 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003933
3934 found = __find_space_info(info, flags);
3935 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003936 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003937 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003938 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003939 found->bytes_used += bytes_used;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003940 found->disk_used += bytes_used * factor;
Josef Bacike40edf22016-03-25 13:25:47 -04003941 found->bytes_readonly += bytes_readonly;
Filipe Manana2e6e5182015-05-12 00:28:11 +01003942 if (total_bytes > 0)
3943 found->full = 0;
Josef Bacik957780e2016-05-17 13:30:55 -04003944 space_info_add_new_bytes(info, found, total_bytes -
3945 bytes_used - bytes_readonly);
Josef Bacik25179202008-10-29 14:49:05 -04003946 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003947 *space_info = found;
3948 return 0;
3949 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003950 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003951 if (!found)
3952 return -ENOMEM;
3953
Tejun Heo908c7f12014-09-08 09:51:29 +09003954 ret = percpu_counter_init(&found->total_bytes_pinned, 0, GFP_KERNEL);
Josef Bacikb150a4f2013-06-19 15:00:04 -04003955 if (ret) {
3956 kfree(found);
3957 return ret;
3958 }
3959
Jeff Mahoneyc1895442014-05-27 12:59:57 -04003960 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
Yan, Zhengb742bb822010-05-16 10:46:24 -04003961 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003962 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003963 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003964 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003965 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003966 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003967 found->bytes_used = bytes_used;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003968 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003969 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003970 found->bytes_reserved = 0;
Josef Bacike40edf22016-03-25 13:25:47 -04003971 found->bytes_readonly = bytes_readonly;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003972 found->bytes_may_use = 0;
Filipe Manana6af3e3a2015-09-07 10:41:12 +01003973 found->full = 0;
Josef Bacik4f4db212015-09-29 11:40:47 -04003974 found->max_extent_size = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003975 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003976 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003977 found->flush = 0;
3978 init_waitqueue_head(&found->wait);
Josef Bacik633c0aa2014-10-31 09:49:34 -04003979 INIT_LIST_HEAD(&found->ro_bgs);
Josef Bacik957780e2016-05-17 13:30:55 -04003980 INIT_LIST_HEAD(&found->tickets);
3981 INIT_LIST_HEAD(&found->priority_tickets);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003982
3983 ret = kobject_init_and_add(&found->kobj, &space_info_ktype,
3984 info->space_info_kobj, "%s",
3985 alloc_name(found->flags));
3986 if (ret) {
3987 kfree(found);
3988 return ret;
3989 }
3990
Chris Mason593060d2008-03-25 16:50:33 -04003991 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003992 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003993 if (flags & BTRFS_BLOCK_GROUP_DATA)
3994 info->data_sinfo = found;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003995
3996 return ret;
Chris Mason593060d2008-03-25 16:50:33 -04003997}
3998
Chris Mason8790d502008-04-03 16:29:03 -04003999static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
4000{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03004001 u64 extra_flags = chunk_to_extended(flags) &
4002 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004003
Miao Xiede98ced2013-01-29 10:13:12 +00004004 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004005 if (flags & BTRFS_BLOCK_GROUP_DATA)
4006 fs_info->avail_data_alloc_bits |= extra_flags;
4007 if (flags & BTRFS_BLOCK_GROUP_METADATA)
4008 fs_info->avail_metadata_alloc_bits |= extra_flags;
4009 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
4010 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004011 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04004012}
Chris Mason593060d2008-03-25 16:50:33 -04004013
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004014/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004015 * returns target flags in extended format or 0 if restripe for this
4016 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04004017 *
4018 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004019 */
4020static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
4021{
4022 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4023 u64 target = 0;
4024
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004025 if (!bctl)
4026 return 0;
4027
4028 if (flags & BTRFS_BLOCK_GROUP_DATA &&
4029 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4030 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
4031 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
4032 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4033 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
4034 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
4035 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4036 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
4037 }
4038
4039 return target;
4040}
4041
4042/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004043 * @flags: available profiles in extended format (see ctree.h)
4044 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004045 * Returns reduced profile in chunk format. If profile changing is in
4046 * progress (either running or paused) picks the target profile (if it's
4047 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004048 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004049static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04004050{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004051 u64 num_devices = fs_info->fs_devices->rw_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004052 u64 target;
Zhao Lei9c170b22015-09-15 21:08:08 +08004053 u64 raid_type;
4054 u64 allowed = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004055
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004056 /*
4057 * see if restripe for this chunk_type is in progress, if so
4058 * try to reduce to the target profile
4059 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004060 spin_lock(&fs_info->balance_lock);
4061 target = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004062 if (target) {
4063 /* pick target profile only if it's already available */
4064 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004065 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004066 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004067 }
4068 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004069 spin_unlock(&fs_info->balance_lock);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004070
David Woodhouse53b381b2013-01-29 18:40:14 -05004071 /* First, mask out the RAID levels which aren't possible */
Zhao Lei9c170b22015-09-15 21:08:08 +08004072 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
4073 if (num_devices >= btrfs_raid_array[raid_type].devs_min)
4074 allowed |= btrfs_raid_group[raid_type];
4075 }
4076 allowed &= flags;
Chris Masona061fc82008-05-07 11:43:44 -04004077
Zhao Lei9c170b22015-09-15 21:08:08 +08004078 if (allowed & BTRFS_BLOCK_GROUP_RAID6)
4079 allowed = BTRFS_BLOCK_GROUP_RAID6;
4080 else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
4081 allowed = BTRFS_BLOCK_GROUP_RAID5;
4082 else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
4083 allowed = BTRFS_BLOCK_GROUP_RAID10;
4084 else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
4085 allowed = BTRFS_BLOCK_GROUP_RAID1;
4086 else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
4087 allowed = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04004088
Zhao Lei9c170b22015-09-15 21:08:08 +08004089 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
Chris Masonec44a352008-04-28 15:29:52 -04004090
Zhao Lei9c170b22015-09-15 21:08:08 +08004091 return extended_to_chunk(flags | allowed);
Chris Masonec44a352008-04-28 15:29:52 -04004092}
4093
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004094static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05004095{
Miao Xiede98ced2013-01-29 10:13:12 +00004096 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004097 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004098
4099 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004100 flags = orig_flags;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004101 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00004102
4103 if (flags & BTRFS_BLOCK_GROUP_DATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004104 flags |= fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004105 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004106 flags |= fs_info->avail_system_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004107 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004108 flags |= fs_info->avail_metadata_alloc_bits;
4109 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02004110
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004111 return btrfs_reduce_alloc_profile(fs_info, flags);
Yan, Zhengb742bb822010-05-16 10:46:24 -04004112}
Josef Bacik6a632092009-02-20 11:00:09 -05004113
Miao Xie6d07bce2011-01-05 10:07:31 +00004114u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04004115{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004116 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb822010-05-16 10:46:24 -04004117 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05004118 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004119
Yan, Zhengb742bb822010-05-16 10:46:24 -04004120 if (data)
4121 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004122 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb822010-05-16 10:46:24 -04004123 flags = BTRFS_BLOCK_GROUP_SYSTEM;
4124 else
4125 flags = BTRFS_BLOCK_GROUP_METADATA;
4126
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004127 ret = get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05004128 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004129}
4130
Liu Bo41361352017-02-13 15:42:21 -08004131static u64 btrfs_space_info_used(struct btrfs_space_info *s_info,
4132 bool may_use_included)
4133{
4134 ASSERT(s_info);
4135 return s_info->bytes_used + s_info->bytes_reserved +
4136 s_info->bytes_pinned + s_info->bytes_readonly +
4137 (may_use_included ? s_info->bytes_may_use : 0);
4138}
4139
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004140int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05004141{
4142 struct btrfs_space_info *data_sinfo;
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004143 struct btrfs_root *root = inode->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004144 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00004145 u64 used;
Zhao Lei94b947b2015-02-14 13:23:45 +08004146 int ret = 0;
Zhao Leic99f1b02015-03-02 19:32:20 +08004147 int need_commit = 2;
4148 int have_pinned_space;
Josef Bacik6a632092009-02-20 11:00:09 -05004149
4150 /* make sure bytes are sectorsize aligned */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004151 bytes = ALIGN(bytes, fs_info->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05004152
Miao Xie9dced182013-10-25 17:33:36 +08004153 if (btrfs_is_free_space_inode(inode)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004154 need_commit = 0;
Miao Xie9dced182013-10-25 17:33:36 +08004155 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04004156 }
4157
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004158 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04004159 if (!data_sinfo)
4160 goto alloc;
4161
Josef Bacik6a632092009-02-20 11:00:09 -05004162again:
4163 /* make sure we have enough space to handle the data first */
4164 spin_lock(&data_sinfo->lock);
Liu Bo41361352017-02-13 15:42:21 -08004165 used = btrfs_space_info_used(data_sinfo, true);
Josef Bacikab6e24102010-03-19 14:38:13 +00004166
4167 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004168 struct btrfs_trans_handle *trans;
4169
Josef Bacik6a632092009-02-20 11:00:09 -05004170 /*
4171 * if we don't have enough free bytes in this space then we need
4172 * to alloc a new chunk.
4173 */
Zhao Leib9fd47c2015-02-09 14:40:20 +08004174 if (!data_sinfo->full) {
Josef Bacik6a632092009-02-20 11:00:09 -05004175 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05004176
Chris Mason0e4f8f82011-04-15 16:05:44 -04004177 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05004178 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04004179alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05004180 alloc_target = btrfs_get_alloc_profile(root, 1);
Miao Xie9dced182013-10-25 17:33:36 +08004181 /*
4182 * It is ugly that we don't call nolock join
4183 * transaction for the free space inode case here.
4184 * But it is safe because we only do the data space
4185 * reservation for the free space cache in the
4186 * transaction context, the common join transaction
4187 * just increase the counter of the current transaction
4188 * handler, doesn't try to acquire the trans_lock of
4189 * the fs.
4190 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004191 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004192 if (IS_ERR(trans))
4193 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05004194
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004195 ret = do_chunk_alloc(trans, fs_info, alloc_target,
Chris Mason0e4f8f82011-04-15 16:05:44 -04004196 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004197 btrfs_end_transaction(trans);
Miao Xied52a5b52011-01-05 10:07:18 +00004198 if (ret < 0) {
4199 if (ret != -ENOSPC)
4200 return ret;
Zhao Leic99f1b02015-03-02 19:32:20 +08004201 else {
4202 have_pinned_space = 1;
Miao Xied52a5b52011-01-05 10:07:18 +00004203 goto commit_trans;
Zhao Leic99f1b02015-03-02 19:32:20 +08004204 }
Miao Xied52a5b52011-01-05 10:07:18 +00004205 }
Chris Mason33b4d472009-09-22 14:45:50 -04004206
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004207 if (!data_sinfo)
4208 data_sinfo = fs_info->data_sinfo;
4209
Josef Bacik6a632092009-02-20 11:00:09 -05004210 goto again;
4211 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004212
4213 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04004214 * If we don't have enough pinned space to deal with this
Zhao Lei94b947b2015-02-14 13:23:45 +08004215 * allocation, and no removed chunk in current transaction,
4216 * don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004217 */
Zhao Leic99f1b02015-03-02 19:32:20 +08004218 have_pinned_space = percpu_counter_compare(
4219 &data_sinfo->total_bytes_pinned,
4220 used + bytes - data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05004221 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004222
4223 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00004224commit_trans:
Zhao Leic99f1b02015-03-02 19:32:20 +08004225 if (need_commit &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004226 !atomic_read(&fs_info->open_ioctl_trans)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004227 need_commit--;
Josef Bacikb150a4f2013-06-19 15:00:04 -04004228
Zhao Leie1746e82015-12-01 18:39:40 +08004229 if (need_commit > 0) {
4230 btrfs_start_delalloc_roots(fs_info, 0, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004231 btrfs_wait_ordered_roots(fs_info, -1, 0,
4232 (u64)-1);
Zhao Leie1746e82015-12-01 18:39:40 +08004233 }
Zhao Lei9a4e7272015-04-09 12:34:43 +08004234
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004235 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004236 if (IS_ERR(trans))
4237 return PTR_ERR(trans);
Zhao Leic99f1b02015-03-02 19:32:20 +08004238 if (have_pinned_space >= 0 ||
Josef Bacik3204d332015-09-24 10:46:10 -04004239 test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
4240 &trans->transaction->flags) ||
Zhao Leic99f1b02015-03-02 19:32:20 +08004241 need_commit > 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004242 ret = btrfs_commit_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004243 if (ret)
4244 return ret;
Zhao Leid7c15172015-02-26 10:49:20 +08004245 /*
Filipe Mananac2d6cb12016-01-15 11:05:12 +00004246 * The cleaner kthread might still be doing iput
4247 * operations. Wait for it to finish so that
4248 * more space is released.
Zhao Leid7c15172015-02-26 10:49:20 +08004249 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004250 mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
4251 mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
Zhao Lei94b947b2015-02-14 13:23:45 +08004252 goto again;
4253 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004254 btrfs_end_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004255 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004256 }
4257
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004258 trace_btrfs_space_reservation(fs_info,
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004259 "space_info:enospc",
4260 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004261 return -ENOSPC;
4262 }
4263 data_sinfo->bytes_may_use += bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004264 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004265 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004266 spin_unlock(&data_sinfo->lock);
4267
Dongsheng Yang237c0e92014-12-29 06:23:05 -05004268 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004269}
4270
4271/*
Qu Wenruo4ceff072015-09-08 17:22:42 +08004272 * New check_data_free_space() with ability for precious data reservation
4273 * Will replace old btrfs_check_data_free_space(), but for patch split,
4274 * add a new function first and then replace it.
4275 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004276int btrfs_check_data_free_space(struct inode *inode, u64 start, u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004277{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004278 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004279 int ret;
4280
4281 /* align the range */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004282 len = round_up(start + len, fs_info->sectorsize) -
4283 round_down(start, fs_info->sectorsize);
4284 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004285
Nikolay Borisov04f4f912017-02-20 13:50:36 +02004286 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004287 if (ret < 0)
4288 return ret;
4289
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004290 /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
Qu Wenruo4ceff072015-09-08 17:22:42 +08004291 ret = btrfs_qgroup_reserve_data(inode, start, len);
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004292 if (ret)
4293 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004294 return ret;
4295}
4296
4297/*
Qu Wenruo4ceff072015-09-08 17:22:42 +08004298 * Called if we need to clear a data reservation for this inode
4299 * Normally in a error case.
4300 *
Qu Wenruo51773be2015-10-08 18:19:37 +08004301 * This one will *NOT* use accurate qgroup reserved space API, just for case
4302 * which we can't sleep and is sure it won't affect qgroup reserved space.
4303 * Like clear_bit_hook().
Qu Wenruo4ceff072015-09-08 17:22:42 +08004304 */
Qu Wenruo51773be2015-10-08 18:19:37 +08004305void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
4306 u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004307{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004308 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004309 struct btrfs_space_info *data_sinfo;
4310
4311 /* Make sure the range is aligned to sectorsize */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004312 len = round_up(start + len, fs_info->sectorsize) -
4313 round_down(start, fs_info->sectorsize);
4314 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004315
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004316 data_sinfo = fs_info->data_sinfo;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004317 spin_lock(&data_sinfo->lock);
4318 if (WARN_ON(data_sinfo->bytes_may_use < len))
4319 data_sinfo->bytes_may_use = 0;
4320 else
4321 data_sinfo->bytes_may_use -= len;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004322 trace_btrfs_space_reservation(fs_info, "space_info",
Qu Wenruo4ceff072015-09-08 17:22:42 +08004323 data_sinfo->flags, len, 0);
4324 spin_unlock(&data_sinfo->lock);
4325}
4326
Qu Wenruo51773be2015-10-08 18:19:37 +08004327/*
4328 * Called if we need to clear a data reservation for this inode
4329 * Normally in a error case.
4330 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04004331 * This one will handle the per-inode data rsv map for accurate reserved
Qu Wenruo51773be2015-10-08 18:19:37 +08004332 * space framework.
4333 */
4334void btrfs_free_reserved_data_space(struct inode *inode, u64 start, u64 len)
4335{
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004336 struct btrfs_root *root = BTRFS_I(inode)->root;
4337
4338 /* Make sure the range is aligned to sectorsize */
Jeff Mahoneyda170662016-06-15 09:22:56 -04004339 len = round_up(start + len, root->fs_info->sectorsize) -
4340 round_down(start, root->fs_info->sectorsize);
4341 start = round_down(start, root->fs_info->sectorsize);
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004342
Qu Wenruo51773be2015-10-08 18:19:37 +08004343 btrfs_free_reserved_data_space_noquota(inode, start, len);
4344 btrfs_qgroup_free_data(inode, start, len);
4345}
4346
Josef Bacik97e728d2009-04-21 17:40:57 -04004347static void force_metadata_allocation(struct btrfs_fs_info *info)
4348{
4349 struct list_head *head = &info->space_info;
4350 struct btrfs_space_info *found;
4351
4352 rcu_read_lock();
4353 list_for_each_entry_rcu(found, head, list) {
4354 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004355 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04004356 }
4357 rcu_read_unlock();
4358}
4359
Miao Xie3c76cd82013-04-25 10:12:38 +00004360static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
4361{
4362 return (global->size << 1);
4363}
4364
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004365static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
Josef Bacik698d0082012-09-12 14:08:47 -04004366 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04004367{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004368 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04004369 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04004370 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04004371 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04004372
Chris Mason0e4f8f82011-04-15 16:05:44 -04004373 if (force == CHUNK_ALLOC_FORCE)
4374 return 1;
4375
4376 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04004377 * We need to take into account the global rsv because for all intents
4378 * and purposes it's used space. Don't worry about locking the
4379 * global_rsv, it doesn't change except when the transaction commits.
4380 */
Josef Bacik54338b52012-08-14 16:20:52 -04004381 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Miao Xie3c76cd82013-04-25 10:12:38 +00004382 num_allocated += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04004383
4384 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04004385 * in limited mode, we want to have some free space up to
4386 * about 1% of the FS size.
4387 */
4388 if (force == CHUNK_ALLOC_LIMITED) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004389 thresh = btrfs_super_total_bytes(fs_info->super_copy);
Byongho Leeee221842015-12-15 01:42:10 +09004390 thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
Chris Mason0e4f8f82011-04-15 16:05:44 -04004391
4392 if (num_bytes - num_allocated < thresh)
4393 return 1;
4394 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004395
Byongho Leeee221842015-12-15 01:42:10 +09004396 if (num_allocated + SZ_2M < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04004397 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04004398 return 1;
4399}
4400
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004401static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004402{
4403 u64 num_dev;
4404
David Woodhouse53b381b2013-01-29 18:40:14 -05004405 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
4406 BTRFS_BLOCK_GROUP_RAID0 |
4407 BTRFS_BLOCK_GROUP_RAID5 |
4408 BTRFS_BLOCK_GROUP_RAID6))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004409 num_dev = fs_info->fs_devices->rw_devices;
Liu Bo15d1ff82012-03-29 09:57:44 -04004410 else if (type & BTRFS_BLOCK_GROUP_RAID1)
4411 num_dev = 2;
4412 else
4413 num_dev = 1; /* DUP or single */
4414
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004415 return num_dev;
Liu Bo15d1ff82012-03-29 09:57:44 -04004416}
4417
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004418/*
4419 * If @is_allocation is true, reserve space in the system space info necessary
4420 * for allocating a chunk, otherwise if it's false, reserve space necessary for
4421 * removing a chunk.
4422 */
4423void check_system_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004424 struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004425{
4426 struct btrfs_space_info *info;
4427 u64 left;
4428 u64 thresh;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004429 int ret = 0;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004430 u64 num_devs;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004431
4432 /*
4433 * Needed because we can end up allocating a system chunk and for an
4434 * atomic and race free space reservation in the chunk block reserve.
4435 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004436 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
Liu Bo15d1ff82012-03-29 09:57:44 -04004437
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004438 info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
Liu Bo15d1ff82012-03-29 09:57:44 -04004439 spin_lock(&info->lock);
Liu Bo41361352017-02-13 15:42:21 -08004440 left = info->total_bytes - btrfs_space_info_used(info, true);
Liu Bo15d1ff82012-03-29 09:57:44 -04004441 spin_unlock(&info->lock);
4442
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004443 num_devs = get_profile_num_devs(fs_info, type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004444
4445 /* num_devs device items to update and 1 chunk item to add or remove */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004446 thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) +
4447 btrfs_calc_trans_metadata_size(fs_info, 1);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004448
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004449 if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
4450 btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
4451 left, thresh, type);
4452 dump_space_info(fs_info, info, 0, 0);
Liu Bo15d1ff82012-03-29 09:57:44 -04004453 }
4454
4455 if (left < thresh) {
4456 u64 flags;
4457
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004458 flags = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004459 /*
4460 * Ignore failure to create system chunk. We might end up not
4461 * needing it, as we might not need to COW all nodes/leafs from
4462 * the paths we visit in the chunk tree (they were already COWed
4463 * or created in the current transaction for example).
4464 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004465 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004466 }
4467
4468 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004469 ret = btrfs_block_rsv_add(fs_info->chunk_root,
4470 &fs_info->chunk_block_rsv,
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004471 thresh, BTRFS_RESERVE_NO_FLUSH);
4472 if (!ret)
4473 trans->chunk_bytes_reserved += thresh;
Liu Bo15d1ff82012-03-29 09:57:44 -04004474 }
4475}
4476
Liu Bo28b737f2016-07-29 11:09:50 -07004477/*
4478 * If force is CHUNK_ALLOC_FORCE:
4479 * - return 1 if it successfully allocates a chunk,
4480 * - return errors including -ENOSPC otherwise.
4481 * If force is NOT CHUNK_ALLOC_FORCE:
4482 * - return 0 if it doesn't need to allocate a new chunk,
4483 * - return 1 if it successfully allocates a chunk,
4484 * - return errors including -ENOSPC otherwise.
4485 */
Chris Mason6324fbf2008-03-24 15:01:59 -04004486static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004487 struct btrfs_fs_info *fs_info, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04004488{
4489 struct btrfs_space_info *space_info;
Josef Bacik6d741192011-04-11 20:20:11 -04004490 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05004491 int ret = 0;
4492
Josef Bacikc6b305a2012-12-18 09:16:16 -05004493 /* Don't re-enter if we're already allocating a chunk */
4494 if (trans->allocating_chunk)
4495 return -ENOSPC;
4496
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004497 space_info = __find_space_info(fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04004498 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004499 ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004500 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04004501 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004502 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04004503
Josef Bacik6d741192011-04-11 20:20:11 -04004504again:
Josef Bacik25179202008-10-29 14:49:05 -04004505 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05004506 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004507 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04004508 if (space_info->full) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004509 if (should_alloc_chunk(fs_info, space_info, force))
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004510 ret = -ENOSPC;
4511 else
4512 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04004513 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004514 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04004515 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004516
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004517 if (!should_alloc_chunk(fs_info, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04004518 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04004519 return 0;
4520 } else if (space_info->chunk_alloc) {
4521 wait_for_alloc = 1;
4522 } else {
4523 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04004524 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004525
Josef Bacik25179202008-10-29 14:49:05 -04004526 spin_unlock(&space_info->lock);
4527
Josef Bacik6d741192011-04-11 20:20:11 -04004528 mutex_lock(&fs_info->chunk_mutex);
4529
4530 /*
4531 * The chunk_mutex is held throughout the entirety of a chunk
4532 * allocation, so once we've acquired the chunk_mutex we know that the
4533 * other guy is done and we need to recheck and see if we should
4534 * allocate.
4535 */
4536 if (wait_for_alloc) {
4537 mutex_unlock(&fs_info->chunk_mutex);
4538 wait_for_alloc = 0;
4539 goto again;
4540 }
4541
Josef Bacikc6b305a2012-12-18 09:16:16 -05004542 trans->allocating_chunk = true;
4543
Josef Bacik97e728d2009-04-21 17:40:57 -04004544 /*
Josef Bacik67377732010-09-16 16:19:09 -04004545 * If we have mixed data/metadata chunks we want to make sure we keep
4546 * allocating mixed chunks instead of individual chunks.
4547 */
4548 if (btrfs_mixed_space_info(space_info))
4549 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
4550
4551 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04004552 * if we're doing a data chunk, go ahead and make sure that
4553 * we keep a reasonable number of metadata chunks allocated in the
4554 * FS as well.
4555 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04004556 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04004557 fs_info->data_chunk_allocations++;
4558 if (!(fs_info->data_chunk_allocations %
4559 fs_info->metadata_ratio))
4560 force_metadata_allocation(fs_info);
4561 }
4562
Liu Bo15d1ff82012-03-29 09:57:44 -04004563 /*
4564 * Check if we have enough space in SYSTEM chunk because we may need
4565 * to update devices.
4566 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004567 check_system_chunk(trans, fs_info, flags);
Liu Bo15d1ff82012-03-29 09:57:44 -04004568
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004569 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05004570 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004571
Josef Bacik9ed74f22009-09-11 16:12:44 -04004572 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004573 if (ret < 0 && ret != -ENOSPC)
4574 goto out;
Chris Masond3977122009-01-05 21:25:51 -05004575 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04004576 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04004577 else
4578 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04004579
Chris Mason0e4f8f82011-04-15 16:05:44 -04004580 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004581out:
Josef Bacik6d741192011-04-11 20:20:11 -04004582 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004583 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03004584 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana00d80e32015-07-20 14:56:20 +01004585 /*
4586 * When we allocate a new chunk we reserve space in the chunk block
4587 * reserve to make sure we can COW nodes/leafs in the chunk tree or
4588 * add new nodes/leafs to it if we end up needing to do it when
4589 * inserting the chunk item and updating device items as part of the
4590 * second phase of chunk allocation, performed by
4591 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
4592 * large number of new block groups to create in our transaction
4593 * handle's new_bgs list to avoid exhausting the chunk block reserve
4594 * in extreme cases - like having a single transaction create many new
4595 * block groups when starting to write out the free space caches of all
4596 * the block groups that were made dirty during the lifetime of the
4597 * transaction.
4598 */
Filipe Mananad9a05402015-10-03 13:13:13 +01004599 if (trans->can_flush_pending_bgs &&
Byongho Leeee221842015-12-15 01:42:10 +09004600 trans->chunk_bytes_reserved >= (u64)SZ_2M) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004601 btrfs_create_pending_block_groups(trans, fs_info);
Filipe Manana00d80e32015-07-20 14:56:20 +01004602 btrfs_trans_release_chunk_metadata(trans);
4603 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004604 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04004605}
4606
Josef Bacika80c8dc2012-09-06 16:59:33 -04004607static int can_overcommit(struct btrfs_root *root,
4608 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00004609 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004610{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004611 struct btrfs_fs_info *fs_info = root->fs_info;
4612 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04004613 u64 profile;
Miao Xie3c76cd82013-04-25 10:12:38 +00004614 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004615 u64 avail;
4616 u64 used;
4617
Josef Bacik957780e2016-05-17 13:30:55 -04004618 /* Don't overcommit when in mixed mode. */
4619 if (space_info->flags & BTRFS_BLOCK_GROUP_DATA)
4620 return 0;
4621
Josef Bacik957780e2016-05-17 13:30:55 -04004622 profile = btrfs_get_alloc_profile(root, 0);
Liu Bo41361352017-02-13 15:42:21 -08004623 used = btrfs_space_info_used(space_info, false);
Josef Bacik96f1bb52013-01-30 17:02:51 -05004624
Josef Bacik96f1bb52013-01-30 17:02:51 -05004625 /*
4626 * We only want to allow over committing if we have lots of actual space
4627 * free, but if we don't have enough space to handle the global reserve
4628 * space then we could end up having a real enospc problem when trying
4629 * to allocate a chunk or some other such important allocation.
4630 */
Miao Xie3c76cd82013-04-25 10:12:38 +00004631 spin_lock(&global_rsv->lock);
4632 space_size = calc_global_rsv_need_space(global_rsv);
4633 spin_unlock(&global_rsv->lock);
4634 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05004635 return 0;
4636
4637 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004638
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004639 spin_lock(&fs_info->free_chunk_lock);
4640 avail = fs_info->free_chunk_space;
4641 spin_unlock(&fs_info->free_chunk_lock);
Josef Bacika80c8dc2012-09-06 16:59:33 -04004642
4643 /*
4644 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05004645 * space is actually useable. For raid56, the space info used
4646 * doesn't include the parity drive, so we don't have to
4647 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04004648 */
4649 if (profile & (BTRFS_BLOCK_GROUP_DUP |
4650 BTRFS_BLOCK_GROUP_RAID1 |
4651 BTRFS_BLOCK_GROUP_RAID10))
4652 avail >>= 1;
4653
4654 /*
Miao Xie561c2942012-10-16 11:32:18 +00004655 * If we aren't flushing all things, let us overcommit up to
4656 * 1/2th of the space. If we can flush, don't let us overcommit
4657 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04004658 */
Miao Xie08e007d2012-10-16 11:33:38 +00004659 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04004660 avail >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004661 else
Josef Bacik14575ae2013-09-17 10:48:00 -04004662 avail >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004663
Josef Bacik14575ae2013-09-17 10:48:00 -04004664 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004665 return 1;
4666 return 0;
4667}
4668
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004669static void btrfs_writeback_inodes_sb_nr(struct btrfs_fs_info *fs_info,
Miao Xie6c255e62014-03-06 13:55:01 +08004670 unsigned long nr_pages, int nr_items)
Miao Xieda633a42012-12-20 11:19:09 +00004671{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004672 struct super_block *sb = fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00004673
Josef Bacik925a6ef2013-06-20 12:31:27 -04004674 if (down_read_trylock(&sb->s_umount)) {
4675 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4676 up_read(&sb->s_umount);
4677 } else {
Miao Xieda633a42012-12-20 11:19:09 +00004678 /*
4679 * We needn't worry the filesystem going from r/w to r/o though
4680 * we don't acquire ->s_umount mutex, because the filesystem
4681 * should guarantee the delalloc inodes list be empty after
4682 * the filesystem is readonly(all dirty pages are written to
4683 * the disk).
4684 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004685 btrfs_start_delalloc_roots(fs_info, 0, nr_items);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004686 if (!current->journal_info)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004687 btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1);
Miao Xieda633a42012-12-20 11:19:09 +00004688 }
4689}
4690
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004691static inline int calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,
4692 u64 to_reclaim)
Miao Xie18cd8ea2013-11-04 23:13:22 +08004693{
4694 u64 bytes;
4695 int nr;
4696
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004697 bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Miao Xie18cd8ea2013-11-04 23:13:22 +08004698 nr = (int)div64_u64(to_reclaim, bytes);
4699 if (!nr)
4700 nr = 1;
4701 return nr;
4702}
4703
Byongho Leeee221842015-12-15 01:42:10 +09004704#define EXTENT_SIZE_PER_ITEM SZ_256K
Miao Xiec61a16a2013-11-04 23:13:23 +08004705
Yan, Zheng5da9d012010-05-16 10:46:25 -04004706/*
4707 * shrink metadata reservation for delalloc
4708 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04004709static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4710 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004711{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004712 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004713 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004714 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004715 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004716 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004717 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004718 long time_left;
Miao Xied3ee29e32013-11-04 23:13:20 +08004719 unsigned long nr_pages;
4720 int loops;
Miao Xieb0244192013-11-04 23:13:25 +08004721 int items;
Miao Xie08e007d2012-10-16 11:33:38 +00004722 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004723
Miao Xiec61a16a2013-11-04 23:13:23 +08004724 /* Calc the number of the pages we need flush for space reservation */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004725 items = calc_reclaim_items_nr(fs_info, to_reclaim);
Adam Borowski8eb0dfd2016-05-08 15:08:00 +02004726 to_reclaim = (u64)items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004727
Josef Bacik663350a2011-11-03 22:54:25 -04004728 trans = (struct btrfs_trans_handle *)current->journal_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004729 block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004730 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04004731
Miao Xie963d6782013-01-29 10:10:51 +00004732 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004733 &fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004734 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004735 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004736 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004737 if (wait_ordered)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004738 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004739 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004740 }
4741
Miao Xied3ee29e32013-11-04 23:13:20 +08004742 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004743 while (delalloc_bytes && loops < 3) {
4744 max_reclaim = min(delalloc_bytes, to_reclaim);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004745 nr_pages = max_reclaim >> PAGE_SHIFT;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004746 btrfs_writeback_inodes_sb_nr(fs_info, nr_pages, items);
Josef Bacikdea31f52012-09-06 16:47:00 -04004747 /*
4748 * We need to wait for the async pages to actually start before
4749 * we do anything.
4750 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004751 max_reclaim = atomic_read(&fs_info->async_delalloc_pages);
Miao Xie9f3a0742013-11-04 23:13:24 +08004752 if (!max_reclaim)
4753 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004754
Miao Xie9f3a0742013-11-04 23:13:24 +08004755 if (max_reclaim <= nr_pages)
4756 max_reclaim = 0;
4757 else
4758 max_reclaim -= nr_pages;
4759
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004760 wait_event(fs_info->async_submit_wait,
4761 atomic_read(&fs_info->async_delalloc_pages) <=
Miao Xie9f3a0742013-11-04 23:13:24 +08004762 (int)max_reclaim);
4763skip_async:
Miao Xie08e007d2012-10-16 11:33:38 +00004764 if (!trans)
4765 flush = BTRFS_RESERVE_FLUSH_ALL;
4766 else
4767 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04004768 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00004769 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004770 spin_unlock(&space_info->lock);
4771 break;
4772 }
Josef Bacik957780e2016-05-17 13:30:55 -04004773 if (list_empty(&space_info->tickets) &&
4774 list_empty(&space_info->priority_tickets)) {
4775 spin_unlock(&space_info->lock);
4776 break;
4777 }
Josef Bacik0019f102010-10-15 15:18:40 -04004778 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004779
Chris Mason36e39c42011-03-12 07:08:42 -05004780 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004781 if (wait_ordered && !trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004782 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf104d042011-10-14 13:56:58 -04004783 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004784 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004785 if (time_left)
4786 break;
4787 }
Miao Xie963d6782013-01-29 10:10:51 +00004788 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004789 &fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004790 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004791}
4792
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004793/**
Josef Bacik663350a2011-11-03 22:54:25 -04004794 * maybe_commit_transaction - possibly commit the transaction if its ok to
4795 * @root - the root we're allocating for
4796 * @bytes - the number of bytes we want to reserve
4797 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004798 *
Josef Bacik663350a2011-11-03 22:54:25 -04004799 * This will check to make sure that committing the transaction will actually
4800 * get us somewhere and then commit the transaction if it does. Otherwise it
4801 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004802 */
Jeff Mahoney0c9ab342017-02-15 16:28:28 -05004803static int may_commit_transaction(struct btrfs_fs_info *fs_info,
Josef Bacik663350a2011-11-03 22:54:25 -04004804 struct btrfs_space_info *space_info,
4805 u64 bytes, int force)
4806{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004807 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;
Josef Bacik663350a2011-11-03 22:54:25 -04004808 struct btrfs_trans_handle *trans;
4809
4810 trans = (struct btrfs_trans_handle *)current->journal_info;
4811 if (trans)
4812 return -EAGAIN;
4813
4814 if (force)
4815 goto commit;
4816
4817 /* See if there is enough pinned space to make this reservation */
Josef Bacikb150a4f2013-06-19 15:00:04 -04004818 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Miao Xie0424c542014-03-06 13:54:59 +08004819 bytes) >= 0)
Josef Bacik663350a2011-11-03 22:54:25 -04004820 goto commit;
Josef Bacik663350a2011-11-03 22:54:25 -04004821
4822 /*
4823 * See if there is some space in the delayed insertion reservation for
4824 * this reservation.
4825 */
4826 if (space_info != delayed_rsv->space_info)
4827 return -ENOSPC;
4828
4829 spin_lock(&delayed_rsv->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004830 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4831 bytes - delayed_rsv->size) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004832 spin_unlock(&delayed_rsv->lock);
4833 return -ENOSPC;
4834 }
4835 spin_unlock(&delayed_rsv->lock);
4836
4837commit:
Jeff Mahoney0c9ab342017-02-15 16:28:28 -05004838 trans = btrfs_join_transaction(fs_info->fs_root);
Josef Bacik663350a2011-11-03 22:54:25 -04004839 if (IS_ERR(trans))
4840 return -ENOSPC;
4841
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004842 return btrfs_commit_transaction(trans);
Josef Bacik663350a2011-11-03 22:54:25 -04004843}
4844
Josef Bacik957780e2016-05-17 13:30:55 -04004845struct reserve_ticket {
4846 u64 bytes;
4847 int error;
4848 struct list_head list;
4849 wait_queue_head_t wait;
Josef Bacik96c3f432012-06-21 14:05:49 -04004850};
4851
Jeff Mahoney0c9ab342017-02-15 16:28:28 -05004852static int flush_space(struct btrfs_fs_info *fs_info,
Josef Bacik96c3f432012-06-21 14:05:49 -04004853 struct btrfs_space_info *space_info, u64 num_bytes,
4854 u64 orig_bytes, int state)
4855{
Jeff Mahoney0c9ab342017-02-15 16:28:28 -05004856 struct btrfs_root *root = fs_info->fs_root;
Josef Bacik96c3f432012-06-21 14:05:49 -04004857 struct btrfs_trans_handle *trans;
4858 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004859 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004860
4861 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004862 case FLUSH_DELAYED_ITEMS_NR:
4863 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08004864 if (state == FLUSH_DELAYED_ITEMS_NR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004865 nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004866 else
Josef Bacik96c3f432012-06-21 14:05:49 -04004867 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004868
Josef Bacik96c3f432012-06-21 14:05:49 -04004869 trans = btrfs_join_transaction(root);
4870 if (IS_ERR(trans)) {
4871 ret = PTR_ERR(trans);
4872 break;
4873 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004874 ret = btrfs_run_delayed_items_nr(trans, fs_info, nr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004875 btrfs_end_transaction(trans);
Josef Bacik96c3f432012-06-21 14:05:49 -04004876 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004877 case FLUSH_DELALLOC:
4878 case FLUSH_DELALLOC_WAIT:
Miao Xie24af7dd2014-03-06 13:55:00 +08004879 shrink_delalloc(root, num_bytes * 2, orig_bytes,
Josef Bacik67b0fd62012-09-24 13:42:00 -04004880 state == FLUSH_DELALLOC_WAIT);
4881 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004882 case ALLOC_CHUNK:
4883 trans = btrfs_join_transaction(root);
4884 if (IS_ERR(trans)) {
4885 ret = PTR_ERR(trans);
4886 break;
4887 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004888 ret = do_chunk_alloc(trans, fs_info,
Josef Bacikea658ba2012-09-11 16:57:25 -04004889 btrfs_get_alloc_profile(root, 0),
4890 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004891 btrfs_end_transaction(trans);
Alex Lyakaseecba892015-12-06 12:32:31 +02004892 if (ret > 0 || ret == -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04004893 ret = 0;
4894 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004895 case COMMIT_TRANS:
Jeff Mahoney0c9ab342017-02-15 16:28:28 -05004896 ret = may_commit_transaction(fs_info, space_info,
4897 orig_bytes, 0);
Josef Bacik96c3f432012-06-21 14:05:49 -04004898 break;
4899 default:
4900 ret = -ENOSPC;
4901 break;
4902 }
4903
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004904 trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes,
Josef Bacikf376df22016-03-25 13:25:56 -04004905 orig_bytes, state, ret);
Josef Bacik96c3f432012-06-21 14:05:49 -04004906 return ret;
4907}
Miao Xie21c7e752014-05-13 17:29:04 -07004908
4909static inline u64
4910btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
4911 struct btrfs_space_info *space_info)
4912{
Josef Bacik957780e2016-05-17 13:30:55 -04004913 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07004914 u64 used;
4915 u64 expected;
Josef Bacik957780e2016-05-17 13:30:55 -04004916 u64 to_reclaim = 0;
Miao Xie21c7e752014-05-13 17:29:04 -07004917
Josef Bacik957780e2016-05-17 13:30:55 -04004918 list_for_each_entry(ticket, &space_info->tickets, list)
4919 to_reclaim += ticket->bytes;
4920 list_for_each_entry(ticket, &space_info->priority_tickets, list)
4921 to_reclaim += ticket->bytes;
4922 if (to_reclaim)
4923 return to_reclaim;
Miao Xie21c7e752014-05-13 17:29:04 -07004924
Wang Xiaoguange0af2482016-08-31 19:46:16 +08004925 to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M);
4926 if (can_overcommit(root, space_info, to_reclaim,
4927 BTRFS_RESERVE_FLUSH_ALL))
4928 return 0;
4929
Miao Xie21c7e752014-05-13 17:29:04 -07004930 used = space_info->bytes_used + space_info->bytes_reserved +
4931 space_info->bytes_pinned + space_info->bytes_readonly +
4932 space_info->bytes_may_use;
Byongho Leeee221842015-12-15 01:42:10 +09004933 if (can_overcommit(root, space_info, SZ_1M, BTRFS_RESERVE_FLUSH_ALL))
Miao Xie21c7e752014-05-13 17:29:04 -07004934 expected = div_factor_fine(space_info->total_bytes, 95);
4935 else
4936 expected = div_factor_fine(space_info->total_bytes, 90);
4937
4938 if (used > expected)
4939 to_reclaim = used - expected;
4940 else
4941 to_reclaim = 0;
4942 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
4943 space_info->bytes_reserved);
Miao Xie21c7e752014-05-13 17:29:04 -07004944 return to_reclaim;
4945}
4946
4947static inline int need_do_async_reclaim(struct btrfs_space_info *space_info,
Josef Bacik87241c22016-04-25 09:58:18 -04004948 struct btrfs_root *root, u64 used)
Miao Xie21c7e752014-05-13 17:29:04 -07004949{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004950 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik365c5312015-02-18 13:58:15 -08004951 u64 thresh = div_factor_fine(space_info->total_bytes, 98);
4952
4953 /* If we're just plain full then async reclaim just slows us down. */
Josef Bacikbaee8792016-01-26 09:35:38 -05004954 if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh)
Josef Bacik365c5312015-02-18 13:58:15 -08004955 return 0;
4956
Josef Bacik87241c22016-04-25 09:58:18 -04004957 if (!btrfs_calc_reclaim_metadata_size(root, space_info))
Liu Bo25ce4592014-09-10 12:58:50 +08004958 return 0;
Liu Bo25ce4592014-09-10 12:58:50 +08004959
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004960 return (used >= thresh && !btrfs_fs_closing(fs_info) &&
4961 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
Miao Xie21c7e752014-05-13 17:29:04 -07004962}
4963
Josef Bacik957780e2016-05-17 13:30:55 -04004964static void wake_all_tickets(struct list_head *head)
Miao Xie21c7e752014-05-13 17:29:04 -07004965{
Josef Bacik957780e2016-05-17 13:30:55 -04004966 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07004967
Josef Bacik957780e2016-05-17 13:30:55 -04004968 while (!list_empty(head)) {
4969 ticket = list_first_entry(head, struct reserve_ticket, list);
4970 list_del_init(&ticket->list);
4971 ticket->error = -ENOSPC;
4972 wake_up(&ticket->wait);
Miao Xie21c7e752014-05-13 17:29:04 -07004973 }
Miao Xie21c7e752014-05-13 17:29:04 -07004974}
4975
Josef Bacik957780e2016-05-17 13:30:55 -04004976/*
4977 * This is for normal flushers, we can wait all goddamned day if we want to. We
4978 * will loop and continuously try to flush as long as we are making progress.
4979 * We count progress as clearing off tickets each time we have to loop.
4980 */
Miao Xie21c7e752014-05-13 17:29:04 -07004981static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
4982{
4983 struct btrfs_fs_info *fs_info;
4984 struct btrfs_space_info *space_info;
4985 u64 to_reclaim;
4986 int flush_state;
Josef Bacik957780e2016-05-17 13:30:55 -04004987 int commit_cycles = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08004988 u64 last_tickets_id;
Miao Xie21c7e752014-05-13 17:29:04 -07004989
4990 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
4991 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4992
Josef Bacik957780e2016-05-17 13:30:55 -04004993 spin_lock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07004994 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
4995 space_info);
Josef Bacik957780e2016-05-17 13:30:55 -04004996 if (!to_reclaim) {
4997 space_info->flush = 0;
4998 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07004999 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005000 }
Wang Xiaoguangce129652016-09-02 10:58:46 +08005001 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005002 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005003
5004 flush_state = FLUSH_DELAYED_ITEMS_NR;
5005 do {
Josef Bacik957780e2016-05-17 13:30:55 -04005006 struct reserve_ticket *ticket;
5007 int ret;
5008
Jeff Mahoney0c9ab342017-02-15 16:28:28 -05005009 ret = flush_space(fs_info, space_info, to_reclaim, to_reclaim,
5010 flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005011 spin_lock(&space_info->lock);
5012 if (list_empty(&space_info->tickets)) {
5013 space_info->flush = 0;
5014 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005015 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005016 }
5017 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
5018 space_info);
5019 ticket = list_first_entry(&space_info->tickets,
5020 struct reserve_ticket, list);
Wang Xiaoguangce129652016-09-02 10:58:46 +08005021 if (last_tickets_id == space_info->tickets_id) {
Josef Bacik957780e2016-05-17 13:30:55 -04005022 flush_state++;
5023 } else {
Wang Xiaoguangce129652016-09-02 10:58:46 +08005024 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005025 flush_state = FLUSH_DELAYED_ITEMS_NR;
5026 if (commit_cycles)
5027 commit_cycles--;
5028 }
5029
5030 if (flush_state > COMMIT_TRANS) {
5031 commit_cycles++;
5032 if (commit_cycles > 2) {
5033 wake_all_tickets(&space_info->tickets);
5034 space_info->flush = 0;
5035 } else {
5036 flush_state = FLUSH_DELAYED_ITEMS_NR;
5037 }
5038 }
5039 spin_unlock(&space_info->lock);
5040 } while (flush_state <= COMMIT_TRANS);
Miao Xie21c7e752014-05-13 17:29:04 -07005041}
5042
5043void btrfs_init_async_reclaim_work(struct work_struct *work)
5044{
5045 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
5046}
5047
Josef Bacik957780e2016-05-17 13:30:55 -04005048static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
5049 struct btrfs_space_info *space_info,
5050 struct reserve_ticket *ticket)
5051{
5052 u64 to_reclaim;
5053 int flush_state = FLUSH_DELAYED_ITEMS_NR;
5054
5055 spin_lock(&space_info->lock);
5056 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
5057 space_info);
5058 if (!to_reclaim) {
5059 spin_unlock(&space_info->lock);
5060 return;
5061 }
5062 spin_unlock(&space_info->lock);
5063
5064 do {
Jeff Mahoney0c9ab342017-02-15 16:28:28 -05005065 flush_space(fs_info, space_info, to_reclaim, to_reclaim,
5066 flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04005067 flush_state++;
5068 spin_lock(&space_info->lock);
5069 if (ticket->bytes == 0) {
5070 spin_unlock(&space_info->lock);
5071 return;
5072 }
5073 spin_unlock(&space_info->lock);
5074
5075 /*
5076 * Priority flushers can't wait on delalloc without
5077 * deadlocking.
5078 */
5079 if (flush_state == FLUSH_DELALLOC ||
5080 flush_state == FLUSH_DELALLOC_WAIT)
5081 flush_state = ALLOC_CHUNK;
5082 } while (flush_state < COMMIT_TRANS);
5083}
5084
5085static int wait_reserve_ticket(struct btrfs_fs_info *fs_info,
5086 struct btrfs_space_info *space_info,
5087 struct reserve_ticket *ticket, u64 orig_bytes)
5088
5089{
5090 DEFINE_WAIT(wait);
5091 int ret = 0;
5092
5093 spin_lock(&space_info->lock);
5094 while (ticket->bytes > 0 && ticket->error == 0) {
5095 ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE);
5096 if (ret) {
5097 ret = -EINTR;
5098 break;
5099 }
5100 spin_unlock(&space_info->lock);
5101
5102 schedule();
5103
5104 finish_wait(&ticket->wait, &wait);
5105 spin_lock(&space_info->lock);
5106 }
5107 if (!ret)
5108 ret = ticket->error;
5109 if (!list_empty(&ticket->list))
5110 list_del_init(&ticket->list);
5111 if (ticket->bytes && ticket->bytes < orig_bytes) {
5112 u64 num_bytes = orig_bytes - ticket->bytes;
5113 space_info->bytes_may_use -= num_bytes;
5114 trace_btrfs_space_reservation(fs_info, "space_info",
5115 space_info->flags, num_bytes, 0);
5116 }
5117 spin_unlock(&space_info->lock);
5118
5119 return ret;
5120}
5121
Josef Bacik663350a2011-11-03 22:54:25 -04005122/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005123 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5124 * @root - the root we're allocating for
Josef Bacik957780e2016-05-17 13:30:55 -04005125 * @space_info - the space info we want to allocate from
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005126 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04005127 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005128 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04005129 * This will reserve orig_bytes number of bytes from the space info associated
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005130 * with the block_rsv. If there is not enough space it will make an attempt to
5131 * flush out space to make room. It will do this by flushing delalloc if
5132 * possible or committing the transaction. If flush is 0 then no attempts to
5133 * regain reservations will be made and this will fail if there is not enough
5134 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005135 */
Josef Bacik957780e2016-05-17 13:30:55 -04005136static int __reserve_metadata_bytes(struct btrfs_root *root,
5137 struct btrfs_space_info *space_info,
5138 u64 orig_bytes,
5139 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005140{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005141 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik957780e2016-05-17 13:30:55 -04005142 struct reserve_ticket ticket;
Josef Bacik2bf64752011-09-26 17:12:22 -04005143 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005144 int ret = 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005145
Josef Bacik957780e2016-05-17 13:30:55 -04005146 ASSERT(orig_bytes);
Josef Bacik8ca17f02016-05-27 13:24:13 -04005147 ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL);
5148
Yan, Zhengf0486c62010-05-16 10:46:25 -04005149 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04005150 ret = -ENOSPC;
Liu Bo41361352017-02-13 15:42:21 -08005151 used = btrfs_space_info_used(space_info, true);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005152
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005153 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005154 * If we have enough space then hooray, make our reservation and carry
5155 * on. If not see if we can overcommit, and if we can, hooray carry on.
5156 * If not things get more complicated.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005157 */
Josef Bacik957780e2016-05-17 13:30:55 -04005158 if (used + orig_bytes <= space_info->total_bytes) {
5159 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005160 trace_btrfs_space_reservation(fs_info, "space_info",
5161 space_info->flags, orig_bytes, 1);
Josef Bacik957780e2016-05-17 13:30:55 -04005162 ret = 0;
5163 } else if (can_overcommit(root, space_info, orig_bytes, flush)) {
Josef Bacik44734ed2012-09-28 16:04:19 -04005164 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005165 trace_btrfs_space_reservation(fs_info, "space_info",
5166 space_info->flags, orig_bytes, 1);
Josef Bacik44734ed2012-09-28 16:04:19 -04005167 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04005168 }
5169
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005170 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005171 * If we couldn't make a reservation then setup our reservation ticket
5172 * and kick the async worker if it's not already running.
Miao Xie08e007d2012-10-16 11:33:38 +00005173 *
Josef Bacik957780e2016-05-17 13:30:55 -04005174 * If we are a priority flusher then we just need to add our ticket to
5175 * the list and we will do our own flushing further down.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005176 */
Josef Bacik72bcd992012-12-18 15:16:34 -05005177 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacik957780e2016-05-17 13:30:55 -04005178 ticket.bytes = orig_bytes;
5179 ticket.error = 0;
5180 init_waitqueue_head(&ticket.wait);
5181 if (flush == BTRFS_RESERVE_FLUSH_ALL) {
5182 list_add_tail(&ticket.list, &space_info->tickets);
5183 if (!space_info->flush) {
5184 space_info->flush = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005185 trace_btrfs_trigger_flush(fs_info,
Josef Bacikf376df22016-03-25 13:25:56 -04005186 space_info->flags,
5187 orig_bytes, flush,
5188 "enospc");
Josef Bacik957780e2016-05-17 13:30:55 -04005189 queue_work(system_unbound_wq,
5190 &root->fs_info->async_reclaim_work);
5191 }
5192 } else {
5193 list_add_tail(&ticket.list,
5194 &space_info->priority_tickets);
5195 }
Miao Xie21c7e752014-05-13 17:29:04 -07005196 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
5197 used += orig_bytes;
Josef Bacikf6acfd52014-09-18 11:27:17 -04005198 /*
5199 * We will do the space reservation dance during log replay,
5200 * which means we won't have fs_info->fs_root set, so don't do
5201 * the async reclaim as we will panic.
5202 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005203 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) &&
Josef Bacik87241c22016-04-25 09:58:18 -04005204 need_do_async_reclaim(space_info, root, used) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005205 !work_busy(&fs_info->async_reclaim_work)) {
5206 trace_btrfs_trigger_flush(fs_info, space_info->flags,
5207 orig_bytes, flush, "preempt");
Miao Xie21c7e752014-05-13 17:29:04 -07005208 queue_work(system_unbound_wq,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005209 &fs_info->async_reclaim_work);
Josef Bacikf376df22016-03-25 13:25:56 -04005210 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005211 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005212 spin_unlock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00005213 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik957780e2016-05-17 13:30:55 -04005214 return ret;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005215
Josef Bacik957780e2016-05-17 13:30:55 -04005216 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005217 return wait_reserve_ticket(fs_info, space_info, &ticket,
Josef Bacik957780e2016-05-17 13:30:55 -04005218 orig_bytes);
Miao Xie08e007d2012-10-16 11:33:38 +00005219
Josef Bacik957780e2016-05-17 13:30:55 -04005220 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005221 priority_reclaim_metadata_space(fs_info, space_info, &ticket);
Josef Bacik957780e2016-05-17 13:30:55 -04005222 spin_lock(&space_info->lock);
5223 if (ticket.bytes) {
5224 if (ticket.bytes < orig_bytes) {
5225 u64 num_bytes = orig_bytes - ticket.bytes;
5226 space_info->bytes_may_use -= num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005227 trace_btrfs_space_reservation(fs_info, "space_info",
5228 space_info->flags,
5229 num_bytes, 0);
Miao Xie08e007d2012-10-16 11:33:38 +00005230
Josef Bacik957780e2016-05-17 13:30:55 -04005231 }
5232 list_del_init(&ticket.list);
5233 ret = -ENOSPC;
5234 }
5235 spin_unlock(&space_info->lock);
5236 ASSERT(list_empty(&ticket.list));
5237 return ret;
5238}
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005239
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005240/**
5241 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5242 * @root - the root we're allocating for
5243 * @block_rsv - the block_rsv we're allocating for
5244 * @orig_bytes - the number of bytes we want
5245 * @flush - whether or not we can flush to make our reservation
5246 *
Josef Bacik957780e2016-05-17 13:30:55 -04005247 * This will reserve orgi_bytes number of bytes from the space info associated
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005248 * with the block_rsv. If there is not enough space it will make an attempt to
5249 * flush out space to make room. It will do this by flushing delalloc if
5250 * possible or committing the transaction. If flush is 0 then no attempts to
5251 * regain reservations will be made and this will fail if there is not enough
5252 * space already.
5253 */
5254static int reserve_metadata_bytes(struct btrfs_root *root,
5255 struct btrfs_block_rsv *block_rsv,
5256 u64 orig_bytes,
5257 enum btrfs_reserve_flush_enum flush)
5258{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005259 struct btrfs_fs_info *fs_info = root->fs_info;
5260 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04005261 int ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005262
Josef Bacik957780e2016-05-17 13:30:55 -04005263 ret = __reserve_metadata_bytes(root, block_rsv->space_info, orig_bytes,
5264 flush);
Josef Bacik5d803662013-02-07 16:06:02 -05005265 if (ret == -ENOSPC &&
5266 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
Josef Bacik5d803662013-02-07 16:06:02 -05005267 if (block_rsv != global_rsv &&
5268 !block_rsv_use_bytes(global_rsv, orig_bytes))
5269 ret = 0;
5270 }
Jeff Mahoneycab45e22013-10-16 16:27:01 -04005271 if (ret == -ENOSPC)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005272 trace_btrfs_space_reservation(fs_info, "space_info:enospc",
Josef Bacik957780e2016-05-17 13:30:55 -04005273 block_rsv->space_info->flags,
5274 orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005275 return ret;
5276}
5277
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005278static struct btrfs_block_rsv *get_block_rsv(
5279 const struct btrfs_trans_handle *trans,
5280 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005281{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005282 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik4c13d752011-08-30 11:31:29 -04005283 struct btrfs_block_rsv *block_rsv = NULL;
5284
Alexandru Moisee9cf4392015-09-09 00:18:50 +00005285 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005286 (root == fs_info->csum_root && trans->adding_csums) ||
5287 (root == fs_info->uuid_root))
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02005288 block_rsv = trans->block_rsv;
5289
Josef Bacik4c13d752011-08-30 11:31:29 -04005290 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005291 block_rsv = root->block_rsv;
5292
5293 if (!block_rsv)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005294 block_rsv = &fs_info->empty_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005295
5296 return block_rsv;
5297}
5298
5299static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
5300 u64 num_bytes)
5301{
5302 int ret = -ENOSPC;
5303 spin_lock(&block_rsv->lock);
5304 if (block_rsv->reserved >= num_bytes) {
5305 block_rsv->reserved -= num_bytes;
5306 if (block_rsv->reserved < block_rsv->size)
5307 block_rsv->full = 0;
5308 ret = 0;
5309 }
5310 spin_unlock(&block_rsv->lock);
5311 return ret;
5312}
5313
5314static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
5315 u64 num_bytes, int update_size)
5316{
5317 spin_lock(&block_rsv->lock);
5318 block_rsv->reserved += num_bytes;
5319 if (update_size)
5320 block_rsv->size += num_bytes;
5321 else if (block_rsv->reserved >= block_rsv->size)
5322 block_rsv->full = 1;
5323 spin_unlock(&block_rsv->lock);
5324}
5325
Josef Bacikd52be812013-05-29 14:54:47 -04005326int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
5327 struct btrfs_block_rsv *dest, u64 num_bytes,
5328 int min_factor)
5329{
5330 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5331 u64 min_bytes;
5332
5333 if (global_rsv->space_info != dest->space_info)
5334 return -ENOSPC;
5335
5336 spin_lock(&global_rsv->lock);
5337 min_bytes = div_factor(global_rsv->size, min_factor);
5338 if (global_rsv->reserved < min_bytes + num_bytes) {
5339 spin_unlock(&global_rsv->lock);
5340 return -ENOSPC;
5341 }
5342 global_rsv->reserved -= num_bytes;
5343 if (global_rsv->reserved < global_rsv->size)
5344 global_rsv->full = 0;
5345 spin_unlock(&global_rsv->lock);
5346
5347 block_rsv_add_bytes(dest, num_bytes, 1);
5348 return 0;
5349}
5350
Josef Bacik957780e2016-05-17 13:30:55 -04005351/*
5352 * This is for space we already have accounted in space_info->bytes_may_use, so
5353 * basically when we're returning space from block_rsv's.
5354 */
5355static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
5356 struct btrfs_space_info *space_info,
5357 u64 num_bytes)
5358{
5359 struct reserve_ticket *ticket;
5360 struct list_head *head;
5361 u64 used;
5362 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH;
5363 bool check_overcommit = false;
5364
5365 spin_lock(&space_info->lock);
5366 head = &space_info->priority_tickets;
5367
5368 /*
5369 * If we are over our limit then we need to check and see if we can
5370 * overcommit, and if we can't then we just need to free up our space
5371 * and not satisfy any requests.
5372 */
5373 used = space_info->bytes_used + space_info->bytes_reserved +
5374 space_info->bytes_pinned + space_info->bytes_readonly +
5375 space_info->bytes_may_use;
5376 if (used - num_bytes >= space_info->total_bytes)
5377 check_overcommit = true;
5378again:
5379 while (!list_empty(head) && num_bytes) {
5380 ticket = list_first_entry(head, struct reserve_ticket,
5381 list);
5382 /*
5383 * We use 0 bytes because this space is already reserved, so
5384 * adding the ticket space would be a double count.
5385 */
5386 if (check_overcommit &&
5387 !can_overcommit(fs_info->extent_root, space_info, 0,
5388 flush))
5389 break;
5390 if (num_bytes >= ticket->bytes) {
5391 list_del_init(&ticket->list);
5392 num_bytes -= ticket->bytes;
5393 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005394 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005395 wake_up(&ticket->wait);
5396 } else {
5397 ticket->bytes -= num_bytes;
5398 num_bytes = 0;
5399 }
5400 }
5401
5402 if (num_bytes && head == &space_info->priority_tickets) {
5403 head = &space_info->tickets;
5404 flush = BTRFS_RESERVE_FLUSH_ALL;
5405 goto again;
5406 }
5407 space_info->bytes_may_use -= num_bytes;
5408 trace_btrfs_space_reservation(fs_info, "space_info",
5409 space_info->flags, num_bytes, 0);
5410 spin_unlock(&space_info->lock);
5411}
5412
5413/*
5414 * This is for newly allocated space that isn't accounted in
5415 * space_info->bytes_may_use yet. So if we allocate a chunk or unpin an extent
5416 * we use this helper.
5417 */
5418static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
5419 struct btrfs_space_info *space_info,
5420 u64 num_bytes)
5421{
5422 struct reserve_ticket *ticket;
5423 struct list_head *head = &space_info->priority_tickets;
5424
5425again:
5426 while (!list_empty(head) && num_bytes) {
5427 ticket = list_first_entry(head, struct reserve_ticket,
5428 list);
5429 if (num_bytes >= ticket->bytes) {
5430 trace_btrfs_space_reservation(fs_info, "space_info",
5431 space_info->flags,
5432 ticket->bytes, 1);
5433 list_del_init(&ticket->list);
5434 num_bytes -= ticket->bytes;
5435 space_info->bytes_may_use += ticket->bytes;
5436 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005437 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005438 wake_up(&ticket->wait);
5439 } else {
5440 trace_btrfs_space_reservation(fs_info, "space_info",
5441 space_info->flags,
5442 num_bytes, 1);
5443 space_info->bytes_may_use += num_bytes;
5444 ticket->bytes -= num_bytes;
5445 num_bytes = 0;
5446 }
5447 }
5448
5449 if (num_bytes && head == &space_info->priority_tickets) {
5450 head = &space_info->tickets;
5451 goto again;
5452 }
5453}
5454
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005455static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
5456 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02005457 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005458{
5459 struct btrfs_space_info *space_info = block_rsv->space_info;
5460
5461 spin_lock(&block_rsv->lock);
5462 if (num_bytes == (u64)-1)
5463 num_bytes = block_rsv->size;
5464 block_rsv->size -= num_bytes;
5465 if (block_rsv->reserved >= block_rsv->size) {
5466 num_bytes = block_rsv->reserved - block_rsv->size;
5467 block_rsv->reserved = block_rsv->size;
5468 block_rsv->full = 1;
5469 } else {
5470 num_bytes = 0;
5471 }
5472 spin_unlock(&block_rsv->lock);
5473
5474 if (num_bytes > 0) {
5475 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00005476 spin_lock(&dest->lock);
5477 if (!dest->full) {
5478 u64 bytes_to_add;
5479
5480 bytes_to_add = dest->size - dest->reserved;
5481 bytes_to_add = min(num_bytes, bytes_to_add);
5482 dest->reserved += bytes_to_add;
5483 if (dest->reserved >= dest->size)
5484 dest->full = 1;
5485 num_bytes -= bytes_to_add;
5486 }
5487 spin_unlock(&dest->lock);
5488 }
Josef Bacik957780e2016-05-17 13:30:55 -04005489 if (num_bytes)
5490 space_info_add_old_bytes(fs_info, space_info,
5491 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005492 }
5493}
5494
Josef Bacik25d609f2016-03-25 13:25:48 -04005495int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
5496 struct btrfs_block_rsv *dst, u64 num_bytes,
5497 int update_size)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005498{
5499 int ret;
5500
5501 ret = block_rsv_use_bytes(src, num_bytes);
5502 if (ret)
5503 return ret;
5504
Josef Bacik25d609f2016-03-25 13:25:48 -04005505 block_rsv_add_bytes(dst, num_bytes, update_size);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005506 return 0;
5507}
5508
Miao Xie66d8f3d2012-09-06 04:02:28 -06005509void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005510{
5511 memset(rsv, 0, sizeof(*rsv));
5512 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06005513 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005514}
5515
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005516struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
Miao Xie66d8f3d2012-09-06 04:02:28 -06005517 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005518{
5519 struct btrfs_block_rsv *block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005520
5521 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
5522 if (!block_rsv)
5523 return NULL;
5524
Miao Xie66d8f3d2012-09-06 04:02:28 -06005525 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005526 block_rsv->space_info = __find_space_info(fs_info,
5527 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005528 return block_rsv;
5529}
5530
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005531void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005532 struct btrfs_block_rsv *rsv)
5533{
Josef Bacik2aaa6652012-08-29 14:27:18 -04005534 if (!rsv)
5535 return;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005536 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Josef Bacikdabdb642011-08-08 12:50:18 -04005537 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005538}
5539
Chris Masoncdfb0802015-04-06 18:17:00 -07005540void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
5541{
5542 kfree(rsv);
5543}
5544
Miao Xie08e007d2012-10-16 11:33:38 +00005545int btrfs_block_rsv_add(struct btrfs_root *root,
5546 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
5547 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005548{
5549 int ret;
5550
5551 if (num_bytes == 0)
5552 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005553
Miao Xie61b520a2011-11-10 20:45:05 -05005554 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005555 if (!ret) {
5556 block_rsv_add_bytes(block_rsv, num_bytes, 1);
5557 return 0;
5558 }
5559
Yan, Zhengf0486c62010-05-16 10:46:25 -04005560 return ret;
5561}
5562
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005563int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005564{
5565 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005566 int ret = -ENOSPC;
5567
5568 if (!block_rsv)
5569 return 0;
5570
5571 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04005572 num_bytes = div_factor(block_rsv->size, min_factor);
5573 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005574 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005575 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005576
Josef Bacik36ba0222011-10-18 12:15:48 -04005577 return ret;
5578}
5579
Miao Xie08e007d2012-10-16 11:33:38 +00005580int btrfs_block_rsv_refill(struct btrfs_root *root,
5581 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
5582 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04005583{
5584 u64 num_bytes = 0;
5585 int ret = -ENOSPC;
5586
5587 if (!block_rsv)
5588 return 0;
5589
5590 spin_lock(&block_rsv->lock);
5591 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04005592 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005593 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04005594 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04005595 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005596 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04005597
Yan, Zhengf0486c62010-05-16 10:46:25 -04005598 if (!ret)
5599 return 0;
5600
Miao Xieaa38a712011-11-18 17:43:00 +08005601 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04005602 if (!ret) {
5603 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005604 return 0;
5605 }
5606
Josef Bacik13553e52011-08-08 13:33:21 -04005607 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005608}
5609
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005610void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005611 struct btrfs_block_rsv *block_rsv,
5612 u64 num_bytes)
5613{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005614 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5615
Liu Bo17504582013-12-29 21:44:50 +08005616 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04005617 block_rsv->space_info != global_rsv->space_info)
5618 global_rsv = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005619 block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005620}
5621
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005622static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
5623{
5624 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
5625 struct btrfs_space_info *sinfo = block_rsv->space_info;
5626 u64 num_bytes;
5627
Josef Bacikae2e4722016-05-27 12:58:35 -04005628 /*
5629 * The global block rsv is based on the size of the extent tree, the
5630 * checksum tree and the root tree. If the fs is empty we want to set
5631 * it to a minimal amount for safety.
5632 */
5633 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) +
5634 btrfs_root_used(&fs_info->csum_root->root_item) +
5635 btrfs_root_used(&fs_info->tree_root->root_item);
5636 num_bytes = max_t(u64, num_bytes, SZ_16M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005637
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005638 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005639 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005640
Byongho Leeee221842015-12-15 01:42:10 +09005641 block_rsv->size = min_t(u64, num_bytes, SZ_512M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005642
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005643 if (block_rsv->reserved < block_rsv->size) {
Liu Bo41361352017-02-13 15:42:21 -08005644 num_bytes = btrfs_space_info_used(sinfo, true);
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005645 if (sinfo->total_bytes > num_bytes) {
5646 num_bytes = sinfo->total_bytes - num_bytes;
5647 num_bytes = min(num_bytes,
5648 block_rsv->size - block_rsv->reserved);
5649 block_rsv->reserved += num_bytes;
5650 sinfo->bytes_may_use += num_bytes;
5651 trace_btrfs_space_reservation(fs_info, "space_info",
5652 sinfo->flags, num_bytes,
5653 1);
5654 }
5655 } else if (block_rsv->reserved > block_rsv->size) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005656 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04005657 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005658 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005659 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005660 block_rsv->reserved = block_rsv->size;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005661 }
David Sterba182608c2011-05-05 13:13:16 +02005662
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005663 if (block_rsv->reserved == block_rsv->size)
5664 block_rsv->full = 1;
5665 else
5666 block_rsv->full = 0;
5667
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005668 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005669 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005670}
5671
Yan, Zhengf0486c62010-05-16 10:46:25 -04005672static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
5673{
5674 struct btrfs_space_info *space_info;
5675
5676 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
5677 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005678
5679 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005680 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005681 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005682 fs_info->trans_block_rsv.space_info = space_info;
5683 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04005684 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005685
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005686 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
5687 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
5688 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
5689 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00005690 if (fs_info->quota_root)
5691 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005692 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005693
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005694 update_global_block_rsv(fs_info);
5695}
5696
5697static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
5698{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005699 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
5700 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005701 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
5702 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
5703 WARN_ON(fs_info->trans_block_rsv.size > 0);
5704 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
5705 WARN_ON(fs_info->chunk_block_rsv.size > 0);
5706 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04005707 WARN_ON(fs_info->delayed_block_rsv.size > 0);
5708 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04005709}
5710
Yan, Zhenga22285a2010-05-16 10:48:46 -04005711void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005712 struct btrfs_fs_info *fs_info)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005713{
Josef Bacik0e721102012-06-26 16:13:18 -04005714 if (!trans->block_rsv)
5715 return;
5716
Yan, Zhenga22285a2010-05-16 10:48:46 -04005717 if (!trans->bytes_reserved)
5718 return;
5719
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005720 trace_btrfs_space_reservation(fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04005721 trans->transid, trans->bytes_reserved, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005722 btrfs_block_rsv_release(fs_info, trans->block_rsv,
5723 trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005724 trans->bytes_reserved = 0;
5725}
5726
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005727/*
5728 * To be called after all the new block groups attached to the transaction
5729 * handle have been created (btrfs_create_pending_block_groups()).
5730 */
5731void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
5732{
Jeff Mahoney64b63582016-06-20 17:23:41 -04005733 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005734
5735 if (!trans->chunk_bytes_reserved)
5736 return;
5737
5738 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
5739
5740 block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL,
5741 trans->chunk_bytes_reserved);
5742 trans->chunk_bytes_reserved = 0;
5743}
5744
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005745/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04005746int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005747 struct btrfs_inode *inode)
Yan, Zhengd68fc572010-05-16 10:49:58 -04005748{
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005749 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
5750 struct btrfs_root *root = inode->root;
Josef Bacik40acc3e2016-05-27 13:01:08 -04005751 /*
5752 * We always use trans->block_rsv here as we will have reserved space
5753 * for our orphan when starting the transaction, using get_block_rsv()
5754 * here will sometimes make us choose the wrong block rsv as we could be
5755 * doing a reloc inode for a non refcounted root.
5756 */
5757 struct btrfs_block_rsv *src_rsv = trans->block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005758 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
5759
5760 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04005761 * We need to hold space in order to delete our orphan item once we've
5762 * added it, so this takes the reservation so we can release it later
5763 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04005764 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005765 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5766
Nikolay Borisov8ed7a2a2017-02-20 13:50:39 +02005767 trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode),
5768 num_bytes, 1);
Josef Bacik25d609f2016-03-25 13:25:48 -04005769 return btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005770}
5771
Nikolay Borisov703b3912017-02-20 13:50:40 +02005772void btrfs_orphan_release_metadata(struct btrfs_inode *inode)
Yan, Zhengd68fc572010-05-16 10:49:58 -04005773{
Nikolay Borisov703b3912017-02-20 13:50:40 +02005774 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
5775 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005776 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5777
Nikolay Borisov703b3912017-02-20 13:50:40 +02005778 trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode),
5779 num_bytes, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005780 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, num_bytes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005781}
5782
Miao Xied5c12072013-02-28 10:04:33 +00005783/*
5784 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
5785 * root: the root of the parent directory
5786 * rsv: block reservation
5787 * items: the number of items that we need do reservation
5788 * qgroup_reserved: used to return the reserved size in qgroup
5789 *
5790 * This function is used to reserve the space for snapshot/subvolume
5791 * creation and deletion. Those operations are different with the
5792 * common file/directory operations, they change two fs/file trees
5793 * and root tree, the number of items that the qgroup reserves is
5794 * different with the free space reservation. So we can not use
Nicholas D Steeves01327612016-05-19 21:18:45 -04005795 * the space reservation mechanism in start_transaction().
Miao Xied5c12072013-02-28 10:04:33 +00005796 */
5797int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
5798 struct btrfs_block_rsv *rsv,
5799 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005800 u64 *qgroup_reserved,
5801 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005802{
Miao Xied5c12072013-02-28 10:04:33 +00005803 u64 num_bytes;
5804 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005805 struct btrfs_fs_info *fs_info = root->fs_info;
5806 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00005807
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005808 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Miao Xied5c12072013-02-28 10:04:33 +00005809 /* One for parent inode, two for dir entries */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005810 num_bytes = 3 * fs_info->nodesize;
Jeff Mahoney003d7c52017-01-25 09:50:33 -05005811 ret = btrfs_qgroup_reserve_meta(root, num_bytes, true);
Miao Xied5c12072013-02-28 10:04:33 +00005812 if (ret)
5813 return ret;
5814 } else {
5815 num_bytes = 0;
5816 }
5817
5818 *qgroup_reserved = num_bytes;
5819
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005820 num_bytes = btrfs_calc_trans_metadata_size(fs_info, items);
5821 rsv->space_info = __find_space_info(fs_info,
Miao Xied5c12072013-02-28 10:04:33 +00005822 BTRFS_BLOCK_GROUP_METADATA);
5823 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
5824 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005825
5826 if (ret == -ENOSPC && use_global_rsv)
Josef Bacik25d609f2016-03-25 13:25:48 -04005827 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005828
Qu Wenruo71741092015-09-08 17:22:41 +08005829 if (ret && *qgroup_reserved)
5830 btrfs_qgroup_free_meta(root, *qgroup_reserved);
Miao Xied5c12072013-02-28 10:04:33 +00005831
5832 return ret;
5833}
5834
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005835void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
David Sterba7775c812017-02-10 19:18:18 +01005836 struct btrfs_block_rsv *rsv)
Miao Xied5c12072013-02-28 10:04:33 +00005837{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005838 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005839}
5840
Josef Bacik7709cde2011-08-04 10:25:02 -04005841/**
5842 * drop_outstanding_extent - drop an outstanding extent
5843 * @inode: the inode we're dropping the extent for
Nicholas D Steeves01327612016-05-19 21:18:45 -04005844 * @num_bytes: the number of bytes we're releasing.
Josef Bacik7709cde2011-08-04 10:25:02 -04005845 *
5846 * This is called when we are freeing up an outstanding extent, either called
5847 * after an error or after an extent is written. This will return the number of
5848 * reserved extents that need to be freed. This must be called with
5849 * BTRFS_I(inode)->lock held.
5850 */
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02005851static unsigned drop_outstanding_extent(struct btrfs_inode *inode,
5852 u64 num_bytes)
Josef Bacik9e0baf62011-07-15 15:16:44 +00005853{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005854 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005855 unsigned dropped_extents = 0;
David Sterba823bb202017-01-04 11:09:51 +01005856 unsigned num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005857
David Sterba823bb202017-01-04 11:09:51 +01005858 num_extents = count_max_extents(num_bytes);
Josef Bacikdcab6a32015-02-11 15:08:59 -05005859 ASSERT(num_extents);
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02005860 ASSERT(inode->outstanding_extents >= num_extents);
5861 inode->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005862
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02005863 if (inode->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04005864 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02005865 &inode->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005866 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005867
Josef Bacik9e0baf62011-07-15 15:16:44 +00005868 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005869 * If we have more or the same amount of outstanding extents than we have
Josef Bacik9e0baf62011-07-15 15:16:44 +00005870 * reserved then we need to leave the reserved extents count alone.
5871 */
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02005872 if (inode->outstanding_extents >= inode->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005873 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005874
Nikolay Borisovbaa3ba32017-02-20 13:50:37 +02005875 dropped_extents = inode->reserved_extents - inode->outstanding_extents;
5876 inode->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005877 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005878}
5879
Josef Bacik7709cde2011-08-04 10:25:02 -04005880/**
Nicholas D Steeves01327612016-05-19 21:18:45 -04005881 * calc_csum_metadata_size - return the amount of metadata space that must be
5882 * reserved/freed for the given bytes.
Josef Bacik7709cde2011-08-04 10:25:02 -04005883 * @inode: the inode we're manipulating
5884 * @num_bytes: the number of bytes in question
5885 * @reserve: 1 if we are reserving space, 0 if we are freeing space
5886 *
5887 * This adjusts the number of csum_bytes in the inode and then returns the
5888 * correct amount of metadata that must either be reserved or freed. We
5889 * calculate how many checksums we can fit into one leaf and then divide the
5890 * number of bytes that will need to be checksumed by this value to figure out
5891 * how many checksums will be required. If we are adding bytes then the number
5892 * may go up and we will return the number of additional bytes that must be
5893 * reserved. If it is going down we will return the number of bytes that must
5894 * be freed.
5895 *
5896 * This must be called with BTRFS_I(inode)->lock held.
5897 */
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02005898static u64 calc_csum_metadata_size(struct btrfs_inode *inode, u64 num_bytes,
Josef Bacik7709cde2011-08-04 10:25:02 -04005899 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005900{
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02005901 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Josef Bacik12621332015-02-03 07:50:16 -08005902 u64 old_csums, num_csums;
Josef Bacik7709cde2011-08-04 10:25:02 -04005903
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02005904 if (inode->flags & BTRFS_INODE_NODATASUM && inode->csum_bytes == 0)
Josef Bacik7709cde2011-08-04 10:25:02 -04005905 return 0;
5906
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02005907 old_csums = btrfs_csum_bytes_to_leaves(fs_info, inode->csum_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04005908 if (reserve)
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02005909 inode->csum_bytes += num_bytes;
Josef Bacik7709cde2011-08-04 10:25:02 -04005910 else
Nikolay Borisov0e6bf9b2017-02-20 13:50:38 +02005911 inode->csum_bytes -= num_bytes;
5912 num_csums = btrfs_csum_bytes_to_leaves(fs_info, inode->csum_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04005913
5914 /* No change, no need to reserve more */
5915 if (old_csums == num_csums)
5916 return 0;
5917
5918 if (reserve)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005919 return btrfs_calc_trans_metadata_size(fs_info,
Josef Bacik7709cde2011-08-04 10:25:02 -04005920 num_csums - old_csums);
5921
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005922 return btrfs_calc_trans_metadata_size(fs_info, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005923}
5924
Nikolay Borisov9f3db422017-02-20 13:50:41 +02005925int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005926{
Nikolay Borisov9f3db422017-02-20 13:50:41 +02005927 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
5928 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005929 struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005930 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05005931 u64 csum_bytes;
David Sterba823bb202017-01-04 11:09:51 +01005932 unsigned nr_extents;
Miao Xie08e007d2012-10-16 11:33:38 +00005933 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07005934 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005935 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00005936 u64 to_free = 0;
5937 unsigned dropped;
Josef Bacik48c3d482016-03-25 13:25:49 -04005938 bool release_extra = false;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005939
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005940 /* If we are a free space inode we need to not flush since we will be in
5941 * the middle of a transaction commit. We also don't need the delalloc
5942 * mutex since we won't race with anybody. We need this mostly to make
5943 * lockdep shut its filthy mouth.
Josef Bacikbac357dc2016-07-20 16:48:45 -07005944 *
5945 * If we have a transaction open (can happen if we call truncate_block
5946 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005947 */
5948 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00005949 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005950 delalloc_lock = false;
Josef Bacikbac357dc2016-07-20 16:48:45 -07005951 } else if (current->journal_info) {
5952 flush = BTRFS_RESERVE_FLUSH_LIMIT;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005953 }
Josef Bacikc09544e2011-08-30 10:19:10 -04005954
Miao Xie08e007d2012-10-16 11:33:38 +00005955 if (flush != BTRFS_RESERVE_NO_FLUSH &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005956 btrfs_transaction_in_commit(fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005957 schedule_timeout(1);
5958
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005959 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02005960 mutex_lock(&inode->delalloc_mutex);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005961
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005962 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005963
Nikolay Borisov9f3db422017-02-20 13:50:41 +02005964 spin_lock(&inode->lock);
David Sterba823bb202017-01-04 11:09:51 +01005965 nr_extents = count_max_extents(num_bytes);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02005966 inode->outstanding_extents += nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05005967
Josef Bacik48c3d482016-03-25 13:25:49 -04005968 nr_extents = 0;
Nikolay Borisov9f3db422017-02-20 13:50:41 +02005969 if (inode->outstanding_extents > inode->reserved_extents)
5970 nr_extents += inode->outstanding_extents -
5971 inode->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005972
Josef Bacik48c3d482016-03-25 13:25:49 -04005973 /* We always want to reserve a slot for updating the inode. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005974 to_reserve = btrfs_calc_trans_metadata_size(fs_info, nr_extents + 1);
Josef Bacik7709cde2011-08-04 10:25:02 -04005975 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Nikolay Borisov9f3db422017-02-20 13:50:41 +02005976 csum_bytes = inode->csum_bytes;
5977 spin_unlock(&inode->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05005978
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005979 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Qu Wenruo71741092015-09-08 17:22:41 +08005980 ret = btrfs_qgroup_reserve_meta(root,
Jeff Mahoney003d7c52017-01-25 09:50:33 -05005981 nr_extents * fs_info->nodesize, true);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005982 if (ret)
5983 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00005984 }
Arne Jansenc5567232011-09-14 15:44:05 +02005985
Josef Bacik48c3d482016-03-25 13:25:49 -04005986 ret = btrfs_block_rsv_add(root, block_rsv, to_reserve, flush);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005987 if (unlikely(ret)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04005988 btrfs_qgroup_free_meta(root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005989 nr_extents * fs_info->nodesize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005990 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005991 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005992
Nikolay Borisov9f3db422017-02-20 13:50:41 +02005993 spin_lock(&inode->lock);
Josef Bacik48c3d482016-03-25 13:25:49 -04005994 if (test_and_set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
Nikolay Borisov9f3db422017-02-20 13:50:41 +02005995 &inode->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005996 to_reserve -= btrfs_calc_trans_metadata_size(fs_info, 1);
Josef Bacik48c3d482016-03-25 13:25:49 -04005997 release_extra = true;
Josef Bacik660d3f62011-12-09 11:18:51 -05005998 }
Nikolay Borisov9f3db422017-02-20 13:50:41 +02005999 inode->reserved_extents += nr_extents;
6000 spin_unlock(&inode->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006001
6002 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006003 mutex_unlock(&inode->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05006004
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006005 if (to_reserve)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006006 trace_btrfs_space_reservation(fs_info, "delalloc",
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006007 btrfs_ino(inode), to_reserve, 1);
Josef Bacik48c3d482016-03-25 13:25:49 -04006008 if (release_extra)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006009 btrfs_block_rsv_release(fs_info, block_rsv,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006010 btrfs_calc_trans_metadata_size(fs_info, 1));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006011 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00006012
6013out_fail:
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006014 spin_lock(&inode->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05006015 dropped = drop_outstanding_extent(inode, num_bytes);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006016 /*
6017 * If the inodes csum_bytes is the same as the original
6018 * csum_bytes then we know we haven't raced with any free()ers
6019 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00006020 */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006021 if (inode->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00006022 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04006023 } else {
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006024 u64 orig_csum_bytes = inode->csum_bytes;
Josef Bacikf4881bc2013-03-25 16:03:35 -04006025 u64 bytes;
6026
6027 /*
6028 * This is tricky, but first we need to figure out how much we
Nicholas D Steeves01327612016-05-19 21:18:45 -04006029 * freed from any free-ers that occurred during this
Josef Bacikf4881bc2013-03-25 16:03:35 -04006030 * reservation, so we reset ->csum_bytes to the csum_bytes
6031 * before we dropped our lock, and then call the free for the
6032 * number of bytes that were freed while we were trying our
6033 * reservation.
6034 */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006035 bytes = csum_bytes - inode->csum_bytes;
6036 inode->csum_bytes = csum_bytes;
Josef Bacikf4881bc2013-03-25 16:03:35 -04006037 to_free = calc_csum_metadata_size(inode, bytes, 0);
6038
6039
6040 /*
6041 * Now we need to see how much we would have freed had we not
6042 * been making this reservation and our ->csum_bytes were not
6043 * artificially inflated.
6044 */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006045 inode->csum_bytes = csum_bytes - num_bytes;
Josef Bacikf4881bc2013-03-25 16:03:35 -04006046 bytes = csum_bytes - orig_csum_bytes;
6047 bytes = calc_csum_metadata_size(inode, bytes, 0);
6048
6049 /*
6050 * Now reset ->csum_bytes to what it should be. If bytes is
Nicholas D Steeves01327612016-05-19 21:18:45 -04006051 * more than to_free then we would have freed more space had we
Josef Bacikf4881bc2013-03-25 16:03:35 -04006052 * not had an artificially high ->csum_bytes, so we need to free
6053 * the remainder. If bytes is the same or less then we don't
6054 * need to do anything, the other free-ers did the correct
6055 * thing.
6056 */
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006057 inode->csum_bytes = orig_csum_bytes - num_bytes;
Josef Bacikf4881bc2013-03-25 16:03:35 -04006058 if (bytes > to_free)
6059 to_free = bytes - to_free;
6060 else
6061 to_free = 0;
6062 }
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006063 spin_unlock(&inode->lock);
Dongsheng Yange2d1f922015-02-06 10:26:52 -05006064 if (dropped)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006065 to_free += btrfs_calc_trans_metadata_size(fs_info, dropped);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006066
6067 if (to_free) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006068 btrfs_block_rsv_release(fs_info, block_rsv, to_free);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006069 trace_btrfs_space_reservation(fs_info, "delalloc",
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006070 btrfs_ino(inode), to_free, 0);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006071 }
6072 if (delalloc_lock)
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006073 mutex_unlock(&inode->delalloc_mutex);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006074 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006075}
6076
Josef Bacik7709cde2011-08-04 10:25:02 -04006077/**
6078 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
6079 * @inode: the inode to release the reservation for
6080 * @num_bytes: the number of bytes we're releasing
6081 *
6082 * This will release the metadata reservation for an inode. This can be called
6083 * once we complete IO for a given set of bytes to release their metadata
6084 * reservations.
6085 */
Nikolay Borisov691fa052017-02-20 13:50:42 +02006086void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006087{
Nikolay Borisov691fa052017-02-20 13:50:42 +02006088 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006089 u64 to_free = 0;
6090 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006091
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006092 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006093 spin_lock(&inode->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05006094 dropped = drop_outstanding_extent(inode, num_bytes);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006095
Miao Xie09348562013-02-07 10:12:07 +00006096 if (num_bytes)
6097 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Nikolay Borisov691fa052017-02-20 13:50:42 +02006098 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006099 if (dropped > 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006100 to_free += btrfs_calc_trans_metadata_size(fs_info, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006101
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006102 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04006103 return;
6104
Nikolay Borisov691fa052017-02-20 13:50:42 +02006105 trace_btrfs_space_reservation(fs_info, "delalloc", btrfs_ino(inode),
6106 to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02006107
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006108 btrfs_block_rsv_release(fs_info, &fs_info->delalloc_block_rsv, to_free);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006109}
6110
Josef Bacik7709cde2011-08-04 10:25:02 -04006111/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006112 * btrfs_delalloc_reserve_space - reserve data and metadata space for
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006113 * delalloc
6114 * @inode: inode we're writing to
6115 * @start: start range we are writing to
6116 * @len: how long the range we are writing to
6117 *
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006118 * This will do the following things
6119 *
6120 * o reserve space in data space info for num bytes
6121 * and reserve precious corresponding qgroup space
6122 * (Done in check_data_free_space)
6123 *
6124 * o reserve space for metadata space, based on the number of outstanding
6125 * extents and how much csums will be needed
6126 * also reserve metadata space in a per root over-reserve method.
6127 * o add to the inodes->delalloc_bytes
6128 * o add it to the fs_info's delalloc inodes list.
6129 * (Above 3 all done in delalloc_reserve_metadata)
6130 *
6131 * Return 0 for success
6132 * Return <0 for error(-ENOSPC or -EQUOT)
6133 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006134int btrfs_delalloc_reserve_space(struct inode *inode, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006135{
6136 int ret;
6137
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006138 ret = btrfs_check_data_free_space(inode, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006139 if (ret < 0)
6140 return ret;
Nikolay Borisov9f3db422017-02-20 13:50:41 +02006141 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006142 if (ret < 0)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006143 btrfs_free_reserved_data_space(inode, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006144 return ret;
6145}
6146
6147/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006148 * btrfs_delalloc_release_space - release data and metadata space for delalloc
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006149 * @inode: inode we're releasing space for
6150 * @start: start position of the space already reserved
6151 * @len: the len of the space already reserved
6152 *
6153 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
6154 * called in the case that we don't need the metadata AND data reservations
6155 * anymore. So if there is an error or we insert an inline extent.
6156 *
6157 * This function will release the metadata space that was not used and will
6158 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
6159 * list if there are no delalloc bytes left.
6160 * Also it will handle the qgroup reserved space.
6161 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006162void btrfs_delalloc_release_space(struct inode *inode, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006163{
Nikolay Borisov691fa052017-02-20 13:50:42 +02006164 btrfs_delalloc_release_metadata(BTRFS_I(inode), len);
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006165 btrfs_free_reserved_data_space(inode, start, len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006166}
6167
Josef Bacikce93ec52014-11-17 15:45:48 -05006168static int update_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6202df62016-06-22 18:54:22 -04006169 struct btrfs_fs_info *info, u64 bytenr,
Josef Bacikce93ec52014-11-17 15:45:48 -05006170 u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04006171{
Josef Bacik0af3d002010-06-21 14:48:16 -04006172 struct btrfs_block_group_cache *cache = NULL;
Chris Masondb945352007-10-15 16:15:53 -04006173 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006174 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04006175 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04006176 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04006177
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006178 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00006179 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02006180 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006181 if (alloc)
6182 old_val += num_bytes;
6183 else
6184 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02006185 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00006186 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006187
Chris Masond3977122009-01-05 21:25:51 -05006188 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04006189 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006190 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006191 return -ENOENT;
Yan, Zhengb742bb822010-05-16 10:46:24 -04006192 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
6193 BTRFS_BLOCK_GROUP_RAID1 |
6194 BTRFS_BLOCK_GROUP_RAID10))
6195 factor = 2;
6196 else
6197 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04006198 /*
6199 * If this block group has free space cache written out, we
6200 * need to make sure to load it if we are removing space. This
6201 * is because we need the unpinning stage to actually add the
6202 * space back to the block group, otherwise we will leak space.
6203 */
6204 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00006205 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04006206
Chris Masondb945352007-10-15 16:15:53 -04006207 byte_in_group = bytenr - cache->key.objectid;
6208 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04006209
Josef Bacik25179202008-10-29 14:49:05 -04006210 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006211 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04006212
Jeff Mahoney6202df62016-06-22 18:54:22 -04006213 if (btrfs_test_opt(info, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04006214 cache->disk_cache_state < BTRFS_DC_CLEAR)
6215 cache->disk_cache_state = BTRFS_DC_CLEAR;
6216
Chris Mason9078a3e2007-04-26 16:46:15 -04006217 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04006218 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04006219 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04006220 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006221 btrfs_set_block_group_used(&cache->item, old_val);
6222 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006223 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb822010-05-16 10:46:24 -04006224 cache->space_info->bytes_used += num_bytes;
6225 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04006226 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006227 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04006228 } else {
Chris Masondb945352007-10-15 16:15:53 -04006229 old_val -= num_bytes;
Filipe Mananaae0ab002014-11-26 15:28:52 +00006230 btrfs_set_block_group_used(&cache->item, old_val);
6231 cache->pinned += num_bytes;
6232 cache->space_info->bytes_pinned += num_bytes;
6233 cache->space_info->bytes_used -= num_bytes;
6234 cache->space_info->disk_used -= num_bytes * factor;
6235 spin_unlock(&cache->lock);
6236 spin_unlock(&cache->space_info->lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04006237
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006238 trace_btrfs_space_reservation(info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006239 cache->space_info->flags,
6240 num_bytes, 1);
Filipe Mananaae0ab002014-11-26 15:28:52 +00006241 set_extent_dirty(info->pinned_extents,
6242 bytenr, bytenr + num_bytes - 1,
6243 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04006244 }
Chris Mason1bbc6212015-04-06 12:46:08 -07006245
6246 spin_lock(&trans->transaction->dirty_bgs_lock);
6247 if (list_empty(&cache->dirty_list)) {
6248 list_add_tail(&cache->dirty_list,
6249 &trans->transaction->dirty_bgs);
6250 trans->transaction->num_dirty_bgs++;
6251 btrfs_get_block_group(cache);
6252 }
6253 spin_unlock(&trans->transaction->dirty_bgs_lock);
6254
Filipe Manana036a9342015-11-23 15:25:16 +00006255 /*
6256 * No longer have used bytes in this block group, queue it for
6257 * deletion. We do this after adding the block group to the
6258 * dirty list to avoid races between cleaner kthread and space
6259 * cache writeout.
6260 */
6261 if (!alloc && old_val == 0) {
6262 spin_lock(&info->unused_bgs_lock);
6263 if (list_empty(&cache->bg_list)) {
6264 btrfs_get_block_group(cache);
6265 list_add_tail(&cache->bg_list,
6266 &info->unused_bgs);
6267 }
6268 spin_unlock(&info->unused_bgs_lock);
6269 }
6270
Chris Masonfa9c0d792009-04-03 09:47:43 -04006271 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04006272 total -= num_bytes;
6273 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006274 }
6275 return 0;
6276}
Chris Mason6324fbf2008-03-24 15:01:59 -04006277
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006278static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04006279{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006280 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05006281 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006282
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006283 spin_lock(&fs_info->block_group_cache_lock);
6284 bytenr = fs_info->first_logical_byte;
6285 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00006286
6287 if (bytenr < (u64)-1)
6288 return bytenr;
6289
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006290 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006291 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04006292 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006293
Yan Zhengd2fb3432008-12-11 16:30:39 -05006294 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006295 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006296
6297 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04006298}
6299
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006300static int pin_down_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006301 struct btrfs_block_group_cache *cache,
6302 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05006303{
Yan Zheng11833d62009-09-11 16:11:19 -04006304 spin_lock(&cache->space_info->lock);
6305 spin_lock(&cache->lock);
6306 cache->pinned += num_bytes;
6307 cache->space_info->bytes_pinned += num_bytes;
6308 if (reserved) {
6309 cache->reserved -= num_bytes;
6310 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05006311 }
Yan Zheng11833d62009-09-11 16:11:19 -04006312 spin_unlock(&cache->lock);
6313 spin_unlock(&cache->space_info->lock);
6314
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006315 trace_btrfs_space_reservation(fs_info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006316 cache->space_info->flags, num_bytes, 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006317 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006318 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05006319 return 0;
6320}
Chris Mason9078a3e2007-04-26 16:46:15 -04006321
Yan, Zhengf0486c62010-05-16 10:46:25 -04006322/*
6323 * this function must be called within transaction
6324 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006325int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006326 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04006327{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006328 struct btrfs_block_group_cache *cache;
6329
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006330 cache = btrfs_lookup_block_group(fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006331 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006332
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006333 pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006334
6335 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04006336 return 0;
6337}
Zheng Yane8569812008-09-26 10:05:48 -04006338
Yan, Zhengf0486c62010-05-16 10:46:25 -04006339/*
Chris Masone688b7252011-10-31 20:52:39 -04006340 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04006341 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006342int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04006343 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006344{
Chris Masone688b7252011-10-31 20:52:39 -04006345 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04006346 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04006347
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006348 cache = btrfs_lookup_block_group(fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006349 if (!cache)
6350 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04006351
6352 /*
6353 * pull in the free space cache (if any) so that our pin
6354 * removes the free space from the cache. We have load_only set
6355 * to one because the slow code to read in the free extents does check
6356 * the pinned extents.
6357 */
Liu Bof6373bf2012-12-27 09:01:18 +00006358 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04006359
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006360 pin_down_extent(fs_info, cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04006361
6362 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04006363 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04006364 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006365 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04006366}
6367
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006368static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
6369 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006370{
6371 int ret;
6372 struct btrfs_block_group_cache *block_group;
6373 struct btrfs_caching_control *caching_ctl;
6374
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006375 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006376 if (!block_group)
6377 return -EINVAL;
6378
6379 cache_block_group(block_group, 0);
6380 caching_ctl = get_caching_control(block_group);
6381
6382 if (!caching_ctl) {
6383 /* Logic error */
6384 BUG_ON(!block_group_cache_done(block_group));
6385 ret = btrfs_remove_free_space(block_group, start, num_bytes);
6386 } else {
6387 mutex_lock(&caching_ctl->mutex);
6388
6389 if (start >= caching_ctl->progress) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006390 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006391 } else if (start + num_bytes <= caching_ctl->progress) {
6392 ret = btrfs_remove_free_space(block_group,
6393 start, num_bytes);
6394 } else {
6395 num_bytes = caching_ctl->progress - start;
6396 ret = btrfs_remove_free_space(block_group,
6397 start, num_bytes);
6398 if (ret)
6399 goto out_lock;
6400
6401 num_bytes = (start + num_bytes) -
6402 caching_ctl->progress;
6403 start = caching_ctl->progress;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006404 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006405 }
6406out_lock:
6407 mutex_unlock(&caching_ctl->mutex);
6408 put_caching_control(caching_ctl);
6409 }
6410 btrfs_put_block_group(block_group);
6411 return ret;
6412}
6413
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006414int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info,
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006415 struct extent_buffer *eb)
6416{
6417 struct btrfs_file_extent_item *item;
6418 struct btrfs_key key;
6419 int found_type;
6420 int i;
6421
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006422 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006423 return 0;
6424
6425 for (i = 0; i < btrfs_header_nritems(eb); i++) {
6426 btrfs_item_key_to_cpu(eb, &key, i);
6427 if (key.type != BTRFS_EXTENT_DATA_KEY)
6428 continue;
6429 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
6430 found_type = btrfs_file_extent_type(eb, item);
6431 if (found_type == BTRFS_FILE_EXTENT_INLINE)
6432 continue;
6433 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
6434 continue;
6435 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
6436 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006437 __exclude_logged_extent(fs_info, key.objectid, key.offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006438 }
6439
6440 return 0;
6441}
6442
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006443static void
6444btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg)
6445{
6446 atomic_inc(&bg->reservations);
6447}
6448
6449void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
6450 const u64 start)
6451{
6452 struct btrfs_block_group_cache *bg;
6453
6454 bg = btrfs_lookup_block_group(fs_info, start);
6455 ASSERT(bg);
6456 if (atomic_dec_and_test(&bg->reservations))
6457 wake_up_atomic_t(&bg->reservations);
6458 btrfs_put_block_group(bg);
6459}
6460
6461static int btrfs_wait_bg_reservations_atomic_t(atomic_t *a)
6462{
6463 schedule();
6464 return 0;
6465}
6466
6467void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6468{
6469 struct btrfs_space_info *space_info = bg->space_info;
6470
6471 ASSERT(bg->ro);
6472
6473 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
6474 return;
6475
6476 /*
6477 * Our block group is read only but before we set it to read only,
6478 * some task might have had allocated an extent from it already, but it
6479 * has not yet created a respective ordered extent (and added it to a
6480 * root's list of ordered extents).
6481 * Therefore wait for any task currently allocating extents, since the
6482 * block group's reservations counter is incremented while a read lock
6483 * on the groups' semaphore is held and decremented after releasing
6484 * the read access on that semaphore and creating the ordered extent.
6485 */
6486 down_write(&space_info->groups_sem);
6487 up_write(&space_info->groups_sem);
6488
6489 wait_on_atomic_t(&bg->reservations,
6490 btrfs_wait_bg_reservations_atomic_t,
6491 TASK_UNINTERRUPTIBLE);
6492}
6493
Josef Bacikfb25e912011-07-26 17:00:46 -04006494/**
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006495 * btrfs_add_reserved_bytes - update the block_group and space info counters
Josef Bacikfb25e912011-07-26 17:00:46 -04006496 * @cache: The cache we are manipulating
Wang Xiaoguang18513092016-07-25 15:51:40 +08006497 * @ram_bytes: The number of bytes of file content, and will be same to
6498 * @num_bytes except for the compress path.
Josef Bacikfb25e912011-07-26 17:00:46 -04006499 * @num_bytes: The number of bytes in question
Miao Xiee570fd22014-06-19 10:42:50 +08006500 * @delalloc: The blocks are allocated for the delalloc write
Josef Bacikfb25e912011-07-26 17:00:46 -04006501 *
Xiaoguang Wang745699e2016-09-23 12:38:50 +08006502 * This is called by the allocator when it reserves space. If this is a
6503 * reservation and the block group has become read only we cannot make the
6504 * reservation and return -EAGAIN, otherwise this function always succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04006505 */
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006506static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
Wang Xiaoguang18513092016-07-25 15:51:40 +08006507 u64 ram_bytes, u64 num_bytes, int delalloc)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006508{
Josef Bacikfb25e912011-07-26 17:00:46 -04006509 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006510 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006511
Josef Bacikfb25e912011-07-26 17:00:46 -04006512 spin_lock(&space_info->lock);
6513 spin_lock(&cache->lock);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006514 if (cache->ro) {
6515 ret = -EAGAIN;
Josef Bacikfb25e912011-07-26 17:00:46 -04006516 } else {
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006517 cache->reserved += num_bytes;
6518 space_info->bytes_reserved += num_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006519
Wang Xiaoguang18513092016-07-25 15:51:40 +08006520 trace_btrfs_space_reservation(cache->fs_info,
6521 "space_info", space_info->flags,
6522 ram_bytes, 0);
6523 space_info->bytes_may_use -= ram_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006524 if (delalloc)
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006525 cache->delalloc_bytes += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006526 }
Josef Bacikfb25e912011-07-26 17:00:46 -04006527 spin_unlock(&cache->lock);
6528 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006529 return ret;
6530}
6531
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006532/**
6533 * btrfs_free_reserved_bytes - update the block_group and space info counters
6534 * @cache: The cache we are manipulating
6535 * @num_bytes: The number of bytes in question
6536 * @delalloc: The blocks are allocated for the delalloc write
6537 *
6538 * This is called by somebody who is freeing space that was never actually used
6539 * on disk. For example if you reserve some space for a new leaf in transaction
6540 * A and before transaction A commits you free that leaf, you call this with
6541 * reserve set to 0 in order to clear the reservation.
6542 */
6543
6544static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
6545 u64 num_bytes, int delalloc)
6546{
6547 struct btrfs_space_info *space_info = cache->space_info;
6548 int ret = 0;
6549
6550 spin_lock(&space_info->lock);
6551 spin_lock(&cache->lock);
6552 if (cache->ro)
6553 space_info->bytes_readonly += num_bytes;
6554 cache->reserved -= num_bytes;
6555 space_info->bytes_reserved -= num_bytes;
6556
6557 if (delalloc)
6558 cache->delalloc_bytes -= num_bytes;
6559 spin_unlock(&cache->lock);
6560 spin_unlock(&space_info->lock);
6561 return ret;
6562}
David Sterba8b74c032017-02-10 19:20:56 +01006563void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04006564{
Yan Zheng11833d62009-09-11 16:11:19 -04006565 struct btrfs_caching_control *next;
6566 struct btrfs_caching_control *caching_ctl;
6567 struct btrfs_block_group_cache *cache;
6568
Josef Bacik9e351cc2014-03-13 15:42:13 -04006569 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04006570
6571 list_for_each_entry_safe(caching_ctl, next,
6572 &fs_info->caching_block_groups, list) {
6573 cache = caching_ctl->block_group;
6574 if (block_group_cache_done(cache)) {
6575 cache->last_byte_to_unpin = (u64)-1;
6576 list_del_init(&caching_ctl->list);
6577 put_caching_control(caching_ctl);
6578 } else {
6579 cache->last_byte_to_unpin = caching_ctl->progress;
6580 }
6581 }
6582
6583 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6584 fs_info->pinned_extents = &fs_info->freed_extents[1];
6585 else
6586 fs_info->pinned_extents = &fs_info->freed_extents[0];
6587
Josef Bacik9e351cc2014-03-13 15:42:13 -04006588 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006589
6590 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04006591}
6592
Josef Bacikc759c4e2015-10-02 15:25:10 -04006593/*
6594 * Returns the free cluster for the given space info and sets empty_cluster to
6595 * what it should be based on the mount options.
6596 */
6597static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006598fetch_cluster_info(struct btrfs_fs_info *fs_info,
6599 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04006600{
6601 struct btrfs_free_cluster *ret = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006602 bool ssd = btrfs_test_opt(fs_info, SSD);
Josef Bacikc759c4e2015-10-02 15:25:10 -04006603
6604 *empty_cluster = 0;
6605 if (btrfs_mixed_space_info(space_info))
6606 return ret;
6607
6608 if (ssd)
Byongho Leeee221842015-12-15 01:42:10 +09006609 *empty_cluster = SZ_2M;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006610 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006611 ret = &fs_info->meta_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006612 if (!ssd)
Byongho Leeee221842015-12-15 01:42:10 +09006613 *empty_cluster = SZ_64K;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006614 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && ssd) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006615 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006616 }
6617
6618 return ret;
6619}
6620
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006621static int unpin_extent_range(struct btrfs_fs_info *fs_info,
6622 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00006623 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04006624{
Yan Zheng11833d62009-09-11 16:11:19 -04006625 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04006626 struct btrfs_space_info *space_info;
6627 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006628 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04006629 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006630 u64 total_unpinned = 0;
6631 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04006632 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04006633
6634 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04006635 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04006636 if (!cache ||
6637 start >= cache->key.objectid + cache->key.offset) {
6638 if (cache)
6639 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04006640 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006641 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006642 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04006643
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006644 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04006645 cache->space_info,
6646 &empty_cluster);
6647 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04006648 }
6649
6650 len = cache->key.objectid + cache->key.offset - start;
6651 len = min(len, end + 1 - start);
6652
6653 if (start < cache->last_byte_to_unpin) {
6654 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00006655 if (return_free_space)
6656 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04006657 }
Josef Bacik25179202008-10-29 14:49:05 -04006658
Yan, Zhengf0486c62010-05-16 10:46:25 -04006659 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006660 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006661 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006662
Josef Bacikc759c4e2015-10-02 15:25:10 -04006663 /*
6664 * If this space cluster has been marked as fragmented and we've
6665 * unpinned enough in this block group to potentially allow a
6666 * cluster to be created inside of it go ahead and clear the
6667 * fragmented check.
6668 */
6669 if (cluster && cluster->fragmented &&
6670 total_unpinned > empty_cluster) {
6671 spin_lock(&cluster->lock);
6672 cluster->fragmented = 0;
6673 spin_unlock(&cluster->lock);
6674 }
6675
Josef Bacik7b398f82012-10-22 15:52:28 -04006676 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006677 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006678 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006679 space_info->bytes_pinned -= len;
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006680
6681 trace_btrfs_space_reservation(fs_info, "pinned",
6682 space_info->flags, len, 0);
Josef Bacik4f4db212015-09-29 11:40:47 -04006683 space_info->max_extent_size = 0;
Liu Bod288db52014-07-02 16:58:01 +08006684 percpu_counter_add(&space_info->total_bytes_pinned, -len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006685 if (cache->ro) {
6686 space_info->bytes_readonly += len;
6687 readonly = true;
6688 }
Josef Bacik25179202008-10-29 14:49:05 -04006689 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006690 if (!readonly && return_free_space &&
6691 global_rsv->space_info == space_info) {
6692 u64 to_add = len;
6693 WARN_ON(!return_free_space);
Josef Bacik7b398f82012-10-22 15:52:28 -04006694 spin_lock(&global_rsv->lock);
6695 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04006696 to_add = min(len, global_rsv->size -
6697 global_rsv->reserved);
6698 global_rsv->reserved += to_add;
6699 space_info->bytes_may_use += to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006700 if (global_rsv->reserved >= global_rsv->size)
6701 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04006702 trace_btrfs_space_reservation(fs_info,
6703 "space_info",
6704 space_info->flags,
6705 to_add, 1);
6706 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006707 }
6708 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006709 /* Add to any tickets we may have */
6710 if (len)
6711 space_info_add_new_bytes(fs_info, space_info,
6712 len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006713 }
6714 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006715 }
6716
6717 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006718 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04006719 return 0;
6720}
6721
6722int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006723 struct btrfs_fs_info *fs_info)
Chris Masona28ec192007-03-06 20:08:01 -05006724{
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006725 struct btrfs_block_group_cache *block_group, *tmp;
6726 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04006727 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04006728 u64 start;
6729 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05006730 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05006731
Yan Zheng11833d62009-09-11 16:11:19 -04006732 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6733 unpin = &fs_info->freed_extents[1];
6734 else
6735 unpin = &fs_info->freed_extents[0];
6736
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006737 while (!trans->aborted) {
Filipe Mananad4b450c2015-01-29 19:18:25 +00006738 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04006739 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04006740 EXTENT_DIRTY, NULL);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006741 if (ret) {
6742 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05006743 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00006744 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006745
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006746 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006747 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00006748 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006749
David Sterbaaf6f8f62016-04-26 23:54:39 +02006750 clear_extent_dirty(unpin, start, end);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006751 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006752 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masonb9473432009-03-13 11:00:37 -04006753 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05006754 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006755
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006756 /*
6757 * Transaction is finished. We don't need the lock anymore. We
6758 * do need to clean up the block groups in case of a transaction
6759 * abort.
6760 */
6761 deleted_bgs = &trans->transaction->deleted_bgs;
6762 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
6763 u64 trimmed = 0;
6764
6765 ret = -EROFS;
6766 if (!trans->aborted)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006767 ret = btrfs_discard_extent(fs_info,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006768 block_group->key.objectid,
6769 block_group->key.offset,
6770 &trimmed);
6771
6772 list_del_init(&block_group->bg_list);
6773 btrfs_put_block_group_trimming(block_group);
6774 btrfs_put_block_group(block_group);
6775
6776 if (ret) {
6777 const char *errstr = btrfs_decode_error(ret);
6778 btrfs_warn(fs_info,
6779 "Discard failed while removing blockgroup: errno=%d %s\n",
6780 ret, errstr);
6781 }
6782 }
6783
Chris Masone20d96d2007-03-22 12:13:20 -04006784 return 0;
6785}
6786
Josef Bacikb150a4f2013-06-19 15:00:04 -04006787static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
6788 u64 owner, u64 root_objectid)
6789{
6790 struct btrfs_space_info *space_info;
6791 u64 flags;
6792
6793 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
6794 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
6795 flags = BTRFS_BLOCK_GROUP_SYSTEM;
6796 else
6797 flags = BTRFS_BLOCK_GROUP_METADATA;
6798 } else {
6799 flags = BTRFS_BLOCK_GROUP_DATA;
6800 }
6801
6802 space_info = __find_space_info(fs_info, flags);
6803 BUG_ON(!space_info); /* Logic bug */
6804 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
6805}
6806
6807
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006808static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006809 struct btrfs_fs_info *info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006810 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006811 u64 root_objectid, u64 owner_objectid,
6812 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006813 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05006814{
Chris Masone2fa7222007-03-12 16:22:34 -04006815 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006816 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04006817 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04006818 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006819 struct btrfs_extent_item *ei;
6820 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05006821 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006822 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05006823 int extent_slot = 0;
6824 int found_extent = 0;
6825 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006826 u32 item_size;
6827 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006828 u64 bytenr = node->bytenr;
6829 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07006830 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006831 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05006832
Chris Mason5caf2a02007-04-02 11:20:42 -04006833 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04006834 if (!path)
6835 return -ENOMEM;
6836
David Sterbae4058b52015-11-27 16:31:35 +01006837 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04006838 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006839
6840 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
6841 BUG_ON(!is_data && refs_to_drop != 1);
6842
Josef Bacik3173a182013-03-07 14:22:04 -05006843 if (is_data)
6844 skinny_metadata = 0;
6845
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05006846 ret = lookup_extent_backref(trans, info, path, &iref,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006847 bytenr, num_bytes, parent,
6848 root_objectid, owner_objectid,
6849 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05006850 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05006851 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006852 while (extent_slot >= 0) {
6853 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05006854 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006855 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05006856 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006857 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
6858 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05006859 found_extent = 1;
6860 break;
6861 }
Josef Bacik3173a182013-03-07 14:22:04 -05006862 if (key.type == BTRFS_METADATA_ITEM_KEY &&
6863 key.offset == owner_objectid) {
6864 found_extent = 1;
6865 break;
6866 }
Chris Mason952fcca2008-02-18 16:33:44 -05006867 if (path->slots[0] - extent_slot > 5)
6868 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006869 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05006870 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006871#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6872 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
6873 if (found_extent && item_size < sizeof(*ei))
6874 found_extent = 0;
6875#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04006876 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006877 BUG_ON(iref);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05006878 ret = remove_extent_backref(trans, info, path, NULL,
6879 refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07006880 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06006881 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006882 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006883 goto out;
6884 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006885 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04006886 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006887
6888 key.objectid = bytenr;
6889 key.type = BTRFS_EXTENT_ITEM_KEY;
6890 key.offset = num_bytes;
6891
Josef Bacik3173a182013-03-07 14:22:04 -05006892 if (!is_data && skinny_metadata) {
6893 key.type = BTRFS_METADATA_ITEM_KEY;
6894 key.offset = owner_objectid;
6895 }
6896
Zheng Yan31840ae2008-09-23 13:14:14 -04006897 ret = btrfs_search_slot(trans, extent_root,
6898 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05006899 if (ret > 0 && skinny_metadata && path->slots[0]) {
6900 /*
6901 * Couldn't find our skinny metadata item,
6902 * see if we have ye olde extent item.
6903 */
6904 path->slots[0]--;
6905 btrfs_item_key_to_cpu(path->nodes[0], &key,
6906 path->slots[0]);
6907 if (key.objectid == bytenr &&
6908 key.type == BTRFS_EXTENT_ITEM_KEY &&
6909 key.offset == num_bytes)
6910 ret = 0;
6911 }
6912
6913 if (ret > 0 && skinny_metadata) {
6914 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01006915 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05006916 key.type = BTRFS_EXTENT_ITEM_KEY;
6917 key.offset = num_bytes;
6918 btrfs_release_path(path);
6919 ret = btrfs_search_slot(trans, extent_root,
6920 &key, path, -1, 1);
6921 }
6922
Josef Bacikf3465ca2008-11-12 14:19:50 -05006923 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006924 btrfs_err(info,
6925 "umm, got %d back from search, was looking for %llu",
6926 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00006927 if (ret > 0)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006928 btrfs_print_leaf(info, path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006929 }
David Sterba005d6422012-09-18 07:52:32 -06006930 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006931 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006932 goto out;
6933 }
Zheng Yan31840ae2008-09-23 13:14:14 -04006934 extent_slot = path->slots[0];
6935 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306936 } else if (WARN_ON(ret == -ENOENT)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006937 btrfs_print_leaf(info, path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006938 btrfs_err(info,
6939 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006940 bytenr, parent, root_objectid, owner_objectid,
6941 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04006942 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04006943 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006944 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04006945 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006946 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05006947 }
Chris Mason5f39d392007-10-15 16:14:19 -04006948
6949 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006950 item_size = btrfs_item_size_nr(leaf, extent_slot);
6951#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6952 if (item_size < sizeof(*ei)) {
6953 BUG_ON(found_extent || extent_slot != path->slots[0]);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05006954 ret = convert_extent_item_v0(trans, info, path, owner_objectid,
6955 0);
David Sterba005d6422012-09-18 07:52:32 -06006956 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006957 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006958 goto out;
6959 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006960
David Sterbab3b4aa72011-04-21 01:20:15 +02006961 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006962 path->leave_spinning = 1;
6963
6964 key.objectid = bytenr;
6965 key.type = BTRFS_EXTENT_ITEM_KEY;
6966 key.offset = num_bytes;
6967
6968 ret = btrfs_search_slot(trans, extent_root, &key, path,
6969 -1, 1);
6970 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006971 btrfs_err(info,
6972 "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006973 ret, bytenr);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006974 btrfs_print_leaf(info, path->nodes[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006975 }
David Sterba005d6422012-09-18 07:52:32 -06006976 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006977 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006978 goto out;
6979 }
6980
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006981 extent_slot = path->slots[0];
6982 leaf = path->nodes[0];
6983 item_size = btrfs_item_size_nr(leaf, extent_slot);
6984 }
6985#endif
6986 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05006987 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04006988 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05006989 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
6990 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006991 struct btrfs_tree_block_info *bi;
6992 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
6993 bi = (struct btrfs_tree_block_info *)(ei + 1);
6994 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05006995 }
6996
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006997 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04006998 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006999 btrfs_err(info,
7000 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
7001 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04007002 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04007003 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04007004 goto out;
7005 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007006 refs -= refs_to_drop;
7007
7008 if (refs > 0) {
7009 if (extent_op)
7010 __run_delayed_extent_op(extent_op, leaf, ei);
7011 /*
7012 * In the case of inline back ref, reference count will
7013 * be updated by remove_extent_backref
7014 */
7015 if (iref) {
7016 BUG_ON(!found_extent);
7017 } else {
7018 btrfs_set_extent_refs(leaf, ei, refs);
7019 btrfs_mark_buffer_dirty(leaf);
7020 }
7021 if (found_extent) {
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05007022 ret = remove_extent_backref(trans, info, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007023 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07007024 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06007025 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007026 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007027 goto out;
7028 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007029 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007030 add_pinned_bytes(info, -num_bytes, owner_objectid,
Josef Bacikb150a4f2013-06-19 15:00:04 -04007031 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007032 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007033 if (found_extent) {
7034 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08007035 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007036 if (iref) {
7037 BUG_ON(path->slots[0] != extent_slot);
7038 } else {
7039 BUG_ON(path->slots[0] != extent_slot + 1);
7040 path->slots[0] = extent_slot;
7041 num_to_del = 2;
7042 }
Chris Mason78fae272007-03-25 11:35:08 -04007043 }
Chris Masonb9473432009-03-13 11:00:37 -04007044
Josef Bacikfcebe452014-05-13 17:30:47 -07007045 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05007046 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
7047 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06007048 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007049 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007050 goto out;
7051 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007052 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01007053
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007054 if (is_data) {
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007055 ret = btrfs_del_csums(trans, info, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06007056 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007057 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007058 goto out;
7059 }
Chris Mason459931e2008-12-10 09:10:46 -05007060 }
7061
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007062 ret = add_to_free_space_tree(trans, info, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007063 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007064 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007065 goto out;
7066 }
7067
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007068 ret = update_block_group(trans, info, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06007069 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007070 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007071 goto out;
7072 }
Chris Masona28ec192007-03-06 20:08:01 -05007073 }
Josef Bacikfcebe452014-05-13 17:30:47 -07007074 btrfs_release_path(path);
7075
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007076out:
Chris Mason5caf2a02007-04-02 11:20:42 -04007077 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05007078 return ret;
7079}
7080
7081/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007082 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04007083 * delayed ref for that extent as well. This searches the delayed ref tree for
7084 * a given extent, and if there are no other delayed refs to be processed, it
7085 * removes it from the tree.
7086 */
7087static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007088 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04007089{
7090 struct btrfs_delayed_ref_head *head;
7091 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007092 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04007093
7094 delayed_refs = &trans->transaction->delayed_refs;
7095 spin_lock(&delayed_refs->lock);
Liu Bof72ad18e2017-01-30 12:24:37 -08007096 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
Chris Mason1887be62009-03-13 10:11:24 -04007097 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08007098 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04007099
Josef Bacikd7df2c72014-01-23 09:21:38 -05007100 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08007101 if (!list_empty(&head->ref_list))
Chris Mason1887be62009-03-13 10:11:24 -04007102 goto out;
7103
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007104 if (head->extent_op) {
7105 if (!head->must_insert_reserved)
7106 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00007107 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007108 head->extent_op = NULL;
7109 }
7110
Chris Mason1887be62009-03-13 10:11:24 -04007111 /*
7112 * waiting for the lock here would deadlock. If someone else has it
7113 * locked they are already in the process of dropping it anyway
7114 */
7115 if (!mutex_trylock(&head->mutex))
7116 goto out;
7117
7118 /*
7119 * at this point we have a head with no other entries. Go
7120 * ahead and process it.
7121 */
7122 head->node.in_tree = 0;
Liu Boc46effa2013-10-14 12:59:45 +08007123 rb_erase(&head->href_node, &delayed_refs->href_root);
Chris Masonc3e69d52009-03-13 10:17:05 -04007124
Josef Bacikd7df2c72014-01-23 09:21:38 -05007125 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04007126
7127 /*
7128 * we don't take a ref on the node because we're removing it from the
7129 * tree, so we just steal the ref the tree was holding.
7130 */
Chris Masonc3e69d52009-03-13 10:17:05 -04007131 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007132 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04007133 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007134 head->processing = 0;
7135 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04007136 spin_unlock(&delayed_refs->lock);
7137
Yan, Zhengf0486c62010-05-16 10:46:25 -04007138 BUG_ON(head->extent_op);
7139 if (head->must_insert_reserved)
7140 ret = 1;
7141
7142 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04007143 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007144 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04007145out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05007146 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08007147
7148out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04007149 spin_unlock(&delayed_refs->lock);
7150 return 0;
7151}
7152
Yan, Zhengf0486c62010-05-16 10:46:25 -04007153void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7154 struct btrfs_root *root,
7155 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02007156 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007157{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007158 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb150a4f2013-06-19 15:00:04 -04007159 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007160 int ret;
7161
7162 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007163 ret = btrfs_add_delayed_tree_ref(fs_info, trans,
7164 buf->start, buf->len,
7165 parent,
7166 root->root_key.objectid,
7167 btrfs_header_level(buf),
7168 BTRFS_DROP_DELAYED_REF, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007169 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007170 }
7171
7172 if (!last_ref)
7173 return;
7174
Yan, Zhengf0486c62010-05-16 10:46:25 -04007175 if (btrfs_header_generation(buf) == trans->transid) {
Filipe Manana62198722015-01-06 20:18:45 +00007176 struct btrfs_block_group_cache *cache;
7177
Yan, Zhengf0486c62010-05-16 10:46:25 -04007178 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007179 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007180 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04007181 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007182 }
7183
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007184 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00007185
Yan, Zhengf0486c62010-05-16 10:46:25 -04007186 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007187 pin_down_extent(fs_info, cache, buf->start,
7188 buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00007189 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04007190 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007191 }
7192
7193 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
7194
7195 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08007196 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00007197 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007198 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Josef Bacikb150a4f2013-06-19 15:00:04 -04007199 pin = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007200 }
7201out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04007202 if (pin)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007203 add_pinned_bytes(fs_info, buf->len, btrfs_header_level(buf),
Josef Bacikb150a4f2013-06-19 15:00:04 -04007204 root->root_key.objectid);
7205
Josef Bacika826d6d2011-03-16 13:42:43 -04007206 /*
7207 * Deleting the buffer, clear the corrupt flag since it doesn't matter
7208 * anymore.
7209 */
7210 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007211}
7212
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007213/* Can return -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007214int btrfs_free_extent(struct btrfs_trans_handle *trans,
7215 struct btrfs_fs_info *fs_info,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007216 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01007217 u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04007218{
7219 int ret;
7220
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04007221 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007222 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02007223
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007224 add_pinned_bytes(fs_info, num_bytes, owner, root_objectid);
Josef Bacikb150a4f2013-06-19 15:00:04 -04007225
Chris Mason56bec292009-03-13 10:10:06 -04007226 /*
7227 * tree log blocks never actually go into the extent allocation
7228 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04007229 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007230 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
7231 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04007232 /* unlocks the pinned mutex */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007233 btrfs_pin_extent(fs_info, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04007234 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007235 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007236 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
7237 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007238 parent, root_objectid, (int)owner,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01007239 BTRFS_DROP_DELAYED_REF, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007240 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007241 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
7242 num_bytes,
7243 parent, root_objectid, owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08007244 offset, 0,
Jeff Mahoneyfef394f2016-12-13 14:39:34 -05007245 BTRFS_DROP_DELAYED_REF);
Chris Mason56bec292009-03-13 10:10:06 -04007246 }
Chris Mason925baed2008-06-25 16:01:30 -04007247 return ret;
7248}
7249
Chris Masonfec577f2007-02-26 10:40:21 -05007250/*
Josef Bacik817d52f2009-07-13 21:29:25 -04007251 * when we wait for progress in the block group caching, its because
7252 * our allocation attempt failed at least once. So, we must sleep
7253 * and let some progress happen before we try again.
7254 *
7255 * This function will sleep at least once waiting for new free space to
7256 * show up, and then it will check the block group free space numbers
7257 * for our min num_bytes. Another option is to have it go ahead
7258 * and look in the rbtree for a free extent of a given size, but this
7259 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04007260 *
7261 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
7262 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04007263 */
Josef Bacik36cce922013-08-05 11:15:21 -04007264static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04007265wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
7266 u64 num_bytes)
7267{
Yan Zheng11833d62009-09-11 16:11:19 -04007268 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04007269
Yan Zheng11833d62009-09-11 16:11:19 -04007270 caching_ctl = get_caching_control(cache);
7271 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007272 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04007273
Yan Zheng11833d62009-09-11 16:11:19 -04007274 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08007275 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04007276
7277 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04007278}
7279
7280static noinline int
7281wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
7282{
7283 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04007284 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007285
7286 caching_ctl = get_caching_control(cache);
7287 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007288 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007289
7290 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04007291 if (cache->cached == BTRFS_CACHE_ERROR)
7292 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04007293 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04007294 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04007295}
7296
Liu Bo31e50222012-11-21 14:18:10 +00007297int __get_raid_index(u64 flags)
Yan, Zhengb742bb822010-05-16 10:46:24 -04007298{
Ilya Dryomov7738a532012-03-27 17:09:17 +03007299 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00007300 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007301 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00007302 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007303 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00007304 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007305 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00007306 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05007307 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05007308 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05007309 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05007310 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007311
Chris Masone942f882013-02-20 14:06:05 -05007312 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb822010-05-16 10:46:24 -04007313}
7314
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007315int get_block_group_index(struct btrfs_block_group_cache *cache)
Ilya Dryomov7738a532012-03-27 17:09:17 +03007316{
Liu Bo31e50222012-11-21 14:18:10 +00007317 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03007318}
7319
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007320static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
7321 [BTRFS_RAID_RAID10] = "raid10",
7322 [BTRFS_RAID_RAID1] = "raid1",
7323 [BTRFS_RAID_DUP] = "dup",
7324 [BTRFS_RAID_RAID0] = "raid0",
7325 [BTRFS_RAID_SINGLE] = "single",
7326 [BTRFS_RAID_RAID5] = "raid5",
7327 [BTRFS_RAID_RAID6] = "raid6",
7328};
7329
Jeff Mahoney1b8e5df2013-11-20 16:50:23 -05007330static const char *get_raid_name(enum btrfs_raid_types type)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007331{
7332 if (type >= BTRFS_NR_RAID_TYPES)
7333 return NULL;
7334
7335 return btrfs_raid_type_names[type];
7336}
7337
Josef Bacik817d52f2009-07-13 21:29:25 -04007338enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05007339 LOOP_CACHING_NOWAIT = 0,
7340 LOOP_CACHING_WAIT = 1,
7341 LOOP_ALLOC_CHUNK = 2,
7342 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04007343};
7344
Miao Xiee570fd22014-06-19 10:42:50 +08007345static inline void
7346btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
7347 int delalloc)
7348{
7349 if (delalloc)
7350 down_read(&cache->data_rwsem);
7351}
7352
7353static inline void
7354btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
7355 int delalloc)
7356{
7357 btrfs_get_block_group(cache);
7358 if (delalloc)
7359 down_read(&cache->data_rwsem);
7360}
7361
7362static struct btrfs_block_group_cache *
7363btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
7364 struct btrfs_free_cluster *cluster,
7365 int delalloc)
7366{
Sudip Mukherjee89771cc2016-02-16 13:32:47 +05307367 struct btrfs_block_group_cache *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007368
Miao Xiee570fd22014-06-19 10:42:50 +08007369 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007370 while (1) {
7371 used_bg = cluster->block_group;
7372 if (!used_bg)
7373 return NULL;
7374
7375 if (used_bg == block_group)
7376 return used_bg;
7377
7378 btrfs_get_block_group(used_bg);
7379
7380 if (!delalloc)
7381 return used_bg;
7382
7383 if (down_read_trylock(&used_bg->data_rwsem))
7384 return used_bg;
7385
7386 spin_unlock(&cluster->refill_lock);
7387
Liu Boe321f8a2016-11-30 16:11:04 -08007388 /* We should only have one-level nested. */
7389 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007390
7391 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007392 if (used_bg == cluster->block_group)
7393 return used_bg;
7394
7395 up_read(&used_bg->data_rwsem);
7396 btrfs_put_block_group(used_bg);
7397 }
Miao Xiee570fd22014-06-19 10:42:50 +08007398}
7399
7400static inline void
7401btrfs_release_block_group(struct btrfs_block_group_cache *cache,
7402 int delalloc)
7403{
7404 if (delalloc)
7405 up_read(&cache->data_rwsem);
7406 btrfs_put_block_group(cache);
7407}
7408
Josef Bacik817d52f2009-07-13 21:29:25 -04007409/*
Chris Masonfec577f2007-02-26 10:40:21 -05007410 * walks the btree of allocated extents and find a hole of a given size.
7411 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08007412 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04007413 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08007414 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05007415 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08007416 *
7417 * If there is no suitable free space, we will record the max size of
7418 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05007419 */
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05007420static noinline int find_free_extent(struct btrfs_fs_info *fs_info,
Wang Xiaoguang18513092016-07-25 15:51:40 +08007421 u64 ram_bytes, u64 num_bytes, u64 empty_size,
7422 u64 hint_byte, struct btrfs_key *ins,
7423 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007424{
Josef Bacik80eb2342008-10-29 14:49:05 -04007425 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007426 struct btrfs_root *root = fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007427 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04007428 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05007429 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08007430 u64 max_extent_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007431 u64 empty_cluster = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04007432 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007433 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00007434 int index = __get_raid_index(flags);
Josef Bacik0a243252009-09-11 16:11:20 -04007435 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007436 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04007437 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08007438 bool have_caching_bg = false;
chandan13a0db52015-11-02 13:59:46 +05307439 bool orig_have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007440 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05007441
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007442 WARN_ON(num_bytes < fs_info->sectorsize);
David Sterba962a2982014-06-04 18:41:45 +02007443 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04007444 ins->objectid = 0;
7445 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04007446
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007447 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05007448
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007449 space_info = __find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007450 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007451 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007452 return -ENOSPC;
7453 }
Josef Bacik2552d172009-04-03 10:14:19 -04007454
Josef Bacik67377732010-09-16 16:19:09 -04007455 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04007456 * If our free space is heavily fragmented we may not be able to make
7457 * big contiguous allocations, so instead of doing the expensive search
7458 * for free space, simply return ENOSPC with our max_extent_size so we
7459 * can go ahead and search for a more manageable chunk.
7460 *
7461 * If our max_extent_size is large enough for our allocation simply
7462 * disable clustering since we will likely not be able to find enough
7463 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04007464 */
Josef Bacik4f4db212015-09-29 11:40:47 -04007465 if (unlikely(space_info->max_extent_size)) {
7466 spin_lock(&space_info->lock);
7467 if (space_info->max_extent_size &&
7468 num_bytes > space_info->max_extent_size) {
7469 ins->offset = space_info->max_extent_size;
7470 spin_unlock(&space_info->lock);
7471 return -ENOSPC;
7472 } else if (space_info->max_extent_size) {
7473 use_cluster = false;
7474 }
7475 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007476 }
7477
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007478 last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04007479 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007480 spin_lock(&last_ptr->lock);
7481 if (last_ptr->block_group)
7482 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007483 if (last_ptr->fragmented) {
7484 /*
7485 * We still set window_start so we can keep track of the
7486 * last place we found an allocation to try and save
7487 * some time.
7488 */
7489 hint_byte = last_ptr->window_start;
7490 use_cluster = false;
7491 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007492 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007493 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007494
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007495 search_start = max(search_start, first_logical_byte(fs_info, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04007496 search_start = max(search_start, hint_byte);
Josef Bacik2552d172009-04-03 10:14:19 -04007497 if (search_start == hint_byte) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007498 block_group = btrfs_lookup_block_group(fs_info, search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04007499 /*
7500 * we don't want to use the block group if it doesn't match our
7501 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05007502 *
7503 * However if we are re-searching with an ideal block group
7504 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04007505 */
David Sterbab6919a52013-04-29 13:39:40 +00007506 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05007507 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04007508 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007509 if (list_empty(&block_group->list) ||
7510 block_group->ro) {
7511 /*
7512 * someone is removing this block group,
7513 * we can't jump into the have_block_group
7514 * target because our list pointers are not
7515 * valid
7516 */
7517 btrfs_put_block_group(block_group);
7518 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05007519 } else {
Yan, Zhengb742bb822010-05-16 10:46:24 -04007520 index = get_block_group_index(block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007521 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04007522 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05007523 }
Josef Bacik2552d172009-04-03 10:14:19 -04007524 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007525 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04007526 }
Chris Mason42e70e72008-11-07 18:17:11 -05007527 }
Josef Bacik2552d172009-04-03 10:14:19 -04007528search:
Miao Xie60d2adb2011-09-09 17:34:35 +08007529 have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007530 if (index == 0 || index == __get_raid_index(flags))
7531 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04007532 down_read(&space_info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007533 list_for_each_entry(block_group, &space_info->block_groups[index],
7534 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04007535 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04007536 int cached;
Chris Mason8a1413a22008-11-10 16:13:54 -05007537
Miao Xiee570fd22014-06-19 10:42:50 +08007538 btrfs_grab_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007539 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05007540
Chris Mason83a50de2010-12-13 15:06:46 -05007541 /*
7542 * this can happen if we end up cycling through all the
7543 * raid types, but we want to make sure we only allocate
7544 * for the proper type.
7545 */
David Sterbab6919a52013-04-29 13:39:40 +00007546 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05007547 u64 extra = BTRFS_BLOCK_GROUP_DUP |
7548 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007549 BTRFS_BLOCK_GROUP_RAID5 |
7550 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05007551 BTRFS_BLOCK_GROUP_RAID10;
7552
7553 /*
7554 * if they asked for extra copies and this block group
7555 * doesn't provide them, bail. This does allow us to
7556 * fill raid0 from raid1.
7557 */
David Sterbab6919a52013-04-29 13:39:40 +00007558 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05007559 goto loop;
7560 }
7561
Josef Bacik2552d172009-04-03 10:14:19 -04007562have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05007563 cached = block_group_cache_done(block_group);
7564 if (unlikely(!cached)) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007565 have_caching_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00007566 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04007567 BUG_ON(ret < 0);
7568 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05007569 }
7570
Josef Bacik36cce922013-08-05 11:15:21 -04007571 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
7572 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05007573 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04007574 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007575
Josef Bacik0a243252009-09-11 16:11:20 -04007576 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007577 * Ok we want to try and use the cluster allocator, so
7578 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04007579 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04007580 if (last_ptr && use_cluster) {
Miao Xie215a63d2014-01-15 20:00:56 +08007581 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05007582 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007583 /*
7584 * the refill lock keeps out other
7585 * people trying to start a new cluster
7586 */
Miao Xiee570fd22014-06-19 10:42:50 +08007587 used_block_group = btrfs_lock_cluster(block_group,
7588 last_ptr,
7589 delalloc);
7590 if (!used_block_group)
Chris Mason44fb5512009-06-04 15:34:51 -04007591 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04007592
Miao Xiee570fd22014-06-19 10:42:50 +08007593 if (used_block_group != block_group &&
7594 (used_block_group->ro ||
7595 !block_group_bits(used_block_group, flags)))
7596 goto release_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007597
7598 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007599 last_ptr,
7600 num_bytes,
7601 used_block_group->key.objectid,
7602 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007603 if (offset) {
7604 /* we have a block, we're done */
7605 spin_unlock(&last_ptr->refill_lock);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007606 trace_btrfs_reserve_extent_cluster(fs_info,
Miao Xie89d43462014-01-15 20:00:57 +08007607 used_block_group,
7608 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08007609 if (used_block_group != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08007610 btrfs_release_block_group(block_group,
7611 delalloc);
Miao Xie215a63d2014-01-15 20:00:56 +08007612 block_group = used_block_group;
7613 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007614 goto checks;
7615 }
7616
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007617 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007618release_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007619 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7620 * set up a new clusters, so lets just skip it
7621 * and let the allocator find whatever block
7622 * it can find. If we reach this point, we
7623 * will have tried the cluster allocator
7624 * plenty of times and not have found
7625 * anything, so we are likely way too
7626 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007627 * anything.
7628 *
7629 * However, if the cluster is taken from the
7630 * current block group, release the cluster
7631 * first, so that we stand a better chance of
7632 * succeeding in the unclustered
7633 * allocation. */
7634 if (loop >= LOOP_NO_EMPTY_SIZE &&
Miao Xiee570fd22014-06-19 10:42:50 +08007635 used_block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007636 spin_unlock(&last_ptr->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007637 btrfs_release_block_group(used_block_group,
7638 delalloc);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007639 goto unclustered_alloc;
7640 }
7641
Chris Masonfa9c0d792009-04-03 09:47:43 -04007642 /*
7643 * this cluster didn't work out, free it and
7644 * start over
7645 */
7646 btrfs_return_cluster_to_free_space(NULL, last_ptr);
7647
Miao Xiee570fd22014-06-19 10:42:50 +08007648 if (used_block_group != block_group)
7649 btrfs_release_block_group(used_block_group,
7650 delalloc);
7651refill_cluster:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007652 if (loop >= LOOP_NO_EMPTY_SIZE) {
7653 spin_unlock(&last_ptr->refill_lock);
7654 goto unclustered_alloc;
7655 }
7656
Chris Mason8de972b2013-01-04 15:39:43 -05007657 aligned_cluster = max_t(unsigned long,
7658 empty_cluster + empty_size,
7659 block_group->full_stripe_len);
7660
Chris Masonfa9c0d792009-04-03 09:47:43 -04007661 /* allocate a cluster in this block group */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007662 ret = btrfs_find_space_cluster(fs_info, block_group,
Josef Bacik00361582013-08-14 14:02:47 -04007663 last_ptr, search_start,
7664 num_bytes,
7665 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007666 if (ret == 0) {
7667 /*
7668 * now pull our allocation out of this
7669 * cluster
7670 */
7671 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007672 last_ptr,
7673 num_bytes,
7674 search_start,
7675 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007676 if (offset) {
7677 /* we found one, proceed */
7678 spin_unlock(&last_ptr->refill_lock);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007679 trace_btrfs_reserve_extent_cluster(fs_info,
Josef Bacik3f7de032011-11-10 08:29:20 -05007680 block_group, search_start,
7681 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007682 goto checks;
7683 }
Josef Bacik0a243252009-09-11 16:11:20 -04007684 } else if (!cached && loop > LOOP_CACHING_NOWAIT
7685 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007686 spin_unlock(&last_ptr->refill_lock);
7687
Josef Bacik0a243252009-09-11 16:11:20 -04007688 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007689 wait_block_group_cache_progress(block_group,
7690 num_bytes + empty_cluster + empty_size);
7691 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007692 }
Josef Bacik817d52f2009-07-13 21:29:25 -04007693
Chris Masonfa9c0d792009-04-03 09:47:43 -04007694 /*
7695 * at this point we either didn't find a cluster
7696 * or we weren't able to allocate a block from our
7697 * cluster. Free the cluster we've been trying
7698 * to use, and go to the next block group
7699 */
Josef Bacik0a243252009-09-11 16:11:20 -04007700 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007701 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04007702 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007703 }
7704
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007705unclustered_alloc:
Josef Bacikc759c4e2015-10-02 15:25:10 -04007706 /*
7707 * We are doing an unclustered alloc, set the fragmented flag so
7708 * we don't bother trying to setup a cluster again until we get
7709 * more space.
7710 */
7711 if (unlikely(last_ptr)) {
7712 spin_lock(&last_ptr->lock);
7713 last_ptr->fragmented = 1;
7714 spin_unlock(&last_ptr->lock);
7715 }
Liu Bo0c9b36e2017-02-13 15:42:30 -08007716 if (cached) {
7717 struct btrfs_free_space_ctl *ctl =
7718 block_group->free_space_ctl;
7719
7720 spin_lock(&ctl->tree_lock);
7721 if (ctl->free_space <
7722 num_bytes + empty_cluster + empty_size) {
7723 if (ctl->free_space > max_extent_size)
7724 max_extent_size = ctl->free_space;
7725 spin_unlock(&ctl->tree_lock);
7726 goto loop;
7727 }
7728 spin_unlock(&ctl->tree_lock);
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007729 }
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007730
Josef Bacik6226cb02009-04-03 10:14:18 -04007731 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08007732 num_bytes, empty_size,
7733 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007734 /*
7735 * If we didn't find a chunk, and we haven't failed on this
7736 * block group before, and this block group is in the middle of
7737 * caching and we are ok with waiting, then go ahead and wait
7738 * for progress to be made, and set failed_alloc to true.
7739 *
7740 * If failed_alloc is true then we've already waited on this
7741 * block group once and should move on to the next block group.
7742 */
7743 if (!offset && !failed_alloc && !cached &&
7744 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007745 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007746 num_bytes + empty_size);
7747 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007748 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007749 } else if (!offset) {
7750 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04007751 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007752checks:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007753 search_start = ALIGN(offset, fs_info->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04007754
Josef Bacik2552d172009-04-03 10:14:19 -04007755 /* move on to the next group */
7756 if (search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08007757 block_group->key.objectid + block_group->key.offset) {
7758 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04007759 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04007760 }
Josef Bacik80eb2342008-10-29 14:49:05 -04007761
Josef Bacik6226cb02009-04-03 10:14:18 -04007762 if (offset < search_start)
Miao Xie215a63d2014-01-15 20:00:56 +08007763 btrfs_add_free_space(block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04007764 search_start - offset);
7765 BUG_ON(offset > search_start);
7766
Wang Xiaoguang18513092016-07-25 15:51:40 +08007767 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
7768 num_bytes, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007769 if (ret == -EAGAIN) {
Miao Xie215a63d2014-01-15 20:00:56 +08007770 btrfs_add_free_space(block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007771 goto loop;
7772 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007773 btrfs_inc_block_group_reservations(block_group);
Yan Zheng11833d62009-09-11 16:11:19 -04007774
Josef Bacik2552d172009-04-03 10:14:19 -04007775 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007776 ins->objectid = search_start;
7777 ins->offset = num_bytes;
7778
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007779 trace_btrfs_reserve_extent(fs_info, block_group,
Josef Bacik3f7de032011-11-10 08:29:20 -05007780 search_start, num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08007781 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007782 break;
7783loop:
Josef Bacik0a243252009-09-11 16:11:20 -04007784 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007785 failed_alloc = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04007786 BUG_ON(index != get_block_group_index(block_group));
Miao Xiee570fd22014-06-19 10:42:50 +08007787 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007788 }
7789 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05007790
chandan13a0db52015-11-02 13:59:46 +05307791 if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg
7792 && !orig_have_caching_bg)
7793 orig_have_caching_bg = true;
7794
Miao Xie60d2adb2011-09-09 17:34:35 +08007795 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
7796 goto search;
7797
Yan, Zhengb742bb822010-05-16 10:46:24 -04007798 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
7799 goto search;
7800
Josef Bacik285ff5a2012-01-13 15:27:45 -05007801 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05007802 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
7803 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04007804 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
7805 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
7806 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
7807 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04007808 */
Josef Bacik723bda22011-05-27 16:11:38 -04007809 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb822010-05-16 10:46:24 -04007810 index = 0;
Josef Bacika5e681d2015-10-01 14:54:10 -04007811 if (loop == LOOP_CACHING_NOWAIT) {
7812 /*
7813 * We want to skip the LOOP_CACHING_WAIT step if we
Nicholas D Steeves01327612016-05-19 21:18:45 -04007814 * don't have any uncached bgs and we've already done a
Josef Bacika5e681d2015-10-01 14:54:10 -04007815 * full search through.
7816 */
chandan13a0db52015-11-02 13:59:46 +05307817 if (orig_have_caching_bg || !full_search)
Josef Bacika5e681d2015-10-01 14:54:10 -04007818 loop = LOOP_CACHING_WAIT;
7819 else
7820 loop = LOOP_ALLOC_CHUNK;
7821 } else {
7822 loop++;
7823 }
7824
Josef Bacik817d52f2009-07-13 21:29:25 -04007825 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04007826 struct btrfs_trans_handle *trans;
Wang Shilongf017f152014-03-13 13:19:47 +08007827 int exist = 0;
Josef Bacik00361582013-08-14 14:02:47 -04007828
Wang Shilongf017f152014-03-13 13:19:47 +08007829 trans = current->journal_info;
7830 if (trans)
7831 exist = 1;
7832 else
7833 trans = btrfs_join_transaction(root);
7834
Josef Bacik00361582013-08-14 14:02:47 -04007835 if (IS_ERR(trans)) {
7836 ret = PTR_ERR(trans);
7837 goto out;
7838 }
7839
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007840 ret = do_chunk_alloc(trans, fs_info, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04007841 CHUNK_ALLOC_FORCE);
Josef Bacika5e681d2015-10-01 14:54:10 -04007842
7843 /*
7844 * If we can't allocate a new chunk we've already looped
7845 * through at least once, move on to the NO_EMPTY_SIZE
7846 * case.
7847 */
7848 if (ret == -ENOSPC)
7849 loop = LOOP_NO_EMPTY_SIZE;
7850
Josef Bacikea658ba2012-09-11 16:57:25 -04007851 /*
7852 * Do not bail out on ENOSPC since we
7853 * can do more things.
7854 */
Josef Bacik00361582013-08-14 14:02:47 -04007855 if (ret < 0 && ret != -ENOSPC)
Jeff Mahoney66642832016-06-10 18:19:25 -04007856 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -04007857 else
7858 ret = 0;
Wang Shilongf017f152014-03-13 13:19:47 +08007859 if (!exist)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007860 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04007861 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04007862 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04007863 }
7864
7865 if (loop == LOOP_NO_EMPTY_SIZE) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007866 /*
7867 * Don't loop again if we already have no empty_size and
7868 * no empty_cluster.
7869 */
7870 if (empty_size == 0 &&
7871 empty_cluster == 0) {
7872 ret = -ENOSPC;
7873 goto out;
7874 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007875 empty_size = 0;
7876 empty_cluster = 0;
7877 }
Chris Mason42e70e72008-11-07 18:17:11 -05007878
Josef Bacik723bda22011-05-27 16:11:38 -04007879 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04007880 } else if (!ins->objectid) {
7881 ret = -ENOSPC;
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007882 } else if (ins->objectid) {
Josef Bacikc759c4e2015-10-02 15:25:10 -04007883 if (!use_cluster && last_ptr) {
7884 spin_lock(&last_ptr->lock);
7885 last_ptr->window_start = ins->objectid;
7886 spin_unlock(&last_ptr->lock);
7887 }
Josef Bacik2552d172009-04-03 10:14:19 -04007888 ret = 0;
7889 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007890out:
Josef Bacik4f4db212015-09-29 11:40:47 -04007891 if (ret == -ENOSPC) {
7892 spin_lock(&space_info->lock);
7893 space_info->max_extent_size = max_extent_size;
7894 spin_unlock(&space_info->lock);
Miao Xiea4820392013-09-09 13:19:42 +08007895 ins->offset = max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04007896 }
Chris Mason0f70abe2007-02-28 16:46:22 -05007897 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05007898}
Chris Masonec44a352008-04-28 15:29:52 -04007899
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007900static void dump_space_info(struct btrfs_fs_info *fs_info,
7901 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007902 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04007903{
7904 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb822010-05-16 10:46:24 -04007905 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007906
Josef Bacik9ed74f22009-09-11 16:12:44 -04007907 spin_lock(&info->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007908 btrfs_info(fs_info, "space_info %llu has %llu free, is %sfull",
7909 info->flags,
Liu Bo41361352017-02-13 15:42:21 -08007910 info->total_bytes - btrfs_space_info_used(info, true),
7911 info->full ? "" : "not ");
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007912 btrfs_info(fs_info,
7913 "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu",
7914 info->total_bytes, info->bytes_used, info->bytes_pinned,
7915 info->bytes_reserved, info->bytes_may_use,
7916 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007917 spin_unlock(&info->lock);
7918
7919 if (!dump_block_groups)
7920 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007921
Josef Bacik80eb2342008-10-29 14:49:05 -04007922 down_read(&info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007923again:
7924 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04007925 spin_lock(&cache->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007926 btrfs_info(fs_info,
7927 "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s",
7928 cache->key.objectid, cache->key.offset,
7929 btrfs_block_group_used(&cache->item), cache->pinned,
7930 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04007931 btrfs_dump_free_space(cache, bytes);
7932 spin_unlock(&cache->lock);
7933 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04007934 if (++index < BTRFS_NR_RAID_TYPES)
7935 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04007936 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007937}
Zheng Yane8569812008-09-26 10:05:48 -04007938
Wang Xiaoguang18513092016-07-25 15:51:40 +08007939int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04007940 u64 num_bytes, u64 min_alloc_size,
7941 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08007942 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007943{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007944 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04007945 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00007946 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05007947 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04007948
David Sterbab6919a52013-04-29 13:39:40 +00007949 flags = btrfs_get_alloc_profile(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04007950again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007951 WARN_ON(num_bytes < fs_info->sectorsize);
Jeff Mahoney87bde3c2017-02-15 16:28:27 -05007952 ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size,
Wang Xiaoguang18513092016-07-25 15:51:40 +08007953 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007954 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007955 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007956 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08007957 if (!final_tried && ins->offset) {
7958 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007959 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007960 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05007961 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08007962 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05007963 if (num_bytes == min_alloc_size)
7964 final_tried = true;
7965 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007966 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05007967 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007968
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007969 sinfo = __find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007970 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007971 "allocation failed flags %llu, wanted %llu",
7972 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01007973 if (sinfo)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007974 dump_space_info(fs_info, sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05007975 }
Chris Mason925baed2008-06-25 16:01:30 -04007976 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007977
7978 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007979}
7980
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007981static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08007982 u64 start, u64 len,
7983 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04007984{
Josef Bacik0f9dd462008-09-23 13:14:11 -04007985 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05007986 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007987
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007988 cache = btrfs_lookup_block_group(fs_info, start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007989 if (!cache) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007990 btrfs_err(fs_info, "Unable to find block group for %llu",
7991 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007992 return -ENOSPC;
7993 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05007994
Chris Masone688b7252011-10-31 20:52:39 -04007995 if (pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007996 pin_down_extent(fs_info, cache, start, len, 1);
Chris Masone688b7252011-10-31 20:52:39 -04007997 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007998 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007999 ret = btrfs_discard_extent(fs_info, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04008000 btrfs_add_free_space(cache, start, len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08008001 btrfs_free_reserved_bytes(cache, len, delalloc);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008002 trace_btrfs_reserved_extent_free(fs_info, start, len);
Chris Masone688b7252011-10-31 20:52:39 -04008003 }
Dongsheng Yang31193212014-12-12 16:44:35 +08008004
Chris Masonfa9c0d792009-04-03 09:47:43 -04008005 btrfs_put_block_group(cache);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008006 return ret;
8007}
8008
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008009int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008010 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04008011{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008012 return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04008013}
8014
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008015int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04008016 u64 start, u64 len)
8017{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008018 return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04008019}
8020
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008021static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008022 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008023 u64 parent, u64 root_objectid,
8024 u64 flags, u64 owner, u64 offset,
8025 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008026{
8027 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008028 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008029 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008030 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008031 struct extent_buffer *leaf;
8032 int type;
8033 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04008034
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008035 if (parent > 0)
8036 type = BTRFS_SHARED_DATA_REF_KEY;
8037 else
8038 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04008039
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008040 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05008041
8042 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00008043 if (!path)
8044 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05008045
Chris Masonb9473432009-03-13 11:00:37 -04008046 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008047 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8048 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008049 if (ret) {
8050 btrfs_free_path(path);
8051 return ret;
8052 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008053
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008054 leaf = path->nodes[0];
8055 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05008056 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008057 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
8058 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8059 btrfs_set_extent_flags(leaf, extent_item,
8060 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05008061
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008062 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
8063 btrfs_set_extent_inline_ref_type(leaf, iref, type);
8064 if (parent > 0) {
8065 struct btrfs_shared_data_ref *ref;
8066 ref = (struct btrfs_shared_data_ref *)(iref + 1);
8067 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8068 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
8069 } else {
8070 struct btrfs_extent_data_ref *ref;
8071 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
8072 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
8073 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
8074 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
8075 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
8076 }
Chris Mason47e4bb92008-02-01 14:51:59 -05008077
8078 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05008079 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04008080
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008081 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
8082 ins->offset);
8083 if (ret)
8084 return ret;
8085
Jeff Mahoney6202df62016-06-22 18:54:22 -04008086 ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008087 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008088 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008089 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05008090 BUG();
8091 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008092 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008093 return ret;
8094}
8095
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008096static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008097 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008098 u64 parent, u64 root_objectid,
8099 u64 flags, struct btrfs_disk_key *key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01008100 int level, struct btrfs_key *ins)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008101{
8102 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008103 struct btrfs_extent_item *extent_item;
8104 struct btrfs_tree_block_info *block_info;
8105 struct btrfs_extent_inline_ref *iref;
8106 struct btrfs_path *path;
8107 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05008108 u32 size = sizeof(*extent_item) + sizeof(*iref);
Josef Bacikfcebe452014-05-13 17:30:47 -07008109 u64 num_bytes = ins->offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008110 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05008111
8112 if (!skinny_metadata)
8113 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008114
8115 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04008116 if (!path) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008117 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008118 fs_info->nodesize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07008119 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04008120 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008121
8122 path->leave_spinning = 1;
8123 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8124 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008125 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07008126 btrfs_free_path(path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008127 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008128 fs_info->nodesize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008129 return ret;
8130 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008131
8132 leaf = path->nodes[0];
8133 extent_item = btrfs_item_ptr(leaf, path->slots[0],
8134 struct btrfs_extent_item);
8135 btrfs_set_extent_refs(leaf, extent_item, 1);
8136 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8137 btrfs_set_extent_flags(leaf, extent_item,
8138 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008139
Josef Bacik3173a182013-03-07 14:22:04 -05008140 if (skinny_metadata) {
8141 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008142 num_bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05008143 } else {
8144 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
8145 btrfs_set_tree_block_key(leaf, block_info, key);
8146 btrfs_set_tree_block_level(leaf, block_info, level);
8147 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
8148 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008149
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008150 if (parent > 0) {
8151 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
8152 btrfs_set_extent_inline_ref_type(leaf, iref,
8153 BTRFS_SHARED_BLOCK_REF_KEY);
8154 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8155 } else {
8156 btrfs_set_extent_inline_ref_type(leaf, iref,
8157 BTRFS_TREE_BLOCK_REF_KEY);
8158 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
8159 }
8160
8161 btrfs_mark_buffer_dirty(leaf);
8162 btrfs_free_path(path);
8163
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008164 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
8165 num_bytes);
8166 if (ret)
8167 return ret;
8168
Jeff Mahoney6202df62016-06-22 18:54:22 -04008169 ret = update_block_group(trans, fs_info, ins->objectid,
8170 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008171 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008172 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008173 ins->objectid, ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008174 BUG();
8175 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04008176
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008177 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008178 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008179 return ret;
8180}
8181
8182int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008183 u64 root_objectid, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08008184 u64 offset, u64 ram_bytes,
8185 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008186{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008187 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008188 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04008189
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008190 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04008191
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008192 ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008193 ins->offset, 0,
8194 root_objectid, owner, offset,
Jeff Mahoneyfef394f2016-12-13 14:39:34 -05008195 ram_bytes, BTRFS_ADD_DELAYED_EXTENT);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008196 return ret;
8197}
Chris Masone02119d2008-09-05 16:13:11 -04008198
8199/*
8200 * this is used by the tree logging recovery code. It records that
8201 * an extent has been allocated and makes sure to clear the free
8202 * space cache bits as well
8203 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008204int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008205 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008206 u64 root_objectid, u64 owner, u64 offset,
8207 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04008208{
8209 int ret;
8210 struct btrfs_block_group_cache *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008211 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008212
8213 /*
8214 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04008215 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008216 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008217 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008218 ret = __exclude_logged_extent(fs_info, ins->objectid,
8219 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008220 if (ret)
8221 return ret;
8222 }
Chris Masone02119d2008-09-05 16:13:11 -04008223
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008224 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008225 if (!block_group)
8226 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04008227
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008228 space_info = block_group->space_info;
8229 spin_lock(&space_info->lock);
8230 spin_lock(&block_group->lock);
8231 space_info->bytes_reserved += ins->offset;
8232 block_group->reserved += ins->offset;
8233 spin_unlock(&block_group->lock);
8234 spin_unlock(&space_info->lock);
8235
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008236 ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008237 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04008238 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04008239 return ret;
8240}
8241
Eric Sandeen48a3b632013-04-25 20:41:01 +00008242static struct extent_buffer *
8243btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
David Sterbafe864572014-06-15 02:28:42 +02008244 u64 bytenr, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04008245{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008246 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04008247 struct extent_buffer *buf;
8248
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008249 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008250 if (IS_ERR(buf))
8251 return buf;
8252
Chris Mason65b51a02008-08-01 15:11:20 -04008253 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04008254 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04008255 btrfs_tree_lock(buf);
David Sterba7c302b42017-02-10 18:47:57 +01008256 clean_tree_block(fs_info, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05008257 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008258
8259 btrfs_set_lock_blocking(buf);
David Sterba4db8c522015-12-03 13:06:46 +01008260 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008261
Chris Masond0c803c2008-09-11 16:17:57 -04008262 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01008263 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008264 /*
8265 * we allow two log transactions at a time, use different
8266 * EXENT bit to differentiate dirty pages.
8267 */
Filipe Manana656f30d2014-09-26 12:25:56 +01008268 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008269 set_extent_dirty(&root->dirty_log_pages, buf->start,
8270 buf->start + buf->len - 1, GFP_NOFS);
8271 else
8272 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02008273 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04008274 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01008275 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04008276 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
8277 buf->start + buf->len - 1, GFP_NOFS);
8278 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04008279 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05008280 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04008281 return buf;
8282}
8283
Yan, Zhengf0486c62010-05-16 10:46:25 -04008284static struct btrfs_block_rsv *
8285use_block_rsv(struct btrfs_trans_handle *trans,
8286 struct btrfs_root *root, u32 blocksize)
8287{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008288 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008289 struct btrfs_block_rsv *block_rsv;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008290 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008291 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00008292 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008293
8294 block_rsv = get_block_rsv(trans, root);
8295
Miao Xieb586b322013-05-13 13:55:10 +00008296 if (unlikely(block_rsv->size == 0))
8297 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00008298again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04008299 ret = block_rsv_use_bytes(block_rsv, blocksize);
8300 if (!ret)
8301 return block_rsv;
8302
Miao Xieb586b322013-05-13 13:55:10 +00008303 if (block_rsv->failfast)
8304 return ERR_PTR(ret);
8305
Miao Xied88033d2013-05-13 13:55:12 +00008306 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
8307 global_updated = true;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008308 update_global_block_rsv(fs_info);
Miao Xied88033d2013-05-13 13:55:12 +00008309 goto again;
8310 }
8311
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008312 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xieb586b322013-05-13 13:55:10 +00008313 static DEFINE_RATELIMIT_STATE(_rs,
8314 DEFAULT_RATELIMIT_INTERVAL * 10,
8315 /*DEFAULT_RATELIMIT_BURST*/ 1);
8316 if (__ratelimit(&_rs))
8317 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05008318 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00008319 }
8320try_reserve:
8321 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
8322 BTRFS_RESERVE_NO_FLUSH);
8323 if (!ret)
8324 return block_rsv;
8325 /*
8326 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00008327 * the global reserve if its space type is the same as the global
8328 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00008329 */
Miao Xie5881cfc2013-05-13 13:55:11 +00008330 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
8331 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00008332 ret = block_rsv_use_bytes(global_rsv, blocksize);
8333 if (!ret)
8334 return global_rsv;
8335 }
8336 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008337}
8338
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008339static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
8340 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008341{
8342 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008343 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008344}
8345
Chris Masonfec577f2007-02-26 10:40:21 -05008346/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04008347 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08008348 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05008349 */
David Sterba4d75f8a2014-06-15 01:54:12 +02008350struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
Omar Sandoval310712b2017-01-17 23:24:37 -08008351 struct btrfs_root *root,
8352 u64 parent, u64 root_objectid,
8353 const struct btrfs_disk_key *key,
8354 int level, u64 hint,
8355 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05008356{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008357 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04008358 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008359 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04008360 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008361 struct btrfs_delayed_extent_op *extent_op;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008362 u64 flags = 0;
8363 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008364 u32 blocksize = fs_info->nodesize;
8365 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008366
David Sterba05653ef2016-07-15 15:23:37 +02008367#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008368 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008369 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
David Sterbafe864572014-06-15 02:28:42 +02008370 level);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008371 if (!IS_ERR(buf))
8372 root->alloc_bytenr += blocksize;
8373 return buf;
8374 }
David Sterba05653ef2016-07-15 15:23:37 +02008375#endif
David Sterbafccb84c2014-09-29 23:53:21 +02008376
Yan, Zhengf0486c62010-05-16 10:46:25 -04008377 block_rsv = use_block_rsv(trans, root, blocksize);
8378 if (IS_ERR(block_rsv))
8379 return ERR_CAST(block_rsv);
8380
Wang Xiaoguang18513092016-07-25 15:51:40 +08008381 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08008382 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008383 if (ret)
8384 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05008385
David Sterbafe864572014-06-15 02:28:42 +02008386 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
Omar Sandoval67b78592015-02-24 02:47:04 -08008387 if (IS_ERR(buf)) {
8388 ret = PTR_ERR(buf);
8389 goto out_free_reserved;
8390 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008391
8392 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
8393 if (parent == 0)
8394 parent = ins.objectid;
8395 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8396 } else
8397 BUG_ON(parent > 0);
8398
8399 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00008400 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08008401 if (!extent_op) {
8402 ret = -ENOMEM;
8403 goto out_free_buf;
8404 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008405 if (key)
8406 memcpy(&extent_op->key, key, sizeof(extent_op->key));
8407 else
8408 memset(&extent_op->key, 0, sizeof(extent_op->key));
8409 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01008410 extent_op->update_key = skinny_metadata ? false : true;
8411 extent_op->update_flags = true;
8412 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04008413 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008414
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008415 ret = btrfs_add_delayed_tree_ref(fs_info, trans,
Omar Sandoval67b78592015-02-24 02:47:04 -08008416 ins.objectid, ins.offset,
8417 parent, root_objectid, level,
8418 BTRFS_ADD_DELAYED_EXTENT,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01008419 extent_op);
Omar Sandoval67b78592015-02-24 02:47:04 -08008420 if (ret)
8421 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008422 }
Chris Masonfec577f2007-02-26 10:40:21 -05008423 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008424
8425out_free_delayed:
8426 btrfs_free_delayed_extent_op(extent_op);
8427out_free_buf:
8428 free_extent_buffer(buf);
8429out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008430 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008431out_unuse:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008432 unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08008433 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05008434}
Chris Masona28ec192007-03-06 20:08:01 -05008435
Yan Zheng2c47e6052009-06-27 21:07:35 -04008436struct walk_control {
8437 u64 refs[BTRFS_MAX_LEVEL];
8438 u64 flags[BTRFS_MAX_LEVEL];
8439 struct btrfs_key update_progress;
8440 int stage;
8441 int level;
8442 int shared_level;
8443 int update_ref;
8444 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008445 int reada_slot;
8446 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008447 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008448};
8449
8450#define DROP_REFERENCE 1
8451#define UPDATE_BACKREF 2
8452
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008453static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8454 struct btrfs_root *root,
8455 struct walk_control *wc,
8456 struct btrfs_path *path)
8457{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008458 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008459 u64 bytenr;
8460 u64 generation;
8461 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008462 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008463 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008464 struct btrfs_key key;
8465 struct extent_buffer *eb;
8466 int ret;
8467 int slot;
8468 int nread = 0;
8469
8470 if (path->slots[wc->level] < wc->reada_slot) {
8471 wc->reada_count = wc->reada_count * 2 / 3;
8472 wc->reada_count = max(wc->reada_count, 2);
8473 } else {
8474 wc->reada_count = wc->reada_count * 3 / 2;
8475 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008476 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008477 }
8478
8479 eb = path->nodes[wc->level];
8480 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008481
8482 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
8483 if (nread >= wc->reada_count)
8484 break;
8485
8486 cond_resched();
8487 bytenr = btrfs_node_blockptr(eb, slot);
8488 generation = btrfs_node_ptr_generation(eb, slot);
8489
8490 if (slot == path->slots[wc->level])
8491 goto reada;
8492
8493 if (wc->stage == UPDATE_BACKREF &&
8494 generation <= root->root_key.offset)
8495 continue;
8496
Yan, Zheng94fcca92009-10-09 09:25:16 -04008497 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008498 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05008499 wc->level - 1, 1, &refs,
8500 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008501 /* We don't care about errors in readahead. */
8502 if (ret < 0)
8503 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008504 BUG_ON(refs == 0);
8505
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008506 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008507 if (refs == 1)
8508 goto reada;
8509
Yan, Zheng94fcca92009-10-09 09:25:16 -04008510 if (wc->level == 1 &&
8511 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8512 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008513 if (!wc->update_ref ||
8514 generation <= root->root_key.offset)
8515 continue;
8516 btrfs_node_key_to_cpu(eb, &key, slot);
8517 ret = btrfs_comp_cpu_keys(&key,
8518 &wc->update_progress);
8519 if (ret < 0)
8520 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008521 } else {
8522 if (wc->level == 1 &&
8523 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8524 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008525 }
8526reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008527 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008528 nread++;
8529 }
8530 wc->reada_slot = slot;
8531}
8532
Chris Mason9aca1d52007-03-13 11:09:37 -04008533/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008534 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008535 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04008536 * when wc->stage == UPDATE_BACKREF, this function updates
8537 * back refs for pointers in the block.
8538 *
8539 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04008540 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008541static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8542 struct btrfs_root *root,
8543 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008544 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04008545{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008546 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008547 int level = wc->level;
8548 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04008549 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8550 int ret;
8551
8552 if (wc->stage == UPDATE_BACKREF &&
8553 btrfs_header_owner(eb) != root->root_key.objectid)
8554 return 1;
8555
8556 /*
8557 * when reference count of tree block is 1, it won't increase
8558 * again. once full backref flag is set, we never clear it.
8559 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04008560 if (lookup_info &&
8561 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
8562 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008563 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008564 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008565 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008566 &wc->refs[level],
8567 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008568 BUG_ON(ret == -ENOMEM);
8569 if (ret)
8570 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008571 BUG_ON(wc->refs[level] == 0);
8572 }
8573
Yan Zheng2c47e6052009-06-27 21:07:35 -04008574 if (wc->stage == DROP_REFERENCE) {
8575 if (wc->refs[level] > 1)
8576 return 1;
8577
8578 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04008579 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008580 path->locks[level] = 0;
8581 }
8582 return 0;
8583 }
8584
8585 /* wc->stage == UPDATE_BACKREF */
8586 if (!(wc->flags[level] & flag)) {
8587 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07008588 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008589 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07008590 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008591 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008592 ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04008593 eb->len, flag,
8594 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008595 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008596 wc->flags[level] |= flag;
8597 }
8598
8599 /*
8600 * the block is shared by multiple trees, so it's not good to
8601 * keep the tree lock
8602 */
8603 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04008604 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008605 path->locks[level] = 0;
8606 }
8607 return 0;
8608}
8609
8610/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008611 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008612 *
8613 * when wc->stage == DROP_REFERENCE, this function checks
8614 * reference count of the block pointed to. if the block
8615 * is shared and we need update back refs for the subtree
8616 * rooted at the block, this function changes wc->stage to
8617 * UPDATE_BACKREF. if the block is shared and there is no
8618 * need to update back, this function drops the reference
8619 * to the block.
8620 *
8621 * NOTE: return value 1 means we should stop walking down.
8622 */
8623static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8624 struct btrfs_root *root,
8625 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008626 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008627{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008628 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008629 u64 bytenr;
8630 u64 generation;
8631 u64 parent;
8632 u32 blocksize;
8633 struct btrfs_key key;
8634 struct extent_buffer *next;
8635 int level = wc->level;
8636 int reada = 0;
8637 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07008638 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008639
8640 generation = btrfs_node_ptr_generation(path->nodes[level],
8641 path->slots[level]);
8642 /*
8643 * if the lower level block was created before the snapshot
8644 * was created, we know there is no need to update back refs
8645 * for the subtree
8646 */
8647 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04008648 generation <= root->root_key.offset) {
8649 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008650 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008651 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008652
8653 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008654 blocksize = fs_info->nodesize;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008655
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008656 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008657 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008658 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008659 if (IS_ERR(next))
8660 return PTR_ERR(next);
8661
Josef Bacikb2aaaa32013-07-05 17:05:38 -04008662 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
8663 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008664 reada = 1;
8665 }
8666 btrfs_tree_lock(next);
8667 btrfs_set_lock_blocking(next);
8668
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008669 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008670 &wc->refs[level - 1],
8671 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02008672 if (ret < 0)
8673 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008674
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008675 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008676 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02008677 ret = -EIO;
8678 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008679 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008680 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008681
Yan, Zheng94fcca92009-10-09 09:25:16 -04008682 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008683 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07008684 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008685 if (level == 1 &&
8686 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8687 goto skip;
8688
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008689 if (!wc->update_ref ||
8690 generation <= root->root_key.offset)
8691 goto skip;
8692
8693 btrfs_node_key_to_cpu(path->nodes[level], &key,
8694 path->slots[level]);
8695 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
8696 if (ret < 0)
8697 goto skip;
8698
8699 wc->stage = UPDATE_BACKREF;
8700 wc->shared_level = level - 1;
8701 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008702 } else {
8703 if (level == 1 &&
8704 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8705 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008706 }
8707
Chris Masonb9fab912012-05-06 07:23:47 -04008708 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008709 btrfs_tree_unlock(next);
8710 free_extent_buffer(next);
8711 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008712 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008713 }
8714
8715 if (!next) {
8716 if (reada && level == 1)
8717 reada_walk_down(trans, root, wc, path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008718 next = read_tree_block(fs_info, bytenr, generation);
Liu Bo64c043d2015-05-25 17:30:15 +08008719 if (IS_ERR(next)) {
8720 return PTR_ERR(next);
8721 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04008722 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008723 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04008724 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008725 btrfs_tree_lock(next);
8726 btrfs_set_lock_blocking(next);
8727 }
8728
8729 level--;
Josef Bacik48672682016-09-23 13:23:28 +02008730 ASSERT(level == btrfs_header_level(next));
8731 if (level != btrfs_header_level(next)) {
8732 btrfs_err(root->fs_info, "mismatched level");
8733 ret = -EIO;
8734 goto out_unlock;
8735 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008736 path->nodes[level] = next;
8737 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008738 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008739 wc->level = level;
8740 if (wc->level == 1)
8741 wc->reada_slot = 0;
8742 return 0;
8743skip:
8744 wc->refs[level - 1] = 0;
8745 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008746 if (wc->stage == DROP_REFERENCE) {
8747 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
8748 parent = path->nodes[level]->start;
8749 } else {
Josef Bacik48672682016-09-23 13:23:28 +02008750 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04008751 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02008752 if (root->root_key.objectid !=
8753 btrfs_header_owner(path->nodes[level])) {
8754 btrfs_err(root->fs_info,
8755 "mismatched block owner");
8756 ret = -EIO;
8757 goto out_unlock;
8758 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008759 parent = 0;
8760 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008761
Mark Fasheh11526512014-07-17 12:39:01 -07008762 if (need_account) {
Qu Wenruo33d1f052016-10-18 09:31:28 +08008763 ret = btrfs_qgroup_trace_subtree(trans, root, next,
8764 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07008765 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008766 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008767 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
8768 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008769 }
8770 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008771 ret = btrfs_free_extent(trans, fs_info, bytenr, blocksize,
8772 parent, root->root_key.objectid,
8773 level - 1, 0);
Josef Bacik48672682016-09-23 13:23:28 +02008774 if (ret)
8775 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008776 }
Josef Bacik48672682016-09-23 13:23:28 +02008777
8778 *lookup_info = 1;
8779 ret = 1;
8780
8781out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008782 btrfs_tree_unlock(next);
8783 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02008784
8785 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008786}
8787
8788/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008789 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008790 *
8791 * when wc->stage == DROP_REFERENCE, this function drops
8792 * reference count on the block.
8793 *
8794 * when wc->stage == UPDATE_BACKREF, this function changes
8795 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8796 * to UPDATE_BACKREF previously while processing the block.
8797 *
8798 * NOTE: return value 1 means we should stop walking up.
8799 */
8800static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8801 struct btrfs_root *root,
8802 struct btrfs_path *path,
8803 struct walk_control *wc)
8804{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008805 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008806 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008807 int level = wc->level;
8808 struct extent_buffer *eb = path->nodes[level];
8809 u64 parent = 0;
8810
8811 if (wc->stage == UPDATE_BACKREF) {
8812 BUG_ON(wc->shared_level < level);
8813 if (level < wc->shared_level)
8814 goto out;
8815
Yan Zheng2c47e6052009-06-27 21:07:35 -04008816 ret = find_next_key(path, level + 1, &wc->update_progress);
8817 if (ret > 0)
8818 wc->update_ref = 0;
8819
8820 wc->stage = DROP_REFERENCE;
8821 wc->shared_level = -1;
8822 path->slots[level] = 0;
8823
8824 /*
8825 * check reference count again if the block isn't locked.
8826 * we should start walking down the tree again if reference
8827 * count is one.
8828 */
8829 if (!path->locks[level]) {
8830 BUG_ON(level == 0);
8831 btrfs_tree_lock(eb);
8832 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008833 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008834
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008835 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008836 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008837 &wc->refs[level],
8838 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008839 if (ret < 0) {
8840 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008841 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008842 return ret;
8843 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008844 BUG_ON(wc->refs[level] == 0);
8845 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04008846 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008847 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008848 return 1;
8849 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008850 }
8851 }
8852
8853 /* wc->stage == DROP_REFERENCE */
8854 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
8855
8856 if (wc->refs[level] == 1) {
8857 if (level == 0) {
8858 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07008859 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008860 else
Josef Bacike339a6b2014-07-02 10:54:25 -07008861 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008862 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008863 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb);
Mark Fasheh11526512014-07-17 12:39:01 -07008864 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008865 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008866 "error %d accounting leaf items. Quota is out of sync, rescan required.",
8867 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008868 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008869 }
8870 /* make block locked assertion in clean_tree_block happy */
8871 if (!path->locks[level] &&
8872 btrfs_header_generation(eb) == trans->transid) {
8873 btrfs_tree_lock(eb);
8874 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008875 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008876 }
David Sterba7c302b42017-02-10 18:47:57 +01008877 clean_tree_block(fs_info, eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008878 }
8879
8880 if (eb == root->node) {
8881 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8882 parent = eb->start;
8883 else
8884 BUG_ON(root->root_key.objectid !=
8885 btrfs_header_owner(eb));
8886 } else {
8887 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8888 parent = path->nodes[level + 1]->start;
8889 else
8890 BUG_ON(root->root_key.objectid !=
8891 btrfs_header_owner(path->nodes[level + 1]));
8892 }
8893
Jan Schmidt5581a512012-05-16 17:04:52 +02008894 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008895out:
8896 wc->refs[level] = 0;
8897 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008898 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008899}
8900
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008901static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
8902 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008903 struct btrfs_path *path,
8904 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008905{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008906 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008907 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008908 int ret;
8909
Yan Zheng2c47e6052009-06-27 21:07:35 -04008910 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04008911 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008912 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008913 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008914
Yan Zheng2c47e6052009-06-27 21:07:35 -04008915 if (level == 0)
8916 break;
8917
Yan, Zheng7a7965f2010-02-01 02:41:17 +00008918 if (path->slots[level] >=
8919 btrfs_header_nritems(path->nodes[level]))
8920 break;
8921
Yan, Zheng94fcca92009-10-09 09:25:16 -04008922 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008923 if (ret > 0) {
8924 path->slots[level]++;
8925 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00008926 } else if (ret < 0)
8927 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008928 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008929 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04008930 return 0;
8931}
8932
Chris Masond3977122009-01-05 21:25:51 -05008933static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05008934 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04008935 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008936 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05008937{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008938 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05008939 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04008940
Yan Zheng2c47e6052009-06-27 21:07:35 -04008941 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
8942 while (level < max_level && path->nodes[level]) {
8943 wc->level = level;
8944 if (path->slots[level] + 1 <
8945 btrfs_header_nritems(path->nodes[level])) {
8946 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05008947 return 0;
8948 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008949 ret = walk_up_proc(trans, root, path, wc);
8950 if (ret > 0)
8951 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05008952
Yan Zheng2c47e6052009-06-27 21:07:35 -04008953 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04008954 btrfs_tree_unlock_rw(path->nodes[level],
8955 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008956 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008957 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008958 free_extent_buffer(path->nodes[level]);
8959 path->nodes[level] = NULL;
8960 level++;
Chris Mason20524f02007-03-10 06:35:47 -05008961 }
8962 }
8963 return 1;
8964}
8965
Chris Mason9aca1d52007-03-13 11:09:37 -04008966/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04008967 * drop a subvolume tree.
8968 *
8969 * this function traverses the tree freeing any blocks that only
8970 * referenced by the tree.
8971 *
8972 * when a shared tree block is found. this function decreases its
8973 * reference count by one. if update_ref is true, this function
8974 * also make sure backrefs for the shared block and all lower level
8975 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00008976 *
8977 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04008978 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04008979int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008980 struct btrfs_block_rsv *block_rsv, int update_ref,
8981 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05008982{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008983 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04008984 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008985 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008986 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04008987 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008988 struct walk_control *wc;
8989 struct btrfs_key key;
8990 int err = 0;
8991 int ret;
8992 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04008993 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05008994
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008995 btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07008996
Chris Mason5caf2a02007-04-02 11:20:42 -04008997 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00008998 if (!path) {
8999 err = -ENOMEM;
9000 goto out;
9001 }
Chris Mason20524f02007-03-10 06:35:47 -05009002
Yan Zheng2c47e6052009-06-27 21:07:35 -04009003 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07009004 if (!wc) {
9005 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009006 err = -ENOMEM;
9007 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07009008 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009009
Yan, Zhenga22285a2010-05-16 10:48:46 -04009010 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009011 if (IS_ERR(trans)) {
9012 err = PTR_ERR(trans);
9013 goto out_free;
9014 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00009015
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009016 if (block_rsv)
9017 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009018
Chris Mason9f3a7422007-08-07 15:52:19 -04009019 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009020 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009021 path->nodes[level] = btrfs_lock_root_node(root);
9022 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04009023 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009024 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009025 memset(&wc->update_progress, 0,
9026 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04009027 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04009028 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009029 memcpy(&wc->update_progress, &key,
9030 sizeof(wc->update_progress));
9031
Chris Mason6702ed42007-08-07 16:15:09 -04009032 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009033 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04009034 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009035 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
9036 path->lowest_level = 0;
9037 if (ret < 0) {
9038 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009039 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04009040 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009041 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009042
Chris Mason7d9eb122008-07-08 14:19:17 -04009043 /*
9044 * unlock our path, this is safe because only this
9045 * function is allowed to delete this snapshot
9046 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009047 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04009048
Yan Zheng2c47e6052009-06-27 21:07:35 -04009049 level = btrfs_header_level(root->node);
9050 while (1) {
9051 btrfs_tree_lock(path->nodes[level]);
9052 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009053 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009054
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009055 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009056 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05009057 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04009058 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009059 if (ret < 0) {
9060 err = ret;
9061 goto out_end_trans;
9062 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009063 BUG_ON(wc->refs[level] == 0);
9064
9065 if (level == root_item->drop_level)
9066 break;
9067
9068 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009069 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009070 WARN_ON(wc->refs[level] != 1);
9071 level--;
9072 }
9073 }
9074
9075 wc->level = level;
9076 wc->shared_level = -1;
9077 wc->stage = DROP_REFERENCE;
9078 wc->update_ref = update_ref;
9079 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009080 wc->for_reloc = for_reloc;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009081 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009082
9083 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00009084
Yan Zheng2c47e6052009-06-27 21:07:35 -04009085 ret = walk_down_tree(trans, root, path, wc);
9086 if (ret < 0) {
9087 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04009088 break;
9089 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009090
9091 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
9092 if (ret < 0) {
9093 err = ret;
9094 break;
9095 }
9096
9097 if (ret > 0) {
9098 BUG_ON(wc->stage != DROP_REFERENCE);
9099 break;
9100 }
9101
9102 if (wc->stage == DROP_REFERENCE) {
9103 level = wc->level;
9104 btrfs_node_key(path->nodes[level],
9105 &root_item->drop_progress,
9106 path->slots[level]);
9107 root_item->drop_level = level;
9108 }
9109
9110 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009111 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009112 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009113 ret = btrfs_update_root(trans, tree_root,
9114 &root->root_key,
9115 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009116 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009117 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009118 err = ret;
9119 goto out_end_trans;
9120 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009121
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009122 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009123 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009124 btrfs_debug(fs_info,
9125 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04009126 err = -EAGAIN;
9127 goto out_free;
9128 }
9129
Yan, Zhenga22285a2010-05-16 10:48:46 -04009130 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009131 if (IS_ERR(trans)) {
9132 err = PTR_ERR(trans);
9133 goto out_free;
9134 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009135 if (block_rsv)
9136 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04009137 }
Chris Mason20524f02007-03-10 06:35:47 -05009138 }
David Sterbab3b4aa72011-04-21 01:20:15 +02009139 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009140 if (err)
9141 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009142
9143 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009144 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009145 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009146 goto out_end_trans;
9147 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009148
Yan, Zheng76dda932009-09-21 16:00:26 -04009149 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00009150 ret = btrfs_find_root(tree_root, &root->root_key, path,
9151 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009152 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009153 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009154 err = ret;
9155 goto out_end_trans;
9156 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05009157 /* if we fail to delete the orphan item this time
9158 * around, it'll get picked up the next time.
9159 *
9160 * The most common failure here is just -ENOENT.
9161 */
9162 btrfs_del_orphan_item(trans, tree_root,
9163 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04009164 }
9165 }
9166
Miao Xie27cdeb72014-04-02 19:51:05 +08009167 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04009168 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009169 } else {
9170 free_extent_buffer(root->node);
9171 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00009172 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009173 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04009174 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009175out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009176 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009177out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04009178 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04009179 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009180out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04009181 /*
9182 * So if we need to stop dropping the snapshot for whatever reason we
9183 * need to make sure to add it back to the dead root list so that we
9184 * keep trying to do the work later. This also cleans up roots if we
9185 * don't have it in the radix (like when we recover after a power fail
9186 * or unmount) so we don't leak memory.
9187 */
Josef Bacikb37b39c2013-07-23 16:57:15 -04009188 if (!for_reloc && root_dropped == false)
Josef Bacikd29a9f62013-07-17 19:30:20 -04009189 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08009190 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009191 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04009192 return err;
Chris Mason20524f02007-03-10 06:35:47 -05009193}
Chris Mason9078a3e2007-04-26 16:46:15 -04009194
Yan Zheng2c47e6052009-06-27 21:07:35 -04009195/*
9196 * drop subtree rooted at tree block 'node'.
9197 *
9198 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009199 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04009200 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04009201int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
9202 struct btrfs_root *root,
9203 struct extent_buffer *node,
9204 struct extent_buffer *parent)
9205{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009206 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009207 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009208 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009209 int level;
9210 int parent_level;
9211 int ret = 0;
9212 int wret;
9213
Yan Zheng2c47e6052009-06-27 21:07:35 -04009214 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
9215
Yan Zhengf82d02d2008-10-29 14:49:05 -04009216 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009217 if (!path)
9218 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009219
Yan Zheng2c47e6052009-06-27 21:07:35 -04009220 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009221 if (!wc) {
9222 btrfs_free_path(path);
9223 return -ENOMEM;
9224 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009225
Chris Masonb9447ef82009-03-09 11:45:38 -04009226 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009227 parent_level = btrfs_header_level(parent);
9228 extent_buffer_get(parent);
9229 path->nodes[parent_level] = parent;
9230 path->slots[parent_level] = btrfs_header_nritems(parent);
9231
Chris Masonb9447ef82009-03-09 11:45:38 -04009232 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009233 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009234 path->nodes[level] = node;
9235 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009236 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009237
9238 wc->refs[parent_level] = 1;
9239 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
9240 wc->level = level;
9241 wc->shared_level = -1;
9242 wc->stage = DROP_REFERENCE;
9243 wc->update_ref = 0;
9244 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009245 wc->for_reloc = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009246 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009247
9248 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009249 wret = walk_down_tree(trans, root, path, wc);
9250 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04009251 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009252 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009253 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009254
Yan Zheng2c47e6052009-06-27 21:07:35 -04009255 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009256 if (wret < 0)
9257 ret = wret;
9258 if (wret != 0)
9259 break;
9260 }
9261
Yan Zheng2c47e6052009-06-27 21:07:35 -04009262 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009263 btrfs_free_path(path);
9264 return ret;
9265}
9266
Jeff Mahoney6202df62016-06-22 18:54:22 -04009267static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04009268{
9269 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009270 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04009271
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009272 /*
9273 * if restripe for this chunk_type is on pick target profile and
9274 * return, otherwise do the usual balance
9275 */
Jeff Mahoney6202df62016-06-22 18:54:22 -04009276 stripped = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009277 if (stripped)
9278 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02009279
Jeff Mahoney6202df62016-06-22 18:54:22 -04009280 num_devices = fs_info->fs_devices->rw_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05009281
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009282 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05009283 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009284 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
9285
Chris Masonec44a352008-04-28 15:29:52 -04009286 if (num_devices == 1) {
9287 stripped |= BTRFS_BLOCK_GROUP_DUP;
9288 stripped = flags & ~stripped;
9289
9290 /* turn raid0 into single device chunks */
9291 if (flags & BTRFS_BLOCK_GROUP_RAID0)
9292 return stripped;
9293
9294 /* turn mirroring into duplication */
9295 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
9296 BTRFS_BLOCK_GROUP_RAID10))
9297 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04009298 } else {
9299 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04009300 if (flags & stripped)
9301 return flags;
9302
9303 stripped |= BTRFS_BLOCK_GROUP_DUP;
9304 stripped = flags & ~stripped;
9305
9306 /* switch duplicated blocks with raid1 */
9307 if (flags & BTRFS_BLOCK_GROUP_DUP)
9308 return stripped | BTRFS_BLOCK_GROUP_RAID1;
9309
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009310 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04009311 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009312
Chris Masonec44a352008-04-28 15:29:52 -04009313 return flags;
9314}
9315
Zhaolei868f4012015-08-05 16:43:27 +08009316static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04009317{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009318 struct btrfs_space_info *sinfo = cache->space_info;
9319 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00009320 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009321 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04009322
Miao Xie199c36e2011-07-15 10:34:36 +00009323 /*
9324 * We need some metadata space and system metadata space for
9325 * allocating chunks in some corner cases until we force to set
9326 * it to be readonly.
9327 */
9328 if ((sinfo->flags &
9329 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
9330 !force)
Byongho Leeee221842015-12-15 01:42:10 +09009331 min_allocable_bytes = SZ_1M;
Miao Xie199c36e2011-07-15 10:34:36 +00009332 else
9333 min_allocable_bytes = 0;
9334
Yan, Zhengf0486c62010-05-16 10:46:25 -04009335 spin_lock(&sinfo->lock);
9336 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00009337
9338 if (cache->ro) {
Zhaolei868f4012015-08-05 16:43:27 +08009339 cache->ro++;
WuBo61cfea92011-07-26 03:30:11 +00009340 ret = 0;
9341 goto out;
9342 }
9343
Yan, Zhengf0486c62010-05-16 10:46:25 -04009344 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
9345 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04009346
Liu Bo41361352017-02-13 15:42:21 -08009347 if (btrfs_space_info_used(sinfo, true) + num_bytes +
Josef Bacik37be25b2011-08-05 10:25:38 -04009348 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04009349 sinfo->bytes_readonly += num_bytes;
Zhaolei868f4012015-08-05 16:43:27 +08009350 cache->ro++;
Josef Bacik633c0aa2014-10-31 09:49:34 -04009351 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009352 ret = 0;
9353 }
WuBo61cfea92011-07-26 03:30:11 +00009354out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04009355 spin_unlock(&cache->lock);
9356 spin_unlock(&sinfo->lock);
9357 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04009358}
9359
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009360int btrfs_inc_block_group_ro(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04009361 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009362
9363{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009364 struct btrfs_trans_handle *trans;
9365 u64 alloc_flags;
9366 int ret;
9367
Chris Mason1bbc6212015-04-06 12:46:08 -07009368again:
Jeff Mahoney5e00f192017-02-15 16:28:29 -05009369 trans = btrfs_join_transaction(fs_info->extent_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009370 if (IS_ERR(trans))
9371 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009372
Chris Mason1bbc6212015-04-06 12:46:08 -07009373 /*
9374 * we're not allowed to set block groups readonly after the dirty
9375 * block groups cache has started writing. If it already started,
9376 * back off and let this transaction commit
9377 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009378 mutex_lock(&fs_info->ro_block_group_mutex);
Josef Bacik3204d332015-09-24 10:46:10 -04009379 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
Chris Mason1bbc6212015-04-06 12:46:08 -07009380 u64 transid = trans->transid;
9381
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009382 mutex_unlock(&fs_info->ro_block_group_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009383 btrfs_end_transaction(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07009384
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009385 ret = btrfs_wait_for_commit(fs_info, transid);
Chris Mason1bbc6212015-04-06 12:46:08 -07009386 if (ret)
9387 return ret;
9388 goto again;
9389 }
9390
Chris Mason153c35b2015-05-19 18:54:41 -07009391 /*
9392 * if we are changing raid levels, try to allocate a corresponding
9393 * block group with the new raid level.
9394 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009395 alloc_flags = update_block_group_flags(fs_info, cache->flags);
Chris Mason153c35b2015-05-19 18:54:41 -07009396 if (alloc_flags != cache->flags) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009397 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason153c35b2015-05-19 18:54:41 -07009398 CHUNK_ALLOC_FORCE);
9399 /*
9400 * ENOSPC is allowed here, we may have enough space
9401 * already allocated at the new raid level to
9402 * carry on
9403 */
9404 if (ret == -ENOSPC)
9405 ret = 0;
9406 if (ret < 0)
9407 goto out;
9408 }
Chris Mason1bbc6212015-04-06 12:46:08 -07009409
Zhaolei868f4012015-08-05 16:43:27 +08009410 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009411 if (!ret)
9412 goto out;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009413 alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags);
9414 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04009415 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009416 if (ret < 0)
9417 goto out;
Zhaolei868f4012015-08-05 16:43:27 +08009418 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009419out:
Shaohua Li2f081082015-01-09 10:40:15 -08009420 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009421 alloc_flags = update_block_group_flags(fs_info, cache->flags);
David Sterba34441362016-10-04 19:34:27 +02009422 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009423 check_system_chunk(trans, fs_info, alloc_flags);
David Sterba34441362016-10-04 19:34:27 +02009424 mutex_unlock(&fs_info->chunk_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009425 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009426 mutex_unlock(&fs_info->ro_block_group_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009427
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009428 btrfs_end_transaction(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009429 return ret;
9430}
9431
Chris Masonc87f08c2011-02-16 13:57:04 -05009432int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009433 struct btrfs_fs_info *fs_info, u64 type)
Chris Masonc87f08c2011-02-16 13:57:04 -05009434{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009435 u64 alloc_flags = get_alloc_profile(fs_info, type);
9436
9437 return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05009438}
9439
Miao Xie6d07bce2011-01-05 10:07:31 +00009440/*
9441 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04009442 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00009443 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009444u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00009445{
9446 struct btrfs_block_group_cache *block_group;
9447 u64 free_bytes = 0;
9448 int factor;
9449
Nicholas D Steeves01327612016-05-19 21:18:45 -04009450 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009451 if (list_empty(&sinfo->ro_bgs))
9452 return 0;
9453
9454 spin_lock(&sinfo->lock);
9455 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00009456 spin_lock(&block_group->lock);
9457
9458 if (!block_group->ro) {
9459 spin_unlock(&block_group->lock);
9460 continue;
9461 }
9462
9463 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
9464 BTRFS_BLOCK_GROUP_RAID10 |
9465 BTRFS_BLOCK_GROUP_DUP))
9466 factor = 2;
9467 else
9468 factor = 1;
9469
9470 free_bytes += (block_group->key.offset -
9471 btrfs_block_group_used(&block_group->item)) *
9472 factor;
9473
9474 spin_unlock(&block_group->lock);
9475 }
Miao Xie6d07bce2011-01-05 10:07:31 +00009476 spin_unlock(&sinfo->lock);
9477
9478 return free_bytes;
9479}
9480
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009481void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache)
Yan, Zhengf0486c62010-05-16 10:46:25 -04009482{
9483 struct btrfs_space_info *sinfo = cache->space_info;
9484 u64 num_bytes;
9485
9486 BUG_ON(!cache->ro);
9487
9488 spin_lock(&sinfo->lock);
9489 spin_lock(&cache->lock);
Zhaolei868f4012015-08-05 16:43:27 +08009490 if (!--cache->ro) {
9491 num_bytes = cache->key.offset - cache->reserved -
9492 cache->pinned - cache->bytes_super -
9493 btrfs_block_group_used(&cache->item);
9494 sinfo->bytes_readonly -= num_bytes;
9495 list_del_init(&cache->ro_list);
9496 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009497 spin_unlock(&cache->lock);
9498 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009499}
9500
Josef Bacikba1bf482009-09-11 16:11:19 -04009501/*
9502 * checks to see if its even possible to relocate this block group.
9503 *
9504 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9505 * ok to go ahead and try.
9506 */
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009507int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04009508{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009509 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04009510 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04009511 struct btrfs_space_info *space_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009512 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacikba1bf482009-09-11 16:11:19 -04009513 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04009514 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00009515 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04009516 u64 dev_min = 1;
9517 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009518 u64 target;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009519 int debug;
liubocdcb7252011-08-03 10:15:25 +00009520 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04009521 int full = 0;
9522 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05009523
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009524 debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG);
Qu Wenruo0305bc22016-03-23 11:38:17 +08009525
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009526 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04009527
Josef Bacikba1bf482009-09-11 16:11:19 -04009528 /* odd, couldn't find the block group, leave it alone */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009529 if (!block_group) {
9530 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009531 btrfs_warn(fs_info,
Qu Wenruo0305bc22016-03-23 11:38:17 +08009532 "can't find block group for bytenr %llu",
9533 bytenr);
Josef Bacikba1bf482009-09-11 16:11:19 -04009534 return -1;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009535 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009536
liubocdcb7252011-08-03 10:15:25 +00009537 min_free = btrfs_block_group_used(&block_group->item);
9538
Josef Bacikba1bf482009-09-11 16:11:19 -04009539 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00009540 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04009541 goto out;
Chris Mason323da792008-05-09 11:46:48 -04009542
Josef Bacikba1bf482009-09-11 16:11:19 -04009543 space_info = block_group->space_info;
9544 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04009545
Josef Bacikba1bf482009-09-11 16:11:19 -04009546 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04009547
Josef Bacikba1bf482009-09-11 16:11:19 -04009548 /*
9549 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04009550 * relocate it unless we're able to allocate a new chunk below.
9551 *
9552 * Otherwise, we need to make sure we have room in the space to handle
9553 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04009554 */
Chris Mason7ce618d2009-09-22 14:48:44 -04009555 if ((space_info->total_bytes != block_group->key.offset) &&
Liu Bo41361352017-02-13 15:42:21 -08009556 (btrfs_space_info_used(space_info, false) + min_free <
9557 space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04009558 spin_unlock(&space_info->lock);
9559 goto out;
9560 }
9561 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009562
Josef Bacikba1bf482009-09-11 16:11:19 -04009563 /*
9564 * ok we don't have enough space, but maybe we have free space on our
9565 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009566 * alloc devices and guess if we have enough space. if this block
9567 * group is going to be restriped, run checks against the target
9568 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04009569 */
9570 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05009571
liubocdcb7252011-08-03 10:15:25 +00009572 /*
9573 * index:
9574 * 0: raid10
9575 * 1: raid1
9576 * 2: dup
9577 * 3: raid0
9578 * 4: single
9579 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009580 target = get_restripe_target(fs_info, block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009581 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00009582 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009583 } else {
9584 /*
9585 * this is just a balance, so if we were marked as full
9586 * we know there is no space for a new chunk
9587 */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009588 if (full) {
9589 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009590 btrfs_warn(fs_info,
9591 "no space to alloc new chunk for block group %llu",
9592 block_group->key.objectid);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009593 goto out;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009594 }
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009595
9596 index = get_block_group_index(block_group);
9597 }
9598
Miao Xiee6ec7162013-01-17 05:38:51 +00009599 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00009600 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04009601 /* Divide by 2 */
9602 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009603 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00009604 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00009605 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04009606 /* Multiply by 2 */
9607 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009608 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00009609 dev_min = fs_devices->rw_devices;
David Sterba47c57132015-02-20 18:43:47 +01009610 min_free = div64_u64(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00009611 }
9612
Josef Bacik6df9a952013-06-27 13:22:46 -04009613 /* We need to do this so that we can look at pending chunks */
9614 trans = btrfs_join_transaction(root);
9615 if (IS_ERR(trans)) {
9616 ret = PTR_ERR(trans);
9617 goto out;
9618 }
9619
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009620 mutex_lock(&fs_info->chunk_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04009621 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00009622 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04009623
Josef Bacikba1bf482009-09-11 16:11:19 -04009624 /*
9625 * check to make sure we can actually find a chunk with enough
9626 * space to fit our block group in.
9627 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01009628 if (device->total_bytes > device->bytes_used + min_free &&
9629 !device->is_tgtdev_for_dev_replace) {
Josef Bacik6df9a952013-06-27 13:22:46 -04009630 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00009631 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04009632 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00009633 dev_nr++;
9634
9635 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05009636 break;
liubocdcb7252011-08-03 10:15:25 +00009637
Josef Bacikba1bf482009-09-11 16:11:19 -04009638 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05009639 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009640 }
Qu Wenruo0305bc22016-03-23 11:38:17 +08009641 if (debug && ret == -1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009642 btrfs_warn(fs_info,
9643 "no space to allocate a new chunk for block group %llu",
9644 block_group->key.objectid);
9645 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009646 btrfs_end_transaction(trans);
Chris Masonedbd8d42007-12-21 16:27:24 -05009647out:
Josef Bacikba1bf482009-09-11 16:11:19 -04009648 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05009649 return ret;
9650}
9651
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009652static int find_first_block_group(struct btrfs_fs_info *fs_info,
9653 struct btrfs_path *path,
9654 struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04009655{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009656 struct btrfs_root *root = fs_info->extent_root;
Chris Mason925baed2008-06-25 16:01:30 -04009657 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009658 struct btrfs_key found_key;
9659 struct extent_buffer *leaf;
9660 int slot;
9661
9662 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
9663 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009664 goto out;
9665
Chris Masond3977122009-01-05 21:25:51 -05009666 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009667 slot = path->slots[0];
9668 leaf = path->nodes[0];
9669 if (slot >= btrfs_header_nritems(leaf)) {
9670 ret = btrfs_next_leaf(root, path);
9671 if (ret == 0)
9672 continue;
9673 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009674 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04009675 break;
9676 }
9677 btrfs_item_key_to_cpu(leaf, &found_key, slot);
9678
9679 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04009680 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
Liu Bo6fb37b72016-06-22 18:31:27 -07009681 struct extent_map_tree *em_tree;
9682 struct extent_map *em;
9683
9684 em_tree = &root->fs_info->mapping_tree.map_tree;
9685 read_lock(&em_tree->lock);
9686 em = lookup_extent_mapping(em_tree, found_key.objectid,
9687 found_key.offset);
9688 read_unlock(&em_tree->lock);
9689 if (!em) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009690 btrfs_err(fs_info,
Liu Bo6fb37b72016-06-22 18:31:27 -07009691 "logical %llu len %llu found bg but no related chunk",
9692 found_key.objectid, found_key.offset);
9693 ret = -ENOENT;
9694 } else {
9695 ret = 0;
9696 }
Josef Bacik187ee582016-08-18 15:30:06 -04009697 free_extent_map(em);
Chris Mason925baed2008-06-25 16:01:30 -04009698 goto out;
9699 }
Chris Mason0b86a832008-03-24 15:01:56 -04009700 path->slots[0]++;
9701 }
Chris Mason925baed2008-06-25 16:01:30 -04009702out:
Chris Mason0b86a832008-03-24 15:01:56 -04009703 return ret;
9704}
9705
Josef Bacik0af3d002010-06-21 14:48:16 -04009706void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9707{
9708 struct btrfs_block_group_cache *block_group;
9709 u64 last = 0;
9710
9711 while (1) {
9712 struct inode *inode;
9713
9714 block_group = btrfs_lookup_first_block_group(info, last);
9715 while (block_group) {
9716 spin_lock(&block_group->lock);
9717 if (block_group->iref)
9718 break;
9719 spin_unlock(&block_group->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009720 block_group = next_block_group(info, block_group);
Josef Bacik0af3d002010-06-21 14:48:16 -04009721 }
9722 if (!block_group) {
9723 if (last == 0)
9724 break;
9725 last = 0;
9726 continue;
9727 }
9728
9729 inode = block_group->inode;
9730 block_group->iref = 0;
9731 block_group->inode = NULL;
9732 spin_unlock(&block_group->lock);
Liu Bof3bca802016-07-20 17:33:44 -07009733 ASSERT(block_group->io_ctl.inode == NULL);
Josef Bacik0af3d002010-06-21 14:48:16 -04009734 iput(inode);
9735 last = block_group->key.objectid + block_group->key.offset;
9736 btrfs_put_block_group(block_group);
9737 }
9738}
9739
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009740/*
9741 * Must be called only after stopping all workers, since we could have block
9742 * group caching kthreads running, and therefore they could race with us if we
9743 * freed the block groups before stopping them.
9744 */
Zheng Yan1a40e232008-09-26 10:09:34 -04009745int btrfs_free_block_groups(struct btrfs_fs_info *info)
9746{
9747 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04009748 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04009749 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04009750 struct rb_node *n;
9751
Josef Bacik9e351cc2014-03-13 15:42:13 -04009752 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009753 while (!list_empty(&info->caching_block_groups)) {
9754 caching_ctl = list_entry(info->caching_block_groups.next,
9755 struct btrfs_caching_control, list);
9756 list_del(&caching_ctl->list);
9757 put_caching_control(caching_ctl);
9758 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04009759 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009760
Josef Bacik47ab2a62014-09-18 11:20:02 -04009761 spin_lock(&info->unused_bgs_lock);
9762 while (!list_empty(&info->unused_bgs)) {
9763 block_group = list_first_entry(&info->unused_bgs,
9764 struct btrfs_block_group_cache,
9765 bg_list);
9766 list_del_init(&block_group->bg_list);
9767 btrfs_put_block_group(block_group);
9768 }
9769 spin_unlock(&info->unused_bgs_lock);
9770
Zheng Yan1a40e232008-09-26 10:09:34 -04009771 spin_lock(&info->block_group_cache_lock);
9772 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
9773 block_group = rb_entry(n, struct btrfs_block_group_cache,
9774 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04009775 rb_erase(&block_group->cache_node,
9776 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009777 RB_CLEAR_NODE(&block_group->cache_node);
Yan Zhengd899e052008-10-30 14:25:28 -04009778 spin_unlock(&info->block_group_cache_lock);
9779
Josef Bacik80eb2342008-10-29 14:49:05 -04009780 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04009781 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04009782 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05009783
Josef Bacik3c148742011-02-02 15:53:47 +00009784 /*
9785 * We haven't cached this block group, which means we could
9786 * possibly have excluded extents on this block group.
9787 */
Josef Bacik36cce922013-08-05 11:15:21 -04009788 if (block_group->cached == BTRFS_CACHE_NO ||
9789 block_group->cached == BTRFS_CACHE_ERROR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009790 free_excluded_extents(info, block_group);
Josef Bacik3c148742011-02-02 15:53:47 +00009791
Josef Bacik817d52f2009-07-13 21:29:25 -04009792 btrfs_remove_free_space_cache(block_group);
Filipe Manana5cdd7db2017-02-01 22:39:50 +00009793 ASSERT(block_group->cached != BTRFS_CACHE_STARTED);
Liu Bof3bca802016-07-20 17:33:44 -07009794 ASSERT(list_empty(&block_group->dirty_list));
9795 ASSERT(list_empty(&block_group->io_list));
9796 ASSERT(list_empty(&block_group->bg_list));
9797 ASSERT(atomic_read(&block_group->count) == 1);
Josef Bacik11dfe352009-11-13 20:12:59 +00009798 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04009799
9800 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009801 }
9802 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04009803
9804 /* now that all the block groups are freed, go through and
9805 * free all the space_info structs. This is only called during
9806 * the final stages of unmount, and so we know nobody is
9807 * using them. We call synchronize_rcu() once before we start,
9808 * just to be on the safe side.
9809 */
9810 synchronize_rcu();
9811
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04009812 release_global_block_rsv(info);
9813
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309814 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009815 int i;
9816
Chris Mason4184ea72009-03-10 12:39:20 -04009817 space_info = list_entry(info->space_info.next,
9818 struct btrfs_space_info,
9819 list);
Josef Bacikd555b6c2016-03-25 13:25:51 -04009820
9821 /*
9822 * Do not hide this behind enospc_debug, this is actually
9823 * important and indicates a real bug if this happens.
9824 */
9825 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00009826 space_info->bytes_reserved > 0 ||
Josef Bacikd555b6c2016-03-25 13:25:51 -04009827 space_info->bytes_may_use > 0))
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009828 dump_space_info(info, space_info, 0, 0);
Chris Mason4184ea72009-03-10 12:39:20 -04009829 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009830 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
9831 struct kobject *kobj;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009832 kobj = space_info->block_group_kobjs[i];
9833 space_info->block_group_kobjs[i] = NULL;
9834 if (kobj) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009835 kobject_del(kobj);
9836 kobject_put(kobj);
9837 }
9838 }
9839 kobject_del(&space_info->kobj);
9840 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04009841 }
Zheng Yan1a40e232008-09-26 10:09:34 -04009842 return 0;
9843}
9844
Yan, Zhengb742bb822010-05-16 10:46:24 -04009845static void __link_block_group(struct btrfs_space_info *space_info,
9846 struct btrfs_block_group_cache *cache)
9847{
9848 int index = get_block_group_index(cache);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009849 bool first = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04009850
9851 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009852 if (list_empty(&space_info->block_groups[index]))
9853 first = true;
9854 list_add_tail(&cache->list, &space_info->block_groups[index]);
9855 up_write(&space_info->groups_sem);
9856
9857 if (first) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009858 struct raid_kobject *rkobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009859 int ret;
9860
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009861 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
9862 if (!rkobj)
9863 goto out_err;
9864 rkobj->raid_type = index;
9865 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
9866 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
9867 "%s", get_raid_name(index));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009868 if (ret) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009869 kobject_put(&rkobj->kobj);
9870 goto out_err;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009871 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009872 space_info->block_group_kobjs[index] = &rkobj->kobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009873 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009874
9875 return;
9876out_err:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009877 btrfs_warn(cache->fs_info,
9878 "failed to add kobject for block cache, ignoring");
Yan, Zhengb742bb822010-05-16 10:46:24 -04009879}
9880
Miao Xie920e4a52014-01-15 20:00:55 +08009881static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009882btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info,
9883 u64 start, u64 size)
Miao Xie920e4a52014-01-15 20:00:55 +08009884{
9885 struct btrfs_block_group_cache *cache;
9886
9887 cache = kzalloc(sizeof(*cache), GFP_NOFS);
9888 if (!cache)
9889 return NULL;
9890
9891 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
9892 GFP_NOFS);
9893 if (!cache->free_space_ctl) {
9894 kfree(cache);
9895 return NULL;
9896 }
9897
9898 cache->key.objectid = start;
9899 cache->key.offset = size;
9900 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
9901
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009902 cache->sectorsize = fs_info->sectorsize;
9903 cache->fs_info = fs_info;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009904 cache->full_stripe_len = btrfs_full_stripe_len(fs_info,
9905 &fs_info->mapping_tree,
9906 start);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07009907 set_free_space_tree_thresholds(cache);
9908
Miao Xie920e4a52014-01-15 20:00:55 +08009909 atomic_set(&cache->count, 1);
9910 spin_lock_init(&cache->lock);
Miao Xiee570fd22014-06-19 10:42:50 +08009911 init_rwsem(&cache->data_rwsem);
Miao Xie920e4a52014-01-15 20:00:55 +08009912 INIT_LIST_HEAD(&cache->list);
9913 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -04009914 INIT_LIST_HEAD(&cache->bg_list);
Josef Bacik633c0aa2014-10-31 09:49:34 -04009915 INIT_LIST_HEAD(&cache->ro_list);
Josef Bacikce93ec52014-11-17 15:45:48 -05009916 INIT_LIST_HEAD(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07009917 INIT_LIST_HEAD(&cache->io_list);
Miao Xie920e4a52014-01-15 20:00:55 +08009918 btrfs_init_free_space_ctl(cache);
Filipe Manana04216822014-11-27 21:14:15 +00009919 atomic_set(&cache->trimming, 0);
Omar Sandovala5ed9182015-09-29 20:50:35 -07009920 mutex_init(&cache->free_space_lock);
Miao Xie920e4a52014-01-15 20:00:55 +08009921
9922 return cache;
9923}
9924
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04009925int btrfs_read_block_groups(struct btrfs_fs_info *info)
Chris Mason9078a3e2007-04-26 16:46:15 -04009926{
9927 struct btrfs_path *path;
9928 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04009929 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04009930 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04009931 struct btrfs_key key;
9932 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04009933 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04009934 int need_clear = 0;
9935 u64 cache_gen;
Liu Bo49303382016-08-25 18:08:27 -07009936 u64 feature;
9937 int mixed;
9938
9939 feature = btrfs_super_incompat_flags(info->super_copy);
9940 mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS);
Chris Mason96b51792007-10-15 16:15:19 -04009941
Chris Mason9078a3e2007-04-26 16:46:15 -04009942 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009943 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009944 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Chris Mason9078a3e2007-04-26 16:46:15 -04009945 path = btrfs_alloc_path();
9946 if (!path)
9947 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01009948 path->reada = READA_FORWARD;
Chris Mason9078a3e2007-04-26 16:46:15 -04009949
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009950 cache_gen = btrfs_super_cache_generation(info->super_copy);
9951 if (btrfs_test_opt(info, SPACE_CACHE) &&
9952 btrfs_super_generation(info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04009953 need_clear = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009954 if (btrfs_test_opt(info, CLEAR_CACHE))
Josef Bacik88c2ba32010-09-21 14:21:34 -04009955 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04009956
Chris Masond3977122009-01-05 21:25:51 -05009957 while (1) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009958 ret = find_first_block_group(info, path, &key);
Yan, Zhengb742bb822010-05-16 10:46:24 -04009959 if (ret > 0)
9960 break;
Chris Mason0b86a832008-03-24 15:01:56 -04009961 if (ret != 0)
9962 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +08009963
Chris Mason5f39d392007-10-15 16:14:19 -04009964 leaf = path->nodes[0];
9965 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +08009966
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009967 cache = btrfs_create_block_group_cache(info, found_key.objectid,
Miao Xie920e4a52014-01-15 20:00:55 +08009968 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04009969 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04009970 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009971 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04009972 }
Josef Bacik96303082009-07-13 21:29:25 -04009973
Liu Bocf7c1ef2012-07-06 03:31:34 -06009974 if (need_clear) {
9975 /*
9976 * When we mount with old space cache, we need to
9977 * set BTRFS_DC_CLEAR and set dirty flag.
9978 *
9979 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
9980 * truncate the old free space cache inode and
9981 * setup a new one.
9982 * b) Setting 'dirty flag' makes sure that we flush
9983 * the new space cache info onto disk.
9984 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009985 if (btrfs_test_opt(info, SPACE_CACHE))
Josef Bacikce93ec52014-11-17 15:45:48 -05009986 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06009987 }
Josef Bacik0af3d002010-06-21 14:48:16 -04009988
Chris Mason5f39d392007-10-15 16:14:19 -04009989 read_extent_buffer(leaf, &cache->item,
9990 btrfs_item_ptr_offset(leaf, path->slots[0]),
9991 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +08009992 cache->flags = btrfs_block_group_flags(&cache->item);
Liu Bo49303382016-08-25 18:08:27 -07009993 if (!mixed &&
9994 ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
9995 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
9996 btrfs_err(info,
9997"bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
9998 cache->key.objectid);
9999 ret = -EINVAL;
10000 goto error;
10001 }
Chris Mason0b86a832008-03-24 15:01:56 -040010002
Chris Mason9078a3e2007-04-26 16:46:15 -040010003 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +020010004 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +080010005
Josef Bacik817d52f2009-07-13 21:29:25 -040010006 /*
Josef Bacik3c148742011-02-02 15:53:47 +000010007 * We need to exclude the super stripes now so that the space
10008 * info has super bytes accounted for, otherwise we'll think
10009 * we have more space than we actually do.
10010 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010011 ret = exclude_super_stripes(info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010012 if (ret) {
10013 /*
10014 * We may have excluded something, so call this just in
10015 * case.
10016 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010017 free_excluded_extents(info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010018 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010019 goto error;
10020 }
Josef Bacik3c148742011-02-02 15:53:47 +000010021
10022 /*
Josef Bacik817d52f2009-07-13 21:29:25 -040010023 * check for two cases, either we are full, and therefore
10024 * don't need to bother with the caching work since we won't
10025 * find any space, or we are empty, and we can just add all
10026 * the space in and be done with it. This saves us _alot_ of
10027 * time, particularly in the full case.
10028 */
10029 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -040010030 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010031 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010032 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010033 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -040010034 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010035 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010036 add_new_free_space(cache, info,
Josef Bacik817d52f2009-07-13 21:29:25 -040010037 found_key.objectid,
10038 found_key.objectid +
10039 found_key.offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010040 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010041 }
Chris Mason96b51792007-10-15 16:15:19 -040010042
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010043 ret = btrfs_add_block_group_cache(info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010044 if (ret) {
10045 btrfs_remove_free_space_cache(cache);
10046 btrfs_put_block_group(cache);
10047 goto error;
10048 }
10049
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010050 trace_btrfs_add_block_group(info, cache, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -040010051 ret = update_space_info(info, cache->flags, found_key.offset,
10052 btrfs_block_group_used(&cache->item),
Josef Bacike40edf22016-03-25 13:25:47 -040010053 cache->bytes_super, &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010054 if (ret) {
10055 btrfs_remove_free_space_cache(cache);
10056 spin_lock(&info->block_group_cache_lock);
10057 rb_erase(&cache->cache_node,
10058 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +000010059 RB_CLEAR_NODE(&cache->cache_node);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010060 spin_unlock(&info->block_group_cache_lock);
10061 btrfs_put_block_group(cache);
10062 goto error;
10063 }
10064
Chris Mason6324fbf2008-03-24 15:01:59 -040010065 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -040010066
Yan, Zhengb742bb822010-05-16 10:46:24 -040010067 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010068
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010069 set_avail_alloc_bits(info, cache->flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010070 if (btrfs_chunk_readonly(info, cache->key.objectid)) {
Zhaolei868f4012015-08-05 16:43:27 +080010071 inc_block_group_ro(cache, 1);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010072 } else if (btrfs_block_group_used(&cache->item) == 0) {
10073 spin_lock(&info->unused_bgs_lock);
10074 /* Should always be true but just in case. */
10075 if (list_empty(&cache->bg_list)) {
10076 btrfs_get_block_group(cache);
10077 list_add_tail(&cache->bg_list,
10078 &info->unused_bgs);
10079 }
10080 spin_unlock(&info->unused_bgs_lock);
10081 }
Chris Mason9078a3e2007-04-26 16:46:15 -040010082 }
Yan, Zhengb742bb822010-05-16 10:46:24 -040010083
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010084 list_for_each_entry_rcu(space_info, &info->space_info, list) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010085 if (!(get_alloc_profile(info, space_info->flags) &
Yan, Zhengb742bb822010-05-16 10:46:24 -040010086 (BTRFS_BLOCK_GROUP_RAID10 |
10087 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -050010088 BTRFS_BLOCK_GROUP_RAID5 |
10089 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb822010-05-16 10:46:24 -040010090 BTRFS_BLOCK_GROUP_DUP)))
10091 continue;
10092 /*
10093 * avoid allocating from un-mirrored block group if there are
10094 * mirrored block groups.
10095 */
chandan1095cc02013-07-16 12:28:56 +053010096 list_for_each_entry(cache,
10097 &space_info->block_groups[BTRFS_RAID_RAID0],
10098 list)
Zhaolei868f4012015-08-05 16:43:27 +080010099 inc_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +053010100 list_for_each_entry(cache,
10101 &space_info->block_groups[BTRFS_RAID_SINGLE],
10102 list)
Zhaolei868f4012015-08-05 16:43:27 +080010103 inc_block_group_ro(cache, 1);
Yan, Zhengb742bb822010-05-16 10:46:24 -040010104 }
Yan, Zhengf0486c62010-05-16 10:46:25 -040010105
10106 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -040010107 ret = 0;
10108error:
Chris Mason9078a3e2007-04-26 16:46:15 -040010109 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -040010110 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010111}
Chris Mason6324fbf2008-03-24 15:01:59 -040010112
Josef Bacikea658ba2012-09-11 16:57:25 -040010113void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010114 struct btrfs_fs_info *fs_info)
Josef Bacikea658ba2012-09-11 16:57:25 -040010115{
10116 struct btrfs_block_group_cache *block_group, *tmp;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010117 struct btrfs_root *extent_root = fs_info->extent_root;
Josef Bacikea658ba2012-09-11 16:57:25 -040010118 struct btrfs_block_group_item item;
10119 struct btrfs_key key;
10120 int ret = 0;
Filipe Mananad9a05402015-10-03 13:13:13 +010010121 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010122
Filipe Mananad9a05402015-10-03 13:13:13 +010010123 trans->can_flush_pending_bgs = false;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010124 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
Josef Bacikea658ba2012-09-11 16:57:25 -040010125 if (ret)
Filipe Mananac92f6be2014-11-26 15:28:55 +000010126 goto next;
Josef Bacikea658ba2012-09-11 16:57:25 -040010127
10128 spin_lock(&block_group->lock);
10129 memcpy(&item, &block_group->item, sizeof(item));
10130 memcpy(&key, &block_group->key, sizeof(key));
10131 spin_unlock(&block_group->lock);
10132
10133 ret = btrfs_insert_item(trans, extent_root, &key, &item,
10134 sizeof(item));
10135 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010136 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010137 ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid,
10138 key.offset);
Josef Bacik6df9a952013-06-27 13:22:46 -040010139 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010140 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010141 add_block_group_free_space(trans, fs_info, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010142 /* already aborted the transaction if it failed. */
Filipe Mananac92f6be2014-11-26 15:28:55 +000010143next:
10144 list_del_init(&block_group->bg_list);
Josef Bacikea658ba2012-09-11 16:57:25 -040010145 }
Filipe Mananad9a05402015-10-03 13:13:13 +010010146 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010147}
10148
Chris Mason6324fbf2008-03-24 15:01:59 -040010149int btrfs_make_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010150 struct btrfs_fs_info *fs_info, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -040010151 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -040010152 u64 size)
10153{
Chris Mason6324fbf2008-03-24 15:01:59 -040010154 struct btrfs_block_group_cache *cache;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010155 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -040010156
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010157 btrfs_set_log_full_commit(fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -040010158
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010159 cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -040010160 if (!cache)
10161 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +080010162
Chris Mason6324fbf2008-03-24 15:01:59 -040010163 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -040010164 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
Chris Mason6324fbf2008-03-24 15:01:59 -040010165 btrfs_set_block_group_flags(&cache->item, type);
10166
Miao Xie920e4a52014-01-15 20:00:55 +080010167 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -040010168 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010169 cache->cached = BTRFS_CACHE_FINISHED;
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010170 cache->needs_free_space = 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010171 ret = exclude_super_stripes(fs_info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010172 if (ret) {
10173 /*
10174 * We may have excluded something, so call this just in
10175 * case.
10176 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010177 free_excluded_extents(fs_info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010178 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010179 return ret;
10180 }
Josef Bacik96303082009-07-13 21:29:25 -040010181
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010182 add_new_free_space(cache, fs_info, chunk_offset, chunk_offset + size);
Josef Bacik817d52f2009-07-13 21:29:25 -040010183
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010184 free_excluded_extents(fs_info, cache);
Yan Zheng11833d62009-09-11 16:11:19 -040010185
Josef Bacikd0bd4562015-09-23 14:54:14 -040010186#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010187 if (btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -040010188 u64 new_bytes_used = size - bytes_used;
10189
10190 bytes_used += new_bytes_used >> 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010191 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -040010192 }
10193#endif
Filipe Manana2e6e5182015-05-12 00:28:11 +010010194 /*
10195 * Call to ensure the corresponding space_info object is created and
10196 * assigned to our block group, but don't update its counters just yet.
10197 * We want our bg to be added to the rbtree with its ->space_info set.
10198 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010199 ret = update_space_info(fs_info, cache->flags, 0, 0, 0,
Filipe Manana2e6e5182015-05-12 00:28:11 +010010200 &cache->space_info);
10201 if (ret) {
10202 btrfs_remove_free_space_cache(cache);
10203 btrfs_put_block_group(cache);
10204 return ret;
10205 }
10206
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010207 ret = btrfs_add_block_group_cache(fs_info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010208 if (ret) {
10209 btrfs_remove_free_space_cache(cache);
10210 btrfs_put_block_group(cache);
10211 return ret;
10212 }
10213
Filipe Manana2e6e5182015-05-12 00:28:11 +010010214 /*
10215 * Now that our block group has its ->space_info set and is inserted in
10216 * the rbtree, update the space info's counters.
10217 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010218 trace_btrfs_add_block_group(fs_info, cache, 1);
10219 ret = update_space_info(fs_info, cache->flags, size, bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -040010220 cache->bytes_super, &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010221 if (ret) {
10222 btrfs_remove_free_space_cache(cache);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010223 spin_lock(&fs_info->block_group_cache_lock);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010224 rb_erase(&cache->cache_node,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010225 &fs_info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +000010226 RB_CLEAR_NODE(&cache->cache_node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010227 spin_unlock(&fs_info->block_group_cache_lock);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010228 btrfs_put_block_group(cache);
10229 return ret;
10230 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010231 update_global_block_rsv(fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -040010232
Yan, Zhengb742bb822010-05-16 10:46:24 -040010233 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010234
Josef Bacik47ab2a62014-09-18 11:20:02 -040010235 list_add_tail(&cache->bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -040010236
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010237 set_avail_alloc_bits(fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -040010238 return 0;
10239}
Zheng Yan1a40e232008-09-26 10:09:34 -040010240
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010241static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
10242{
Ilya Dryomov899c81e2012-03-27 17:09:16 +030010243 u64 extra_flags = chunk_to_extended(flags) &
10244 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010245
Miao Xiede98ced2013-01-29 10:13:12 +000010246 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010247 if (flags & BTRFS_BLOCK_GROUP_DATA)
10248 fs_info->avail_data_alloc_bits &= ~extra_flags;
10249 if (flags & BTRFS_BLOCK_GROUP_METADATA)
10250 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
10251 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
10252 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +000010253 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010254}
10255
Zheng Yan1a40e232008-09-26 10:09:34 -040010256int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010257 struct btrfs_fs_info *fs_info, u64 group_start,
Filipe Manana04216822014-11-27 21:14:15 +000010258 struct extent_map *em)
Zheng Yan1a40e232008-09-26 10:09:34 -040010259{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010260 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010261 struct btrfs_path *path;
10262 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -040010263 struct btrfs_free_cluster *cluster;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010264 struct btrfs_root *tree_root = fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010265 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -040010266 struct inode *inode;
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010267 struct kobject *kobj = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -040010268 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010269 int index;
Josef Bacik89a55892010-10-14 14:52:27 -040010270 int factor;
Filipe Manana4f69cb92014-11-26 15:28:51 +000010271 struct btrfs_caching_control *caching_ctl = NULL;
Filipe Manana04216822014-11-27 21:14:15 +000010272 bool remove_em;
Zheng Yan1a40e232008-09-26 10:09:34 -040010273
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010274 block_group = btrfs_lookup_block_group(fs_info, group_start);
Zheng Yan1a40e232008-09-26 10:09:34 -040010275 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -050010276 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -040010277
liubo9f7c43c2011-03-07 02:13:33 +000010278 /*
10279 * Free the reserved super bytes from this block group before
10280 * remove it.
10281 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010282 free_excluded_extents(fs_info, block_group);
liubo9f7c43c2011-03-07 02:13:33 +000010283
Zheng Yan1a40e232008-09-26 10:09:34 -040010284 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010285 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -040010286 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
10287 BTRFS_BLOCK_GROUP_RAID1 |
10288 BTRFS_BLOCK_GROUP_RAID10))
10289 factor = 2;
10290 else
10291 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -040010292
Chris Mason44fb5512009-06-04 15:34:51 -040010293 /* make sure this block group isn't part of an allocation cluster */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010294 cluster = &fs_info->data_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010295 spin_lock(&cluster->refill_lock);
10296 btrfs_return_cluster_to_free_space(block_group, cluster);
10297 spin_unlock(&cluster->refill_lock);
10298
10299 /*
10300 * make sure this block group isn't part of a metadata
10301 * allocation cluster
10302 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010303 cluster = &fs_info->meta_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010304 spin_lock(&cluster->refill_lock);
10305 btrfs_return_cluster_to_free_space(block_group, cluster);
10306 spin_unlock(&cluster->refill_lock);
10307
Zheng Yan1a40e232008-09-26 10:09:34 -040010308 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010309 if (!path) {
10310 ret = -ENOMEM;
10311 goto out;
10312 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010313
Chris Mason1bbc6212015-04-06 12:46:08 -070010314 /*
10315 * get the inode first so any iput calls done for the io_list
10316 * aren't the final iput (no unlinks allowed now)
10317 */
Jeff Mahoney77ab86b2017-02-15 16:28:30 -050010318 inode = lookup_free_space_inode(fs_info, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010319
10320 mutex_lock(&trans->transaction->cache_write_mutex);
10321 /*
10322 * make sure our free spache cache IO is done before remove the
10323 * free space inode
10324 */
10325 spin_lock(&trans->transaction->dirty_bgs_lock);
10326 if (!list_empty(&block_group->io_list)) {
10327 list_del_init(&block_group->io_list);
10328
10329 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
10330
10331 spin_unlock(&trans->transaction->dirty_bgs_lock);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040010332 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010333 btrfs_put_block_group(block_group);
10334 spin_lock(&trans->transaction->dirty_bgs_lock);
10335 }
10336
10337 if (!list_empty(&block_group->dirty_list)) {
10338 list_del_init(&block_group->dirty_list);
10339 btrfs_put_block_group(block_group);
10340 }
10341 spin_unlock(&trans->transaction->dirty_bgs_lock);
10342 mutex_unlock(&trans->transaction->cache_write_mutex);
10343
Josef Bacik0af3d002010-06-21 14:48:16 -040010344 if (!IS_ERR(inode)) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010345 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010346 if (ret) {
10347 btrfs_add_delayed_iput(inode);
10348 goto out;
10349 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010350 clear_nlink(inode);
10351 /* One for the block groups ref */
10352 spin_lock(&block_group->lock);
10353 if (block_group->iref) {
10354 block_group->iref = 0;
10355 block_group->inode = NULL;
10356 spin_unlock(&block_group->lock);
10357 iput(inode);
10358 } else {
10359 spin_unlock(&block_group->lock);
10360 }
10361 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -040010362 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -040010363 }
10364
10365 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
10366 key.offset = block_group->key.objectid;
10367 key.type = 0;
10368
10369 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
10370 if (ret < 0)
10371 goto out;
10372 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +020010373 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010374 if (ret == 0) {
10375 ret = btrfs_del_item(trans, tree_root, path);
10376 if (ret)
10377 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +020010378 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010379 }
10380
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010381 spin_lock(&fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -040010382 rb_erase(&block_group->cache_node,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010383 &fs_info->block_group_cache_tree);
Filipe Manana292cbd52014-11-26 15:28:50 +000010384 RB_CLEAR_NODE(&block_group->cache_node);
Liu Boa1897fd2012-12-27 09:01:23 +000010385
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010386 if (fs_info->first_logical_byte == block_group->key.objectid)
10387 fs_info->first_logical_byte = (u64)-1;
10388 spin_unlock(&fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010389
Josef Bacik80eb2342008-10-29 14:49:05 -040010390 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -040010391 /*
10392 * we must use list_del_init so people can check to see if they
10393 * are still on the list after taking the semaphore
10394 */
10395 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010396 if (list_empty(&block_group->space_info->block_groups[index])) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010397 kobj = block_group->space_info->block_group_kobjs[index];
10398 block_group->space_info->block_group_kobjs[index] = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010399 clear_avail_alloc_bits(fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010400 }
Josef Bacik80eb2342008-10-29 14:49:05 -040010401 up_write(&block_group->space_info->groups_sem);
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010402 if (kobj) {
10403 kobject_del(kobj);
10404 kobject_put(kobj);
10405 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010406
Filipe Manana4f69cb92014-11-26 15:28:51 +000010407 if (block_group->has_caching_ctl)
10408 caching_ctl = get_caching_control(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -040010409 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -040010410 wait_block_group_cache_done(block_group);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010411 if (block_group->has_caching_ctl) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010412 down_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010413 if (!caching_ctl) {
10414 struct btrfs_caching_control *ctl;
10415
10416 list_for_each_entry(ctl,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010417 &fs_info->caching_block_groups, list)
Filipe Manana4f69cb92014-11-26 15:28:51 +000010418 if (ctl->block_group == block_group) {
10419 caching_ctl = ctl;
Elena Reshetova1e4f4712017-03-03 10:55:14 +020010420 refcount_inc(&caching_ctl->count);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010421 break;
10422 }
10423 }
10424 if (caching_ctl)
10425 list_del_init(&caching_ctl->list);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010426 up_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010427 if (caching_ctl) {
10428 /* Once for the caching bgs list and once for us. */
10429 put_caching_control(caching_ctl);
10430 put_caching_control(caching_ctl);
10431 }
10432 }
Josef Bacik817d52f2009-07-13 21:29:25 -040010433
Josef Bacikce93ec52014-11-17 15:45:48 -050010434 spin_lock(&trans->transaction->dirty_bgs_lock);
10435 if (!list_empty(&block_group->dirty_list)) {
Chris Mason1bbc6212015-04-06 12:46:08 -070010436 WARN_ON(1);
10437 }
10438 if (!list_empty(&block_group->io_list)) {
10439 WARN_ON(1);
Josef Bacikce93ec52014-11-17 15:45:48 -050010440 }
10441 spin_unlock(&trans->transaction->dirty_bgs_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010442 btrfs_remove_free_space_cache(block_group);
10443
Yan Zhengc146afa2008-11-12 14:34:12 -050010444 spin_lock(&block_group->space_info->lock);
Filipe Manana75c68e92015-01-16 13:24:40 +000010445 list_del_init(&block_group->ro_list);
Zhao Lei18d018a2015-02-24 20:07:44 +080010446
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010447 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Zhao Lei18d018a2015-02-24 20:07:44 +080010448 WARN_ON(block_group->space_info->total_bytes
10449 < block_group->key.offset);
10450 WARN_ON(block_group->space_info->bytes_readonly
10451 < block_group->key.offset);
10452 WARN_ON(block_group->space_info->disk_total
10453 < block_group->key.offset * factor);
10454 }
Yan Zhengc146afa2008-11-12 14:34:12 -050010455 block_group->space_info->total_bytes -= block_group->key.offset;
10456 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -040010457 block_group->space_info->disk_total -= block_group->key.offset * factor;
Zhao Lei18d018a2015-02-24 20:07:44 +080010458
Yan Zhengc146afa2008-11-12 14:34:12 -050010459 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -040010460
Josef Bacik0af3d002010-06-21 14:48:16 -040010461 memcpy(&key, &block_group->key, sizeof(key));
10462
David Sterba34441362016-10-04 19:34:27 +020010463 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana495e64f2014-12-02 18:07:30 +000010464 if (!list_empty(&em->list)) {
10465 /* We're in the transaction->pending_chunks list. */
10466 free_extent_map(em);
10467 }
Filipe Manana04216822014-11-27 21:14:15 +000010468 spin_lock(&block_group->lock);
10469 block_group->removed = 1;
10470 /*
10471 * At this point trimming can't start on this block group, because we
10472 * removed the block group from the tree fs_info->block_group_cache_tree
10473 * so no one can't find it anymore and even if someone already got this
10474 * block group before we removed it from the rbtree, they have already
10475 * incremented block_group->trimming - if they didn't, they won't find
10476 * any free space entries because we already removed them all when we
10477 * called btrfs_remove_free_space_cache().
10478 *
10479 * And we must not remove the extent map from the fs_info->mapping_tree
10480 * to prevent the same logical address range and physical device space
10481 * ranges from being reused for a new block group. This is because our
10482 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
10483 * completely transactionless, so while it is trimming a range the
10484 * currently running transaction might finish and a new one start,
10485 * allowing for new block groups to be created that can reuse the same
10486 * physical device locations unless we take this special care.
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010487 *
10488 * There may also be an implicit trim operation if the file system
10489 * is mounted with -odiscard. The same protections must remain
10490 * in place until the extents have been discarded completely when
10491 * the transaction commit has completed.
Filipe Manana04216822014-11-27 21:14:15 +000010492 */
10493 remove_em = (atomic_read(&block_group->trimming) == 0);
10494 /*
10495 * Make sure a trimmer task always sees the em in the pinned_chunks list
10496 * if it sees block_group->removed == 1 (needs to lock block_group->lock
10497 * before checking block_group->removed).
10498 */
10499 if (!remove_em) {
10500 /*
10501 * Our em might be in trans->transaction->pending_chunks which
10502 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10503 * and so is the fs_info->pinned_chunks list.
10504 *
10505 * So at this point we must be holding the chunk_mutex to avoid
10506 * any races with chunk allocation (more specifically at
10507 * volumes.c:contains_pending_extent()), to ensure it always
10508 * sees the em, either in the pending_chunks list or in the
10509 * pinned_chunks list.
10510 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010511 list_move_tail(&em->list, &fs_info->pinned_chunks);
Filipe Manana04216822014-11-27 21:14:15 +000010512 }
10513 spin_unlock(&block_group->lock);
Filipe Manana04216822014-11-27 21:14:15 +000010514
10515 if (remove_em) {
10516 struct extent_map_tree *em_tree;
10517
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010518 em_tree = &fs_info->mapping_tree.map_tree;
Filipe Manana04216822014-11-27 21:14:15 +000010519 write_lock(&em_tree->lock);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010520 /*
10521 * The em might be in the pending_chunks list, so make sure the
10522 * chunk mutex is locked, since remove_extent_mapping() will
10523 * delete us from that list.
10524 */
Filipe Manana04216822014-11-27 21:14:15 +000010525 remove_extent_mapping(em_tree, em);
10526 write_unlock(&em_tree->lock);
10527 /* once for the tree */
10528 free_extent_map(em);
10529 }
10530
David Sterba34441362016-10-04 19:34:27 +020010531 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010532
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010533 ret = remove_block_group_free_space(trans, fs_info, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010534 if (ret)
10535 goto out;
10536
Chris Masonfa9c0d792009-04-03 09:47:43 -040010537 btrfs_put_block_group(block_group);
10538 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -040010539
10540 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
10541 if (ret > 0)
10542 ret = -EIO;
10543 if (ret < 0)
10544 goto out;
10545
10546 ret = btrfs_del_item(trans, root, path);
10547out:
10548 btrfs_free_path(path);
10549 return ret;
10550}
liuboacce9522011-01-06 19:30:25 +080010551
Filipe Manana8eab77f2015-11-13 23:57:16 +000010552struct btrfs_trans_handle *
Filipe Manana7fd01182015-11-13 23:57:17 +000010553btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info,
10554 const u64 chunk_offset)
Filipe Manana8eab77f2015-11-13 23:57:16 +000010555{
Filipe Manana7fd01182015-11-13 23:57:17 +000010556 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
10557 struct extent_map *em;
10558 struct map_lookup *map;
10559 unsigned int num_items;
10560
10561 read_lock(&em_tree->lock);
10562 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
10563 read_unlock(&em_tree->lock);
10564 ASSERT(em && em->start == chunk_offset);
10565
Filipe Manana8eab77f2015-11-13 23:57:16 +000010566 /*
Filipe Manana7fd01182015-11-13 23:57:17 +000010567 * We need to reserve 3 + N units from the metadata space info in order
10568 * to remove a block group (done at btrfs_remove_chunk() and at
10569 * btrfs_remove_block_group()), which are used for:
10570 *
Filipe Manana8eab77f2015-11-13 23:57:16 +000010571 * 1 unit for adding the free space inode's orphan (located in the tree
10572 * of tree roots).
Filipe Manana7fd01182015-11-13 23:57:17 +000010573 * 1 unit for deleting the block group item (located in the extent
10574 * tree).
10575 * 1 unit for deleting the free space item (located in tree of tree
10576 * roots).
10577 * N units for deleting N device extent items corresponding to each
10578 * stripe (located in the device tree).
10579 *
10580 * In order to remove a block group we also need to reserve units in the
10581 * system space info in order to update the chunk tree (update one or
10582 * more device items and remove one chunk item), but this is done at
10583 * btrfs_remove_chunk() through a call to check_system_chunk().
Filipe Manana8eab77f2015-11-13 23:57:16 +000010584 */
Jeff Mahoney95617d62015-06-03 10:55:48 -040010585 map = em->map_lookup;
Filipe Manana7fd01182015-11-13 23:57:17 +000010586 num_items = 3 + map->num_stripes;
10587 free_extent_map(em);
10588
Filipe Manana8eab77f2015-11-13 23:57:16 +000010589 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
Filipe Manana7fd01182015-11-13 23:57:17 +000010590 num_items, 1);
Filipe Manana8eab77f2015-11-13 23:57:16 +000010591}
10592
Josef Bacik47ab2a62014-09-18 11:20:02 -040010593/*
10594 * Process the unused_bgs list and remove any that don't have any allocated
10595 * space inside of them.
10596 */
10597void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10598{
10599 struct btrfs_block_group_cache *block_group;
10600 struct btrfs_space_info *space_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010601 struct btrfs_trans_handle *trans;
10602 int ret = 0;
10603
Josef Bacikafcdd122016-09-02 15:40:02 -040010604 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
Josef Bacik47ab2a62014-09-18 11:20:02 -040010605 return;
10606
10607 spin_lock(&fs_info->unused_bgs_lock);
10608 while (!list_empty(&fs_info->unused_bgs)) {
10609 u64 start, end;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010610 int trimming;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010611
10612 block_group = list_first_entry(&fs_info->unused_bgs,
10613 struct btrfs_block_group_cache,
10614 bg_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010615 list_del_init(&block_group->bg_list);
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010616
10617 space_info = block_group->space_info;
10618
Josef Bacik47ab2a62014-09-18 11:20:02 -040010619 if (ret || btrfs_mixed_space_info(space_info)) {
10620 btrfs_put_block_group(block_group);
10621 continue;
10622 }
10623 spin_unlock(&fs_info->unused_bgs_lock);
10624
Zhao Leid5f2e332015-10-08 18:46:44 +080010625 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +010010626
Josef Bacik47ab2a62014-09-18 11:20:02 -040010627 /* Don't want to race with allocators so take the groups_sem */
10628 down_write(&space_info->groups_sem);
10629 spin_lock(&block_group->lock);
10630 if (block_group->reserved ||
10631 btrfs_block_group_used(&block_group->item) ||
Chris Mason19c4d2f2016-10-10 13:43:31 -070010632 block_group->ro ||
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010633 list_is_singular(&block_group->list)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -040010634 /*
10635 * We want to bail if we made new allocations or have
10636 * outstanding allocations in this block group. We do
10637 * the ro check in case balance is currently acting on
10638 * this block group.
10639 */
10640 spin_unlock(&block_group->lock);
10641 up_write(&space_info->groups_sem);
10642 goto next;
10643 }
10644 spin_unlock(&block_group->lock);
10645
10646 /* We don't want to force the issue, only flip if it's ok. */
Zhaolei868f4012015-08-05 16:43:27 +080010647 ret = inc_block_group_ro(block_group, 0);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010648 up_write(&space_info->groups_sem);
10649 if (ret < 0) {
10650 ret = 0;
10651 goto next;
10652 }
10653
10654 /*
10655 * Want to do this before we do anything else so we can recover
10656 * properly if we fail to join the transaction.
10657 */
Filipe Manana7fd01182015-11-13 23:57:17 +000010658 trans = btrfs_start_trans_remove_block_group(fs_info,
10659 block_group->key.objectid);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010660 if (IS_ERR(trans)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010661 btrfs_dec_block_group_ro(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010662 ret = PTR_ERR(trans);
10663 goto next;
10664 }
10665
10666 /*
10667 * We could have pending pinned extents for this block group,
10668 * just delete them, we don't care about them anymore.
10669 */
10670 start = block_group->key.objectid;
10671 end = start + block_group->key.offset - 1;
Filipe Mananad4b450c2015-01-29 19:18:25 +000010672 /*
10673 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10674 * btrfs_finish_extent_commit(). If we are at transaction N,
10675 * another task might be running finish_extent_commit() for the
10676 * previous transaction N - 1, and have seen a range belonging
10677 * to the block group in freed_extents[] before we were able to
10678 * clear the whole block group range from freed_extents[]. This
10679 * means that task can lookup for the block group after we
10680 * unpinned it from freed_extents[] and removed it, leading to
10681 * a BUG_ON() at btrfs_unpin_extent_range().
10682 */
10683 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana758eb512014-11-03 14:08:39 +000010684 ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
David Sterba91166212016-04-26 23:54:39 +020010685 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010686 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010687 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010688 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010689 goto end_trans;
10690 }
10691 ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
David Sterba91166212016-04-26 23:54:39 +020010692 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010693 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010694 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010695 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010696 goto end_trans;
10697 }
Filipe Mananad4b450c2015-01-29 19:18:25 +000010698 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010699
10700 /* Reset pinned so btrfs_put_block_group doesn't complain */
Zhao Leic30666d2015-02-25 14:17:20 +080010701 spin_lock(&space_info->lock);
10702 spin_lock(&block_group->lock);
10703
10704 space_info->bytes_pinned -= block_group->pinned;
10705 space_info->bytes_readonly += block_group->pinned;
10706 percpu_counter_add(&space_info->total_bytes_pinned,
10707 -block_group->pinned);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010708 block_group->pinned = 0;
10709
Zhao Leic30666d2015-02-25 14:17:20 +080010710 spin_unlock(&block_group->lock);
10711 spin_unlock(&space_info->lock);
10712
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010713 /* DISCARD can flip during remount */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010714 trimming = btrfs_test_opt(fs_info, DISCARD);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010715
10716 /* Implicit trim during transaction commit. */
10717 if (trimming)
10718 btrfs_get_block_group_trimming(block_group);
10719
Josef Bacik47ab2a62014-09-18 11:20:02 -040010720 /*
10721 * Btrfs_remove_chunk will abort the transaction if things go
10722 * horribly wrong.
10723 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010724 ret = btrfs_remove_chunk(trans, fs_info,
Josef Bacik47ab2a62014-09-18 11:20:02 -040010725 block_group->key.objectid);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010726
10727 if (ret) {
10728 if (trimming)
10729 btrfs_put_block_group_trimming(block_group);
10730 goto end_trans;
10731 }
10732
10733 /*
10734 * If we're not mounted with -odiscard, we can just forget
10735 * about this block group. Otherwise we'll need to wait
10736 * until transaction commit to do the actual discard.
10737 */
10738 if (trimming) {
Filipe Manana348a0012015-11-27 12:16:16 +000010739 spin_lock(&fs_info->unused_bgs_lock);
10740 /*
10741 * A concurrent scrub might have added us to the list
10742 * fs_info->unused_bgs, so use a list_move operation
10743 * to add the block group to the deleted_bgs list.
10744 */
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010745 list_move(&block_group->bg_list,
10746 &trans->transaction->deleted_bgs);
Filipe Manana348a0012015-11-27 12:16:16 +000010747 spin_unlock(&fs_info->unused_bgs_lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010748 btrfs_get_block_group(block_group);
10749 }
Filipe Manana758eb512014-11-03 14:08:39 +000010750end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010751 btrfs_end_transaction(trans);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010752next:
Zhao Leid5f2e332015-10-08 18:46:44 +080010753 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010754 btrfs_put_block_group(block_group);
10755 spin_lock(&fs_info->unused_bgs_lock);
10756 }
10757 spin_unlock(&fs_info->unused_bgs_lock);
10758}
10759
liuboc59021f2011-03-07 02:13:14 +000010760int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
10761{
10762 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +000010763 struct btrfs_super_block *disk_super;
10764 u64 features;
10765 u64 flags;
10766 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +000010767 int ret;
10768
David Sterba6c417612011-04-13 15:41:04 +020010769 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +000010770 if (!btrfs_super_root(disk_super))
Dan Carpenter0dc924c52016-01-13 15:21:17 +030010771 return -EINVAL;
liuboc59021f2011-03-07 02:13:14 +000010772
liubo1aba86d2011-04-08 08:44:37 +000010773 features = btrfs_super_incompat_flags(disk_super);
10774 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
10775 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +000010776
liubo1aba86d2011-04-08 08:44:37 +000010777 flags = BTRFS_BLOCK_GROUP_SYSTEM;
Josef Bacike40edf22016-03-25 13:25:47 -040010778 ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +000010779 if (ret)
liubo1aba86d2011-04-08 08:44:37 +000010780 goto out;
liuboc59021f2011-03-07 02:13:14 +000010781
liubo1aba86d2011-04-08 08:44:37 +000010782 if (mixed) {
10783 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
Josef Bacike40edf22016-03-25 13:25:47 -040010784 ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010785 } else {
10786 flags = BTRFS_BLOCK_GROUP_METADATA;
Josef Bacike40edf22016-03-25 13:25:47 -040010787 ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010788 if (ret)
10789 goto out;
10790
10791 flags = BTRFS_BLOCK_GROUP_DATA;
Josef Bacike40edf22016-03-25 13:25:47 -040010792 ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010793 }
10794out:
liuboc59021f2011-03-07 02:13:14 +000010795 return ret;
10796}
10797
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010798int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
10799 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +080010800{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010801 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +080010802}
10803
Jeff Mahoney499f3772015-06-15 09:41:17 -040010804/*
10805 * It used to be that old block groups would be left around forever.
10806 * Iterating over them would be enough to trim unused space. Since we
10807 * now automatically remove them, we also need to iterate over unallocated
10808 * space.
10809 *
10810 * We don't want a transaction for this since the discard may take a
10811 * substantial amount of time. We don't require that a transaction be
10812 * running, but we do need to take a running transaction into account
10813 * to ensure that we're not discarding chunks that were released in
10814 * the current transaction.
10815 *
10816 * Holding the chunks lock will prevent other threads from allocating
10817 * or releasing chunks, but it won't prevent a running transaction
10818 * from committing and releasing the memory that the pending chunks
10819 * list head uses. For that, we need to take a reference to the
10820 * transaction.
10821 */
10822static int btrfs_trim_free_extents(struct btrfs_device *device,
10823 u64 minlen, u64 *trimmed)
10824{
10825 u64 start = 0, len = 0;
10826 int ret;
10827
10828 *trimmed = 0;
10829
10830 /* Not writeable = nothing to do. */
10831 if (!device->writeable)
10832 return 0;
10833
10834 /* No free space = nothing to do. */
10835 if (device->total_bytes <= device->bytes_used)
10836 return 0;
10837
10838 ret = 0;
10839
10840 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -040010841 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010842 struct btrfs_transaction *trans;
10843 u64 bytes;
10844
10845 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
10846 if (ret)
10847 return ret;
10848
10849 down_read(&fs_info->commit_root_sem);
10850
10851 spin_lock(&fs_info->trans_lock);
10852 trans = fs_info->running_transaction;
10853 if (trans)
Elena Reshetova9b64f572017-03-03 10:55:11 +020010854 refcount_inc(&trans->use_count);
Jeff Mahoney499f3772015-06-15 09:41:17 -040010855 spin_unlock(&fs_info->trans_lock);
10856
10857 ret = find_free_dev_extent_start(trans, device, minlen, start,
10858 &start, &len);
10859 if (trans)
10860 btrfs_put_transaction(trans);
10861
10862 if (ret) {
10863 up_read(&fs_info->commit_root_sem);
10864 mutex_unlock(&fs_info->chunk_mutex);
10865 if (ret == -ENOSPC)
10866 ret = 0;
10867 break;
10868 }
10869
10870 ret = btrfs_issue_discard(device->bdev, start, len, &bytes);
10871 up_read(&fs_info->commit_root_sem);
10872 mutex_unlock(&fs_info->chunk_mutex);
10873
10874 if (ret)
10875 break;
10876
10877 start += len;
10878 *trimmed += bytes;
10879
10880 if (fatal_signal_pending(current)) {
10881 ret = -ERESTARTSYS;
10882 break;
10883 }
10884
10885 cond_resched();
10886 }
10887
10888 return ret;
10889}
10890
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010891int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +000010892{
Li Dongyangf7039b12011-03-24 10:24:28 +000010893 struct btrfs_block_group_cache *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010894 struct btrfs_device *device;
10895 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +000010896 u64 group_trimmed;
10897 u64 start;
10898 u64 end;
10899 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +080010900 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +000010901 int ret = 0;
10902
Liu Bo2cac13e2012-02-09 18:17:41 +080010903 /*
10904 * try to trim all FS space, our block group may start from non-zero.
10905 */
10906 if (range->len == total_bytes)
10907 cache = btrfs_lookup_first_block_group(fs_info, range->start);
10908 else
10909 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +000010910
10911 while (cache) {
10912 if (cache->key.objectid >= (range->start + range->len)) {
10913 btrfs_put_block_group(cache);
10914 break;
10915 }
10916
10917 start = max(range->start, cache->key.objectid);
10918 end = min(range->start + range->len,
10919 cache->key.objectid + cache->key.offset);
10920
10921 if (end - start >= range->minlen) {
10922 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +000010923 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -040010924 if (ret) {
10925 btrfs_put_block_group(cache);
10926 break;
10927 }
10928 ret = wait_block_group_cache_done(cache);
10929 if (ret) {
10930 btrfs_put_block_group(cache);
10931 break;
10932 }
Li Dongyangf7039b12011-03-24 10:24:28 +000010933 }
10934 ret = btrfs_trim_block_group(cache,
10935 &group_trimmed,
10936 start,
10937 end,
10938 range->minlen);
10939
10940 trimmed += group_trimmed;
10941 if (ret) {
10942 btrfs_put_block_group(cache);
10943 break;
10944 }
10945 }
10946
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010947 cache = next_block_group(fs_info, cache);
Li Dongyangf7039b12011-03-24 10:24:28 +000010948 }
10949
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010950 mutex_lock(&fs_info->fs_devices->device_list_mutex);
10951 devices = &fs_info->fs_devices->alloc_list;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010952 list_for_each_entry(device, devices, dev_alloc_list) {
10953 ret = btrfs_trim_free_extents(device, range->minlen,
10954 &group_trimmed);
10955 if (ret)
10956 break;
10957
10958 trimmed += group_trimmed;
10959 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010960 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -040010961
Li Dongyangf7039b12011-03-24 10:24:28 +000010962 range->len = trimmed;
10963 return ret;
10964}
Miao Xie8257b2d2014-03-06 13:38:19 +080010965
10966/*
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010967 * btrfs_{start,end}_write_no_snapshoting() are similar to
10968 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
10969 * data into the page cache through nocow before the subvolume is snapshoted,
10970 * but flush the data into disk after the snapshot creation, or to prevent
10971 * operations while snapshoting is ongoing and that cause the snapshot to be
10972 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +080010973 */
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010974void btrfs_end_write_no_snapshoting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080010975{
10976 percpu_counter_dec(&root->subv_writers->counter);
10977 /*
David Sterbaa83342a2015-02-16 19:36:47 +010010978 * Make sure counter is updated before we wake up waiters.
Miao Xie8257b2d2014-03-06 13:38:19 +080010979 */
10980 smp_mb();
10981 if (waitqueue_active(&root->subv_writers->wait))
10982 wake_up(&root->subv_writers->wait);
10983}
10984
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010985int btrfs_start_write_no_snapshoting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080010986{
David Sterbaee39b432014-09-30 01:33:33 +020010987 if (atomic_read(&root->will_be_snapshoted))
Miao Xie8257b2d2014-03-06 13:38:19 +080010988 return 0;
10989
10990 percpu_counter_inc(&root->subv_writers->counter);
10991 /*
10992 * Make sure counter is updated before we check for snapshot creation.
10993 */
10994 smp_mb();
David Sterbaee39b432014-09-30 01:33:33 +020010995 if (atomic_read(&root->will_be_snapshoted)) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010996 btrfs_end_write_no_snapshoting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +080010997 return 0;
10998 }
10999 return 1;
11000}
Zhao Lei0bc19f902016-01-06 18:56:36 +080011001
11002static int wait_snapshoting_atomic_t(atomic_t *a)
11003{
11004 schedule();
11005 return 0;
11006}
11007
11008void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
11009{
11010 while (true) {
11011 int ret;
11012
11013 ret = btrfs_start_write_no_snapshoting(root);
11014 if (ret)
11015 break;
11016 wait_on_atomic_t(&root->will_be_snapshoted,
11017 wait_snapshoting_atomic_t,
11018 TASK_UNINTERRUPTIBLE);
11019 }
11020}