blob: ac7e6713033c7f1f12406ac28adec98f1d14d7e7 [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>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
David Sterbadff51cd2011-06-14 12:52:17 +020026#include <linux/ratelimit.h>
Josef Bacikb150a4f2013-06-19 15:00:04 -040027#include <linux/percpu_counter.h>
Chris Mason74493f72007-12-11 09:25:06 -050028#include "hash.h"
Miao Xie995946d2014-04-02 19:51:06 +080029#include "tree-log.h"
Chris Masonfec577f2007-02-26 10:40:21 -050030#include "disk-io.h"
31#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050033#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040034#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040035#include "free-space-cache.h"
Omar Sandoval1e144fb2015-09-29 20:50:37 -070036#include "free-space-tree.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060037#include "math.h"
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040038#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070039#include "qgroup.h"
Chris Masonfec577f2007-02-26 10:40:21 -050040
Arne Jansen709c0482011-09-12 12:22:57 +020041#undef SCRAMBLE_DELAYED_REFS
42
Miao Xie9e622d62012-01-26 15:01:12 -050043/*
44 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040045 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
46 * if we really need one.
47 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040048 * CHUNK_ALLOC_LIMITED means to only try and allocate one
49 * if we have very few chunks already allocated. This is
50 * used as part of the clustering code to help make sure
51 * we have a good pool of storage to cluster in, without
52 * filling the FS with empty chunks
53 *
Miao Xie9e622d62012-01-26 15:01:12 -050054 * CHUNK_ALLOC_FORCE means it must try to allocate one
55 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040056 */
57enum {
58 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050059 CHUNK_ALLOC_LIMITED = 1,
60 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040061};
62
Josef Bacikce93ec52014-11-17 15:45:48 -050063static int update_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6202df62016-06-22 18:54:22 -040064 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacikce93ec52014-11-17 15:45:48 -050065 u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040066static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040067 struct btrfs_fs_info *fs_info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080068 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040069 u64 root_objectid, u64 owner_objectid,
70 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080071 struct btrfs_delayed_extent_op *extra_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040072static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
73 struct extent_buffer *leaf,
74 struct btrfs_extent_item *ei);
75static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040076 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040077 u64 parent, u64 root_objectid,
78 u64 flags, u64 owner, u64 offset,
79 struct btrfs_key *ins, int ref_mod);
80static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040081 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040082 u64 parent, u64 root_objectid,
83 u64 flags, struct btrfs_disk_key *key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +010084 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050085static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040086 struct btrfs_fs_info *fs_info, u64 flags,
Josef Bacik698d0082012-09-12 14:08:47 -040087 int force);
Yan Zheng11833d62009-09-11 16:11:19 -040088static int find_next_key(struct btrfs_path *path, int level,
89 struct btrfs_key *key);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -040090static void dump_space_info(struct btrfs_fs_info *fs_info,
91 struct btrfs_space_info *info, u64 bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -040092 int dump_block_groups);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +080093static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
Wang Xiaoguang18513092016-07-25 15:51:40 +080094 u64 ram_bytes, u64 num_bytes, int delalloc);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +080095static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
96 u64 num_bytes, int delalloc);
Josef Bacik5d803662013-02-07 16:06:02 -050097static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
98 u64 num_bytes);
Josef Bacik957780e2016-05-17 13:30:55 -040099static int __reserve_metadata_bytes(struct btrfs_root *root,
100 struct btrfs_space_info *space_info,
101 u64 orig_bytes,
102 enum btrfs_reserve_flush_enum flush);
103static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
104 struct btrfs_space_info *space_info,
105 u64 num_bytes);
106static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
107 struct btrfs_space_info *space_info,
108 u64 num_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -0500109
Josef Bacik817d52f2009-07-13 21:29:25 -0400110static noinline int
111block_group_cache_done(struct btrfs_block_group_cache *cache)
112{
113 smp_mb();
Josef Bacik36cce922013-08-05 11:15:21 -0400114 return cache->cached == BTRFS_CACHE_FINISHED ||
115 cache->cached == BTRFS_CACHE_ERROR;
Josef Bacik817d52f2009-07-13 21:29:25 -0400116}
117
Josef Bacik0f9dd462008-09-23 13:14:11 -0400118static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
119{
120 return (cache->flags & bits) == bits;
121}
122
Filipe Manana758f2df2015-11-19 11:45:48 +0000123void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000124{
125 atomic_inc(&cache->count);
126}
127
128void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
129{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400130 if (atomic_dec_and_test(&cache->count)) {
131 WARN_ON(cache->pinned > 0);
132 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb2011-03-29 13:46:06 +0800133 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000134 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400135 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000136}
137
Josef Bacik0f9dd462008-09-23 13:14:11 -0400138/*
139 * this adds the block group to the fs_info rb tree for the block group
140 * cache
141 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500142static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400143 struct btrfs_block_group_cache *block_group)
144{
145 struct rb_node **p;
146 struct rb_node *parent = NULL;
147 struct btrfs_block_group_cache *cache;
148
149 spin_lock(&info->block_group_cache_lock);
150 p = &info->block_group_cache_tree.rb_node;
151
152 while (*p) {
153 parent = *p;
154 cache = rb_entry(parent, struct btrfs_block_group_cache,
155 cache_node);
156 if (block_group->key.objectid < cache->key.objectid) {
157 p = &(*p)->rb_left;
158 } else if (block_group->key.objectid > cache->key.objectid) {
159 p = &(*p)->rb_right;
160 } else {
161 spin_unlock(&info->block_group_cache_lock);
162 return -EEXIST;
163 }
164 }
165
166 rb_link_node(&block_group->cache_node, parent, p);
167 rb_insert_color(&block_group->cache_node,
168 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000169
170 if (info->first_logical_byte > block_group->key.objectid)
171 info->first_logical_byte = block_group->key.objectid;
172
Josef Bacik0f9dd462008-09-23 13:14:11 -0400173 spin_unlock(&info->block_group_cache_lock);
174
175 return 0;
176}
177
178/*
179 * This will return the block group at or after bytenr if contains is 0, else
180 * it will return the block group that contains the bytenr
181 */
182static struct btrfs_block_group_cache *
183block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
184 int contains)
185{
186 struct btrfs_block_group_cache *cache, *ret = NULL;
187 struct rb_node *n;
188 u64 end, start;
189
190 spin_lock(&info->block_group_cache_lock);
191 n = info->block_group_cache_tree.rb_node;
192
193 while (n) {
194 cache = rb_entry(n, struct btrfs_block_group_cache,
195 cache_node);
196 end = cache->key.objectid + cache->key.offset - 1;
197 start = cache->key.objectid;
198
199 if (bytenr < start) {
200 if (!contains && (!ret || start < ret->key.objectid))
201 ret = cache;
202 n = n->rb_left;
203 } else if (bytenr > start) {
204 if (contains && bytenr <= end) {
205 ret = cache;
206 break;
207 }
208 n = n->rb_right;
209 } else {
210 ret = cache;
211 break;
212 }
213 }
Liu Boa1897fd2012-12-27 09:01:23 +0000214 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000215 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000216 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
217 info->first_logical_byte = ret->key.objectid;
218 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400219 spin_unlock(&info->block_group_cache_lock);
220
221 return ret;
222}
223
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400224static int add_excluded_extent(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400225 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400226{
Yan Zheng11833d62009-09-11 16:11:19 -0400227 u64 end = start + num_bytes - 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400228 set_extent_bits(&fs_info->freed_extents[0],
David Sterbaceeb0ae2016-04-26 23:54:39 +0200229 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400230 set_extent_bits(&fs_info->freed_extents[1],
David Sterbaceeb0ae2016-04-26 23:54:39 +0200231 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -0400232 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400233}
234
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400235static void free_excluded_extents(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400236 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400237{
Yan Zheng11833d62009-09-11 16:11:19 -0400238 u64 start, end;
239
240 start = cache->key.objectid;
241 end = start + cache->key.offset - 1;
242
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400243 clear_extent_bits(&fs_info->freed_extents[0],
David Sterba91166212016-04-26 23:54:39 +0200244 start, end, EXTENT_UPTODATE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400245 clear_extent_bits(&fs_info->freed_extents[1],
David Sterba91166212016-04-26 23:54:39 +0200246 start, end, EXTENT_UPTODATE);
Yan Zheng11833d62009-09-11 16:11:19 -0400247}
248
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400249static int exclude_super_stripes(struct btrfs_fs_info *fs_info,
Yan Zheng11833d62009-09-11 16:11:19 -0400250 struct btrfs_block_group_cache *cache)
251{
Josef Bacik817d52f2009-07-13 21:29:25 -0400252 u64 bytenr;
253 u64 *logical;
254 int stripe_len;
255 int i, nr, ret;
256
Yan, Zheng06b23312009-11-26 09:31:11 +0000257 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
258 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
259 cache->bytes_super += stripe_len;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400260 ret = add_excluded_extent(fs_info, cache->key.objectid,
Yan, Zheng06b23312009-11-26 09:31:11 +0000261 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400262 if (ret)
263 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000264 }
265
Josef Bacik817d52f2009-07-13 21:29:25 -0400266 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
267 bytenr = btrfs_sb_offset(i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400268 ret = btrfs_rmap_block(fs_info, cache->key.objectid,
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -0400269 bytenr, 0, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400270 if (ret)
271 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400272
Josef Bacik817d52f2009-07-13 21:29:25 -0400273 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400274 u64 start, len;
275
276 if (logical[nr] > cache->key.objectid +
277 cache->key.offset)
278 continue;
279
280 if (logical[nr] + stripe_len <= cache->key.objectid)
281 continue;
282
283 start = logical[nr];
284 if (start < cache->key.objectid) {
285 start = cache->key.objectid;
286 len = (logical[nr] + stripe_len) - start;
287 } else {
288 len = min_t(u64, stripe_len,
289 cache->key.objectid +
290 cache->key.offset - start);
291 }
292
293 cache->bytes_super += len;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400294 ret = add_excluded_extent(fs_info, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400295 if (ret) {
296 kfree(logical);
297 return ret;
298 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400299 }
Yan Zheng11833d62009-09-11 16:11:19 -0400300
Josef Bacik817d52f2009-07-13 21:29:25 -0400301 kfree(logical);
302 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400303 return 0;
304}
305
Yan Zheng11833d62009-09-11 16:11:19 -0400306static struct btrfs_caching_control *
307get_caching_control(struct btrfs_block_group_cache *cache)
308{
309 struct btrfs_caching_control *ctl;
310
311 spin_lock(&cache->lock);
Josef Bacikdde5abe2010-09-16 16:17:03 -0400312 if (!cache->caching_ctl) {
313 spin_unlock(&cache->lock);
314 return NULL;
315 }
316
Yan Zheng11833d62009-09-11 16:11:19 -0400317 ctl = cache->caching_ctl;
318 atomic_inc(&ctl->count);
319 spin_unlock(&cache->lock);
320 return ctl;
321}
322
323static void put_caching_control(struct btrfs_caching_control *ctl)
324{
325 if (atomic_dec_and_test(&ctl->count))
326 kfree(ctl);
327}
328
Josef Bacikd0bd4562015-09-23 14:54:14 -0400329#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400330static void fragment_free_space(struct btrfs_block_group_cache *block_group)
Josef Bacikd0bd4562015-09-23 14:54:14 -0400331{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400332 struct btrfs_fs_info *fs_info = block_group->fs_info;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400333 u64 start = block_group->key.objectid;
334 u64 len = block_group->key.offset;
335 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400336 fs_info->nodesize : fs_info->sectorsize;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400337 u64 step = chunk << 1;
338
339 while (len > chunk) {
340 btrfs_remove_free_space(block_group, start, chunk);
341 start += step;
342 if (len < step)
343 len = 0;
344 else
345 len -= step;
346 }
347}
348#endif
349
Josef Bacik0f9dd462008-09-23 13:14:11 -0400350/*
351 * this is only called by cache_block_group, since we could have freed extents
352 * we need to check the pinned_extents for any extents that can't be used yet
353 * since their free space will be released as soon as the transaction commits.
354 */
Omar Sandovala5ed9182015-09-29 20:50:35 -0700355u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
356 struct btrfs_fs_info *info, u64 start, u64 end)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400357{
Josef Bacik817d52f2009-07-13 21:29:25 -0400358 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400359 int ret;
360
361 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400362 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400363 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400364 EXTENT_DIRTY | EXTENT_UPTODATE,
365 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400366 if (ret)
367 break;
368
Yan, Zheng06b23312009-11-26 09:31:11 +0000369 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400370 start = extent_end + 1;
371 } else if (extent_start > start && extent_start < end) {
372 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400373 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500374 ret = btrfs_add_free_space(block_group, start,
375 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100376 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400377 start = extent_end + 1;
378 } else {
379 break;
380 }
381 }
382
383 if (start < end) {
384 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400385 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500386 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100387 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400388 }
389
Josef Bacik817d52f2009-07-13 21:29:25 -0400390 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400391}
392
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700393static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
Chris Masone37c9e62007-05-09 20:13:14 -0400394{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400395 struct btrfs_block_group_cache *block_group = caching_ctl->block_group;
396 struct btrfs_fs_info *fs_info = block_group->fs_info;
397 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400398 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400399 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400400 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400401 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400402 u64 last = 0;
403 u32 nritems;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700404 int ret;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400405 bool wakeup = true;
Chris Masonf510cfe2007-10-15 16:14:48 -0400406
Chris Masone37c9e62007-05-09 20:13:14 -0400407 path = btrfs_alloc_path();
408 if (!path)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700409 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400410
Josef Bacik817d52f2009-07-13 21:29:25 -0400411 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400412
Josef Bacikd0bd4562015-09-23 14:54:14 -0400413#ifdef CONFIG_BTRFS_DEBUG
414 /*
415 * If we're fragmenting we don't want to make anybody think we can
416 * allocate from this block group until we've had a chance to fragment
417 * the free space.
418 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400419 if (btrfs_should_fragment_free_space(block_group))
Josef Bacikd0bd4562015-09-23 14:54:14 -0400420 wakeup = false;
421#endif
Chris Mason5cd57b22008-06-25 16:01:30 -0400422 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400423 * We don't want to deadlock with somebody trying to allocate a new
424 * extent for the extent root while also trying to search the extent
425 * root to add free space. So we skip locking and search the commit
426 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400427 */
428 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400429 path->search_commit_root = 1;
David Sterbae4058b52015-11-27 16:31:35 +0100430 path->reada = READA_FORWARD;
Josef Bacik817d52f2009-07-13 21:29:25 -0400431
Yan Zhenge4404d62008-12-12 10:03:26 -0500432 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400433 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400434 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400435
Liu Bo52ee28d2013-07-11 17:51:15 +0800436next:
Yan Zheng11833d62009-09-11 16:11:19 -0400437 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400438 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700439 goto out;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500440
Yan Zheng11833d62009-09-11 16:11:19 -0400441 leaf = path->nodes[0];
442 nritems = btrfs_header_nritems(leaf);
443
Chris Masond3977122009-01-05 21:25:51 -0500444 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200445 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400446 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400447 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400448 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400449
Yan Zheng11833d62009-09-11 16:11:19 -0400450 if (path->slots[0] < nritems) {
451 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
452 } else {
453 ret = find_next_key(path, 0, &key);
454 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400455 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400456
Josef Bacikc9ea7b22013-09-19 10:02:11 -0400457 if (need_resched() ||
Josef Bacik9e351cc2014-03-13 15:42:13 -0400458 rwsem_is_contended(&fs_info->commit_root_sem)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400459 if (wakeup)
460 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400461 btrfs_release_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400462 up_read(&fs_info->commit_root_sem);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400463 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400464 cond_resched();
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700465 mutex_lock(&caching_ctl->mutex);
466 down_read(&fs_info->commit_root_sem);
467 goto next;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400468 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400469
470 ret = btrfs_next_leaf(extent_root, path);
471 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700472 goto out;
Josef Bacik0a3896d2013-04-19 14:37:26 -0400473 if (ret)
474 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400475 leaf = path->nodes[0];
476 nritems = btrfs_header_nritems(leaf);
477 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400478 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400479
Liu Bo52ee28d2013-07-11 17:51:15 +0800480 if (key.objectid < last) {
481 key.objectid = last;
482 key.offset = 0;
483 key.type = BTRFS_EXTENT_ITEM_KEY;
484
Josef Bacikd0bd4562015-09-23 14:54:14 -0400485 if (wakeup)
486 caching_ctl->progress = last;
Liu Bo52ee28d2013-07-11 17:51:15 +0800487 btrfs_release_path(path);
488 goto next;
489 }
490
Yan Zheng11833d62009-09-11 16:11:19 -0400491 if (key.objectid < block_group->key.objectid) {
492 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400493 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400494 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400495
Chris Masone37c9e62007-05-09 20:13:14 -0400496 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400497 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400498 break;
Yan7d7d6062007-09-14 16:15:28 -0400499
Josef Bacik3173a182013-03-07 14:22:04 -0500500 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
501 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400502 total_found += add_new_free_space(block_group,
503 fs_info, last,
504 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500505 if (key.type == BTRFS_METADATA_ITEM_KEY)
506 last = key.objectid +
Jeff Mahoneyda170662016-06-15 09:22:56 -0400507 fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500508 else
509 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400510
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700511 if (total_found > CACHING_CTL_WAKE_UP) {
Yan Zheng11833d62009-09-11 16:11:19 -0400512 total_found = 0;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400513 if (wakeup)
514 wake_up(&caching_ctl->wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400515 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400516 }
Chris Masone37c9e62007-05-09 20:13:14 -0400517 path->slots[0]++;
518 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400519 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400520
521 total_found += add_new_free_space(block_group, fs_info, last,
522 block_group->key.objectid +
523 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400524 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400525
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700526out:
527 btrfs_free_path(path);
528 return ret;
529}
530
531static noinline void caching_thread(struct btrfs_work *work)
532{
533 struct btrfs_block_group_cache *block_group;
534 struct btrfs_fs_info *fs_info;
535 struct btrfs_caching_control *caching_ctl;
Chris Masonb4570aa2015-12-30 07:37:26 -0800536 struct btrfs_root *extent_root;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700537 int ret;
538
539 caching_ctl = container_of(work, struct btrfs_caching_control, work);
540 block_group = caching_ctl->block_group;
541 fs_info = block_group->fs_info;
Chris Masonb4570aa2015-12-30 07:37:26 -0800542 extent_root = fs_info->extent_root;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700543
544 mutex_lock(&caching_ctl->mutex);
545 down_read(&fs_info->commit_root_sem);
546
Omar Sandoval1e144fb2015-09-29 20:50:37 -0700547 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
548 ret = load_free_space_tree(caching_ctl);
549 else
550 ret = load_extent_tree_free(caching_ctl);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700551
Josef Bacik817d52f2009-07-13 21:29:25 -0400552 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400553 block_group->caching_ctl = NULL;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700554 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
Josef Bacik817d52f2009-07-13 21:29:25 -0400555 spin_unlock(&block_group->lock);
556
Josef Bacikd0bd4562015-09-23 14:54:14 -0400557#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400558 if (btrfs_should_fragment_free_space(block_group)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400559 u64 bytes_used;
560
561 spin_lock(&block_group->space_info->lock);
562 spin_lock(&block_group->lock);
563 bytes_used = block_group->key.offset -
564 btrfs_block_group_used(&block_group->item);
565 block_group->space_info->bytes_used += bytes_used >> 1;
566 spin_unlock(&block_group->lock);
567 spin_unlock(&block_group->space_info->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400568 fragment_free_space(block_group);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400569 }
570#endif
571
572 caching_ctl->progress = (u64)-1;
Chris Masonf7d3d2f2015-12-18 11:11:10 -0800573
Josef Bacik9e351cc2014-03-13 15:42:13 -0400574 up_read(&fs_info->commit_root_sem);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400575 free_excluded_extents(fs_info, block_group);
Yan Zheng11833d62009-09-11 16:11:19 -0400576 mutex_unlock(&caching_ctl->mutex);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700577
Yan Zheng11833d62009-09-11 16:11:19 -0400578 wake_up(&caching_ctl->wait);
579
580 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000581 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400582}
583
Josef Bacik9d66e232010-08-25 16:54:15 -0400584static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400585 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400586{
Josef Bacik291c7d22011-11-14 13:52:14 -0500587 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400588 struct btrfs_fs_info *fs_info = cache->fs_info;
589 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400590 int ret = 0;
591
Josef Bacik291c7d22011-11-14 13:52:14 -0500592 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100593 if (!caching_ctl)
594 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500595
596 INIT_LIST_HEAD(&caching_ctl->list);
597 mutex_init(&caching_ctl->mutex);
598 init_waitqueue_head(&caching_ctl->wait);
599 caching_ctl->block_group = cache;
600 caching_ctl->progress = cache->key.objectid;
601 atomic_set(&caching_ctl->count, 1);
Liu Bo9e0af232014-08-15 23:36:53 +0800602 btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
603 caching_thread, NULL, NULL);
Josef Bacik291c7d22011-11-14 13:52:14 -0500604
605 spin_lock(&cache->lock);
606 /*
607 * This should be a rare occasion, but this could happen I think in the
608 * case where one thread starts to load the space cache info, and then
609 * some other thread starts a transaction commit which tries to do an
610 * allocation while the other thread is still loading the space cache
611 * info. The previous loop should have kept us from choosing this block
612 * group, but if we've moved to the state where we will wait on caching
613 * block groups we need to first check if we're doing a fast load here,
614 * so we can wait for it to finish, otherwise we could end up allocating
615 * from a block group who's cache gets evicted for one reason or
616 * another.
617 */
618 while (cache->cached == BTRFS_CACHE_FAST) {
619 struct btrfs_caching_control *ctl;
620
621 ctl = cache->caching_ctl;
622 atomic_inc(&ctl->count);
623 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
624 spin_unlock(&cache->lock);
625
626 schedule();
627
628 finish_wait(&ctl->wait, &wait);
629 put_caching_control(ctl);
630 spin_lock(&cache->lock);
631 }
632
633 if (cache->cached != BTRFS_CACHE_NO) {
634 spin_unlock(&cache->lock);
635 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400636 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500637 }
638 WARN_ON(cache->caching_ctl);
639 cache->caching_ctl = caching_ctl;
640 cache->cached = BTRFS_CACHE_FAST;
641 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400642
Josef Bacikd53ba472012-04-12 16:03:57 -0400643 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500644 mutex_lock(&caching_ctl->mutex);
Josef Bacik9d66e232010-08-25 16:54:15 -0400645 ret = load_free_space_cache(fs_info, cache);
646
647 spin_lock(&cache->lock);
648 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500649 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400650 cache->cached = BTRFS_CACHE_FINISHED;
651 cache->last_byte_to_unpin = (u64)-1;
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500652 caching_ctl->progress = (u64)-1;
Josef Bacik9d66e232010-08-25 16:54:15 -0400653 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500654 if (load_cache_only) {
655 cache->caching_ctl = NULL;
656 cache->cached = BTRFS_CACHE_NO;
657 } else {
658 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000659 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500660 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400661 }
662 spin_unlock(&cache->lock);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400663#ifdef CONFIG_BTRFS_DEBUG
664 if (ret == 1 &&
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400665 btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400666 u64 bytes_used;
667
668 spin_lock(&cache->space_info->lock);
669 spin_lock(&cache->lock);
670 bytes_used = cache->key.offset -
671 btrfs_block_group_used(&cache->item);
672 cache->space_info->bytes_used += bytes_used >> 1;
673 spin_unlock(&cache->lock);
674 spin_unlock(&cache->space_info->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400675 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400676 }
677#endif
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500678 mutex_unlock(&caching_ctl->mutex);
679
Josef Bacik291c7d22011-11-14 13:52:14 -0500680 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000681 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500682 put_caching_control(caching_ctl);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400683 free_excluded_extents(fs_info, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400684 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000685 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500686 } else {
687 /*
Omar Sandoval1e144fb2015-09-29 20:50:37 -0700688 * We're either using the free space tree or no caching at all.
689 * Set cached to the appropriate value and wakeup any waiters.
Josef Bacik291c7d22011-11-14 13:52:14 -0500690 */
691 spin_lock(&cache->lock);
692 if (load_cache_only) {
693 cache->caching_ctl = NULL;
694 cache->cached = BTRFS_CACHE_NO;
695 } else {
696 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000697 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500698 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400699 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500700 wake_up(&caching_ctl->wait);
701 }
702
703 if (load_cache_only) {
704 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400705 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400706 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400707
Josef Bacik9e351cc2014-03-13 15:42:13 -0400708 down_write(&fs_info->commit_root_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500709 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400710 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400711 up_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -0400712
Josef Bacik11dfe352009-11-13 20:12:59 +0000713 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400714
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800715 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400716
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400717 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400718}
719
Josef Bacik0f9dd462008-09-23 13:14:11 -0400720/*
721 * return the block group that starts at or after bytenr
722 */
Chris Masond3977122009-01-05 21:25:51 -0500723static struct btrfs_block_group_cache *
724btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400725{
Masahiro Yamadae2c89902016-09-13 04:35:52 +0900726 return block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400727}
728
Josef Bacik0f9dd462008-09-23 13:14:11 -0400729/*
Sankar P9f556842009-05-14 13:52:22 -0400730 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400731 */
Chris Masond3977122009-01-05 21:25:51 -0500732struct btrfs_block_group_cache *btrfs_lookup_block_group(
733 struct btrfs_fs_info *info,
734 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400735{
Masahiro Yamadae2c89902016-09-13 04:35:52 +0900736 return block_group_cache_tree_search(info, bytenr, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400737}
Chris Mason0b86a832008-03-24 15:01:56 -0400738
Josef Bacik0f9dd462008-09-23 13:14:11 -0400739static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
740 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400741{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400742 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400743 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400744
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200745 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400746
Chris Mason4184ea72009-03-10 12:39:20 -0400747 rcu_read_lock();
748 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400749 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400750 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400751 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400752 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400753 }
Chris Mason4184ea72009-03-10 12:39:20 -0400754 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400755 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400756}
757
Chris Mason4184ea72009-03-10 12:39:20 -0400758/*
759 * after adding space to the filesystem, we need to clear the full flags
760 * on all the space infos.
761 */
762void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
763{
764 struct list_head *head = &info->space_info;
765 struct btrfs_space_info *found;
766
767 rcu_read_lock();
768 list_for_each_entry_rcu(found, head, list)
769 found->full = 0;
770 rcu_read_unlock();
771}
772
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000773/* simple helper to search for an existing data extent at a given offset */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400774int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400775{
776 int ret;
777 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400778 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400779
Zheng Yan31840ae2008-09-23 13:14:14 -0400780 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700781 if (!path)
782 return -ENOMEM;
783
Chris Masone02119d2008-09-05 16:13:11 -0400784 key.objectid = start;
785 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500786 key.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400787 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400788 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500789 return ret;
790}
791
Chris Masond8d5f3e2007-12-11 12:42:00 -0500792/*
Josef Bacik3173a182013-03-07 14:22:04 -0500793 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400794 *
795 * the head node for delayed ref is used to store the sum of all the
796 * reference count modifications queued up in the rbtree. the head
797 * node may also store the extent flags to set. This way you can check
798 * to see what the reference count and extent flags would be if all of
799 * the delayed refs are not processed.
800 */
801int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400802 struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500803 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400804{
805 struct btrfs_delayed_ref_head *head;
806 struct btrfs_delayed_ref_root *delayed_refs;
807 struct btrfs_path *path;
808 struct btrfs_extent_item *ei;
809 struct extent_buffer *leaf;
810 struct btrfs_key key;
811 u32 item_size;
812 u64 num_refs;
813 u64 extent_flags;
814 int ret;
815
Josef Bacik3173a182013-03-07 14:22:04 -0500816 /*
817 * If we don't have skinny metadata, don't bother doing anything
818 * different
819 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400820 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
821 offset = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500822 metadata = 0;
823 }
824
Yan, Zhenga22285a2010-05-16 10:48:46 -0400825 path = btrfs_alloc_path();
826 if (!path)
827 return -ENOMEM;
828
Yan, Zhenga22285a2010-05-16 10:48:46 -0400829 if (!trans) {
830 path->skip_locking = 1;
831 path->search_commit_root = 1;
832 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000833
834search_again:
835 key.objectid = bytenr;
836 key.offset = offset;
837 if (metadata)
838 key.type = BTRFS_METADATA_ITEM_KEY;
839 else
840 key.type = BTRFS_EXTENT_ITEM_KEY;
841
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400842 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400843 if (ret < 0)
844 goto out_free;
845
Josef Bacik3173a182013-03-07 14:22:04 -0500846 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100847 if (path->slots[0]) {
848 path->slots[0]--;
849 btrfs_item_key_to_cpu(path->nodes[0], &key,
850 path->slots[0]);
851 if (key.objectid == bytenr &&
852 key.type == BTRFS_EXTENT_ITEM_KEY &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400853 key.offset == fs_info->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100854 ret = 0;
855 }
Josef Bacik3173a182013-03-07 14:22:04 -0500856 }
857
Yan, Zhenga22285a2010-05-16 10:48:46 -0400858 if (ret == 0) {
859 leaf = path->nodes[0];
860 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
861 if (item_size >= sizeof(*ei)) {
862 ei = btrfs_item_ptr(leaf, path->slots[0],
863 struct btrfs_extent_item);
864 num_refs = btrfs_extent_refs(leaf, ei);
865 extent_flags = btrfs_extent_flags(leaf, ei);
866 } else {
867#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
868 struct btrfs_extent_item_v0 *ei0;
869 BUG_ON(item_size != sizeof(*ei0));
870 ei0 = btrfs_item_ptr(leaf, path->slots[0],
871 struct btrfs_extent_item_v0);
872 num_refs = btrfs_extent_refs_v0(leaf, ei0);
873 /* FIXME: this isn't correct for data */
874 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
875#else
876 BUG();
877#endif
878 }
879 BUG_ON(num_refs == 0);
880 } else {
881 num_refs = 0;
882 extent_flags = 0;
883 ret = 0;
884 }
885
886 if (!trans)
887 goto out;
888
889 delayed_refs = &trans->transaction->delayed_refs;
890 spin_lock(&delayed_refs->lock);
891 head = btrfs_find_delayed_ref_head(trans, bytenr);
892 if (head) {
893 if (!mutex_trylock(&head->mutex)) {
894 atomic_inc(&head->node.refs);
895 spin_unlock(&delayed_refs->lock);
896
David Sterbab3b4aa72011-04-21 01:20:15 +0200897 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400898
David Sterba8cc33e52011-05-02 15:29:25 +0200899 /*
900 * Mutex was contended, block until it's released and try
901 * again
902 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400903 mutex_lock(&head->mutex);
904 mutex_unlock(&head->mutex);
905 btrfs_put_delayed_ref(&head->node);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000906 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400907 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500908 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400909 if (head->extent_op && head->extent_op->update_flags)
910 extent_flags |= head->extent_op->flags_to_set;
911 else
912 BUG_ON(num_refs == 0);
913
914 num_refs += head->node.ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500915 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400916 mutex_unlock(&head->mutex);
917 }
918 spin_unlock(&delayed_refs->lock);
919out:
920 WARN_ON(num_refs == 0);
921 if (refs)
922 *refs = num_refs;
923 if (flags)
924 *flags = extent_flags;
925out_free:
926 btrfs_free_path(path);
927 return ret;
928}
929
930/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500931 * Back reference rules. Back refs have three main goals:
932 *
933 * 1) differentiate between all holders of references to an extent so that
934 * when a reference is dropped we can make sure it was a valid reference
935 * before freeing the extent.
936 *
937 * 2) Provide enough information to quickly find the holders of an extent
938 * if we notice a given block is corrupted or bad.
939 *
940 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
941 * maintenance. This is actually the same as #2, but with a slightly
942 * different use case.
943 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400944 * There are two kinds of back refs. The implicit back refs is optimized
945 * for pointers in non-shared tree blocks. For a given pointer in a block,
946 * back refs of this kind provide information about the block's owner tree
947 * and the pointer's key. These information allow us to find the block by
948 * b-tree searching. The full back refs is for pointers in tree blocks not
949 * referenced by their owner trees. The location of tree block is recorded
950 * in the back refs. Actually the full back refs is generic, and can be
951 * used in all cases the implicit back refs is used. The major shortcoming
952 * of the full back refs is its overhead. Every time a tree block gets
953 * COWed, we have to update back refs entry for all pointers in it.
954 *
955 * For a newly allocated tree block, we use implicit back refs for
956 * pointers in it. This means most tree related operations only involve
957 * implicit back refs. For a tree block created in old transaction, the
958 * only way to drop a reference to it is COW it. So we can detect the
959 * event that tree block loses its owner tree's reference and do the
960 * back refs conversion.
961 *
Nicholas D Steeves01327612016-05-19 21:18:45 -0400962 * When a tree block is COWed through a tree, there are four cases:
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400963 *
964 * The reference count of the block is one and the tree is the block's
965 * owner tree. Nothing to do in this case.
966 *
967 * The reference count of the block is one and the tree is not the
968 * block's owner tree. In this case, full back refs is used for pointers
969 * in the block. Remove these full back refs, add implicit back refs for
970 * every pointers in the new block.
971 *
972 * The reference count of the block is greater than one and the tree is
973 * the block's owner tree. In this case, implicit back refs is used for
974 * pointers in the block. Add full back refs for every pointers in the
975 * block, increase lower level extents' reference counts. The original
976 * implicit back refs are entailed to the new block.
977 *
978 * The reference count of the block is greater than one and the tree is
979 * not the block's owner tree. Add implicit back refs for every pointer in
980 * the new block, increase lower level extents' reference count.
981 *
982 * Back Reference Key composing:
983 *
984 * The key objectid corresponds to the first byte in the extent,
985 * The key type is used to differentiate between types of back refs.
986 * There are different meanings of the key offset for different types
987 * of back refs.
988 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500989 * File extents can be referenced by:
990 *
991 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400992 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500993 * - different offsets inside a file (bookend extents in file.c)
994 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500996 *
997 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500998 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400999 * - original offset in the file
1000 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -04001001 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001002 * The key offset for the implicit back refs is hash of the first
1003 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001004 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001005 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001006 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001007 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001008 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001009 * The key offset for the implicit back refs is the first byte of
1010 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001011 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001012 * When a file extent is allocated, The implicit back refs is used.
1013 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001014 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001015 * (root_key.objectid, inode objectid, offset in file, 1)
1016 *
1017 * When a file extent is removed file truncation, we find the
1018 * corresponding implicit back refs and check the following fields:
1019 *
1020 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -05001021 *
1022 * Btree extents can be referenced by:
1023 *
1024 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -05001025 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001026 * Both the implicit back refs and the full back refs for tree blocks
1027 * only consist of key. The key offset for the implicit back refs is
1028 * objectid of block's owner tree. The key offset for the full back refs
1029 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001030 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001031 * When implicit back refs is used, information about the lowest key and
1032 * level of the tree block are required. These information are stored in
1033 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001034 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001035
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001036#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1037static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
1038 struct btrfs_root *root,
1039 struct btrfs_path *path,
1040 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -05001041{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001042 struct btrfs_extent_item *item;
1043 struct btrfs_extent_item_v0 *ei0;
1044 struct btrfs_extent_ref_v0 *ref0;
1045 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -04001046 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001047 struct btrfs_key key;
1048 struct btrfs_key found_key;
1049 u32 new_size = sizeof(*item);
1050 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -05001051 int ret;
1052
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001053 leaf = path->nodes[0];
1054 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -05001055
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001056 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1057 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1058 struct btrfs_extent_item_v0);
1059 refs = btrfs_extent_refs_v0(leaf, ei0);
1060
1061 if (owner == (u64)-1) {
1062 while (1) {
1063 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1064 ret = btrfs_next_leaf(root, path);
1065 if (ret < 0)
1066 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001067 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001068 leaf = path->nodes[0];
1069 }
1070 btrfs_item_key_to_cpu(leaf, &found_key,
1071 path->slots[0]);
1072 BUG_ON(key.objectid != found_key.objectid);
1073 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1074 path->slots[0]++;
1075 continue;
1076 }
1077 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1078 struct btrfs_extent_ref_v0);
1079 owner = btrfs_ref_objectid_v0(leaf, ref0);
1080 break;
1081 }
1082 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001083 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001084
1085 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1086 new_size += sizeof(*bi);
1087
1088 new_size -= sizeof(*ei0);
1089 ret = btrfs_search_slot(trans, root, &key, path,
1090 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001091 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001092 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001093 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001094
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001095 btrfs_extend_item(root->fs_info, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001096
1097 leaf = path->nodes[0];
1098 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1099 btrfs_set_extent_refs(leaf, item, refs);
1100 /* FIXME: get real generation */
1101 btrfs_set_extent_generation(leaf, item, 0);
1102 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1103 btrfs_set_extent_flags(leaf, item,
1104 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1105 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1106 bi = (struct btrfs_tree_block_info *)(item + 1);
1107 /* FIXME: get first key of the block */
David Sterbab159fa22016-11-08 18:09:03 +01001108 memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001109 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1110 } else {
1111 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1112 }
1113 btrfs_mark_buffer_dirty(leaf);
1114 return 0;
1115}
1116#endif
1117
1118static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1119{
1120 u32 high_crc = ~(u32)0;
1121 u32 low_crc = ~(u32)0;
1122 __le64 lenum;
1123
1124 lenum = cpu_to_le64(root_objectid);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001125 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001126 lenum = cpu_to_le64(owner);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001127 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001128 lenum = cpu_to_le64(offset);
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
1131 return ((u64)high_crc << 31) ^ (u64)low_crc;
1132}
1133
1134static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1135 struct btrfs_extent_data_ref *ref)
1136{
1137 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1138 btrfs_extent_data_ref_objectid(leaf, ref),
1139 btrfs_extent_data_ref_offset(leaf, ref));
1140}
1141
1142static int match_extent_data_ref(struct extent_buffer *leaf,
1143 struct btrfs_extent_data_ref *ref,
1144 u64 root_objectid, u64 owner, u64 offset)
1145{
1146 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1147 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1148 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1149 return 0;
1150 return 1;
1151}
1152
1153static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1154 struct btrfs_root *root,
1155 struct btrfs_path *path,
1156 u64 bytenr, u64 parent,
1157 u64 root_objectid,
1158 u64 owner, u64 offset)
1159{
1160 struct btrfs_key key;
1161 struct btrfs_extent_data_ref *ref;
1162 struct extent_buffer *leaf;
1163 u32 nritems;
1164 int ret;
1165 int recow;
1166 int err = -ENOENT;
1167
1168 key.objectid = bytenr;
1169 if (parent) {
1170 key.type = BTRFS_SHARED_DATA_REF_KEY;
1171 key.offset = parent;
1172 } else {
1173 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1174 key.offset = hash_extent_data_ref(root_objectid,
1175 owner, offset);
1176 }
1177again:
1178 recow = 0;
1179 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1180 if (ret < 0) {
1181 err = ret;
1182 goto fail;
1183 }
1184
1185 if (parent) {
1186 if (!ret)
1187 return 0;
1188#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1189 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001190 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001191 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1192 if (ret < 0) {
1193 err = ret;
1194 goto fail;
1195 }
1196 if (!ret)
1197 return 0;
1198#endif
1199 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001200 }
1201
1202 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001203 nritems = btrfs_header_nritems(leaf);
1204 while (1) {
1205 if (path->slots[0] >= nritems) {
1206 ret = btrfs_next_leaf(root, path);
1207 if (ret < 0)
1208 err = ret;
1209 if (ret)
1210 goto fail;
1211
1212 leaf = path->nodes[0];
1213 nritems = btrfs_header_nritems(leaf);
1214 recow = 1;
1215 }
1216
1217 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1218 if (key.objectid != bytenr ||
1219 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1220 goto fail;
1221
1222 ref = btrfs_item_ptr(leaf, path->slots[0],
1223 struct btrfs_extent_data_ref);
1224
1225 if (match_extent_data_ref(leaf, ref, root_objectid,
1226 owner, offset)) {
1227 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001228 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001229 goto again;
1230 }
1231 err = 0;
1232 break;
1233 }
1234 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001235 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001236fail:
1237 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001238}
1239
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001240static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1241 struct btrfs_root *root,
1242 struct btrfs_path *path,
1243 u64 bytenr, u64 parent,
1244 u64 root_objectid, u64 owner,
1245 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001246{
1247 struct btrfs_key key;
1248 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001249 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001250 u32 num_refs;
1251 int ret;
1252
1253 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001254 if (parent) {
1255 key.type = BTRFS_SHARED_DATA_REF_KEY;
1256 key.offset = parent;
1257 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001258 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001259 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1260 key.offset = hash_extent_data_ref(root_objectid,
1261 owner, offset);
1262 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001263 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001264
1265 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1266 if (ret && ret != -EEXIST)
1267 goto fail;
1268
1269 leaf = path->nodes[0];
1270 if (parent) {
1271 struct btrfs_shared_data_ref *ref;
1272 ref = btrfs_item_ptr(leaf, path->slots[0],
1273 struct btrfs_shared_data_ref);
1274 if (ret == 0) {
1275 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1276 } else {
1277 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1278 num_refs += refs_to_add;
1279 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1280 }
1281 } else {
1282 struct btrfs_extent_data_ref *ref;
1283 while (ret == -EEXIST) {
1284 ref = btrfs_item_ptr(leaf, path->slots[0],
1285 struct btrfs_extent_data_ref);
1286 if (match_extent_data_ref(leaf, ref, root_objectid,
1287 owner, offset))
1288 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001289 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001290 key.offset++;
1291 ret = btrfs_insert_empty_item(trans, root, path, &key,
1292 size);
1293 if (ret && ret != -EEXIST)
1294 goto fail;
1295
1296 leaf = path->nodes[0];
1297 }
1298 ref = btrfs_item_ptr(leaf, path->slots[0],
1299 struct btrfs_extent_data_ref);
1300 if (ret == 0) {
1301 btrfs_set_extent_data_ref_root(leaf, ref,
1302 root_objectid);
1303 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1304 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1305 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1306 } else {
1307 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1308 num_refs += refs_to_add;
1309 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1310 }
1311 }
1312 btrfs_mark_buffer_dirty(leaf);
1313 ret = 0;
1314fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001315 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001316 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001317}
1318
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001319static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1320 struct btrfs_root *root,
1321 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -07001322 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001323{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001324 struct btrfs_key key;
1325 struct btrfs_extent_data_ref *ref1 = NULL;
1326 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001327 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001328 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001329 int ret = 0;
1330
1331 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001332 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1333
1334 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1335 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1336 struct btrfs_extent_data_ref);
1337 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1338 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1339 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1340 struct btrfs_shared_data_ref);
1341 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1342#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1343 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1344 struct btrfs_extent_ref_v0 *ref0;
1345 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1346 struct btrfs_extent_ref_v0);
1347 num_refs = btrfs_ref_count_v0(leaf, ref0);
1348#endif
1349 } else {
1350 BUG();
1351 }
1352
Chris Mason56bec292009-03-13 10:10:06 -04001353 BUG_ON(num_refs < refs_to_drop);
1354 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001355
Zheng Yan31840ae2008-09-23 13:14:14 -04001356 if (num_refs == 0) {
1357 ret = btrfs_del_item(trans, root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -07001358 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001359 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001360 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1361 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1362 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1363 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1364#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1365 else {
1366 struct btrfs_extent_ref_v0 *ref0;
1367 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1368 struct btrfs_extent_ref_v0);
1369 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1370 }
1371#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001372 btrfs_mark_buffer_dirty(leaf);
1373 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001374 return ret;
1375}
1376
Zhaolei9ed0dea2015-08-06 22:16:24 +08001377static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001378 struct btrfs_extent_inline_ref *iref)
1379{
1380 struct btrfs_key key;
1381 struct extent_buffer *leaf;
1382 struct btrfs_extent_data_ref *ref1;
1383 struct btrfs_shared_data_ref *ref2;
1384 u32 num_refs = 0;
1385
1386 leaf = path->nodes[0];
1387 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1388 if (iref) {
1389 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1390 BTRFS_EXTENT_DATA_REF_KEY) {
1391 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1392 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1393 } else {
1394 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1395 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1396 }
1397 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1398 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1399 struct btrfs_extent_data_ref);
1400 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1401 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1402 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1403 struct btrfs_shared_data_ref);
1404 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1405#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1406 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1407 struct btrfs_extent_ref_v0 *ref0;
1408 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1409 struct btrfs_extent_ref_v0);
1410 num_refs = btrfs_ref_count_v0(leaf, ref0);
1411#endif
1412 } else {
1413 WARN_ON(1);
1414 }
1415 return num_refs;
1416}
1417
1418static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1419 struct btrfs_root *root,
1420 struct btrfs_path *path,
1421 u64 bytenr, u64 parent,
1422 u64 root_objectid)
1423{
1424 struct btrfs_key key;
1425 int ret;
1426
1427 key.objectid = bytenr;
1428 if (parent) {
1429 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1430 key.offset = parent;
1431 } else {
1432 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1433 key.offset = root_objectid;
1434 }
1435
1436 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1437 if (ret > 0)
1438 ret = -ENOENT;
1439#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1440 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001441 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001442 key.type = BTRFS_EXTENT_REF_V0_KEY;
1443 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1444 if (ret > 0)
1445 ret = -ENOENT;
1446 }
1447#endif
1448 return ret;
1449}
1450
1451static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1452 struct btrfs_root *root,
1453 struct btrfs_path *path,
1454 u64 bytenr, u64 parent,
1455 u64 root_objectid)
1456{
1457 struct btrfs_key key;
1458 int ret;
1459
1460 key.objectid = bytenr;
1461 if (parent) {
1462 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1463 key.offset = parent;
1464 } else {
1465 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1466 key.offset = root_objectid;
1467 }
1468
1469 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001470 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001471 return ret;
1472}
1473
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001474static inline int extent_ref_type(u64 parent, u64 owner)
1475{
1476 int type;
1477 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1478 if (parent > 0)
1479 type = BTRFS_SHARED_BLOCK_REF_KEY;
1480 else
1481 type = BTRFS_TREE_BLOCK_REF_KEY;
1482 } else {
1483 if (parent > 0)
1484 type = BTRFS_SHARED_DATA_REF_KEY;
1485 else
1486 type = BTRFS_EXTENT_DATA_REF_KEY;
1487 }
1488 return type;
1489}
1490
Yan Zheng2c47e6052009-06-27 21:07:35 -04001491static int find_next_key(struct btrfs_path *path, int level,
1492 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001493
1494{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001495 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001496 if (!path->nodes[level])
1497 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001498 if (path->slots[level] + 1 >=
1499 btrfs_header_nritems(path->nodes[level]))
1500 continue;
1501 if (level == 0)
1502 btrfs_item_key_to_cpu(path->nodes[level], key,
1503 path->slots[level] + 1);
1504 else
1505 btrfs_node_key_to_cpu(path->nodes[level], key,
1506 path->slots[level] + 1);
1507 return 0;
1508 }
1509 return 1;
1510}
1511
1512/*
1513 * look for inline back ref. if back ref is found, *ref_ret is set
1514 * to the address of inline back ref, and 0 is returned.
1515 *
1516 * if back ref isn't found, *ref_ret is set to the address where it
1517 * should be inserted, and -ENOENT is returned.
1518 *
1519 * if insert is true and there are too many inline back refs, the path
1520 * points to the extent item, and -EAGAIN is returned.
1521 *
1522 * NOTE: inline back refs are ordered in the same way that back ref
1523 * items in the tree are ordered.
1524 */
1525static noinline_for_stack
1526int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1527 struct btrfs_root *root,
1528 struct btrfs_path *path,
1529 struct btrfs_extent_inline_ref **ref_ret,
1530 u64 bytenr, u64 num_bytes,
1531 u64 parent, u64 root_objectid,
1532 u64 owner, u64 offset, int insert)
1533{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001534 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001535 struct btrfs_key key;
1536 struct extent_buffer *leaf;
1537 struct btrfs_extent_item *ei;
1538 struct btrfs_extent_inline_ref *iref;
1539 u64 flags;
1540 u64 item_size;
1541 unsigned long ptr;
1542 unsigned long end;
1543 int extra_size;
1544 int type;
1545 int want;
1546 int ret;
1547 int err = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001548 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001549
1550 key.objectid = bytenr;
1551 key.type = BTRFS_EXTENT_ITEM_KEY;
1552 key.offset = num_bytes;
1553
1554 want = extent_ref_type(parent, owner);
1555 if (insert) {
1556 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001557 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001558 } else
1559 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001560
1561 /*
1562 * Owner is our parent level, so we can just add one to get the level
1563 * for the block we are interested in.
1564 */
1565 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1566 key.type = BTRFS_METADATA_ITEM_KEY;
1567 key.offset = owner;
1568 }
1569
1570again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001571 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1572 if (ret < 0) {
1573 err = ret;
1574 goto out;
1575 }
Josef Bacik3173a182013-03-07 14:22:04 -05001576
1577 /*
1578 * We may be a newly converted file system which still has the old fat
1579 * extent entries for metadata, so try and see if we have one of those.
1580 */
1581 if (ret > 0 && skinny_metadata) {
1582 skinny_metadata = false;
1583 if (path->slots[0]) {
1584 path->slots[0]--;
1585 btrfs_item_key_to_cpu(path->nodes[0], &key,
1586 path->slots[0]);
1587 if (key.objectid == bytenr &&
1588 key.type == BTRFS_EXTENT_ITEM_KEY &&
1589 key.offset == num_bytes)
1590 ret = 0;
1591 }
1592 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +01001593 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05001594 key.type = BTRFS_EXTENT_ITEM_KEY;
1595 key.offset = num_bytes;
1596 btrfs_release_path(path);
1597 goto again;
1598 }
1599 }
1600
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001601 if (ret && !insert) {
1602 err = -ENOENT;
1603 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301604 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -05001605 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -05001606 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001607 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001608
1609 leaf = path->nodes[0];
1610 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1611#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1612 if (item_size < sizeof(*ei)) {
1613 if (!insert) {
1614 err = -ENOENT;
1615 goto out;
1616 }
1617 ret = convert_extent_item_v0(trans, root, path, owner,
1618 extra_size);
1619 if (ret < 0) {
1620 err = ret;
1621 goto out;
1622 }
1623 leaf = path->nodes[0];
1624 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1625 }
1626#endif
1627 BUG_ON(item_size < sizeof(*ei));
1628
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001629 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1630 flags = btrfs_extent_flags(leaf, ei);
1631
1632 ptr = (unsigned long)(ei + 1);
1633 end = (unsigned long)ei + item_size;
1634
Josef Bacik3173a182013-03-07 14:22:04 -05001635 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001636 ptr += sizeof(struct btrfs_tree_block_info);
1637 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001638 }
1639
1640 err = -ENOENT;
1641 while (1) {
1642 if (ptr >= end) {
1643 WARN_ON(ptr > end);
1644 break;
1645 }
1646 iref = (struct btrfs_extent_inline_ref *)ptr;
1647 type = btrfs_extent_inline_ref_type(leaf, iref);
1648 if (want < type)
1649 break;
1650 if (want > type) {
1651 ptr += btrfs_extent_inline_ref_size(type);
1652 continue;
1653 }
1654
1655 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1656 struct btrfs_extent_data_ref *dref;
1657 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1658 if (match_extent_data_ref(leaf, dref, root_objectid,
1659 owner, offset)) {
1660 err = 0;
1661 break;
1662 }
1663 if (hash_extent_data_ref_item(leaf, dref) <
1664 hash_extent_data_ref(root_objectid, owner, offset))
1665 break;
1666 } else {
1667 u64 ref_offset;
1668 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1669 if (parent > 0) {
1670 if (parent == ref_offset) {
1671 err = 0;
1672 break;
1673 }
1674 if (ref_offset < parent)
1675 break;
1676 } else {
1677 if (root_objectid == ref_offset) {
1678 err = 0;
1679 break;
1680 }
1681 if (ref_offset < root_objectid)
1682 break;
1683 }
1684 }
1685 ptr += btrfs_extent_inline_ref_size(type);
1686 }
1687 if (err == -ENOENT && insert) {
1688 if (item_size + extra_size >=
1689 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1690 err = -EAGAIN;
1691 goto out;
1692 }
1693 /*
1694 * To add new inline back ref, we have to make sure
1695 * there is no corresponding back ref item.
1696 * For simplicity, we just do not add new inline back
1697 * ref if there is any kind of item for this block
1698 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001699 if (find_next_key(path, 0, &key) == 0 &&
1700 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001701 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001702 err = -EAGAIN;
1703 goto out;
1704 }
1705 }
1706 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1707out:
Yan Zheng85d41982009-06-11 08:51:10 -04001708 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001709 path->keep_locks = 0;
1710 btrfs_unlock_up_safe(path, 1);
1711 }
1712 return err;
1713}
1714
1715/*
1716 * helper to add new inline back ref
1717 */
1718static noinline_for_stack
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001719void setup_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001720 struct btrfs_path *path,
1721 struct btrfs_extent_inline_ref *iref,
1722 u64 parent, u64 root_objectid,
1723 u64 owner, u64 offset, int refs_to_add,
1724 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001725{
1726 struct extent_buffer *leaf;
1727 struct btrfs_extent_item *ei;
1728 unsigned long ptr;
1729 unsigned long end;
1730 unsigned long item_offset;
1731 u64 refs;
1732 int size;
1733 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001734
1735 leaf = path->nodes[0];
1736 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1737 item_offset = (unsigned long)iref - (unsigned long)ei;
1738
1739 type = extent_ref_type(parent, owner);
1740 size = btrfs_extent_inline_ref_size(type);
1741
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001742 btrfs_extend_item(root->fs_info, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001743
1744 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1745 refs = btrfs_extent_refs(leaf, ei);
1746 refs += refs_to_add;
1747 btrfs_set_extent_refs(leaf, ei, refs);
1748 if (extent_op)
1749 __run_delayed_extent_op(extent_op, leaf, ei);
1750
1751 ptr = (unsigned long)ei + item_offset;
1752 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1753 if (ptr < end - size)
1754 memmove_extent_buffer(leaf, ptr + size, ptr,
1755 end - size - ptr);
1756
1757 iref = (struct btrfs_extent_inline_ref *)ptr;
1758 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1759 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1760 struct btrfs_extent_data_ref *dref;
1761 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1762 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1763 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1764 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1765 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1766 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1767 struct btrfs_shared_data_ref *sref;
1768 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1769 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1770 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1771 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1772 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1773 } else {
1774 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1775 }
1776 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001777}
1778
1779static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1780 struct btrfs_root *root,
1781 struct btrfs_path *path,
1782 struct btrfs_extent_inline_ref **ref_ret,
1783 u64 bytenr, u64 num_bytes, u64 parent,
1784 u64 root_objectid, u64 owner, u64 offset)
1785{
1786 int ret;
1787
1788 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1789 bytenr, num_bytes, parent,
1790 root_objectid, owner, offset, 0);
1791 if (ret != -ENOENT)
1792 return ret;
1793
David Sterbab3b4aa72011-04-21 01:20:15 +02001794 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001795 *ref_ret = NULL;
1796
1797 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1798 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1799 root_objectid);
1800 } else {
1801 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1802 root_objectid, owner, offset);
1803 }
1804 return ret;
1805}
1806
1807/*
1808 * helper to update/remove inline back ref
1809 */
1810static noinline_for_stack
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001811void update_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001812 struct btrfs_path *path,
1813 struct btrfs_extent_inline_ref *iref,
1814 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001815 struct btrfs_delayed_extent_op *extent_op,
1816 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001817{
1818 struct extent_buffer *leaf;
1819 struct btrfs_extent_item *ei;
1820 struct btrfs_extent_data_ref *dref = NULL;
1821 struct btrfs_shared_data_ref *sref = NULL;
1822 unsigned long ptr;
1823 unsigned long end;
1824 u32 item_size;
1825 int size;
1826 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001827 u64 refs;
1828
1829 leaf = path->nodes[0];
1830 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1831 refs = btrfs_extent_refs(leaf, ei);
1832 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1833 refs += refs_to_mod;
1834 btrfs_set_extent_refs(leaf, ei, refs);
1835 if (extent_op)
1836 __run_delayed_extent_op(extent_op, leaf, ei);
1837
1838 type = btrfs_extent_inline_ref_type(leaf, iref);
1839
1840 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1841 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1842 refs = btrfs_extent_data_ref_count(leaf, dref);
1843 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1844 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1845 refs = btrfs_shared_data_ref_count(leaf, sref);
1846 } else {
1847 refs = 1;
1848 BUG_ON(refs_to_mod != -1);
1849 }
1850
1851 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1852 refs += refs_to_mod;
1853
1854 if (refs > 0) {
1855 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1856 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1857 else
1858 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1859 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001860 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001861 size = btrfs_extent_inline_ref_size(type);
1862 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1863 ptr = (unsigned long)iref;
1864 end = (unsigned long)ei + item_size;
1865 if (ptr + size < end)
1866 memmove_extent_buffer(leaf, ptr, ptr + size,
1867 end - ptr - size);
1868 item_size -= size;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001869 btrfs_truncate_item(root->fs_info, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870 }
1871 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001872}
1873
1874static noinline_for_stack
1875int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1876 struct btrfs_root *root,
1877 struct btrfs_path *path,
1878 u64 bytenr, u64 num_bytes, u64 parent,
1879 u64 root_objectid, u64 owner,
1880 u64 offset, int refs_to_add,
1881 struct btrfs_delayed_extent_op *extent_op)
1882{
1883 struct btrfs_extent_inline_ref *iref;
1884 int ret;
1885
1886 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1887 bytenr, num_bytes, parent,
1888 root_objectid, owner, offset, 1);
1889 if (ret == 0) {
1890 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001891 update_inline_extent_backref(root, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001892 refs_to_add, extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001893 } else if (ret == -ENOENT) {
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001894 setup_inline_extent_backref(root, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001895 root_objectid, owner, offset,
1896 refs_to_add, extent_op);
1897 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001898 }
1899 return ret;
1900}
1901
1902static int insert_extent_backref(struct btrfs_trans_handle *trans,
1903 struct btrfs_root *root,
1904 struct btrfs_path *path,
1905 u64 bytenr, u64 parent, u64 root_objectid,
1906 u64 owner, u64 offset, int refs_to_add)
1907{
1908 int ret;
1909 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1910 BUG_ON(refs_to_add != 1);
1911 ret = insert_tree_block_ref(trans, root, path, bytenr,
1912 parent, root_objectid);
1913 } else {
1914 ret = insert_extent_data_ref(trans, root, path, bytenr,
1915 parent, root_objectid,
1916 owner, offset, refs_to_add);
1917 }
1918 return ret;
1919}
1920
1921static int remove_extent_backref(struct btrfs_trans_handle *trans,
1922 struct btrfs_root *root,
1923 struct btrfs_path *path,
1924 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001925 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001926{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001927 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001928
1929 BUG_ON(!is_data && refs_to_drop != 1);
1930 if (iref) {
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001931 update_inline_extent_backref(root, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001932 -refs_to_drop, NULL, last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001933 } else if (is_data) {
Josef Bacikfcebe452014-05-13 17:30:47 -07001934 ret = remove_extent_data_ref(trans, root, path, refs_to_drop,
1935 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001936 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001937 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001938 ret = btrfs_del_item(trans, root, path);
1939 }
1940 return ret;
1941}
1942
Jeff Mahoney86557862015-06-15 09:41:16 -04001943#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001944static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
1945 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05001946{
Jeff Mahoney86557862015-06-15 09:41:16 -04001947 int j, ret = 0;
1948 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001949 u64 aligned_start = ALIGN(start, 1 << 9);
1950
1951 if (WARN_ON(start != aligned_start)) {
1952 len -= aligned_start - start;
1953 len = round_down(len, 1 << 9);
1954 start = aligned_start;
1955 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001956
1957 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04001958
1959 if (!len)
1960 return 0;
1961
1962 end = start + len;
1963 bytes_left = len;
1964
1965 /* Skip any superblocks on this device. */
1966 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
1967 u64 sb_start = btrfs_sb_offset(j);
1968 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
1969 u64 size = sb_start - start;
1970
1971 if (!in_range(sb_start, start, bytes_left) &&
1972 !in_range(sb_end, start, bytes_left) &&
1973 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
1974 continue;
1975
1976 /*
1977 * Superblock spans beginning of range. Adjust start and
1978 * try again.
1979 */
1980 if (sb_start <= start) {
1981 start += sb_end - start;
1982 if (start > end) {
1983 bytes_left = 0;
1984 break;
1985 }
1986 bytes_left = end - start;
1987 continue;
1988 }
1989
1990 if (size) {
1991 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
1992 GFP_NOFS, 0);
1993 if (!ret)
1994 *discarded_bytes += size;
1995 else if (ret != -EOPNOTSUPP)
1996 return ret;
1997 }
1998
1999 start = sb_end;
2000 if (start > end) {
2001 bytes_left = 0;
2002 break;
2003 }
2004 bytes_left = end - start;
2005 }
2006
2007 if (bytes_left) {
2008 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002009 GFP_NOFS, 0);
2010 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04002011 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002012 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002013 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05002014}
Chris Mason15916de2008-11-19 21:17:22 -05002015
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002016int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
Filipe Manana1edb647b2014-12-08 14:01:12 +00002017 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05002018{
Liu Hui1f3c79a2009-01-05 15:57:51 -05002019 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00002020 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02002021 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002022
Christoph Hellwige244a0a2009-10-14 09:24:59 -04002023
Filipe Manana29992412016-05-27 17:42:05 +01002024 /*
2025 * Avoid races with device replace and make sure our bbio has devices
2026 * associated to its stripes that don't go away while we are discarding.
2027 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002028 btrfs_bio_counter_inc_blocked(fs_info);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002029 /* Tell the block device(s) that the sectors can be discarded */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002030 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
2031 &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002032 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05002033 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02002034 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002035 int i;
2036
Liu Hui1f3c79a2009-01-05 15:57:51 -05002037
Jan Schmidta1d3c472011-08-04 17:15:33 +02002038 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002039 u64 bytes;
Josef Bacikd5e20032011-08-04 14:52:27 +00002040 if (!stripe->dev->can_discard)
2041 continue;
2042
Li Dongyang5378e602011-03-24 10:24:27 +00002043 ret = btrfs_issue_discard(stripe->dev->bdev,
2044 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002045 stripe->length,
2046 &bytes);
Li Dongyang5378e602011-03-24 10:24:27 +00002047 if (!ret)
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002048 discarded_bytes += bytes;
Li Dongyang5378e602011-03-24 10:24:27 +00002049 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002050 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00002051
2052 /*
2053 * Just in case we get back EOPNOTSUPP for some reason,
2054 * just ignore the return value so we don't screw up
2055 * people calling discard_extent.
2056 */
2057 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002058 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08002059 btrfs_put_bbio(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002060 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002061 btrfs_bio_counter_dec(fs_info);
Li Dongyang5378e602011-03-24 10:24:27 +00002062
2063 if (actual_bytes)
2064 *actual_bytes = discarded_bytes;
2065
Liu Hui1f3c79a2009-01-05 15:57:51 -05002066
David Woodhouse53b381b2013-01-29 18:40:14 -05002067 if (ret == -EOPNOTSUPP)
2068 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002069 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002070}
2071
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002072/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002073int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002074 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002075 u64 bytenr, u64 num_bytes, u64 parent,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002076 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04002077{
2078 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002079
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002080 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
2081 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04002082
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002083 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002084 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
2085 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002086 parent, root_objectid, (int)owner,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002087 BTRFS_ADD_DELAYED_REF, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002088 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002089 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002090 num_bytes, parent, root_objectid,
2091 owner, offset, 0,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002092 BTRFS_ADD_DELAYED_REF, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002093 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002094 return ret;
2095}
2096
Chris Mason925baed2008-06-25 16:01:30 -04002097static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002098 struct btrfs_fs_info *fs_info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002099 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002100 u64 parent, u64 root_objectid,
2101 u64 owner, u64 offset, int refs_to_add,
2102 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04002103{
Chris Mason5caf2a02007-04-02 11:20:42 -04002104 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002105 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04002106 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07002107 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002108 u64 bytenr = node->bytenr;
2109 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002110 u64 refs;
2111 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05002112
Chris Mason5caf2a02007-04-02 11:20:42 -04002113 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002114 if (!path)
2115 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04002116
David Sterbae4058b52015-11-27 16:31:35 +01002117 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002118 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002119 /* this will setup the path even if it fails to insert the back ref */
Josef Bacikfcebe452014-05-13 17:30:47 -07002120 ret = insert_inline_extent_backref(trans, fs_info->extent_root, path,
2121 bytenr, num_bytes, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002122 root_objectid, owner, offset,
2123 refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08002124 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002125 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07002126
2127 /*
2128 * Ok we had -EAGAIN which means we didn't have space to insert and
2129 * inline extent ref, so just update the reference count and add a
2130 * normal backref.
2131 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002132 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07002133 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002134 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2135 refs = btrfs_extent_refs(leaf, item);
2136 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2137 if (extent_op)
2138 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002139
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002140 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002141 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002142
David Sterbae4058b52015-11-27 16:31:35 +01002143 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04002144 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04002145 /* now insert the actual backref */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002146 ret = insert_extent_backref(trans, fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002147 path, bytenr, parent, root_objectid,
2148 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002149 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04002150 btrfs_abort_transaction(trans, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002151out:
Chris Mason74493f72007-12-11 09:25:06 -05002152 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08002153 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002154}
2155
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002156static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002157 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002158 struct btrfs_delayed_ref_node *node,
2159 struct btrfs_delayed_extent_op *extent_op,
2160 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002161{
Chris Mason56bec292009-03-13 10:10:06 -04002162 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002163 struct btrfs_delayed_data_ref *ref;
2164 struct btrfs_key ins;
2165 u64 parent = 0;
2166 u64 ref_root = 0;
2167 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002168
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002169 ins.objectid = node->bytenr;
2170 ins.offset = node->num_bytes;
2171 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002172
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002173 ref = btrfs_delayed_node_to_data_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002174 trace_run_delayed_data_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002175
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002176 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2177 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002178 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002179
2180 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002181 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002182 flags |= extent_op->flags_to_set;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002183 ret = alloc_reserved_file_extent(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002184 parent, ref_root, flags,
2185 ref->objectid, ref->offset,
2186 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002187 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002188 ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002189 ref_root, ref->objectid,
2190 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002191 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002192 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002193 ret = __btrfs_free_extent(trans, fs_info, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002194 ref_root, ref->objectid,
2195 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002196 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002197 } else {
2198 BUG();
2199 }
Chris Mason56bec292009-03-13 10:10:06 -04002200 return ret;
2201}
2202
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002203static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2204 struct extent_buffer *leaf,
2205 struct btrfs_extent_item *ei)
2206{
2207 u64 flags = btrfs_extent_flags(leaf, ei);
2208 if (extent_op->update_flags) {
2209 flags |= extent_op->flags_to_set;
2210 btrfs_set_extent_flags(leaf, ei, flags);
2211 }
2212
2213 if (extent_op->update_key) {
2214 struct btrfs_tree_block_info *bi;
2215 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2216 bi = (struct btrfs_tree_block_info *)(ei + 1);
2217 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2218 }
2219}
2220
2221static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002222 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002223 struct btrfs_delayed_ref_node *node,
2224 struct btrfs_delayed_extent_op *extent_op)
2225{
2226 struct btrfs_key key;
2227 struct btrfs_path *path;
2228 struct btrfs_extent_item *ei;
2229 struct extent_buffer *leaf;
2230 u32 item_size;
2231 int ret;
2232 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002233 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002234
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002235 if (trans->aborted)
2236 return 0;
2237
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002238 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik3173a182013-03-07 14:22:04 -05002239 metadata = 0;
2240
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002241 path = btrfs_alloc_path();
2242 if (!path)
2243 return -ENOMEM;
2244
2245 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002246
Josef Bacik3173a182013-03-07 14:22:04 -05002247 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002248 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002249 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002250 } else {
2251 key.type = BTRFS_EXTENT_ITEM_KEY;
2252 key.offset = node->num_bytes;
2253 }
2254
2255again:
David Sterbae4058b52015-11-27 16:31:35 +01002256 path->reada = READA_FORWARD;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002257 path->leave_spinning = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002258 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002259 if (ret < 0) {
2260 err = ret;
2261 goto out;
2262 }
2263 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002264 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002265 if (path->slots[0] > 0) {
2266 path->slots[0]--;
2267 btrfs_item_key_to_cpu(path->nodes[0], &key,
2268 path->slots[0]);
2269 if (key.objectid == node->bytenr &&
2270 key.type == BTRFS_EXTENT_ITEM_KEY &&
2271 key.offset == node->num_bytes)
2272 ret = 0;
2273 }
2274 if (ret > 0) {
2275 btrfs_release_path(path);
2276 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002277
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002278 key.objectid = node->bytenr;
2279 key.offset = node->num_bytes;
2280 key.type = BTRFS_EXTENT_ITEM_KEY;
2281 goto again;
2282 }
2283 } else {
2284 err = -EIO;
2285 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002286 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002287 }
2288
2289 leaf = path->nodes[0];
2290 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2291#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2292 if (item_size < sizeof(*ei)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002293 ret = convert_extent_item_v0(trans, fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002294 path, (u64)-1, 0);
2295 if (ret < 0) {
2296 err = ret;
2297 goto out;
2298 }
2299 leaf = path->nodes[0];
2300 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2301 }
2302#endif
2303 BUG_ON(item_size < sizeof(*ei));
2304 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2305 __run_delayed_extent_op(extent_op, leaf, ei);
2306
2307 btrfs_mark_buffer_dirty(leaf);
2308out:
2309 btrfs_free_path(path);
2310 return err;
2311}
2312
2313static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002314 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002315 struct btrfs_delayed_ref_node *node,
2316 struct btrfs_delayed_extent_op *extent_op,
2317 int insert_reserved)
2318{
2319 int ret = 0;
2320 struct btrfs_delayed_tree_ref *ref;
2321 struct btrfs_key ins;
2322 u64 parent = 0;
2323 u64 ref_root = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002324 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002325
2326 ref = btrfs_delayed_node_to_tree_ref(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002327 trace_run_delayed_tree_ref(fs_info, node, ref, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002328
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002329 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2330 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002331 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002332
Josef Bacik3173a182013-03-07 14:22:04 -05002333 ins.objectid = node->bytenr;
2334 if (skinny_metadata) {
2335 ins.offset = ref->level;
2336 ins.type = BTRFS_METADATA_ITEM_KEY;
2337 } else {
2338 ins.offset = node->num_bytes;
2339 ins.type = BTRFS_EXTENT_ITEM_KEY;
2340 }
2341
Liu Bo02794222016-09-14 19:19:05 -07002342 if (node->ref_mod != 1) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002343 btrfs_err(fs_info,
Liu Bo02794222016-09-14 19:19:05 -07002344 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
2345 node->bytenr, node->ref_mod, node->action, ref_root,
2346 parent);
2347 return -EIO;
2348 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002349 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002350 BUG_ON(!extent_op || !extent_op->update_flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002351 ret = alloc_reserved_tree_block(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002352 parent, ref_root,
2353 extent_op->flags_to_set,
2354 &extent_op->key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002355 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002356 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002357 ret = __btrfs_inc_extent_ref(trans, fs_info, node,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002358 parent, ref_root,
2359 ref->level, 0, 1,
Josef Bacikfcebe452014-05-13 17:30:47 -07002360 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002361 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002362 ret = __btrfs_free_extent(trans, fs_info, node,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002363 parent, ref_root,
2364 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002365 } else {
2366 BUG();
2367 }
2368 return ret;
2369}
2370
Chris Mason56bec292009-03-13 10:10:06 -04002371/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002372static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002373 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002374 struct btrfs_delayed_ref_node *node,
2375 struct btrfs_delayed_extent_op *extent_op,
2376 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002377{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002378 int ret = 0;
2379
Josef Bacik857cc2f2013-10-07 15:21:08 -04002380 if (trans->aborted) {
2381 if (insert_reserved)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002382 btrfs_pin_extent(fs_info, node->bytenr,
Josef Bacik857cc2f2013-10-07 15:21:08 -04002383 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002384 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002385 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002386
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002387 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002388 struct btrfs_delayed_ref_head *head;
2389 /*
2390 * we've hit the end of the chain and we were supposed
2391 * to insert this extent into the tree. But, it got
2392 * deleted before we ever needed to insert it, so all
2393 * we have to do is clean up the accounting
2394 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002395 BUG_ON(extent_op);
2396 head = btrfs_delayed_node_to_head(node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002397 trace_run_delayed_ref_head(fs_info, node, head, node->action);
Liu Bo599c75e2013-07-16 19:03:36 +08002398
Chris Mason56bec292009-03-13 10:10:06 -04002399 if (insert_reserved) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002400 btrfs_pin_extent(fs_info, node->bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04002401 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002402 if (head->is_data) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002403 ret = btrfs_del_csums(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002404 node->bytenr,
2405 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002406 }
Chris Mason56bec292009-03-13 10:10:06 -04002407 }
Qu Wenruo297d7502015-09-08 17:08:37 +08002408
2409 /* Also free its reserved qgroup space */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002410 btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
Qu Wenruo297d7502015-09-08 17:08:37 +08002411 head->qgroup_reserved);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002412 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002413 }
Josef Bacikeb099672009-02-12 09:27:38 -05002414
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002415 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2416 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002417 ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002418 insert_reserved);
2419 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2420 node->type == BTRFS_SHARED_DATA_REF_KEY)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002421 ret = run_delayed_data_ref(trans, fs_info, node, extent_op,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002422 insert_reserved);
2423 else
2424 BUG();
2425 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002426}
2427
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002428static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04002429select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002430{
Filipe Mananacffc3372015-07-09 13:13:44 +01002431 struct btrfs_delayed_ref_node *ref;
2432
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002433 if (list_empty(&head->ref_list))
2434 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002435
Filipe Mananacffc3372015-07-09 13:13:44 +01002436 /*
2437 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2438 * This is to prevent a ref count from going down to zero, which deletes
2439 * the extent item from the extent tree, when there still are references
2440 * to add, which would fail because they would not find the extent item.
2441 */
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002442 if (!list_empty(&head->ref_add_list))
2443 return list_first_entry(&head->ref_add_list,
2444 struct btrfs_delayed_ref_node, add_list);
Filipe Mananacffc3372015-07-09 13:13:44 +01002445
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002446 ref = list_first_entry(&head->ref_list, struct btrfs_delayed_ref_node,
2447 list);
2448 ASSERT(list_empty(&ref->add_list));
2449 return ref;
Chris Mason56bec292009-03-13 10:10:06 -04002450}
2451
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002452/*
2453 * Returns 0 on success or if called with an already aborted transaction.
2454 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2455 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002456static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002457 struct btrfs_fs_info *fs_info,
Josef Bacikd7df2c72014-01-23 09:21:38 -05002458 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002459{
Chris Mason56bec292009-03-13 10:10:06 -04002460 struct btrfs_delayed_ref_root *delayed_refs;
2461 struct btrfs_delayed_ref_node *ref;
2462 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002463 struct btrfs_delayed_extent_op *extent_op;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002464 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002465 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002466 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002467 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002468 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002469
2470 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002471 while (1) {
2472 if (!locked_ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002473 if (count >= nr)
Chris Mason56bec292009-03-13 10:10:06 -04002474 break;
Chris Mason56bec292009-03-13 10:10:06 -04002475
Josef Bacikd7df2c72014-01-23 09:21:38 -05002476 spin_lock(&delayed_refs->lock);
2477 locked_ref = btrfs_select_ref_head(trans);
2478 if (!locked_ref) {
2479 spin_unlock(&delayed_refs->lock);
2480 break;
2481 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002482
2483 /* grab the lock that says we are going to process
2484 * all the refs for this head */
2485 ret = btrfs_delayed_ref_lock(trans, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002486 spin_unlock(&delayed_refs->lock);
Chris Masonc3e69d52009-03-13 10:17:05 -04002487 /*
2488 * we may have dropped the spin lock to get the head
2489 * mutex lock, and that might have given someone else
2490 * time to free the head. If that's true, it has been
2491 * removed from our list and we can move on.
2492 */
2493 if (ret == -EAGAIN) {
2494 locked_ref = NULL;
2495 count++;
2496 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002497 }
2498 }
2499
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002500 /*
2501 * We need to try and merge add/drops of the same ref since we
2502 * can run into issues with relocate dropping the implicit ref
2503 * and then it being added back again before the drop can
2504 * finish. If we merged anything we need to re-loop so we can
2505 * get a good ref.
2506 * Or we can get node references of the same type that weren't
2507 * merged when created due to bumps in the tree mod seq, and
2508 * we need to merge them to prevent adding an inline extent
2509 * backref before dropping it (triggering a BUG_ON at
2510 * insert_inline_extent_backref()).
2511 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002512 spin_lock(&locked_ref->lock);
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002513 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2514 locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002515
2516 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002517 * locked_ref is the head node, so we have to go one
2518 * node back for any delayed ref updates
2519 */
2520 ref = select_delayed_ref(locked_ref);
2521
2522 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002523 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002524 spin_unlock(&locked_ref->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002525 spin_lock(&delayed_refs->lock);
2526 locked_ref->processing = 0;
Arne Jansend1270cd2011-09-13 15:16:43 +02002527 delayed_refs->num_heads_ready++;
2528 spin_unlock(&delayed_refs->lock);
Jeff Mahoneyd0280992016-12-20 13:28:28 -05002529 btrfs_delayed_ref_unlock(locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002530 locked_ref = NULL;
Arne Jansend1270cd2011-09-13 15:16:43 +02002531 cond_resched();
Josef Bacik27a377d2014-02-07 13:57:59 -05002532 count++;
Arne Jansend1270cd2011-09-13 15:16:43 +02002533 continue;
2534 }
2535
2536 /*
Chris Mason56bec292009-03-13 10:10:06 -04002537 * record the must insert reserved flag before we
2538 * drop the spin lock.
2539 */
2540 must_insert_reserved = locked_ref->must_insert_reserved;
2541 locked_ref->must_insert_reserved = 0;
2542
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002543 extent_op = locked_ref->extent_op;
2544 locked_ref->extent_op = NULL;
2545
Chris Mason56bec292009-03-13 10:10:06 -04002546 if (!ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002547
2548
Chris Mason56bec292009-03-13 10:10:06 -04002549 /* All delayed refs have been processed, Go ahead
2550 * and send the head node to run_one_delayed_ref,
2551 * so that any accounting fixes can happen
2552 */
2553 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002554
2555 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002556 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002557 extent_op = NULL;
2558 }
2559
2560 if (extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002561 spin_unlock(&locked_ref->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002562 ret = run_delayed_extent_op(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002563 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002564 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002565
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002566 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04002567 /*
2568 * Need to reset must_insert_reserved if
2569 * there was an error so the abort stuff
2570 * can cleanup the reserved space
2571 * properly.
2572 */
2573 if (must_insert_reserved)
2574 locked_ref->must_insert_reserved = 1;
Jeff Mahoneyaa7c8da2016-12-20 13:28:27 -05002575 spin_lock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002576 locked_ref->processing = 0;
Jeff Mahoneyaa7c8da2016-12-20 13:28:27 -05002577 delayed_refs->num_heads_ready++;
2578 spin_unlock(&delayed_refs->lock);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002579 btrfs_debug(fs_info,
2580 "run_delayed_extent_op returned %d",
2581 ret);
Miao Xie093486c2012-12-19 08:10:10 +00002582 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002583 return ret;
2584 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002585 continue;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002586 }
Chris Mason56bec292009-03-13 10:10:06 -04002587
Josef Bacikd7df2c72014-01-23 09:21:38 -05002588 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002589 * Need to drop our head ref lock and re-acquire the
Josef Bacikd7df2c72014-01-23 09:21:38 -05002590 * delayed ref lock and then re-check to make sure
2591 * nobody got added.
2592 */
2593 spin_unlock(&locked_ref->lock);
2594 spin_lock(&delayed_refs->lock);
2595 spin_lock(&locked_ref->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002596 if (!list_empty(&locked_ref->ref_list) ||
Josef Bacik573a0752014-03-27 19:41:34 -04002597 locked_ref->extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002598 spin_unlock(&locked_ref->lock);
2599 spin_unlock(&delayed_refs->lock);
2600 continue;
2601 }
2602 ref->in_tree = 0;
2603 delayed_refs->num_heads--;
Liu Boc46effa2013-10-14 12:59:45 +08002604 rb_erase(&locked_ref->href_node,
2605 &delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002606 spin_unlock(&delayed_refs->lock);
2607 } else {
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002608 actual_count++;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002609 ref->in_tree = 0;
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002610 list_del(&ref->list);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08002611 if (!list_empty(&ref->add_list))
2612 list_del(&ref->add_list);
Liu Boc46effa2013-10-14 12:59:45 +08002613 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002614 atomic_dec(&delayed_refs->num_entries);
2615
Miao Xie093486c2012-12-19 08:10:10 +00002616 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002617 /*
2618 * when we play the delayed ref, also correct the
2619 * ref_mod on head
2620 */
2621 switch (ref->action) {
2622 case BTRFS_ADD_DELAYED_REF:
2623 case BTRFS_ADD_DELAYED_EXTENT:
2624 locked_ref->node.ref_mod -= ref->ref_mod;
2625 break;
2626 case BTRFS_DROP_DELAYED_REF:
2627 locked_ref->node.ref_mod += ref->ref_mod;
2628 break;
2629 default:
2630 WARN_ON(1);
2631 }
2632 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002633 spin_unlock(&locked_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002634
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002635 ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002636 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002637
Miao Xie78a61842012-11-21 02:21:28 +00002638 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002639 if (ret) {
Wang Xiaoguang9d1032c2016-06-20 09:18:52 +08002640 spin_lock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002641 locked_ref->processing = 0;
Wang Xiaoguang9d1032c2016-06-20 09:18:52 +08002642 delayed_refs->num_heads_ready++;
2643 spin_unlock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002644 btrfs_delayed_ref_unlock(locked_ref);
2645 btrfs_put_delayed_ref(ref);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002646 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
2647 ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002648 return ret;
2649 }
2650
Miao Xie093486c2012-12-19 08:10:10 +00002651 /*
2652 * If this node is a head, that means all the refs in this head
2653 * have been dealt with, and we will pick the next head to deal
2654 * with, so we must unlock the head and drop it from the cluster
2655 * list before we release it.
2656 */
2657 if (btrfs_delayed_ref_is_head(ref)) {
Josef Bacik12621332015-02-03 07:50:16 -08002658 if (locked_ref->is_data &&
2659 locked_ref->total_ref_mod < 0) {
2660 spin_lock(&delayed_refs->lock);
2661 delayed_refs->pending_csums -= ref->num_bytes;
2662 spin_unlock(&delayed_refs->lock);
2663 }
Miao Xie093486c2012-12-19 08:10:10 +00002664 btrfs_delayed_ref_unlock(locked_ref);
2665 locked_ref = NULL;
2666 }
2667 btrfs_put_delayed_ref(ref);
2668 count++;
Chris Mason1887be62009-03-13 10:11:24 -04002669 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002670 }
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002671
2672 /*
2673 * We don't want to include ref heads since we can have empty ref heads
2674 * and those will drastically skew our runtime down since we just do
2675 * accounting, no actual extent tree updates.
2676 */
2677 if (actual_count > 0) {
2678 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2679 u64 avg;
2680
2681 /*
2682 * We weigh the current average higher than our current runtime
2683 * to avoid large swings in the average.
2684 */
2685 spin_lock(&delayed_refs->lock);
2686 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002687 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002688 spin_unlock(&delayed_refs->lock);
2689 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002690 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002691}
2692
Arne Jansen709c0482011-09-12 12:22:57 +02002693#ifdef SCRAMBLE_DELAYED_REFS
2694/*
2695 * Normally delayed refs get processed in ascending bytenr order. This
2696 * correlates in most cases to the order added. To expose dependencies on this
2697 * order, we start to process the tree in the middle instead of the beginning
2698 */
2699static u64 find_middle(struct rb_root *root)
2700{
2701 struct rb_node *n = root->rb_node;
2702 struct btrfs_delayed_ref_node *entry;
2703 int alt = 1;
2704 u64 middle;
2705 u64 first = 0, last = 0;
2706
2707 n = rb_first(root);
2708 if (n) {
2709 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2710 first = entry->bytenr;
2711 }
2712 n = rb_last(root);
2713 if (n) {
2714 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2715 last = entry->bytenr;
2716 }
2717 n = root->rb_node;
2718
2719 while (n) {
2720 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2721 WARN_ON(!entry->in_tree);
2722
2723 middle = entry->bytenr;
2724
2725 if (alt)
2726 n = n->rb_left;
2727 else
2728 n = n->rb_right;
2729
2730 alt = 1 - alt;
2731 }
2732 return middle;
2733}
2734#endif
2735
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002736static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
Josef Bacik1be41b72013-06-12 13:56:06 -04002737{
2738 u64 num_bytes;
2739
2740 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2741 sizeof(struct btrfs_extent_inline_ref));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002742 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
Josef Bacik1be41b72013-06-12 13:56:06 -04002743 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2744
2745 /*
2746 * 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 -04002747 * closer to what we're really going to want to use.
Josef Bacik1be41b72013-06-12 13:56:06 -04002748 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002749 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
Josef Bacik1be41b72013-06-12 13:56:06 -04002750}
2751
Josef Bacik12621332015-02-03 07:50:16 -08002752/*
2753 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2754 * would require to store the csums for that many bytes.
2755 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002756u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002757{
2758 u64 csum_size;
2759 u64 num_csums_per_leaf;
2760 u64 num_csums;
2761
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002762 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
Josef Bacik12621332015-02-03 07:50:16 -08002763 num_csums_per_leaf = div64_u64(csum_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002764 (u64)btrfs_super_csum_size(fs_info->super_copy));
2765 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
Josef Bacik12621332015-02-03 07:50:16 -08002766 num_csums += num_csums_per_leaf - 1;
2767 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2768 return num_csums;
2769}
2770
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002771int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002772 struct btrfs_fs_info *fs_info)
Josef Bacik1be41b72013-06-12 13:56:06 -04002773{
2774 struct btrfs_block_rsv *global_rsv;
2775 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
Josef Bacik12621332015-02-03 07:50:16 -08002776 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
Josef Bacikcb723e42015-02-18 08:06:57 -08002777 u64 num_dirty_bgs = trans->transaction->num_dirty_bgs;
2778 u64 num_bytes, num_dirty_bgs_bytes;
Josef Bacik1be41b72013-06-12 13:56:06 -04002779 int ret = 0;
2780
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002781 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002782 num_heads = heads_to_leaves(fs_info, num_heads);
Josef Bacik1be41b72013-06-12 13:56:06 -04002783 if (num_heads > 1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002784 num_bytes += (num_heads - 1) * fs_info->nodesize;
Josef Bacik1be41b72013-06-12 13:56:06 -04002785 num_bytes <<= 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002786 num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
2787 fs_info->nodesize;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002788 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
Josef Bacikcb723e42015-02-18 08:06:57 -08002789 num_dirty_bgs);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002790 global_rsv = &fs_info->global_block_rsv;
Josef Bacik1be41b72013-06-12 13:56:06 -04002791
2792 /*
2793 * If we can't allocate any more chunks lets make sure we have _lots_ of
2794 * wiggle room since running delayed refs can create more delayed refs.
2795 */
Josef Bacikcb723e42015-02-18 08:06:57 -08002796 if (global_rsv->space_info->full) {
2797 num_dirty_bgs_bytes <<= 1;
Josef Bacik1be41b72013-06-12 13:56:06 -04002798 num_bytes <<= 1;
Josef Bacikcb723e42015-02-18 08:06:57 -08002799 }
Josef Bacik1be41b72013-06-12 13:56:06 -04002800
2801 spin_lock(&global_rsv->lock);
Josef Bacikcb723e42015-02-18 08:06:57 -08002802 if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
Josef Bacik1be41b72013-06-12 13:56:06 -04002803 ret = 1;
2804 spin_unlock(&global_rsv->lock);
2805 return ret;
2806}
2807
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002808int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002809 struct btrfs_fs_info *fs_info)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002810{
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002811 u64 num_entries =
2812 atomic_read(&trans->transaction->delayed_refs.num_entries);
2813 u64 avg_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002814 u64 val;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002815
2816 smp_mb();
2817 avg_runtime = fs_info->avg_delayed_ref_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002818 val = num_entries * avg_runtime;
Wang Xiaoguangdc1a90c2016-10-26 15:23:01 +08002819 if (val >= NSEC_PER_SEC)
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002820 return 1;
Chris Masona79b7d42014-05-22 16:18:52 -07002821 if (val >= NSEC_PER_SEC / 2)
2822 return 2;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002823
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002824 return btrfs_check_space_for_delayed_refs(trans, fs_info);
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002825}
2826
Chris Masona79b7d42014-05-22 16:18:52 -07002827struct async_delayed_refs {
2828 struct btrfs_root *root;
Josef Bacik31b96552016-04-11 17:37:40 -04002829 u64 transid;
Chris Masona79b7d42014-05-22 16:18:52 -07002830 int count;
2831 int error;
2832 int sync;
2833 struct completion wait;
2834 struct btrfs_work work;
2835};
2836
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002837static inline struct async_delayed_refs *
2838to_async_delayed_refs(struct btrfs_work *work)
2839{
2840 return container_of(work, struct async_delayed_refs, work);
2841}
2842
Chris Masona79b7d42014-05-22 16:18:52 -07002843static void delayed_ref_async_start(struct btrfs_work *work)
2844{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002845 struct async_delayed_refs *async = to_async_delayed_refs(work);
Chris Masona79b7d42014-05-22 16:18:52 -07002846 struct btrfs_trans_handle *trans;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002847 struct btrfs_fs_info *fs_info = async->root->fs_info;
Chris Masona79b7d42014-05-22 16:18:52 -07002848 int ret;
2849
Chris Mason0f873ec2016-04-27 09:59:38 -04002850 /* if the commit is already started, we don't need to wait here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002851 if (btrfs_transaction_blocked(fs_info))
Josef Bacik31b96552016-04-11 17:37:40 -04002852 goto done;
Josef Bacik31b96552016-04-11 17:37:40 -04002853
Chris Mason0f873ec2016-04-27 09:59:38 -04002854 trans = btrfs_join_transaction(async->root);
2855 if (IS_ERR(trans)) {
2856 async->error = PTR_ERR(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07002857 goto done;
2858 }
2859
2860 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002861 * trans->sync means that when we call end_transaction, we won't
Chris Masona79b7d42014-05-22 16:18:52 -07002862 * wait on delayed refs
2863 */
2864 trans->sync = true;
Chris Mason0f873ec2016-04-27 09:59:38 -04002865
2866 /* Don't bother flushing if we got into a different transaction */
2867 if (trans->transid > async->transid)
2868 goto end;
2869
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002870 ret = btrfs_run_delayed_refs(trans, fs_info, async->count);
Chris Masona79b7d42014-05-22 16:18:52 -07002871 if (ret)
2872 async->error = ret;
Chris Mason0f873ec2016-04-27 09:59:38 -04002873end:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002874 ret = btrfs_end_transaction(trans);
Chris Masona79b7d42014-05-22 16:18:52 -07002875 if (ret && !async->error)
2876 async->error = ret;
2877done:
2878 if (async->sync)
2879 complete(&async->wait);
2880 else
2881 kfree(async);
2882}
2883
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002884int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
Josef Bacik31b96552016-04-11 17:37:40 -04002885 unsigned long count, u64 transid, int wait)
Chris Masona79b7d42014-05-22 16:18:52 -07002886{
2887 struct async_delayed_refs *async;
2888 int ret;
2889
2890 async = kmalloc(sizeof(*async), GFP_NOFS);
2891 if (!async)
2892 return -ENOMEM;
2893
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002894 async->root = fs_info->tree_root;
Chris Masona79b7d42014-05-22 16:18:52 -07002895 async->count = count;
2896 async->error = 0;
Josef Bacik31b96552016-04-11 17:37:40 -04002897 async->transid = transid;
Chris Masona79b7d42014-05-22 16:18:52 -07002898 if (wait)
2899 async->sync = 1;
2900 else
2901 async->sync = 0;
2902 init_completion(&async->wait);
2903
Liu Bo9e0af232014-08-15 23:36:53 +08002904 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
2905 delayed_ref_async_start, NULL, NULL);
Chris Masona79b7d42014-05-22 16:18:52 -07002906
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002907 btrfs_queue_work(fs_info->extent_workers, &async->work);
Chris Masona79b7d42014-05-22 16:18:52 -07002908
2909 if (wait) {
2910 wait_for_completion(&async->wait);
2911 ret = async->error;
2912 kfree(async);
2913 return ret;
2914 }
2915 return 0;
2916}
2917
Chris Masonc3e69d52009-03-13 10:17:05 -04002918/*
2919 * this starts processing the delayed reference count updates and
2920 * extent insertions we have queued up so far. count can be
2921 * 0, which means to process everything in the tree at the start
2922 * of the run (but not newly added entries), or it can be some target
2923 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002924 *
2925 * Returns 0 on success or if called with an aborted transaction
2926 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002927 */
2928int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002929 struct btrfs_fs_info *fs_info, unsigned long count)
Chris Masonc3e69d52009-03-13 10:17:05 -04002930{
2931 struct rb_node *node;
2932 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002933 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002934 int ret;
2935 int run_all = count == (unsigned long)-1;
Filipe Mananad9a05402015-10-03 13:13:13 +01002936 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Chris Masonc3e69d52009-03-13 10:17:05 -04002937
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002938 /* We'll clean this up in btrfs_cleanup_transaction */
2939 if (trans->aborted)
2940 return 0;
2941
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002942 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
Chris Mason511711a2015-12-30 07:52:35 -08002943 return 0;
2944
Chris Masonc3e69d52009-03-13 10:17:05 -04002945 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08002946 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05002947 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002948
Chris Masonc3e69d52009-03-13 10:17:05 -04002949again:
Arne Jansen709c0482011-09-12 12:22:57 +02002950#ifdef SCRAMBLE_DELAYED_REFS
2951 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2952#endif
Filipe Mananad9a05402015-10-03 13:13:13 +01002953 trans->can_flush_pending_bgs = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002954 ret = __btrfs_run_delayed_refs(trans, fs_info, count);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002955 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002956 btrfs_abort_transaction(trans, ret);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002957 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002958 }
2959
Chris Mason56bec292009-03-13 10:10:06 -04002960 if (run_all) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002961 if (!list_empty(&trans->new_bgs))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002962 btrfs_create_pending_block_groups(trans, fs_info);
Josef Bacikea658ba2012-09-11 16:57:25 -04002963
Josef Bacikd7df2c72014-01-23 09:21:38 -05002964 spin_lock(&delayed_refs->lock);
Liu Boc46effa2013-10-14 12:59:45 +08002965 node = rb_first(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002966 if (!node) {
2967 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002968 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002969 }
Chris Mason56bec292009-03-13 10:10:06 -04002970
2971 while (node) {
Liu Boc46effa2013-10-14 12:59:45 +08002972 head = rb_entry(node, struct btrfs_delayed_ref_head,
2973 href_node);
2974 if (btrfs_delayed_ref_is_head(&head->node)) {
2975 struct btrfs_delayed_ref_node *ref;
Chris Mason56bec292009-03-13 10:10:06 -04002976
Liu Boc46effa2013-10-14 12:59:45 +08002977 ref = &head->node;
Chris Mason56bec292009-03-13 10:10:06 -04002978 atomic_inc(&ref->refs);
2979
2980 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002981 /*
2982 * Mutex was contended, block until it's
2983 * released and try again
2984 */
Chris Mason56bec292009-03-13 10:10:06 -04002985 mutex_lock(&head->mutex);
2986 mutex_unlock(&head->mutex);
2987
2988 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002989 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002990 goto again;
Liu Boc46effa2013-10-14 12:59:45 +08002991 } else {
2992 WARN_ON(1);
Chris Mason56bec292009-03-13 10:10:06 -04002993 }
2994 node = rb_next(node);
2995 }
2996 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002997 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002998 goto again;
2999 }
Chris Mason54aa1f42007-06-22 14:16:25 -04003000out:
Jan Schmidtedf39272012-06-28 18:04:55 +02003001 assert_qgroups_uptodate(trans);
Filipe Mananad9a05402015-10-03 13:13:13 +01003002 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Chris Masona28ec192007-03-06 20:08:01 -05003003 return 0;
3004}
3005
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003006int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003007 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003008 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04003009 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003010{
3011 struct btrfs_delayed_extent_op *extent_op;
3012 int ret;
3013
Miao Xie78a61842012-11-21 02:21:28 +00003014 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003015 if (!extent_op)
3016 return -ENOMEM;
3017
3018 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01003019 extent_op->update_flags = true;
3020 extent_op->update_key = false;
3021 extent_op->is_data = is_data ? true : false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04003022 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003023
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003024 ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003025 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003026 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00003027 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003028 return ret;
3029}
3030
3031static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
3032 struct btrfs_root *root,
3033 struct btrfs_path *path,
3034 u64 objectid, u64 offset, u64 bytenr)
3035{
3036 struct btrfs_delayed_ref_head *head;
3037 struct btrfs_delayed_ref_node *ref;
3038 struct btrfs_delayed_data_ref *data_ref;
3039 struct btrfs_delayed_ref_root *delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003040 int ret = 0;
3041
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003042 delayed_refs = &trans->transaction->delayed_refs;
3043 spin_lock(&delayed_refs->lock);
3044 head = btrfs_find_delayed_ref_head(trans, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003045 if (!head) {
3046 spin_unlock(&delayed_refs->lock);
3047 return 0;
3048 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003049
3050 if (!mutex_trylock(&head->mutex)) {
3051 atomic_inc(&head->node.refs);
3052 spin_unlock(&delayed_refs->lock);
3053
David Sterbab3b4aa72011-04-21 01:20:15 +02003054 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003055
David Sterba8cc33e52011-05-02 15:29:25 +02003056 /*
3057 * Mutex was contended, block until it's released and let
3058 * caller try again
3059 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003060 mutex_lock(&head->mutex);
3061 mutex_unlock(&head->mutex);
3062 btrfs_put_delayed_ref(&head->node);
3063 return -EAGAIN;
3064 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003065 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003066
3067 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08003068 list_for_each_entry(ref, &head->ref_list, list) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05003069 /* If it's a shared ref we know a cross reference exists */
3070 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
3071 ret = 1;
3072 break;
3073 }
3074
3075 data_ref = btrfs_delayed_node_to_data_ref(ref);
3076
3077 /*
3078 * If our ref doesn't match the one we're currently looking at
3079 * then we have a cross reference.
3080 */
3081 if (data_ref->root != root->root_key.objectid ||
3082 data_ref->objectid != objectid ||
3083 data_ref->offset != offset) {
3084 ret = 1;
3085 break;
3086 }
3087 }
3088 spin_unlock(&head->lock);
3089 mutex_unlock(&head->mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003090 return ret;
3091}
3092
3093static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
3094 struct btrfs_root *root,
3095 struct btrfs_path *path,
3096 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05003097{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003098 struct btrfs_fs_info *fs_info = root->fs_info;
3099 struct btrfs_root *extent_root = fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04003100 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003101 struct btrfs_extent_data_ref *ref;
3102 struct btrfs_extent_inline_ref *iref;
3103 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05003104 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003105 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04003106 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05003107
Chris Masonbe20aa92007-12-17 20:14:01 -05003108 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04003109 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04003110 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05003111
Chris Masonbe20aa92007-12-17 20:14:01 -05003112 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3113 if (ret < 0)
3114 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003115 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04003116
3117 ret = -ENOENT;
3118 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04003119 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003120
Zheng Yan31840ae2008-09-23 13:14:14 -04003121 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04003122 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003123 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05003124
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003125 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05003126 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003127
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003128 ret = 1;
3129 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3130#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3131 if (item_size < sizeof(*ei)) {
3132 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3133 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003134 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003135#endif
3136 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
3137
3138 if (item_size != sizeof(*ei) +
3139 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
3140 goto out;
3141
3142 if (btrfs_extent_generation(leaf, ei) <=
3143 btrfs_root_last_snapshot(&root->root_item))
3144 goto out;
3145
3146 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
3147 if (btrfs_extent_inline_ref_type(leaf, iref) !=
3148 BTRFS_EXTENT_DATA_REF_KEY)
3149 goto out;
3150
3151 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3152 if (btrfs_extent_refs(leaf, ei) !=
3153 btrfs_extent_data_ref_count(leaf, ref) ||
3154 btrfs_extent_data_ref_root(leaf, ref) !=
3155 root->root_key.objectid ||
3156 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3157 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3158 goto out;
3159
Yan Zhengf321e492008-07-30 09:26:11 -04003160 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05003161out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003162 return ret;
3163}
3164
3165int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
3166 struct btrfs_root *root,
3167 u64 objectid, u64 offset, u64 bytenr)
3168{
3169 struct btrfs_path *path;
3170 int ret;
3171 int ret2;
3172
3173 path = btrfs_alloc_path();
3174 if (!path)
3175 return -ENOENT;
3176
3177 do {
3178 ret = check_committed_ref(trans, root, path, objectid,
3179 offset, bytenr);
3180 if (ret && ret != -ENOENT)
3181 goto out;
3182
3183 ret2 = check_delayed_ref(trans, root, path, objectid,
3184 offset, bytenr);
3185 } while (ret2 == -EAGAIN);
3186
3187 if (ret2 && ret2 != -ENOENT) {
3188 ret = ret2;
3189 goto out;
3190 }
3191
3192 if (ret != -ENOENT || ret2 != -ENOENT)
3193 ret = 0;
3194out:
Yan Zhengf321e492008-07-30 09:26:11 -04003195 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003196 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3197 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04003198 return ret;
3199}
3200
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003201static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05003202 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003203 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07003204 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04003205{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003206 struct btrfs_fs_info *fs_info = root->fs_info;
Zheng Yan31840ae2008-09-23 13:14:14 -04003207 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003208 u64 num_bytes;
3209 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003210 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04003211 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003212 struct btrfs_key key;
3213 struct btrfs_file_extent_item *fi;
3214 int i;
3215 int level;
3216 int ret = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003217 int (*process_func)(struct btrfs_trans_handle *,
3218 struct btrfs_fs_info *,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003219 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04003220
David Sterbafccb84c2014-09-29 23:53:21 +02003221
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003222 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003223 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003224
Zheng Yan31840ae2008-09-23 13:14:14 -04003225 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003226 nritems = btrfs_header_nritems(buf);
3227 level = btrfs_header_level(buf);
3228
Miao Xie27cdeb72014-04-02 19:51:05 +08003229 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003230 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003231
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003232 if (inc)
3233 process_func = btrfs_inc_extent_ref;
3234 else
3235 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003236
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003237 if (full_backref)
3238 parent = buf->start;
3239 else
3240 parent = 0;
3241
Zheng Yan31840ae2008-09-23 13:14:14 -04003242 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003243 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003244 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02003245 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003246 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003247 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003248 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003249 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003250 BTRFS_FILE_EXTENT_INLINE)
3251 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003252 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3253 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003254 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003255
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003256 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3257 key.offset -= btrfs_file_extent_offset(buf, fi);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003258 ret = process_func(trans, fs_info, bytenr, num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003259 parent, ref_root, key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003260 key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003261 if (ret)
3262 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003263 } else {
3264 bytenr = btrfs_node_blockptr(buf, i);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003265 num_bytes = fs_info->nodesize;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003266 ret = process_func(trans, fs_info, bytenr, num_bytes,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003267 parent, ref_root, level - 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003268 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003269 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003270 }
3271 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003272 return 0;
3273fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003274 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003275}
3276
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003277int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003278 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04003279{
Josef Bacike339a6b2014-07-02 10:54:25 -07003280 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003281}
Zheng Yan31840ae2008-09-23 13:14:14 -04003282
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003283int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003284 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003285{
Josef Bacike339a6b2014-07-02 10:54:25 -07003286 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003287}
3288
Chris Mason9078a3e2007-04-26 16:46:15 -04003289static int write_one_cache_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003290 struct btrfs_fs_info *fs_info,
Chris Mason9078a3e2007-04-26 16:46:15 -04003291 struct btrfs_path *path,
3292 struct btrfs_block_group_cache *cache)
3293{
3294 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003295 struct btrfs_root *extent_root = fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003296 unsigned long bi;
3297 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003298
Chris Mason9078a3e2007-04-26 16:46:15 -04003299 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003300 if (ret) {
3301 if (ret > 0)
3302 ret = -ENOENT;
Chris Mason54aa1f42007-06-22 14:16:25 -04003303 goto fail;
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003304 }
Chris Mason5f39d392007-10-15 16:14:19 -04003305
3306 leaf = path->nodes[0];
3307 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3308 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3309 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -04003310fail:
Filipe Manana24b89d02015-04-25 18:31:05 +01003311 btrfs_release_path(path);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003312 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003313
3314}
3315
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003316static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003317next_block_group(struct btrfs_fs_info *fs_info,
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003318 struct btrfs_block_group_cache *cache)
3319{
3320 struct rb_node *node;
Filipe Manana292cbd52014-11-26 15:28:50 +00003321
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003322 spin_lock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003323
3324 /* If our block group was removed, we need a full search. */
3325 if (RB_EMPTY_NODE(&cache->cache_node)) {
3326 const u64 next_bytenr = cache->key.objectid + cache->key.offset;
3327
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003328 spin_unlock(&fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003329 btrfs_put_block_group(cache);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003330 cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
Filipe Manana292cbd52014-11-26 15:28:50 +00003331 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003332 node = rb_next(&cache->cache_node);
3333 btrfs_put_block_group(cache);
3334 if (node) {
3335 cache = rb_entry(node, struct btrfs_block_group_cache,
3336 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003337 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003338 } else
3339 cache = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003340 spin_unlock(&fs_info->block_group_cache_lock);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003341 return cache;
3342}
3343
Josef Bacik0af3d002010-06-21 14:48:16 -04003344static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3345 struct btrfs_trans_handle *trans,
3346 struct btrfs_path *path)
3347{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003348 struct btrfs_fs_info *fs_info = block_group->fs_info;
3349 struct btrfs_root *root = fs_info->tree_root;
Josef Bacik0af3d002010-06-21 14:48:16 -04003350 struct inode *inode = NULL;
3351 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003352 int dcs = BTRFS_DC_ERROR;
David Sterbaf8c269d2015-01-16 17:21:12 +01003353 u64 num_pages = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003354 int retries = 0;
3355 int ret = 0;
3356
3357 /*
3358 * If this block group is smaller than 100 megs don't bother caching the
3359 * block group.
3360 */
Byongho Leeee221842015-12-15 01:42:10 +09003361 if (block_group->key.offset < (100 * SZ_1M)) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003362 spin_lock(&block_group->lock);
3363 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3364 spin_unlock(&block_group->lock);
3365 return 0;
3366 }
3367
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003368 if (trans->aborted)
3369 return 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003370again:
3371 inode = lookup_free_space_inode(root, block_group, path);
3372 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3373 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003374 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003375 goto out;
3376 }
3377
3378 if (IS_ERR(inode)) {
3379 BUG_ON(retries);
3380 retries++;
3381
3382 if (block_group->ro)
3383 goto out_free;
3384
3385 ret = create_free_space_inode(root, trans, block_group, path);
3386 if (ret)
3387 goto out_free;
3388 goto again;
3389 }
3390
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003391 /* We've already setup this transaction, go ahead and exit */
3392 if (block_group->cache_generation == trans->transid &&
3393 i_size_read(inode)) {
3394 dcs = BTRFS_DC_SETUP;
3395 goto out_put;
3396 }
3397
Josef Bacik0af3d002010-06-21 14:48:16 -04003398 /*
3399 * We want to set the generation to 0, that way if anything goes wrong
3400 * from here on out we know not to trust this cache when we load up next
3401 * time.
3402 */
3403 BTRFS_I(inode)->generation = 0;
3404 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003405 if (ret) {
3406 /*
3407 * So theoretically we could recover from this, simply set the
3408 * super cache generation to 0 so we know to invalidate the
3409 * cache, but then we'd have to keep track of the block groups
3410 * that fail this way so we know we _have_ to reset this cache
3411 * before the next commit or risk reading stale cache. So to
3412 * limit our exposure to horrible edge cases lets just abort the
3413 * transaction, this only happens in really bad situations
3414 * anyway.
3415 */
Jeff Mahoney66642832016-06-10 18:19:25 -04003416 btrfs_abort_transaction(trans, ret);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003417 goto out_put;
3418 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003419 WARN_ON(ret);
3420
3421 if (i_size_read(inode) > 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003422 ret = btrfs_check_trunc_cache_free_space(fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003423 &fs_info->global_block_rsv);
Miao Xie7b61cd92013-05-13 13:55:09 +00003424 if (ret)
3425 goto out_put;
3426
Chris Mason1bbc6212015-04-06 12:46:08 -07003427 ret = btrfs_truncate_free_space_cache(root, trans, NULL, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003428 if (ret)
3429 goto out_put;
3430 }
3431
3432 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003433 if (block_group->cached != BTRFS_CACHE_FINISHED ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003434 !btrfs_test_opt(fs_info, SPACE_CACHE)) {
Liu Bocf7c1ef2012-07-06 03:31:34 -06003435 /*
3436 * don't bother trying to write stuff out _if_
3437 * a) we're not cached,
3438 * b) we're with nospace_cache mount option.
3439 */
Josef Bacik2b209822010-12-03 13:17:53 -05003440 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003441 spin_unlock(&block_group->lock);
3442 goto out_put;
3443 }
3444 spin_unlock(&block_group->lock);
3445
Josef Bacik6fc823b2012-08-06 13:46:38 -06003446 /*
Josef Bacik2968b1f2015-10-01 12:55:18 -04003447 * We hit an ENOSPC when setting up the cache in this transaction, just
3448 * skip doing the setup, we've already cleared the cache so we're safe.
3449 */
3450 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
3451 ret = -ENOSPC;
3452 goto out_put;
3453 }
3454
3455 /*
Josef Bacik6fc823b2012-08-06 13:46:38 -06003456 * Try to preallocate enough space based on how big the block group is.
3457 * Keep in mind this has to include any pinned space which could end up
3458 * taking up quite a bit since it's not folded into the other space
3459 * cache.
3460 */
Byongho Leeee221842015-12-15 01:42:10 +09003461 num_pages = div_u64(block_group->key.offset, SZ_256M);
Josef Bacik0af3d002010-06-21 14:48:16 -04003462 if (!num_pages)
3463 num_pages = 1;
3464
Josef Bacik0af3d002010-06-21 14:48:16 -04003465 num_pages *= 16;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003466 num_pages *= PAGE_SIZE;
Josef Bacik0af3d002010-06-21 14:48:16 -04003467
Qu Wenruo7cf5b972015-09-08 17:25:55 +08003468 ret = btrfs_check_data_free_space(inode, 0, num_pages);
Josef Bacik0af3d002010-06-21 14:48:16 -04003469 if (ret)
3470 goto out_put;
3471
3472 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3473 num_pages, num_pages,
3474 &alloc_hint);
Josef Bacik2968b1f2015-10-01 12:55:18 -04003475 /*
3476 * Our cache requires contiguous chunks so that we don't modify a bunch
3477 * of metadata or split extents when writing the cache out, which means
3478 * we can enospc if we are heavily fragmented in addition to just normal
3479 * out of space conditions. So if we hit this just skip setting up any
3480 * other block groups for this transaction, maybe we'll unpin enough
3481 * space the next time around.
3482 */
Josef Bacik2b209822010-12-03 13:17:53 -05003483 if (!ret)
3484 dcs = BTRFS_DC_SETUP;
Josef Bacik2968b1f2015-10-01 12:55:18 -04003485 else if (ret == -ENOSPC)
3486 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
Josef Bacikc09544e2011-08-30 10:19:10 -04003487
Josef Bacik0af3d002010-06-21 14:48:16 -04003488out_put:
3489 iput(inode);
3490out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003491 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003492out:
3493 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003494 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003495 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003496 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003497 spin_unlock(&block_group->lock);
3498
3499 return ret;
3500}
3501
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003502int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003503 struct btrfs_fs_info *fs_info)
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003504{
3505 struct btrfs_block_group_cache *cache, *tmp;
3506 struct btrfs_transaction *cur_trans = trans->transaction;
3507 struct btrfs_path *path;
3508
3509 if (list_empty(&cur_trans->dirty_bgs) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003510 !btrfs_test_opt(fs_info, SPACE_CACHE))
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003511 return 0;
3512
3513 path = btrfs_alloc_path();
3514 if (!path)
3515 return -ENOMEM;
3516
3517 /* Could add new block groups, use _safe just in case */
3518 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
3519 dirty_list) {
3520 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3521 cache_save_setup(cache, trans, path);
3522 }
3523
3524 btrfs_free_path(path);
3525 return 0;
3526}
3527
Chris Mason1bbc6212015-04-06 12:46:08 -07003528/*
3529 * transaction commit does final block group cache writeback during a
3530 * critical section where nothing is allowed to change the FS. This is
3531 * required in order for the cache to actually match the block group,
3532 * but can introduce a lot of latency into the commit.
3533 *
3534 * So, btrfs_start_dirty_block_groups is here to kick off block group
3535 * cache IO. There's a chance we'll have to redo some of it if the
3536 * block group changes again during the commit, but it greatly reduces
3537 * the commit latency by getting rid of the easy block groups while
3538 * we're still allowing others to join the commit.
3539 */
3540int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003541 struct btrfs_fs_info *fs_info)
Chris Mason1bbc6212015-04-06 12:46:08 -07003542{
3543 struct btrfs_block_group_cache *cache;
3544 struct btrfs_transaction *cur_trans = trans->transaction;
3545 int ret = 0;
3546 int should_put;
3547 struct btrfs_path *path = NULL;
3548 LIST_HEAD(dirty);
3549 struct list_head *io = &cur_trans->io_bgs;
3550 int num_started = 0;
3551 int loops = 0;
3552
3553 spin_lock(&cur_trans->dirty_bgs_lock);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003554 if (list_empty(&cur_trans->dirty_bgs)) {
3555 spin_unlock(&cur_trans->dirty_bgs_lock);
3556 return 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003557 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003558 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Chris Mason1bbc6212015-04-06 12:46:08 -07003559 spin_unlock(&cur_trans->dirty_bgs_lock);
3560
3561again:
Chris Mason1bbc6212015-04-06 12:46:08 -07003562 /*
3563 * make sure all the block groups on our dirty list actually
3564 * exist
3565 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003566 btrfs_create_pending_block_groups(trans, fs_info);
Chris Mason1bbc6212015-04-06 12:46:08 -07003567
3568 if (!path) {
3569 path = btrfs_alloc_path();
3570 if (!path)
3571 return -ENOMEM;
3572 }
3573
Filipe Mananab58d1a92015-04-25 18:29:16 +01003574 /*
3575 * cache_write_mutex is here only to save us from balance or automatic
3576 * removal of empty block groups deleting this block group while we are
3577 * writing out the cache
3578 */
3579 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003580 while (!list_empty(&dirty)) {
3581 cache = list_first_entry(&dirty,
3582 struct btrfs_block_group_cache,
3583 dirty_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07003584 /*
3585 * this can happen if something re-dirties a block
3586 * group that is already under IO. Just wait for it to
3587 * finish and then do it all again
3588 */
3589 if (!list_empty(&cache->io_list)) {
3590 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003591 btrfs_wait_cache_io(trans, cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003592 btrfs_put_block_group(cache);
3593 }
3594
3595
3596 /*
3597 * btrfs_wait_cache_io uses the cache->dirty_list to decide
3598 * if it should update the cache_state. Don't delete
3599 * until after we wait.
3600 *
3601 * Since we're not running in the commit critical section
3602 * we need the dirty_bgs_lock to protect from update_block_group
3603 */
3604 spin_lock(&cur_trans->dirty_bgs_lock);
3605 list_del_init(&cache->dirty_list);
3606 spin_unlock(&cur_trans->dirty_bgs_lock);
3607
3608 should_put = 1;
3609
3610 cache_save_setup(cache, trans, path);
3611
3612 if (cache->disk_cache_state == BTRFS_DC_SETUP) {
3613 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003614 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003615 cache, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07003616 if (ret == 0 && cache->io_ctl.inode) {
3617 num_started++;
3618 should_put = 0;
3619
3620 /*
3621 * the cache_write_mutex is protecting
3622 * the io_list
3623 */
3624 list_add_tail(&cache->io_list, io);
3625 } else {
3626 /*
3627 * if we failed to write the cache, the
3628 * generation will be bad and life goes on
3629 */
3630 ret = 0;
3631 }
3632 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003633 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003634 ret = write_one_cache_group(trans, fs_info,
3635 path, cache);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003636 /*
3637 * Our block group might still be attached to the list
3638 * of new block groups in the transaction handle of some
3639 * other task (struct btrfs_trans_handle->new_bgs). This
3640 * means its block group item isn't yet in the extent
3641 * tree. If this happens ignore the error, as we will
3642 * try again later in the critical section of the
3643 * transaction commit.
3644 */
3645 if (ret == -ENOENT) {
3646 ret = 0;
3647 spin_lock(&cur_trans->dirty_bgs_lock);
3648 if (list_empty(&cache->dirty_list)) {
3649 list_add_tail(&cache->dirty_list,
3650 &cur_trans->dirty_bgs);
3651 btrfs_get_block_group(cache);
3652 }
3653 spin_unlock(&cur_trans->dirty_bgs_lock);
3654 } else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003655 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003656 }
3657 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003658
3659 /* if its not on the io list, we need to put the block group */
3660 if (should_put)
3661 btrfs_put_block_group(cache);
3662
3663 if (ret)
3664 break;
Filipe Mananab58d1a92015-04-25 18:29:16 +01003665
3666 /*
3667 * Avoid blocking other tasks for too long. It might even save
3668 * us from writing caches for block groups that are going to be
3669 * removed.
3670 */
3671 mutex_unlock(&trans->transaction->cache_write_mutex);
3672 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003673 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003674 mutex_unlock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003675
3676 /*
3677 * go through delayed refs for all the stuff we've just kicked off
3678 * and then loop back (just once)
3679 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003680 ret = btrfs_run_delayed_refs(trans, fs_info, 0);
Chris Mason1bbc6212015-04-06 12:46:08 -07003681 if (!ret && loops == 0) {
3682 loops++;
3683 spin_lock(&cur_trans->dirty_bgs_lock);
3684 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003685 /*
3686 * dirty_bgs_lock protects us from concurrent block group
3687 * deletes too (not just cache_write_mutex).
3688 */
3689 if (!list_empty(&dirty)) {
3690 spin_unlock(&cur_trans->dirty_bgs_lock);
3691 goto again;
3692 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003693 spin_unlock(&cur_trans->dirty_bgs_lock);
Liu Boc79a1752016-07-20 17:44:12 -07003694 } else if (ret < 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003695 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
Chris Mason1bbc6212015-04-06 12:46:08 -07003696 }
3697
3698 btrfs_free_path(path);
3699 return ret;
3700}
3701
Chris Mason96b51792007-10-15 16:15:19 -04003702int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003703 struct btrfs_fs_info *fs_info)
Chris Mason9078a3e2007-04-26 16:46:15 -04003704{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003705 struct btrfs_block_group_cache *cache;
Josef Bacikce93ec52014-11-17 15:45:48 -05003706 struct btrfs_transaction *cur_trans = trans->transaction;
3707 int ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003708 int should_put;
Chris Mason9078a3e2007-04-26 16:46:15 -04003709 struct btrfs_path *path;
Chris Mason1bbc6212015-04-06 12:46:08 -07003710 struct list_head *io = &cur_trans->io_bgs;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003711 int num_started = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003712
3713 path = btrfs_alloc_path();
3714 if (!path)
3715 return -ENOMEM;
3716
Josef Bacikce93ec52014-11-17 15:45:48 -05003717 /*
Filipe Mananae44081e2015-12-18 03:02:48 +00003718 * Even though we are in the critical section of the transaction commit,
3719 * we can still have concurrent tasks adding elements to this
3720 * transaction's list of dirty block groups. These tasks correspond to
3721 * endio free space workers started when writeback finishes for a
3722 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can
3723 * allocate new block groups as a result of COWing nodes of the root
3724 * tree when updating the free space inode. The writeback for the space
3725 * caches is triggered by an earlier call to
3726 * btrfs_start_dirty_block_groups() and iterations of the following
3727 * loop.
3728 * Also we want to do the cache_save_setup first and then run the
Josef Bacikce93ec52014-11-17 15:45:48 -05003729 * delayed refs to make sure we have the best chance at doing this all
3730 * in one shot.
3731 */
Filipe Mananae44081e2015-12-18 03:02:48 +00003732 spin_lock(&cur_trans->dirty_bgs_lock);
Josef Bacikce93ec52014-11-17 15:45:48 -05003733 while (!list_empty(&cur_trans->dirty_bgs)) {
3734 cache = list_first_entry(&cur_trans->dirty_bgs,
3735 struct btrfs_block_group_cache,
3736 dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003737
3738 /*
3739 * this can happen if cache_save_setup re-dirties a block
3740 * group that is already under IO. Just wait for it to
3741 * finish and then do it all again
3742 */
3743 if (!list_empty(&cache->io_list)) {
Filipe Mananae44081e2015-12-18 03:02:48 +00003744 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003745 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003746 btrfs_wait_cache_io(trans, cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003747 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003748 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003749 }
3750
Chris Mason1bbc6212015-04-06 12:46:08 -07003751 /*
3752 * don't remove from the dirty list until after we've waited
3753 * on any pending IO
3754 */
Josef Bacikce93ec52014-11-17 15:45:48 -05003755 list_del_init(&cache->dirty_list);
Filipe Mananae44081e2015-12-18 03:02:48 +00003756 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003757 should_put = 1;
3758
Chris Mason1bbc6212015-04-06 12:46:08 -07003759 cache_save_setup(cache, trans, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003760
Josef Bacikce93ec52014-11-17 15:45:48 -05003761 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003762 ret = btrfs_run_delayed_refs(trans, fs_info,
3763 (unsigned long) -1);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003764
3765 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3766 cache->io_ctl.inode = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003767 ret = btrfs_write_out_cache(fs_info, trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003768 cache, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003769 if (ret == 0 && cache->io_ctl.inode) {
3770 num_started++;
3771 should_put = 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003772 list_add_tail(&cache->io_list, io);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003773 } else {
3774 /*
3775 * if we failed to write the cache, the
3776 * generation will be bad and life goes on
3777 */
3778 ret = 0;
3779 }
3780 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003781 if (!ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003782 ret = write_one_cache_group(trans, fs_info,
3783 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003784 /*
3785 * One of the free space endio workers might have
3786 * created a new block group while updating a free space
3787 * cache's inode (at inode.c:btrfs_finish_ordered_io())
3788 * and hasn't released its transaction handle yet, in
3789 * which case the new block group is still attached to
3790 * its transaction handle and its creation has not
3791 * finished yet (no block group item in the extent tree
3792 * yet, etc). If this is the case, wait for all free
3793 * space endio workers to finish and retry. This is a
3794 * a very rare case so no need for a more efficient and
3795 * complex approach.
3796 */
3797 if (ret == -ENOENT) {
3798 wait_event(cur_trans->writer_wait,
3799 atomic_read(&cur_trans->num_writers) == 1);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003800 ret = write_one_cache_group(trans, fs_info,
3801 path, cache);
Filipe Manana2bc0bb52015-12-30 02:42:30 +00003802 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003803 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003804 btrfs_abort_transaction(trans, ret);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003805 }
Chris Masonc9dc4c62015-04-04 17:14:42 -07003806
3807 /* if its not on the io list, we need to put the block group */
3808 if (should_put)
3809 btrfs_put_block_group(cache);
Filipe Mananae44081e2015-12-18 03:02:48 +00003810 spin_lock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003811 }
Filipe Mananae44081e2015-12-18 03:02:48 +00003812 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003813
Chris Mason1bbc6212015-04-06 12:46:08 -07003814 while (!list_empty(io)) {
3815 cache = list_first_entry(io, struct btrfs_block_group_cache,
Chris Masonc9dc4c62015-04-04 17:14:42 -07003816 io_list);
3817 list_del_init(&cache->io_list);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -04003818 btrfs_wait_cache_io(trans, cache, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003819 btrfs_put_block_group(cache);
3820 }
3821
Chris Mason9078a3e2007-04-26 16:46:15 -04003822 btrfs_free_path(path);
Josef Bacikce93ec52014-11-17 15:45:48 -05003823 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003824}
3825
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003826int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
Yan Zhengd2fb3432008-12-11 16:30:39 -05003827{
3828 struct btrfs_block_group_cache *block_group;
3829 int readonly = 0;
3830
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003831 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003832 if (!block_group || block_group->ro)
3833 readonly = 1;
3834 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003835 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003836 return readonly;
3837}
3838
Filipe Mananaf78c4362016-05-09 13:15:41 +01003839bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3840{
3841 struct btrfs_block_group_cache *bg;
3842 bool ret = true;
3843
3844 bg = btrfs_lookup_block_group(fs_info, bytenr);
3845 if (!bg)
3846 return false;
3847
3848 spin_lock(&bg->lock);
3849 if (bg->ro)
3850 ret = false;
3851 else
3852 atomic_inc(&bg->nocow_writers);
3853 spin_unlock(&bg->lock);
3854
3855 /* no put on block group, done by btrfs_dec_nocow_writers */
3856 if (!ret)
3857 btrfs_put_block_group(bg);
3858
3859 return ret;
3860
3861}
3862
3863void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr)
3864{
3865 struct btrfs_block_group_cache *bg;
3866
3867 bg = btrfs_lookup_block_group(fs_info, bytenr);
3868 ASSERT(bg);
3869 if (atomic_dec_and_test(&bg->nocow_writers))
3870 wake_up_atomic_t(&bg->nocow_writers);
3871 /*
3872 * Once for our lookup and once for the lookup done by a previous call
3873 * to btrfs_inc_nocow_writers()
3874 */
3875 btrfs_put_block_group(bg);
3876 btrfs_put_block_group(bg);
3877}
3878
3879static int btrfs_wait_nocow_writers_atomic_t(atomic_t *a)
3880{
3881 schedule();
3882 return 0;
3883}
3884
3885void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg)
3886{
3887 wait_on_atomic_t(&bg->nocow_writers,
3888 btrfs_wait_nocow_writers_atomic_t,
3889 TASK_UNINTERRUPTIBLE);
3890}
3891
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003892static const char *alloc_name(u64 flags)
3893{
3894 switch (flags) {
3895 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
3896 return "mixed";
3897 case BTRFS_BLOCK_GROUP_METADATA:
3898 return "metadata";
3899 case BTRFS_BLOCK_GROUP_DATA:
3900 return "data";
3901 case BTRFS_BLOCK_GROUP_SYSTEM:
3902 return "system";
3903 default:
3904 WARN_ON(1);
3905 return "invalid-combination";
3906 };
3907}
3908
Chris Mason593060d2008-03-25 16:50:33 -04003909static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3910 u64 total_bytes, u64 bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -04003911 u64 bytes_readonly,
Chris Mason593060d2008-03-25 16:50:33 -04003912 struct btrfs_space_info **space_info)
3913{
3914 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003915 int i;
3916 int factor;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003917 int ret;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003918
3919 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3920 BTRFS_BLOCK_GROUP_RAID10))
3921 factor = 2;
3922 else
3923 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003924
3925 found = __find_space_info(info, flags);
3926 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003927 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003928 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003929 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003930 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003931 found->disk_used += bytes_used * factor;
Josef Bacike40edf22016-03-25 13:25:47 -04003932 found->bytes_readonly += bytes_readonly;
Filipe Manana2e6e5182015-05-12 00:28:11 +01003933 if (total_bytes > 0)
3934 found->full = 0;
Josef Bacik957780e2016-05-17 13:30:55 -04003935 space_info_add_new_bytes(info, found, total_bytes -
3936 bytes_used - bytes_readonly);
Josef Bacik25179202008-10-29 14:49:05 -04003937 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003938 *space_info = found;
3939 return 0;
3940 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003941 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003942 if (!found)
3943 return -ENOMEM;
3944
Tejun Heo908c7f12014-09-08 09:51:29 +09003945 ret = percpu_counter_init(&found->total_bytes_pinned, 0, GFP_KERNEL);
Josef Bacikb150a4f2013-06-19 15:00:04 -04003946 if (ret) {
3947 kfree(found);
3948 return ret;
3949 }
3950
Jeff Mahoneyc1895442014-05-27 12:59:57 -04003951 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003952 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003953 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003954 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003955 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003956 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003957 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003958 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003959 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003960 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003961 found->bytes_reserved = 0;
Josef Bacike40edf22016-03-25 13:25:47 -04003962 found->bytes_readonly = bytes_readonly;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003963 found->bytes_may_use = 0;
Filipe Manana6af3e3a2015-09-07 10:41:12 +01003964 found->full = 0;
Josef Bacik4f4db212015-09-29 11:40:47 -04003965 found->max_extent_size = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003966 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003967 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003968 found->flush = 0;
3969 init_waitqueue_head(&found->wait);
Josef Bacik633c0aa2014-10-31 09:49:34 -04003970 INIT_LIST_HEAD(&found->ro_bgs);
Josef Bacik957780e2016-05-17 13:30:55 -04003971 INIT_LIST_HEAD(&found->tickets);
3972 INIT_LIST_HEAD(&found->priority_tickets);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003973
3974 ret = kobject_init_and_add(&found->kobj, &space_info_ktype,
3975 info->space_info_kobj, "%s",
3976 alloc_name(found->flags));
3977 if (ret) {
3978 kfree(found);
3979 return ret;
3980 }
3981
Chris Mason593060d2008-03-25 16:50:33 -04003982 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003983 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003984 if (flags & BTRFS_BLOCK_GROUP_DATA)
3985 info->data_sinfo = found;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003986
3987 return ret;
Chris Mason593060d2008-03-25 16:50:33 -04003988}
3989
Chris Mason8790d502008-04-03 16:29:03 -04003990static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3991{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003992 u64 extra_flags = chunk_to_extended(flags) &
3993 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003994
Miao Xiede98ced2013-01-29 10:13:12 +00003995 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003996 if (flags & BTRFS_BLOCK_GROUP_DATA)
3997 fs_info->avail_data_alloc_bits |= extra_flags;
3998 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3999 fs_info->avail_metadata_alloc_bits |= extra_flags;
4000 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
4001 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004002 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04004003}
Chris Mason593060d2008-03-25 16:50:33 -04004004
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004005/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004006 * returns target flags in extended format or 0 if restripe for this
4007 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04004008 *
4009 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004010 */
4011static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
4012{
4013 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4014 u64 target = 0;
4015
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004016 if (!bctl)
4017 return 0;
4018
4019 if (flags & BTRFS_BLOCK_GROUP_DATA &&
4020 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4021 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
4022 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
4023 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4024 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
4025 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
4026 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
4027 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
4028 }
4029
4030 return target;
4031}
4032
4033/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004034 * @flags: available profiles in extended format (see ctree.h)
4035 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004036 * Returns reduced profile in chunk format. If profile changing is in
4037 * progress (either running or paused) picks the target profile (if it's
4038 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02004039 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004040static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04004041{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004042 u64 num_devices = fs_info->fs_devices->rw_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004043 u64 target;
Zhao Lei9c170b22015-09-15 21:08:08 +08004044 u64 raid_type;
4045 u64 allowed = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004046
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004047 /*
4048 * see if restripe for this chunk_type is in progress, if so
4049 * try to reduce to the target profile
4050 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004051 spin_lock(&fs_info->balance_lock);
4052 target = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004053 if (target) {
4054 /* pick target profile only if it's already available */
4055 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004056 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03004057 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004058 }
4059 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004060 spin_unlock(&fs_info->balance_lock);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004061
David Woodhouse53b381b2013-01-29 18:40:14 -05004062 /* First, mask out the RAID levels which aren't possible */
Zhao Lei9c170b22015-09-15 21:08:08 +08004063 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
4064 if (num_devices >= btrfs_raid_array[raid_type].devs_min)
4065 allowed |= btrfs_raid_group[raid_type];
4066 }
4067 allowed &= flags;
Chris Masona061fc82008-05-07 11:43:44 -04004068
Zhao Lei9c170b22015-09-15 21:08:08 +08004069 if (allowed & BTRFS_BLOCK_GROUP_RAID6)
4070 allowed = BTRFS_BLOCK_GROUP_RAID6;
4071 else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
4072 allowed = BTRFS_BLOCK_GROUP_RAID5;
4073 else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
4074 allowed = BTRFS_BLOCK_GROUP_RAID10;
4075 else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
4076 allowed = BTRFS_BLOCK_GROUP_RAID1;
4077 else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
4078 allowed = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04004079
Zhao Lei9c170b22015-09-15 21:08:08 +08004080 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
Chris Masonec44a352008-04-28 15:29:52 -04004081
Zhao Lei9c170b22015-09-15 21:08:08 +08004082 return extended_to_chunk(flags | allowed);
Chris Masonec44a352008-04-28 15:29:52 -04004083}
4084
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004085static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05004086{
Miao Xiede98ced2013-01-29 10:13:12 +00004087 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004088 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00004089
4090 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01004091 flags = orig_flags;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004092 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00004093
4094 if (flags & BTRFS_BLOCK_GROUP_DATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004095 flags |= fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004096 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004097 flags |= fs_info->avail_system_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004098 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004099 flags |= fs_info->avail_metadata_alloc_bits;
4100 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02004101
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004102 return btrfs_reduce_alloc_profile(fs_info, flags);
Yan, Zhengb742bb82010-05-16 10:46:24 -04004103}
Josef Bacik6a632092009-02-20 11:00:09 -05004104
Miao Xie6d07bce2011-01-05 10:07:31 +00004105u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04004106{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004107 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengb742bb82010-05-16 10:46:24 -04004108 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05004109 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004110
Yan, Zhengb742bb82010-05-16 10:46:24 -04004111 if (data)
4112 flags = BTRFS_BLOCK_GROUP_DATA;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004113 else if (root == fs_info->chunk_root)
Yan, Zhengb742bb82010-05-16 10:46:24 -04004114 flags = BTRFS_BLOCK_GROUP_SYSTEM;
4115 else
4116 flags = BTRFS_BLOCK_GROUP_METADATA;
4117
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004118 ret = get_alloc_profile(fs_info, flags);
David Woodhouse53b381b2013-01-29 18:40:14 -05004119 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004120}
4121
Qu Wenruo4ceff072015-09-08 17:22:42 +08004122int btrfs_alloc_data_chunk_ondemand(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05004123{
4124 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004125 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004126 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00004127 u64 used;
Zhao Lei94b947b2015-02-14 13:23:45 +08004128 int ret = 0;
Zhao Leic99f1b02015-03-02 19:32:20 +08004129 int need_commit = 2;
4130 int have_pinned_space;
Josef Bacik6a632092009-02-20 11:00:09 -05004131
4132 /* make sure bytes are sectorsize aligned */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004133 bytes = ALIGN(bytes, fs_info->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05004134
Miao Xie9dced182013-10-25 17:33:36 +08004135 if (btrfs_is_free_space_inode(inode)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004136 need_commit = 0;
Miao Xie9dced182013-10-25 17:33:36 +08004137 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04004138 }
4139
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004140 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04004141 if (!data_sinfo)
4142 goto alloc;
4143
Josef Bacik6a632092009-02-20 11:00:09 -05004144again:
4145 /* make sure we have enough space to handle the data first */
4146 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004147 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
4148 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
4149 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00004150
4151 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004152 struct btrfs_trans_handle *trans;
4153
Josef Bacik6a632092009-02-20 11:00:09 -05004154 /*
4155 * if we don't have enough free bytes in this space then we need
4156 * to alloc a new chunk.
4157 */
Zhao Leib9fd47c2015-02-09 14:40:20 +08004158 if (!data_sinfo->full) {
Josef Bacik6a632092009-02-20 11:00:09 -05004159 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05004160
Chris Mason0e4f8f82011-04-15 16:05:44 -04004161 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05004162 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04004163alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05004164 alloc_target = btrfs_get_alloc_profile(root, 1);
Miao Xie9dced182013-10-25 17:33:36 +08004165 /*
4166 * It is ugly that we don't call nolock join
4167 * transaction for the free space inode case here.
4168 * But it is safe because we only do the data space
4169 * reservation for the free space cache in the
4170 * transaction context, the common join transaction
4171 * just increase the counter of the current transaction
4172 * handler, doesn't try to acquire the trans_lock of
4173 * the fs.
4174 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004175 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004176 if (IS_ERR(trans))
4177 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05004178
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004179 ret = do_chunk_alloc(trans, fs_info, alloc_target,
Chris Mason0e4f8f82011-04-15 16:05:44 -04004180 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004181 btrfs_end_transaction(trans);
Miao Xied52a5b52011-01-05 10:07:18 +00004182 if (ret < 0) {
4183 if (ret != -ENOSPC)
4184 return ret;
Zhao Leic99f1b02015-03-02 19:32:20 +08004185 else {
4186 have_pinned_space = 1;
Miao Xied52a5b52011-01-05 10:07:18 +00004187 goto commit_trans;
Zhao Leic99f1b02015-03-02 19:32:20 +08004188 }
Miao Xied52a5b52011-01-05 10:07:18 +00004189 }
Chris Mason33b4d472009-09-22 14:45:50 -04004190
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004191 if (!data_sinfo)
4192 data_sinfo = fs_info->data_sinfo;
4193
Josef Bacik6a632092009-02-20 11:00:09 -05004194 goto again;
4195 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004196
4197 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04004198 * If we don't have enough pinned space to deal with this
Zhao Lei94b947b2015-02-14 13:23:45 +08004199 * allocation, and no removed chunk in current transaction,
4200 * don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004201 */
Zhao Leic99f1b02015-03-02 19:32:20 +08004202 have_pinned_space = percpu_counter_compare(
4203 &data_sinfo->total_bytes_pinned,
4204 used + bytes - data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05004205 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004206
4207 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00004208commit_trans:
Zhao Leic99f1b02015-03-02 19:32:20 +08004209 if (need_commit &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004210 !atomic_read(&fs_info->open_ioctl_trans)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004211 need_commit--;
Josef Bacikb150a4f2013-06-19 15:00:04 -04004212
Zhao Leie1746e82015-12-01 18:39:40 +08004213 if (need_commit > 0) {
4214 btrfs_start_delalloc_roots(fs_info, 0, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004215 btrfs_wait_ordered_roots(fs_info, -1, 0,
4216 (u64)-1);
Zhao Leie1746e82015-12-01 18:39:40 +08004217 }
Zhao Lei9a4e7272015-04-09 12:34:43 +08004218
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004219 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004220 if (IS_ERR(trans))
4221 return PTR_ERR(trans);
Zhao Leic99f1b02015-03-02 19:32:20 +08004222 if (have_pinned_space >= 0 ||
Josef Bacik3204d332015-09-24 10:46:10 -04004223 test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
4224 &trans->transaction->flags) ||
Zhao Leic99f1b02015-03-02 19:32:20 +08004225 need_commit > 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004226 ret = btrfs_commit_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004227 if (ret)
4228 return ret;
Zhao Leid7c15172015-02-26 10:49:20 +08004229 /*
Filipe Mananac2d6cb12016-01-15 11:05:12 +00004230 * The cleaner kthread might still be doing iput
4231 * operations. Wait for it to finish so that
4232 * more space is released.
Zhao Leid7c15172015-02-26 10:49:20 +08004233 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004234 mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
4235 mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
Zhao Lei94b947b2015-02-14 13:23:45 +08004236 goto again;
4237 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004238 btrfs_end_transaction(trans);
Zhao Lei94b947b2015-02-14 13:23:45 +08004239 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004240 }
4241
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004242 trace_btrfs_space_reservation(fs_info,
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004243 "space_info:enospc",
4244 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004245 return -ENOSPC;
4246 }
4247 data_sinfo->bytes_may_use += bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004248 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004249 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004250 spin_unlock(&data_sinfo->lock);
4251
Dongsheng Yang237c0e92014-12-29 06:23:05 -05004252 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004253}
4254
4255/*
Qu Wenruo4ceff072015-09-08 17:22:42 +08004256 * New check_data_free_space() with ability for precious data reservation
4257 * Will replace old btrfs_check_data_free_space(), but for patch split,
4258 * add a new function first and then replace it.
4259 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004260int btrfs_check_data_free_space(struct inode *inode, u64 start, u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004261{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004262 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004263 int ret;
4264
4265 /* align the range */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004266 len = round_up(start + len, fs_info->sectorsize) -
4267 round_down(start, fs_info->sectorsize);
4268 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004269
4270 ret = btrfs_alloc_data_chunk_ondemand(inode, len);
4271 if (ret < 0)
4272 return ret;
4273
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004274 /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */
Qu Wenruo4ceff072015-09-08 17:22:42 +08004275 ret = btrfs_qgroup_reserve_data(inode, start, len);
Josef Bacik1e5ec2e2016-09-15 14:57:48 -04004276 if (ret)
4277 btrfs_free_reserved_data_space_noquota(inode, start, len);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004278 return ret;
4279}
4280
4281/*
Qu Wenruo4ceff072015-09-08 17:22:42 +08004282 * Called if we need to clear a data reservation for this inode
4283 * Normally in a error case.
4284 *
Qu Wenruo51773be2015-10-08 18:19:37 +08004285 * This one will *NOT* use accurate qgroup reserved space API, just for case
4286 * which we can't sleep and is sure it won't affect qgroup reserved space.
4287 * Like clear_bit_hook().
Qu Wenruo4ceff072015-09-08 17:22:42 +08004288 */
Qu Wenruo51773be2015-10-08 18:19:37 +08004289void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
4290 u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004291{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004292 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004293 struct btrfs_space_info *data_sinfo;
4294
4295 /* Make sure the range is aligned to sectorsize */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004296 len = round_up(start + len, fs_info->sectorsize) -
4297 round_down(start, fs_info->sectorsize);
4298 start = round_down(start, fs_info->sectorsize);
Qu Wenruo4ceff072015-09-08 17:22:42 +08004299
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004300 data_sinfo = fs_info->data_sinfo;
Qu Wenruo4ceff072015-09-08 17:22:42 +08004301 spin_lock(&data_sinfo->lock);
4302 if (WARN_ON(data_sinfo->bytes_may_use < len))
4303 data_sinfo->bytes_may_use = 0;
4304 else
4305 data_sinfo->bytes_may_use -= len;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004306 trace_btrfs_space_reservation(fs_info, "space_info",
Qu Wenruo4ceff072015-09-08 17:22:42 +08004307 data_sinfo->flags, len, 0);
4308 spin_unlock(&data_sinfo->lock);
4309}
4310
Qu Wenruo51773be2015-10-08 18:19:37 +08004311/*
4312 * Called if we need to clear a data reservation for this inode
4313 * Normally in a error case.
4314 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04004315 * This one will handle the per-inode data rsv map for accurate reserved
Qu Wenruo51773be2015-10-08 18:19:37 +08004316 * space framework.
4317 */
4318void btrfs_free_reserved_data_space(struct inode *inode, u64 start, u64 len)
4319{
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004320 struct btrfs_root *root = BTRFS_I(inode)->root;
4321
4322 /* Make sure the range is aligned to sectorsize */
Jeff Mahoneyda170662016-06-15 09:22:56 -04004323 len = round_up(start + len, root->fs_info->sectorsize) -
4324 round_down(start, root->fs_info->sectorsize);
4325 start = round_down(start, root->fs_info->sectorsize);
Jeff Mahoney0c476a52016-11-18 21:52:40 -05004326
Qu Wenruo51773be2015-10-08 18:19:37 +08004327 btrfs_free_reserved_data_space_noquota(inode, start, len);
4328 btrfs_qgroup_free_data(inode, start, len);
4329}
4330
Josef Bacik97e728d2009-04-21 17:40:57 -04004331static void force_metadata_allocation(struct btrfs_fs_info *info)
4332{
4333 struct list_head *head = &info->space_info;
4334 struct btrfs_space_info *found;
4335
4336 rcu_read_lock();
4337 list_for_each_entry_rcu(found, head, list) {
4338 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004339 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04004340 }
4341 rcu_read_unlock();
4342}
4343
Miao Xie3c76cd82013-04-25 10:12:38 +00004344static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
4345{
4346 return (global->size << 1);
4347}
4348
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004349static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
Josef Bacik698d0082012-09-12 14:08:47 -04004350 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04004351{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004352 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04004353 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04004354 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04004355 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04004356
Chris Mason0e4f8f82011-04-15 16:05:44 -04004357 if (force == CHUNK_ALLOC_FORCE)
4358 return 1;
4359
4360 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04004361 * We need to take into account the global rsv because for all intents
4362 * and purposes it's used space. Don't worry about locking the
4363 * global_rsv, it doesn't change except when the transaction commits.
4364 */
Josef Bacik54338b52012-08-14 16:20:52 -04004365 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Miao Xie3c76cd82013-04-25 10:12:38 +00004366 num_allocated += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04004367
4368 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04004369 * in limited mode, we want to have some free space up to
4370 * about 1% of the FS size.
4371 */
4372 if (force == CHUNK_ALLOC_LIMITED) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004373 thresh = btrfs_super_total_bytes(fs_info->super_copy);
Byongho Leeee221842015-12-15 01:42:10 +09004374 thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
Chris Mason0e4f8f82011-04-15 16:05:44 -04004375
4376 if (num_bytes - num_allocated < thresh)
4377 return 1;
4378 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004379
Byongho Leeee221842015-12-15 01:42:10 +09004380 if (num_allocated + SZ_2M < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04004381 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04004382 return 1;
4383}
4384
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004385static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004386{
4387 u64 num_dev;
4388
David Woodhouse53b381b2013-01-29 18:40:14 -05004389 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
4390 BTRFS_BLOCK_GROUP_RAID0 |
4391 BTRFS_BLOCK_GROUP_RAID5 |
4392 BTRFS_BLOCK_GROUP_RAID6))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004393 num_dev = fs_info->fs_devices->rw_devices;
Liu Bo15d1ff82012-03-29 09:57:44 -04004394 else if (type & BTRFS_BLOCK_GROUP_RAID1)
4395 num_dev = 2;
4396 else
4397 num_dev = 1; /* DUP or single */
4398
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004399 return num_dev;
Liu Bo15d1ff82012-03-29 09:57:44 -04004400}
4401
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004402/*
4403 * If @is_allocation is true, reserve space in the system space info necessary
4404 * for allocating a chunk, otherwise if it's false, reserve space necessary for
4405 * removing a chunk.
4406 */
4407void check_system_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004408 struct btrfs_fs_info *fs_info, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004409{
4410 struct btrfs_space_info *info;
4411 u64 left;
4412 u64 thresh;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004413 int ret = 0;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004414 u64 num_devs;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004415
4416 /*
4417 * Needed because we can end up allocating a system chunk and for an
4418 * atomic and race free space reservation in the chunk block reserve.
4419 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004420 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
Liu Bo15d1ff82012-03-29 09:57:44 -04004421
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004422 info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
Liu Bo15d1ff82012-03-29 09:57:44 -04004423 spin_lock(&info->lock);
4424 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004425 info->bytes_reserved - info->bytes_readonly -
4426 info->bytes_may_use;
Liu Bo15d1ff82012-03-29 09:57:44 -04004427 spin_unlock(&info->lock);
4428
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004429 num_devs = get_profile_num_devs(fs_info, type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004430
4431 /* num_devs device items to update and 1 chunk item to add or remove */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004432 thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) +
4433 btrfs_calc_trans_metadata_size(fs_info, 1);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004434
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004435 if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
4436 btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
4437 left, thresh, type);
4438 dump_space_info(fs_info, info, 0, 0);
Liu Bo15d1ff82012-03-29 09:57:44 -04004439 }
4440
4441 if (left < thresh) {
4442 u64 flags;
4443
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004444 flags = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004445 /*
4446 * Ignore failure to create system chunk. We might end up not
4447 * needing it, as we might not need to COW all nodes/leafs from
4448 * the paths we visit in the chunk tree (they were already COWed
4449 * or created in the current transaction for example).
4450 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004451 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004452 }
4453
4454 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004455 ret = btrfs_block_rsv_add(fs_info->chunk_root,
4456 &fs_info->chunk_block_rsv,
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004457 thresh, BTRFS_RESERVE_NO_FLUSH);
4458 if (!ret)
4459 trans->chunk_bytes_reserved += thresh;
Liu Bo15d1ff82012-03-29 09:57:44 -04004460 }
4461}
4462
Liu Bo28b737f2016-07-29 11:09:50 -07004463/*
4464 * If force is CHUNK_ALLOC_FORCE:
4465 * - return 1 if it successfully allocates a chunk,
4466 * - return errors including -ENOSPC otherwise.
4467 * If force is NOT CHUNK_ALLOC_FORCE:
4468 * - return 0 if it doesn't need to allocate a new chunk,
4469 * - return 1 if it successfully allocates a chunk,
4470 * - return errors including -ENOSPC otherwise.
4471 */
Chris Mason6324fbf2008-03-24 15:01:59 -04004472static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004473 struct btrfs_fs_info *fs_info, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04004474{
4475 struct btrfs_space_info *space_info;
Josef Bacik6d741192011-04-11 20:20:11 -04004476 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05004477 int ret = 0;
4478
Josef Bacikc6b305a2012-12-18 09:16:16 -05004479 /* Don't re-enter if we're already allocating a chunk */
4480 if (trans->allocating_chunk)
4481 return -ENOSPC;
4482
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004483 space_info = __find_space_info(fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04004484 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004485 ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004486 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04004487 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004488 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04004489
Josef Bacik6d741192011-04-11 20:20:11 -04004490again:
Josef Bacik25179202008-10-29 14:49:05 -04004491 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05004492 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004493 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04004494 if (space_info->full) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004495 if (should_alloc_chunk(fs_info, space_info, force))
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004496 ret = -ENOSPC;
4497 else
4498 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04004499 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004500 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04004501 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004502
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004503 if (!should_alloc_chunk(fs_info, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04004504 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04004505 return 0;
4506 } else if (space_info->chunk_alloc) {
4507 wait_for_alloc = 1;
4508 } else {
4509 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04004510 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004511
Josef Bacik25179202008-10-29 14:49:05 -04004512 spin_unlock(&space_info->lock);
4513
Josef Bacik6d741192011-04-11 20:20:11 -04004514 mutex_lock(&fs_info->chunk_mutex);
4515
4516 /*
4517 * The chunk_mutex is held throughout the entirety of a chunk
4518 * allocation, so once we've acquired the chunk_mutex we know that the
4519 * other guy is done and we need to recheck and see if we should
4520 * allocate.
4521 */
4522 if (wait_for_alloc) {
4523 mutex_unlock(&fs_info->chunk_mutex);
4524 wait_for_alloc = 0;
4525 goto again;
4526 }
4527
Josef Bacikc6b305a2012-12-18 09:16:16 -05004528 trans->allocating_chunk = true;
4529
Josef Bacik97e728d2009-04-21 17:40:57 -04004530 /*
Josef Bacik67377732010-09-16 16:19:09 -04004531 * If we have mixed data/metadata chunks we want to make sure we keep
4532 * allocating mixed chunks instead of individual chunks.
4533 */
4534 if (btrfs_mixed_space_info(space_info))
4535 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
4536
4537 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04004538 * if we're doing a data chunk, go ahead and make sure that
4539 * we keep a reasonable number of metadata chunks allocated in the
4540 * FS as well.
4541 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04004542 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04004543 fs_info->data_chunk_allocations++;
4544 if (!(fs_info->data_chunk_allocations %
4545 fs_info->metadata_ratio))
4546 force_metadata_allocation(fs_info);
4547 }
4548
Liu Bo15d1ff82012-03-29 09:57:44 -04004549 /*
4550 * Check if we have enough space in SYSTEM chunk because we may need
4551 * to update devices.
4552 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004553 check_system_chunk(trans, fs_info, flags);
Liu Bo15d1ff82012-03-29 09:57:44 -04004554
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004555 ret = btrfs_alloc_chunk(trans, fs_info, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05004556 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004557
Josef Bacik9ed74f22009-09-11 16:12:44 -04004558 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004559 if (ret < 0 && ret != -ENOSPC)
4560 goto out;
Chris Masond3977122009-01-05 21:25:51 -05004561 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04004562 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04004563 else
4564 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04004565
Chris Mason0e4f8f82011-04-15 16:05:44 -04004566 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004567out:
Josef Bacik6d741192011-04-11 20:20:11 -04004568 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004569 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03004570 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana00d80e32015-07-20 14:56:20 +01004571 /*
4572 * When we allocate a new chunk we reserve space in the chunk block
4573 * reserve to make sure we can COW nodes/leafs in the chunk tree or
4574 * add new nodes/leafs to it if we end up needing to do it when
4575 * inserting the chunk item and updating device items as part of the
4576 * second phase of chunk allocation, performed by
4577 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
4578 * large number of new block groups to create in our transaction
4579 * handle's new_bgs list to avoid exhausting the chunk block reserve
4580 * in extreme cases - like having a single transaction create many new
4581 * block groups when starting to write out the free space caches of all
4582 * the block groups that were made dirty during the lifetime of the
4583 * transaction.
4584 */
Filipe Mananad9a05402015-10-03 13:13:13 +01004585 if (trans->can_flush_pending_bgs &&
Byongho Leeee221842015-12-15 01:42:10 +09004586 trans->chunk_bytes_reserved >= (u64)SZ_2M) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004587 btrfs_create_pending_block_groups(trans, fs_info);
Filipe Manana00d80e32015-07-20 14:56:20 +01004588 btrfs_trans_release_chunk_metadata(trans);
4589 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004590 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04004591}
4592
Josef Bacika80c8dc2012-09-06 16:59:33 -04004593static int can_overcommit(struct btrfs_root *root,
4594 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00004595 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004596{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004597 struct btrfs_fs_info *fs_info = root->fs_info;
4598 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04004599 u64 profile;
Miao Xie3c76cd82013-04-25 10:12:38 +00004600 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004601 u64 avail;
4602 u64 used;
4603
Josef Bacik957780e2016-05-17 13:30:55 -04004604 /* Don't overcommit when in mixed mode. */
4605 if (space_info->flags & BTRFS_BLOCK_GROUP_DATA)
4606 return 0;
4607
Josef Bacik957780e2016-05-17 13:30:55 -04004608 profile = btrfs_get_alloc_profile(root, 0);
Josef Bacika80c8dc2012-09-06 16:59:33 -04004609 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05004610 space_info->bytes_pinned + space_info->bytes_readonly;
4611
Josef Bacik96f1bb52013-01-30 17:02:51 -05004612 /*
4613 * We only want to allow over committing if we have lots of actual space
4614 * free, but if we don't have enough space to handle the global reserve
4615 * space then we could end up having a real enospc problem when trying
4616 * to allocate a chunk or some other such important allocation.
4617 */
Miao Xie3c76cd82013-04-25 10:12:38 +00004618 spin_lock(&global_rsv->lock);
4619 space_size = calc_global_rsv_need_space(global_rsv);
4620 spin_unlock(&global_rsv->lock);
4621 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05004622 return 0;
4623
4624 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004625
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004626 spin_lock(&fs_info->free_chunk_lock);
4627 avail = fs_info->free_chunk_space;
4628 spin_unlock(&fs_info->free_chunk_lock);
Josef Bacika80c8dc2012-09-06 16:59:33 -04004629
4630 /*
4631 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05004632 * space is actually useable. For raid56, the space info used
4633 * doesn't include the parity drive, so we don't have to
4634 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04004635 */
4636 if (profile & (BTRFS_BLOCK_GROUP_DUP |
4637 BTRFS_BLOCK_GROUP_RAID1 |
4638 BTRFS_BLOCK_GROUP_RAID10))
4639 avail >>= 1;
4640
4641 /*
Miao Xie561c2942012-10-16 11:32:18 +00004642 * If we aren't flushing all things, let us overcommit up to
4643 * 1/2th of the space. If we can flush, don't let us overcommit
4644 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04004645 */
Miao Xie08e007d2012-10-16 11:33:38 +00004646 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04004647 avail >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004648 else
Josef Bacik14575ae2013-09-17 10:48:00 -04004649 avail >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004650
Josef Bacik14575ae2013-09-17 10:48:00 -04004651 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004652 return 1;
4653 return 0;
4654}
4655
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004656static void btrfs_writeback_inodes_sb_nr(struct btrfs_fs_info *fs_info,
Miao Xie6c255e62014-03-06 13:55:01 +08004657 unsigned long nr_pages, int nr_items)
Miao Xieda633a42012-12-20 11:19:09 +00004658{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004659 struct super_block *sb = fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00004660
Josef Bacik925a6ef2013-06-20 12:31:27 -04004661 if (down_read_trylock(&sb->s_umount)) {
4662 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4663 up_read(&sb->s_umount);
4664 } else {
Miao Xieda633a42012-12-20 11:19:09 +00004665 /*
4666 * We needn't worry the filesystem going from r/w to r/o though
4667 * we don't acquire ->s_umount mutex, because the filesystem
4668 * should guarantee the delalloc inodes list be empty after
4669 * the filesystem is readonly(all dirty pages are written to
4670 * the disk).
4671 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004672 btrfs_start_delalloc_roots(fs_info, 0, nr_items);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004673 if (!current->journal_info)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004674 btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1);
Miao Xieda633a42012-12-20 11:19:09 +00004675 }
4676}
4677
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004678static inline int calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,
4679 u64 to_reclaim)
Miao Xie18cd8ea2013-11-04 23:13:22 +08004680{
4681 u64 bytes;
4682 int nr;
4683
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004684 bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
Miao Xie18cd8ea2013-11-04 23:13:22 +08004685 nr = (int)div64_u64(to_reclaim, bytes);
4686 if (!nr)
4687 nr = 1;
4688 return nr;
4689}
4690
Byongho Leeee221842015-12-15 01:42:10 +09004691#define EXTENT_SIZE_PER_ITEM SZ_256K
Miao Xiec61a16a2013-11-04 23:13:23 +08004692
Yan, Zheng5da9d012010-05-16 10:46:25 -04004693/*
4694 * shrink metadata reservation for delalloc
4695 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04004696static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4697 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004698{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004699 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004700 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004701 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004702 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004703 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004704 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004705 long time_left;
Miao Xied3ee29e32013-11-04 23:13:20 +08004706 unsigned long nr_pages;
4707 int loops;
Miao Xieb0244192013-11-04 23:13:25 +08004708 int items;
Miao Xie08e007d2012-10-16 11:33:38 +00004709 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004710
Miao Xiec61a16a2013-11-04 23:13:23 +08004711 /* Calc the number of the pages we need flush for space reservation */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004712 items = calc_reclaim_items_nr(fs_info, to_reclaim);
Adam Borowski8eb0dfd2016-05-08 15:08:00 +02004713 to_reclaim = (u64)items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004714
Josef Bacik663350a2011-11-03 22:54:25 -04004715 trans = (struct btrfs_trans_handle *)current->journal_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004716 block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004717 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04004718
Miao Xie963d6782013-01-29 10:10:51 +00004719 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004720 &fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004721 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004722 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004723 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004724 if (wait_ordered)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004725 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004726 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004727 }
4728
Miao Xied3ee29e32013-11-04 23:13:20 +08004729 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004730 while (delalloc_bytes && loops < 3) {
4731 max_reclaim = min(delalloc_bytes, to_reclaim);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004732 nr_pages = max_reclaim >> PAGE_SHIFT;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004733 btrfs_writeback_inodes_sb_nr(fs_info, nr_pages, items);
Josef Bacikdea31f52012-09-06 16:47:00 -04004734 /*
4735 * We need to wait for the async pages to actually start before
4736 * we do anything.
4737 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004738 max_reclaim = atomic_read(&fs_info->async_delalloc_pages);
Miao Xie9f3a0742013-11-04 23:13:24 +08004739 if (!max_reclaim)
4740 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004741
Miao Xie9f3a0742013-11-04 23:13:24 +08004742 if (max_reclaim <= nr_pages)
4743 max_reclaim = 0;
4744 else
4745 max_reclaim -= nr_pages;
4746
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004747 wait_event(fs_info->async_submit_wait,
4748 atomic_read(&fs_info->async_delalloc_pages) <=
Miao Xie9f3a0742013-11-04 23:13:24 +08004749 (int)max_reclaim);
4750skip_async:
Miao Xie08e007d2012-10-16 11:33:38 +00004751 if (!trans)
4752 flush = BTRFS_RESERVE_FLUSH_ALL;
4753 else
4754 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04004755 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00004756 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004757 spin_unlock(&space_info->lock);
4758 break;
4759 }
Josef Bacik957780e2016-05-17 13:30:55 -04004760 if (list_empty(&space_info->tickets) &&
4761 list_empty(&space_info->priority_tickets)) {
4762 spin_unlock(&space_info->lock);
4763 break;
4764 }
Josef Bacik0019f102010-10-15 15:18:40 -04004765 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004766
Chris Mason36e39c42011-03-12 07:08:42 -05004767 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004768 if (wait_ordered && !trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004769 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
Josef Bacikf104d042011-10-14 13:56:58 -04004770 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004771 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004772 if (time_left)
4773 break;
4774 }
Miao Xie963d6782013-01-29 10:10:51 +00004775 delalloc_bytes = percpu_counter_sum_positive(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004776 &fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004777 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004778}
4779
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004780/**
Josef Bacik663350a2011-11-03 22:54:25 -04004781 * maybe_commit_transaction - possibly commit the transaction if its ok to
4782 * @root - the root we're allocating for
4783 * @bytes - the number of bytes we want to reserve
4784 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004785 *
Josef Bacik663350a2011-11-03 22:54:25 -04004786 * This will check to make sure that committing the transaction will actually
4787 * get us somewhere and then commit the transaction if it does. Otherwise it
4788 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004789 */
Josef Bacik663350a2011-11-03 22:54:25 -04004790static int may_commit_transaction(struct btrfs_root *root,
4791 struct btrfs_space_info *space_info,
4792 u64 bytes, int force)
4793{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004794 struct btrfs_fs_info *fs_info = root->fs_info;
4795 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;
Josef Bacik663350a2011-11-03 22:54:25 -04004796 struct btrfs_trans_handle *trans;
4797
4798 trans = (struct btrfs_trans_handle *)current->journal_info;
4799 if (trans)
4800 return -EAGAIN;
4801
4802 if (force)
4803 goto commit;
4804
4805 /* See if there is enough pinned space to make this reservation */
Josef Bacikb150a4f2013-06-19 15:00:04 -04004806 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Miao Xie0424c542014-03-06 13:54:59 +08004807 bytes) >= 0)
Josef Bacik663350a2011-11-03 22:54:25 -04004808 goto commit;
Josef Bacik663350a2011-11-03 22:54:25 -04004809
4810 /*
4811 * See if there is some space in the delayed insertion reservation for
4812 * this reservation.
4813 */
4814 if (space_info != delayed_rsv->space_info)
4815 return -ENOSPC;
4816
4817 spin_lock(&delayed_rsv->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004818 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4819 bytes - delayed_rsv->size) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004820 spin_unlock(&delayed_rsv->lock);
4821 return -ENOSPC;
4822 }
4823 spin_unlock(&delayed_rsv->lock);
4824
4825commit:
4826 trans = btrfs_join_transaction(root);
4827 if (IS_ERR(trans))
4828 return -ENOSPC;
4829
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004830 return btrfs_commit_transaction(trans);
Josef Bacik663350a2011-11-03 22:54:25 -04004831}
4832
Josef Bacik957780e2016-05-17 13:30:55 -04004833struct reserve_ticket {
4834 u64 bytes;
4835 int error;
4836 struct list_head list;
4837 wait_queue_head_t wait;
Josef Bacik96c3f432012-06-21 14:05:49 -04004838};
4839
4840static int flush_space(struct btrfs_root *root,
4841 struct btrfs_space_info *space_info, u64 num_bytes,
4842 u64 orig_bytes, int state)
4843{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004844 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik96c3f432012-06-21 14:05:49 -04004845 struct btrfs_trans_handle *trans;
4846 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004847 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004848
4849 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004850 case FLUSH_DELAYED_ITEMS_NR:
4851 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08004852 if (state == FLUSH_DELAYED_ITEMS_NR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004853 nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004854 else
Josef Bacik96c3f432012-06-21 14:05:49 -04004855 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004856
Josef Bacik96c3f432012-06-21 14:05:49 -04004857 trans = btrfs_join_transaction(root);
4858 if (IS_ERR(trans)) {
4859 ret = PTR_ERR(trans);
4860 break;
4861 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004862 ret = btrfs_run_delayed_items_nr(trans, fs_info, nr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004863 btrfs_end_transaction(trans);
Josef Bacik96c3f432012-06-21 14:05:49 -04004864 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004865 case FLUSH_DELALLOC:
4866 case FLUSH_DELALLOC_WAIT:
Miao Xie24af7dd2014-03-06 13:55:00 +08004867 shrink_delalloc(root, num_bytes * 2, orig_bytes,
Josef Bacik67b0fd62012-09-24 13:42:00 -04004868 state == FLUSH_DELALLOC_WAIT);
4869 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004870 case ALLOC_CHUNK:
4871 trans = btrfs_join_transaction(root);
4872 if (IS_ERR(trans)) {
4873 ret = PTR_ERR(trans);
4874 break;
4875 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004876 ret = do_chunk_alloc(trans, fs_info,
Josef Bacikea658ba2012-09-11 16:57:25 -04004877 btrfs_get_alloc_profile(root, 0),
4878 CHUNK_ALLOC_NO_FORCE);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004879 btrfs_end_transaction(trans);
Alex Lyakaseecba892015-12-06 12:32:31 +02004880 if (ret > 0 || ret == -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04004881 ret = 0;
4882 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004883 case COMMIT_TRANS:
4884 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4885 break;
4886 default:
4887 ret = -ENOSPC;
4888 break;
4889 }
4890
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004891 trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes,
Josef Bacikf376df22016-03-25 13:25:56 -04004892 orig_bytes, state, ret);
Josef Bacik96c3f432012-06-21 14:05:49 -04004893 return ret;
4894}
Miao Xie21c7e752014-05-13 17:29:04 -07004895
4896static inline u64
4897btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
4898 struct btrfs_space_info *space_info)
4899{
Josef Bacik957780e2016-05-17 13:30:55 -04004900 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07004901 u64 used;
4902 u64 expected;
Josef Bacik957780e2016-05-17 13:30:55 -04004903 u64 to_reclaim = 0;
Miao Xie21c7e752014-05-13 17:29:04 -07004904
Josef Bacik957780e2016-05-17 13:30:55 -04004905 list_for_each_entry(ticket, &space_info->tickets, list)
4906 to_reclaim += ticket->bytes;
4907 list_for_each_entry(ticket, &space_info->priority_tickets, list)
4908 to_reclaim += ticket->bytes;
4909 if (to_reclaim)
4910 return to_reclaim;
Miao Xie21c7e752014-05-13 17:29:04 -07004911
Wang Xiaoguange0af2482016-08-31 19:46:16 +08004912 to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M);
4913 if (can_overcommit(root, space_info, to_reclaim,
4914 BTRFS_RESERVE_FLUSH_ALL))
4915 return 0;
4916
Miao Xie21c7e752014-05-13 17:29:04 -07004917 used = space_info->bytes_used + space_info->bytes_reserved +
4918 space_info->bytes_pinned + space_info->bytes_readonly +
4919 space_info->bytes_may_use;
Byongho Leeee221842015-12-15 01:42:10 +09004920 if (can_overcommit(root, space_info, SZ_1M, BTRFS_RESERVE_FLUSH_ALL))
Miao Xie21c7e752014-05-13 17:29:04 -07004921 expected = div_factor_fine(space_info->total_bytes, 95);
4922 else
4923 expected = div_factor_fine(space_info->total_bytes, 90);
4924
4925 if (used > expected)
4926 to_reclaim = used - expected;
4927 else
4928 to_reclaim = 0;
4929 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
4930 space_info->bytes_reserved);
Miao Xie21c7e752014-05-13 17:29:04 -07004931 return to_reclaim;
4932}
4933
4934static inline int need_do_async_reclaim(struct btrfs_space_info *space_info,
Josef Bacik87241c22016-04-25 09:58:18 -04004935 struct btrfs_root *root, u64 used)
Miao Xie21c7e752014-05-13 17:29:04 -07004936{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004937 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik365c5312015-02-18 13:58:15 -08004938 u64 thresh = div_factor_fine(space_info->total_bytes, 98);
4939
4940 /* If we're just plain full then async reclaim just slows us down. */
Josef Bacikbaee8792016-01-26 09:35:38 -05004941 if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh)
Josef Bacik365c5312015-02-18 13:58:15 -08004942 return 0;
4943
Josef Bacik87241c22016-04-25 09:58:18 -04004944 if (!btrfs_calc_reclaim_metadata_size(root, space_info))
Liu Bo25ce4592014-09-10 12:58:50 +08004945 return 0;
Liu Bo25ce4592014-09-10 12:58:50 +08004946
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004947 return (used >= thresh && !btrfs_fs_closing(fs_info) &&
4948 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
Miao Xie21c7e752014-05-13 17:29:04 -07004949}
4950
Josef Bacik957780e2016-05-17 13:30:55 -04004951static void wake_all_tickets(struct list_head *head)
Miao Xie21c7e752014-05-13 17:29:04 -07004952{
Josef Bacik957780e2016-05-17 13:30:55 -04004953 struct reserve_ticket *ticket;
Miao Xie21c7e752014-05-13 17:29:04 -07004954
Josef Bacik957780e2016-05-17 13:30:55 -04004955 while (!list_empty(head)) {
4956 ticket = list_first_entry(head, struct reserve_ticket, list);
4957 list_del_init(&ticket->list);
4958 ticket->error = -ENOSPC;
4959 wake_up(&ticket->wait);
Miao Xie21c7e752014-05-13 17:29:04 -07004960 }
Miao Xie21c7e752014-05-13 17:29:04 -07004961}
4962
Josef Bacik957780e2016-05-17 13:30:55 -04004963/*
4964 * This is for normal flushers, we can wait all goddamned day if we want to. We
4965 * will loop and continuously try to flush as long as we are making progress.
4966 * We count progress as clearing off tickets each time we have to loop.
4967 */
Miao Xie21c7e752014-05-13 17:29:04 -07004968static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
4969{
4970 struct btrfs_fs_info *fs_info;
4971 struct btrfs_space_info *space_info;
4972 u64 to_reclaim;
4973 int flush_state;
Josef Bacik957780e2016-05-17 13:30:55 -04004974 int commit_cycles = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08004975 u64 last_tickets_id;
Miao Xie21c7e752014-05-13 17:29:04 -07004976
4977 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
4978 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4979
Josef Bacik957780e2016-05-17 13:30:55 -04004980 spin_lock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07004981 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
4982 space_info);
Josef Bacik957780e2016-05-17 13:30:55 -04004983 if (!to_reclaim) {
4984 space_info->flush = 0;
4985 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07004986 return;
Josef Bacik957780e2016-05-17 13:30:55 -04004987 }
Wang Xiaoguangce129652016-09-02 10:58:46 +08004988 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04004989 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07004990
4991 flush_state = FLUSH_DELAYED_ITEMS_NR;
4992 do {
Josef Bacik957780e2016-05-17 13:30:55 -04004993 struct reserve_ticket *ticket;
4994 int ret;
4995
4996 ret = flush_space(fs_info->fs_root, space_info, to_reclaim,
Miao Xie21c7e752014-05-13 17:29:04 -07004997 to_reclaim, flush_state);
Josef Bacik957780e2016-05-17 13:30:55 -04004998 spin_lock(&space_info->lock);
4999 if (list_empty(&space_info->tickets)) {
5000 space_info->flush = 0;
5001 spin_unlock(&space_info->lock);
Miao Xie21c7e752014-05-13 17:29:04 -07005002 return;
Josef Bacik957780e2016-05-17 13:30:55 -04005003 }
5004 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
5005 space_info);
5006 ticket = list_first_entry(&space_info->tickets,
5007 struct reserve_ticket, list);
Wang Xiaoguangce129652016-09-02 10:58:46 +08005008 if (last_tickets_id == space_info->tickets_id) {
Josef Bacik957780e2016-05-17 13:30:55 -04005009 flush_state++;
5010 } else {
Wang Xiaoguangce129652016-09-02 10:58:46 +08005011 last_tickets_id = space_info->tickets_id;
Josef Bacik957780e2016-05-17 13:30:55 -04005012 flush_state = FLUSH_DELAYED_ITEMS_NR;
5013 if (commit_cycles)
5014 commit_cycles--;
5015 }
5016
5017 if (flush_state > COMMIT_TRANS) {
5018 commit_cycles++;
5019 if (commit_cycles > 2) {
5020 wake_all_tickets(&space_info->tickets);
5021 space_info->flush = 0;
5022 } else {
5023 flush_state = FLUSH_DELAYED_ITEMS_NR;
5024 }
5025 }
5026 spin_unlock(&space_info->lock);
5027 } while (flush_state <= COMMIT_TRANS);
Miao Xie21c7e752014-05-13 17:29:04 -07005028}
5029
5030void btrfs_init_async_reclaim_work(struct work_struct *work)
5031{
5032 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
5033}
5034
Josef Bacik957780e2016-05-17 13:30:55 -04005035static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info,
5036 struct btrfs_space_info *space_info,
5037 struct reserve_ticket *ticket)
5038{
5039 u64 to_reclaim;
5040 int flush_state = FLUSH_DELAYED_ITEMS_NR;
5041
5042 spin_lock(&space_info->lock);
5043 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
5044 space_info);
5045 if (!to_reclaim) {
5046 spin_unlock(&space_info->lock);
5047 return;
5048 }
5049 spin_unlock(&space_info->lock);
5050
5051 do {
5052 flush_space(fs_info->fs_root, space_info, to_reclaim,
5053 to_reclaim, flush_state);
5054 flush_state++;
5055 spin_lock(&space_info->lock);
5056 if (ticket->bytes == 0) {
5057 spin_unlock(&space_info->lock);
5058 return;
5059 }
5060 spin_unlock(&space_info->lock);
5061
5062 /*
5063 * Priority flushers can't wait on delalloc without
5064 * deadlocking.
5065 */
5066 if (flush_state == FLUSH_DELALLOC ||
5067 flush_state == FLUSH_DELALLOC_WAIT)
5068 flush_state = ALLOC_CHUNK;
5069 } while (flush_state < COMMIT_TRANS);
5070}
5071
5072static int wait_reserve_ticket(struct btrfs_fs_info *fs_info,
5073 struct btrfs_space_info *space_info,
5074 struct reserve_ticket *ticket, u64 orig_bytes)
5075
5076{
5077 DEFINE_WAIT(wait);
5078 int ret = 0;
5079
5080 spin_lock(&space_info->lock);
5081 while (ticket->bytes > 0 && ticket->error == 0) {
5082 ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE);
5083 if (ret) {
5084 ret = -EINTR;
5085 break;
5086 }
5087 spin_unlock(&space_info->lock);
5088
5089 schedule();
5090
5091 finish_wait(&ticket->wait, &wait);
5092 spin_lock(&space_info->lock);
5093 }
5094 if (!ret)
5095 ret = ticket->error;
5096 if (!list_empty(&ticket->list))
5097 list_del_init(&ticket->list);
5098 if (ticket->bytes && ticket->bytes < orig_bytes) {
5099 u64 num_bytes = orig_bytes - ticket->bytes;
5100 space_info->bytes_may_use -= num_bytes;
5101 trace_btrfs_space_reservation(fs_info, "space_info",
5102 space_info->flags, num_bytes, 0);
5103 }
5104 spin_unlock(&space_info->lock);
5105
5106 return ret;
5107}
5108
Josef Bacik663350a2011-11-03 22:54:25 -04005109/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005110 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5111 * @root - the root we're allocating for
Josef Bacik957780e2016-05-17 13:30:55 -04005112 * @space_info - the space info we want to allocate from
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005113 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04005114 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005115 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04005116 * This will reserve orig_bytes number of bytes from the space info associated
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005117 * with the block_rsv. If there is not enough space it will make an attempt to
5118 * flush out space to make room. It will do this by flushing delalloc if
5119 * possible or committing the transaction. If flush is 0 then no attempts to
5120 * regain reservations will be made and this will fail if there is not enough
5121 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005122 */
Josef Bacik957780e2016-05-17 13:30:55 -04005123static int __reserve_metadata_bytes(struct btrfs_root *root,
5124 struct btrfs_space_info *space_info,
5125 u64 orig_bytes,
5126 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005127{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005128 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik957780e2016-05-17 13:30:55 -04005129 struct reserve_ticket ticket;
Josef Bacik2bf64752011-09-26 17:12:22 -04005130 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005131 int ret = 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005132
Josef Bacik957780e2016-05-17 13:30:55 -04005133 ASSERT(orig_bytes);
Josef Bacik8ca17f02016-05-27 13:24:13 -04005134 ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL);
5135
Yan, Zhengf0486c62010-05-16 10:46:25 -04005136 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04005137 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04005138 used = space_info->bytes_used + space_info->bytes_reserved +
5139 space_info->bytes_pinned + space_info->bytes_readonly +
5140 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005141
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005142 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005143 * If we have enough space then hooray, make our reservation and carry
5144 * on. If not see if we can overcommit, and if we can, hooray carry on.
5145 * If not things get more complicated.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005146 */
Josef Bacik957780e2016-05-17 13:30:55 -04005147 if (used + orig_bytes <= space_info->total_bytes) {
5148 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005149 trace_btrfs_space_reservation(fs_info, "space_info",
5150 space_info->flags, orig_bytes, 1);
Josef Bacik957780e2016-05-17 13:30:55 -04005151 ret = 0;
5152 } else if (can_overcommit(root, space_info, orig_bytes, flush)) {
Josef Bacik44734ed2012-09-28 16:04:19 -04005153 space_info->bytes_may_use += orig_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005154 trace_btrfs_space_reservation(fs_info, "space_info",
5155 space_info->flags, orig_bytes, 1);
Josef Bacik44734ed2012-09-28 16:04:19 -04005156 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04005157 }
5158
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005159 /*
Josef Bacik957780e2016-05-17 13:30:55 -04005160 * If we couldn't make a reservation then setup our reservation ticket
5161 * and kick the async worker if it's not already running.
Miao Xie08e007d2012-10-16 11:33:38 +00005162 *
Josef Bacik957780e2016-05-17 13:30:55 -04005163 * If we are a priority flusher then we just need to add our ticket to
5164 * the list and we will do our own flushing further down.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005165 */
Josef Bacik72bcd992012-12-18 15:16:34 -05005166 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacik957780e2016-05-17 13:30:55 -04005167 ticket.bytes = orig_bytes;
5168 ticket.error = 0;
5169 init_waitqueue_head(&ticket.wait);
5170 if (flush == BTRFS_RESERVE_FLUSH_ALL) {
5171 list_add_tail(&ticket.list, &space_info->tickets);
5172 if (!space_info->flush) {
5173 space_info->flush = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005174 trace_btrfs_trigger_flush(fs_info,
Josef Bacikf376df22016-03-25 13:25:56 -04005175 space_info->flags,
5176 orig_bytes, flush,
5177 "enospc");
Josef Bacik957780e2016-05-17 13:30:55 -04005178 queue_work(system_unbound_wq,
5179 &root->fs_info->async_reclaim_work);
5180 }
5181 } else {
5182 list_add_tail(&ticket.list,
5183 &space_info->priority_tickets);
5184 }
Miao Xie21c7e752014-05-13 17:29:04 -07005185 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
5186 used += orig_bytes;
Josef Bacikf6acfd52014-09-18 11:27:17 -04005187 /*
5188 * We will do the space reservation dance during log replay,
5189 * which means we won't have fs_info->fs_root set, so don't do
5190 * the async reclaim as we will panic.
5191 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005192 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) &&
Josef Bacik87241c22016-04-25 09:58:18 -04005193 need_do_async_reclaim(space_info, root, used) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005194 !work_busy(&fs_info->async_reclaim_work)) {
5195 trace_btrfs_trigger_flush(fs_info, space_info->flags,
5196 orig_bytes, flush, "preempt");
Miao Xie21c7e752014-05-13 17:29:04 -07005197 queue_work(system_unbound_wq,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005198 &fs_info->async_reclaim_work);
Josef Bacikf376df22016-03-25 13:25:56 -04005199 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005200 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005201 spin_unlock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00005202 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik957780e2016-05-17 13:30:55 -04005203 return ret;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005204
Josef Bacik957780e2016-05-17 13:30:55 -04005205 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005206 return wait_reserve_ticket(fs_info, space_info, &ticket,
Josef Bacik957780e2016-05-17 13:30:55 -04005207 orig_bytes);
Miao Xie08e007d2012-10-16 11:33:38 +00005208
Josef Bacik957780e2016-05-17 13:30:55 -04005209 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005210 priority_reclaim_metadata_space(fs_info, space_info, &ticket);
Josef Bacik957780e2016-05-17 13:30:55 -04005211 spin_lock(&space_info->lock);
5212 if (ticket.bytes) {
5213 if (ticket.bytes < orig_bytes) {
5214 u64 num_bytes = orig_bytes - ticket.bytes;
5215 space_info->bytes_may_use -= num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005216 trace_btrfs_space_reservation(fs_info, "space_info",
5217 space_info->flags,
5218 num_bytes, 0);
Miao Xie08e007d2012-10-16 11:33:38 +00005219
Josef Bacik957780e2016-05-17 13:30:55 -04005220 }
5221 list_del_init(&ticket.list);
5222 ret = -ENOSPC;
5223 }
5224 spin_unlock(&space_info->lock);
5225 ASSERT(list_empty(&ticket.list));
5226 return ret;
5227}
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005228
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005229/**
5230 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
5231 * @root - the root we're allocating for
5232 * @block_rsv - the block_rsv we're allocating for
5233 * @orig_bytes - the number of bytes we want
5234 * @flush - whether or not we can flush to make our reservation
5235 *
Josef Bacik957780e2016-05-17 13:30:55 -04005236 * This will reserve orgi_bytes number of bytes from the space info associated
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005237 * with the block_rsv. If there is not enough space it will make an attempt to
5238 * flush out space to make room. It will do this by flushing delalloc if
5239 * possible or committing the transaction. If flush is 0 then no attempts to
5240 * regain reservations will be made and this will fail if there is not enough
5241 * space already.
5242 */
5243static int reserve_metadata_bytes(struct btrfs_root *root,
5244 struct btrfs_block_rsv *block_rsv,
5245 u64 orig_bytes,
5246 enum btrfs_reserve_flush_enum flush)
5247{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005248 struct btrfs_fs_info *fs_info = root->fs_info;
5249 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacik957780e2016-05-17 13:30:55 -04005250 int ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005251
Josef Bacik957780e2016-05-17 13:30:55 -04005252 ret = __reserve_metadata_bytes(root, block_rsv->space_info, orig_bytes,
5253 flush);
Josef Bacik5d803662013-02-07 16:06:02 -05005254 if (ret == -ENOSPC &&
5255 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
Josef Bacik5d803662013-02-07 16:06:02 -05005256 if (block_rsv != global_rsv &&
5257 !block_rsv_use_bytes(global_rsv, orig_bytes))
5258 ret = 0;
5259 }
Jeff Mahoneycab45e22013-10-16 16:27:01 -04005260 if (ret == -ENOSPC)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005261 trace_btrfs_space_reservation(fs_info, "space_info:enospc",
Josef Bacik957780e2016-05-17 13:30:55 -04005262 block_rsv->space_info->flags,
5263 orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005264 return ret;
5265}
5266
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005267static struct btrfs_block_rsv *get_block_rsv(
5268 const struct btrfs_trans_handle *trans,
5269 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005270{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005271 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik4c13d752011-08-30 11:31:29 -04005272 struct btrfs_block_rsv *block_rsv = NULL;
5273
Alexandru Moisee9cf4392015-09-09 00:18:50 +00005274 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005275 (root == fs_info->csum_root && trans->adding_csums) ||
5276 (root == fs_info->uuid_root))
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02005277 block_rsv = trans->block_rsv;
5278
Josef Bacik4c13d752011-08-30 11:31:29 -04005279 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005280 block_rsv = root->block_rsv;
5281
5282 if (!block_rsv)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005283 block_rsv = &fs_info->empty_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005284
5285 return block_rsv;
5286}
5287
5288static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
5289 u64 num_bytes)
5290{
5291 int ret = -ENOSPC;
5292 spin_lock(&block_rsv->lock);
5293 if (block_rsv->reserved >= num_bytes) {
5294 block_rsv->reserved -= num_bytes;
5295 if (block_rsv->reserved < block_rsv->size)
5296 block_rsv->full = 0;
5297 ret = 0;
5298 }
5299 spin_unlock(&block_rsv->lock);
5300 return ret;
5301}
5302
5303static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
5304 u64 num_bytes, int update_size)
5305{
5306 spin_lock(&block_rsv->lock);
5307 block_rsv->reserved += num_bytes;
5308 if (update_size)
5309 block_rsv->size += num_bytes;
5310 else if (block_rsv->reserved >= block_rsv->size)
5311 block_rsv->full = 1;
5312 spin_unlock(&block_rsv->lock);
5313}
5314
Josef Bacikd52be812013-05-29 14:54:47 -04005315int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
5316 struct btrfs_block_rsv *dest, u64 num_bytes,
5317 int min_factor)
5318{
5319 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5320 u64 min_bytes;
5321
5322 if (global_rsv->space_info != dest->space_info)
5323 return -ENOSPC;
5324
5325 spin_lock(&global_rsv->lock);
5326 min_bytes = div_factor(global_rsv->size, min_factor);
5327 if (global_rsv->reserved < min_bytes + num_bytes) {
5328 spin_unlock(&global_rsv->lock);
5329 return -ENOSPC;
5330 }
5331 global_rsv->reserved -= num_bytes;
5332 if (global_rsv->reserved < global_rsv->size)
5333 global_rsv->full = 0;
5334 spin_unlock(&global_rsv->lock);
5335
5336 block_rsv_add_bytes(dest, num_bytes, 1);
5337 return 0;
5338}
5339
Josef Bacik957780e2016-05-17 13:30:55 -04005340/*
5341 * This is for space we already have accounted in space_info->bytes_may_use, so
5342 * basically when we're returning space from block_rsv's.
5343 */
5344static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
5345 struct btrfs_space_info *space_info,
5346 u64 num_bytes)
5347{
5348 struct reserve_ticket *ticket;
5349 struct list_head *head;
5350 u64 used;
5351 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH;
5352 bool check_overcommit = false;
5353
5354 spin_lock(&space_info->lock);
5355 head = &space_info->priority_tickets;
5356
5357 /*
5358 * If we are over our limit then we need to check and see if we can
5359 * overcommit, and if we can't then we just need to free up our space
5360 * and not satisfy any requests.
5361 */
5362 used = space_info->bytes_used + space_info->bytes_reserved +
5363 space_info->bytes_pinned + space_info->bytes_readonly +
5364 space_info->bytes_may_use;
5365 if (used - num_bytes >= space_info->total_bytes)
5366 check_overcommit = true;
5367again:
5368 while (!list_empty(head) && num_bytes) {
5369 ticket = list_first_entry(head, struct reserve_ticket,
5370 list);
5371 /*
5372 * We use 0 bytes because this space is already reserved, so
5373 * adding the ticket space would be a double count.
5374 */
5375 if (check_overcommit &&
5376 !can_overcommit(fs_info->extent_root, space_info, 0,
5377 flush))
5378 break;
5379 if (num_bytes >= ticket->bytes) {
5380 list_del_init(&ticket->list);
5381 num_bytes -= ticket->bytes;
5382 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005383 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005384 wake_up(&ticket->wait);
5385 } else {
5386 ticket->bytes -= num_bytes;
5387 num_bytes = 0;
5388 }
5389 }
5390
5391 if (num_bytes && head == &space_info->priority_tickets) {
5392 head = &space_info->tickets;
5393 flush = BTRFS_RESERVE_FLUSH_ALL;
5394 goto again;
5395 }
5396 space_info->bytes_may_use -= num_bytes;
5397 trace_btrfs_space_reservation(fs_info, "space_info",
5398 space_info->flags, num_bytes, 0);
5399 spin_unlock(&space_info->lock);
5400}
5401
5402/*
5403 * This is for newly allocated space that isn't accounted in
5404 * space_info->bytes_may_use yet. So if we allocate a chunk or unpin an extent
5405 * we use this helper.
5406 */
5407static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
5408 struct btrfs_space_info *space_info,
5409 u64 num_bytes)
5410{
5411 struct reserve_ticket *ticket;
5412 struct list_head *head = &space_info->priority_tickets;
5413
5414again:
5415 while (!list_empty(head) && num_bytes) {
5416 ticket = list_first_entry(head, struct reserve_ticket,
5417 list);
5418 if (num_bytes >= ticket->bytes) {
5419 trace_btrfs_space_reservation(fs_info, "space_info",
5420 space_info->flags,
5421 ticket->bytes, 1);
5422 list_del_init(&ticket->list);
5423 num_bytes -= ticket->bytes;
5424 space_info->bytes_may_use += ticket->bytes;
5425 ticket->bytes = 0;
Wang Xiaoguangce129652016-09-02 10:58:46 +08005426 space_info->tickets_id++;
Josef Bacik957780e2016-05-17 13:30:55 -04005427 wake_up(&ticket->wait);
5428 } else {
5429 trace_btrfs_space_reservation(fs_info, "space_info",
5430 space_info->flags,
5431 num_bytes, 1);
5432 space_info->bytes_may_use += num_bytes;
5433 ticket->bytes -= num_bytes;
5434 num_bytes = 0;
5435 }
5436 }
5437
5438 if (num_bytes && head == &space_info->priority_tickets) {
5439 head = &space_info->tickets;
5440 goto again;
5441 }
5442}
5443
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005444static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
5445 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02005446 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005447{
5448 struct btrfs_space_info *space_info = block_rsv->space_info;
5449
5450 spin_lock(&block_rsv->lock);
5451 if (num_bytes == (u64)-1)
5452 num_bytes = block_rsv->size;
5453 block_rsv->size -= num_bytes;
5454 if (block_rsv->reserved >= block_rsv->size) {
5455 num_bytes = block_rsv->reserved - block_rsv->size;
5456 block_rsv->reserved = block_rsv->size;
5457 block_rsv->full = 1;
5458 } else {
5459 num_bytes = 0;
5460 }
5461 spin_unlock(&block_rsv->lock);
5462
5463 if (num_bytes > 0) {
5464 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00005465 spin_lock(&dest->lock);
5466 if (!dest->full) {
5467 u64 bytes_to_add;
5468
5469 bytes_to_add = dest->size - dest->reserved;
5470 bytes_to_add = min(num_bytes, bytes_to_add);
5471 dest->reserved += bytes_to_add;
5472 if (dest->reserved >= dest->size)
5473 dest->full = 1;
5474 num_bytes -= bytes_to_add;
5475 }
5476 spin_unlock(&dest->lock);
5477 }
Josef Bacik957780e2016-05-17 13:30:55 -04005478 if (num_bytes)
5479 space_info_add_old_bytes(fs_info, space_info,
5480 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005481 }
5482}
5483
Josef Bacik25d609f2016-03-25 13:25:48 -04005484int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
5485 struct btrfs_block_rsv *dst, u64 num_bytes,
5486 int update_size)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005487{
5488 int ret;
5489
5490 ret = block_rsv_use_bytes(src, num_bytes);
5491 if (ret)
5492 return ret;
5493
Josef Bacik25d609f2016-03-25 13:25:48 -04005494 block_rsv_add_bytes(dst, num_bytes, update_size);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005495 return 0;
5496}
5497
Miao Xie66d8f3d2012-09-06 04:02:28 -06005498void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005499{
5500 memset(rsv, 0, sizeof(*rsv));
5501 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06005502 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005503}
5504
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005505struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
Miao Xie66d8f3d2012-09-06 04:02:28 -06005506 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005507{
5508 struct btrfs_block_rsv *block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005509
5510 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
5511 if (!block_rsv)
5512 return NULL;
5513
Miao Xie66d8f3d2012-09-06 04:02:28 -06005514 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005515 block_rsv->space_info = __find_space_info(fs_info,
5516 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005517 return block_rsv;
5518}
5519
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005520void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005521 struct btrfs_block_rsv *rsv)
5522{
Josef Bacik2aaa6652012-08-29 14:27:18 -04005523 if (!rsv)
5524 return;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005525 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Josef Bacikdabdb642011-08-08 12:50:18 -04005526 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005527}
5528
Chris Masoncdfb0802015-04-06 18:17:00 -07005529void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
5530{
5531 kfree(rsv);
5532}
5533
Miao Xie08e007d2012-10-16 11:33:38 +00005534int btrfs_block_rsv_add(struct btrfs_root *root,
5535 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
5536 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005537{
5538 int ret;
5539
5540 if (num_bytes == 0)
5541 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005542
Miao Xie61b520a2011-11-10 20:45:05 -05005543 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005544 if (!ret) {
5545 block_rsv_add_bytes(block_rsv, num_bytes, 1);
5546 return 0;
5547 }
5548
Yan, Zhengf0486c62010-05-16 10:46:25 -04005549 return ret;
5550}
5551
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005552int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005553{
5554 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005555 int ret = -ENOSPC;
5556
5557 if (!block_rsv)
5558 return 0;
5559
5560 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04005561 num_bytes = div_factor(block_rsv->size, min_factor);
5562 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005563 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005564 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005565
Josef Bacik36ba0222011-10-18 12:15:48 -04005566 return ret;
5567}
5568
Miao Xie08e007d2012-10-16 11:33:38 +00005569int btrfs_block_rsv_refill(struct btrfs_root *root,
5570 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
5571 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04005572{
5573 u64 num_bytes = 0;
5574 int ret = -ENOSPC;
5575
5576 if (!block_rsv)
5577 return 0;
5578
5579 spin_lock(&block_rsv->lock);
5580 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04005581 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005582 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04005583 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04005584 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005585 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04005586
Yan, Zhengf0486c62010-05-16 10:46:25 -04005587 if (!ret)
5588 return 0;
5589
Miao Xieaa38a712011-11-18 17:43:00 +08005590 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04005591 if (!ret) {
5592 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005593 return 0;
5594 }
5595
Josef Bacik13553e52011-08-08 13:33:21 -04005596 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005597}
5598
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005599void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005600 struct btrfs_block_rsv *block_rsv,
5601 u64 num_bytes)
5602{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005603 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5604
Liu Bo17504582013-12-29 21:44:50 +08005605 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04005606 block_rsv->space_info != global_rsv->space_info)
5607 global_rsv = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005608 block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005609}
5610
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005611static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
5612{
5613 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
5614 struct btrfs_space_info *sinfo = block_rsv->space_info;
5615 u64 num_bytes;
5616
Josef Bacikae2e4722016-05-27 12:58:35 -04005617 /*
5618 * The global block rsv is based on the size of the extent tree, the
5619 * checksum tree and the root tree. If the fs is empty we want to set
5620 * it to a minimal amount for safety.
5621 */
5622 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) +
5623 btrfs_root_used(&fs_info->csum_root->root_item) +
5624 btrfs_root_used(&fs_info->tree_root->root_item);
5625 num_bytes = max_t(u64, num_bytes, SZ_16M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005626
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005627 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005628 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005629
Byongho Leeee221842015-12-15 01:42:10 +09005630 block_rsv->size = min_t(u64, num_bytes, SZ_512M);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005631
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005632 if (block_rsv->reserved < block_rsv->size) {
5633 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
5634 sinfo->bytes_reserved + sinfo->bytes_readonly +
5635 sinfo->bytes_may_use;
5636 if (sinfo->total_bytes > num_bytes) {
5637 num_bytes = sinfo->total_bytes - num_bytes;
5638 num_bytes = min(num_bytes,
5639 block_rsv->size - block_rsv->reserved);
5640 block_rsv->reserved += num_bytes;
5641 sinfo->bytes_may_use += num_bytes;
5642 trace_btrfs_space_reservation(fs_info, "space_info",
5643 sinfo->flags, num_bytes,
5644 1);
5645 }
5646 } else if (block_rsv->reserved > block_rsv->size) {
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005647 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04005648 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005649 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005650 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005651 block_rsv->reserved = block_rsv->size;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005652 }
David Sterba182608c2011-05-05 13:13:16 +02005653
Josef Bacikfb4b10e2016-01-11 17:28:38 -05005654 if (block_rsv->reserved == block_rsv->size)
5655 block_rsv->full = 1;
5656 else
5657 block_rsv->full = 0;
5658
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005659 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005660 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005661}
5662
Yan, Zhengf0486c62010-05-16 10:46:25 -04005663static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
5664{
5665 struct btrfs_space_info *space_info;
5666
5667 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
5668 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005669
5670 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005671 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005672 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005673 fs_info->trans_block_rsv.space_info = space_info;
5674 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04005675 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005676
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005677 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
5678 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
5679 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
5680 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00005681 if (fs_info->quota_root)
5682 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005683 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005684
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005685 update_global_block_rsv(fs_info);
5686}
5687
5688static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
5689{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005690 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
5691 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005692 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
5693 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
5694 WARN_ON(fs_info->trans_block_rsv.size > 0);
5695 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
5696 WARN_ON(fs_info->chunk_block_rsv.size > 0);
5697 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04005698 WARN_ON(fs_info->delayed_block_rsv.size > 0);
5699 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04005700}
5701
Yan, Zhenga22285a2010-05-16 10:48:46 -04005702void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005703 struct btrfs_fs_info *fs_info)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005704{
Josef Bacik0e721102012-06-26 16:13:18 -04005705 if (!trans->block_rsv)
5706 return;
5707
Yan, Zhenga22285a2010-05-16 10:48:46 -04005708 if (!trans->bytes_reserved)
5709 return;
5710
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005711 trace_btrfs_space_reservation(fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04005712 trans->transid, trans->bytes_reserved, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005713 btrfs_block_rsv_release(fs_info, trans->block_rsv,
5714 trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005715 trans->bytes_reserved = 0;
5716}
5717
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005718/*
5719 * To be called after all the new block groups attached to the transaction
5720 * handle have been created (btrfs_create_pending_block_groups()).
5721 */
5722void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
5723{
Jeff Mahoney64b63582016-06-20 17:23:41 -04005724 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005725
5726 if (!trans->chunk_bytes_reserved)
5727 return;
5728
5729 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
5730
5731 block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL,
5732 trans->chunk_bytes_reserved);
5733 trans->chunk_bytes_reserved = 0;
5734}
5735
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005736/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04005737int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
5738 struct inode *inode)
5739{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005740 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005741 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik40acc3e2016-05-27 13:01:08 -04005742 /*
5743 * We always use trans->block_rsv here as we will have reserved space
5744 * for our orphan when starting the transaction, using get_block_rsv()
5745 * here will sometimes make us choose the wrong block rsv as we could be
5746 * doing a reloc inode for a non refcounted root.
5747 */
5748 struct btrfs_block_rsv *src_rsv = trans->block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005749 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
5750
5751 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04005752 * We need to hold space in order to delete our orphan item once we've
5753 * added it, so this takes the reservation so we can release it later
5754 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04005755 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005756 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5757
5758 trace_btrfs_space_reservation(fs_info, "orphan",
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005759 btrfs_ino(inode), num_bytes, 1);
Josef Bacik25d609f2016-03-25 13:25:48 -04005760 return btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005761}
5762
5763void btrfs_orphan_release_metadata(struct inode *inode)
5764{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005765 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005766 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005767 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5768
5769 trace_btrfs_space_reservation(fs_info, "orphan",
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005770 btrfs_ino(inode), num_bytes, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005771 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, num_bytes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005772}
5773
Miao Xied5c12072013-02-28 10:04:33 +00005774/*
5775 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
5776 * root: the root of the parent directory
5777 * rsv: block reservation
5778 * items: the number of items that we need do reservation
5779 * qgroup_reserved: used to return the reserved size in qgroup
5780 *
5781 * This function is used to reserve the space for snapshot/subvolume
5782 * creation and deletion. Those operations are different with the
5783 * common file/directory operations, they change two fs/file trees
5784 * and root tree, the number of items that the qgroup reserves is
5785 * different with the free space reservation. So we can not use
Nicholas D Steeves01327612016-05-19 21:18:45 -04005786 * the space reservation mechanism in start_transaction().
Miao Xied5c12072013-02-28 10:04:33 +00005787 */
5788int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
5789 struct btrfs_block_rsv *rsv,
5790 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005791 u64 *qgroup_reserved,
5792 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005793{
Miao Xied5c12072013-02-28 10:04:33 +00005794 u64 num_bytes;
5795 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005796 struct btrfs_fs_info *fs_info = root->fs_info;
5797 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00005798
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005799 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Miao Xied5c12072013-02-28 10:04:33 +00005800 /* One for parent inode, two for dir entries */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005801 num_bytes = 3 * fs_info->nodesize;
Qu Wenruo71741092015-09-08 17:22:41 +08005802 ret = btrfs_qgroup_reserve_meta(root, num_bytes);
Miao Xied5c12072013-02-28 10:04:33 +00005803 if (ret)
5804 return ret;
5805 } else {
5806 num_bytes = 0;
5807 }
5808
5809 *qgroup_reserved = num_bytes;
5810
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005811 num_bytes = btrfs_calc_trans_metadata_size(fs_info, items);
5812 rsv->space_info = __find_space_info(fs_info,
Miao Xied5c12072013-02-28 10:04:33 +00005813 BTRFS_BLOCK_GROUP_METADATA);
5814 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
5815 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005816
5817 if (ret == -ENOSPC && use_global_rsv)
Josef Bacik25d609f2016-03-25 13:25:48 -04005818 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005819
Qu Wenruo71741092015-09-08 17:22:41 +08005820 if (ret && *qgroup_reserved)
5821 btrfs_qgroup_free_meta(root, *qgroup_reserved);
Miao Xied5c12072013-02-28 10:04:33 +00005822
5823 return ret;
5824}
5825
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005826void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
Miao Xied5c12072013-02-28 10:04:33 +00005827 struct btrfs_block_rsv *rsv,
5828 u64 qgroup_reserved)
5829{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005830 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005831}
5832
Josef Bacik7709cde2011-08-04 10:25:02 -04005833/**
5834 * drop_outstanding_extent - drop an outstanding extent
5835 * @inode: the inode we're dropping the extent for
Nicholas D Steeves01327612016-05-19 21:18:45 -04005836 * @num_bytes: the number of bytes we're releasing.
Josef Bacik7709cde2011-08-04 10:25:02 -04005837 *
5838 * This is called when we are freeing up an outstanding extent, either called
5839 * after an error or after an extent is written. This will return the number of
5840 * reserved extents that need to be freed. This must be called with
5841 * BTRFS_I(inode)->lock held.
5842 */
Josef Bacikdcab6a32015-02-11 15:08:59 -05005843static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes)
Josef Bacik9e0baf62011-07-15 15:16:44 +00005844{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005845 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005846 unsigned dropped_extents = 0;
Josef Bacikdcab6a32015-02-11 15:08:59 -05005847 unsigned num_extents = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005848
Josef Bacikdcab6a32015-02-11 15:08:59 -05005849 num_extents = (unsigned)div64_u64(num_bytes +
5850 BTRFS_MAX_EXTENT_SIZE - 1,
5851 BTRFS_MAX_EXTENT_SIZE);
5852 ASSERT(num_extents);
5853 ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents);
5854 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005855
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005856 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04005857 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5858 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005859 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005860
Josef Bacik9e0baf62011-07-15 15:16:44 +00005861 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005862 * If we have more or the same amount of outstanding extents than we have
Josef Bacik9e0baf62011-07-15 15:16:44 +00005863 * reserved then we need to leave the reserved extents count alone.
5864 */
5865 if (BTRFS_I(inode)->outstanding_extents >=
5866 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005867 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005868
5869 dropped_extents = BTRFS_I(inode)->reserved_extents -
5870 BTRFS_I(inode)->outstanding_extents;
5871 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005872 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005873}
5874
Josef Bacik7709cde2011-08-04 10:25:02 -04005875/**
Nicholas D Steeves01327612016-05-19 21:18:45 -04005876 * calc_csum_metadata_size - return the amount of metadata space that must be
5877 * reserved/freed for the given bytes.
Josef Bacik7709cde2011-08-04 10:25:02 -04005878 * @inode: the inode we're manipulating
5879 * @num_bytes: the number of bytes in question
5880 * @reserve: 1 if we are reserving space, 0 if we are freeing space
5881 *
5882 * This adjusts the number of csum_bytes in the inode and then returns the
5883 * correct amount of metadata that must either be reserved or freed. We
5884 * calculate how many checksums we can fit into one leaf and then divide the
5885 * number of bytes that will need to be checksumed by this value to figure out
5886 * how many checksums will be required. If we are adding bytes then the number
5887 * may go up and we will return the number of additional bytes that must be
5888 * reserved. If it is going down we will return the number of bytes that must
5889 * be freed.
5890 *
5891 * This must be called with BTRFS_I(inode)->lock held.
5892 */
5893static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
5894 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005895{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005896 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik12621332015-02-03 07:50:16 -08005897 u64 old_csums, num_csums;
Josef Bacik7709cde2011-08-04 10:25:02 -04005898
5899 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
5900 BTRFS_I(inode)->csum_bytes == 0)
5901 return 0;
5902
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005903 old_csums = btrfs_csum_bytes_to_leaves(fs_info,
5904 BTRFS_I(inode)->csum_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04005905 if (reserve)
5906 BTRFS_I(inode)->csum_bytes += num_bytes;
5907 else
5908 BTRFS_I(inode)->csum_bytes -= num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005909 num_csums = btrfs_csum_bytes_to_leaves(fs_info,
5910 BTRFS_I(inode)->csum_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04005911
5912 /* No change, no need to reserve more */
5913 if (old_csums == num_csums)
5914 return 0;
5915
5916 if (reserve)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005917 return btrfs_calc_trans_metadata_size(fs_info,
Josef Bacik7709cde2011-08-04 10:25:02 -04005918 num_csums - old_csums);
5919
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005920 return btrfs_calc_trans_metadata_size(fs_info, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005921}
5922
5923int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
5924{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005925 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005926 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005927 struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005928 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05005929 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005930 unsigned nr_extents = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00005931 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07005932 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005933 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00005934 u64 to_free = 0;
5935 unsigned dropped;
Josef Bacik48c3d482016-03-25 13:25:49 -04005936 bool release_extra = false;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005937
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005938 /* If we are a free space inode we need to not flush since we will be in
5939 * the middle of a transaction commit. We also don't need the delalloc
5940 * mutex since we won't race with anybody. We need this mostly to make
5941 * lockdep shut its filthy mouth.
Josef Bacikbac357dc2016-07-20 16:48:45 -07005942 *
5943 * If we have a transaction open (can happen if we call truncate_block
5944 * from truncate), then we need FLUSH_LIMIT so we don't deadlock.
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005945 */
5946 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00005947 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005948 delalloc_lock = false;
Josef Bacikbac357dc2016-07-20 16:48:45 -07005949 } else if (current->journal_info) {
5950 flush = BTRFS_RESERVE_FLUSH_LIMIT;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005951 }
Josef Bacikc09544e2011-08-30 10:19:10 -04005952
Miao Xie08e007d2012-10-16 11:33:38 +00005953 if (flush != BTRFS_RESERVE_NO_FLUSH &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005954 btrfs_transaction_in_commit(fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005955 schedule_timeout(1);
5956
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005957 if (delalloc_lock)
5958 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
5959
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005960 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005961
Josef Bacik9e0baf62011-07-15 15:16:44 +00005962 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik6a41dd02015-03-13 15:12:23 -04005963 nr_extents = (unsigned)div64_u64(num_bytes +
5964 BTRFS_MAX_EXTENT_SIZE - 1,
5965 BTRFS_MAX_EXTENT_SIZE);
5966 BTRFS_I(inode)->outstanding_extents += nr_extents;
Josef Bacik57a45ced2011-01-25 16:30:38 -05005967
Josef Bacik48c3d482016-03-25 13:25:49 -04005968 nr_extents = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005969 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05005970 BTRFS_I(inode)->reserved_extents)
Josef Bacik48c3d482016-03-25 13:25:49 -04005971 nr_extents += BTRFS_I(inode)->outstanding_extents -
Josef Bacik9e0baf62011-07-15 15:16:44 +00005972 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005973
Josef Bacik48c3d482016-03-25 13:25:49 -04005974 /* We always want to reserve a slot for updating the inode. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005975 to_reserve = btrfs_calc_trans_metadata_size(fs_info, nr_extents + 1);
Josef Bacik7709cde2011-08-04 10:25:02 -04005976 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05005977 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005978 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05005979
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005980 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Qu Wenruo71741092015-09-08 17:22:41 +08005981 ret = btrfs_qgroup_reserve_meta(root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005982 nr_extents * fs_info->nodesize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005983 if (ret)
5984 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00005985 }
Arne Jansenc5567232011-09-14 15:44:05 +02005986
Josef Bacik48c3d482016-03-25 13:25:49 -04005987 ret = btrfs_block_rsv_add(root, block_rsv, to_reserve, flush);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005988 if (unlikely(ret)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04005989 btrfs_qgroup_free_meta(root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005990 nr_extents * fs_info->nodesize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005991 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005992 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005993
Josef Bacik660d3f62011-12-09 11:18:51 -05005994 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik48c3d482016-03-25 13:25:49 -04005995 if (test_and_set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
Josef Bacikf485c9e2016-03-25 13:25:55 -04005996 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005997 to_reserve -= btrfs_calc_trans_metadata_size(fs_info, 1);
Josef Bacik48c3d482016-03-25 13:25:49 -04005998 release_extra = true;
Josef Bacik660d3f62011-12-09 11:18:51 -05005999 }
6000 BTRFS_I(inode)->reserved_extents += nr_extents;
6001 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05006002
6003 if (delalloc_lock)
6004 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05006005
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006006 if (to_reserve)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006007 trace_btrfs_space_reservation(fs_info, "delalloc",
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006008 btrfs_ino(inode), to_reserve, 1);
Josef Bacik48c3d482016-03-25 13:25:49 -04006009 if (release_extra)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006010 btrfs_block_rsv_release(fs_info, block_rsv,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006011 btrfs_calc_trans_metadata_size(fs_info, 1));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006012 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00006013
6014out_fail:
6015 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05006016 dropped = drop_outstanding_extent(inode, num_bytes);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006017 /*
6018 * If the inodes csum_bytes is the same as the original
6019 * csum_bytes then we know we haven't raced with any free()ers
6020 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00006021 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04006022 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00006023 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04006024 } else {
6025 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
6026 u64 bytes;
6027
6028 /*
6029 * This is tricky, but first we need to figure out how much we
Nicholas D Steeves01327612016-05-19 21:18:45 -04006030 * freed from any free-ers that occurred during this
Josef Bacikf4881bc2013-03-25 16:03:35 -04006031 * reservation, so we reset ->csum_bytes to the csum_bytes
6032 * before we dropped our lock, and then call the free for the
6033 * number of bytes that were freed while we were trying our
6034 * reservation.
6035 */
6036 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
6037 BTRFS_I(inode)->csum_bytes = csum_bytes;
6038 to_free = calc_csum_metadata_size(inode, bytes, 0);
6039
6040
6041 /*
6042 * Now we need to see how much we would have freed had we not
6043 * been making this reservation and our ->csum_bytes were not
6044 * artificially inflated.
6045 */
6046 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
6047 bytes = csum_bytes - orig_csum_bytes;
6048 bytes = calc_csum_metadata_size(inode, bytes, 0);
6049
6050 /*
6051 * Now reset ->csum_bytes to what it should be. If bytes is
Nicholas D Steeves01327612016-05-19 21:18:45 -04006052 * more than to_free then we would have freed more space had we
Josef Bacikf4881bc2013-03-25 16:03:35 -04006053 * not had an artificially high ->csum_bytes, so we need to free
6054 * the remainder. If bytes is the same or less then we don't
6055 * need to do anything, the other free-ers did the correct
6056 * thing.
6057 */
6058 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
6059 if (bytes > to_free)
6060 to_free = bytes - to_free;
6061 else
6062 to_free = 0;
6063 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00006064 spin_unlock(&BTRFS_I(inode)->lock);
Dongsheng Yange2d1f922015-02-06 10:26:52 -05006065 if (dropped)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006066 to_free += btrfs_calc_trans_metadata_size(fs_info, dropped);
Wang Shilong88e081bf2013-03-01 11:36:01 +00006067
6068 if (to_free) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006069 btrfs_block_rsv_release(fs_info, block_rsv, to_free);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006070 trace_btrfs_space_reservation(fs_info, "delalloc",
Wang Shilong88e081bf2013-03-01 11:36:01 +00006071 btrfs_ino(inode), to_free, 0);
6072 }
6073 if (delalloc_lock)
6074 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
6075 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006076}
6077
Josef Bacik7709cde2011-08-04 10:25:02 -04006078/**
6079 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
6080 * @inode: the inode to release the reservation for
6081 * @num_bytes: the number of bytes we're releasing
6082 *
6083 * This will release the metadata reservation for an inode. This can be called
6084 * once we complete IO for a given set of bytes to release their metadata
6085 * reservations.
6086 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006087void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
6088{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006089 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006090 u64 to_free = 0;
6091 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006092
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006093 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04006094 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05006095 dropped = drop_outstanding_extent(inode, num_bytes);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006096
Miao Xie09348562013-02-07 10:12:07 +00006097 if (num_bytes)
6098 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04006099 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006100 if (dropped > 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006101 to_free += btrfs_calc_trans_metadata_size(fs_info, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006102
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006103 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04006104 return;
6105
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006106 trace_btrfs_space_reservation(fs_info, "delalloc",
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006107 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02006108
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006109 btrfs_block_rsv_release(fs_info, &fs_info->delalloc_block_rsv, to_free);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006110}
6111
Josef Bacik7709cde2011-08-04 10:25:02 -04006112/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006113 * btrfs_delalloc_reserve_space - reserve data and metadata space for
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006114 * delalloc
6115 * @inode: inode we're writing to
6116 * @start: start range we are writing to
6117 * @len: how long the range we are writing to
6118 *
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006119 * This will do the following things
6120 *
6121 * o reserve space in data space info for num bytes
6122 * and reserve precious corresponding qgroup space
6123 * (Done in check_data_free_space)
6124 *
6125 * o reserve space for metadata space, based on the number of outstanding
6126 * extents and how much csums will be needed
6127 * also reserve metadata space in a per root over-reserve method.
6128 * o add to the inodes->delalloc_bytes
6129 * o add it to the fs_info's delalloc inodes list.
6130 * (Above 3 all done in delalloc_reserve_metadata)
6131 *
6132 * Return 0 for success
6133 * Return <0 for error(-ENOSPC or -EQUOT)
6134 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006135int btrfs_delalloc_reserve_space(struct inode *inode, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006136{
6137 int ret;
6138
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006139 ret = btrfs_check_data_free_space(inode, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006140 if (ret < 0)
6141 return ret;
6142 ret = btrfs_delalloc_reserve_metadata(inode, len);
6143 if (ret < 0)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006144 btrfs_free_reserved_data_space(inode, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006145 return ret;
6146}
6147
6148/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006149 * btrfs_delalloc_release_space - release data and metadata space for delalloc
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006150 * @inode: inode we're releasing space for
6151 * @start: start position of the space already reserved
6152 * @len: the len of the space already reserved
6153 *
6154 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
6155 * called in the case that we don't need the metadata AND data reservations
6156 * anymore. So if there is an error or we insert an inline extent.
6157 *
6158 * This function will release the metadata space that was not used and will
6159 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
6160 * list if there are no delalloc bytes left.
6161 * Also it will handle the qgroup reserved space.
6162 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006163void btrfs_delalloc_release_space(struct inode *inode, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08006164{
6165 btrfs_delalloc_release_metadata(inode, len);
Qu Wenruo7cf5b972015-09-08 17:25:55 +08006166 btrfs_free_reserved_data_space(inode, start, len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006167}
6168
Josef Bacikce93ec52014-11-17 15:45:48 -05006169static int update_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6202df62016-06-22 18:54:22 -04006170 struct btrfs_fs_info *info, u64 bytenr,
Josef Bacikce93ec52014-11-17 15:45:48 -05006171 u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04006172{
Josef Bacik0af3d002010-06-21 14:48:16 -04006173 struct btrfs_block_group_cache *cache = NULL;
Chris Masondb945352007-10-15 16:15:53 -04006174 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006175 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04006176 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04006177 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04006178
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006179 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00006180 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02006181 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006182 if (alloc)
6183 old_val += num_bytes;
6184 else
6185 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02006186 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00006187 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006188
Chris Masond3977122009-01-05 21:25:51 -05006189 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04006190 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006191 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006192 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006193 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
6194 BTRFS_BLOCK_GROUP_RAID1 |
6195 BTRFS_BLOCK_GROUP_RAID10))
6196 factor = 2;
6197 else
6198 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04006199 /*
6200 * If this block group has free space cache written out, we
6201 * need to make sure to load it if we are removing space. This
6202 * is because we need the unpinning stage to actually add the
6203 * space back to the block group, otherwise we will leak space.
6204 */
6205 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00006206 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04006207
Chris Masondb945352007-10-15 16:15:53 -04006208 byte_in_group = bytenr - cache->key.objectid;
6209 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04006210
Josef Bacik25179202008-10-29 14:49:05 -04006211 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006212 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04006213
Jeff Mahoney6202df62016-06-22 18:54:22 -04006214 if (btrfs_test_opt(info, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04006215 cache->disk_cache_state < BTRFS_DC_CLEAR)
6216 cache->disk_cache_state = BTRFS_DC_CLEAR;
6217
Chris Mason9078a3e2007-04-26 16:46:15 -04006218 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04006219 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04006220 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04006221 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006222 btrfs_set_block_group_used(&cache->item, old_val);
6223 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04006224 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006225 cache->space_info->bytes_used += num_bytes;
6226 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04006227 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006228 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04006229 } else {
Chris Masondb945352007-10-15 16:15:53 -04006230 old_val -= num_bytes;
Filipe Mananaae0ab002014-11-26 15:28:52 +00006231 btrfs_set_block_group_used(&cache->item, old_val);
6232 cache->pinned += num_bytes;
6233 cache->space_info->bytes_pinned += num_bytes;
6234 cache->space_info->bytes_used -= num_bytes;
6235 cache->space_info->disk_used -= num_bytes * factor;
6236 spin_unlock(&cache->lock);
6237 spin_unlock(&cache->space_info->lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04006238
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006239 trace_btrfs_space_reservation(info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006240 cache->space_info->flags,
6241 num_bytes, 1);
Filipe Mananaae0ab002014-11-26 15:28:52 +00006242 set_extent_dirty(info->pinned_extents,
6243 bytenr, bytenr + num_bytes - 1,
6244 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04006245 }
Chris Mason1bbc6212015-04-06 12:46:08 -07006246
6247 spin_lock(&trans->transaction->dirty_bgs_lock);
6248 if (list_empty(&cache->dirty_list)) {
6249 list_add_tail(&cache->dirty_list,
6250 &trans->transaction->dirty_bgs);
6251 trans->transaction->num_dirty_bgs++;
6252 btrfs_get_block_group(cache);
6253 }
6254 spin_unlock(&trans->transaction->dirty_bgs_lock);
6255
Filipe Manana036a9342015-11-23 15:25:16 +00006256 /*
6257 * No longer have used bytes in this block group, queue it for
6258 * deletion. We do this after adding the block group to the
6259 * dirty list to avoid races between cleaner kthread and space
6260 * cache writeout.
6261 */
6262 if (!alloc && old_val == 0) {
6263 spin_lock(&info->unused_bgs_lock);
6264 if (list_empty(&cache->bg_list)) {
6265 btrfs_get_block_group(cache);
6266 list_add_tail(&cache->bg_list,
6267 &info->unused_bgs);
6268 }
6269 spin_unlock(&info->unused_bgs_lock);
6270 }
6271
Chris Masonfa9c0d792009-04-03 09:47:43 -04006272 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04006273 total -= num_bytes;
6274 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04006275 }
6276 return 0;
6277}
Chris Mason6324fbf2008-03-24 15:01:59 -04006278
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006279static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
Chris Masona061fc82008-05-07 11:43:44 -04006280{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006281 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05006282 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006283
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006284 spin_lock(&fs_info->block_group_cache_lock);
6285 bytenr = fs_info->first_logical_byte;
6286 spin_unlock(&fs_info->block_group_cache_lock);
Liu Boa1897fd2012-12-27 09:01:23 +00006287
6288 if (bytenr < (u64)-1)
6289 return bytenr;
6290
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006291 cache = btrfs_lookup_first_block_group(fs_info, search_start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006292 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04006293 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006294
Yan Zhengd2fb3432008-12-11 16:30:39 -05006295 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006296 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006297
6298 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04006299}
6300
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006301static int pin_down_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006302 struct btrfs_block_group_cache *cache,
6303 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05006304{
Yan Zheng11833d62009-09-11 16:11:19 -04006305 spin_lock(&cache->space_info->lock);
6306 spin_lock(&cache->lock);
6307 cache->pinned += num_bytes;
6308 cache->space_info->bytes_pinned += num_bytes;
6309 if (reserved) {
6310 cache->reserved -= num_bytes;
6311 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05006312 }
Yan Zheng11833d62009-09-11 16:11:19 -04006313 spin_unlock(&cache->lock);
6314 spin_unlock(&cache->space_info->lock);
6315
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006316 trace_btrfs_space_reservation(fs_info, "pinned",
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006317 cache->space_info->flags, num_bytes, 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006318 set_extent_dirty(fs_info->pinned_extents, bytenr,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006319 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05006320 return 0;
6321}
Chris Mason9078a3e2007-04-26 16:46:15 -04006322
Yan, Zhengf0486c62010-05-16 10:46:25 -04006323/*
6324 * this function must be called within transaction
6325 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006326int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006327 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04006328{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006329 struct btrfs_block_group_cache *cache;
6330
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006331 cache = btrfs_lookup_block_group(fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006332 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006333
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006334 pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006335
6336 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04006337 return 0;
6338}
Zheng Yane8569812008-09-26 10:05:48 -04006339
Yan, Zhengf0486c62010-05-16 10:46:25 -04006340/*
Chris Masone688b7252011-10-31 20:52:39 -04006341 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04006342 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006343int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04006344 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006345{
Chris Masone688b7252011-10-31 20:52:39 -04006346 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04006347 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04006348
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006349 cache = btrfs_lookup_block_group(fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006350 if (!cache)
6351 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04006352
6353 /*
6354 * pull in the free space cache (if any) so that our pin
6355 * removes the free space from the cache. We have load_only set
6356 * to one because the slow code to read in the free extents does check
6357 * the pinned extents.
6358 */
Liu Bof6373bf2012-12-27 09:01:18 +00006359 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04006360
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006361 pin_down_extent(fs_info, cache, bytenr, num_bytes, 0);
Chris Masone688b7252011-10-31 20:52:39 -04006362
6363 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04006364 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04006365 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006366 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04006367}
6368
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006369static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
6370 u64 start, u64 num_bytes)
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006371{
6372 int ret;
6373 struct btrfs_block_group_cache *block_group;
6374 struct btrfs_caching_control *caching_ctl;
6375
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006376 block_group = btrfs_lookup_block_group(fs_info, start);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006377 if (!block_group)
6378 return -EINVAL;
6379
6380 cache_block_group(block_group, 0);
6381 caching_ctl = get_caching_control(block_group);
6382
6383 if (!caching_ctl) {
6384 /* Logic error */
6385 BUG_ON(!block_group_cache_done(block_group));
6386 ret = btrfs_remove_free_space(block_group, start, num_bytes);
6387 } else {
6388 mutex_lock(&caching_ctl->mutex);
6389
6390 if (start >= caching_ctl->progress) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006391 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006392 } else if (start + num_bytes <= caching_ctl->progress) {
6393 ret = btrfs_remove_free_space(block_group,
6394 start, num_bytes);
6395 } else {
6396 num_bytes = caching_ctl->progress - start;
6397 ret = btrfs_remove_free_space(block_group,
6398 start, num_bytes);
6399 if (ret)
6400 goto out_lock;
6401
6402 num_bytes = (start + num_bytes) -
6403 caching_ctl->progress;
6404 start = caching_ctl->progress;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006405 ret = add_excluded_extent(fs_info, start, num_bytes);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006406 }
6407out_lock:
6408 mutex_unlock(&caching_ctl->mutex);
6409 put_caching_control(caching_ctl);
6410 }
6411 btrfs_put_block_group(block_group);
6412 return ret;
6413}
6414
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006415int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info,
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006416 struct extent_buffer *eb)
6417{
6418 struct btrfs_file_extent_item *item;
6419 struct btrfs_key key;
6420 int found_type;
6421 int i;
6422
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006423 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006424 return 0;
6425
6426 for (i = 0; i < btrfs_header_nritems(eb); i++) {
6427 btrfs_item_key_to_cpu(eb, &key, i);
6428 if (key.type != BTRFS_EXTENT_DATA_KEY)
6429 continue;
6430 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
6431 found_type = btrfs_file_extent_type(eb, item);
6432 if (found_type == BTRFS_FILE_EXTENT_INLINE)
6433 continue;
6434 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
6435 continue;
6436 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
6437 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006438 __exclude_logged_extent(fs_info, key.objectid, key.offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006439 }
6440
6441 return 0;
6442}
6443
Filipe Manana9cfa3e32016-04-26 15:39:32 +01006444static void
6445btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg)
6446{
6447 atomic_inc(&bg->reservations);
6448}
6449
6450void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
6451 const u64 start)
6452{
6453 struct btrfs_block_group_cache *bg;
6454
6455 bg = btrfs_lookup_block_group(fs_info, start);
6456 ASSERT(bg);
6457 if (atomic_dec_and_test(&bg->reservations))
6458 wake_up_atomic_t(&bg->reservations);
6459 btrfs_put_block_group(bg);
6460}
6461
6462static int btrfs_wait_bg_reservations_atomic_t(atomic_t *a)
6463{
6464 schedule();
6465 return 0;
6466}
6467
6468void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
6469{
6470 struct btrfs_space_info *space_info = bg->space_info;
6471
6472 ASSERT(bg->ro);
6473
6474 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA))
6475 return;
6476
6477 /*
6478 * Our block group is read only but before we set it to read only,
6479 * some task might have had allocated an extent from it already, but it
6480 * has not yet created a respective ordered extent (and added it to a
6481 * root's list of ordered extents).
6482 * Therefore wait for any task currently allocating extents, since the
6483 * block group's reservations counter is incremented while a read lock
6484 * on the groups' semaphore is held and decremented after releasing
6485 * the read access on that semaphore and creating the ordered extent.
6486 */
6487 down_write(&space_info->groups_sem);
6488 up_write(&space_info->groups_sem);
6489
6490 wait_on_atomic_t(&bg->reservations,
6491 btrfs_wait_bg_reservations_atomic_t,
6492 TASK_UNINTERRUPTIBLE);
6493}
6494
Josef Bacikfb25e912011-07-26 17:00:46 -04006495/**
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006496 * btrfs_add_reserved_bytes - update the block_group and space info counters
Josef Bacikfb25e912011-07-26 17:00:46 -04006497 * @cache: The cache we are manipulating
Wang Xiaoguang18513092016-07-25 15:51:40 +08006498 * @ram_bytes: The number of bytes of file content, and will be same to
6499 * @num_bytes except for the compress path.
Josef Bacikfb25e912011-07-26 17:00:46 -04006500 * @num_bytes: The number of bytes in question
Miao Xiee570fd22014-06-19 10:42:50 +08006501 * @delalloc: The blocks are allocated for the delalloc write
Josef Bacikfb25e912011-07-26 17:00:46 -04006502 *
Xiaoguang Wang745699e2016-09-23 12:38:50 +08006503 * This is called by the allocator when it reserves space. If this is a
6504 * reservation and the block group has become read only we cannot make the
6505 * reservation and return -EAGAIN, otherwise this function always succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04006506 */
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006507static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
Wang Xiaoguang18513092016-07-25 15:51:40 +08006508 u64 ram_bytes, u64 num_bytes, int delalloc)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006509{
Josef Bacikfb25e912011-07-26 17:00:46 -04006510 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006511 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006512
Josef Bacikfb25e912011-07-26 17:00:46 -04006513 spin_lock(&space_info->lock);
6514 spin_lock(&cache->lock);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006515 if (cache->ro) {
6516 ret = -EAGAIN;
Josef Bacikfb25e912011-07-26 17:00:46 -04006517 } else {
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006518 cache->reserved += num_bytes;
6519 space_info->bytes_reserved += num_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006520
Wang Xiaoguang18513092016-07-25 15:51:40 +08006521 trace_btrfs_space_reservation(cache->fs_info,
6522 "space_info", space_info->flags,
6523 ram_bytes, 0);
6524 space_info->bytes_may_use -= ram_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006525 if (delalloc)
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006526 cache->delalloc_bytes += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006527 }
Josef Bacikfb25e912011-07-26 17:00:46 -04006528 spin_unlock(&cache->lock);
6529 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006530 return ret;
6531}
6532
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08006533/**
6534 * btrfs_free_reserved_bytes - update the block_group and space info counters
6535 * @cache: The cache we are manipulating
6536 * @num_bytes: The number of bytes in question
6537 * @delalloc: The blocks are allocated for the delalloc write
6538 *
6539 * This is called by somebody who is freeing space that was never actually used
6540 * on disk. For example if you reserve some space for a new leaf in transaction
6541 * A and before transaction A commits you free that leaf, you call this with
6542 * reserve set to 0 in order to clear the reservation.
6543 */
6544
6545static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
6546 u64 num_bytes, int delalloc)
6547{
6548 struct btrfs_space_info *space_info = cache->space_info;
6549 int ret = 0;
6550
6551 spin_lock(&space_info->lock);
6552 spin_lock(&cache->lock);
6553 if (cache->ro)
6554 space_info->bytes_readonly += num_bytes;
6555 cache->reserved -= num_bytes;
6556 space_info->bytes_reserved -= num_bytes;
6557
6558 if (delalloc)
6559 cache->delalloc_bytes -= num_bytes;
6560 spin_unlock(&cache->lock);
6561 spin_unlock(&space_info->lock);
6562 return ret;
6563}
Jeff Mahoney143bede2012-03-01 14:56:26 +01006564void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006565 struct btrfs_fs_info *fs_info)
Yan Zheng11833d62009-09-11 16:11:19 -04006566{
Yan Zheng11833d62009-09-11 16:11:19 -04006567 struct btrfs_caching_control *next;
6568 struct btrfs_caching_control *caching_ctl;
6569 struct btrfs_block_group_cache *cache;
6570
Josef Bacik9e351cc2014-03-13 15:42:13 -04006571 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04006572
6573 list_for_each_entry_safe(caching_ctl, next,
6574 &fs_info->caching_block_groups, list) {
6575 cache = caching_ctl->block_group;
6576 if (block_group_cache_done(cache)) {
6577 cache->last_byte_to_unpin = (u64)-1;
6578 list_del_init(&caching_ctl->list);
6579 put_caching_control(caching_ctl);
6580 } else {
6581 cache->last_byte_to_unpin = caching_ctl->progress;
6582 }
6583 }
6584
6585 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6586 fs_info->pinned_extents = &fs_info->freed_extents[1];
6587 else
6588 fs_info->pinned_extents = &fs_info->freed_extents[0];
6589
Josef Bacik9e351cc2014-03-13 15:42:13 -04006590 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006591
6592 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04006593}
6594
Josef Bacikc759c4e2015-10-02 15:25:10 -04006595/*
6596 * Returns the free cluster for the given space info and sets empty_cluster to
6597 * what it should be based on the mount options.
6598 */
6599static struct btrfs_free_cluster *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006600fetch_cluster_info(struct btrfs_fs_info *fs_info,
6601 struct btrfs_space_info *space_info, u64 *empty_cluster)
Josef Bacikc759c4e2015-10-02 15:25:10 -04006602{
6603 struct btrfs_free_cluster *ret = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006604 bool ssd = btrfs_test_opt(fs_info, SSD);
Josef Bacikc759c4e2015-10-02 15:25:10 -04006605
6606 *empty_cluster = 0;
6607 if (btrfs_mixed_space_info(space_info))
6608 return ret;
6609
6610 if (ssd)
Byongho Leeee221842015-12-15 01:42:10 +09006611 *empty_cluster = SZ_2M;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006612 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006613 ret = &fs_info->meta_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006614 if (!ssd)
Byongho Leeee221842015-12-15 01:42:10 +09006615 *empty_cluster = SZ_64K;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006616 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && ssd) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006617 ret = &fs_info->data_alloc_cluster;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006618 }
6619
6620 return ret;
6621}
6622
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006623static int unpin_extent_range(struct btrfs_fs_info *fs_info,
6624 u64 start, u64 end,
Filipe Manana678886b2014-12-07 21:31:47 +00006625 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04006626{
Yan Zheng11833d62009-09-11 16:11:19 -04006627 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04006628 struct btrfs_space_info *space_info;
6629 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006630 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04006631 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006632 u64 total_unpinned = 0;
6633 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04006634 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04006635
6636 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04006637 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04006638 if (!cache ||
6639 start >= cache->key.objectid + cache->key.offset) {
6640 if (cache)
6641 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04006642 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006643 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006644 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04006645
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006646 cluster = fetch_cluster_info(fs_info,
Josef Bacikc759c4e2015-10-02 15:25:10 -04006647 cache->space_info,
6648 &empty_cluster);
6649 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04006650 }
6651
6652 len = cache->key.objectid + cache->key.offset - start;
6653 len = min(len, end + 1 - start);
6654
6655 if (start < cache->last_byte_to_unpin) {
6656 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00006657 if (return_free_space)
6658 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04006659 }
Josef Bacik25179202008-10-29 14:49:05 -04006660
Yan, Zhengf0486c62010-05-16 10:46:25 -04006661 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006662 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006663 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006664
Josef Bacikc759c4e2015-10-02 15:25:10 -04006665 /*
6666 * If this space cluster has been marked as fragmented and we've
6667 * unpinned enough in this block group to potentially allow a
6668 * cluster to be created inside of it go ahead and clear the
6669 * fragmented check.
6670 */
6671 if (cluster && cluster->fragmented &&
6672 total_unpinned > empty_cluster) {
6673 spin_lock(&cluster->lock);
6674 cluster->fragmented = 0;
6675 spin_unlock(&cluster->lock);
6676 }
6677
Josef Bacik7b398f82012-10-22 15:52:28 -04006678 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006679 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006680 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006681 space_info->bytes_pinned -= len;
Josef Bacikc51e7bb2016-03-25 13:25:54 -04006682
6683 trace_btrfs_space_reservation(fs_info, "pinned",
6684 space_info->flags, len, 0);
Josef Bacik4f4db212015-09-29 11:40:47 -04006685 space_info->max_extent_size = 0;
Liu Bod288db52014-07-02 16:58:01 +08006686 percpu_counter_add(&space_info->total_bytes_pinned, -len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006687 if (cache->ro) {
6688 space_info->bytes_readonly += len;
6689 readonly = true;
6690 }
Josef Bacik25179202008-10-29 14:49:05 -04006691 spin_unlock(&cache->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006692 if (!readonly && return_free_space &&
6693 global_rsv->space_info == space_info) {
6694 u64 to_add = len;
6695 WARN_ON(!return_free_space);
Josef Bacik7b398f82012-10-22 15:52:28 -04006696 spin_lock(&global_rsv->lock);
6697 if (!global_rsv->full) {
Josef Bacik957780e2016-05-17 13:30:55 -04006698 to_add = min(len, global_rsv->size -
6699 global_rsv->reserved);
6700 global_rsv->reserved += to_add;
6701 space_info->bytes_may_use += to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006702 if (global_rsv->reserved >= global_rsv->size)
6703 global_rsv->full = 1;
Josef Bacik957780e2016-05-17 13:30:55 -04006704 trace_btrfs_space_reservation(fs_info,
6705 "space_info",
6706 space_info->flags,
6707 to_add, 1);
6708 len -= to_add;
Josef Bacik7b398f82012-10-22 15:52:28 -04006709 }
6710 spin_unlock(&global_rsv->lock);
Josef Bacik957780e2016-05-17 13:30:55 -04006711 /* Add to any tickets we may have */
6712 if (len)
6713 space_info_add_new_bytes(fs_info, space_info,
6714 len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006715 }
6716 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006717 }
6718
6719 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006720 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04006721 return 0;
6722}
6723
6724int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006725 struct btrfs_fs_info *fs_info)
Chris Masona28ec192007-03-06 20:08:01 -05006726{
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006727 struct btrfs_block_group_cache *block_group, *tmp;
6728 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04006729 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04006730 u64 start;
6731 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05006732 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05006733
Yan Zheng11833d62009-09-11 16:11:19 -04006734 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6735 unpin = &fs_info->freed_extents[1];
6736 else
6737 unpin = &fs_info->freed_extents[0];
6738
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006739 while (!trans->aborted) {
Filipe Mananad4b450c2015-01-29 19:18:25 +00006740 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04006741 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04006742 EXTENT_DIRTY, NULL);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006743 if (ret) {
6744 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05006745 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00006746 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006747
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006748 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006749 ret = btrfs_discard_extent(fs_info, start,
Li Dongyang5378e602011-03-24 10:24:27 +00006750 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006751
David Sterbaaf6f8f62016-04-26 23:54:39 +02006752 clear_extent_dirty(unpin, start, end);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006753 unpin_extent_range(fs_info, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006754 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masonb9473432009-03-13 11:00:37 -04006755 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05006756 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006757
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006758 /*
6759 * Transaction is finished. We don't need the lock anymore. We
6760 * do need to clean up the block groups in case of a transaction
6761 * abort.
6762 */
6763 deleted_bgs = &trans->transaction->deleted_bgs;
6764 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
6765 u64 trimmed = 0;
6766
6767 ret = -EROFS;
6768 if (!trans->aborted)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006769 ret = btrfs_discard_extent(fs_info,
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006770 block_group->key.objectid,
6771 block_group->key.offset,
6772 &trimmed);
6773
6774 list_del_init(&block_group->bg_list);
6775 btrfs_put_block_group_trimming(block_group);
6776 btrfs_put_block_group(block_group);
6777
6778 if (ret) {
6779 const char *errstr = btrfs_decode_error(ret);
6780 btrfs_warn(fs_info,
6781 "Discard failed while removing blockgroup: errno=%d %s\n",
6782 ret, errstr);
6783 }
6784 }
6785
Chris Masone20d96d2007-03-22 12:13:20 -04006786 return 0;
6787}
6788
Josef Bacikb150a4f2013-06-19 15:00:04 -04006789static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
6790 u64 owner, u64 root_objectid)
6791{
6792 struct btrfs_space_info *space_info;
6793 u64 flags;
6794
6795 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
6796 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
6797 flags = BTRFS_BLOCK_GROUP_SYSTEM;
6798 else
6799 flags = BTRFS_BLOCK_GROUP_METADATA;
6800 } else {
6801 flags = BTRFS_BLOCK_GROUP_DATA;
6802 }
6803
6804 space_info = __find_space_info(fs_info, flags);
6805 BUG_ON(!space_info); /* Logic bug */
6806 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
6807}
6808
6809
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006810static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006811 struct btrfs_fs_info *info,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006812 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006813 u64 root_objectid, u64 owner_objectid,
6814 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006815 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05006816{
Chris Masone2fa7222007-03-12 16:22:34 -04006817 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006818 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04006819 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04006820 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006821 struct btrfs_extent_item *ei;
6822 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05006823 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006824 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05006825 int extent_slot = 0;
6826 int found_extent = 0;
6827 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006828 u32 item_size;
6829 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006830 u64 bytenr = node->bytenr;
6831 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07006832 int last_ref = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006833 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05006834
Chris Mason5caf2a02007-04-02 11:20:42 -04006835 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04006836 if (!path)
6837 return -ENOMEM;
6838
David Sterbae4058b52015-11-27 16:31:35 +01006839 path->reada = READA_FORWARD;
Chris Masonb9473432009-03-13 11:00:37 -04006840 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006841
6842 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
6843 BUG_ON(!is_data && refs_to_drop != 1);
6844
Josef Bacik3173a182013-03-07 14:22:04 -05006845 if (is_data)
6846 skinny_metadata = 0;
6847
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006848 ret = lookup_extent_backref(trans, extent_root, path, &iref,
6849 bytenr, num_bytes, parent,
6850 root_objectid, owner_objectid,
6851 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05006852 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05006853 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006854 while (extent_slot >= 0) {
6855 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05006856 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006857 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05006858 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006859 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
6860 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05006861 found_extent = 1;
6862 break;
6863 }
Josef Bacik3173a182013-03-07 14:22:04 -05006864 if (key.type == BTRFS_METADATA_ITEM_KEY &&
6865 key.offset == owner_objectid) {
6866 found_extent = 1;
6867 break;
6868 }
Chris Mason952fcca2008-02-18 16:33:44 -05006869 if (path->slots[0] - extent_slot > 5)
6870 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006871 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05006872 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006873#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6874 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
6875 if (found_extent && item_size < sizeof(*ei))
6876 found_extent = 0;
6877#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04006878 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006879 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04006880 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006881 NULL, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07006882 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06006883 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006884 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006885 goto out;
6886 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006887 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04006888 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006889
6890 key.objectid = bytenr;
6891 key.type = BTRFS_EXTENT_ITEM_KEY;
6892 key.offset = num_bytes;
6893
Josef Bacik3173a182013-03-07 14:22:04 -05006894 if (!is_data && skinny_metadata) {
6895 key.type = BTRFS_METADATA_ITEM_KEY;
6896 key.offset = owner_objectid;
6897 }
6898
Zheng Yan31840ae2008-09-23 13:14:14 -04006899 ret = btrfs_search_slot(trans, extent_root,
6900 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05006901 if (ret > 0 && skinny_metadata && path->slots[0]) {
6902 /*
6903 * Couldn't find our skinny metadata item,
6904 * see if we have ye olde extent item.
6905 */
6906 path->slots[0]--;
6907 btrfs_item_key_to_cpu(path->nodes[0], &key,
6908 path->slots[0]);
6909 if (key.objectid == bytenr &&
6910 key.type == BTRFS_EXTENT_ITEM_KEY &&
6911 key.offset == num_bytes)
6912 ret = 0;
6913 }
6914
6915 if (ret > 0 && skinny_metadata) {
6916 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01006917 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05006918 key.type = BTRFS_EXTENT_ITEM_KEY;
6919 key.offset = num_bytes;
6920 btrfs_release_path(path);
6921 ret = btrfs_search_slot(trans, extent_root,
6922 &key, path, -1, 1);
6923 }
6924
Josef Bacikf3465ca2008-11-12 14:19:50 -05006925 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006926 btrfs_err(info,
6927 "umm, got %d back from search, was looking for %llu",
6928 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00006929 if (ret > 0)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006930 btrfs_print_leaf(info, path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006931 }
David Sterba005d6422012-09-18 07:52:32 -06006932 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006933 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006934 goto out;
6935 }
Zheng Yan31840ae2008-09-23 13:14:14 -04006936 extent_slot = path->slots[0];
6937 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306938 } else if (WARN_ON(ret == -ENOENT)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006939 btrfs_print_leaf(info, path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006940 btrfs_err(info,
6941 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006942 bytenr, parent, root_objectid, owner_objectid,
6943 owner_offset);
Jeff Mahoney66642832016-06-10 18:19:25 -04006944 btrfs_abort_transaction(trans, ret);
Josef Bacikc4a050b2014-03-14 16:36:53 -04006945 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006946 } else {
Jeff Mahoney66642832016-06-10 18:19:25 -04006947 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006948 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05006949 }
Chris Mason5f39d392007-10-15 16:14:19 -04006950
6951 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006952 item_size = btrfs_item_size_nr(leaf, extent_slot);
6953#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6954 if (item_size < sizeof(*ei)) {
6955 BUG_ON(found_extent || extent_slot != path->slots[0]);
6956 ret = convert_extent_item_v0(trans, extent_root, path,
6957 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06006958 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006959 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006960 goto out;
6961 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006962
David Sterbab3b4aa72011-04-21 01:20:15 +02006963 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006964 path->leave_spinning = 1;
6965
6966 key.objectid = bytenr;
6967 key.type = BTRFS_EXTENT_ITEM_KEY;
6968 key.offset = num_bytes;
6969
6970 ret = btrfs_search_slot(trans, extent_root, &key, path,
6971 -1, 1);
6972 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006973 btrfs_err(info,
6974 "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006975 ret, bytenr);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006976 btrfs_print_leaf(info, path->nodes[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006977 }
David Sterba005d6422012-09-18 07:52:32 -06006978 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006979 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06006980 goto out;
6981 }
6982
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006983 extent_slot = path->slots[0];
6984 leaf = path->nodes[0];
6985 item_size = btrfs_item_size_nr(leaf, extent_slot);
6986 }
6987#endif
6988 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05006989 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04006990 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05006991 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
6992 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006993 struct btrfs_tree_block_info *bi;
6994 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
6995 bi = (struct btrfs_tree_block_info *)(ei + 1);
6996 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05006997 }
6998
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006999 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04007000 if (refs < refs_to_drop) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007001 btrfs_err(info,
7002 "trying to drop %d refs but we only have %Lu for bytenr %Lu",
7003 refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04007004 ret = -EINVAL;
Jeff Mahoney66642832016-06-10 18:19:25 -04007005 btrfs_abort_transaction(trans, ret);
Josef Bacik32b02532013-04-24 16:38:50 -04007006 goto out;
7007 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007008 refs -= refs_to_drop;
7009
7010 if (refs > 0) {
7011 if (extent_op)
7012 __run_delayed_extent_op(extent_op, leaf, ei);
7013 /*
7014 * In the case of inline back ref, reference count will
7015 * be updated by remove_extent_backref
7016 */
7017 if (iref) {
7018 BUG_ON(!found_extent);
7019 } else {
7020 btrfs_set_extent_refs(leaf, ei, refs);
7021 btrfs_mark_buffer_dirty(leaf);
7022 }
7023 if (found_extent) {
7024 ret = remove_extent_backref(trans, extent_root, path,
7025 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07007026 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06007027 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007028 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007029 goto out;
7030 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007031 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007032 add_pinned_bytes(info, -num_bytes, owner_objectid,
Josef Bacikb150a4f2013-06-19 15:00:04 -04007033 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007034 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007035 if (found_extent) {
7036 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08007037 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007038 if (iref) {
7039 BUG_ON(path->slots[0] != extent_slot);
7040 } else {
7041 BUG_ON(path->slots[0] != extent_slot + 1);
7042 path->slots[0] = extent_slot;
7043 num_to_del = 2;
7044 }
Chris Mason78fae272007-03-25 11:35:08 -04007045 }
Chris Masonb9473432009-03-13 11:00:37 -04007046
Josef Bacikfcebe452014-05-13 17:30:47 -07007047 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05007048 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
7049 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06007050 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007051 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007052 goto out;
7053 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007054 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01007055
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007056 if (is_data) {
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007057 ret = btrfs_del_csums(trans, info, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06007058 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007059 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007060 goto out;
7061 }
Chris Mason459931e2008-12-10 09:10:46 -05007062 }
7063
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007064 ret = add_to_free_space_tree(trans, info, bytenr, num_bytes);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007065 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007066 btrfs_abort_transaction(trans, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007067 goto out;
7068 }
7069
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007070 ret = update_block_group(trans, info, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06007071 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04007072 btrfs_abort_transaction(trans, ret);
David Sterba005d6422012-09-18 07:52:32 -06007073 goto out;
7074 }
Chris Masona28ec192007-03-06 20:08:01 -05007075 }
Josef Bacikfcebe452014-05-13 17:30:47 -07007076 btrfs_release_path(path);
7077
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007078out:
Chris Mason5caf2a02007-04-02 11:20:42 -04007079 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05007080 return ret;
7081}
7082
7083/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007084 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04007085 * delayed ref for that extent as well. This searches the delayed ref tree for
7086 * a given extent, and if there are no other delayed refs to be processed, it
7087 * removes it from the tree.
7088 */
7089static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007090 u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -04007091{
7092 struct btrfs_delayed_ref_head *head;
7093 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007094 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04007095
7096 delayed_refs = &trans->transaction->delayed_refs;
7097 spin_lock(&delayed_refs->lock);
7098 head = btrfs_find_delayed_ref_head(trans, bytenr);
7099 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08007100 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04007101
Josef Bacikd7df2c72014-01-23 09:21:38 -05007102 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08007103 if (!list_empty(&head->ref_list))
Chris Mason1887be62009-03-13 10:11:24 -04007104 goto out;
7105
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007106 if (head->extent_op) {
7107 if (!head->must_insert_reserved)
7108 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00007109 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007110 head->extent_op = NULL;
7111 }
7112
Chris Mason1887be62009-03-13 10:11:24 -04007113 /*
7114 * waiting for the lock here would deadlock. If someone else has it
7115 * locked they are already in the process of dropping it anyway
7116 */
7117 if (!mutex_trylock(&head->mutex))
7118 goto out;
7119
7120 /*
7121 * at this point we have a head with no other entries. Go
7122 * ahead and process it.
7123 */
7124 head->node.in_tree = 0;
Liu Boc46effa2013-10-14 12:59:45 +08007125 rb_erase(&head->href_node, &delayed_refs->href_root);
Chris Masonc3e69d52009-03-13 10:17:05 -04007126
Josef Bacikd7df2c72014-01-23 09:21:38 -05007127 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04007128
7129 /*
7130 * we don't take a ref on the node because we're removing it from the
7131 * tree, so we just steal the ref the tree was holding.
7132 */
Chris Masonc3e69d52009-03-13 10:17:05 -04007133 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007134 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04007135 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05007136 head->processing = 0;
7137 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04007138 spin_unlock(&delayed_refs->lock);
7139
Yan, Zhengf0486c62010-05-16 10:46:25 -04007140 BUG_ON(head->extent_op);
7141 if (head->must_insert_reserved)
7142 ret = 1;
7143
7144 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04007145 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007146 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04007147out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05007148 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08007149
7150out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04007151 spin_unlock(&delayed_refs->lock);
7152 return 0;
7153}
7154
Yan, Zhengf0486c62010-05-16 10:46:25 -04007155void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7156 struct btrfs_root *root,
7157 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02007158 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007159{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007160 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb150a4f2013-06-19 15:00:04 -04007161 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007162 int ret;
7163
7164 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007165 ret = btrfs_add_delayed_tree_ref(fs_info, trans,
7166 buf->start, buf->len,
7167 parent,
7168 root->root_key.objectid,
7169 btrfs_header_level(buf),
7170 BTRFS_DROP_DELAYED_REF, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007171 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007172 }
7173
7174 if (!last_ref)
7175 return;
7176
Yan, Zhengf0486c62010-05-16 10:46:25 -04007177 if (btrfs_header_generation(buf) == trans->transid) {
Filipe Manana62198722015-01-06 20:18:45 +00007178 struct btrfs_block_group_cache *cache;
7179
Yan, Zhengf0486c62010-05-16 10:46:25 -04007180 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007181 ret = check_ref_cleanup(trans, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007182 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04007183 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007184 }
7185
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007186 cache = btrfs_lookup_block_group(fs_info, buf->start);
Filipe Manana62198722015-01-06 20:18:45 +00007187
Yan, Zhengf0486c62010-05-16 10:46:25 -04007188 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007189 pin_down_extent(fs_info, cache, buf->start,
7190 buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00007191 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04007192 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007193 }
7194
7195 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
7196
7197 btrfs_add_free_space(cache, buf->start, buf->len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08007198 btrfs_free_reserved_bytes(cache, buf->len, 0);
Filipe Manana62198722015-01-06 20:18:45 +00007199 btrfs_put_block_group(cache);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007200 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
Josef Bacikb150a4f2013-06-19 15:00:04 -04007201 pin = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007202 }
7203out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04007204 if (pin)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007205 add_pinned_bytes(fs_info, buf->len, btrfs_header_level(buf),
Josef Bacikb150a4f2013-06-19 15:00:04 -04007206 root->root_key.objectid);
7207
Josef Bacika826d6d2011-03-16 13:42:43 -04007208 /*
7209 * Deleting the buffer, clear the corrupt flag since it doesn't matter
7210 * anymore.
7211 */
7212 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007213}
7214
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007215/* Can return -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007216int btrfs_free_extent(struct btrfs_trans_handle *trans,
7217 struct btrfs_fs_info *fs_info,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007218 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01007219 u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04007220{
7221 int ret;
7222
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04007223 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007224 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02007225
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007226 add_pinned_bytes(fs_info, num_bytes, owner, root_objectid);
Josef Bacikb150a4f2013-06-19 15:00:04 -04007227
Chris Mason56bec292009-03-13 10:10:06 -04007228 /*
7229 * tree log blocks never actually go into the extent allocation
7230 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04007231 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007232 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
7233 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04007234 /* unlocks the pinned mutex */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007235 btrfs_pin_extent(fs_info, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04007236 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007237 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007238 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
7239 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007240 parent, root_objectid, (int)owner,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01007241 BTRFS_DROP_DELAYED_REF, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007242 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007243 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
7244 num_bytes,
7245 parent, root_objectid, owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08007246 offset, 0,
7247 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason56bec292009-03-13 10:10:06 -04007248 }
Chris Mason925baed2008-06-25 16:01:30 -04007249 return ret;
7250}
7251
Chris Masonfec577f2007-02-26 10:40:21 -05007252/*
Josef Bacik817d52f2009-07-13 21:29:25 -04007253 * when we wait for progress in the block group caching, its because
7254 * our allocation attempt failed at least once. So, we must sleep
7255 * and let some progress happen before we try again.
7256 *
7257 * This function will sleep at least once waiting for new free space to
7258 * show up, and then it will check the block group free space numbers
7259 * for our min num_bytes. Another option is to have it go ahead
7260 * and look in the rbtree for a free extent of a given size, but this
7261 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04007262 *
7263 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
7264 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04007265 */
Josef Bacik36cce922013-08-05 11:15:21 -04007266static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04007267wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
7268 u64 num_bytes)
7269{
Yan Zheng11833d62009-09-11 16:11:19 -04007270 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04007271
Yan Zheng11833d62009-09-11 16:11:19 -04007272 caching_ctl = get_caching_control(cache);
7273 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007274 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04007275
Yan Zheng11833d62009-09-11 16:11:19 -04007276 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08007277 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04007278
7279 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04007280}
7281
7282static noinline int
7283wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
7284{
7285 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04007286 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007287
7288 caching_ctl = get_caching_control(cache);
7289 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04007290 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04007291
7292 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04007293 if (cache->cached == BTRFS_CACHE_ERROR)
7294 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04007295 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04007296 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04007297}
7298
Liu Bo31e50222012-11-21 14:18:10 +00007299int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04007300{
Ilya Dryomov7738a532012-03-27 17:09:17 +03007301 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00007302 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007303 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00007304 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007305 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00007306 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007307 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00007308 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05007309 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05007310 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05007311 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05007312 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03007313
Chris Masone942f882013-02-20 14:06:05 -05007314 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04007315}
7316
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007317int get_block_group_index(struct btrfs_block_group_cache *cache)
Ilya Dryomov7738a532012-03-27 17:09:17 +03007318{
Liu Bo31e50222012-11-21 14:18:10 +00007319 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03007320}
7321
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007322static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
7323 [BTRFS_RAID_RAID10] = "raid10",
7324 [BTRFS_RAID_RAID1] = "raid1",
7325 [BTRFS_RAID_DUP] = "dup",
7326 [BTRFS_RAID_RAID0] = "raid0",
7327 [BTRFS_RAID_SINGLE] = "single",
7328 [BTRFS_RAID_RAID5] = "raid5",
7329 [BTRFS_RAID_RAID6] = "raid6",
7330};
7331
Jeff Mahoney1b8e5df2013-11-20 16:50:23 -05007332static const char *get_raid_name(enum btrfs_raid_types type)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04007333{
7334 if (type >= BTRFS_NR_RAID_TYPES)
7335 return NULL;
7336
7337 return btrfs_raid_type_names[type];
7338}
7339
Josef Bacik817d52f2009-07-13 21:29:25 -04007340enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05007341 LOOP_CACHING_NOWAIT = 0,
7342 LOOP_CACHING_WAIT = 1,
7343 LOOP_ALLOC_CHUNK = 2,
7344 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04007345};
7346
Miao Xiee570fd22014-06-19 10:42:50 +08007347static inline void
7348btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
7349 int delalloc)
7350{
7351 if (delalloc)
7352 down_read(&cache->data_rwsem);
7353}
7354
7355static inline void
7356btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
7357 int delalloc)
7358{
7359 btrfs_get_block_group(cache);
7360 if (delalloc)
7361 down_read(&cache->data_rwsem);
7362}
7363
7364static struct btrfs_block_group_cache *
7365btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
7366 struct btrfs_free_cluster *cluster,
7367 int delalloc)
7368{
Sudip Mukherjee89771cc2016-02-16 13:32:47 +05307369 struct btrfs_block_group_cache *used_bg = NULL;
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007370
Miao Xiee570fd22014-06-19 10:42:50 +08007371 spin_lock(&cluster->refill_lock);
Geert Uytterhoeven6719afd2014-06-22 14:30:09 +02007372 while (1) {
7373 used_bg = cluster->block_group;
7374 if (!used_bg)
7375 return NULL;
7376
7377 if (used_bg == block_group)
7378 return used_bg;
7379
7380 btrfs_get_block_group(used_bg);
7381
7382 if (!delalloc)
7383 return used_bg;
7384
7385 if (down_read_trylock(&used_bg->data_rwsem))
7386 return used_bg;
7387
7388 spin_unlock(&cluster->refill_lock);
7389
7390 down_read(&used_bg->data_rwsem);
7391
7392 spin_lock(&cluster->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007393 if (used_bg == cluster->block_group)
7394 return used_bg;
7395
7396 up_read(&used_bg->data_rwsem);
7397 btrfs_put_block_group(used_bg);
7398 }
Miao Xiee570fd22014-06-19 10:42:50 +08007399}
7400
7401static inline void
7402btrfs_release_block_group(struct btrfs_block_group_cache *cache,
7403 int delalloc)
7404{
7405 if (delalloc)
7406 up_read(&cache->data_rwsem);
7407 btrfs_put_block_group(cache);
7408}
7409
Josef Bacik817d52f2009-07-13 21:29:25 -04007410/*
Chris Masonfec577f2007-02-26 10:40:21 -05007411 * walks the btree of allocated extents and find a hole of a given size.
7412 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08007413 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04007414 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08007415 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05007416 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08007417 *
7418 * If there is no suitable free space, we will record the max size of
7419 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05007420 */
Josef Bacik00361582013-08-14 14:02:47 -04007421static noinline int find_free_extent(struct btrfs_root *orig_root,
Wang Xiaoguang18513092016-07-25 15:51:40 +08007422 u64 ram_bytes, u64 num_bytes, u64 empty_size,
7423 u64 hint_byte, struct btrfs_key *ins,
7424 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007425{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007426 struct btrfs_fs_info *fs_info = orig_root->fs_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04007427 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007428 struct btrfs_root *root = fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007429 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04007430 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05007431 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08007432 u64 max_extent_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007433 u64 empty_cluster = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04007434 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007435 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00007436 int index = __get_raid_index(flags);
Josef Bacik0a243252009-09-11 16:11:20 -04007437 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007438 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04007439 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08007440 bool have_caching_bg = false;
chandan13a0db52015-11-02 13:59:46 +05307441 bool orig_have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007442 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05007443
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007444 WARN_ON(num_bytes < fs_info->sectorsize);
David Sterba962a2982014-06-04 18:41:45 +02007445 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04007446 ins->objectid = 0;
7447 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04007448
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007449 trace_find_free_extent(fs_info, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05007450
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007451 space_info = __find_space_info(fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007452 if (!space_info) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007453 btrfs_err(fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007454 return -ENOSPC;
7455 }
Josef Bacik2552d172009-04-03 10:14:19 -04007456
Josef Bacik67377732010-09-16 16:19:09 -04007457 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04007458 * If our free space is heavily fragmented we may not be able to make
7459 * big contiguous allocations, so instead of doing the expensive search
7460 * for free space, simply return ENOSPC with our max_extent_size so we
7461 * can go ahead and search for a more manageable chunk.
7462 *
7463 * If our max_extent_size is large enough for our allocation simply
7464 * disable clustering since we will likely not be able to find enough
7465 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04007466 */
Josef Bacik4f4db212015-09-29 11:40:47 -04007467 if (unlikely(space_info->max_extent_size)) {
7468 spin_lock(&space_info->lock);
7469 if (space_info->max_extent_size &&
7470 num_bytes > space_info->max_extent_size) {
7471 ins->offset = space_info->max_extent_size;
7472 spin_unlock(&space_info->lock);
7473 return -ENOSPC;
7474 } else if (space_info->max_extent_size) {
7475 use_cluster = false;
7476 }
7477 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007478 }
7479
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007480 last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04007481 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007482 spin_lock(&last_ptr->lock);
7483 if (last_ptr->block_group)
7484 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007485 if (last_ptr->fragmented) {
7486 /*
7487 * We still set window_start so we can keep track of the
7488 * last place we found an allocation to try and save
7489 * some time.
7490 */
7491 hint_byte = last_ptr->window_start;
7492 use_cluster = false;
7493 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007494 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007495 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007496
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007497 search_start = max(search_start, first_logical_byte(fs_info, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04007498 search_start = max(search_start, hint_byte);
Josef Bacik2552d172009-04-03 10:14:19 -04007499 if (search_start == hint_byte) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007500 block_group = btrfs_lookup_block_group(fs_info, search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04007501 /*
7502 * we don't want to use the block group if it doesn't match our
7503 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05007504 *
7505 * However if we are re-searching with an ideal block group
7506 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04007507 */
David Sterbab6919a52013-04-29 13:39:40 +00007508 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05007509 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04007510 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007511 if (list_empty(&block_group->list) ||
7512 block_group->ro) {
7513 /*
7514 * someone is removing this block group,
7515 * we can't jump into the have_block_group
7516 * target because our list pointers are not
7517 * valid
7518 */
7519 btrfs_put_block_group(block_group);
7520 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05007521 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04007522 index = get_block_group_index(block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007523 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04007524 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05007525 }
Josef Bacik2552d172009-04-03 10:14:19 -04007526 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007527 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04007528 }
Chris Mason42e70e72008-11-07 18:17:11 -05007529 }
Josef Bacik2552d172009-04-03 10:14:19 -04007530search:
Miao Xie60d2adb2011-09-09 17:34:35 +08007531 have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007532 if (index == 0 || index == __get_raid_index(flags))
7533 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04007534 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007535 list_for_each_entry(block_group, &space_info->block_groups[index],
7536 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04007537 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04007538 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05007539
Miao Xiee570fd22014-06-19 10:42:50 +08007540 btrfs_grab_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007541 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05007542
Chris Mason83a50de2010-12-13 15:06:46 -05007543 /*
7544 * this can happen if we end up cycling through all the
7545 * raid types, but we want to make sure we only allocate
7546 * for the proper type.
7547 */
David Sterbab6919a52013-04-29 13:39:40 +00007548 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05007549 u64 extra = BTRFS_BLOCK_GROUP_DUP |
7550 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007551 BTRFS_BLOCK_GROUP_RAID5 |
7552 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05007553 BTRFS_BLOCK_GROUP_RAID10;
7554
7555 /*
7556 * if they asked for extra copies and this block group
7557 * doesn't provide them, bail. This does allow us to
7558 * fill raid0 from raid1.
7559 */
David Sterbab6919a52013-04-29 13:39:40 +00007560 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05007561 goto loop;
7562 }
7563
Josef Bacik2552d172009-04-03 10:14:19 -04007564have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05007565 cached = block_group_cache_done(block_group);
7566 if (unlikely(!cached)) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007567 have_caching_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00007568 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04007569 BUG_ON(ret < 0);
7570 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05007571 }
7572
Josef Bacik36cce922013-08-05 11:15:21 -04007573 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
7574 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05007575 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04007576 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007577
Josef Bacik0a243252009-09-11 16:11:20 -04007578 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007579 * Ok we want to try and use the cluster allocator, so
7580 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04007581 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04007582 if (last_ptr && use_cluster) {
Miao Xie215a63d2014-01-15 20:00:56 +08007583 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05007584 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007585 /*
7586 * the refill lock keeps out other
7587 * people trying to start a new cluster
7588 */
Miao Xiee570fd22014-06-19 10:42:50 +08007589 used_block_group = btrfs_lock_cluster(block_group,
7590 last_ptr,
7591 delalloc);
7592 if (!used_block_group)
Chris Mason44fb5512009-06-04 15:34:51 -04007593 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04007594
Miao Xiee570fd22014-06-19 10:42:50 +08007595 if (used_block_group != block_group &&
7596 (used_block_group->ro ||
7597 !block_group_bits(used_block_group, flags)))
7598 goto release_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007599
7600 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007601 last_ptr,
7602 num_bytes,
7603 used_block_group->key.objectid,
7604 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007605 if (offset) {
7606 /* we have a block, we're done */
7607 spin_unlock(&last_ptr->refill_lock);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007608 trace_btrfs_reserve_extent_cluster(fs_info,
Miao Xie89d43462014-01-15 20:00:57 +08007609 used_block_group,
7610 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08007611 if (used_block_group != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08007612 btrfs_release_block_group(block_group,
7613 delalloc);
Miao Xie215a63d2014-01-15 20:00:56 +08007614 block_group = used_block_group;
7615 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007616 goto checks;
7617 }
7618
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007619 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007620release_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007621 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7622 * set up a new clusters, so lets just skip it
7623 * and let the allocator find whatever block
7624 * it can find. If we reach this point, we
7625 * will have tried the cluster allocator
7626 * plenty of times and not have found
7627 * anything, so we are likely way too
7628 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007629 * anything.
7630 *
7631 * However, if the cluster is taken from the
7632 * current block group, release the cluster
7633 * first, so that we stand a better chance of
7634 * succeeding in the unclustered
7635 * allocation. */
7636 if (loop >= LOOP_NO_EMPTY_SIZE &&
Miao Xiee570fd22014-06-19 10:42:50 +08007637 used_block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007638 spin_unlock(&last_ptr->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007639 btrfs_release_block_group(used_block_group,
7640 delalloc);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007641 goto unclustered_alloc;
7642 }
7643
Chris Masonfa9c0d792009-04-03 09:47:43 -04007644 /*
7645 * this cluster didn't work out, free it and
7646 * start over
7647 */
7648 btrfs_return_cluster_to_free_space(NULL, last_ptr);
7649
Miao Xiee570fd22014-06-19 10:42:50 +08007650 if (used_block_group != block_group)
7651 btrfs_release_block_group(used_block_group,
7652 delalloc);
7653refill_cluster:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007654 if (loop >= LOOP_NO_EMPTY_SIZE) {
7655 spin_unlock(&last_ptr->refill_lock);
7656 goto unclustered_alloc;
7657 }
7658
Chris Mason8de972b2013-01-04 15:39:43 -05007659 aligned_cluster = max_t(unsigned long,
7660 empty_cluster + empty_size,
7661 block_group->full_stripe_len);
7662
Chris Masonfa9c0d792009-04-03 09:47:43 -04007663 /* allocate a cluster in this block group */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007664 ret = btrfs_find_space_cluster(fs_info, block_group,
Josef Bacik00361582013-08-14 14:02:47 -04007665 last_ptr, search_start,
7666 num_bytes,
7667 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007668 if (ret == 0) {
7669 /*
7670 * now pull our allocation out of this
7671 * cluster
7672 */
7673 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007674 last_ptr,
7675 num_bytes,
7676 search_start,
7677 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007678 if (offset) {
7679 /* we found one, proceed */
7680 spin_unlock(&last_ptr->refill_lock);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04007681 trace_btrfs_reserve_extent_cluster(fs_info,
Josef Bacik3f7de032011-11-10 08:29:20 -05007682 block_group, search_start,
7683 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007684 goto checks;
7685 }
Josef Bacik0a243252009-09-11 16:11:20 -04007686 } else if (!cached && loop > LOOP_CACHING_NOWAIT
7687 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007688 spin_unlock(&last_ptr->refill_lock);
7689
Josef Bacik0a243252009-09-11 16:11:20 -04007690 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007691 wait_block_group_cache_progress(block_group,
7692 num_bytes + empty_cluster + empty_size);
7693 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007694 }
Josef Bacik817d52f2009-07-13 21:29:25 -04007695
Chris Masonfa9c0d792009-04-03 09:47:43 -04007696 /*
7697 * at this point we either didn't find a cluster
7698 * or we weren't able to allocate a block from our
7699 * cluster. Free the cluster we've been trying
7700 * to use, and go to the next block group
7701 */
Josef Bacik0a243252009-09-11 16:11:20 -04007702 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007703 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04007704 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007705 }
7706
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007707unclustered_alloc:
Josef Bacikc759c4e2015-10-02 15:25:10 -04007708 /*
7709 * We are doing an unclustered alloc, set the fragmented flag so
7710 * we don't bother trying to setup a cluster again until we get
7711 * more space.
7712 */
7713 if (unlikely(last_ptr)) {
7714 spin_lock(&last_ptr->lock);
7715 last_ptr->fragmented = 1;
7716 spin_unlock(&last_ptr->lock);
7717 }
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007718 spin_lock(&block_group->free_space_ctl->tree_lock);
7719 if (cached &&
7720 block_group->free_space_ctl->free_space <
7721 num_bytes + empty_cluster + empty_size) {
Miao Xiea4820392013-09-09 13:19:42 +08007722 if (block_group->free_space_ctl->free_space >
7723 max_extent_size)
7724 max_extent_size =
7725 block_group->free_space_ctl->free_space;
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007726 spin_unlock(&block_group->free_space_ctl->tree_lock);
7727 goto loop;
7728 }
7729 spin_unlock(&block_group->free_space_ctl->tree_lock);
7730
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, Zhengb742bb82010-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, Zhengb742bb82010-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, Zhengb742bb82010-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 Bacikd82a6f12011-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, Zhengb742bb82010-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,
7910 info->total_bytes - info->bytes_used - info->bytes_pinned -
7911 info->bytes_reserved - info->bytes_readonly -
7912 info->bytes_may_use, (info->full) ? "" : "not ");
7913 btrfs_info(fs_info,
7914 "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu",
7915 info->total_bytes, info->bytes_used, info->bytes_pinned,
7916 info->bytes_reserved, info->bytes_may_use,
7917 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007918 spin_unlock(&info->lock);
7919
7920 if (!dump_block_groups)
7921 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007922
Josef Bacik80eb2342008-10-29 14:49:05 -04007923 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04007924again:
7925 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04007926 spin_lock(&cache->lock);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007927 btrfs_info(fs_info,
7928 "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s",
7929 cache->key.objectid, cache->key.offset,
7930 btrfs_block_group_used(&cache->item), cache->pinned,
7931 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04007932 btrfs_dump_free_space(cache, bytes);
7933 spin_unlock(&cache->lock);
7934 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04007935 if (++index < BTRFS_NR_RAID_TYPES)
7936 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04007937 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007938}
Zheng Yane8569812008-09-26 10:05:48 -04007939
Wang Xiaoguang18513092016-07-25 15:51:40 +08007940int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
Yan Zheng11833d62009-09-11 16:11:19 -04007941 u64 num_bytes, u64 min_alloc_size,
7942 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08007943 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007944{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007945 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik36af4e02015-09-25 16:13:11 -04007946 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00007947 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05007948 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04007949
David Sterbab6919a52013-04-29 13:39:40 +00007950 flags = btrfs_get_alloc_profile(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04007951again:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007952 WARN_ON(num_bytes < fs_info->sectorsize);
Wang Xiaoguang18513092016-07-25 15:51:40 +08007953 ret = find_free_extent(root, ram_bytes, num_bytes, empty_size,
7954 hint_byte, ins, flags, delalloc);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007955 if (!ret && !is_data) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007956 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007957 } else if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08007958 if (!final_tried && ins->offset) {
7959 num_bytes = min(num_bytes >> 1, ins->offset);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007960 num_bytes = round_down(num_bytes,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007961 fs_info->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05007962 num_bytes = max(num_bytes, min_alloc_size);
Wang Xiaoguang18513092016-07-25 15:51:40 +08007963 ram_bytes = num_bytes;
Miao Xie9e622d62012-01-26 15:01:12 -05007964 if (num_bytes == min_alloc_size)
7965 final_tried = true;
7966 goto again;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007967 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xie9e622d62012-01-26 15:01:12 -05007968 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007969
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007970 sinfo = __find_space_info(fs_info, flags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007971 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007972 "allocation failed flags %llu, wanted %llu",
7973 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01007974 if (sinfo)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007975 dump_space_info(fs_info, sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05007976 }
Chris Mason925baed2008-06-25 16:01:30 -04007977 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007978
7979 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007980}
7981
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007982static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08007983 u64 start, u64 len,
7984 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04007985{
Josef Bacik0f9dd462008-09-23 13:14:11 -04007986 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05007987 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007988
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007989 cache = btrfs_lookup_block_group(fs_info, start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007990 if (!cache) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007991 btrfs_err(fs_info, "Unable to find block group for %llu",
7992 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007993 return -ENOSPC;
7994 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05007995
Chris Masone688b7252011-10-31 20:52:39 -04007996 if (pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007997 pin_down_extent(fs_info, cache, start, len, 1);
Chris Masone688b7252011-10-31 20:52:39 -04007998 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007999 if (btrfs_test_opt(fs_info, DISCARD))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008000 ret = btrfs_discard_extent(fs_info, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04008001 btrfs_add_free_space(cache, start, len);
Wang Xiaoguang4824f1f2016-07-25 15:51:39 +08008002 btrfs_free_reserved_bytes(cache, len, delalloc);
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008003 trace_btrfs_reserved_extent_free(fs_info, start, len);
Chris Masone688b7252011-10-31 20:52:39 -04008004 }
Dongsheng Yang31193212014-12-12 16:44:35 +08008005
Chris Masonfa9c0d792009-04-03 09:47:43 -04008006 btrfs_put_block_group(cache);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008007 return ret;
8008}
8009
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008010int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08008011 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04008012{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008013 return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04008014}
8015
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008016int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
Chris Masone688b7252011-10-31 20:52:39 -04008017 u64 start, u64 len)
8018{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008019 return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04008020}
8021
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008022static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008023 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008024 u64 parent, u64 root_objectid,
8025 u64 flags, u64 owner, u64 offset,
8026 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008027{
8028 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008029 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008030 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008031 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008032 struct extent_buffer *leaf;
8033 int type;
8034 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04008035
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008036 if (parent > 0)
8037 type = BTRFS_SHARED_DATA_REF_KEY;
8038 else
8039 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04008040
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008041 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05008042
8043 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00008044 if (!path)
8045 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05008046
Chris Masonb9473432009-03-13 11:00:37 -04008047 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008048 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8049 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008050 if (ret) {
8051 btrfs_free_path(path);
8052 return ret;
8053 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008054
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008055 leaf = path->nodes[0];
8056 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05008057 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008058 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
8059 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8060 btrfs_set_extent_flags(leaf, extent_item,
8061 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05008062
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008063 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
8064 btrfs_set_extent_inline_ref_type(leaf, iref, type);
8065 if (parent > 0) {
8066 struct btrfs_shared_data_ref *ref;
8067 ref = (struct btrfs_shared_data_ref *)(iref + 1);
8068 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8069 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
8070 } else {
8071 struct btrfs_extent_data_ref *ref;
8072 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
8073 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
8074 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
8075 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
8076 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
8077 }
Chris Mason47e4bb92008-02-01 14:51:59 -05008078
8079 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05008080 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04008081
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008082 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
8083 ins->offset);
8084 if (ret)
8085 return ret;
8086
Jeff Mahoney6202df62016-06-22 18:54:22 -04008087 ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008088 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008089 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008090 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05008091 BUG();
8092 }
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008093 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008094 return ret;
8095}
8096
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008097static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008098 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008099 u64 parent, u64 root_objectid,
8100 u64 flags, struct btrfs_disk_key *key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01008101 int level, struct btrfs_key *ins)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008102{
8103 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008104 struct btrfs_extent_item *extent_item;
8105 struct btrfs_tree_block_info *block_info;
8106 struct btrfs_extent_inline_ref *iref;
8107 struct btrfs_path *path;
8108 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05008109 u32 size = sizeof(*extent_item) + sizeof(*iref);
Josef Bacikfcebe452014-05-13 17:30:47 -07008110 u64 num_bytes = ins->offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008111 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Josef Bacik3173a182013-03-07 14:22:04 -05008112
8113 if (!skinny_metadata)
8114 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008115
8116 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04008117 if (!path) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008118 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008119 fs_info->nodesize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07008120 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04008121 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008122
8123 path->leave_spinning = 1;
8124 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
8125 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008126 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07008127 btrfs_free_path(path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008128 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008129 fs_info->nodesize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008130 return ret;
8131 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008132
8133 leaf = path->nodes[0];
8134 extent_item = btrfs_item_ptr(leaf, path->slots[0],
8135 struct btrfs_extent_item);
8136 btrfs_set_extent_refs(leaf, extent_item, 1);
8137 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
8138 btrfs_set_extent_flags(leaf, extent_item,
8139 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008140
Josef Bacik3173a182013-03-07 14:22:04 -05008141 if (skinny_metadata) {
8142 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008143 num_bytes = fs_info->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05008144 } else {
8145 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
8146 btrfs_set_tree_block_key(leaf, block_info, key);
8147 btrfs_set_tree_block_level(leaf, block_info, level);
8148 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
8149 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008150
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008151 if (parent > 0) {
8152 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
8153 btrfs_set_extent_inline_ref_type(leaf, iref,
8154 BTRFS_SHARED_BLOCK_REF_KEY);
8155 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
8156 } else {
8157 btrfs_set_extent_inline_ref_type(leaf, iref,
8158 BTRFS_TREE_BLOCK_REF_KEY);
8159 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
8160 }
8161
8162 btrfs_mark_buffer_dirty(leaf);
8163 btrfs_free_path(path);
8164
Omar Sandoval1e144fb2015-09-29 20:50:37 -07008165 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
8166 num_bytes);
8167 if (ret)
8168 return ret;
8169
Jeff Mahoney6202df62016-06-22 18:54:22 -04008170 ret = update_block_group(trans, fs_info, ins->objectid,
8171 fs_info->nodesize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008172 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008173 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02008174 ins->objectid, ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008175 BUG();
8176 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04008177
Jeff Mahoney71ff6432016-09-06 16:00:42 -04008178 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008179 fs_info->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008180 return ret;
8181}
8182
8183int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008184 u64 root_objectid, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08008185 u64 offset, u64 ram_bytes,
8186 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04008187{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008188 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008189 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04008190
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008191 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04008192
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008193 ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008194 ins->offset, 0,
8195 root_objectid, owner, offset,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08008196 ram_bytes, BTRFS_ADD_DELAYED_EXTENT,
8197 NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008198 return ret;
8199}
Chris Masone02119d2008-09-05 16:13:11 -04008200
8201/*
8202 * this is used by the tree logging recovery code. It records that
8203 * an extent has been allocated and makes sure to clear the free
8204 * space cache bits as well
8205 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008206int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008207 struct btrfs_fs_info *fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008208 u64 root_objectid, u64 owner, u64 offset,
8209 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04008210{
8211 int ret;
8212 struct btrfs_block_group_cache *block_group;
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008213 struct btrfs_space_info *space_info;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008214
8215 /*
8216 * Mixed block groups will exclude before processing the log so we only
Nicholas D Steeves01327612016-05-19 21:18:45 -04008217 * need to do the exclude dance if this fs isn't mixed.
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008218 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008219 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008220 ret = __exclude_logged_extent(fs_info, ins->objectid,
8221 ins->offset);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008222 if (ret)
8223 return ret;
8224 }
Chris Masone02119d2008-09-05 16:13:11 -04008225
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008226 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04008227 if (!block_group)
8228 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04008229
Wang Xiaoguanged7a6942016-08-26 11:33:14 +08008230 space_info = block_group->space_info;
8231 spin_lock(&space_info->lock);
8232 spin_lock(&block_group->lock);
8233 space_info->bytes_reserved += ins->offset;
8234 block_group->reserved += ins->offset;
8235 spin_unlock(&block_group->lock);
8236 spin_unlock(&space_info->lock);
8237
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008238 ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008239 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04008240 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04008241 return ret;
8242}
8243
Eric Sandeen48a3b632013-04-25 20:41:01 +00008244static struct extent_buffer *
8245btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
David Sterbafe864572014-06-15 02:28:42 +02008246 u64 bytenr, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04008247{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008248 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason65b51a02008-08-01 15:11:20 -04008249 struct extent_buffer *buf;
8250
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008251 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008252 if (IS_ERR(buf))
8253 return buf;
8254
Chris Mason65b51a02008-08-01 15:11:20 -04008255 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04008256 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04008257 btrfs_tree_lock(buf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008258 clean_tree_block(trans, fs_info, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05008259 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008260
8261 btrfs_set_lock_blocking(buf);
David Sterba4db8c522015-12-03 13:06:46 +01008262 set_extent_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05008263
Chris Masond0c803c2008-09-11 16:17:57 -04008264 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01008265 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008266 /*
8267 * we allow two log transactions at a time, use different
8268 * EXENT bit to differentiate dirty pages.
8269 */
Filipe Manana656f30d2014-09-26 12:25:56 +01008270 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00008271 set_extent_dirty(&root->dirty_log_pages, buf->start,
8272 buf->start + buf->len - 1, GFP_NOFS);
8273 else
8274 set_extent_new(&root->dirty_log_pages, buf->start,
David Sterba3744dbe2016-04-26 23:54:39 +02008275 buf->start + buf->len - 1);
Chris Masond0c803c2008-09-11 16:17:57 -04008276 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01008277 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04008278 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
8279 buf->start + buf->len - 1, GFP_NOFS);
8280 }
Jeff Mahoney64c12922016-06-08 00:36:38 -04008281 trans->dirty = true;
Chris Masonb4ce94d2009-02-04 09:25:08 -05008282 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04008283 return buf;
8284}
8285
Yan, Zhengf0486c62010-05-16 10:46:25 -04008286static struct btrfs_block_rsv *
8287use_block_rsv(struct btrfs_trans_handle *trans,
8288 struct btrfs_root *root, u32 blocksize)
8289{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008290 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008291 struct btrfs_block_rsv *block_rsv;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008292 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008293 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00008294 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008295
8296 block_rsv = get_block_rsv(trans, root);
8297
Miao Xieb586b322013-05-13 13:55:10 +00008298 if (unlikely(block_rsv->size == 0))
8299 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00008300again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04008301 ret = block_rsv_use_bytes(block_rsv, blocksize);
8302 if (!ret)
8303 return block_rsv;
8304
Miao Xieb586b322013-05-13 13:55:10 +00008305 if (block_rsv->failfast)
8306 return ERR_PTR(ret);
8307
Miao Xied88033d2013-05-13 13:55:12 +00008308 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
8309 global_updated = true;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008310 update_global_block_rsv(fs_info);
Miao Xied88033d2013-05-13 13:55:12 +00008311 goto again;
8312 }
8313
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008314 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Miao Xieb586b322013-05-13 13:55:10 +00008315 static DEFINE_RATELIMIT_STATE(_rs,
8316 DEFAULT_RATELIMIT_INTERVAL * 10,
8317 /*DEFAULT_RATELIMIT_BURST*/ 1);
8318 if (__ratelimit(&_rs))
8319 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05008320 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00008321 }
8322try_reserve:
8323 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
8324 BTRFS_RESERVE_NO_FLUSH);
8325 if (!ret)
8326 return block_rsv;
8327 /*
8328 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00008329 * the global reserve if its space type is the same as the global
8330 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00008331 */
Miao Xie5881cfc2013-05-13 13:55:11 +00008332 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
8333 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00008334 ret = block_rsv_use_bytes(global_rsv, blocksize);
8335 if (!ret)
8336 return global_rsv;
8337 }
8338 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008339}
8340
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008341static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
8342 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04008343{
8344 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05008345 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008346}
8347
Chris Masonfec577f2007-02-26 10:40:21 -05008348/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04008349 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08008350 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05008351 */
David Sterba4d75f8a2014-06-15 01:54:12 +02008352struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
8353 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008354 u64 parent, u64 root_objectid,
8355 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02008356 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05008357{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008358 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone2fa7222007-03-12 16:22:34 -04008359 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008360 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04008361 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008362 struct btrfs_delayed_extent_op *extent_op;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008363 u64 flags = 0;
8364 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008365 u32 blocksize = fs_info->nodesize;
8366 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008367
David Sterba05653ef2016-07-15 15:23:37 +02008368#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008369 if (btrfs_is_testing(fs_info)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008370 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
David Sterbafe864572014-06-15 02:28:42 +02008371 level);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04008372 if (!IS_ERR(buf))
8373 root->alloc_bytenr += blocksize;
8374 return buf;
8375 }
David Sterba05653ef2016-07-15 15:23:37 +02008376#endif
David Sterbafccb84c2014-09-29 23:53:21 +02008377
Yan, Zhengf0486c62010-05-16 10:46:25 -04008378 block_rsv = use_block_rsv(trans, root, blocksize);
8379 if (IS_ERR(block_rsv))
8380 return ERR_CAST(block_rsv);
8381
Wang Xiaoguang18513092016-07-25 15:51:40 +08008382 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08008383 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008384 if (ret)
8385 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05008386
David Sterbafe864572014-06-15 02:28:42 +02008387 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
Omar Sandoval67b78592015-02-24 02:47:04 -08008388 if (IS_ERR(buf)) {
8389 ret = PTR_ERR(buf);
8390 goto out_free_reserved;
8391 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008392
8393 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
8394 if (parent == 0)
8395 parent = ins.objectid;
8396 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8397 } else
8398 BUG_ON(parent > 0);
8399
8400 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00008401 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08008402 if (!extent_op) {
8403 ret = -ENOMEM;
8404 goto out_free_buf;
8405 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008406 if (key)
8407 memcpy(&extent_op->key, key, sizeof(extent_op->key));
8408 else
8409 memset(&extent_op->key, 0, sizeof(extent_op->key));
8410 extent_op->flags_to_set = flags;
David Sterba35b3ad52015-11-30 16:51:29 +01008411 extent_op->update_key = skinny_metadata ? false : true;
8412 extent_op->update_flags = true;
8413 extent_op->is_data = false;
Josef Bacikb1c79e02013-05-09 13:49:30 -04008414 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008415
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008416 ret = btrfs_add_delayed_tree_ref(fs_info, trans,
Omar Sandoval67b78592015-02-24 02:47:04 -08008417 ins.objectid, ins.offset,
8418 parent, root_objectid, level,
8419 BTRFS_ADD_DELAYED_EXTENT,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01008420 extent_op);
Omar Sandoval67b78592015-02-24 02:47:04 -08008421 if (ret)
8422 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008423 }
Chris Masonfec577f2007-02-26 10:40:21 -05008424 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008425
8426out_free_delayed:
8427 btrfs_free_delayed_extent_op(extent_op);
8428out_free_buf:
8429 free_extent_buffer(buf);
8430out_free_reserved:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008431 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08008432out_unuse:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008433 unuse_block_rsv(fs_info, block_rsv, blocksize);
Omar Sandoval67b78592015-02-24 02:47:04 -08008434 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05008435}
Chris Masona28ec192007-03-06 20:08:01 -05008436
Yan Zheng2c47e6052009-06-27 21:07:35 -04008437struct walk_control {
8438 u64 refs[BTRFS_MAX_LEVEL];
8439 u64 flags[BTRFS_MAX_LEVEL];
8440 struct btrfs_key update_progress;
8441 int stage;
8442 int level;
8443 int shared_level;
8444 int update_ref;
8445 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008446 int reada_slot;
8447 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008448 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008449};
8450
8451#define DROP_REFERENCE 1
8452#define UPDATE_BACKREF 2
8453
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008454static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8455 struct btrfs_root *root,
8456 struct walk_control *wc,
8457 struct btrfs_path *path)
8458{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008459 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008460 u64 bytenr;
8461 u64 generation;
8462 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008463 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008464 u32 nritems;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008465 struct btrfs_key key;
8466 struct extent_buffer *eb;
8467 int ret;
8468 int slot;
8469 int nread = 0;
8470
8471 if (path->slots[wc->level] < wc->reada_slot) {
8472 wc->reada_count = wc->reada_count * 2 / 3;
8473 wc->reada_count = max(wc->reada_count, 2);
8474 } else {
8475 wc->reada_count = wc->reada_count * 3 / 2;
8476 wc->reada_count = min_t(int, wc->reada_count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008477 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008478 }
8479
8480 eb = path->nodes[wc->level];
8481 nritems = btrfs_header_nritems(eb);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008482
8483 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
8484 if (nread >= wc->reada_count)
8485 break;
8486
8487 cond_resched();
8488 bytenr = btrfs_node_blockptr(eb, slot);
8489 generation = btrfs_node_ptr_generation(eb, slot);
8490
8491 if (slot == path->slots[wc->level])
8492 goto reada;
8493
8494 if (wc->stage == UPDATE_BACKREF &&
8495 generation <= root->root_key.offset)
8496 continue;
8497
Yan, Zheng94fcca92009-10-09 09:25:16 -04008498 /* We don't lock the tree block, it's OK to be racy here */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008499 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -05008500 wc->level - 1, 1, &refs,
8501 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008502 /* We don't care about errors in readahead. */
8503 if (ret < 0)
8504 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008505 BUG_ON(refs == 0);
8506
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008507 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008508 if (refs == 1)
8509 goto reada;
8510
Yan, Zheng94fcca92009-10-09 09:25:16 -04008511 if (wc->level == 1 &&
8512 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8513 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008514 if (!wc->update_ref ||
8515 generation <= root->root_key.offset)
8516 continue;
8517 btrfs_node_key_to_cpu(eb, &key, slot);
8518 ret = btrfs_comp_cpu_keys(&key,
8519 &wc->update_progress);
8520 if (ret < 0)
8521 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008522 } else {
8523 if (wc->level == 1 &&
8524 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8525 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008526 }
8527reada:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008528 readahead_tree_block(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008529 nread++;
8530 }
8531 wc->reada_slot = slot;
8532}
8533
Chris Mason9aca1d52007-03-13 11:09:37 -04008534/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008535 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008536 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04008537 * when wc->stage == UPDATE_BACKREF, this function updates
8538 * back refs for pointers in the block.
8539 *
8540 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04008541 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008542static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8543 struct btrfs_root *root,
8544 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008545 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04008546{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008547 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008548 int level = wc->level;
8549 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04008550 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8551 int ret;
8552
8553 if (wc->stage == UPDATE_BACKREF &&
8554 btrfs_header_owner(eb) != root->root_key.objectid)
8555 return 1;
8556
8557 /*
8558 * when reference count of tree block is 1, it won't increase
8559 * again. once full backref flag is set, we never clear it.
8560 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04008561 if (lookup_info &&
8562 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
8563 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008564 BUG_ON(!path->locks[level]);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008565 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008566 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008567 &wc->refs[level],
8568 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008569 BUG_ON(ret == -ENOMEM);
8570 if (ret)
8571 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008572 BUG_ON(wc->refs[level] == 0);
8573 }
8574
Yan Zheng2c47e6052009-06-27 21:07:35 -04008575 if (wc->stage == DROP_REFERENCE) {
8576 if (wc->refs[level] > 1)
8577 return 1;
8578
8579 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04008580 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008581 path->locks[level] = 0;
8582 }
8583 return 0;
8584 }
8585
8586 /* wc->stage == UPDATE_BACKREF */
8587 if (!(wc->flags[level] & flag)) {
8588 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07008589 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008590 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07008591 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008592 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008593 ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04008594 eb->len, flag,
8595 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008596 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008597 wc->flags[level] |= flag;
8598 }
8599
8600 /*
8601 * the block is shared by multiple trees, so it's not good to
8602 * keep the tree lock
8603 */
8604 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04008605 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008606 path->locks[level] = 0;
8607 }
8608 return 0;
8609}
8610
8611/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008612 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008613 *
8614 * when wc->stage == DROP_REFERENCE, this function checks
8615 * reference count of the block pointed to. if the block
8616 * is shared and we need update back refs for the subtree
8617 * rooted at the block, this function changes wc->stage to
8618 * UPDATE_BACKREF. if the block is shared and there is no
8619 * need to update back, this function drops the reference
8620 * to the block.
8621 *
8622 * NOTE: return value 1 means we should stop walking down.
8623 */
8624static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8625 struct btrfs_root *root,
8626 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008627 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008628{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008629 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008630 u64 bytenr;
8631 u64 generation;
8632 u64 parent;
8633 u32 blocksize;
8634 struct btrfs_key key;
8635 struct extent_buffer *next;
8636 int level = wc->level;
8637 int reada = 0;
8638 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07008639 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008640
8641 generation = btrfs_node_ptr_generation(path->nodes[level],
8642 path->slots[level]);
8643 /*
8644 * if the lower level block was created before the snapshot
8645 * was created, we know there is no need to update back refs
8646 * for the subtree
8647 */
8648 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04008649 generation <= root->root_key.offset) {
8650 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008651 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008652 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008653
8654 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008655 blocksize = fs_info->nodesize;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008656
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008657 next = find_extent_buffer(fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008658 if (!next) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008659 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07008660 if (IS_ERR(next))
8661 return PTR_ERR(next);
8662
Josef Bacikb2aaaa32013-07-05 17:05:38 -04008663 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
8664 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008665 reada = 1;
8666 }
8667 btrfs_tree_lock(next);
8668 btrfs_set_lock_blocking(next);
8669
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008670 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008671 &wc->refs[level - 1],
8672 &wc->flags[level - 1]);
Josef Bacik48672682016-09-23 13:23:28 +02008673 if (ret < 0)
8674 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008675
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008676 if (unlikely(wc->refs[level - 1] == 0)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008677 btrfs_err(fs_info, "Missing references.");
Josef Bacik48672682016-09-23 13:23:28 +02008678 ret = -EIO;
8679 goto out_unlock;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008680 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008681 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008682
Yan, Zheng94fcca92009-10-09 09:25:16 -04008683 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008684 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07008685 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008686 if (level == 1 &&
8687 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8688 goto skip;
8689
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008690 if (!wc->update_ref ||
8691 generation <= root->root_key.offset)
8692 goto skip;
8693
8694 btrfs_node_key_to_cpu(path->nodes[level], &key,
8695 path->slots[level]);
8696 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
8697 if (ret < 0)
8698 goto skip;
8699
8700 wc->stage = UPDATE_BACKREF;
8701 wc->shared_level = level - 1;
8702 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008703 } else {
8704 if (level == 1 &&
8705 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8706 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008707 }
8708
Chris Masonb9fab912012-05-06 07:23:47 -04008709 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008710 btrfs_tree_unlock(next);
8711 free_extent_buffer(next);
8712 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008713 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008714 }
8715
8716 if (!next) {
8717 if (reada && level == 1)
8718 reada_walk_down(trans, root, wc, path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008719 next = read_tree_block(fs_info, bytenr, generation);
Liu Bo64c043d2015-05-25 17:30:15 +08008720 if (IS_ERR(next)) {
8721 return PTR_ERR(next);
8722 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04008723 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008724 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04008725 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008726 btrfs_tree_lock(next);
8727 btrfs_set_lock_blocking(next);
8728 }
8729
8730 level--;
Josef Bacik48672682016-09-23 13:23:28 +02008731 ASSERT(level == btrfs_header_level(next));
8732 if (level != btrfs_header_level(next)) {
8733 btrfs_err(root->fs_info, "mismatched level");
8734 ret = -EIO;
8735 goto out_unlock;
8736 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008737 path->nodes[level] = next;
8738 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008739 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008740 wc->level = level;
8741 if (wc->level == 1)
8742 wc->reada_slot = 0;
8743 return 0;
8744skip:
8745 wc->refs[level - 1] = 0;
8746 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008747 if (wc->stage == DROP_REFERENCE) {
8748 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
8749 parent = path->nodes[level]->start;
8750 } else {
Josef Bacik48672682016-09-23 13:23:28 +02008751 ASSERT(root->root_key.objectid ==
Yan, Zheng94fcca92009-10-09 09:25:16 -04008752 btrfs_header_owner(path->nodes[level]));
Josef Bacik48672682016-09-23 13:23:28 +02008753 if (root->root_key.objectid !=
8754 btrfs_header_owner(path->nodes[level])) {
8755 btrfs_err(root->fs_info,
8756 "mismatched block owner");
8757 ret = -EIO;
8758 goto out_unlock;
8759 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008760 parent = 0;
8761 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008762
Mark Fasheh11526512014-07-17 12:39:01 -07008763 if (need_account) {
Qu Wenruo33d1f052016-10-18 09:31:28 +08008764 ret = btrfs_qgroup_trace_subtree(trans, root, next,
8765 generation, level - 1);
Mark Fasheh11526512014-07-17 12:39:01 -07008766 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008767 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008768 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
8769 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008770 }
8771 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008772 ret = btrfs_free_extent(trans, fs_info, bytenr, blocksize,
8773 parent, root->root_key.objectid,
8774 level - 1, 0);
Josef Bacik48672682016-09-23 13:23:28 +02008775 if (ret)
8776 goto out_unlock;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008777 }
Josef Bacik48672682016-09-23 13:23:28 +02008778
8779 *lookup_info = 1;
8780 ret = 1;
8781
8782out_unlock:
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008783 btrfs_tree_unlock(next);
8784 free_extent_buffer(next);
Josef Bacik48672682016-09-23 13:23:28 +02008785
8786 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008787}
8788
8789/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008790 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008791 *
8792 * when wc->stage == DROP_REFERENCE, this function drops
8793 * reference count on the block.
8794 *
8795 * when wc->stage == UPDATE_BACKREF, this function changes
8796 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8797 * to UPDATE_BACKREF previously while processing the block.
8798 *
8799 * NOTE: return value 1 means we should stop walking up.
8800 */
8801static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8802 struct btrfs_root *root,
8803 struct btrfs_path *path,
8804 struct walk_control *wc)
8805{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008806 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008807 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008808 int level = wc->level;
8809 struct extent_buffer *eb = path->nodes[level];
8810 u64 parent = 0;
8811
8812 if (wc->stage == UPDATE_BACKREF) {
8813 BUG_ON(wc->shared_level < level);
8814 if (level < wc->shared_level)
8815 goto out;
8816
Yan Zheng2c47e6052009-06-27 21:07:35 -04008817 ret = find_next_key(path, level + 1, &wc->update_progress);
8818 if (ret > 0)
8819 wc->update_ref = 0;
8820
8821 wc->stage = DROP_REFERENCE;
8822 wc->shared_level = -1;
8823 path->slots[level] = 0;
8824
8825 /*
8826 * check reference count again if the block isn't locked.
8827 * we should start walking down the tree again if reference
8828 * count is one.
8829 */
8830 if (!path->locks[level]) {
8831 BUG_ON(level == 0);
8832 btrfs_tree_lock(eb);
8833 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008834 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008835
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008836 ret = btrfs_lookup_extent_info(trans, fs_info,
Josef Bacik3173a182013-03-07 14:22:04 -05008837 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008838 &wc->refs[level],
8839 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008840 if (ret < 0) {
8841 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008842 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008843 return ret;
8844 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008845 BUG_ON(wc->refs[level] == 0);
8846 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04008847 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008848 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008849 return 1;
8850 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008851 }
8852 }
8853
8854 /* wc->stage == DROP_REFERENCE */
8855 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
8856
8857 if (wc->refs[level] == 1) {
8858 if (level == 0) {
8859 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07008860 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008861 else
Josef Bacike339a6b2014-07-02 10:54:25 -07008862 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008863 BUG_ON(ret); /* -ENOMEM */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008864 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb);
Mark Fasheh11526512014-07-17 12:39:01 -07008865 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008866 btrfs_err_rl(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008867 "error %d accounting leaf items. Quota is out of sync, rescan required.",
8868 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008869 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008870 }
8871 /* make block locked assertion in clean_tree_block happy */
8872 if (!path->locks[level] &&
8873 btrfs_header_generation(eb) == trans->transid) {
8874 btrfs_tree_lock(eb);
8875 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008876 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008877 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008878 clean_tree_block(trans, fs_info, eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008879 }
8880
8881 if (eb == root->node) {
8882 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8883 parent = eb->start;
8884 else
8885 BUG_ON(root->root_key.objectid !=
8886 btrfs_header_owner(eb));
8887 } else {
8888 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8889 parent = path->nodes[level + 1]->start;
8890 else
8891 BUG_ON(root->root_key.objectid !=
8892 btrfs_header_owner(path->nodes[level + 1]));
8893 }
8894
Jan Schmidt5581a512012-05-16 17:04:52 +02008895 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008896out:
8897 wc->refs[level] = 0;
8898 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008899 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008900}
8901
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008902static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
8903 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008904 struct btrfs_path *path,
8905 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008906{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008907 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008908 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008909 int ret;
8910
Yan Zheng2c47e6052009-06-27 21:07:35 -04008911 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04008912 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008913 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008914 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008915
Yan Zheng2c47e6052009-06-27 21:07:35 -04008916 if (level == 0)
8917 break;
8918
Yan, Zheng7a7965f2010-02-01 02:41:17 +00008919 if (path->slots[level] >=
8920 btrfs_header_nritems(path->nodes[level]))
8921 break;
8922
Yan, Zheng94fcca92009-10-09 09:25:16 -04008923 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008924 if (ret > 0) {
8925 path->slots[level]++;
8926 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00008927 } else if (ret < 0)
8928 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008929 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008930 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04008931 return 0;
8932}
8933
Chris Masond3977122009-01-05 21:25:51 -05008934static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05008935 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04008936 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008937 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05008938{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008939 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05008940 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04008941
Yan Zheng2c47e6052009-06-27 21:07:35 -04008942 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
8943 while (level < max_level && path->nodes[level]) {
8944 wc->level = level;
8945 if (path->slots[level] + 1 <
8946 btrfs_header_nritems(path->nodes[level])) {
8947 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05008948 return 0;
8949 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008950 ret = walk_up_proc(trans, root, path, wc);
8951 if (ret > 0)
8952 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05008953
Yan Zheng2c47e6052009-06-27 21:07:35 -04008954 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04008955 btrfs_tree_unlock_rw(path->nodes[level],
8956 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008957 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008958 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008959 free_extent_buffer(path->nodes[level]);
8960 path->nodes[level] = NULL;
8961 level++;
Chris Mason20524f02007-03-10 06:35:47 -05008962 }
8963 }
8964 return 1;
8965}
8966
Chris Mason9aca1d52007-03-13 11:09:37 -04008967/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04008968 * drop a subvolume tree.
8969 *
8970 * this function traverses the tree freeing any blocks that only
8971 * referenced by the tree.
8972 *
8973 * when a shared tree block is found. this function decreases its
8974 * reference count by one. if update_ref is true, this function
8975 * also make sure backrefs for the shared block and all lower level
8976 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00008977 *
8978 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04008979 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04008980int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008981 struct btrfs_block_rsv *block_rsv, int update_ref,
8982 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05008983{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008984 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04008985 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008986 struct btrfs_trans_handle *trans;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008987 struct btrfs_root *tree_root = fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04008988 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008989 struct walk_control *wc;
8990 struct btrfs_key key;
8991 int err = 0;
8992 int ret;
8993 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04008994 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05008995
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008996 btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid);
Mark Fasheh11526512014-07-17 12:39:01 -07008997
Chris Mason5caf2a02007-04-02 11:20:42 -04008998 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00008999 if (!path) {
9000 err = -ENOMEM;
9001 goto out;
9002 }
Chris Mason20524f02007-03-10 06:35:47 -05009003
Yan Zheng2c47e6052009-06-27 21:07:35 -04009004 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07009005 if (!wc) {
9006 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009007 err = -ENOMEM;
9008 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07009009 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009010
Yan, Zhenga22285a2010-05-16 10:48:46 -04009011 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009012 if (IS_ERR(trans)) {
9013 err = PTR_ERR(trans);
9014 goto out_free;
9015 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00009016
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009017 if (block_rsv)
9018 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009019
Chris Mason9f3a7422007-08-07 15:52:19 -04009020 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009021 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009022 path->nodes[level] = btrfs_lock_root_node(root);
9023 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04009024 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009025 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009026 memset(&wc->update_progress, 0,
9027 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04009028 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04009029 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009030 memcpy(&wc->update_progress, &key,
9031 sizeof(wc->update_progress));
9032
Chris Mason6702ed42007-08-07 16:15:09 -04009033 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009034 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04009035 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009036 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
9037 path->lowest_level = 0;
9038 if (ret < 0) {
9039 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009040 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04009041 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009042 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009043
Chris Mason7d9eb122008-07-08 14:19:17 -04009044 /*
9045 * unlock our path, this is safe because only this
9046 * function is allowed to delete this snapshot
9047 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009048 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04009049
Yan Zheng2c47e6052009-06-27 21:07:35 -04009050 level = btrfs_header_level(root->node);
9051 while (1) {
9052 btrfs_tree_lock(path->nodes[level]);
9053 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009054 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009055
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009056 ret = btrfs_lookup_extent_info(trans, fs_info,
Yan Zheng2c47e6052009-06-27 21:07:35 -04009057 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05009058 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04009059 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009060 if (ret < 0) {
9061 err = ret;
9062 goto out_end_trans;
9063 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009064 BUG_ON(wc->refs[level] == 0);
9065
9066 if (level == root_item->drop_level)
9067 break;
9068
9069 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04009070 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009071 WARN_ON(wc->refs[level] != 1);
9072 level--;
9073 }
9074 }
9075
9076 wc->level = level;
9077 wc->shared_level = -1;
9078 wc->stage = DROP_REFERENCE;
9079 wc->update_ref = update_ref;
9080 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009081 wc->for_reloc = for_reloc;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009082 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04009083
9084 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00009085
Yan Zheng2c47e6052009-06-27 21:07:35 -04009086 ret = walk_down_tree(trans, root, path, wc);
9087 if (ret < 0) {
9088 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04009089 break;
9090 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009091
9092 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
9093 if (ret < 0) {
9094 err = ret;
9095 break;
9096 }
9097
9098 if (ret > 0) {
9099 BUG_ON(wc->stage != DROP_REFERENCE);
9100 break;
9101 }
9102
9103 if (wc->stage == DROP_REFERENCE) {
9104 level = wc->level;
9105 btrfs_node_key(path->nodes[level],
9106 &root_item->drop_progress,
9107 path->slots[level]);
9108 root_item->drop_level = level;
9109 }
9110
9111 BUG_ON(wc->level == 0);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009112 if (btrfs_should_end_transaction(trans) ||
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009113 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009114 ret = btrfs_update_root(trans, tree_root,
9115 &root->root_key,
9116 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009117 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009118 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009119 err = ret;
9120 goto out_end_trans;
9121 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009122
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009123 btrfs_end_transaction_throttle(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009124 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009125 btrfs_debug(fs_info,
9126 "drop snapshot early exit");
Josef Bacik3c8f2422013-07-15 11:57:06 -04009127 err = -EAGAIN;
9128 goto out_free;
9129 }
9130
Yan, Zhenga22285a2010-05-16 10:48:46 -04009131 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009132 if (IS_ERR(trans)) {
9133 err = PTR_ERR(trans);
9134 goto out_free;
9135 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009136 if (block_rsv)
9137 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04009138 }
Chris Mason20524f02007-03-10 06:35:47 -05009139 }
David Sterbab3b4aa72011-04-21 01:20:15 +02009140 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009141 if (err)
9142 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009143
9144 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009145 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009146 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009147 goto out_end_trans;
9148 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009149
Yan, Zheng76dda932009-09-21 16:00:26 -04009150 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00009151 ret = btrfs_find_root(tree_root, &root->root_key, path,
9152 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009153 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009154 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009155 err = ret;
9156 goto out_end_trans;
9157 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05009158 /* if we fail to delete the orphan item this time
9159 * around, it'll get picked up the next time.
9160 *
9161 * The most common failure here is just -ENOENT.
9162 */
9163 btrfs_del_orphan_item(trans, tree_root,
9164 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04009165 }
9166 }
9167
Miao Xie27cdeb72014-04-02 19:51:05 +08009168 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04009169 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009170 } else {
9171 free_extent_buffer(root->node);
9172 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00009173 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009174 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04009175 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009176out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009177 btrfs_end_transaction_throttle(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009178out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04009179 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04009180 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009181out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04009182 /*
9183 * So if we need to stop dropping the snapshot for whatever reason we
9184 * need to make sure to add it back to the dead root list so that we
9185 * keep trying to do the work later. This also cleans up roots if we
9186 * don't have it in the radix (like when we recover after a power fail
9187 * or unmount) so we don't leak memory.
9188 */
Josef Bacikb37b39c2013-07-23 16:57:15 -04009189 if (!for_reloc && root_dropped == false)
Josef Bacikd29a9f62013-07-17 19:30:20 -04009190 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08009191 if (err && err != -EAGAIN)
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009192 btrfs_handle_fs_error(fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04009193 return err;
Chris Mason20524f02007-03-10 06:35:47 -05009194}
Chris Mason9078a3e2007-04-26 16:46:15 -04009195
Yan Zheng2c47e6052009-06-27 21:07:35 -04009196/*
9197 * drop subtree rooted at tree block 'node'.
9198 *
9199 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009200 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04009201 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04009202int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
9203 struct btrfs_root *root,
9204 struct extent_buffer *node,
9205 struct extent_buffer *parent)
9206{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009207 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009208 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009209 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009210 int level;
9211 int parent_level;
9212 int ret = 0;
9213 int wret;
9214
Yan Zheng2c47e6052009-06-27 21:07:35 -04009215 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
9216
Yan Zhengf82d02d2008-10-29 14:49:05 -04009217 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009218 if (!path)
9219 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009220
Yan Zheng2c47e6052009-06-27 21:07:35 -04009221 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009222 if (!wc) {
9223 btrfs_free_path(path);
9224 return -ENOMEM;
9225 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009226
Chris Masonb9447ef2009-03-09 11:45:38 -04009227 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009228 parent_level = btrfs_header_level(parent);
9229 extent_buffer_get(parent);
9230 path->nodes[parent_level] = parent;
9231 path->slots[parent_level] = btrfs_header_nritems(parent);
9232
Chris Masonb9447ef2009-03-09 11:45:38 -04009233 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009234 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009235 path->nodes[level] = node;
9236 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009237 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009238
9239 wc->refs[parent_level] = 1;
9240 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
9241 wc->level = level;
9242 wc->shared_level = -1;
9243 wc->stage = DROP_REFERENCE;
9244 wc->update_ref = 0;
9245 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009246 wc->for_reloc = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009247 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009248
9249 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009250 wret = walk_down_tree(trans, root, path, wc);
9251 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04009252 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009253 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009254 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009255
Yan Zheng2c47e6052009-06-27 21:07:35 -04009256 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009257 if (wret < 0)
9258 ret = wret;
9259 if (wret != 0)
9260 break;
9261 }
9262
Yan Zheng2c47e6052009-06-27 21:07:35 -04009263 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009264 btrfs_free_path(path);
9265 return ret;
9266}
9267
Jeff Mahoney6202df62016-06-22 18:54:22 -04009268static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04009269{
9270 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009271 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04009272
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009273 /*
9274 * if restripe for this chunk_type is on pick target profile and
9275 * return, otherwise do the usual balance
9276 */
Jeff Mahoney6202df62016-06-22 18:54:22 -04009277 stripped = get_restripe_target(fs_info, flags);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009278 if (stripped)
9279 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02009280
Jeff Mahoney6202df62016-06-22 18:54:22 -04009281 num_devices = fs_info->fs_devices->rw_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05009282
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009283 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05009284 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009285 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
9286
Chris Masonec44a352008-04-28 15:29:52 -04009287 if (num_devices == 1) {
9288 stripped |= BTRFS_BLOCK_GROUP_DUP;
9289 stripped = flags & ~stripped;
9290
9291 /* turn raid0 into single device chunks */
9292 if (flags & BTRFS_BLOCK_GROUP_RAID0)
9293 return stripped;
9294
9295 /* turn mirroring into duplication */
9296 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
9297 BTRFS_BLOCK_GROUP_RAID10))
9298 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04009299 } else {
9300 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04009301 if (flags & stripped)
9302 return flags;
9303
9304 stripped |= BTRFS_BLOCK_GROUP_DUP;
9305 stripped = flags & ~stripped;
9306
9307 /* switch duplicated blocks with raid1 */
9308 if (flags & BTRFS_BLOCK_GROUP_DUP)
9309 return stripped | BTRFS_BLOCK_GROUP_RAID1;
9310
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009311 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04009312 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009313
Chris Masonec44a352008-04-28 15:29:52 -04009314 return flags;
9315}
9316
Zhaolei868f4012015-08-05 16:43:27 +08009317static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04009318{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009319 struct btrfs_space_info *sinfo = cache->space_info;
9320 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00009321 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009322 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04009323
Miao Xie199c36e2011-07-15 10:34:36 +00009324 /*
9325 * We need some metadata space and system metadata space for
9326 * allocating chunks in some corner cases until we force to set
9327 * it to be readonly.
9328 */
9329 if ((sinfo->flags &
9330 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
9331 !force)
Byongho Leeee221842015-12-15 01:42:10 +09009332 min_allocable_bytes = SZ_1M;
Miao Xie199c36e2011-07-15 10:34:36 +00009333 else
9334 min_allocable_bytes = 0;
9335
Yan, Zhengf0486c62010-05-16 10:46:25 -04009336 spin_lock(&sinfo->lock);
9337 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00009338
9339 if (cache->ro) {
Zhaolei868f4012015-08-05 16:43:27 +08009340 cache->ro++;
WuBo61cfea92011-07-26 03:30:11 +00009341 ret = 0;
9342 goto out;
9343 }
9344
Yan, Zhengf0486c62010-05-16 10:46:25 -04009345 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
9346 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04009347
Yan, Zhengf0486c62010-05-16 10:46:25 -04009348 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04009349 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
9350 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04009351 sinfo->bytes_readonly += num_bytes;
Zhaolei868f4012015-08-05 16:43:27 +08009352 cache->ro++;
Josef Bacik633c0aa2014-10-31 09:49:34 -04009353 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009354 ret = 0;
9355 }
WuBo61cfea92011-07-26 03:30:11 +00009356out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04009357 spin_unlock(&cache->lock);
9358 spin_unlock(&sinfo->lock);
9359 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04009360}
9361
Zhaolei868f4012015-08-05 16:43:27 +08009362int btrfs_inc_block_group_ro(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04009363 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009364
9365{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009366 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009367 struct btrfs_trans_handle *trans;
9368 u64 alloc_flags;
9369 int ret;
9370
Chris Mason1bbc6212015-04-06 12:46:08 -07009371again:
Josef Bacik7a7eaa42011-04-13 12:54:33 -04009372 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009373 if (IS_ERR(trans))
9374 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009375
Chris Mason1bbc6212015-04-06 12:46:08 -07009376 /*
9377 * we're not allowed to set block groups readonly after the dirty
9378 * block groups cache has started writing. If it already started,
9379 * back off and let this transaction commit
9380 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009381 mutex_lock(&fs_info->ro_block_group_mutex);
Josef Bacik3204d332015-09-24 10:46:10 -04009382 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
Chris Mason1bbc6212015-04-06 12:46:08 -07009383 u64 transid = trans->transid;
9384
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009385 mutex_unlock(&fs_info->ro_block_group_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009386 btrfs_end_transaction(trans);
Chris Mason1bbc6212015-04-06 12:46:08 -07009387
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009388 ret = btrfs_wait_for_commit(fs_info, transid);
Chris Mason1bbc6212015-04-06 12:46:08 -07009389 if (ret)
9390 return ret;
9391 goto again;
9392 }
9393
Chris Mason153c35b2015-05-19 18:54:41 -07009394 /*
9395 * if we are changing raid levels, try to allocate a corresponding
9396 * block group with the new raid level.
9397 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009398 alloc_flags = update_block_group_flags(fs_info, cache->flags);
Chris Mason153c35b2015-05-19 18:54:41 -07009399 if (alloc_flags != cache->flags) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009400 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason153c35b2015-05-19 18:54:41 -07009401 CHUNK_ALLOC_FORCE);
9402 /*
9403 * ENOSPC is allowed here, we may have enough space
9404 * already allocated at the new raid level to
9405 * carry on
9406 */
9407 if (ret == -ENOSPC)
9408 ret = 0;
9409 if (ret < 0)
9410 goto out;
9411 }
Chris Mason1bbc6212015-04-06 12:46:08 -07009412
Zhaolei868f4012015-08-05 16:43:27 +08009413 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009414 if (!ret)
9415 goto out;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009416 alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags);
9417 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04009418 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009419 if (ret < 0)
9420 goto out;
Zhaolei868f4012015-08-05 16:43:27 +08009421 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009422out:
Shaohua Li2f081082015-01-09 10:40:15 -08009423 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009424 alloc_flags = update_block_group_flags(fs_info, cache->flags);
David Sterba34441362016-10-04 19:34:27 +02009425 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009426 check_system_chunk(trans, fs_info, alloc_flags);
David Sterba34441362016-10-04 19:34:27 +02009427 mutex_unlock(&fs_info->chunk_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009428 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009429 mutex_unlock(&fs_info->ro_block_group_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009430
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009431 btrfs_end_transaction(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009432 return ret;
9433}
9434
Chris Masonc87f08c2011-02-16 13:57:04 -05009435int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009436 struct btrfs_fs_info *fs_info, u64 type)
Chris Masonc87f08c2011-02-16 13:57:04 -05009437{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009438 u64 alloc_flags = get_alloc_profile(fs_info, type);
9439
9440 return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05009441}
9442
Miao Xie6d07bce2011-01-05 10:07:31 +00009443/*
9444 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04009445 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00009446 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009447u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00009448{
9449 struct btrfs_block_group_cache *block_group;
9450 u64 free_bytes = 0;
9451 int factor;
9452
Nicholas D Steeves01327612016-05-19 21:18:45 -04009453 /* It's df, we don't care if it's racy */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009454 if (list_empty(&sinfo->ro_bgs))
9455 return 0;
9456
9457 spin_lock(&sinfo->lock);
9458 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00009459 spin_lock(&block_group->lock);
9460
9461 if (!block_group->ro) {
9462 spin_unlock(&block_group->lock);
9463 continue;
9464 }
9465
9466 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
9467 BTRFS_BLOCK_GROUP_RAID10 |
9468 BTRFS_BLOCK_GROUP_DUP))
9469 factor = 2;
9470 else
9471 factor = 1;
9472
9473 free_bytes += (block_group->key.offset -
9474 btrfs_block_group_used(&block_group->item)) *
9475 factor;
9476
9477 spin_unlock(&block_group->lock);
9478 }
Miao Xie6d07bce2011-01-05 10:07:31 +00009479 spin_unlock(&sinfo->lock);
9480
9481 return free_bytes;
9482}
9483
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009484void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache)
Yan, Zhengf0486c62010-05-16 10:46:25 -04009485{
9486 struct btrfs_space_info *sinfo = cache->space_info;
9487 u64 num_bytes;
9488
9489 BUG_ON(!cache->ro);
9490
9491 spin_lock(&sinfo->lock);
9492 spin_lock(&cache->lock);
Zhaolei868f4012015-08-05 16:43:27 +08009493 if (!--cache->ro) {
9494 num_bytes = cache->key.offset - cache->reserved -
9495 cache->pinned - cache->bytes_super -
9496 btrfs_block_group_used(&cache->item);
9497 sinfo->bytes_readonly -= num_bytes;
9498 list_del_init(&cache->ro_list);
9499 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009500 spin_unlock(&cache->lock);
9501 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009502}
9503
Josef Bacikba1bf482009-09-11 16:11:19 -04009504/*
9505 * checks to see if its even possible to relocate this block group.
9506 *
9507 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9508 * ok to go ahead and try.
9509 */
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009510int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04009511{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009512 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04009513 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04009514 struct btrfs_space_info *space_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009515 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacikba1bf482009-09-11 16:11:19 -04009516 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04009517 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00009518 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04009519 u64 dev_min = 1;
9520 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009521 u64 target;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009522 int debug;
liubocdcb7252011-08-03 10:15:25 +00009523 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04009524 int full = 0;
9525 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05009526
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009527 debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG);
Qu Wenruo0305bc22016-03-23 11:38:17 +08009528
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009529 block_group = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04009530
Josef Bacikba1bf482009-09-11 16:11:19 -04009531 /* odd, couldn't find the block group, leave it alone */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009532 if (!block_group) {
9533 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009534 btrfs_warn(fs_info,
Qu Wenruo0305bc22016-03-23 11:38:17 +08009535 "can't find block group for bytenr %llu",
9536 bytenr);
Josef Bacikba1bf482009-09-11 16:11:19 -04009537 return -1;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009538 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009539
liubocdcb7252011-08-03 10:15:25 +00009540 min_free = btrfs_block_group_used(&block_group->item);
9541
Josef Bacikba1bf482009-09-11 16:11:19 -04009542 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00009543 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04009544 goto out;
Chris Mason323da792008-05-09 11:46:48 -04009545
Josef Bacikba1bf482009-09-11 16:11:19 -04009546 space_info = block_group->space_info;
9547 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04009548
Josef Bacikba1bf482009-09-11 16:11:19 -04009549 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04009550
Josef Bacikba1bf482009-09-11 16:11:19 -04009551 /*
9552 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04009553 * relocate it unless we're able to allocate a new chunk below.
9554 *
9555 * Otherwise, we need to make sure we have room in the space to handle
9556 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04009557 */
Chris Mason7ce618d2009-09-22 14:48:44 -04009558 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00009559 (space_info->bytes_used + space_info->bytes_reserved +
9560 space_info->bytes_pinned + space_info->bytes_readonly +
9561 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04009562 spin_unlock(&space_info->lock);
9563 goto out;
9564 }
9565 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009566
Josef Bacikba1bf482009-09-11 16:11:19 -04009567 /*
9568 * ok we don't have enough space, but maybe we have free space on our
9569 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009570 * alloc devices and guess if we have enough space. if this block
9571 * group is going to be restriped, run checks against the target
9572 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04009573 */
9574 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05009575
liubocdcb7252011-08-03 10:15:25 +00009576 /*
9577 * index:
9578 * 0: raid10
9579 * 1: raid1
9580 * 2: dup
9581 * 3: raid0
9582 * 4: single
9583 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009584 target = get_restripe_target(fs_info, block_group->flags);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009585 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00009586 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009587 } else {
9588 /*
9589 * this is just a balance, so if we were marked as full
9590 * we know there is no space for a new chunk
9591 */
Qu Wenruo0305bc22016-03-23 11:38:17 +08009592 if (full) {
9593 if (debug)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009594 btrfs_warn(fs_info,
9595 "no space to alloc new chunk for block group %llu",
9596 block_group->key.objectid);
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009597 goto out;
Qu Wenruo0305bc22016-03-23 11:38:17 +08009598 }
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009599
9600 index = get_block_group_index(block_group);
9601 }
9602
Miao Xiee6ec7162013-01-17 05:38:51 +00009603 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00009604 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04009605 /* Divide by 2 */
9606 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009607 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00009608 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00009609 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04009610 /* Multiply by 2 */
9611 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009612 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00009613 dev_min = fs_devices->rw_devices;
David Sterba47c57132015-02-20 18:43:47 +01009614 min_free = div64_u64(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00009615 }
9616
Josef Bacik6df9a952013-06-27 13:22:46 -04009617 /* We need to do this so that we can look at pending chunks */
9618 trans = btrfs_join_transaction(root);
9619 if (IS_ERR(trans)) {
9620 ret = PTR_ERR(trans);
9621 goto out;
9622 }
9623
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009624 mutex_lock(&fs_info->chunk_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04009625 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00009626 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04009627
Josef Bacikba1bf482009-09-11 16:11:19 -04009628 /*
9629 * check to make sure we can actually find a chunk with enough
9630 * space to fit our block group in.
9631 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01009632 if (device->total_bytes > device->bytes_used + min_free &&
9633 !device->is_tgtdev_for_dev_replace) {
Josef Bacik6df9a952013-06-27 13:22:46 -04009634 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00009635 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04009636 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00009637 dev_nr++;
9638
9639 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05009640 break;
liubocdcb7252011-08-03 10:15:25 +00009641
Josef Bacikba1bf482009-09-11 16:11:19 -04009642 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05009643 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009644 }
Qu Wenruo0305bc22016-03-23 11:38:17 +08009645 if (debug && ret == -1)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009646 btrfs_warn(fs_info,
9647 "no space to allocate a new chunk for block group %llu",
9648 block_group->key.objectid);
9649 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009650 btrfs_end_transaction(trans);
Chris Masonedbd8d42007-12-21 16:27:24 -05009651out:
Josef Bacikba1bf482009-09-11 16:11:19 -04009652 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05009653 return ret;
9654}
9655
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009656static int find_first_block_group(struct btrfs_fs_info *fs_info,
9657 struct btrfs_path *path,
9658 struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04009659{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009660 struct btrfs_root *root = fs_info->extent_root;
Chris Mason925baed2008-06-25 16:01:30 -04009661 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009662 struct btrfs_key found_key;
9663 struct extent_buffer *leaf;
9664 int slot;
9665
9666 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
9667 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009668 goto out;
9669
Chris Masond3977122009-01-05 21:25:51 -05009670 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009671 slot = path->slots[0];
9672 leaf = path->nodes[0];
9673 if (slot >= btrfs_header_nritems(leaf)) {
9674 ret = btrfs_next_leaf(root, path);
9675 if (ret == 0)
9676 continue;
9677 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009678 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04009679 break;
9680 }
9681 btrfs_item_key_to_cpu(leaf, &found_key, slot);
9682
9683 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04009684 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
Liu Bo6fb37b72016-06-22 18:31:27 -07009685 struct extent_map_tree *em_tree;
9686 struct extent_map *em;
9687
9688 em_tree = &root->fs_info->mapping_tree.map_tree;
9689 read_lock(&em_tree->lock);
9690 em = lookup_extent_mapping(em_tree, found_key.objectid,
9691 found_key.offset);
9692 read_unlock(&em_tree->lock);
9693 if (!em) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009694 btrfs_err(fs_info,
Liu Bo6fb37b72016-06-22 18:31:27 -07009695 "logical %llu len %llu found bg but no related chunk",
9696 found_key.objectid, found_key.offset);
9697 ret = -ENOENT;
9698 } else {
9699 ret = 0;
9700 }
Josef Bacik187ee582016-08-18 15:30:06 -04009701 free_extent_map(em);
Chris Mason925baed2008-06-25 16:01:30 -04009702 goto out;
9703 }
Chris Mason0b86a832008-03-24 15:01:56 -04009704 path->slots[0]++;
9705 }
Chris Mason925baed2008-06-25 16:01:30 -04009706out:
Chris Mason0b86a832008-03-24 15:01:56 -04009707 return ret;
9708}
9709
Josef Bacik0af3d002010-06-21 14:48:16 -04009710void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9711{
9712 struct btrfs_block_group_cache *block_group;
9713 u64 last = 0;
9714
9715 while (1) {
9716 struct inode *inode;
9717
9718 block_group = btrfs_lookup_first_block_group(info, last);
9719 while (block_group) {
9720 spin_lock(&block_group->lock);
9721 if (block_group->iref)
9722 break;
9723 spin_unlock(&block_group->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009724 block_group = next_block_group(info, block_group);
Josef Bacik0af3d002010-06-21 14:48:16 -04009725 }
9726 if (!block_group) {
9727 if (last == 0)
9728 break;
9729 last = 0;
9730 continue;
9731 }
9732
9733 inode = block_group->inode;
9734 block_group->iref = 0;
9735 block_group->inode = NULL;
9736 spin_unlock(&block_group->lock);
Liu Bof3bca802016-07-20 17:33:44 -07009737 ASSERT(block_group->io_ctl.inode == NULL);
Josef Bacik0af3d002010-06-21 14:48:16 -04009738 iput(inode);
9739 last = block_group->key.objectid + block_group->key.offset;
9740 btrfs_put_block_group(block_group);
9741 }
9742}
9743
Zheng Yan1a40e232008-09-26 10:09:34 -04009744int btrfs_free_block_groups(struct btrfs_fs_info *info)
9745{
9746 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04009747 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04009748 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04009749 struct rb_node *n;
9750
Josef Bacik9e351cc2014-03-13 15:42:13 -04009751 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009752 while (!list_empty(&info->caching_block_groups)) {
9753 caching_ctl = list_entry(info->caching_block_groups.next,
9754 struct btrfs_caching_control, list);
9755 list_del(&caching_ctl->list);
9756 put_caching_control(caching_ctl);
9757 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04009758 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009759
Josef Bacik47ab2a62014-09-18 11:20:02 -04009760 spin_lock(&info->unused_bgs_lock);
9761 while (!list_empty(&info->unused_bgs)) {
9762 block_group = list_first_entry(&info->unused_bgs,
9763 struct btrfs_block_group_cache,
9764 bg_list);
9765 list_del_init(&block_group->bg_list);
9766 btrfs_put_block_group(block_group);
9767 }
9768 spin_unlock(&info->unused_bgs_lock);
9769
Zheng Yan1a40e232008-09-26 10:09:34 -04009770 spin_lock(&info->block_group_cache_lock);
9771 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
9772 block_group = rb_entry(n, struct btrfs_block_group_cache,
9773 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04009774 rb_erase(&block_group->cache_node,
9775 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009776 RB_CLEAR_NODE(&block_group->cache_node);
Yan Zhengd899e052008-10-30 14:25:28 -04009777 spin_unlock(&info->block_group_cache_lock);
9778
Josef Bacik80eb2342008-10-29 14:49:05 -04009779 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04009780 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04009781 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05009782
Josef Bacik817d52f2009-07-13 21:29:25 -04009783 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04009784 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04009785
Josef Bacik3c148742011-02-02 15:53:47 +00009786 /*
9787 * We haven't cached this block group, which means we could
9788 * possibly have excluded extents on this block group.
9789 */
Josef Bacik36cce922013-08-05 11:15:21 -04009790 if (block_group->cached == BTRFS_CACHE_NO ||
9791 block_group->cached == BTRFS_CACHE_ERROR)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009792 free_excluded_extents(info, block_group);
Josef Bacik3c148742011-02-02 15:53:47 +00009793
Josef Bacik817d52f2009-07-13 21:29:25 -04009794 btrfs_remove_free_space_cache(block_group);
Liu Bof3bca802016-07-20 17:33:44 -07009795 ASSERT(list_empty(&block_group->dirty_list));
9796 ASSERT(list_empty(&block_group->io_list));
9797 ASSERT(list_empty(&block_group->bg_list));
9798 ASSERT(atomic_read(&block_group->count) == 1);
Josef Bacik11dfe352009-11-13 20:12:59 +00009799 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04009800
9801 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009802 }
9803 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04009804
9805 /* now that all the block groups are freed, go through and
9806 * free all the space_info structs. This is only called during
9807 * the final stages of unmount, and so we know nobody is
9808 * using them. We call synchronize_rcu() once before we start,
9809 * just to be on the safe side.
9810 */
9811 synchronize_rcu();
9812
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04009813 release_global_block_rsv(info);
9814
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309815 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009816 int i;
9817
Chris Mason4184ea72009-03-10 12:39:20 -04009818 space_info = list_entry(info->space_info.next,
9819 struct btrfs_space_info,
9820 list);
Josef Bacikd555b6c2016-03-25 13:25:51 -04009821
9822 /*
9823 * Do not hide this behind enospc_debug, this is actually
9824 * important and indicates a real bug if this happens.
9825 */
9826 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00009827 space_info->bytes_reserved > 0 ||
Josef Bacikd555b6c2016-03-25 13:25:51 -04009828 space_info->bytes_may_use > 0))
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009829 dump_space_info(info, space_info, 0, 0);
Chris Mason4184ea72009-03-10 12:39:20 -04009830 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009831 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
9832 struct kobject *kobj;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009833 kobj = space_info->block_group_kobjs[i];
9834 space_info->block_group_kobjs[i] = NULL;
9835 if (kobj) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009836 kobject_del(kobj);
9837 kobject_put(kobj);
9838 }
9839 }
9840 kobject_del(&space_info->kobj);
9841 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04009842 }
Zheng Yan1a40e232008-09-26 10:09:34 -04009843 return 0;
9844}
9845
Yan, Zhengb742bb82010-05-16 10:46:24 -04009846static void __link_block_group(struct btrfs_space_info *space_info,
9847 struct btrfs_block_group_cache *cache)
9848{
9849 int index = get_block_group_index(cache);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009850 bool first = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04009851
9852 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009853 if (list_empty(&space_info->block_groups[index]))
9854 first = true;
9855 list_add_tail(&cache->list, &space_info->block_groups[index]);
9856 up_write(&space_info->groups_sem);
9857
9858 if (first) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009859 struct raid_kobject *rkobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009860 int ret;
9861
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009862 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
9863 if (!rkobj)
9864 goto out_err;
9865 rkobj->raid_type = index;
9866 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
9867 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
9868 "%s", get_raid_name(index));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009869 if (ret) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009870 kobject_put(&rkobj->kobj);
9871 goto out_err;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009872 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009873 space_info->block_group_kobjs[index] = &rkobj->kobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009874 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009875
9876 return;
9877out_err:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04009878 btrfs_warn(cache->fs_info,
9879 "failed to add kobject for block cache, ignoring");
Yan, Zhengb742bb82010-05-16 10:46:24 -04009880}
9881
Miao Xie920e4a52014-01-15 20:00:55 +08009882static struct btrfs_block_group_cache *
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009883btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info,
9884 u64 start, u64 size)
Miao Xie920e4a52014-01-15 20:00:55 +08009885{
9886 struct btrfs_block_group_cache *cache;
9887
9888 cache = kzalloc(sizeof(*cache), GFP_NOFS);
9889 if (!cache)
9890 return NULL;
9891
9892 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
9893 GFP_NOFS);
9894 if (!cache->free_space_ctl) {
9895 kfree(cache);
9896 return NULL;
9897 }
9898
9899 cache->key.objectid = start;
9900 cache->key.offset = size;
9901 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
9902
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009903 cache->sectorsize = fs_info->sectorsize;
9904 cache->fs_info = fs_info;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009905 cache->full_stripe_len = btrfs_full_stripe_len(fs_info,
9906 &fs_info->mapping_tree,
9907 start);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07009908 set_free_space_tree_thresholds(cache);
9909
Miao Xie920e4a52014-01-15 20:00:55 +08009910 atomic_set(&cache->count, 1);
9911 spin_lock_init(&cache->lock);
Miao Xiee570fd22014-06-19 10:42:50 +08009912 init_rwsem(&cache->data_rwsem);
Miao Xie920e4a52014-01-15 20:00:55 +08009913 INIT_LIST_HEAD(&cache->list);
9914 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -04009915 INIT_LIST_HEAD(&cache->bg_list);
Josef Bacik633c0aa2014-10-31 09:49:34 -04009916 INIT_LIST_HEAD(&cache->ro_list);
Josef Bacikce93ec52014-11-17 15:45:48 -05009917 INIT_LIST_HEAD(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07009918 INIT_LIST_HEAD(&cache->io_list);
Miao Xie920e4a52014-01-15 20:00:55 +08009919 btrfs_init_free_space_ctl(cache);
Filipe Manana04216822014-11-27 21:14:15 +00009920 atomic_set(&cache->trimming, 0);
Omar Sandovala5ed9182015-09-29 20:50:35 -07009921 mutex_init(&cache->free_space_lock);
Miao Xie920e4a52014-01-15 20:00:55 +08009922
9923 return cache;
9924}
9925
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04009926int btrfs_read_block_groups(struct btrfs_fs_info *info)
Chris Mason9078a3e2007-04-26 16:46:15 -04009927{
9928 struct btrfs_path *path;
9929 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04009930 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04009931 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04009932 struct btrfs_key key;
9933 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04009934 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04009935 int need_clear = 0;
9936 u64 cache_gen;
Liu Bo49303382016-08-25 18:08:27 -07009937 u64 feature;
9938 int mixed;
9939
9940 feature = btrfs_super_incompat_flags(info->super_copy);
9941 mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS);
Chris Mason96b51792007-10-15 16:15:19 -04009942
Chris Mason9078a3e2007-04-26 16:46:15 -04009943 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009944 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009945 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Chris Mason9078a3e2007-04-26 16:46:15 -04009946 path = btrfs_alloc_path();
9947 if (!path)
9948 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01009949 path->reada = READA_FORWARD;
Chris Mason9078a3e2007-04-26 16:46:15 -04009950
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009951 cache_gen = btrfs_super_cache_generation(info->super_copy);
9952 if (btrfs_test_opt(info, SPACE_CACHE) &&
9953 btrfs_super_generation(info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04009954 need_clear = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009955 if (btrfs_test_opt(info, CLEAR_CACHE))
Josef Bacik88c2ba32010-09-21 14:21:34 -04009956 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04009957
Chris Masond3977122009-01-05 21:25:51 -05009958 while (1) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04009959 ret = find_first_block_group(info, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04009960 if (ret > 0)
9961 break;
Chris Mason0b86a832008-03-24 15:01:56 -04009962 if (ret != 0)
9963 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +08009964
Chris Mason5f39d392007-10-15 16:14:19 -04009965 leaf = path->nodes[0];
9966 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +08009967
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009968 cache = btrfs_create_block_group_cache(info, found_key.objectid,
Miao Xie920e4a52014-01-15 20:00:55 +08009969 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04009970 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04009971 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009972 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04009973 }
Josef Bacik96303082009-07-13 21:29:25 -04009974
Liu Bocf7c1ef2012-07-06 03:31:34 -06009975 if (need_clear) {
9976 /*
9977 * When we mount with old space cache, we need to
9978 * set BTRFS_DC_CLEAR and set dirty flag.
9979 *
9980 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
9981 * truncate the old free space cache inode and
9982 * setup a new one.
9983 * b) Setting 'dirty flag' makes sure that we flush
9984 * the new space cache info onto disk.
9985 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009986 if (btrfs_test_opt(info, SPACE_CACHE))
Josef Bacikce93ec52014-11-17 15:45:48 -05009987 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06009988 }
Josef Bacik0af3d002010-06-21 14:48:16 -04009989
Chris Mason5f39d392007-10-15 16:14:19 -04009990 read_extent_buffer(leaf, &cache->item,
9991 btrfs_item_ptr_offset(leaf, path->slots[0]),
9992 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +08009993 cache->flags = btrfs_block_group_flags(&cache->item);
Liu Bo49303382016-08-25 18:08:27 -07009994 if (!mixed &&
9995 ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) &&
9996 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) {
9997 btrfs_err(info,
9998"bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups",
9999 cache->key.objectid);
10000 ret = -EINVAL;
10001 goto error;
10002 }
Chris Mason0b86a832008-03-24 15:01:56 -040010003
Chris Mason9078a3e2007-04-26 16:46:15 -040010004 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +020010005 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +080010006
Josef Bacik817d52f2009-07-13 21:29:25 -040010007 /*
Josef Bacik3c148742011-02-02 15:53:47 +000010008 * We need to exclude the super stripes now so that the space
10009 * info has super bytes accounted for, otherwise we'll think
10010 * we have more space than we actually do.
10011 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010012 ret = exclude_super_stripes(info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010013 if (ret) {
10014 /*
10015 * We may have excluded something, so call this just in
10016 * case.
10017 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010018 free_excluded_extents(info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010019 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010020 goto error;
10021 }
Josef Bacik3c148742011-02-02 15:53:47 +000010022
10023 /*
Josef Bacik817d52f2009-07-13 21:29:25 -040010024 * check for two cases, either we are full, and therefore
10025 * don't need to bother with the caching work since we won't
10026 * find any space, or we are empty, and we can just add all
10027 * the space in and be done with it. This saves us _alot_ of
10028 * time, particularly in the full case.
10029 */
10030 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -040010031 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010032 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010033 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010034 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -040010035 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010036 cache->cached = BTRFS_CACHE_FINISHED;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010037 add_new_free_space(cache, info,
Josef Bacik817d52f2009-07-13 21:29:25 -040010038 found_key.objectid,
10039 found_key.objectid +
10040 found_key.offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010041 free_excluded_extents(info, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -040010042 }
Chris Mason96b51792007-10-15 16:15:19 -040010043
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010044 ret = btrfs_add_block_group_cache(info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010045 if (ret) {
10046 btrfs_remove_free_space_cache(cache);
10047 btrfs_put_block_group(cache);
10048 goto error;
10049 }
10050
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010051 trace_btrfs_add_block_group(info, cache, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -040010052 ret = update_space_info(info, cache->flags, found_key.offset,
10053 btrfs_block_group_used(&cache->item),
Josef Bacike40edf22016-03-25 13:25:47 -040010054 cache->bytes_super, &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010055 if (ret) {
10056 btrfs_remove_free_space_cache(cache);
10057 spin_lock(&info->block_group_cache_lock);
10058 rb_erase(&cache->cache_node,
10059 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +000010060 RB_CLEAR_NODE(&cache->cache_node);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010061 spin_unlock(&info->block_group_cache_lock);
10062 btrfs_put_block_group(cache);
10063 goto error;
10064 }
10065
Chris Mason6324fbf2008-03-24 15:01:59 -040010066 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -040010067
Yan, Zhengb742bb82010-05-16 10:46:24 -040010068 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010069
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010070 set_avail_alloc_bits(info, cache->flags);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010071 if (btrfs_chunk_readonly(info, cache->key.objectid)) {
Zhaolei868f4012015-08-05 16:43:27 +080010072 inc_block_group_ro(cache, 1);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010073 } else if (btrfs_block_group_used(&cache->item) == 0) {
10074 spin_lock(&info->unused_bgs_lock);
10075 /* Should always be true but just in case. */
10076 if (list_empty(&cache->bg_list)) {
10077 btrfs_get_block_group(cache);
10078 list_add_tail(&cache->bg_list,
10079 &info->unused_bgs);
10080 }
10081 spin_unlock(&info->unused_bgs_lock);
10082 }
Chris Mason9078a3e2007-04-26 16:46:15 -040010083 }
Yan, Zhengb742bb82010-05-16 10:46:24 -040010084
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010085 list_for_each_entry_rcu(space_info, &info->space_info, list) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010086 if (!(get_alloc_profile(info, space_info->flags) &
Yan, Zhengb742bb82010-05-16 10:46:24 -040010087 (BTRFS_BLOCK_GROUP_RAID10 |
10088 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -050010089 BTRFS_BLOCK_GROUP_RAID5 |
10090 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -040010091 BTRFS_BLOCK_GROUP_DUP)))
10092 continue;
10093 /*
10094 * avoid allocating from un-mirrored block group if there are
10095 * mirrored block groups.
10096 */
chandan1095cc02013-07-16 12:28:56 +053010097 list_for_each_entry(cache,
10098 &space_info->block_groups[BTRFS_RAID_RAID0],
10099 list)
Zhaolei868f4012015-08-05 16:43:27 +080010100 inc_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +053010101 list_for_each_entry(cache,
10102 &space_info->block_groups[BTRFS_RAID_SINGLE],
10103 list)
Zhaolei868f4012015-08-05 16:43:27 +080010104 inc_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -040010105 }
Yan, Zhengf0486c62010-05-16 10:46:25 -040010106
10107 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -040010108 ret = 0;
10109error:
Chris Mason9078a3e2007-04-26 16:46:15 -040010110 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -040010111 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -040010112}
Chris Mason6324fbf2008-03-24 15:01:59 -040010113
Josef Bacikea658ba2012-09-11 16:57:25 -040010114void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010115 struct btrfs_fs_info *fs_info)
Josef Bacikea658ba2012-09-11 16:57:25 -040010116{
10117 struct btrfs_block_group_cache *block_group, *tmp;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010118 struct btrfs_root *extent_root = fs_info->extent_root;
Josef Bacikea658ba2012-09-11 16:57:25 -040010119 struct btrfs_block_group_item item;
10120 struct btrfs_key key;
10121 int ret = 0;
Filipe Mananad9a05402015-10-03 13:13:13 +010010122 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010123
Filipe Mananad9a05402015-10-03 13:13:13 +010010124 trans->can_flush_pending_bgs = false;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010125 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
Josef Bacikea658ba2012-09-11 16:57:25 -040010126 if (ret)
Filipe Mananac92f6be2014-11-26 15:28:55 +000010127 goto next;
Josef Bacikea658ba2012-09-11 16:57:25 -040010128
10129 spin_lock(&block_group->lock);
10130 memcpy(&item, &block_group->item, sizeof(item));
10131 memcpy(&key, &block_group->key, sizeof(key));
10132 spin_unlock(&block_group->lock);
10133
10134 ret = btrfs_insert_item(trans, extent_root, &key, &item,
10135 sizeof(item));
10136 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010137 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010138 ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid,
10139 key.offset);
Josef Bacik6df9a952013-06-27 13:22:46 -040010140 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -040010141 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010142 add_block_group_free_space(trans, fs_info, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010143 /* already aborted the transaction if it failed. */
Filipe Mananac92f6be2014-11-26 15:28:55 +000010144next:
10145 list_del_init(&block_group->bg_list);
Josef Bacikea658ba2012-09-11 16:57:25 -040010146 }
Filipe Mananad9a05402015-10-03 13:13:13 +010010147 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -040010148}
10149
Chris Mason6324fbf2008-03-24 15:01:59 -040010150int btrfs_make_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010151 struct btrfs_fs_info *fs_info, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -040010152 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -040010153 u64 size)
10154{
Chris Mason6324fbf2008-03-24 15:01:59 -040010155 struct btrfs_block_group_cache *cache;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010156 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -040010157
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010158 btrfs_set_log_full_commit(fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -040010159
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010160 cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -040010161 if (!cache)
10162 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +080010163
Chris Mason6324fbf2008-03-24 15:01:59 -040010164 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -040010165 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
Chris Mason6324fbf2008-03-24 15:01:59 -040010166 btrfs_set_block_group_flags(&cache->item, type);
10167
Miao Xie920e4a52014-01-15 20:00:55 +080010168 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -040010169 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -040010170 cache->cached = BTRFS_CACHE_FINISHED;
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010171 cache->needs_free_space = 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010172 ret = exclude_super_stripes(fs_info, cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010173 if (ret) {
10174 /*
10175 * We may have excluded something, so call this just in
10176 * case.
10177 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010178 free_excluded_extents(fs_info, cache);
Miao Xie920e4a52014-01-15 20:00:55 +080010179 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -040010180 return ret;
10181 }
Josef Bacik96303082009-07-13 21:29:25 -040010182
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010183 add_new_free_space(cache, fs_info, chunk_offset, chunk_offset + size);
Josef Bacik817d52f2009-07-13 21:29:25 -040010184
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010185 free_excluded_extents(fs_info, cache);
Yan Zheng11833d62009-09-11 16:11:19 -040010186
Josef Bacikd0bd4562015-09-23 14:54:14 -040010187#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010188 if (btrfs_should_fragment_free_space(cache)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -040010189 u64 new_bytes_used = size - bytes_used;
10190
10191 bytes_used += new_bytes_used >> 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010192 fragment_free_space(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -040010193 }
10194#endif
Filipe Manana2e6e5182015-05-12 00:28:11 +010010195 /*
10196 * Call to ensure the corresponding space_info object is created and
10197 * assigned to our block group, but don't update its counters just yet.
10198 * We want our bg to be added to the rbtree with its ->space_info set.
10199 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010200 ret = update_space_info(fs_info, cache->flags, 0, 0, 0,
Filipe Manana2e6e5182015-05-12 00:28:11 +010010201 &cache->space_info);
10202 if (ret) {
10203 btrfs_remove_free_space_cache(cache);
10204 btrfs_put_block_group(cache);
10205 return ret;
10206 }
10207
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010208 ret = btrfs_add_block_group_cache(fs_info, cache);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010209 if (ret) {
10210 btrfs_remove_free_space_cache(cache);
10211 btrfs_put_block_group(cache);
10212 return ret;
10213 }
10214
Filipe Manana2e6e5182015-05-12 00:28:11 +010010215 /*
10216 * Now that our block group has its ->space_info set and is inserted in
10217 * the rbtree, update the space info's counters.
10218 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010219 trace_btrfs_add_block_group(fs_info, cache, 1);
10220 ret = update_space_info(fs_info, cache->flags, size, bytes_used,
Josef Bacike40edf22016-03-25 13:25:47 -040010221 cache->bytes_super, &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010222 if (ret) {
10223 btrfs_remove_free_space_cache(cache);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010224 spin_lock(&fs_info->block_group_cache_lock);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010225 rb_erase(&cache->cache_node,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010226 &fs_info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +000010227 RB_CLEAR_NODE(&cache->cache_node);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010228 spin_unlock(&fs_info->block_group_cache_lock);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010229 btrfs_put_block_group(cache);
10230 return ret;
10231 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010232 update_global_block_rsv(fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -040010233
Yan, Zhengb742bb82010-05-16 10:46:24 -040010234 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010235
Josef Bacik47ab2a62014-09-18 11:20:02 -040010236 list_add_tail(&cache->bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -040010237
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010238 set_avail_alloc_bits(fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -040010239 return 0;
10240}
Zheng Yan1a40e232008-09-26 10:09:34 -040010241
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010242static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
10243{
Ilya Dryomov899c81e2012-03-27 17:09:16 +030010244 u64 extra_flags = chunk_to_extended(flags) &
10245 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010246
Miao Xiede98ced2013-01-29 10:13:12 +000010247 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010248 if (flags & BTRFS_BLOCK_GROUP_DATA)
10249 fs_info->avail_data_alloc_bits &= ~extra_flags;
10250 if (flags & BTRFS_BLOCK_GROUP_METADATA)
10251 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
10252 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
10253 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +000010254 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010255}
10256
Zheng Yan1a40e232008-09-26 10:09:34 -040010257int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010258 struct btrfs_fs_info *fs_info, u64 group_start,
Filipe Manana04216822014-11-27 21:14:15 +000010259 struct extent_map *em)
Zheng Yan1a40e232008-09-26 10:09:34 -040010260{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010261 struct btrfs_root *root = fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010262 struct btrfs_path *path;
10263 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -040010264 struct btrfs_free_cluster *cluster;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010265 struct btrfs_root *tree_root = fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010266 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -040010267 struct inode *inode;
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010268 struct kobject *kobj = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -040010269 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010270 int index;
Josef Bacik89a55892010-10-14 14:52:27 -040010271 int factor;
Filipe Manana4f69cb92014-11-26 15:28:51 +000010272 struct btrfs_caching_control *caching_ctl = NULL;
Filipe Manana04216822014-11-27 21:14:15 +000010273 bool remove_em;
Zheng Yan1a40e232008-09-26 10:09:34 -040010274
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040010275 block_group = btrfs_lookup_block_group(fs_info, group_start);
Zheng Yan1a40e232008-09-26 10:09:34 -040010276 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -050010277 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -040010278
liubo9f7c43c2011-03-07 02:13:33 +000010279 /*
10280 * Free the reserved super bytes from this block group before
10281 * remove it.
10282 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010283 free_excluded_extents(fs_info, block_group);
liubo9f7c43c2011-03-07 02:13:33 +000010284
Zheng Yan1a40e232008-09-26 10:09:34 -040010285 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010286 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -040010287 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
10288 BTRFS_BLOCK_GROUP_RAID1 |
10289 BTRFS_BLOCK_GROUP_RAID10))
10290 factor = 2;
10291 else
10292 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -040010293
Chris Mason44fb5512009-06-04 15:34:51 -040010294 /* make sure this block group isn't part of an allocation cluster */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010295 cluster = &fs_info->data_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010296 spin_lock(&cluster->refill_lock);
10297 btrfs_return_cluster_to_free_space(block_group, cluster);
10298 spin_unlock(&cluster->refill_lock);
10299
10300 /*
10301 * make sure this block group isn't part of a metadata
10302 * allocation cluster
10303 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010304 cluster = &fs_info->meta_alloc_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -040010305 spin_lock(&cluster->refill_lock);
10306 btrfs_return_cluster_to_free_space(block_group, cluster);
10307 spin_unlock(&cluster->refill_lock);
10308
Zheng Yan1a40e232008-09-26 10:09:34 -040010309 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010310 if (!path) {
10311 ret = -ENOMEM;
10312 goto out;
10313 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010314
Chris Mason1bbc6212015-04-06 12:46:08 -070010315 /*
10316 * get the inode first so any iput calls done for the io_list
10317 * aren't the final iput (no unlinks allowed now)
10318 */
Ilya Dryomov10b2f342011-10-02 13:56:53 +030010319 inode = lookup_free_space_inode(tree_root, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010320
10321 mutex_lock(&trans->transaction->cache_write_mutex);
10322 /*
10323 * make sure our free spache cache IO is done before remove the
10324 * free space inode
10325 */
10326 spin_lock(&trans->transaction->dirty_bgs_lock);
10327 if (!list_empty(&block_group->io_list)) {
10328 list_del_init(&block_group->io_list);
10329
10330 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
10331
10332 spin_unlock(&trans->transaction->dirty_bgs_lock);
Jeff Mahoneyafdb5712016-09-09 12:09:35 -040010333 btrfs_wait_cache_io(trans, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010334 btrfs_put_block_group(block_group);
10335 spin_lock(&trans->transaction->dirty_bgs_lock);
10336 }
10337
10338 if (!list_empty(&block_group->dirty_list)) {
10339 list_del_init(&block_group->dirty_list);
10340 btrfs_put_block_group(block_group);
10341 }
10342 spin_unlock(&trans->transaction->dirty_bgs_lock);
10343 mutex_unlock(&trans->transaction->cache_write_mutex);
10344
Josef Bacik0af3d002010-06-21 14:48:16 -040010345 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +000010346 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010347 if (ret) {
10348 btrfs_add_delayed_iput(inode);
10349 goto out;
10350 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010351 clear_nlink(inode);
10352 /* One for the block groups ref */
10353 spin_lock(&block_group->lock);
10354 if (block_group->iref) {
10355 block_group->iref = 0;
10356 block_group->inode = NULL;
10357 spin_unlock(&block_group->lock);
10358 iput(inode);
10359 } else {
10360 spin_unlock(&block_group->lock);
10361 }
10362 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -040010363 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -040010364 }
10365
10366 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
10367 key.offset = block_group->key.objectid;
10368 key.type = 0;
10369
10370 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
10371 if (ret < 0)
10372 goto out;
10373 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +020010374 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010375 if (ret == 0) {
10376 ret = btrfs_del_item(trans, tree_root, path);
10377 if (ret)
10378 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +020010379 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010380 }
10381
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010382 spin_lock(&fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -040010383 rb_erase(&block_group->cache_node,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010384 &fs_info->block_group_cache_tree);
Filipe Manana292cbd52014-11-26 15:28:50 +000010385 RB_CLEAR_NODE(&block_group->cache_node);
Liu Boa1897fd2012-12-27 09:01:23 +000010386
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010387 if (fs_info->first_logical_byte == block_group->key.objectid)
10388 fs_info->first_logical_byte = (u64)-1;
10389 spin_unlock(&fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010390
Josef Bacik80eb2342008-10-29 14:49:05 -040010391 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -040010392 /*
10393 * we must use list_del_init so people can check to see if they
10394 * are still on the list after taking the semaphore
10395 */
10396 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010397 if (list_empty(&block_group->space_info->block_groups[index])) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010398 kobj = block_group->space_info->block_group_kobjs[index];
10399 block_group->space_info->block_group_kobjs[index] = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010400 clear_avail_alloc_bits(fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010401 }
Josef Bacik80eb2342008-10-29 14:49:05 -040010402 up_write(&block_group->space_info->groups_sem);
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010403 if (kobj) {
10404 kobject_del(kobj);
10405 kobject_put(kobj);
10406 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010407
Filipe Manana4f69cb92014-11-26 15:28:51 +000010408 if (block_group->has_caching_ctl)
10409 caching_ctl = get_caching_control(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -040010410 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -040010411 wait_block_group_cache_done(block_group);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010412 if (block_group->has_caching_ctl) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010413 down_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010414 if (!caching_ctl) {
10415 struct btrfs_caching_control *ctl;
10416
10417 list_for_each_entry(ctl,
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010418 &fs_info->caching_block_groups, list)
Filipe Manana4f69cb92014-11-26 15:28:51 +000010419 if (ctl->block_group == block_group) {
10420 caching_ctl = ctl;
10421 atomic_inc(&caching_ctl->count);
10422 break;
10423 }
10424 }
10425 if (caching_ctl)
10426 list_del_init(&caching_ctl->list);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010427 up_write(&fs_info->commit_root_sem);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010428 if (caching_ctl) {
10429 /* Once for the caching bgs list and once for us. */
10430 put_caching_control(caching_ctl);
10431 put_caching_control(caching_ctl);
10432 }
10433 }
Josef Bacik817d52f2009-07-13 21:29:25 -040010434
Josef Bacikce93ec52014-11-17 15:45:48 -050010435 spin_lock(&trans->transaction->dirty_bgs_lock);
10436 if (!list_empty(&block_group->dirty_list)) {
Chris Mason1bbc6212015-04-06 12:46:08 -070010437 WARN_ON(1);
10438 }
10439 if (!list_empty(&block_group->io_list)) {
10440 WARN_ON(1);
Josef Bacikce93ec52014-11-17 15:45:48 -050010441 }
10442 spin_unlock(&trans->transaction->dirty_bgs_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010443 btrfs_remove_free_space_cache(block_group);
10444
Yan Zhengc146afa2008-11-12 14:34:12 -050010445 spin_lock(&block_group->space_info->lock);
Filipe Manana75c68e92015-01-16 13:24:40 +000010446 list_del_init(&block_group->ro_list);
Zhao Lei18d018a2015-02-24 20:07:44 +080010447
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010448 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
Zhao Lei18d018a2015-02-24 20:07:44 +080010449 WARN_ON(block_group->space_info->total_bytes
10450 < block_group->key.offset);
10451 WARN_ON(block_group->space_info->bytes_readonly
10452 < block_group->key.offset);
10453 WARN_ON(block_group->space_info->disk_total
10454 < block_group->key.offset * factor);
10455 }
Yan Zhengc146afa2008-11-12 14:34:12 -050010456 block_group->space_info->total_bytes -= block_group->key.offset;
10457 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -040010458 block_group->space_info->disk_total -= block_group->key.offset * factor;
Zhao Lei18d018a2015-02-24 20:07:44 +080010459
Yan Zhengc146afa2008-11-12 14:34:12 -050010460 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -040010461
Josef Bacik0af3d002010-06-21 14:48:16 -040010462 memcpy(&key, &block_group->key, sizeof(key));
10463
David Sterba34441362016-10-04 19:34:27 +020010464 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana495e64f2014-12-02 18:07:30 +000010465 if (!list_empty(&em->list)) {
10466 /* We're in the transaction->pending_chunks list. */
10467 free_extent_map(em);
10468 }
Filipe Manana04216822014-11-27 21:14:15 +000010469 spin_lock(&block_group->lock);
10470 block_group->removed = 1;
10471 /*
10472 * At this point trimming can't start on this block group, because we
10473 * removed the block group from the tree fs_info->block_group_cache_tree
10474 * so no one can't find it anymore and even if someone already got this
10475 * block group before we removed it from the rbtree, they have already
10476 * incremented block_group->trimming - if they didn't, they won't find
10477 * any free space entries because we already removed them all when we
10478 * called btrfs_remove_free_space_cache().
10479 *
10480 * And we must not remove the extent map from the fs_info->mapping_tree
10481 * to prevent the same logical address range and physical device space
10482 * ranges from being reused for a new block group. This is because our
10483 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
10484 * completely transactionless, so while it is trimming a range the
10485 * currently running transaction might finish and a new one start,
10486 * allowing for new block groups to be created that can reuse the same
10487 * physical device locations unless we take this special care.
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010488 *
10489 * There may also be an implicit trim operation if the file system
10490 * is mounted with -odiscard. The same protections must remain
10491 * in place until the extents have been discarded completely when
10492 * the transaction commit has completed.
Filipe Manana04216822014-11-27 21:14:15 +000010493 */
10494 remove_em = (atomic_read(&block_group->trimming) == 0);
10495 /*
10496 * Make sure a trimmer task always sees the em in the pinned_chunks list
10497 * if it sees block_group->removed == 1 (needs to lock block_group->lock
10498 * before checking block_group->removed).
10499 */
10500 if (!remove_em) {
10501 /*
10502 * Our em might be in trans->transaction->pending_chunks which
10503 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10504 * and so is the fs_info->pinned_chunks list.
10505 *
10506 * So at this point we must be holding the chunk_mutex to avoid
10507 * any races with chunk allocation (more specifically at
10508 * volumes.c:contains_pending_extent()), to ensure it always
10509 * sees the em, either in the pending_chunks list or in the
10510 * pinned_chunks list.
10511 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010512 list_move_tail(&em->list, &fs_info->pinned_chunks);
Filipe Manana04216822014-11-27 21:14:15 +000010513 }
10514 spin_unlock(&block_group->lock);
Filipe Manana04216822014-11-27 21:14:15 +000010515
10516 if (remove_em) {
10517 struct extent_map_tree *em_tree;
10518
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010519 em_tree = &fs_info->mapping_tree.map_tree;
Filipe Manana04216822014-11-27 21:14:15 +000010520 write_lock(&em_tree->lock);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010521 /*
10522 * The em might be in the pending_chunks list, so make sure the
10523 * chunk mutex is locked, since remove_extent_mapping() will
10524 * delete us from that list.
10525 */
Filipe Manana04216822014-11-27 21:14:15 +000010526 remove_extent_mapping(em_tree, em);
10527 write_unlock(&em_tree->lock);
10528 /* once for the tree */
10529 free_extent_map(em);
10530 }
10531
David Sterba34441362016-10-04 19:34:27 +020010532 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010533
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010534 ret = remove_block_group_free_space(trans, fs_info, block_group);
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010535 if (ret)
10536 goto out;
10537
Chris Masonfa9c0d792009-04-03 09:47:43 -040010538 btrfs_put_block_group(block_group);
10539 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -040010540
10541 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
10542 if (ret > 0)
10543 ret = -EIO;
10544 if (ret < 0)
10545 goto out;
10546
10547 ret = btrfs_del_item(trans, root, path);
10548out:
10549 btrfs_free_path(path);
10550 return ret;
10551}
liuboacce9522011-01-06 19:30:25 +080010552
Filipe Manana8eab77f2015-11-13 23:57:16 +000010553struct btrfs_trans_handle *
Filipe Manana7fd01182015-11-13 23:57:17 +000010554btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info,
10555 const u64 chunk_offset)
Filipe Manana8eab77f2015-11-13 23:57:16 +000010556{
Filipe Manana7fd01182015-11-13 23:57:17 +000010557 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
10558 struct extent_map *em;
10559 struct map_lookup *map;
10560 unsigned int num_items;
10561
10562 read_lock(&em_tree->lock);
10563 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
10564 read_unlock(&em_tree->lock);
10565 ASSERT(em && em->start == chunk_offset);
10566
Filipe Manana8eab77f2015-11-13 23:57:16 +000010567 /*
Filipe Manana7fd01182015-11-13 23:57:17 +000010568 * We need to reserve 3 + N units from the metadata space info in order
10569 * to remove a block group (done at btrfs_remove_chunk() and at
10570 * btrfs_remove_block_group()), which are used for:
10571 *
Filipe Manana8eab77f2015-11-13 23:57:16 +000010572 * 1 unit for adding the free space inode's orphan (located in the tree
10573 * of tree roots).
Filipe Manana7fd01182015-11-13 23:57:17 +000010574 * 1 unit for deleting the block group item (located in the extent
10575 * tree).
10576 * 1 unit for deleting the free space item (located in tree of tree
10577 * roots).
10578 * N units for deleting N device extent items corresponding to each
10579 * stripe (located in the device tree).
10580 *
10581 * In order to remove a block group we also need to reserve units in the
10582 * system space info in order to update the chunk tree (update one or
10583 * more device items and remove one chunk item), but this is done at
10584 * btrfs_remove_chunk() through a call to check_system_chunk().
Filipe Manana8eab77f2015-11-13 23:57:16 +000010585 */
Jeff Mahoney95617d62015-06-03 10:55:48 -040010586 map = em->map_lookup;
Filipe Manana7fd01182015-11-13 23:57:17 +000010587 num_items = 3 + map->num_stripes;
10588 free_extent_map(em);
10589
Filipe Manana8eab77f2015-11-13 23:57:16 +000010590 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
Filipe Manana7fd01182015-11-13 23:57:17 +000010591 num_items, 1);
Filipe Manana8eab77f2015-11-13 23:57:16 +000010592}
10593
Josef Bacik47ab2a62014-09-18 11:20:02 -040010594/*
10595 * Process the unused_bgs list and remove any that don't have any allocated
10596 * space inside of them.
10597 */
10598void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10599{
10600 struct btrfs_block_group_cache *block_group;
10601 struct btrfs_space_info *space_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010602 struct btrfs_trans_handle *trans;
10603 int ret = 0;
10604
Josef Bacikafcdd122016-09-02 15:40:02 -040010605 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
Josef Bacik47ab2a62014-09-18 11:20:02 -040010606 return;
10607
10608 spin_lock(&fs_info->unused_bgs_lock);
10609 while (!list_empty(&fs_info->unused_bgs)) {
10610 u64 start, end;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010611 int trimming;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010612
10613 block_group = list_first_entry(&fs_info->unused_bgs,
10614 struct btrfs_block_group_cache,
10615 bg_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010616 list_del_init(&block_group->bg_list);
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010617
10618 space_info = block_group->space_info;
10619
Josef Bacik47ab2a62014-09-18 11:20:02 -040010620 if (ret || btrfs_mixed_space_info(space_info)) {
10621 btrfs_put_block_group(block_group);
10622 continue;
10623 }
10624 spin_unlock(&fs_info->unused_bgs_lock);
10625
Zhao Leid5f2e332015-10-08 18:46:44 +080010626 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +010010627
Josef Bacik47ab2a62014-09-18 11:20:02 -040010628 /* Don't want to race with allocators so take the groups_sem */
10629 down_write(&space_info->groups_sem);
10630 spin_lock(&block_group->lock);
10631 if (block_group->reserved ||
10632 btrfs_block_group_used(&block_group->item) ||
Chris Mason19c4d2f2016-10-10 13:43:31 -070010633 block_group->ro ||
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010634 list_is_singular(&block_group->list)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -040010635 /*
10636 * We want to bail if we made new allocations or have
10637 * outstanding allocations in this block group. We do
10638 * the ro check in case balance is currently acting on
10639 * this block group.
10640 */
10641 spin_unlock(&block_group->lock);
10642 up_write(&space_info->groups_sem);
10643 goto next;
10644 }
10645 spin_unlock(&block_group->lock);
10646
10647 /* We don't want to force the issue, only flip if it's ok. */
Zhaolei868f4012015-08-05 16:43:27 +080010648 ret = inc_block_group_ro(block_group, 0);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010649 up_write(&space_info->groups_sem);
10650 if (ret < 0) {
10651 ret = 0;
10652 goto next;
10653 }
10654
10655 /*
10656 * Want to do this before we do anything else so we can recover
10657 * properly if we fail to join the transaction.
10658 */
Filipe Manana7fd01182015-11-13 23:57:17 +000010659 trans = btrfs_start_trans_remove_block_group(fs_info,
10660 block_group->key.objectid);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010661 if (IS_ERR(trans)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010662 btrfs_dec_block_group_ro(block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010663 ret = PTR_ERR(trans);
10664 goto next;
10665 }
10666
10667 /*
10668 * We could have pending pinned extents for this block group,
10669 * just delete them, we don't care about them anymore.
10670 */
10671 start = block_group->key.objectid;
10672 end = start + block_group->key.offset - 1;
Filipe Mananad4b450c2015-01-29 19:18:25 +000010673 /*
10674 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10675 * btrfs_finish_extent_commit(). If we are at transaction N,
10676 * another task might be running finish_extent_commit() for the
10677 * previous transaction N - 1, and have seen a range belonging
10678 * to the block group in freed_extents[] before we were able to
10679 * clear the whole block group range from freed_extents[]. This
10680 * means that task can lookup for the block group after we
10681 * unpinned it from freed_extents[] and removed it, leading to
10682 * a BUG_ON() at btrfs_unpin_extent_range().
10683 */
10684 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana758eb512014-11-03 14:08:39 +000010685 ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
David Sterba91166212016-04-26 23:54:39 +020010686 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010687 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010688 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010689 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010690 goto end_trans;
10691 }
10692 ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
David Sterba91166212016-04-26 23:54:39 +020010693 EXTENT_DIRTY);
Filipe Manana758eb512014-11-03 14:08:39 +000010694 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010695 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010696 btrfs_dec_block_group_ro(block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010697 goto end_trans;
10698 }
Filipe Mananad4b450c2015-01-29 19:18:25 +000010699 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010700
10701 /* Reset pinned so btrfs_put_block_group doesn't complain */
Zhao Leic30666d2015-02-25 14:17:20 +080010702 spin_lock(&space_info->lock);
10703 spin_lock(&block_group->lock);
10704
10705 space_info->bytes_pinned -= block_group->pinned;
10706 space_info->bytes_readonly += block_group->pinned;
10707 percpu_counter_add(&space_info->total_bytes_pinned,
10708 -block_group->pinned);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010709 block_group->pinned = 0;
10710
Zhao Leic30666d2015-02-25 14:17:20 +080010711 spin_unlock(&block_group->lock);
10712 spin_unlock(&space_info->lock);
10713
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010714 /* DISCARD can flip during remount */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010715 trimming = btrfs_test_opt(fs_info, DISCARD);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010716
10717 /* Implicit trim during transaction commit. */
10718 if (trimming)
10719 btrfs_get_block_group_trimming(block_group);
10720
Josef Bacik47ab2a62014-09-18 11:20:02 -040010721 /*
10722 * Btrfs_remove_chunk will abort the transaction if things go
10723 * horribly wrong.
10724 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -040010725 ret = btrfs_remove_chunk(trans, fs_info,
Josef Bacik47ab2a62014-09-18 11:20:02 -040010726 block_group->key.objectid);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010727
10728 if (ret) {
10729 if (trimming)
10730 btrfs_put_block_group_trimming(block_group);
10731 goto end_trans;
10732 }
10733
10734 /*
10735 * If we're not mounted with -odiscard, we can just forget
10736 * about this block group. Otherwise we'll need to wait
10737 * until transaction commit to do the actual discard.
10738 */
10739 if (trimming) {
Filipe Manana348a0012015-11-27 12:16:16 +000010740 spin_lock(&fs_info->unused_bgs_lock);
10741 /*
10742 * A concurrent scrub might have added us to the list
10743 * fs_info->unused_bgs, so use a list_move operation
10744 * to add the block group to the deleted_bgs list.
10745 */
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010746 list_move(&block_group->bg_list,
10747 &trans->transaction->deleted_bgs);
Filipe Manana348a0012015-11-27 12:16:16 +000010748 spin_unlock(&fs_info->unused_bgs_lock);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010749 btrfs_get_block_group(block_group);
10750 }
Filipe Manana758eb512014-11-03 14:08:39 +000010751end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010752 btrfs_end_transaction(trans);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010753next:
Zhao Leid5f2e332015-10-08 18:46:44 +080010754 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010755 btrfs_put_block_group(block_group);
10756 spin_lock(&fs_info->unused_bgs_lock);
10757 }
10758 spin_unlock(&fs_info->unused_bgs_lock);
10759}
10760
liuboc59021f2011-03-07 02:13:14 +000010761int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
10762{
10763 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +000010764 struct btrfs_super_block *disk_super;
10765 u64 features;
10766 u64 flags;
10767 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +000010768 int ret;
10769
David Sterba6c417612011-04-13 15:41:04 +020010770 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +000010771 if (!btrfs_super_root(disk_super))
Dan Carpenter0dc924c52016-01-13 15:21:17 +030010772 return -EINVAL;
liuboc59021f2011-03-07 02:13:14 +000010773
liubo1aba86d2011-04-08 08:44:37 +000010774 features = btrfs_super_incompat_flags(disk_super);
10775 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
10776 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +000010777
liubo1aba86d2011-04-08 08:44:37 +000010778 flags = BTRFS_BLOCK_GROUP_SYSTEM;
Josef Bacike40edf22016-03-25 13:25:47 -040010779 ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +000010780 if (ret)
liubo1aba86d2011-04-08 08:44:37 +000010781 goto out;
liuboc59021f2011-03-07 02:13:14 +000010782
liubo1aba86d2011-04-08 08:44:37 +000010783 if (mixed) {
10784 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
Josef Bacike40edf22016-03-25 13:25:47 -040010785 ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010786 } else {
10787 flags = BTRFS_BLOCK_GROUP_METADATA;
Josef Bacike40edf22016-03-25 13:25:47 -040010788 ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010789 if (ret)
10790 goto out;
10791
10792 flags = BTRFS_BLOCK_GROUP_DATA;
Josef Bacike40edf22016-03-25 13:25:47 -040010793 ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info);
liubo1aba86d2011-04-08 08:44:37 +000010794 }
10795out:
liuboc59021f2011-03-07 02:13:14 +000010796 return ret;
10797}
10798
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010799int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
10800 u64 start, u64 end)
liuboacce9522011-01-06 19:30:25 +080010801{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010802 return unpin_extent_range(fs_info, start, end, false);
liuboacce9522011-01-06 19:30:25 +080010803}
10804
Jeff Mahoney499f3772015-06-15 09:41:17 -040010805/*
10806 * It used to be that old block groups would be left around forever.
10807 * Iterating over them would be enough to trim unused space. Since we
10808 * now automatically remove them, we also need to iterate over unallocated
10809 * space.
10810 *
10811 * We don't want a transaction for this since the discard may take a
10812 * substantial amount of time. We don't require that a transaction be
10813 * running, but we do need to take a running transaction into account
10814 * to ensure that we're not discarding chunks that were released in
10815 * the current transaction.
10816 *
10817 * Holding the chunks lock will prevent other threads from allocating
10818 * or releasing chunks, but it won't prevent a running transaction
10819 * from committing and releasing the memory that the pending chunks
10820 * list head uses. For that, we need to take a reference to the
10821 * transaction.
10822 */
10823static int btrfs_trim_free_extents(struct btrfs_device *device,
10824 u64 minlen, u64 *trimmed)
10825{
10826 u64 start = 0, len = 0;
10827 int ret;
10828
10829 *trimmed = 0;
10830
10831 /* Not writeable = nothing to do. */
10832 if (!device->writeable)
10833 return 0;
10834
10835 /* No free space = nothing to do. */
10836 if (device->total_bytes <= device->bytes_used)
10837 return 0;
10838
10839 ret = 0;
10840
10841 while (1) {
Jeff Mahoneyfb456252016-06-22 18:54:56 -040010842 struct btrfs_fs_info *fs_info = device->fs_info;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010843 struct btrfs_transaction *trans;
10844 u64 bytes;
10845
10846 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
10847 if (ret)
10848 return ret;
10849
10850 down_read(&fs_info->commit_root_sem);
10851
10852 spin_lock(&fs_info->trans_lock);
10853 trans = fs_info->running_transaction;
10854 if (trans)
10855 atomic_inc(&trans->use_count);
10856 spin_unlock(&fs_info->trans_lock);
10857
10858 ret = find_free_dev_extent_start(trans, device, minlen, start,
10859 &start, &len);
10860 if (trans)
10861 btrfs_put_transaction(trans);
10862
10863 if (ret) {
10864 up_read(&fs_info->commit_root_sem);
10865 mutex_unlock(&fs_info->chunk_mutex);
10866 if (ret == -ENOSPC)
10867 ret = 0;
10868 break;
10869 }
10870
10871 ret = btrfs_issue_discard(device->bdev, start, len, &bytes);
10872 up_read(&fs_info->commit_root_sem);
10873 mutex_unlock(&fs_info->chunk_mutex);
10874
10875 if (ret)
10876 break;
10877
10878 start += len;
10879 *trimmed += bytes;
10880
10881 if (fatal_signal_pending(current)) {
10882 ret = -ERESTARTSYS;
10883 break;
10884 }
10885
10886 cond_resched();
10887 }
10888
10889 return ret;
10890}
10891
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010892int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
Li Dongyangf7039b12011-03-24 10:24:28 +000010893{
Li Dongyangf7039b12011-03-24 10:24:28 +000010894 struct btrfs_block_group_cache *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010895 struct btrfs_device *device;
10896 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +000010897 u64 group_trimmed;
10898 u64 start;
10899 u64 end;
10900 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +080010901 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +000010902 int ret = 0;
10903
Liu Bo2cac13e2012-02-09 18:17:41 +080010904 /*
10905 * try to trim all FS space, our block group may start from non-zero.
10906 */
10907 if (range->len == total_bytes)
10908 cache = btrfs_lookup_first_block_group(fs_info, range->start);
10909 else
10910 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +000010911
10912 while (cache) {
10913 if (cache->key.objectid >= (range->start + range->len)) {
10914 btrfs_put_block_group(cache);
10915 break;
10916 }
10917
10918 start = max(range->start, cache->key.objectid);
10919 end = min(range->start + range->len,
10920 cache->key.objectid + cache->key.offset);
10921
10922 if (end - start >= range->minlen) {
10923 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +000010924 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -040010925 if (ret) {
10926 btrfs_put_block_group(cache);
10927 break;
10928 }
10929 ret = wait_block_group_cache_done(cache);
10930 if (ret) {
10931 btrfs_put_block_group(cache);
10932 break;
10933 }
Li Dongyangf7039b12011-03-24 10:24:28 +000010934 }
10935 ret = btrfs_trim_block_group(cache,
10936 &group_trimmed,
10937 start,
10938 end,
10939 range->minlen);
10940
10941 trimmed += group_trimmed;
10942 if (ret) {
10943 btrfs_put_block_group(cache);
10944 break;
10945 }
10946 }
10947
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010948 cache = next_block_group(fs_info, cache);
Li Dongyangf7039b12011-03-24 10:24:28 +000010949 }
10950
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010951 mutex_lock(&fs_info->fs_devices->device_list_mutex);
10952 devices = &fs_info->fs_devices->alloc_list;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010953 list_for_each_entry(device, devices, dev_alloc_list) {
10954 ret = btrfs_trim_free_extents(device, range->minlen,
10955 &group_trimmed);
10956 if (ret)
10957 break;
10958
10959 trimmed += group_trimmed;
10960 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010961 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney499f3772015-06-15 09:41:17 -040010962
Li Dongyangf7039b12011-03-24 10:24:28 +000010963 range->len = trimmed;
10964 return ret;
10965}
Miao Xie8257b2d2014-03-06 13:38:19 +080010966
10967/*
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010968 * btrfs_{start,end}_write_no_snapshoting() are similar to
10969 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
10970 * data into the page cache through nocow before the subvolume is snapshoted,
10971 * but flush the data into disk after the snapshot creation, or to prevent
10972 * operations while snapshoting is ongoing and that cause the snapshot to be
10973 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +080010974 */
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010975void btrfs_end_write_no_snapshoting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080010976{
10977 percpu_counter_dec(&root->subv_writers->counter);
10978 /*
David Sterbaa83342a2015-02-16 19:36:47 +010010979 * Make sure counter is updated before we wake up waiters.
Miao Xie8257b2d2014-03-06 13:38:19 +080010980 */
10981 smp_mb();
10982 if (waitqueue_active(&root->subv_writers->wait))
10983 wake_up(&root->subv_writers->wait);
10984}
10985
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010986int btrfs_start_write_no_snapshoting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080010987{
David Sterbaee39b432014-09-30 01:33:33 +020010988 if (atomic_read(&root->will_be_snapshoted))
Miao Xie8257b2d2014-03-06 13:38:19 +080010989 return 0;
10990
10991 percpu_counter_inc(&root->subv_writers->counter);
10992 /*
10993 * Make sure counter is updated before we check for snapshot creation.
10994 */
10995 smp_mb();
David Sterbaee39b432014-09-30 01:33:33 +020010996 if (atomic_read(&root->will_be_snapshoted)) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010997 btrfs_end_write_no_snapshoting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +080010998 return 0;
10999 }
11000 return 1;
11001}
Zhao Lei0bc19f902016-01-06 18:56:36 +080011002
11003static int wait_snapshoting_atomic_t(atomic_t *a)
11004{
11005 schedule();
11006 return 0;
11007}
11008
11009void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
11010{
11011 while (true) {
11012 int ret;
11013
11014 ret = btrfs_start_write_no_snapshoting(root);
11015 if (ret)
11016 break;
11017 wait_on_atomic_t(&root->will_be_snapshoted,
11018 wait_snapshoting_atomic_t,
11019 TASK_UNINTERRUPTIBLE);
11020 }
11021}