blob: 8abb344e3dcb003ec5a3410e9cbbd23073e49db9 [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 Bacikfb25e912011-07-26 17:00:46 -040063/*
64 * Control how reservations are dealt with.
65 *
66 * RESERVE_FREE - freeing a reservation.
67 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
68 * ENOSPC accounting
69 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
70 * bytes_may_use as the ENOSPC accounting is done elsewhere
71 */
72enum {
73 RESERVE_FREE = 0,
74 RESERVE_ALLOC = 1,
75 RESERVE_ALLOC_NO_ACCOUNT = 2,
76};
77
Josef Bacikce93ec52014-11-17 15:45:48 -050078static int update_block_group(struct btrfs_trans_handle *trans,
79 struct btrfs_root *root, u64 bytenr,
80 u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040081static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
82 struct btrfs_root *root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080083 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040084 u64 root_objectid, u64 owner_objectid,
85 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080086 struct btrfs_delayed_extent_op *extra_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040087static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
88 struct extent_buffer *leaf,
89 struct btrfs_extent_item *ei);
90static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
91 struct btrfs_root *root,
92 u64 parent, u64 root_objectid,
93 u64 flags, u64 owner, u64 offset,
94 struct btrfs_key *ins, int ref_mod);
95static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
96 struct btrfs_root *root,
97 u64 parent, u64 root_objectid,
98 u64 flags, struct btrfs_disk_key *key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +010099 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -0500100static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -0400101 struct btrfs_root *extent_root, u64 flags,
102 int force);
Yan Zheng11833d62009-09-11 16:11:19 -0400103static int find_next_key(struct btrfs_path *path, int level,
104 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400105static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
106 int dump_block_groups);
Josef Bacikfb25e912011-07-26 17:00:46 -0400107static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
Miao Xiee570fd22014-06-19 10:42:50 +0800108 u64 num_bytes, int reserve,
109 int delalloc);
Josef Bacik5d803662013-02-07 16:06:02 -0500110static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
111 u64 num_bytes);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000112int btrfs_pin_extent(struct btrfs_root *root,
113 u64 bytenr, u64 num_bytes, int reserved);
Josef Bacik6a632092009-02-20 11:00:09 -0500114
Josef Bacik817d52f2009-07-13 21:29:25 -0400115static noinline int
116block_group_cache_done(struct btrfs_block_group_cache *cache)
117{
118 smp_mb();
Josef Bacik36cce922013-08-05 11:15:21 -0400119 return cache->cached == BTRFS_CACHE_FINISHED ||
120 cache->cached == BTRFS_CACHE_ERROR;
Josef Bacik817d52f2009-07-13 21:29:25 -0400121}
122
Josef Bacik0f9dd462008-09-23 13:14:11 -0400123static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
124{
125 return (cache->flags & bits) == bits;
126}
127
Filipe Manana758f2df2015-11-19 11:45:48 +0000128void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000129{
130 atomic_inc(&cache->count);
131}
132
133void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
134{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400135 if (atomic_dec_and_test(&cache->count)) {
136 WARN_ON(cache->pinned > 0);
137 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb2011-03-29 13:46:06 +0800138 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000139 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400140 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000141}
142
Josef Bacik0f9dd462008-09-23 13:14:11 -0400143/*
144 * this adds the block group to the fs_info rb tree for the block group
145 * cache
146 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500147static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400148 struct btrfs_block_group_cache *block_group)
149{
150 struct rb_node **p;
151 struct rb_node *parent = NULL;
152 struct btrfs_block_group_cache *cache;
153
154 spin_lock(&info->block_group_cache_lock);
155 p = &info->block_group_cache_tree.rb_node;
156
157 while (*p) {
158 parent = *p;
159 cache = rb_entry(parent, struct btrfs_block_group_cache,
160 cache_node);
161 if (block_group->key.objectid < cache->key.objectid) {
162 p = &(*p)->rb_left;
163 } else if (block_group->key.objectid > cache->key.objectid) {
164 p = &(*p)->rb_right;
165 } else {
166 spin_unlock(&info->block_group_cache_lock);
167 return -EEXIST;
168 }
169 }
170
171 rb_link_node(&block_group->cache_node, parent, p);
172 rb_insert_color(&block_group->cache_node,
173 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000174
175 if (info->first_logical_byte > block_group->key.objectid)
176 info->first_logical_byte = block_group->key.objectid;
177
Josef Bacik0f9dd462008-09-23 13:14:11 -0400178 spin_unlock(&info->block_group_cache_lock);
179
180 return 0;
181}
182
183/*
184 * This will return the block group at or after bytenr if contains is 0, else
185 * it will return the block group that contains the bytenr
186 */
187static struct btrfs_block_group_cache *
188block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
189 int contains)
190{
191 struct btrfs_block_group_cache *cache, *ret = NULL;
192 struct rb_node *n;
193 u64 end, start;
194
195 spin_lock(&info->block_group_cache_lock);
196 n = info->block_group_cache_tree.rb_node;
197
198 while (n) {
199 cache = rb_entry(n, struct btrfs_block_group_cache,
200 cache_node);
201 end = cache->key.objectid + cache->key.offset - 1;
202 start = cache->key.objectid;
203
204 if (bytenr < start) {
205 if (!contains && (!ret || start < ret->key.objectid))
206 ret = cache;
207 n = n->rb_left;
208 } else if (bytenr > start) {
209 if (contains && bytenr <= end) {
210 ret = cache;
211 break;
212 }
213 n = n->rb_right;
214 } else {
215 ret = cache;
216 break;
217 }
218 }
Liu Boa1897fd2012-12-27 09:01:23 +0000219 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000220 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000221 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
222 info->first_logical_byte = ret->key.objectid;
223 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400224 spin_unlock(&info->block_group_cache_lock);
225
226 return ret;
227}
228
Yan Zheng11833d62009-09-11 16:11:19 -0400229static int add_excluded_extent(struct btrfs_root *root,
230 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400231{
Yan Zheng11833d62009-09-11 16:11:19 -0400232 u64 end = start + num_bytes - 1;
233 set_extent_bits(&root->fs_info->freed_extents[0],
234 start, end, EXTENT_UPTODATE, GFP_NOFS);
235 set_extent_bits(&root->fs_info->freed_extents[1],
236 start, end, EXTENT_UPTODATE, GFP_NOFS);
237 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400238}
239
Yan Zheng11833d62009-09-11 16:11:19 -0400240static void free_excluded_extents(struct btrfs_root *root,
241 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400242{
Yan Zheng11833d62009-09-11 16:11:19 -0400243 u64 start, end;
244
245 start = cache->key.objectid;
246 end = start + cache->key.offset - 1;
247
248 clear_extent_bits(&root->fs_info->freed_extents[0],
249 start, end, EXTENT_UPTODATE, GFP_NOFS);
250 clear_extent_bits(&root->fs_info->freed_extents[1],
251 start, end, EXTENT_UPTODATE, GFP_NOFS);
252}
253
254static int exclude_super_stripes(struct btrfs_root *root,
255 struct btrfs_block_group_cache *cache)
256{
Josef Bacik817d52f2009-07-13 21:29:25 -0400257 u64 bytenr;
258 u64 *logical;
259 int stripe_len;
260 int i, nr, ret;
261
Yan, Zheng06b23312009-11-26 09:31:11 +0000262 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
263 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
264 cache->bytes_super += stripe_len;
265 ret = add_excluded_extent(root, cache->key.objectid,
266 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400267 if (ret)
268 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000269 }
270
Josef Bacik817d52f2009-07-13 21:29:25 -0400271 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
272 bytenr = btrfs_sb_offset(i);
273 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
274 cache->key.objectid, bytenr,
275 0, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400276 if (ret)
277 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400278
Josef Bacik817d52f2009-07-13 21:29:25 -0400279 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400280 u64 start, len;
281
282 if (logical[nr] > cache->key.objectid +
283 cache->key.offset)
284 continue;
285
286 if (logical[nr] + stripe_len <= cache->key.objectid)
287 continue;
288
289 start = logical[nr];
290 if (start < cache->key.objectid) {
291 start = cache->key.objectid;
292 len = (logical[nr] + stripe_len) - start;
293 } else {
294 len = min_t(u64, stripe_len,
295 cache->key.objectid +
296 cache->key.offset - start);
297 }
298
299 cache->bytes_super += len;
300 ret = add_excluded_extent(root, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400301 if (ret) {
302 kfree(logical);
303 return ret;
304 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400305 }
Yan Zheng11833d62009-09-11 16:11:19 -0400306
Josef Bacik817d52f2009-07-13 21:29:25 -0400307 kfree(logical);
308 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400309 return 0;
310}
311
Yan Zheng11833d62009-09-11 16:11:19 -0400312static struct btrfs_caching_control *
313get_caching_control(struct btrfs_block_group_cache *cache)
314{
315 struct btrfs_caching_control *ctl;
316
317 spin_lock(&cache->lock);
Josef Bacikdde5abe2010-09-16 16:17:03 -0400318 if (!cache->caching_ctl) {
319 spin_unlock(&cache->lock);
320 return NULL;
321 }
322
Yan Zheng11833d62009-09-11 16:11:19 -0400323 ctl = cache->caching_ctl;
324 atomic_inc(&ctl->count);
325 spin_unlock(&cache->lock);
326 return ctl;
327}
328
329static void put_caching_control(struct btrfs_caching_control *ctl)
330{
331 if (atomic_dec_and_test(&ctl->count))
332 kfree(ctl);
333}
334
Josef Bacikd0bd4562015-09-23 14:54:14 -0400335#ifdef CONFIG_BTRFS_DEBUG
336static void fragment_free_space(struct btrfs_root *root,
337 struct btrfs_block_group_cache *block_group)
338{
339 u64 start = block_group->key.objectid;
340 u64 len = block_group->key.offset;
341 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
342 root->nodesize : root->sectorsize;
343 u64 step = chunk << 1;
344
345 while (len > chunk) {
346 btrfs_remove_free_space(block_group, start, chunk);
347 start += step;
348 if (len < step)
349 len = 0;
350 else
351 len -= step;
352 }
353}
354#endif
355
Josef Bacik0f9dd462008-09-23 13:14:11 -0400356/*
357 * this is only called by cache_block_group, since we could have freed extents
358 * we need to check the pinned_extents for any extents that can't be used yet
359 * since their free space will be released as soon as the transaction commits.
360 */
Omar Sandovala5ed9182015-09-29 20:50:35 -0700361u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
362 struct btrfs_fs_info *info, u64 start, u64 end)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400363{
Josef Bacik817d52f2009-07-13 21:29:25 -0400364 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400365 int ret;
366
367 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400368 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400369 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400370 EXTENT_DIRTY | EXTENT_UPTODATE,
371 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400372 if (ret)
373 break;
374
Yan, Zheng06b23312009-11-26 09:31:11 +0000375 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400376 start = extent_end + 1;
377 } else if (extent_start > start && extent_start < end) {
378 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400379 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500380 ret = btrfs_add_free_space(block_group, start,
381 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100382 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400383 start = extent_end + 1;
384 } else {
385 break;
386 }
387 }
388
389 if (start < end) {
390 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400391 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500392 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100393 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400394 }
395
Josef Bacik817d52f2009-07-13 21:29:25 -0400396 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400397}
398
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700399static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
Chris Masone37c9e62007-05-09 20:13:14 -0400400{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400401 struct btrfs_block_group_cache *block_group;
402 struct btrfs_fs_info *fs_info;
Josef Bacikbab39bf2011-06-30 14:42:28 -0400403 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400404 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400405 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400406 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400407 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400408 u64 last = 0;
409 u32 nritems;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700410 int ret;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400411 bool wakeup = true;
Chris Masonf510cfe2007-10-15 16:14:48 -0400412
Josef Bacikbab39bf2011-06-30 14:42:28 -0400413 block_group = caching_ctl->block_group;
414 fs_info = block_group->fs_info;
415 extent_root = fs_info->extent_root;
416
Chris Masone37c9e62007-05-09 20:13:14 -0400417 path = btrfs_alloc_path();
418 if (!path)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700419 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400420
Josef Bacik817d52f2009-07-13 21:29:25 -0400421 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400422
Josef Bacikd0bd4562015-09-23 14:54:14 -0400423#ifdef CONFIG_BTRFS_DEBUG
424 /*
425 * If we're fragmenting we don't want to make anybody think we can
426 * allocate from this block group until we've had a chance to fragment
427 * the free space.
428 */
429 if (btrfs_should_fragment_free_space(extent_root, block_group))
430 wakeup = false;
431#endif
Chris Mason5cd57b22008-06-25 16:01:30 -0400432 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400433 * We don't want to deadlock with somebody trying to allocate a new
434 * extent for the extent root while also trying to search the extent
435 * root to add free space. So we skip locking and search the commit
436 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400437 */
438 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400439 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400440 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400441
Yan Zhenge4404d62008-12-12 10:03:26 -0500442 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400443 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400444 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400445
Liu Bo52ee28d2013-07-11 17:51:15 +0800446next:
Yan Zheng11833d62009-09-11 16:11:19 -0400447 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400448 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700449 goto out;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500450
Yan Zheng11833d62009-09-11 16:11:19 -0400451 leaf = path->nodes[0];
452 nritems = btrfs_header_nritems(leaf);
453
Chris Masond3977122009-01-05 21:25:51 -0500454 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200455 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400456 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400457 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400458 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400459
Yan Zheng11833d62009-09-11 16:11:19 -0400460 if (path->slots[0] < nritems) {
461 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
462 } else {
463 ret = find_next_key(path, 0, &key);
464 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400465 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400466
Josef Bacikc9ea7b22013-09-19 10:02:11 -0400467 if (need_resched() ||
Josef Bacik9e351cc2014-03-13 15:42:13 -0400468 rwsem_is_contended(&fs_info->commit_root_sem)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400469 if (wakeup)
470 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400471 btrfs_release_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400472 up_read(&fs_info->commit_root_sem);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400473 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400474 cond_resched();
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700475 mutex_lock(&caching_ctl->mutex);
476 down_read(&fs_info->commit_root_sem);
477 goto next;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400478 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400479
480 ret = btrfs_next_leaf(extent_root, path);
481 if (ret < 0)
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700482 goto out;
Josef Bacik0a3896d2013-04-19 14:37:26 -0400483 if (ret)
484 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400485 leaf = path->nodes[0];
486 nritems = btrfs_header_nritems(leaf);
487 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400488 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400489
Liu Bo52ee28d2013-07-11 17:51:15 +0800490 if (key.objectid < last) {
491 key.objectid = last;
492 key.offset = 0;
493 key.type = BTRFS_EXTENT_ITEM_KEY;
494
Josef Bacikd0bd4562015-09-23 14:54:14 -0400495 if (wakeup)
496 caching_ctl->progress = last;
Liu Bo52ee28d2013-07-11 17:51:15 +0800497 btrfs_release_path(path);
498 goto next;
499 }
500
Yan Zheng11833d62009-09-11 16:11:19 -0400501 if (key.objectid < block_group->key.objectid) {
502 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400503 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400504 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400505
Chris Masone37c9e62007-05-09 20:13:14 -0400506 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400507 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400508 break;
Yan7d7d6062007-09-14 16:15:28 -0400509
Josef Bacik3173a182013-03-07 14:22:04 -0500510 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
511 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400512 total_found += add_new_free_space(block_group,
513 fs_info, last,
514 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500515 if (key.type == BTRFS_METADATA_ITEM_KEY)
516 last = key.objectid +
David Sterba707e8a02014-06-04 19:22:26 +0200517 fs_info->tree_root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500518 else
519 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400520
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700521 if (total_found > CACHING_CTL_WAKE_UP) {
Yan Zheng11833d62009-09-11 16:11:19 -0400522 total_found = 0;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400523 if (wakeup)
524 wake_up(&caching_ctl->wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400525 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400526 }
Chris Masone37c9e62007-05-09 20:13:14 -0400527 path->slots[0]++;
528 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400529 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400530
531 total_found += add_new_free_space(block_group, fs_info, last,
532 block_group->key.objectid +
533 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400534 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400535
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700536out:
537 btrfs_free_path(path);
538 return ret;
539}
540
541static noinline void caching_thread(struct btrfs_work *work)
542{
543 struct btrfs_block_group_cache *block_group;
544 struct btrfs_fs_info *fs_info;
545 struct btrfs_caching_control *caching_ctl;
546 int ret;
547
548 caching_ctl = container_of(work, struct btrfs_caching_control, work);
549 block_group = caching_ctl->block_group;
550 fs_info = block_group->fs_info;
551
552 mutex_lock(&caching_ctl->mutex);
553 down_read(&fs_info->commit_root_sem);
554
Omar Sandoval1e144fb2015-09-29 20:50:37 -0700555 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
556 ret = load_free_space_tree(caching_ctl);
557 else
558 ret = load_extent_tree_free(caching_ctl);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700559
Josef Bacik817d52f2009-07-13 21:29:25 -0400560 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400561 block_group->caching_ctl = NULL;
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700562 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED;
Josef Bacik817d52f2009-07-13 21:29:25 -0400563 spin_unlock(&block_group->lock);
564
Josef Bacikd0bd4562015-09-23 14:54:14 -0400565#ifdef CONFIG_BTRFS_DEBUG
566 if (btrfs_should_fragment_free_space(extent_root, block_group)) {
567 u64 bytes_used;
568
569 spin_lock(&block_group->space_info->lock);
570 spin_lock(&block_group->lock);
571 bytes_used = block_group->key.offset -
572 btrfs_block_group_used(&block_group->item);
573 block_group->space_info->bytes_used += bytes_used >> 1;
574 spin_unlock(&block_group->lock);
575 spin_unlock(&block_group->space_info->lock);
576 fragment_free_space(extent_root, block_group);
577 }
578#endif
579
580 caching_ctl->progress = (u64)-1;
Chris Masonf7d3d2f2015-12-18 11:11:10 -0800581
Josef Bacik9e351cc2014-03-13 15:42:13 -0400582 up_read(&fs_info->commit_root_sem);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700583 free_excluded_extents(fs_info->extent_root, block_group);
Yan Zheng11833d62009-09-11 16:11:19 -0400584 mutex_unlock(&caching_ctl->mutex);
Omar Sandoval73fa48b2015-09-29 20:50:33 -0700585
Yan Zheng11833d62009-09-11 16:11:19 -0400586 wake_up(&caching_ctl->wait);
587
588 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000589 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400590}
591
Josef Bacik9d66e232010-08-25 16:54:15 -0400592static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400593 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400594{
Josef Bacik291c7d22011-11-14 13:52:14 -0500595 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400596 struct btrfs_fs_info *fs_info = cache->fs_info;
597 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400598 int ret = 0;
599
Josef Bacik291c7d22011-11-14 13:52:14 -0500600 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100601 if (!caching_ctl)
602 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500603
604 INIT_LIST_HEAD(&caching_ctl->list);
605 mutex_init(&caching_ctl->mutex);
606 init_waitqueue_head(&caching_ctl->wait);
607 caching_ctl->block_group = cache;
608 caching_ctl->progress = cache->key.objectid;
609 atomic_set(&caching_ctl->count, 1);
Liu Bo9e0af232014-08-15 23:36:53 +0800610 btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
611 caching_thread, NULL, NULL);
Josef Bacik291c7d22011-11-14 13:52:14 -0500612
613 spin_lock(&cache->lock);
614 /*
615 * This should be a rare occasion, but this could happen I think in the
616 * case where one thread starts to load the space cache info, and then
617 * some other thread starts a transaction commit which tries to do an
618 * allocation while the other thread is still loading the space cache
619 * info. The previous loop should have kept us from choosing this block
620 * group, but if we've moved to the state where we will wait on caching
621 * block groups we need to first check if we're doing a fast load here,
622 * so we can wait for it to finish, otherwise we could end up allocating
623 * from a block group who's cache gets evicted for one reason or
624 * another.
625 */
626 while (cache->cached == BTRFS_CACHE_FAST) {
627 struct btrfs_caching_control *ctl;
628
629 ctl = cache->caching_ctl;
630 atomic_inc(&ctl->count);
631 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
632 spin_unlock(&cache->lock);
633
634 schedule();
635
636 finish_wait(&ctl->wait, &wait);
637 put_caching_control(ctl);
638 spin_lock(&cache->lock);
639 }
640
641 if (cache->cached != BTRFS_CACHE_NO) {
642 spin_unlock(&cache->lock);
643 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400644 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500645 }
646 WARN_ON(cache->caching_ctl);
647 cache->caching_ctl = caching_ctl;
648 cache->cached = BTRFS_CACHE_FAST;
649 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400650
Josef Bacikd53ba472012-04-12 16:03:57 -0400651 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500652 mutex_lock(&caching_ctl->mutex);
Josef Bacik9d66e232010-08-25 16:54:15 -0400653 ret = load_free_space_cache(fs_info, cache);
654
655 spin_lock(&cache->lock);
656 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500657 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400658 cache->cached = BTRFS_CACHE_FINISHED;
659 cache->last_byte_to_unpin = (u64)-1;
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500660 caching_ctl->progress = (u64)-1;
Josef Bacik9d66e232010-08-25 16:54:15 -0400661 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500662 if (load_cache_only) {
663 cache->caching_ctl = NULL;
664 cache->cached = BTRFS_CACHE_NO;
665 } else {
666 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000667 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500668 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400669 }
670 spin_unlock(&cache->lock);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400671#ifdef CONFIG_BTRFS_DEBUG
672 if (ret == 1 &&
673 btrfs_should_fragment_free_space(fs_info->extent_root,
674 cache)) {
675 u64 bytes_used;
676
677 spin_lock(&cache->space_info->lock);
678 spin_lock(&cache->lock);
679 bytes_used = cache->key.offset -
680 btrfs_block_group_used(&cache->item);
681 cache->space_info->bytes_used += bytes_used >> 1;
682 spin_unlock(&cache->lock);
683 spin_unlock(&cache->space_info->lock);
684 fragment_free_space(fs_info->extent_root, cache);
685 }
686#endif
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500687 mutex_unlock(&caching_ctl->mutex);
688
Josef Bacik291c7d22011-11-14 13:52:14 -0500689 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000690 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500691 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000692 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400693 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000694 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500695 } else {
696 /*
Omar Sandoval1e144fb2015-09-29 20:50:37 -0700697 * We're either using the free space tree or no caching at all.
698 * Set cached to the appropriate value and wakeup any waiters.
Josef Bacik291c7d22011-11-14 13:52:14 -0500699 */
700 spin_lock(&cache->lock);
701 if (load_cache_only) {
702 cache->caching_ctl = NULL;
703 cache->cached = BTRFS_CACHE_NO;
704 } else {
705 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000706 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500707 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400708 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500709 wake_up(&caching_ctl->wait);
710 }
711
712 if (load_cache_only) {
713 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400714 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400715 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400716
Josef Bacik9e351cc2014-03-13 15:42:13 -0400717 down_write(&fs_info->commit_root_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500718 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400719 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400720 up_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -0400721
Josef Bacik11dfe352009-11-13 20:12:59 +0000722 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400723
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800724 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400725
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400726 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400727}
728
Josef Bacik0f9dd462008-09-23 13:14:11 -0400729/*
730 * return the block group that starts at or after bytenr
731 */
Chris Masond3977122009-01-05 21:25:51 -0500732static struct btrfs_block_group_cache *
733btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400734{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400735 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400736
Josef Bacik0f9dd462008-09-23 13:14:11 -0400737 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400738
Josef Bacik0f9dd462008-09-23 13:14:11 -0400739 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400740}
741
Josef Bacik0f9dd462008-09-23 13:14:11 -0400742/*
Sankar P9f556842009-05-14 13:52:22 -0400743 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400744 */
Chris Masond3977122009-01-05 21:25:51 -0500745struct btrfs_block_group_cache *btrfs_lookup_block_group(
746 struct btrfs_fs_info *info,
747 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400748{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400749 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400750
Josef Bacik0f9dd462008-09-23 13:14:11 -0400751 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400752
Josef Bacik0f9dd462008-09-23 13:14:11 -0400753 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400754}
Chris Mason0b86a832008-03-24 15:01:56 -0400755
Josef Bacik0f9dd462008-09-23 13:14:11 -0400756static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
757 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400758{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400759 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400760 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400761
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200762 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb822010-05-16 10:46:24 -0400763
Chris Mason4184ea72009-03-10 12:39:20 -0400764 rcu_read_lock();
765 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400766 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400767 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400768 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400769 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400770 }
Chris Mason4184ea72009-03-10 12:39:20 -0400771 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400772 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400773}
774
Chris Mason4184ea72009-03-10 12:39:20 -0400775/*
776 * after adding space to the filesystem, we need to clear the full flags
777 * on all the space infos.
778 */
779void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
780{
781 struct list_head *head = &info->space_info;
782 struct btrfs_space_info *found;
783
784 rcu_read_lock();
785 list_for_each_entry_rcu(found, head, list)
786 found->full = 0;
787 rcu_read_unlock();
788}
789
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000790/* simple helper to search for an existing data extent at a given offset */
791int btrfs_lookup_data_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400792{
793 int ret;
794 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400795 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400796
Zheng Yan31840ae2008-09-23 13:14:14 -0400797 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700798 if (!path)
799 return -ENOMEM;
800
Chris Masone02119d2008-09-05 16:13:11 -0400801 key.objectid = start;
802 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500803 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400804 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
805 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400806 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500807 return ret;
808}
809
Chris Masond8d5f3e2007-12-11 12:42:00 -0500810/*
Josef Bacik3173a182013-03-07 14:22:04 -0500811 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400812 *
813 * the head node for delayed ref is used to store the sum of all the
814 * reference count modifications queued up in the rbtree. the head
815 * node may also store the extent flags to set. This way you can check
816 * to see what the reference count and extent flags would be if all of
817 * the delayed refs are not processed.
818 */
819int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
820 struct btrfs_root *root, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500821 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400822{
823 struct btrfs_delayed_ref_head *head;
824 struct btrfs_delayed_ref_root *delayed_refs;
825 struct btrfs_path *path;
826 struct btrfs_extent_item *ei;
827 struct extent_buffer *leaf;
828 struct btrfs_key key;
829 u32 item_size;
830 u64 num_refs;
831 u64 extent_flags;
832 int ret;
833
Josef Bacik3173a182013-03-07 14:22:04 -0500834 /*
835 * If we don't have skinny metadata, don't bother doing anything
836 * different
837 */
838 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
David Sterba707e8a02014-06-04 19:22:26 +0200839 offset = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500840 metadata = 0;
841 }
842
Yan, Zhenga22285a2010-05-16 10:48:46 -0400843 path = btrfs_alloc_path();
844 if (!path)
845 return -ENOMEM;
846
Yan, Zhenga22285a2010-05-16 10:48:46 -0400847 if (!trans) {
848 path->skip_locking = 1;
849 path->search_commit_root = 1;
850 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000851
852search_again:
853 key.objectid = bytenr;
854 key.offset = offset;
855 if (metadata)
856 key.type = BTRFS_METADATA_ITEM_KEY;
857 else
858 key.type = BTRFS_EXTENT_ITEM_KEY;
859
Yan, Zhenga22285a2010-05-16 10:48:46 -0400860 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
861 &key, path, 0, 0);
862 if (ret < 0)
863 goto out_free;
864
Josef Bacik3173a182013-03-07 14:22:04 -0500865 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100866 if (path->slots[0]) {
867 path->slots[0]--;
868 btrfs_item_key_to_cpu(path->nodes[0], &key,
869 path->slots[0]);
870 if (key.objectid == bytenr &&
871 key.type == BTRFS_EXTENT_ITEM_KEY &&
David Sterba707e8a02014-06-04 19:22:26 +0200872 key.offset == root->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100873 ret = 0;
874 }
Josef Bacik3173a182013-03-07 14:22:04 -0500875 }
876
Yan, Zhenga22285a2010-05-16 10:48:46 -0400877 if (ret == 0) {
878 leaf = path->nodes[0];
879 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
880 if (item_size >= sizeof(*ei)) {
881 ei = btrfs_item_ptr(leaf, path->slots[0],
882 struct btrfs_extent_item);
883 num_refs = btrfs_extent_refs(leaf, ei);
884 extent_flags = btrfs_extent_flags(leaf, ei);
885 } else {
886#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
887 struct btrfs_extent_item_v0 *ei0;
888 BUG_ON(item_size != sizeof(*ei0));
889 ei0 = btrfs_item_ptr(leaf, path->slots[0],
890 struct btrfs_extent_item_v0);
891 num_refs = btrfs_extent_refs_v0(leaf, ei0);
892 /* FIXME: this isn't correct for data */
893 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
894#else
895 BUG();
896#endif
897 }
898 BUG_ON(num_refs == 0);
899 } else {
900 num_refs = 0;
901 extent_flags = 0;
902 ret = 0;
903 }
904
905 if (!trans)
906 goto out;
907
908 delayed_refs = &trans->transaction->delayed_refs;
909 spin_lock(&delayed_refs->lock);
910 head = btrfs_find_delayed_ref_head(trans, bytenr);
911 if (head) {
912 if (!mutex_trylock(&head->mutex)) {
913 atomic_inc(&head->node.refs);
914 spin_unlock(&delayed_refs->lock);
915
David Sterbab3b4aa72011-04-21 01:20:15 +0200916 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400917
David Sterba8cc33e52011-05-02 15:29:25 +0200918 /*
919 * Mutex was contended, block until it's released and try
920 * again
921 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400922 mutex_lock(&head->mutex);
923 mutex_unlock(&head->mutex);
924 btrfs_put_delayed_ref(&head->node);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000925 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400926 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500927 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400928 if (head->extent_op && head->extent_op->update_flags)
929 extent_flags |= head->extent_op->flags_to_set;
930 else
931 BUG_ON(num_refs == 0);
932
933 num_refs += head->node.ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500934 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400935 mutex_unlock(&head->mutex);
936 }
937 spin_unlock(&delayed_refs->lock);
938out:
939 WARN_ON(num_refs == 0);
940 if (refs)
941 *refs = num_refs;
942 if (flags)
943 *flags = extent_flags;
944out_free:
945 btrfs_free_path(path);
946 return ret;
947}
948
949/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500950 * Back reference rules. Back refs have three main goals:
951 *
952 * 1) differentiate between all holders of references to an extent so that
953 * when a reference is dropped we can make sure it was a valid reference
954 * before freeing the extent.
955 *
956 * 2) Provide enough information to quickly find the holders of an extent
957 * if we notice a given block is corrupted or bad.
958 *
959 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
960 * maintenance. This is actually the same as #2, but with a slightly
961 * different use case.
962 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400963 * There are two kinds of back refs. The implicit back refs is optimized
964 * for pointers in non-shared tree blocks. For a given pointer in a block,
965 * back refs of this kind provide information about the block's owner tree
966 * and the pointer's key. These information allow us to find the block by
967 * b-tree searching. The full back refs is for pointers in tree blocks not
968 * referenced by their owner trees. The location of tree block is recorded
969 * in the back refs. Actually the full back refs is generic, and can be
970 * used in all cases the implicit back refs is used. The major shortcoming
971 * of the full back refs is its overhead. Every time a tree block gets
972 * COWed, we have to update back refs entry for all pointers in it.
973 *
974 * For a newly allocated tree block, we use implicit back refs for
975 * pointers in it. This means most tree related operations only involve
976 * implicit back refs. For a tree block created in old transaction, the
977 * only way to drop a reference to it is COW it. So we can detect the
978 * event that tree block loses its owner tree's reference and do the
979 * back refs conversion.
980 *
981 * When a tree block is COW'd through a tree, there are four cases:
982 *
983 * The reference count of the block is one and the tree is the block's
984 * owner tree. Nothing to do in this case.
985 *
986 * The reference count of the block is one and the tree is not the
987 * block's owner tree. In this case, full back refs is used for pointers
988 * in the block. Remove these full back refs, add implicit back refs for
989 * every pointers in the new block.
990 *
991 * The reference count of the block is greater than one and the tree is
992 * the block's owner tree. In this case, implicit back refs is used for
993 * pointers in the block. Add full back refs for every pointers in the
994 * block, increase lower level extents' reference counts. The original
995 * implicit back refs are entailed to the new block.
996 *
997 * The reference count of the block is greater than one and the tree is
998 * not the block's owner tree. Add implicit back refs for every pointer in
999 * the new block, increase lower level extents' reference count.
1000 *
1001 * Back Reference Key composing:
1002 *
1003 * The key objectid corresponds to the first byte in the extent,
1004 * The key type is used to differentiate between types of back refs.
1005 * There are different meanings of the key offset for different types
1006 * of back refs.
1007 *
Chris Masond8d5f3e2007-12-11 12:42:00 -05001008 * File extents can be referenced by:
1009 *
1010 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -04001011 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -05001012 * - different offsets inside a file (bookend extents in file.c)
1013 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001014 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001015 *
1016 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -05001017 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001018 * - original offset in the file
1019 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -04001020 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021 * The key offset for the implicit back refs is hash of the first
1022 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001023 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001024 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001025 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001026 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001027 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001028 * The key offset for the implicit back refs is the first byte of
1029 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001030 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001031 * When a file extent is allocated, The implicit back refs is used.
1032 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001033 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001034 * (root_key.objectid, inode objectid, offset in file, 1)
1035 *
1036 * When a file extent is removed file truncation, we find the
1037 * corresponding implicit back refs and check the following fields:
1038 *
1039 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -05001040 *
1041 * Btree extents can be referenced by:
1042 *
1043 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -05001044 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001045 * Both the implicit back refs and the full back refs for tree blocks
1046 * only consist of key. The key offset for the implicit back refs is
1047 * objectid of block's owner tree. The key offset for the full back refs
1048 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001049 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001050 * When implicit back refs is used, information about the lowest key and
1051 * level of the tree block are required. These information are stored in
1052 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001053 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001054
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001055#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1056static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
1057 struct btrfs_root *root,
1058 struct btrfs_path *path,
1059 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -05001060{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001061 struct btrfs_extent_item *item;
1062 struct btrfs_extent_item_v0 *ei0;
1063 struct btrfs_extent_ref_v0 *ref0;
1064 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -04001065 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001066 struct btrfs_key key;
1067 struct btrfs_key found_key;
1068 u32 new_size = sizeof(*item);
1069 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -05001070 int ret;
1071
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001072 leaf = path->nodes[0];
1073 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -05001074
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001075 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1076 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1077 struct btrfs_extent_item_v0);
1078 refs = btrfs_extent_refs_v0(leaf, ei0);
1079
1080 if (owner == (u64)-1) {
1081 while (1) {
1082 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1083 ret = btrfs_next_leaf(root, path);
1084 if (ret < 0)
1085 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001086 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001087 leaf = path->nodes[0];
1088 }
1089 btrfs_item_key_to_cpu(leaf, &found_key,
1090 path->slots[0]);
1091 BUG_ON(key.objectid != found_key.objectid);
1092 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1093 path->slots[0]++;
1094 continue;
1095 }
1096 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1097 struct btrfs_extent_ref_v0);
1098 owner = btrfs_ref_objectid_v0(leaf, ref0);
1099 break;
1100 }
1101 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001102 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001103
1104 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1105 new_size += sizeof(*bi);
1106
1107 new_size -= sizeof(*ei0);
1108 ret = btrfs_search_slot(trans, root, &key, path,
1109 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001110 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001111 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001112 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001113
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001114 btrfs_extend_item(root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001115
1116 leaf = path->nodes[0];
1117 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1118 btrfs_set_extent_refs(leaf, item, refs);
1119 /* FIXME: get real generation */
1120 btrfs_set_extent_generation(leaf, item, 0);
1121 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1122 btrfs_set_extent_flags(leaf, item,
1123 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1124 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1125 bi = (struct btrfs_tree_block_info *)(item + 1);
1126 /* FIXME: get first key of the block */
1127 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1128 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1129 } else {
1130 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1131 }
1132 btrfs_mark_buffer_dirty(leaf);
1133 return 0;
1134}
1135#endif
1136
1137static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1138{
1139 u32 high_crc = ~(u32)0;
1140 u32 low_crc = ~(u32)0;
1141 __le64 lenum;
1142
1143 lenum = cpu_to_le64(root_objectid);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001144 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001145 lenum = cpu_to_le64(owner);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001146 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001147 lenum = cpu_to_le64(offset);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001148 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001149
1150 return ((u64)high_crc << 31) ^ (u64)low_crc;
1151}
1152
1153static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1154 struct btrfs_extent_data_ref *ref)
1155{
1156 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1157 btrfs_extent_data_ref_objectid(leaf, ref),
1158 btrfs_extent_data_ref_offset(leaf, ref));
1159}
1160
1161static int match_extent_data_ref(struct extent_buffer *leaf,
1162 struct btrfs_extent_data_ref *ref,
1163 u64 root_objectid, u64 owner, u64 offset)
1164{
1165 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1166 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1167 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1168 return 0;
1169 return 1;
1170}
1171
1172static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1173 struct btrfs_root *root,
1174 struct btrfs_path *path,
1175 u64 bytenr, u64 parent,
1176 u64 root_objectid,
1177 u64 owner, u64 offset)
1178{
1179 struct btrfs_key key;
1180 struct btrfs_extent_data_ref *ref;
1181 struct extent_buffer *leaf;
1182 u32 nritems;
1183 int ret;
1184 int recow;
1185 int err = -ENOENT;
1186
1187 key.objectid = bytenr;
1188 if (parent) {
1189 key.type = BTRFS_SHARED_DATA_REF_KEY;
1190 key.offset = parent;
1191 } else {
1192 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1193 key.offset = hash_extent_data_ref(root_objectid,
1194 owner, offset);
1195 }
1196again:
1197 recow = 0;
1198 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1199 if (ret < 0) {
1200 err = ret;
1201 goto fail;
1202 }
1203
1204 if (parent) {
1205 if (!ret)
1206 return 0;
1207#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1208 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001209 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001210 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1211 if (ret < 0) {
1212 err = ret;
1213 goto fail;
1214 }
1215 if (!ret)
1216 return 0;
1217#endif
1218 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001219 }
1220
1221 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001222 nritems = btrfs_header_nritems(leaf);
1223 while (1) {
1224 if (path->slots[0] >= nritems) {
1225 ret = btrfs_next_leaf(root, path);
1226 if (ret < 0)
1227 err = ret;
1228 if (ret)
1229 goto fail;
1230
1231 leaf = path->nodes[0];
1232 nritems = btrfs_header_nritems(leaf);
1233 recow = 1;
1234 }
1235
1236 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1237 if (key.objectid != bytenr ||
1238 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1239 goto fail;
1240
1241 ref = btrfs_item_ptr(leaf, path->slots[0],
1242 struct btrfs_extent_data_ref);
1243
1244 if (match_extent_data_ref(leaf, ref, root_objectid,
1245 owner, offset)) {
1246 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001247 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001248 goto again;
1249 }
1250 err = 0;
1251 break;
1252 }
1253 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001254 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001255fail:
1256 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001257}
1258
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001259static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1260 struct btrfs_root *root,
1261 struct btrfs_path *path,
1262 u64 bytenr, u64 parent,
1263 u64 root_objectid, u64 owner,
1264 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001265{
1266 struct btrfs_key key;
1267 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001268 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001269 u32 num_refs;
1270 int ret;
1271
1272 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001273 if (parent) {
1274 key.type = BTRFS_SHARED_DATA_REF_KEY;
1275 key.offset = parent;
1276 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001277 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001278 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1279 key.offset = hash_extent_data_ref(root_objectid,
1280 owner, offset);
1281 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001282 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001283
1284 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1285 if (ret && ret != -EEXIST)
1286 goto fail;
1287
1288 leaf = path->nodes[0];
1289 if (parent) {
1290 struct btrfs_shared_data_ref *ref;
1291 ref = btrfs_item_ptr(leaf, path->slots[0],
1292 struct btrfs_shared_data_ref);
1293 if (ret == 0) {
1294 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1295 } else {
1296 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1297 num_refs += refs_to_add;
1298 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1299 }
1300 } else {
1301 struct btrfs_extent_data_ref *ref;
1302 while (ret == -EEXIST) {
1303 ref = btrfs_item_ptr(leaf, path->slots[0],
1304 struct btrfs_extent_data_ref);
1305 if (match_extent_data_ref(leaf, ref, root_objectid,
1306 owner, offset))
1307 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001308 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001309 key.offset++;
1310 ret = btrfs_insert_empty_item(trans, root, path, &key,
1311 size);
1312 if (ret && ret != -EEXIST)
1313 goto fail;
1314
1315 leaf = path->nodes[0];
1316 }
1317 ref = btrfs_item_ptr(leaf, path->slots[0],
1318 struct btrfs_extent_data_ref);
1319 if (ret == 0) {
1320 btrfs_set_extent_data_ref_root(leaf, ref,
1321 root_objectid);
1322 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1323 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1324 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1325 } else {
1326 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1327 num_refs += refs_to_add;
1328 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1329 }
1330 }
1331 btrfs_mark_buffer_dirty(leaf);
1332 ret = 0;
1333fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001334 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001335 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001336}
1337
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001338static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1339 struct btrfs_root *root,
1340 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -07001341 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001342{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001343 struct btrfs_key key;
1344 struct btrfs_extent_data_ref *ref1 = NULL;
1345 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001346 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001347 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001348 int ret = 0;
1349
1350 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001351 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1352
1353 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1354 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1355 struct btrfs_extent_data_ref);
1356 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1357 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1358 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1359 struct btrfs_shared_data_ref);
1360 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1361#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1362 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1363 struct btrfs_extent_ref_v0 *ref0;
1364 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1365 struct btrfs_extent_ref_v0);
1366 num_refs = btrfs_ref_count_v0(leaf, ref0);
1367#endif
1368 } else {
1369 BUG();
1370 }
1371
Chris Mason56bec292009-03-13 10:10:06 -04001372 BUG_ON(num_refs < refs_to_drop);
1373 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001374
Zheng Yan31840ae2008-09-23 13:14:14 -04001375 if (num_refs == 0) {
1376 ret = btrfs_del_item(trans, root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -07001377 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001378 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001379 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1380 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1381 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1382 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1383#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1384 else {
1385 struct btrfs_extent_ref_v0 *ref0;
1386 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1387 struct btrfs_extent_ref_v0);
1388 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1389 }
1390#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001391 btrfs_mark_buffer_dirty(leaf);
1392 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001393 return ret;
1394}
1395
Zhaolei9ed0dea2015-08-06 22:16:24 +08001396static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397 struct btrfs_extent_inline_ref *iref)
1398{
1399 struct btrfs_key key;
1400 struct extent_buffer *leaf;
1401 struct btrfs_extent_data_ref *ref1;
1402 struct btrfs_shared_data_ref *ref2;
1403 u32 num_refs = 0;
1404
1405 leaf = path->nodes[0];
1406 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1407 if (iref) {
1408 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1409 BTRFS_EXTENT_DATA_REF_KEY) {
1410 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1411 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1412 } else {
1413 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1414 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1415 }
1416 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1417 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1418 struct btrfs_extent_data_ref);
1419 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1420 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1421 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1422 struct btrfs_shared_data_ref);
1423 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1424#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1425 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1426 struct btrfs_extent_ref_v0 *ref0;
1427 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1428 struct btrfs_extent_ref_v0);
1429 num_refs = btrfs_ref_count_v0(leaf, ref0);
1430#endif
1431 } else {
1432 WARN_ON(1);
1433 }
1434 return num_refs;
1435}
1436
1437static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1438 struct btrfs_root *root,
1439 struct btrfs_path *path,
1440 u64 bytenr, u64 parent,
1441 u64 root_objectid)
1442{
1443 struct btrfs_key key;
1444 int ret;
1445
1446 key.objectid = bytenr;
1447 if (parent) {
1448 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1449 key.offset = parent;
1450 } else {
1451 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1452 key.offset = root_objectid;
1453 }
1454
1455 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1456 if (ret > 0)
1457 ret = -ENOENT;
1458#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1459 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001460 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001461 key.type = BTRFS_EXTENT_REF_V0_KEY;
1462 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1463 if (ret > 0)
1464 ret = -ENOENT;
1465 }
1466#endif
1467 return ret;
1468}
1469
1470static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1471 struct btrfs_root *root,
1472 struct btrfs_path *path,
1473 u64 bytenr, u64 parent,
1474 u64 root_objectid)
1475{
1476 struct btrfs_key key;
1477 int ret;
1478
1479 key.objectid = bytenr;
1480 if (parent) {
1481 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1482 key.offset = parent;
1483 } else {
1484 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1485 key.offset = root_objectid;
1486 }
1487
1488 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001489 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001490 return ret;
1491}
1492
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001493static inline int extent_ref_type(u64 parent, u64 owner)
1494{
1495 int type;
1496 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1497 if (parent > 0)
1498 type = BTRFS_SHARED_BLOCK_REF_KEY;
1499 else
1500 type = BTRFS_TREE_BLOCK_REF_KEY;
1501 } else {
1502 if (parent > 0)
1503 type = BTRFS_SHARED_DATA_REF_KEY;
1504 else
1505 type = BTRFS_EXTENT_DATA_REF_KEY;
1506 }
1507 return type;
1508}
1509
Yan Zheng2c47e6052009-06-27 21:07:35 -04001510static int find_next_key(struct btrfs_path *path, int level,
1511 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001512
1513{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001514 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001515 if (!path->nodes[level])
1516 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001517 if (path->slots[level] + 1 >=
1518 btrfs_header_nritems(path->nodes[level]))
1519 continue;
1520 if (level == 0)
1521 btrfs_item_key_to_cpu(path->nodes[level], key,
1522 path->slots[level] + 1);
1523 else
1524 btrfs_node_key_to_cpu(path->nodes[level], key,
1525 path->slots[level] + 1);
1526 return 0;
1527 }
1528 return 1;
1529}
1530
1531/*
1532 * look for inline back ref. if back ref is found, *ref_ret is set
1533 * to the address of inline back ref, and 0 is returned.
1534 *
1535 * if back ref isn't found, *ref_ret is set to the address where it
1536 * should be inserted, and -ENOENT is returned.
1537 *
1538 * if insert is true and there are too many inline back refs, the path
1539 * points to the extent item, and -EAGAIN is returned.
1540 *
1541 * NOTE: inline back refs are ordered in the same way that back ref
1542 * items in the tree are ordered.
1543 */
1544static noinline_for_stack
1545int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1546 struct btrfs_root *root,
1547 struct btrfs_path *path,
1548 struct btrfs_extent_inline_ref **ref_ret,
1549 u64 bytenr, u64 num_bytes,
1550 u64 parent, u64 root_objectid,
1551 u64 owner, u64 offset, int insert)
1552{
1553 struct btrfs_key key;
1554 struct extent_buffer *leaf;
1555 struct btrfs_extent_item *ei;
1556 struct btrfs_extent_inline_ref *iref;
1557 u64 flags;
1558 u64 item_size;
1559 unsigned long ptr;
1560 unsigned long end;
1561 int extra_size;
1562 int type;
1563 int want;
1564 int ret;
1565 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001566 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1567 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001568
1569 key.objectid = bytenr;
1570 key.type = BTRFS_EXTENT_ITEM_KEY;
1571 key.offset = num_bytes;
1572
1573 want = extent_ref_type(parent, owner);
1574 if (insert) {
1575 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001576 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001577 } else
1578 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001579
1580 /*
1581 * Owner is our parent level, so we can just add one to get the level
1582 * for the block we are interested in.
1583 */
1584 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1585 key.type = BTRFS_METADATA_ITEM_KEY;
1586 key.offset = owner;
1587 }
1588
1589again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001590 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1591 if (ret < 0) {
1592 err = ret;
1593 goto out;
1594 }
Josef Bacik3173a182013-03-07 14:22:04 -05001595
1596 /*
1597 * We may be a newly converted file system which still has the old fat
1598 * extent entries for metadata, so try and see if we have one of those.
1599 */
1600 if (ret > 0 && skinny_metadata) {
1601 skinny_metadata = false;
1602 if (path->slots[0]) {
1603 path->slots[0]--;
1604 btrfs_item_key_to_cpu(path->nodes[0], &key,
1605 path->slots[0]);
1606 if (key.objectid == bytenr &&
1607 key.type == BTRFS_EXTENT_ITEM_KEY &&
1608 key.offset == num_bytes)
1609 ret = 0;
1610 }
1611 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +01001612 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05001613 key.type = BTRFS_EXTENT_ITEM_KEY;
1614 key.offset = num_bytes;
1615 btrfs_release_path(path);
1616 goto again;
1617 }
1618 }
1619
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001620 if (ret && !insert) {
1621 err = -ENOENT;
1622 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301623 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -05001624 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -05001625 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001626 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001627
1628 leaf = path->nodes[0];
1629 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1630#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1631 if (item_size < sizeof(*ei)) {
1632 if (!insert) {
1633 err = -ENOENT;
1634 goto out;
1635 }
1636 ret = convert_extent_item_v0(trans, root, path, owner,
1637 extra_size);
1638 if (ret < 0) {
1639 err = ret;
1640 goto out;
1641 }
1642 leaf = path->nodes[0];
1643 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1644 }
1645#endif
1646 BUG_ON(item_size < sizeof(*ei));
1647
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001648 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1649 flags = btrfs_extent_flags(leaf, ei);
1650
1651 ptr = (unsigned long)(ei + 1);
1652 end = (unsigned long)ei + item_size;
1653
Josef Bacik3173a182013-03-07 14:22:04 -05001654 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001655 ptr += sizeof(struct btrfs_tree_block_info);
1656 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001657 }
1658
1659 err = -ENOENT;
1660 while (1) {
1661 if (ptr >= end) {
1662 WARN_ON(ptr > end);
1663 break;
1664 }
1665 iref = (struct btrfs_extent_inline_ref *)ptr;
1666 type = btrfs_extent_inline_ref_type(leaf, iref);
1667 if (want < type)
1668 break;
1669 if (want > type) {
1670 ptr += btrfs_extent_inline_ref_size(type);
1671 continue;
1672 }
1673
1674 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1675 struct btrfs_extent_data_ref *dref;
1676 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1677 if (match_extent_data_ref(leaf, dref, root_objectid,
1678 owner, offset)) {
1679 err = 0;
1680 break;
1681 }
1682 if (hash_extent_data_ref_item(leaf, dref) <
1683 hash_extent_data_ref(root_objectid, owner, offset))
1684 break;
1685 } else {
1686 u64 ref_offset;
1687 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1688 if (parent > 0) {
1689 if (parent == ref_offset) {
1690 err = 0;
1691 break;
1692 }
1693 if (ref_offset < parent)
1694 break;
1695 } else {
1696 if (root_objectid == ref_offset) {
1697 err = 0;
1698 break;
1699 }
1700 if (ref_offset < root_objectid)
1701 break;
1702 }
1703 }
1704 ptr += btrfs_extent_inline_ref_size(type);
1705 }
1706 if (err == -ENOENT && insert) {
1707 if (item_size + extra_size >=
1708 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1709 err = -EAGAIN;
1710 goto out;
1711 }
1712 /*
1713 * To add new inline back ref, we have to make sure
1714 * there is no corresponding back ref item.
1715 * For simplicity, we just do not add new inline back
1716 * ref if there is any kind of item for this block
1717 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001718 if (find_next_key(path, 0, &key) == 0 &&
1719 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001720 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001721 err = -EAGAIN;
1722 goto out;
1723 }
1724 }
1725 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1726out:
Yan Zheng85d41982009-06-11 08:51:10 -04001727 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001728 path->keep_locks = 0;
1729 btrfs_unlock_up_safe(path, 1);
1730 }
1731 return err;
1732}
1733
1734/*
1735 * helper to add new inline back ref
1736 */
1737static noinline_for_stack
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001738void setup_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001739 struct btrfs_path *path,
1740 struct btrfs_extent_inline_ref *iref,
1741 u64 parent, u64 root_objectid,
1742 u64 owner, u64 offset, int refs_to_add,
1743 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001744{
1745 struct extent_buffer *leaf;
1746 struct btrfs_extent_item *ei;
1747 unsigned long ptr;
1748 unsigned long end;
1749 unsigned long item_offset;
1750 u64 refs;
1751 int size;
1752 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001753
1754 leaf = path->nodes[0];
1755 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1756 item_offset = (unsigned long)iref - (unsigned long)ei;
1757
1758 type = extent_ref_type(parent, owner);
1759 size = btrfs_extent_inline_ref_size(type);
1760
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001761 btrfs_extend_item(root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001762
1763 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1764 refs = btrfs_extent_refs(leaf, ei);
1765 refs += refs_to_add;
1766 btrfs_set_extent_refs(leaf, ei, refs);
1767 if (extent_op)
1768 __run_delayed_extent_op(extent_op, leaf, ei);
1769
1770 ptr = (unsigned long)ei + item_offset;
1771 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1772 if (ptr < end - size)
1773 memmove_extent_buffer(leaf, ptr + size, ptr,
1774 end - size - ptr);
1775
1776 iref = (struct btrfs_extent_inline_ref *)ptr;
1777 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1778 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1779 struct btrfs_extent_data_ref *dref;
1780 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1781 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1782 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1783 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1784 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1785 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1786 struct btrfs_shared_data_ref *sref;
1787 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1788 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1789 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1790 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1791 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1792 } else {
1793 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1794 }
1795 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001796}
1797
1798static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1799 struct btrfs_root *root,
1800 struct btrfs_path *path,
1801 struct btrfs_extent_inline_ref **ref_ret,
1802 u64 bytenr, u64 num_bytes, u64 parent,
1803 u64 root_objectid, u64 owner, u64 offset)
1804{
1805 int ret;
1806
1807 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1808 bytenr, num_bytes, parent,
1809 root_objectid, owner, offset, 0);
1810 if (ret != -ENOENT)
1811 return ret;
1812
David Sterbab3b4aa72011-04-21 01:20:15 +02001813 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001814 *ref_ret = NULL;
1815
1816 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1817 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1818 root_objectid);
1819 } else {
1820 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1821 root_objectid, owner, offset);
1822 }
1823 return ret;
1824}
1825
1826/*
1827 * helper to update/remove inline back ref
1828 */
1829static noinline_for_stack
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001830void update_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001831 struct btrfs_path *path,
1832 struct btrfs_extent_inline_ref *iref,
1833 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001834 struct btrfs_delayed_extent_op *extent_op,
1835 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001836{
1837 struct extent_buffer *leaf;
1838 struct btrfs_extent_item *ei;
1839 struct btrfs_extent_data_ref *dref = NULL;
1840 struct btrfs_shared_data_ref *sref = NULL;
1841 unsigned long ptr;
1842 unsigned long end;
1843 u32 item_size;
1844 int size;
1845 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001846 u64 refs;
1847
1848 leaf = path->nodes[0];
1849 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1850 refs = btrfs_extent_refs(leaf, ei);
1851 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1852 refs += refs_to_mod;
1853 btrfs_set_extent_refs(leaf, ei, refs);
1854 if (extent_op)
1855 __run_delayed_extent_op(extent_op, leaf, ei);
1856
1857 type = btrfs_extent_inline_ref_type(leaf, iref);
1858
1859 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1860 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1861 refs = btrfs_extent_data_ref_count(leaf, dref);
1862 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1863 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1864 refs = btrfs_shared_data_ref_count(leaf, sref);
1865 } else {
1866 refs = 1;
1867 BUG_ON(refs_to_mod != -1);
1868 }
1869
1870 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1871 refs += refs_to_mod;
1872
1873 if (refs > 0) {
1874 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1875 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1876 else
1877 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1878 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001879 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001880 size = btrfs_extent_inline_ref_size(type);
1881 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1882 ptr = (unsigned long)iref;
1883 end = (unsigned long)ei + item_size;
1884 if (ptr + size < end)
1885 memmove_extent_buffer(leaf, ptr, ptr + size,
1886 end - ptr - size);
1887 item_size -= size;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001888 btrfs_truncate_item(root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001889 }
1890 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001891}
1892
1893static noinline_for_stack
1894int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1895 struct btrfs_root *root,
1896 struct btrfs_path *path,
1897 u64 bytenr, u64 num_bytes, u64 parent,
1898 u64 root_objectid, u64 owner,
1899 u64 offset, int refs_to_add,
1900 struct btrfs_delayed_extent_op *extent_op)
1901{
1902 struct btrfs_extent_inline_ref *iref;
1903 int ret;
1904
1905 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1906 bytenr, num_bytes, parent,
1907 root_objectid, owner, offset, 1);
1908 if (ret == 0) {
1909 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001910 update_inline_extent_backref(root, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001911 refs_to_add, extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001912 } else if (ret == -ENOENT) {
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001913 setup_inline_extent_backref(root, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001914 root_objectid, owner, offset,
1915 refs_to_add, extent_op);
1916 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001917 }
1918 return ret;
1919}
1920
1921static int insert_extent_backref(struct btrfs_trans_handle *trans,
1922 struct btrfs_root *root,
1923 struct btrfs_path *path,
1924 u64 bytenr, u64 parent, u64 root_objectid,
1925 u64 owner, u64 offset, int refs_to_add)
1926{
1927 int ret;
1928 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1929 BUG_ON(refs_to_add != 1);
1930 ret = insert_tree_block_ref(trans, root, path, bytenr,
1931 parent, root_objectid);
1932 } else {
1933 ret = insert_extent_data_ref(trans, root, path, bytenr,
1934 parent, root_objectid,
1935 owner, offset, refs_to_add);
1936 }
1937 return ret;
1938}
1939
1940static int remove_extent_backref(struct btrfs_trans_handle *trans,
1941 struct btrfs_root *root,
1942 struct btrfs_path *path,
1943 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001944 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001945{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001946 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001947
1948 BUG_ON(!is_data && refs_to_drop != 1);
1949 if (iref) {
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001950 update_inline_extent_backref(root, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001951 -refs_to_drop, NULL, last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001952 } else if (is_data) {
Josef Bacikfcebe452014-05-13 17:30:47 -07001953 ret = remove_extent_data_ref(trans, root, path, refs_to_drop,
1954 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001955 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001956 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001957 ret = btrfs_del_item(trans, root, path);
1958 }
1959 return ret;
1960}
1961
Jeff Mahoney86557862015-06-15 09:41:16 -04001962#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001963static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
1964 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05001965{
Jeff Mahoney86557862015-06-15 09:41:16 -04001966 int j, ret = 0;
1967 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001968 u64 aligned_start = ALIGN(start, 1 << 9);
1969
1970 if (WARN_ON(start != aligned_start)) {
1971 len -= aligned_start - start;
1972 len = round_down(len, 1 << 9);
1973 start = aligned_start;
1974 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001975
1976 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04001977
1978 if (!len)
1979 return 0;
1980
1981 end = start + len;
1982 bytes_left = len;
1983
1984 /* Skip any superblocks on this device. */
1985 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
1986 u64 sb_start = btrfs_sb_offset(j);
1987 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
1988 u64 size = sb_start - start;
1989
1990 if (!in_range(sb_start, start, bytes_left) &&
1991 !in_range(sb_end, start, bytes_left) &&
1992 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
1993 continue;
1994
1995 /*
1996 * Superblock spans beginning of range. Adjust start and
1997 * try again.
1998 */
1999 if (sb_start <= start) {
2000 start += sb_end - start;
2001 if (start > end) {
2002 bytes_left = 0;
2003 break;
2004 }
2005 bytes_left = end - start;
2006 continue;
2007 }
2008
2009 if (size) {
2010 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
2011 GFP_NOFS, 0);
2012 if (!ret)
2013 *discarded_bytes += size;
2014 else if (ret != -EOPNOTSUPP)
2015 return ret;
2016 }
2017
2018 start = sb_end;
2019 if (start > end) {
2020 bytes_left = 0;
2021 break;
2022 }
2023 bytes_left = end - start;
2024 }
2025
2026 if (bytes_left) {
2027 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002028 GFP_NOFS, 0);
2029 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04002030 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002031 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002032 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05002033}
Chris Mason15916de2008-11-19 21:17:22 -05002034
Filipe Manana1edb647b2014-12-08 14:01:12 +00002035int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
2036 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05002037{
Liu Hui1f3c79a2009-01-05 15:57:51 -05002038 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00002039 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02002040 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002041
Christoph Hellwige244a0a2009-10-14 09:24:59 -04002042
Liu Hui1f3c79a2009-01-05 15:57:51 -05002043 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002044 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02002045 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002046 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05002047 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02002048 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002049 int i;
2050
Liu Hui1f3c79a2009-01-05 15:57:51 -05002051
Jan Schmidta1d3c472011-08-04 17:15:33 +02002052 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002053 u64 bytes;
Josef Bacikd5e20032011-08-04 14:52:27 +00002054 if (!stripe->dev->can_discard)
2055 continue;
2056
Li Dongyang5378e602011-03-24 10:24:27 +00002057 ret = btrfs_issue_discard(stripe->dev->bdev,
2058 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002059 stripe->length,
2060 &bytes);
Li Dongyang5378e602011-03-24 10:24:27 +00002061 if (!ret)
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002062 discarded_bytes += bytes;
Li Dongyang5378e602011-03-24 10:24:27 +00002063 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002064 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00002065
2066 /*
2067 * Just in case we get back EOPNOTSUPP for some reason,
2068 * just ignore the return value so we don't screw up
2069 * people calling discard_extent.
2070 */
2071 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002072 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08002073 btrfs_put_bbio(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002074 }
Li Dongyang5378e602011-03-24 10:24:27 +00002075
2076 if (actual_bytes)
2077 *actual_bytes = discarded_bytes;
2078
Liu Hui1f3c79a2009-01-05 15:57:51 -05002079
David Woodhouse53b381b2013-01-29 18:40:14 -05002080 if (ret == -EOPNOTSUPP)
2081 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002082 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002083}
2084
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002085/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002086int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2087 struct btrfs_root *root,
2088 u64 bytenr, u64 num_bytes, u64 parent,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002089 u64 root_objectid, u64 owner, u64 offset)
Zheng Yan31840ae2008-09-23 13:14:14 -04002090{
2091 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002092 struct btrfs_fs_info *fs_info = root->fs_info;
2093
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002094 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
2095 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04002096
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002097 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002098 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
2099 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002100 parent, root_objectid, (int)owner,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002101 BTRFS_ADD_DELAYED_REF, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002102 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002103 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002104 num_bytes, parent, root_objectid,
2105 owner, offset, 0,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002106 BTRFS_ADD_DELAYED_REF, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002107 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002108 return ret;
2109}
2110
Chris Mason925baed2008-06-25 16:01:30 -04002111static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002112 struct btrfs_root *root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002113 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002114 u64 parent, u64 root_objectid,
2115 u64 owner, u64 offset, int refs_to_add,
2116 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04002117{
Josef Bacikfcebe452014-05-13 17:30:47 -07002118 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04002119 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002120 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04002121 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07002122 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002123 u64 bytenr = node->bytenr;
2124 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002125 u64 refs;
2126 int ret;
Chris Mason037e6392007-03-07 11:50:24 -05002127
Chris Mason5caf2a02007-04-02 11:20:42 -04002128 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002129 if (!path)
2130 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04002131
Chris Mason3c12ac72008-04-21 12:01:38 -04002132 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002133 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002134 /* this will setup the path even if it fails to insert the back ref */
Josef Bacikfcebe452014-05-13 17:30:47 -07002135 ret = insert_inline_extent_backref(trans, fs_info->extent_root, path,
2136 bytenr, num_bytes, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002137 root_objectid, owner, offset,
2138 refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08002139 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002140 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07002141
2142 /*
2143 * Ok we had -EAGAIN which means we didn't have space to insert and
2144 * inline extent ref, so just update the reference count and add a
2145 * normal backref.
2146 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002147 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07002148 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002149 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2150 refs = btrfs_extent_refs(leaf, item);
2151 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2152 if (extent_op)
2153 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002154
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002155 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002156 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002157
Chris Mason3c12ac72008-04-21 12:01:38 -04002158 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002159 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04002160 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002161 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002162 path, bytenr, parent, root_objectid,
2163 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002164 if (ret)
2165 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002166out:
Chris Mason74493f72007-12-11 09:25:06 -05002167 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08002168 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002169}
2170
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002171static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2172 struct btrfs_root *root,
2173 struct btrfs_delayed_ref_node *node,
2174 struct btrfs_delayed_extent_op *extent_op,
2175 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002176{
Chris Mason56bec292009-03-13 10:10:06 -04002177 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002178 struct btrfs_delayed_data_ref *ref;
2179 struct btrfs_key ins;
2180 u64 parent = 0;
2181 u64 ref_root = 0;
2182 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002183
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002184 ins.objectid = node->bytenr;
2185 ins.offset = node->num_bytes;
2186 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002187
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002188 ref = btrfs_delayed_node_to_data_ref(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002189 trace_run_delayed_data_ref(node, ref, node->action);
2190
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002191 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2192 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002193 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002194
2195 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002196 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002197 flags |= extent_op->flags_to_set;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002198 ret = alloc_reserved_file_extent(trans, root,
2199 parent, ref_root, flags,
2200 ref->objectid, ref->offset,
2201 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002202 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002203 ret = __btrfs_inc_extent_ref(trans, root, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002204 ref_root, ref->objectid,
2205 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002206 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002207 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002208 ret = __btrfs_free_extent(trans, root, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002209 ref_root, ref->objectid,
2210 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002211 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002212 } else {
2213 BUG();
2214 }
Chris Mason56bec292009-03-13 10:10:06 -04002215 return ret;
2216}
2217
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002218static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2219 struct extent_buffer *leaf,
2220 struct btrfs_extent_item *ei)
2221{
2222 u64 flags = btrfs_extent_flags(leaf, ei);
2223 if (extent_op->update_flags) {
2224 flags |= extent_op->flags_to_set;
2225 btrfs_set_extent_flags(leaf, ei, flags);
2226 }
2227
2228 if (extent_op->update_key) {
2229 struct btrfs_tree_block_info *bi;
2230 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2231 bi = (struct btrfs_tree_block_info *)(ei + 1);
2232 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2233 }
2234}
2235
2236static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2237 struct btrfs_root *root,
2238 struct btrfs_delayed_ref_node *node,
2239 struct btrfs_delayed_extent_op *extent_op)
2240{
2241 struct btrfs_key key;
2242 struct btrfs_path *path;
2243 struct btrfs_extent_item *ei;
2244 struct extent_buffer *leaf;
2245 u32 item_size;
2246 int ret;
2247 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002248 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002249
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002250 if (trans->aborted)
2251 return 0;
2252
Josef Bacik3173a182013-03-07 14:22:04 -05002253 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2254 metadata = 0;
2255
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002256 path = btrfs_alloc_path();
2257 if (!path)
2258 return -ENOMEM;
2259
2260 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002261
Josef Bacik3173a182013-03-07 14:22:04 -05002262 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002263 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002264 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002265 } else {
2266 key.type = BTRFS_EXTENT_ITEM_KEY;
2267 key.offset = node->num_bytes;
2268 }
2269
2270again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002271 path->reada = 1;
2272 path->leave_spinning = 1;
2273 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2274 path, 0, 1);
2275 if (ret < 0) {
2276 err = ret;
2277 goto out;
2278 }
2279 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002280 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002281 if (path->slots[0] > 0) {
2282 path->slots[0]--;
2283 btrfs_item_key_to_cpu(path->nodes[0], &key,
2284 path->slots[0]);
2285 if (key.objectid == node->bytenr &&
2286 key.type == BTRFS_EXTENT_ITEM_KEY &&
2287 key.offset == node->num_bytes)
2288 ret = 0;
2289 }
2290 if (ret > 0) {
2291 btrfs_release_path(path);
2292 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002293
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002294 key.objectid = node->bytenr;
2295 key.offset = node->num_bytes;
2296 key.type = BTRFS_EXTENT_ITEM_KEY;
2297 goto again;
2298 }
2299 } else {
2300 err = -EIO;
2301 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002302 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002303 }
2304
2305 leaf = path->nodes[0];
2306 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2307#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2308 if (item_size < sizeof(*ei)) {
2309 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2310 path, (u64)-1, 0);
2311 if (ret < 0) {
2312 err = ret;
2313 goto out;
2314 }
2315 leaf = path->nodes[0];
2316 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2317 }
2318#endif
2319 BUG_ON(item_size < sizeof(*ei));
2320 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2321 __run_delayed_extent_op(extent_op, leaf, ei);
2322
2323 btrfs_mark_buffer_dirty(leaf);
2324out:
2325 btrfs_free_path(path);
2326 return err;
2327}
2328
2329static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2330 struct btrfs_root *root,
2331 struct btrfs_delayed_ref_node *node,
2332 struct btrfs_delayed_extent_op *extent_op,
2333 int insert_reserved)
2334{
2335 int ret = 0;
2336 struct btrfs_delayed_tree_ref *ref;
2337 struct btrfs_key ins;
2338 u64 parent = 0;
2339 u64 ref_root = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002340 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2341 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002342
2343 ref = btrfs_delayed_node_to_tree_ref(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002344 trace_run_delayed_tree_ref(node, ref, node->action);
2345
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002346 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2347 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002348 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002349
Josef Bacik3173a182013-03-07 14:22:04 -05002350 ins.objectid = node->bytenr;
2351 if (skinny_metadata) {
2352 ins.offset = ref->level;
2353 ins.type = BTRFS_METADATA_ITEM_KEY;
2354 } else {
2355 ins.offset = node->num_bytes;
2356 ins.type = BTRFS_EXTENT_ITEM_KEY;
2357 }
2358
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002359 BUG_ON(node->ref_mod != 1);
2360 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002361 BUG_ON(!extent_op || !extent_op->update_flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002362 ret = alloc_reserved_tree_block(trans, root,
2363 parent, ref_root,
2364 extent_op->flags_to_set,
2365 &extent_op->key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002366 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002367 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002368 ret = __btrfs_inc_extent_ref(trans, root, node,
2369 parent, ref_root,
2370 ref->level, 0, 1,
Josef Bacikfcebe452014-05-13 17:30:47 -07002371 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002372 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002373 ret = __btrfs_free_extent(trans, root, node,
2374 parent, ref_root,
2375 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002376 } else {
2377 BUG();
2378 }
2379 return ret;
2380}
2381
Chris Mason56bec292009-03-13 10:10:06 -04002382/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002383static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2384 struct btrfs_root *root,
2385 struct btrfs_delayed_ref_node *node,
2386 struct btrfs_delayed_extent_op *extent_op,
2387 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002388{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002389 int ret = 0;
2390
Josef Bacik857cc2f2013-10-07 15:21:08 -04002391 if (trans->aborted) {
2392 if (insert_reserved)
2393 btrfs_pin_extent(root, node->bytenr,
2394 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002395 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002396 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002397
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002398 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002399 struct btrfs_delayed_ref_head *head;
2400 /*
2401 * we've hit the end of the chain and we were supposed
2402 * to insert this extent into the tree. But, it got
2403 * deleted before we ever needed to insert it, so all
2404 * we have to do is clean up the accounting
2405 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002406 BUG_ON(extent_op);
2407 head = btrfs_delayed_node_to_head(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002408 trace_run_delayed_ref_head(node, head, node->action);
2409
Chris Mason56bec292009-03-13 10:10:06 -04002410 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002411 btrfs_pin_extent(root, node->bytenr,
2412 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002413 if (head->is_data) {
2414 ret = btrfs_del_csums(trans, root,
2415 node->bytenr,
2416 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002417 }
Chris Mason56bec292009-03-13 10:10:06 -04002418 }
Qu Wenruo297d7502015-09-08 17:08:37 +08002419
2420 /* Also free its reserved qgroup space */
2421 btrfs_qgroup_free_delayed_ref(root->fs_info,
2422 head->qgroup_ref_root,
2423 head->qgroup_reserved);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002424 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002425 }
Josef Bacikeb099672009-02-12 09:27:38 -05002426
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002427 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2428 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2429 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2430 insert_reserved);
2431 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2432 node->type == BTRFS_SHARED_DATA_REF_KEY)
2433 ret = run_delayed_data_ref(trans, root, node, extent_op,
2434 insert_reserved);
2435 else
2436 BUG();
2437 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002438}
2439
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002440static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04002441select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002442{
Filipe Mananacffc3372015-07-09 13:13:44 +01002443 struct btrfs_delayed_ref_node *ref;
2444
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002445 if (list_empty(&head->ref_list))
2446 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002447
Filipe Mananacffc3372015-07-09 13:13:44 +01002448 /*
2449 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2450 * This is to prevent a ref count from going down to zero, which deletes
2451 * the extent item from the extent tree, when there still are references
2452 * to add, which would fail because they would not find the extent item.
2453 */
2454 list_for_each_entry(ref, &head->ref_list, list) {
2455 if (ref->action == BTRFS_ADD_DELAYED_REF)
2456 return ref;
2457 }
2458
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002459 return list_entry(head->ref_list.next, struct btrfs_delayed_ref_node,
2460 list);
Chris Mason56bec292009-03-13 10:10:06 -04002461}
2462
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002463/*
2464 * Returns 0 on success or if called with an already aborted transaction.
2465 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2466 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002467static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2468 struct btrfs_root *root,
2469 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002470{
Chris Mason56bec292009-03-13 10:10:06 -04002471 struct btrfs_delayed_ref_root *delayed_refs;
2472 struct btrfs_delayed_ref_node *ref;
2473 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002474 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002475 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002476 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002477 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002478 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002479 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002480 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002481
2482 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002483 while (1) {
2484 if (!locked_ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002485 if (count >= nr)
Chris Mason56bec292009-03-13 10:10:06 -04002486 break;
Chris Mason56bec292009-03-13 10:10:06 -04002487
Josef Bacikd7df2c72014-01-23 09:21:38 -05002488 spin_lock(&delayed_refs->lock);
2489 locked_ref = btrfs_select_ref_head(trans);
2490 if (!locked_ref) {
2491 spin_unlock(&delayed_refs->lock);
2492 break;
2493 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002494
2495 /* grab the lock that says we are going to process
2496 * all the refs for this head */
2497 ret = btrfs_delayed_ref_lock(trans, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002498 spin_unlock(&delayed_refs->lock);
Chris Masonc3e69d52009-03-13 10:17:05 -04002499 /*
2500 * we may have dropped the spin lock to get the head
2501 * mutex lock, and that might have given someone else
2502 * time to free the head. If that's true, it has been
2503 * removed from our list and we can move on.
2504 */
2505 if (ret == -EAGAIN) {
2506 locked_ref = NULL;
2507 count++;
2508 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002509 }
2510 }
2511
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002512 /*
2513 * We need to try and merge add/drops of the same ref since we
2514 * can run into issues with relocate dropping the implicit ref
2515 * and then it being added back again before the drop can
2516 * finish. If we merged anything we need to re-loop so we can
2517 * get a good ref.
2518 * Or we can get node references of the same type that weren't
2519 * merged when created due to bumps in the tree mod seq, and
2520 * we need to merge them to prevent adding an inline extent
2521 * backref before dropping it (triggering a BUG_ON at
2522 * insert_inline_extent_backref()).
2523 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002524 spin_lock(&locked_ref->lock);
Filipe Manana2c3cf7d2015-10-22 09:47:34 +01002525 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2526 locked_ref);
Josef Bacikae1e2062012-08-07 16:00:32 -04002527
2528 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002529 * locked_ref is the head node, so we have to go one
2530 * node back for any delayed ref updates
2531 */
2532 ref = select_delayed_ref(locked_ref);
2533
2534 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002535 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002536 spin_unlock(&locked_ref->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002537 btrfs_delayed_ref_unlock(locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002538 spin_lock(&delayed_refs->lock);
2539 locked_ref->processing = 0;
Arne Jansend1270cd2011-09-13 15:16:43 +02002540 delayed_refs->num_heads_ready++;
2541 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002542 locked_ref = NULL;
Arne Jansend1270cd2011-09-13 15:16:43 +02002543 cond_resched();
Josef Bacik27a377d2014-02-07 13:57:59 -05002544 count++;
Arne Jansend1270cd2011-09-13 15:16:43 +02002545 continue;
2546 }
2547
2548 /*
Chris Mason56bec292009-03-13 10:10:06 -04002549 * record the must insert reserved flag before we
2550 * drop the spin lock.
2551 */
2552 must_insert_reserved = locked_ref->must_insert_reserved;
2553 locked_ref->must_insert_reserved = 0;
2554
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002555 extent_op = locked_ref->extent_op;
2556 locked_ref->extent_op = NULL;
2557
Chris Mason56bec292009-03-13 10:10:06 -04002558 if (!ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002559
2560
Chris Mason56bec292009-03-13 10:10:06 -04002561 /* All delayed refs have been processed, Go ahead
2562 * and send the head node to run_one_delayed_ref,
2563 * so that any accounting fixes can happen
2564 */
2565 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002566
2567 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002568 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002569 extent_op = NULL;
2570 }
2571
2572 if (extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002573 spin_unlock(&locked_ref->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002574 ret = run_delayed_extent_op(trans, root,
2575 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002576 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002577
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002578 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04002579 /*
2580 * Need to reset must_insert_reserved if
2581 * there was an error so the abort stuff
2582 * can cleanup the reserved space
2583 * properly.
2584 */
2585 if (must_insert_reserved)
2586 locked_ref->must_insert_reserved = 1;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002587 locked_ref->processing = 0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002588 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
Miao Xie093486c2012-12-19 08:10:10 +00002589 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002590 return ret;
2591 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002592 continue;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002593 }
Chris Mason56bec292009-03-13 10:10:06 -04002594
Josef Bacikd7df2c72014-01-23 09:21:38 -05002595 /*
2596 * Need to drop our head ref lock and re-aqcuire the
2597 * delayed ref lock and then re-check to make sure
2598 * nobody got added.
2599 */
2600 spin_unlock(&locked_ref->lock);
2601 spin_lock(&delayed_refs->lock);
2602 spin_lock(&locked_ref->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002603 if (!list_empty(&locked_ref->ref_list) ||
Josef Bacik573a0752014-03-27 19:41:34 -04002604 locked_ref->extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002605 spin_unlock(&locked_ref->lock);
2606 spin_unlock(&delayed_refs->lock);
2607 continue;
2608 }
2609 ref->in_tree = 0;
2610 delayed_refs->num_heads--;
Liu Boc46effa2013-10-14 12:59:45 +08002611 rb_erase(&locked_ref->href_node,
2612 &delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002613 spin_unlock(&delayed_refs->lock);
2614 } else {
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002615 actual_count++;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002616 ref->in_tree = 0;
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002617 list_del(&ref->list);
Liu Boc46effa2013-10-14 12:59:45 +08002618 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002619 atomic_dec(&delayed_refs->num_entries);
2620
Miao Xie093486c2012-12-19 08:10:10 +00002621 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002622 /*
2623 * when we play the delayed ref, also correct the
2624 * ref_mod on head
2625 */
2626 switch (ref->action) {
2627 case BTRFS_ADD_DELAYED_REF:
2628 case BTRFS_ADD_DELAYED_EXTENT:
2629 locked_ref->node.ref_mod -= ref->ref_mod;
2630 break;
2631 case BTRFS_DROP_DELAYED_REF:
2632 locked_ref->node.ref_mod += ref->ref_mod;
2633 break;
2634 default:
2635 WARN_ON(1);
2636 }
2637 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002638 spin_unlock(&locked_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002639
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002640 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002641 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002642
Miao Xie78a61842012-11-21 02:21:28 +00002643 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002644 if (ret) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002645 locked_ref->processing = 0;
Miao Xie093486c2012-12-19 08:10:10 +00002646 btrfs_delayed_ref_unlock(locked_ref);
2647 btrfs_put_delayed_ref(ref);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002648 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002649 return ret;
2650 }
2651
Miao Xie093486c2012-12-19 08:10:10 +00002652 /*
2653 * If this node is a head, that means all the refs in this head
2654 * have been dealt with, and we will pick the next head to deal
2655 * with, so we must unlock the head and drop it from the cluster
2656 * list before we release it.
2657 */
2658 if (btrfs_delayed_ref_is_head(ref)) {
Josef Bacik12621332015-02-03 07:50:16 -08002659 if (locked_ref->is_data &&
2660 locked_ref->total_ref_mod < 0) {
2661 spin_lock(&delayed_refs->lock);
2662 delayed_refs->pending_csums -= ref->num_bytes;
2663 spin_unlock(&delayed_refs->lock);
2664 }
Miao Xie093486c2012-12-19 08:10:10 +00002665 btrfs_delayed_ref_unlock(locked_ref);
2666 locked_ref = NULL;
2667 }
2668 btrfs_put_delayed_ref(ref);
2669 count++;
Chris Mason1887be62009-03-13 10:11:24 -04002670 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002671 }
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002672
2673 /*
2674 * We don't want to include ref heads since we can have empty ref heads
2675 * and those will drastically skew our runtime down since we just do
2676 * accounting, no actual extent tree updates.
2677 */
2678 if (actual_count > 0) {
2679 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2680 u64 avg;
2681
2682 /*
2683 * We weigh the current average higher than our current runtime
2684 * to avoid large swings in the average.
2685 */
2686 spin_lock(&delayed_refs->lock);
2687 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002688 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002689 spin_unlock(&delayed_refs->lock);
2690 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002691 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002692}
2693
Arne Jansen709c0482011-09-12 12:22:57 +02002694#ifdef SCRAMBLE_DELAYED_REFS
2695/*
2696 * Normally delayed refs get processed in ascending bytenr order. This
2697 * correlates in most cases to the order added. To expose dependencies on this
2698 * order, we start to process the tree in the middle instead of the beginning
2699 */
2700static u64 find_middle(struct rb_root *root)
2701{
2702 struct rb_node *n = root->rb_node;
2703 struct btrfs_delayed_ref_node *entry;
2704 int alt = 1;
2705 u64 middle;
2706 u64 first = 0, last = 0;
2707
2708 n = rb_first(root);
2709 if (n) {
2710 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2711 first = entry->bytenr;
2712 }
2713 n = rb_last(root);
2714 if (n) {
2715 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2716 last = entry->bytenr;
2717 }
2718 n = root->rb_node;
2719
2720 while (n) {
2721 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2722 WARN_ON(!entry->in_tree);
2723
2724 middle = entry->bytenr;
2725
2726 if (alt)
2727 n = n->rb_left;
2728 else
2729 n = n->rb_right;
2730
2731 alt = 1 - alt;
2732 }
2733 return middle;
2734}
2735#endif
2736
Josef Bacik1be41b72013-06-12 13:56:06 -04002737static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2738{
2739 u64 num_bytes;
2740
2741 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2742 sizeof(struct btrfs_extent_inline_ref));
2743 if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2744 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2745
2746 /*
2747 * We don't ever fill up leaves all the way so multiply by 2 just to be
2748 * closer to what we're really going to want to ouse.
2749 */
David Sterbaf8c269d2015-01-16 17:21:12 +01002750 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
Josef Bacik1be41b72013-06-12 13:56:06 -04002751}
2752
Josef Bacik12621332015-02-03 07:50:16 -08002753/*
2754 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2755 * would require to store the csums for that many bytes.
2756 */
Chris Mason28f75a02015-02-04 06:59:29 -08002757u64 btrfs_csum_bytes_to_leaves(struct btrfs_root *root, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002758{
2759 u64 csum_size;
2760 u64 num_csums_per_leaf;
2761 u64 num_csums;
2762
2763 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
2764 num_csums_per_leaf = div64_u64(csum_size,
2765 (u64)btrfs_super_csum_size(root->fs_info->super_copy));
2766 num_csums = div64_u64(csum_bytes, root->sectorsize);
2767 num_csums += num_csums_per_leaf - 1;
2768 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2769 return num_csums;
2770}
2771
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002772int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacik1be41b72013-06-12 13:56:06 -04002773 struct btrfs_root *root)
2774{
2775 struct btrfs_block_rsv *global_rsv;
2776 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
Josef Bacik12621332015-02-03 07:50:16 -08002777 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
Josef Bacikcb723e42015-02-18 08:06:57 -08002778 u64 num_dirty_bgs = trans->transaction->num_dirty_bgs;
2779 u64 num_bytes, num_dirty_bgs_bytes;
Josef Bacik1be41b72013-06-12 13:56:06 -04002780 int ret = 0;
2781
2782 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
2783 num_heads = heads_to_leaves(root, num_heads);
2784 if (num_heads > 1)
David Sterba707e8a02014-06-04 19:22:26 +02002785 num_bytes += (num_heads - 1) * root->nodesize;
Josef Bacik1be41b72013-06-12 13:56:06 -04002786 num_bytes <<= 1;
Chris Mason28f75a02015-02-04 06:59:29 -08002787 num_bytes += btrfs_csum_bytes_to_leaves(root, csum_bytes) * root->nodesize;
Josef Bacikcb723e42015-02-18 08:06:57 -08002788 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(root,
2789 num_dirty_bgs);
Josef Bacik1be41b72013-06-12 13:56:06 -04002790 global_rsv = &root->fs_info->global_block_rsv;
2791
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,
2809 struct btrfs_root *root)
2810{
2811 struct btrfs_fs_info *fs_info = root->fs_info;
2812 u64 num_entries =
2813 atomic_read(&trans->transaction->delayed_refs.num_entries);
2814 u64 avg_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002815 u64 val;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002816
2817 smp_mb();
2818 avg_runtime = fs_info->avg_delayed_ref_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002819 val = num_entries * avg_runtime;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002820 if (num_entries * avg_runtime >= NSEC_PER_SEC)
2821 return 1;
Chris Masona79b7d42014-05-22 16:18:52 -07002822 if (val >= NSEC_PER_SEC / 2)
2823 return 2;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002824
2825 return btrfs_check_space_for_delayed_refs(trans, root);
2826}
2827
Chris Masona79b7d42014-05-22 16:18:52 -07002828struct async_delayed_refs {
2829 struct btrfs_root *root;
2830 int count;
2831 int error;
2832 int sync;
2833 struct completion wait;
2834 struct btrfs_work work;
2835};
2836
2837static void delayed_ref_async_start(struct btrfs_work *work)
2838{
2839 struct async_delayed_refs *async;
2840 struct btrfs_trans_handle *trans;
2841 int ret;
2842
2843 async = container_of(work, struct async_delayed_refs, work);
2844
2845 trans = btrfs_join_transaction(async->root);
2846 if (IS_ERR(trans)) {
2847 async->error = PTR_ERR(trans);
2848 goto done;
2849 }
2850
2851 /*
2852 * trans->sync means that when we call end_transaciton, we won't
2853 * wait on delayed refs
2854 */
2855 trans->sync = true;
2856 ret = btrfs_run_delayed_refs(trans, async->root, async->count);
2857 if (ret)
2858 async->error = ret;
2859
2860 ret = btrfs_end_transaction(trans, async->root);
2861 if (ret && !async->error)
2862 async->error = ret;
2863done:
2864 if (async->sync)
2865 complete(&async->wait);
2866 else
2867 kfree(async);
2868}
2869
2870int btrfs_async_run_delayed_refs(struct btrfs_root *root,
2871 unsigned long count, int wait)
2872{
2873 struct async_delayed_refs *async;
2874 int ret;
2875
2876 async = kmalloc(sizeof(*async), GFP_NOFS);
2877 if (!async)
2878 return -ENOMEM;
2879
2880 async->root = root->fs_info->tree_root;
2881 async->count = count;
2882 async->error = 0;
2883 if (wait)
2884 async->sync = 1;
2885 else
2886 async->sync = 0;
2887 init_completion(&async->wait);
2888
Liu Bo9e0af232014-08-15 23:36:53 +08002889 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
2890 delayed_ref_async_start, NULL, NULL);
Chris Masona79b7d42014-05-22 16:18:52 -07002891
2892 btrfs_queue_work(root->fs_info->extent_workers, &async->work);
2893
2894 if (wait) {
2895 wait_for_completion(&async->wait);
2896 ret = async->error;
2897 kfree(async);
2898 return ret;
2899 }
2900 return 0;
2901}
2902
Chris Masonc3e69d52009-03-13 10:17:05 -04002903/*
2904 * this starts processing the delayed reference count updates and
2905 * extent insertions we have queued up so far. count can be
2906 * 0, which means to process everything in the tree at the start
2907 * of the run (but not newly added entries), or it can be some target
2908 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002909 *
2910 * Returns 0 on success or if called with an aborted transaction
2911 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002912 */
2913int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2914 struct btrfs_root *root, unsigned long count)
2915{
2916 struct rb_node *node;
2917 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002918 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002919 int ret;
2920 int run_all = count == (unsigned long)-1;
Filipe Mananad9a05402015-10-03 13:13:13 +01002921 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Chris Masonc3e69d52009-03-13 10:17:05 -04002922
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002923 /* We'll clean this up in btrfs_cleanup_transaction */
2924 if (trans->aborted)
2925 return 0;
2926
Chris Masonc3e69d52009-03-13 10:17:05 -04002927 if (root == root->fs_info->extent_root)
2928 root = root->fs_info->tree_root;
2929
2930 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08002931 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05002932 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002933
Chris Masonc3e69d52009-03-13 10:17:05 -04002934again:
Arne Jansen709c0482011-09-12 12:22:57 +02002935#ifdef SCRAMBLE_DELAYED_REFS
2936 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2937#endif
Filipe Mananad9a05402015-10-03 13:13:13 +01002938 trans->can_flush_pending_bgs = false;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002939 ret = __btrfs_run_delayed_refs(trans, root, count);
2940 if (ret < 0) {
2941 btrfs_abort_transaction(trans, root, ret);
2942 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002943 }
2944
Chris Mason56bec292009-03-13 10:10:06 -04002945 if (run_all) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002946 if (!list_empty(&trans->new_bgs))
Josef Bacikea658ba2012-09-11 16:57:25 -04002947 btrfs_create_pending_block_groups(trans, root);
Josef Bacikea658ba2012-09-11 16:57:25 -04002948
Josef Bacikd7df2c72014-01-23 09:21:38 -05002949 spin_lock(&delayed_refs->lock);
Liu Boc46effa2013-10-14 12:59:45 +08002950 node = rb_first(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002951 if (!node) {
2952 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002953 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002954 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002955 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002956
2957 while (node) {
Liu Boc46effa2013-10-14 12:59:45 +08002958 head = rb_entry(node, struct btrfs_delayed_ref_head,
2959 href_node);
2960 if (btrfs_delayed_ref_is_head(&head->node)) {
2961 struct btrfs_delayed_ref_node *ref;
Chris Mason56bec292009-03-13 10:10:06 -04002962
Liu Boc46effa2013-10-14 12:59:45 +08002963 ref = &head->node;
Chris Mason56bec292009-03-13 10:10:06 -04002964 atomic_inc(&ref->refs);
2965
2966 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002967 /*
2968 * Mutex was contended, block until it's
2969 * released and try again
2970 */
Chris Mason56bec292009-03-13 10:10:06 -04002971 mutex_lock(&head->mutex);
2972 mutex_unlock(&head->mutex);
2973
2974 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002975 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002976 goto again;
Liu Boc46effa2013-10-14 12:59:45 +08002977 } else {
2978 WARN_ON(1);
Chris Mason56bec292009-03-13 10:10:06 -04002979 }
2980 node = rb_next(node);
2981 }
2982 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002983 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002984 goto again;
2985 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002986out:
Jan Schmidtedf39272012-06-28 18:04:55 +02002987 assert_qgroups_uptodate(trans);
Filipe Mananad9a05402015-10-03 13:13:13 +01002988 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Chris Masona28ec192007-03-06 20:08:01 -05002989 return 0;
2990}
2991
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002992int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2993 struct btrfs_root *root,
2994 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002995 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002996{
2997 struct btrfs_delayed_extent_op *extent_op;
2998 int ret;
2999
Miao Xie78a61842012-11-21 02:21:28 +00003000 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003001 if (!extent_op)
3002 return -ENOMEM;
3003
3004 extent_op->flags_to_set = flags;
3005 extent_op->update_flags = 1;
3006 extent_op->update_key = 0;
3007 extent_op->is_data = is_data ? 1 : 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04003008 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003009
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003010 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
3011 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003012 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00003013 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003014 return ret;
3015}
3016
3017static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
3018 struct btrfs_root *root,
3019 struct btrfs_path *path,
3020 u64 objectid, u64 offset, u64 bytenr)
3021{
3022 struct btrfs_delayed_ref_head *head;
3023 struct btrfs_delayed_ref_node *ref;
3024 struct btrfs_delayed_data_ref *data_ref;
3025 struct btrfs_delayed_ref_root *delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003026 int ret = 0;
3027
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003028 delayed_refs = &trans->transaction->delayed_refs;
3029 spin_lock(&delayed_refs->lock);
3030 head = btrfs_find_delayed_ref_head(trans, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003031 if (!head) {
3032 spin_unlock(&delayed_refs->lock);
3033 return 0;
3034 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003035
3036 if (!mutex_trylock(&head->mutex)) {
3037 atomic_inc(&head->node.refs);
3038 spin_unlock(&delayed_refs->lock);
3039
David Sterbab3b4aa72011-04-21 01:20:15 +02003040 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003041
David Sterba8cc33e52011-05-02 15:29:25 +02003042 /*
3043 * Mutex was contended, block until it's released and let
3044 * caller try again
3045 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003046 mutex_lock(&head->mutex);
3047 mutex_unlock(&head->mutex);
3048 btrfs_put_delayed_ref(&head->node);
3049 return -EAGAIN;
3050 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003051 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003052
3053 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08003054 list_for_each_entry(ref, &head->ref_list, list) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05003055 /* If it's a shared ref we know a cross reference exists */
3056 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
3057 ret = 1;
3058 break;
3059 }
3060
3061 data_ref = btrfs_delayed_node_to_data_ref(ref);
3062
3063 /*
3064 * If our ref doesn't match the one we're currently looking at
3065 * then we have a cross reference.
3066 */
3067 if (data_ref->root != root->root_key.objectid ||
3068 data_ref->objectid != objectid ||
3069 data_ref->offset != offset) {
3070 ret = 1;
3071 break;
3072 }
3073 }
3074 spin_unlock(&head->lock);
3075 mutex_unlock(&head->mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003076 return ret;
3077}
3078
3079static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
3080 struct btrfs_root *root,
3081 struct btrfs_path *path,
3082 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05003083{
3084 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04003085 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003086 struct btrfs_extent_data_ref *ref;
3087 struct btrfs_extent_inline_ref *iref;
3088 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05003089 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003090 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04003091 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05003092
Chris Masonbe20aa92007-12-17 20:14:01 -05003093 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04003094 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04003095 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05003096
Chris Masonbe20aa92007-12-17 20:14:01 -05003097 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3098 if (ret < 0)
3099 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003100 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04003101
3102 ret = -ENOENT;
3103 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04003104 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003105
Zheng Yan31840ae2008-09-23 13:14:14 -04003106 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04003107 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003108 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05003109
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003110 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05003111 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003112
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003113 ret = 1;
3114 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3115#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3116 if (item_size < sizeof(*ei)) {
3117 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3118 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003119 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003120#endif
3121 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
3122
3123 if (item_size != sizeof(*ei) +
3124 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
3125 goto out;
3126
3127 if (btrfs_extent_generation(leaf, ei) <=
3128 btrfs_root_last_snapshot(&root->root_item))
3129 goto out;
3130
3131 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
3132 if (btrfs_extent_inline_ref_type(leaf, iref) !=
3133 BTRFS_EXTENT_DATA_REF_KEY)
3134 goto out;
3135
3136 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3137 if (btrfs_extent_refs(leaf, ei) !=
3138 btrfs_extent_data_ref_count(leaf, ref) ||
3139 btrfs_extent_data_ref_root(leaf, ref) !=
3140 root->root_key.objectid ||
3141 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3142 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3143 goto out;
3144
Yan Zhengf321e492008-07-30 09:26:11 -04003145 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05003146out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003147 return ret;
3148}
3149
3150int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
3151 struct btrfs_root *root,
3152 u64 objectid, u64 offset, u64 bytenr)
3153{
3154 struct btrfs_path *path;
3155 int ret;
3156 int ret2;
3157
3158 path = btrfs_alloc_path();
3159 if (!path)
3160 return -ENOENT;
3161
3162 do {
3163 ret = check_committed_ref(trans, root, path, objectid,
3164 offset, bytenr);
3165 if (ret && ret != -ENOENT)
3166 goto out;
3167
3168 ret2 = check_delayed_ref(trans, root, path, objectid,
3169 offset, bytenr);
3170 } while (ret2 == -EAGAIN);
3171
3172 if (ret2 && ret2 != -ENOENT) {
3173 ret = ret2;
3174 goto out;
3175 }
3176
3177 if (ret != -ENOENT || ret2 != -ENOENT)
3178 ret = 0;
3179out:
Yan Zhengf321e492008-07-30 09:26:11 -04003180 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003181 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3182 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04003183 return ret;
3184}
3185
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003186static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05003187 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003188 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07003189 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04003190{
3191 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003192 u64 num_bytes;
3193 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003194 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04003195 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003196 struct btrfs_key key;
3197 struct btrfs_file_extent_item *fi;
3198 int i;
3199 int level;
3200 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003201 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003202 u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04003203
David Sterbafccb84c2014-09-29 23:53:21 +02003204
3205 if (btrfs_test_is_dummy_root(root))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003206 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003207
Zheng Yan31840ae2008-09-23 13:14:14 -04003208 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003209 nritems = btrfs_header_nritems(buf);
3210 level = btrfs_header_level(buf);
3211
Miao Xie27cdeb72014-04-02 19:51:05 +08003212 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003213 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003214
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003215 if (inc)
3216 process_func = btrfs_inc_extent_ref;
3217 else
3218 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003219
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003220 if (full_backref)
3221 parent = buf->start;
3222 else
3223 parent = 0;
3224
Zheng Yan31840ae2008-09-23 13:14:14 -04003225 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003226 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003227 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02003228 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003229 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003230 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003231 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003232 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003233 BTRFS_FILE_EXTENT_INLINE)
3234 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003235 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3236 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003237 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003238
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003239 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3240 key.offset -= btrfs_file_extent_offset(buf, fi);
3241 ret = process_func(trans, root, bytenr, num_bytes,
3242 parent, ref_root, key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003243 key.offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003244 if (ret)
3245 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003246 } else {
3247 bytenr = btrfs_node_blockptr(buf, i);
David Sterba707e8a02014-06-04 19:22:26 +02003248 num_bytes = root->nodesize;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003249 ret = process_func(trans, root, bytenr, num_bytes,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01003250 parent, ref_root, level - 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003251 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003252 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003253 }
3254 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003255 return 0;
3256fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003257 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003258}
3259
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003260int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003261 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04003262{
Josef Bacike339a6b2014-07-02 10:54:25 -07003263 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003264}
Zheng Yan31840ae2008-09-23 13:14:14 -04003265
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003266int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003267 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003268{
Josef Bacike339a6b2014-07-02 10:54:25 -07003269 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003270}
3271
Chris Mason9078a3e2007-04-26 16:46:15 -04003272static int write_one_cache_group(struct btrfs_trans_handle *trans,
3273 struct btrfs_root *root,
3274 struct btrfs_path *path,
3275 struct btrfs_block_group_cache *cache)
3276{
3277 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003278 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003279 unsigned long bi;
3280 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003281
Chris Mason9078a3e2007-04-26 16:46:15 -04003282 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003283 if (ret) {
3284 if (ret > 0)
3285 ret = -ENOENT;
Chris Mason54aa1f42007-06-22 14:16:25 -04003286 goto fail;
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003287 }
Chris Mason5f39d392007-10-15 16:14:19 -04003288
3289 leaf = path->nodes[0];
3290 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3291 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3292 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -04003293fail:
Filipe Manana24b89d02015-04-25 18:31:05 +01003294 btrfs_release_path(path);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003295 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003296
3297}
3298
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003299static struct btrfs_block_group_cache *
3300next_block_group(struct btrfs_root *root,
3301 struct btrfs_block_group_cache *cache)
3302{
3303 struct rb_node *node;
Filipe Manana292cbd52014-11-26 15:28:50 +00003304
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003305 spin_lock(&root->fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003306
3307 /* If our block group was removed, we need a full search. */
3308 if (RB_EMPTY_NODE(&cache->cache_node)) {
3309 const u64 next_bytenr = cache->key.objectid + cache->key.offset;
3310
3311 spin_unlock(&root->fs_info->block_group_cache_lock);
3312 btrfs_put_block_group(cache);
3313 cache = btrfs_lookup_first_block_group(root->fs_info,
3314 next_bytenr);
3315 return cache;
3316 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003317 node = rb_next(&cache->cache_node);
3318 btrfs_put_block_group(cache);
3319 if (node) {
3320 cache = rb_entry(node, struct btrfs_block_group_cache,
3321 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003322 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003323 } else
3324 cache = NULL;
3325 spin_unlock(&root->fs_info->block_group_cache_lock);
3326 return cache;
3327}
3328
Josef Bacik0af3d002010-06-21 14:48:16 -04003329static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3330 struct btrfs_trans_handle *trans,
3331 struct btrfs_path *path)
3332{
3333 struct btrfs_root *root = block_group->fs_info->tree_root;
3334 struct inode *inode = NULL;
3335 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003336 int dcs = BTRFS_DC_ERROR;
David Sterbaf8c269d2015-01-16 17:21:12 +01003337 u64 num_pages = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003338 int retries = 0;
3339 int ret = 0;
3340
3341 /*
3342 * If this block group is smaller than 100 megs don't bother caching the
3343 * block group.
3344 */
3345 if (block_group->key.offset < (100 * 1024 * 1024)) {
3346 spin_lock(&block_group->lock);
3347 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3348 spin_unlock(&block_group->lock);
3349 return 0;
3350 }
3351
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003352 if (trans->aborted)
3353 return 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003354again:
3355 inode = lookup_free_space_inode(root, block_group, path);
3356 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3357 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003358 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003359 goto out;
3360 }
3361
3362 if (IS_ERR(inode)) {
3363 BUG_ON(retries);
3364 retries++;
3365
3366 if (block_group->ro)
3367 goto out_free;
3368
3369 ret = create_free_space_inode(root, trans, block_group, path);
3370 if (ret)
3371 goto out_free;
3372 goto again;
3373 }
3374
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003375 /* We've already setup this transaction, go ahead and exit */
3376 if (block_group->cache_generation == trans->transid &&
3377 i_size_read(inode)) {
3378 dcs = BTRFS_DC_SETUP;
3379 goto out_put;
3380 }
3381
Josef Bacik0af3d002010-06-21 14:48:16 -04003382 /*
3383 * We want to set the generation to 0, that way if anything goes wrong
3384 * from here on out we know not to trust this cache when we load up next
3385 * time.
3386 */
3387 BTRFS_I(inode)->generation = 0;
3388 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003389 if (ret) {
3390 /*
3391 * So theoretically we could recover from this, simply set the
3392 * super cache generation to 0 so we know to invalidate the
3393 * cache, but then we'd have to keep track of the block groups
3394 * that fail this way so we know we _have_ to reset this cache
3395 * before the next commit or risk reading stale cache. So to
3396 * limit our exposure to horrible edge cases lets just abort the
3397 * transaction, this only happens in really bad situations
3398 * anyway.
3399 */
3400 btrfs_abort_transaction(trans, root, ret);
3401 goto out_put;
3402 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003403 WARN_ON(ret);
3404
3405 if (i_size_read(inode) > 0) {
Miao Xie7b61cd92013-05-13 13:55:09 +00003406 ret = btrfs_check_trunc_cache_free_space(root,
3407 &root->fs_info->global_block_rsv);
3408 if (ret)
3409 goto out_put;
3410
Chris Mason1bbc6212015-04-06 12:46:08 -07003411 ret = btrfs_truncate_free_space_cache(root, trans, NULL, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003412 if (ret)
3413 goto out_put;
3414 }
3415
3416 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003417 if (block_group->cached != BTRFS_CACHE_FINISHED ||
Chris Masone4c88f02015-04-18 05:22:48 -07003418 !btrfs_test_opt(root, SPACE_CACHE)) {
Liu Bocf7c1ef2012-07-06 03:31:34 -06003419 /*
3420 * don't bother trying to write stuff out _if_
3421 * a) we're not cached,
3422 * b) we're with nospace_cache mount option.
3423 */
Josef Bacik2b209822010-12-03 13:17:53 -05003424 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003425 spin_unlock(&block_group->lock);
3426 goto out_put;
3427 }
3428 spin_unlock(&block_group->lock);
3429
Josef Bacik6fc823b2012-08-06 13:46:38 -06003430 /*
Josef Bacik2968b1f2015-10-01 12:55:18 -04003431 * We hit an ENOSPC when setting up the cache in this transaction, just
3432 * skip doing the setup, we've already cleared the cache so we're safe.
3433 */
3434 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
3435 ret = -ENOSPC;
3436 goto out_put;
3437 }
3438
3439 /*
Josef Bacik6fc823b2012-08-06 13:46:38 -06003440 * Try to preallocate enough space based on how big the block group is.
3441 * Keep in mind this has to include any pinned space which could end up
3442 * taking up quite a bit since it's not folded into the other space
3443 * cache.
3444 */
David Sterbaf8c269d2015-01-16 17:21:12 +01003445 num_pages = div_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003446 if (!num_pages)
3447 num_pages = 1;
3448
Josef Bacik0af3d002010-06-21 14:48:16 -04003449 num_pages *= 16;
3450 num_pages *= PAGE_CACHE_SIZE;
3451
Qu Wenruo7cf5b972015-09-08 17:25:55 +08003452 ret = btrfs_check_data_free_space(inode, 0, num_pages);
Josef Bacik0af3d002010-06-21 14:48:16 -04003453 if (ret)
3454 goto out_put;
3455
3456 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3457 num_pages, num_pages,
3458 &alloc_hint);
Josef Bacik2968b1f2015-10-01 12:55:18 -04003459 /*
3460 * Our cache requires contiguous chunks so that we don't modify a bunch
3461 * of metadata or split extents when writing the cache out, which means
3462 * we can enospc if we are heavily fragmented in addition to just normal
3463 * out of space conditions. So if we hit this just skip setting up any
3464 * other block groups for this transaction, maybe we'll unpin enough
3465 * space the next time around.
3466 */
Josef Bacik2b209822010-12-03 13:17:53 -05003467 if (!ret)
3468 dcs = BTRFS_DC_SETUP;
Josef Bacik2968b1f2015-10-01 12:55:18 -04003469 else if (ret == -ENOSPC)
3470 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
Qu Wenruo7cf5b972015-09-08 17:25:55 +08003471 btrfs_free_reserved_data_space(inode, 0, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003472
Josef Bacik0af3d002010-06-21 14:48:16 -04003473out_put:
3474 iput(inode);
3475out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003476 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003477out:
3478 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003479 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003480 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003481 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003482 spin_unlock(&block_group->lock);
3483
3484 return ret;
3485}
3486
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003487int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
3488 struct btrfs_root *root)
3489{
3490 struct btrfs_block_group_cache *cache, *tmp;
3491 struct btrfs_transaction *cur_trans = trans->transaction;
3492 struct btrfs_path *path;
3493
3494 if (list_empty(&cur_trans->dirty_bgs) ||
3495 !btrfs_test_opt(root, SPACE_CACHE))
3496 return 0;
3497
3498 path = btrfs_alloc_path();
3499 if (!path)
3500 return -ENOMEM;
3501
3502 /* Could add new block groups, use _safe just in case */
3503 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
3504 dirty_list) {
3505 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3506 cache_save_setup(cache, trans, path);
3507 }
3508
3509 btrfs_free_path(path);
3510 return 0;
3511}
3512
Chris Mason1bbc6212015-04-06 12:46:08 -07003513/*
3514 * transaction commit does final block group cache writeback during a
3515 * critical section where nothing is allowed to change the FS. This is
3516 * required in order for the cache to actually match the block group,
3517 * but can introduce a lot of latency into the commit.
3518 *
3519 * So, btrfs_start_dirty_block_groups is here to kick off block group
3520 * cache IO. There's a chance we'll have to redo some of it if the
3521 * block group changes again during the commit, but it greatly reduces
3522 * the commit latency by getting rid of the easy block groups while
3523 * we're still allowing others to join the commit.
3524 */
3525int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans,
3526 struct btrfs_root *root)
3527{
3528 struct btrfs_block_group_cache *cache;
3529 struct btrfs_transaction *cur_trans = trans->transaction;
3530 int ret = 0;
3531 int should_put;
3532 struct btrfs_path *path = NULL;
3533 LIST_HEAD(dirty);
3534 struct list_head *io = &cur_trans->io_bgs;
3535 int num_started = 0;
3536 int loops = 0;
3537
3538 spin_lock(&cur_trans->dirty_bgs_lock);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003539 if (list_empty(&cur_trans->dirty_bgs)) {
3540 spin_unlock(&cur_trans->dirty_bgs_lock);
3541 return 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003542 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003543 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Chris Mason1bbc6212015-04-06 12:46:08 -07003544 spin_unlock(&cur_trans->dirty_bgs_lock);
3545
3546again:
Chris Mason1bbc6212015-04-06 12:46:08 -07003547 /*
3548 * make sure all the block groups on our dirty list actually
3549 * exist
3550 */
3551 btrfs_create_pending_block_groups(trans, root);
3552
3553 if (!path) {
3554 path = btrfs_alloc_path();
3555 if (!path)
3556 return -ENOMEM;
3557 }
3558
Filipe Mananab58d1a92015-04-25 18:29:16 +01003559 /*
3560 * cache_write_mutex is here only to save us from balance or automatic
3561 * removal of empty block groups deleting this block group while we are
3562 * writing out the cache
3563 */
3564 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003565 while (!list_empty(&dirty)) {
3566 cache = list_first_entry(&dirty,
3567 struct btrfs_block_group_cache,
3568 dirty_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07003569 /*
3570 * this can happen if something re-dirties a block
3571 * group that is already under IO. Just wait for it to
3572 * finish and then do it all again
3573 */
3574 if (!list_empty(&cache->io_list)) {
3575 list_del_init(&cache->io_list);
3576 btrfs_wait_cache_io(root, trans, cache,
3577 &cache->io_ctl, path,
3578 cache->key.objectid);
3579 btrfs_put_block_group(cache);
3580 }
3581
3582
3583 /*
3584 * btrfs_wait_cache_io uses the cache->dirty_list to decide
3585 * if it should update the cache_state. Don't delete
3586 * until after we wait.
3587 *
3588 * Since we're not running in the commit critical section
3589 * we need the dirty_bgs_lock to protect from update_block_group
3590 */
3591 spin_lock(&cur_trans->dirty_bgs_lock);
3592 list_del_init(&cache->dirty_list);
3593 spin_unlock(&cur_trans->dirty_bgs_lock);
3594
3595 should_put = 1;
3596
3597 cache_save_setup(cache, trans, path);
3598
3599 if (cache->disk_cache_state == BTRFS_DC_SETUP) {
3600 cache->io_ctl.inode = NULL;
3601 ret = btrfs_write_out_cache(root, trans, cache, path);
3602 if (ret == 0 && cache->io_ctl.inode) {
3603 num_started++;
3604 should_put = 0;
3605
3606 /*
3607 * the cache_write_mutex is protecting
3608 * the io_list
3609 */
3610 list_add_tail(&cache->io_list, io);
3611 } else {
3612 /*
3613 * if we failed to write the cache, the
3614 * generation will be bad and life goes on
3615 */
3616 ret = 0;
3617 }
3618 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003619 if (!ret) {
Chris Mason1bbc6212015-04-06 12:46:08 -07003620 ret = write_one_cache_group(trans, root, path, cache);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003621 /*
3622 * Our block group might still be attached to the list
3623 * of new block groups in the transaction handle of some
3624 * other task (struct btrfs_trans_handle->new_bgs). This
3625 * means its block group item isn't yet in the extent
3626 * tree. If this happens ignore the error, as we will
3627 * try again later in the critical section of the
3628 * transaction commit.
3629 */
3630 if (ret == -ENOENT) {
3631 ret = 0;
3632 spin_lock(&cur_trans->dirty_bgs_lock);
3633 if (list_empty(&cache->dirty_list)) {
3634 list_add_tail(&cache->dirty_list,
3635 &cur_trans->dirty_bgs);
3636 btrfs_get_block_group(cache);
3637 }
3638 spin_unlock(&cur_trans->dirty_bgs_lock);
3639 } else if (ret) {
3640 btrfs_abort_transaction(trans, root, ret);
3641 }
3642 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003643
3644 /* if its not on the io list, we need to put the block group */
3645 if (should_put)
3646 btrfs_put_block_group(cache);
3647
3648 if (ret)
3649 break;
Filipe Mananab58d1a92015-04-25 18:29:16 +01003650
3651 /*
3652 * Avoid blocking other tasks for too long. It might even save
3653 * us from writing caches for block groups that are going to be
3654 * removed.
3655 */
3656 mutex_unlock(&trans->transaction->cache_write_mutex);
3657 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003658 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003659 mutex_unlock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003660
3661 /*
3662 * go through delayed refs for all the stuff we've just kicked off
3663 * and then loop back (just once)
3664 */
3665 ret = btrfs_run_delayed_refs(trans, root, 0);
3666 if (!ret && loops == 0) {
3667 loops++;
3668 spin_lock(&cur_trans->dirty_bgs_lock);
3669 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003670 /*
3671 * dirty_bgs_lock protects us from concurrent block group
3672 * deletes too (not just cache_write_mutex).
3673 */
3674 if (!list_empty(&dirty)) {
3675 spin_unlock(&cur_trans->dirty_bgs_lock);
3676 goto again;
3677 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003678 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Mason1bbc6212015-04-06 12:46:08 -07003679 }
3680
3681 btrfs_free_path(path);
3682 return ret;
3683}
3684
Chris Mason96b51792007-10-15 16:15:19 -04003685int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3686 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003687{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003688 struct btrfs_block_group_cache *cache;
Josef Bacikce93ec52014-11-17 15:45:48 -05003689 struct btrfs_transaction *cur_trans = trans->transaction;
3690 int ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003691 int should_put;
Chris Mason9078a3e2007-04-26 16:46:15 -04003692 struct btrfs_path *path;
Chris Mason1bbc6212015-04-06 12:46:08 -07003693 struct list_head *io = &cur_trans->io_bgs;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003694 int num_started = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003695
3696 path = btrfs_alloc_path();
3697 if (!path)
3698 return -ENOMEM;
3699
Josef Bacikce93ec52014-11-17 15:45:48 -05003700 /*
3701 * We don't need the lock here since we are protected by the transaction
3702 * commit. We want to do the cache_save_setup first and then run the
3703 * delayed refs to make sure we have the best chance at doing this all
3704 * in one shot.
3705 */
3706 while (!list_empty(&cur_trans->dirty_bgs)) {
3707 cache = list_first_entry(&cur_trans->dirty_bgs,
3708 struct btrfs_block_group_cache,
3709 dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003710
3711 /*
3712 * this can happen if cache_save_setup re-dirties a block
3713 * group that is already under IO. Just wait for it to
3714 * finish and then do it all again
3715 */
3716 if (!list_empty(&cache->io_list)) {
3717 list_del_init(&cache->io_list);
3718 btrfs_wait_cache_io(root, trans, cache,
3719 &cache->io_ctl, path,
3720 cache->key.objectid);
3721 btrfs_put_block_group(cache);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003722 }
3723
Chris Mason1bbc6212015-04-06 12:46:08 -07003724 /*
3725 * don't remove from the dirty list until after we've waited
3726 * on any pending IO
3727 */
Josef Bacikce93ec52014-11-17 15:45:48 -05003728 list_del_init(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003729 should_put = 1;
3730
Chris Mason1bbc6212015-04-06 12:46:08 -07003731 cache_save_setup(cache, trans, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003732
Josef Bacikce93ec52014-11-17 15:45:48 -05003733 if (!ret)
Chris Masonc9dc4c62015-04-04 17:14:42 -07003734 ret = btrfs_run_delayed_refs(trans, root, (unsigned long) -1);
3735
3736 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3737 cache->io_ctl.inode = NULL;
3738 ret = btrfs_write_out_cache(root, trans, cache, path);
3739 if (ret == 0 && cache->io_ctl.inode) {
3740 num_started++;
3741 should_put = 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003742 list_add_tail(&cache->io_list, io);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003743 } else {
3744 /*
3745 * if we failed to write the cache, the
3746 * generation will be bad and life goes on
3747 */
3748 ret = 0;
3749 }
3750 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003751 if (!ret) {
Josef Bacikce93ec52014-11-17 15:45:48 -05003752 ret = write_one_cache_group(trans, root, path, cache);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003753 if (ret)
3754 btrfs_abort_transaction(trans, root, ret);
3755 }
Chris Masonc9dc4c62015-04-04 17:14:42 -07003756
3757 /* if its not on the io list, we need to put the block group */
3758 if (should_put)
3759 btrfs_put_block_group(cache);
3760 }
3761
Chris Mason1bbc6212015-04-06 12:46:08 -07003762 while (!list_empty(io)) {
3763 cache = list_first_entry(io, struct btrfs_block_group_cache,
Chris Masonc9dc4c62015-04-04 17:14:42 -07003764 io_list);
3765 list_del_init(&cache->io_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003766 btrfs_wait_cache_io(root, trans, cache,
3767 &cache->io_ctl, path, cache->key.objectid);
Josef Bacik0af3d002010-06-21 14:48:16 -04003768 btrfs_put_block_group(cache);
3769 }
3770
Chris Mason9078a3e2007-04-26 16:46:15 -04003771 btrfs_free_path(path);
Josef Bacikce93ec52014-11-17 15:45:48 -05003772 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003773}
3774
Yan Zhengd2fb3432008-12-11 16:30:39 -05003775int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3776{
3777 struct btrfs_block_group_cache *block_group;
3778 int readonly = 0;
3779
3780 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3781 if (!block_group || block_group->ro)
3782 readonly = 1;
3783 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003784 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003785 return readonly;
3786}
3787
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003788static const char *alloc_name(u64 flags)
3789{
3790 switch (flags) {
3791 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
3792 return "mixed";
3793 case BTRFS_BLOCK_GROUP_METADATA:
3794 return "metadata";
3795 case BTRFS_BLOCK_GROUP_DATA:
3796 return "data";
3797 case BTRFS_BLOCK_GROUP_SYSTEM:
3798 return "system";
3799 default:
3800 WARN_ON(1);
3801 return "invalid-combination";
3802 };
3803}
3804
Chris Mason593060d2008-03-25 16:50:33 -04003805static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3806 u64 total_bytes, u64 bytes_used,
3807 struct btrfs_space_info **space_info)
3808{
3809 struct btrfs_space_info *found;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003810 int i;
3811 int factor;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003812 int ret;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003813
3814 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3815 BTRFS_BLOCK_GROUP_RAID10))
3816 factor = 2;
3817 else
3818 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003819
3820 found = __find_space_info(info, flags);
3821 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003822 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003823 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003824 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003825 found->bytes_used += bytes_used;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003826 found->disk_used += bytes_used * factor;
Filipe Manana2e6e5182015-05-12 00:28:11 +01003827 if (total_bytes > 0)
3828 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003829 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003830 *space_info = found;
3831 return 0;
3832 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003833 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003834 if (!found)
3835 return -ENOMEM;
3836
Tejun Heo908c7f12014-09-08 09:51:29 +09003837 ret = percpu_counter_init(&found->total_bytes_pinned, 0, GFP_KERNEL);
Josef Bacikb150a4f2013-06-19 15:00:04 -04003838 if (ret) {
3839 kfree(found);
3840 return ret;
3841 }
3842
Jeff Mahoneyc1895442014-05-27 12:59:57 -04003843 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
Yan, Zhengb742bb822010-05-16 10:46:24 -04003844 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003845 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003846 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003847 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003848 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003849 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003850 found->bytes_used = bytes_used;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003851 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003852 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003853 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003854 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003855 found->bytes_may_use = 0;
Filipe Manana6af3e3a2015-09-07 10:41:12 +01003856 found->full = 0;
Josef Bacik4f4db212015-09-29 11:40:47 -04003857 found->max_extent_size = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003858 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003859 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003860 found->flush = 0;
3861 init_waitqueue_head(&found->wait);
Josef Bacik633c0aa2014-10-31 09:49:34 -04003862 INIT_LIST_HEAD(&found->ro_bgs);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003863
3864 ret = kobject_init_and_add(&found->kobj, &space_info_ktype,
3865 info->space_info_kobj, "%s",
3866 alloc_name(found->flags));
3867 if (ret) {
3868 kfree(found);
3869 return ret;
3870 }
3871
Chris Mason593060d2008-03-25 16:50:33 -04003872 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003873 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003874 if (flags & BTRFS_BLOCK_GROUP_DATA)
3875 info->data_sinfo = found;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003876
3877 return ret;
Chris Mason593060d2008-03-25 16:50:33 -04003878}
3879
Chris Mason8790d502008-04-03 16:29:03 -04003880static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3881{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003882 u64 extra_flags = chunk_to_extended(flags) &
3883 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003884
Miao Xiede98ced2013-01-29 10:13:12 +00003885 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003886 if (flags & BTRFS_BLOCK_GROUP_DATA)
3887 fs_info->avail_data_alloc_bits |= extra_flags;
3888 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3889 fs_info->avail_metadata_alloc_bits |= extra_flags;
3890 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3891 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003892 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003893}
Chris Mason593060d2008-03-25 16:50:33 -04003894
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003895/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003896 * returns target flags in extended format or 0 if restripe for this
3897 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003898 *
3899 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003900 */
3901static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3902{
3903 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3904 u64 target = 0;
3905
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003906 if (!bctl)
3907 return 0;
3908
3909 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3910 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3911 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3912 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3913 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3914 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3915 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3916 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3917 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3918 }
3919
3920 return target;
3921}
3922
3923/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003924 * @flags: available profiles in extended format (see ctree.h)
3925 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003926 * Returns reduced profile in chunk format. If profile changing is in
3927 * progress (either running or paused) picks the target profile (if it's
3928 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003929 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003930static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003931{
Miao Xie95669972014-07-24 11:37:14 +08003932 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003933 u64 target;
Zhao Lei9c170b22015-09-15 21:08:08 +08003934 u64 raid_type;
3935 u64 allowed = 0;
Chris Masona061fc82008-05-07 11:43:44 -04003936
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003937 /*
3938 * see if restripe for this chunk_type is in progress, if so
3939 * try to reduce to the target profile
3940 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003941 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003942 target = get_restripe_target(root->fs_info, flags);
3943 if (target) {
3944 /* pick target profile only if it's already available */
3945 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003946 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003947 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003948 }
3949 }
3950 spin_unlock(&root->fs_info->balance_lock);
3951
David Woodhouse53b381b2013-01-29 18:40:14 -05003952 /* First, mask out the RAID levels which aren't possible */
Zhao Lei9c170b22015-09-15 21:08:08 +08003953 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
3954 if (num_devices >= btrfs_raid_array[raid_type].devs_min)
3955 allowed |= btrfs_raid_group[raid_type];
3956 }
3957 allowed &= flags;
Chris Masona061fc82008-05-07 11:43:44 -04003958
Zhao Lei9c170b22015-09-15 21:08:08 +08003959 if (allowed & BTRFS_BLOCK_GROUP_RAID6)
3960 allowed = BTRFS_BLOCK_GROUP_RAID6;
3961 else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
3962 allowed = BTRFS_BLOCK_GROUP_RAID5;
3963 else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
3964 allowed = BTRFS_BLOCK_GROUP_RAID10;
3965 else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
3966 allowed = BTRFS_BLOCK_GROUP_RAID1;
3967 else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
3968 allowed = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003969
Zhao Lei9c170b22015-09-15 21:08:08 +08003970 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
Chris Masonec44a352008-04-28 15:29:52 -04003971
Zhao Lei9c170b22015-09-15 21:08:08 +08003972 return extended_to_chunk(flags | allowed);
Chris Masonec44a352008-04-28 15:29:52 -04003973}
3974
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003975static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003976{
Miao Xiede98ced2013-01-29 10:13:12 +00003977 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003978 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003979
3980 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003981 flags = orig_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003982 seq = read_seqbegin(&root->fs_info->profiles_lock);
3983
3984 if (flags & BTRFS_BLOCK_GROUP_DATA)
3985 flags |= root->fs_info->avail_data_alloc_bits;
3986 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3987 flags |= root->fs_info->avail_system_alloc_bits;
3988 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3989 flags |= root->fs_info->avail_metadata_alloc_bits;
3990 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003991
Yan, Zhengb742bb822010-05-16 10:46:24 -04003992 return btrfs_reduce_alloc_profile(root, flags);
3993}
Josef Bacik6a632092009-02-20 11:00:09 -05003994
Miao Xie6d07bce2011-01-05 10:07:31 +00003995u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04003996{
3997 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003998 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003999
Yan, Zhengb742bb822010-05-16 10:46:24 -04004000 if (data)
4001 flags = BTRFS_BLOCK_GROUP_DATA;
4002 else if (root == root->fs_info->chunk_root)
4003 flags = BTRFS_BLOCK_GROUP_SYSTEM;
4004 else
4005 flags = BTRFS_BLOCK_GROUP_METADATA;
4006
David Woodhouse53b381b2013-01-29 18:40:14 -05004007 ret = get_alloc_profile(root, flags);
4008 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004009}
4010
Qu Wenruo4ceff072015-09-08 17:22:42 +08004011int btrfs_alloc_data_chunk_ondemand(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05004012{
4013 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004014 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004015 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00004016 u64 used;
Zhao Lei94b947b2015-02-14 13:23:45 +08004017 int ret = 0;
Zhao Leic99f1b02015-03-02 19:32:20 +08004018 int need_commit = 2;
4019 int have_pinned_space;
Josef Bacik6a632092009-02-20 11:00:09 -05004020
4021 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00004022 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05004023
Miao Xie9dced182013-10-25 17:33:36 +08004024 if (btrfs_is_free_space_inode(inode)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004025 need_commit = 0;
Miao Xie9dced182013-10-25 17:33:36 +08004026 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04004027 }
4028
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004029 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04004030 if (!data_sinfo)
4031 goto alloc;
4032
Josef Bacik6a632092009-02-20 11:00:09 -05004033again:
4034 /* make sure we have enough space to handle the data first */
4035 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004036 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
4037 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
4038 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00004039
4040 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004041 struct btrfs_trans_handle *trans;
4042
Josef Bacik6a632092009-02-20 11:00:09 -05004043 /*
4044 * if we don't have enough free bytes in this space then we need
4045 * to alloc a new chunk.
4046 */
Zhao Leib9fd47c2015-02-09 14:40:20 +08004047 if (!data_sinfo->full) {
Josef Bacik6a632092009-02-20 11:00:09 -05004048 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05004049
Chris Mason0e4f8f82011-04-15 16:05:44 -04004050 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05004051 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04004052alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05004053 alloc_target = btrfs_get_alloc_profile(root, 1);
Miao Xie9dced182013-10-25 17:33:36 +08004054 /*
4055 * It is ugly that we don't call nolock join
4056 * transaction for the free space inode case here.
4057 * But it is safe because we only do the data space
4058 * reservation for the free space cache in the
4059 * transaction context, the common join transaction
4060 * just increase the counter of the current transaction
4061 * handler, doesn't try to acquire the trans_lock of
4062 * the fs.
4063 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004064 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004065 if (IS_ERR(trans))
4066 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05004067
4068 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04004069 alloc_target,
4070 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05004071 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00004072 if (ret < 0) {
4073 if (ret != -ENOSPC)
4074 return ret;
Zhao Leic99f1b02015-03-02 19:32:20 +08004075 else {
4076 have_pinned_space = 1;
Miao Xied52a5b52011-01-05 10:07:18 +00004077 goto commit_trans;
Zhao Leic99f1b02015-03-02 19:32:20 +08004078 }
Miao Xied52a5b52011-01-05 10:07:18 +00004079 }
Chris Mason33b4d472009-09-22 14:45:50 -04004080
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004081 if (!data_sinfo)
4082 data_sinfo = fs_info->data_sinfo;
4083
Josef Bacik6a632092009-02-20 11:00:09 -05004084 goto again;
4085 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004086
4087 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04004088 * If we don't have enough pinned space to deal with this
Zhao Lei94b947b2015-02-14 13:23:45 +08004089 * allocation, and no removed chunk in current transaction,
4090 * don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004091 */
Zhao Leic99f1b02015-03-02 19:32:20 +08004092 have_pinned_space = percpu_counter_compare(
4093 &data_sinfo->total_bytes_pinned,
4094 used + bytes - data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05004095 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004096
4097 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00004098commit_trans:
Zhao Leic99f1b02015-03-02 19:32:20 +08004099 if (need_commit &&
Josef Bacika4abeea2011-04-11 17:25:13 -04004100 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004101 need_commit--;
Josef Bacikb150a4f2013-06-19 15:00:04 -04004102
Zhao Lei9a4e7272015-04-09 12:34:43 +08004103 if (need_commit > 0)
4104 btrfs_wait_ordered_roots(fs_info, -1);
4105
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004106 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004107 if (IS_ERR(trans))
4108 return PTR_ERR(trans);
Zhao Leic99f1b02015-03-02 19:32:20 +08004109 if (have_pinned_space >= 0 ||
Josef Bacik3204d332015-09-24 10:46:10 -04004110 test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
4111 &trans->transaction->flags) ||
Zhao Leic99f1b02015-03-02 19:32:20 +08004112 need_commit > 0) {
Zhao Lei94b947b2015-02-14 13:23:45 +08004113 ret = btrfs_commit_transaction(trans, root);
4114 if (ret)
4115 return ret;
Zhao Leid7c15172015-02-26 10:49:20 +08004116 /*
4117 * make sure that all running delayed iput are
4118 * done
4119 */
4120 down_write(&root->fs_info->delayed_iput_sem);
4121 up_write(&root->fs_info->delayed_iput_sem);
Zhao Lei94b947b2015-02-14 13:23:45 +08004122 goto again;
4123 } else {
4124 btrfs_end_transaction(trans, root);
4125 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004126 }
4127
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004128 trace_btrfs_space_reservation(root->fs_info,
4129 "space_info:enospc",
4130 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004131 return -ENOSPC;
4132 }
4133 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004134 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004135 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004136 spin_unlock(&data_sinfo->lock);
4137
Dongsheng Yang237c0e92014-12-29 06:23:05 -05004138 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004139}
4140
4141/*
Qu Wenruo4ceff072015-09-08 17:22:42 +08004142 * New check_data_free_space() with ability for precious data reservation
4143 * Will replace old btrfs_check_data_free_space(), but for patch split,
4144 * add a new function first and then replace it.
4145 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004146int btrfs_check_data_free_space(struct inode *inode, u64 start, u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004147{
4148 struct btrfs_root *root = BTRFS_I(inode)->root;
4149 int ret;
4150
4151 /* align the range */
4152 len = round_up(start + len, root->sectorsize) -
4153 round_down(start, root->sectorsize);
4154 start = round_down(start, root->sectorsize);
4155
4156 ret = btrfs_alloc_data_chunk_ondemand(inode, len);
4157 if (ret < 0)
4158 return ret;
4159
Qu Wenruo94ed9382015-09-08 17:25:56 +08004160 /*
4161 * Use new btrfs_qgroup_reserve_data to reserve precious data space
4162 *
4163 * TODO: Find a good method to avoid reserve data space for NOCOW
4164 * range, but don't impact performance on quota disable case.
4165 */
Qu Wenruo4ceff072015-09-08 17:22:42 +08004166 ret = btrfs_qgroup_reserve_data(inode, start, len);
4167 return ret;
4168}
4169
4170/*
Qu Wenruo4ceff072015-09-08 17:22:42 +08004171 * Called if we need to clear a data reservation for this inode
4172 * Normally in a error case.
4173 *
Qu Wenruo51773be2015-10-08 18:19:37 +08004174 * This one will *NOT* use accurate qgroup reserved space API, just for case
4175 * which we can't sleep and is sure it won't affect qgroup reserved space.
4176 * Like clear_bit_hook().
Qu Wenruo4ceff072015-09-08 17:22:42 +08004177 */
Qu Wenruo51773be2015-10-08 18:19:37 +08004178void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
4179 u64 len)
Qu Wenruo4ceff072015-09-08 17:22:42 +08004180{
4181 struct btrfs_root *root = BTRFS_I(inode)->root;
4182 struct btrfs_space_info *data_sinfo;
4183
4184 /* Make sure the range is aligned to sectorsize */
4185 len = round_up(start + len, root->sectorsize) -
4186 round_down(start, root->sectorsize);
4187 start = round_down(start, root->sectorsize);
4188
Qu Wenruo4ceff072015-09-08 17:22:42 +08004189 data_sinfo = root->fs_info->data_sinfo;
4190 spin_lock(&data_sinfo->lock);
4191 if (WARN_ON(data_sinfo->bytes_may_use < len))
4192 data_sinfo->bytes_may_use = 0;
4193 else
4194 data_sinfo->bytes_may_use -= len;
4195 trace_btrfs_space_reservation(root->fs_info, "space_info",
4196 data_sinfo->flags, len, 0);
4197 spin_unlock(&data_sinfo->lock);
4198}
4199
Qu Wenruo51773be2015-10-08 18:19:37 +08004200/*
4201 * Called if we need to clear a data reservation for this inode
4202 * Normally in a error case.
4203 *
4204 * This one will handle the per-indoe data rsv map for accurate reserved
4205 * space framework.
4206 */
4207void btrfs_free_reserved_data_space(struct inode *inode, u64 start, u64 len)
4208{
4209 btrfs_free_reserved_data_space_noquota(inode, start, len);
4210 btrfs_qgroup_free_data(inode, start, len);
4211}
4212
Josef Bacik97e728d2009-04-21 17:40:57 -04004213static void force_metadata_allocation(struct btrfs_fs_info *info)
4214{
4215 struct list_head *head = &info->space_info;
4216 struct btrfs_space_info *found;
4217
4218 rcu_read_lock();
4219 list_for_each_entry_rcu(found, head, list) {
4220 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004221 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04004222 }
4223 rcu_read_unlock();
4224}
4225
Miao Xie3c76cd82013-04-25 10:12:38 +00004226static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
4227{
4228 return (global->size << 1);
4229}
4230
Chris Masone5bc2452010-10-26 13:37:56 -04004231static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04004232 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04004233{
Josef Bacikfb25e912011-07-26 17:00:46 -04004234 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04004235 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04004236 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04004237 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04004238
Chris Mason0e4f8f82011-04-15 16:05:44 -04004239 if (force == CHUNK_ALLOC_FORCE)
4240 return 1;
4241
4242 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04004243 * We need to take into account the global rsv because for all intents
4244 * and purposes it's used space. Don't worry about locking the
4245 * global_rsv, it doesn't change except when the transaction commits.
4246 */
Josef Bacik54338b52012-08-14 16:20:52 -04004247 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Miao Xie3c76cd82013-04-25 10:12:38 +00004248 num_allocated += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04004249
4250 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04004251 * in limited mode, we want to have some free space up to
4252 * about 1% of the FS size.
4253 */
4254 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02004255 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04004256 thresh = max_t(u64, 64 * 1024 * 1024,
4257 div_factor_fine(thresh, 1));
4258
4259 if (num_bytes - num_allocated < thresh)
4260 return 1;
4261 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004262
Josef Bacik698d0082012-09-12 14:08:47 -04004263 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04004264 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04004265 return 1;
4266}
4267
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004268static u64 get_profile_num_devs(struct btrfs_root *root, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004269{
4270 u64 num_dev;
4271
David Woodhouse53b381b2013-01-29 18:40:14 -05004272 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
4273 BTRFS_BLOCK_GROUP_RAID0 |
4274 BTRFS_BLOCK_GROUP_RAID5 |
4275 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04004276 num_dev = root->fs_info->fs_devices->rw_devices;
4277 else if (type & BTRFS_BLOCK_GROUP_RAID1)
4278 num_dev = 2;
4279 else
4280 num_dev = 1; /* DUP or single */
4281
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004282 return num_dev;
Liu Bo15d1ff82012-03-29 09:57:44 -04004283}
4284
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004285/*
4286 * If @is_allocation is true, reserve space in the system space info necessary
4287 * for allocating a chunk, otherwise if it's false, reserve space necessary for
4288 * removing a chunk.
4289 */
4290void check_system_chunk(struct btrfs_trans_handle *trans,
4291 struct btrfs_root *root,
Filipe Manana4617ea32015-06-09 17:48:21 +01004292 u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004293{
4294 struct btrfs_space_info *info;
4295 u64 left;
4296 u64 thresh;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004297 int ret = 0;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004298 u64 num_devs;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004299
4300 /*
4301 * Needed because we can end up allocating a system chunk and for an
4302 * atomic and race free space reservation in the chunk block reserve.
4303 */
4304 ASSERT(mutex_is_locked(&root->fs_info->chunk_mutex));
Liu Bo15d1ff82012-03-29 09:57:44 -04004305
4306 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4307 spin_lock(&info->lock);
4308 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004309 info->bytes_reserved - info->bytes_readonly -
4310 info->bytes_may_use;
Liu Bo15d1ff82012-03-29 09:57:44 -04004311 spin_unlock(&info->lock);
4312
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004313 num_devs = get_profile_num_devs(root, type);
4314
4315 /* num_devs device items to update and 1 chunk item to add or remove */
Filipe Manana4617ea32015-06-09 17:48:21 +01004316 thresh = btrfs_calc_trunc_metadata_size(root, num_devs) +
4317 btrfs_calc_trans_metadata_size(root, 1);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004318
Liu Bo15d1ff82012-03-29 09:57:44 -04004319 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004320 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
4321 left, thresh, type);
Liu Bo15d1ff82012-03-29 09:57:44 -04004322 dump_space_info(info, 0, 0);
4323 }
4324
4325 if (left < thresh) {
4326 u64 flags;
4327
4328 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004329 /*
4330 * Ignore failure to create system chunk. We might end up not
4331 * needing it, as we might not need to COW all nodes/leafs from
4332 * the paths we visit in the chunk tree (they were already COWed
4333 * or created in the current transaction for example).
4334 */
4335 ret = btrfs_alloc_chunk(trans, root, flags);
4336 }
4337
4338 if (!ret) {
4339 ret = btrfs_block_rsv_add(root->fs_info->chunk_root,
4340 &root->fs_info->chunk_block_rsv,
4341 thresh, BTRFS_RESERVE_NO_FLUSH);
4342 if (!ret)
4343 trans->chunk_bytes_reserved += thresh;
Liu Bo15d1ff82012-03-29 09:57:44 -04004344 }
4345}
4346
Chris Mason6324fbf2008-03-24 15:01:59 -04004347static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04004348 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04004349{
4350 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04004351 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04004352 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05004353 int ret = 0;
4354
Josef Bacikc6b305a2012-12-18 09:16:16 -05004355 /* Don't re-enter if we're already allocating a chunk */
4356 if (trans->allocating_chunk)
4357 return -ENOSPC;
4358
Chris Mason6324fbf2008-03-24 15:01:59 -04004359 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04004360 if (!space_info) {
4361 ret = update_space_info(extent_root->fs_info, flags,
4362 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004363 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04004364 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004365 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04004366
Josef Bacik6d741192011-04-11 20:20:11 -04004367again:
Josef Bacik25179202008-10-29 14:49:05 -04004368 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05004369 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004370 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04004371 if (space_info->full) {
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004372 if (should_alloc_chunk(extent_root, space_info, force))
4373 ret = -ENOSPC;
4374 else
4375 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04004376 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004377 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04004378 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004379
Josef Bacik698d0082012-09-12 14:08:47 -04004380 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04004381 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04004382 return 0;
4383 } else if (space_info->chunk_alloc) {
4384 wait_for_alloc = 1;
4385 } else {
4386 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04004387 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004388
Josef Bacik25179202008-10-29 14:49:05 -04004389 spin_unlock(&space_info->lock);
4390
Josef Bacik6d741192011-04-11 20:20:11 -04004391 mutex_lock(&fs_info->chunk_mutex);
4392
4393 /*
4394 * The chunk_mutex is held throughout the entirety of a chunk
4395 * allocation, so once we've acquired the chunk_mutex we know that the
4396 * other guy is done and we need to recheck and see if we should
4397 * allocate.
4398 */
4399 if (wait_for_alloc) {
4400 mutex_unlock(&fs_info->chunk_mutex);
4401 wait_for_alloc = 0;
4402 goto again;
4403 }
4404
Josef Bacikc6b305a2012-12-18 09:16:16 -05004405 trans->allocating_chunk = true;
4406
Josef Bacik97e728d2009-04-21 17:40:57 -04004407 /*
Josef Bacik67377732010-09-16 16:19:09 -04004408 * If we have mixed data/metadata chunks we want to make sure we keep
4409 * allocating mixed chunks instead of individual chunks.
4410 */
4411 if (btrfs_mixed_space_info(space_info))
4412 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
4413
4414 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04004415 * if we're doing a data chunk, go ahead and make sure that
4416 * we keep a reasonable number of metadata chunks allocated in the
4417 * FS as well.
4418 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04004419 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04004420 fs_info->data_chunk_allocations++;
4421 if (!(fs_info->data_chunk_allocations %
4422 fs_info->metadata_ratio))
4423 force_metadata_allocation(fs_info);
4424 }
4425
Liu Bo15d1ff82012-03-29 09:57:44 -04004426 /*
4427 * Check if we have enough space in SYSTEM chunk because we may need
4428 * to update devices.
4429 */
Filipe Manana4617ea32015-06-09 17:48:21 +01004430 check_system_chunk(trans, extent_root, flags);
Liu Bo15d1ff82012-03-29 09:57:44 -04004431
Yan Zheng2b820322008-11-17 21:11:30 -05004432 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05004433 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004434
Josef Bacik9ed74f22009-09-11 16:12:44 -04004435 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004436 if (ret < 0 && ret != -ENOSPC)
4437 goto out;
Chris Masond3977122009-01-05 21:25:51 -05004438 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04004439 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04004440 else
4441 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04004442
Chris Mason0e4f8f82011-04-15 16:05:44 -04004443 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004444out:
Josef Bacik6d741192011-04-11 20:20:11 -04004445 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004446 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03004447 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana00d80e32015-07-20 14:56:20 +01004448 /*
4449 * When we allocate a new chunk we reserve space in the chunk block
4450 * reserve to make sure we can COW nodes/leafs in the chunk tree or
4451 * add new nodes/leafs to it if we end up needing to do it when
4452 * inserting the chunk item and updating device items as part of the
4453 * second phase of chunk allocation, performed by
4454 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
4455 * large number of new block groups to create in our transaction
4456 * handle's new_bgs list to avoid exhausting the chunk block reserve
4457 * in extreme cases - like having a single transaction create many new
4458 * block groups when starting to write out the free space caches of all
4459 * the block groups that were made dirty during the lifetime of the
4460 * transaction.
4461 */
Filipe Mananad9a05402015-10-03 13:13:13 +01004462 if (trans->can_flush_pending_bgs &&
4463 trans->chunk_bytes_reserved >= (2 * 1024 * 1024ull)) {
Filipe Manana00d80e32015-07-20 14:56:20 +01004464 btrfs_create_pending_block_groups(trans, trans->root);
4465 btrfs_trans_release_chunk_metadata(trans);
4466 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004467 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04004468}
4469
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004470static int can_overcommit(struct btrfs_root *root,
4471 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00004472 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004473{
Josef Bacik96f1bb52013-01-30 17:02:51 -05004474 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004475 u64 profile = btrfs_get_alloc_profile(root, 0);
Miao Xie3c76cd82013-04-25 10:12:38 +00004476 u64 space_size;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004477 u64 avail;
4478 u64 used;
4479
4480 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05004481 space_info->bytes_pinned + space_info->bytes_readonly;
4482
Josef Bacik96f1bb52013-01-30 17:02:51 -05004483 /*
4484 * We only want to allow over committing if we have lots of actual space
4485 * free, but if we don't have enough space to handle the global reserve
4486 * space then we could end up having a real enospc problem when trying
4487 * to allocate a chunk or some other such important allocation.
4488 */
Miao Xie3c76cd82013-04-25 10:12:38 +00004489 spin_lock(&global_rsv->lock);
4490 space_size = calc_global_rsv_need_space(global_rsv);
4491 spin_unlock(&global_rsv->lock);
4492 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05004493 return 0;
4494
4495 used += space_info->bytes_may_use;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004496
4497 spin_lock(&root->fs_info->free_chunk_lock);
4498 avail = root->fs_info->free_chunk_space;
4499 spin_unlock(&root->fs_info->free_chunk_lock);
4500
4501 /*
4502 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05004503 * space is actually useable. For raid56, the space info used
4504 * doesn't include the parity drive, so we don't have to
4505 * change the math
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004506 */
4507 if (profile & (BTRFS_BLOCK_GROUP_DUP |
4508 BTRFS_BLOCK_GROUP_RAID1 |
4509 BTRFS_BLOCK_GROUP_RAID10))
4510 avail >>= 1;
4511
4512 /*
Miao Xie561c2942012-10-16 11:32:18 +00004513 * If we aren't flushing all things, let us overcommit up to
4514 * 1/2th of the space. If we can flush, don't let us overcommit
4515 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004516 */
Miao Xie08e007d2012-10-16 11:33:38 +00004517 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04004518 avail >>= 3;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004519 else
Josef Bacik14575ae2013-09-17 10:48:00 -04004520 avail >>= 1;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004521
Josef Bacik14575ae2013-09-17 10:48:00 -04004522 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004523 return 1;
4524 return 0;
4525}
4526
Eric Sandeen48a3b632013-04-25 20:41:01 +00004527static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
Miao Xie6c255e62014-03-06 13:55:01 +08004528 unsigned long nr_pages, int nr_items)
Miao Xieda633a42012-12-20 11:19:09 +00004529{
4530 struct super_block *sb = root->fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00004531
Josef Bacik925a6ef2013-06-20 12:31:27 -04004532 if (down_read_trylock(&sb->s_umount)) {
4533 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4534 up_read(&sb->s_umount);
4535 } else {
Miao Xieda633a42012-12-20 11:19:09 +00004536 /*
4537 * We needn't worry the filesystem going from r/w to r/o though
4538 * we don't acquire ->s_umount mutex, because the filesystem
4539 * should guarantee the delalloc inodes list be empty after
4540 * the filesystem is readonly(all dirty pages are written to
4541 * the disk).
4542 */
Miao Xie6c255e62014-03-06 13:55:01 +08004543 btrfs_start_delalloc_roots(root->fs_info, 0, nr_items);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004544 if (!current->journal_info)
Miao Xie6c255e62014-03-06 13:55:01 +08004545 btrfs_wait_ordered_roots(root->fs_info, nr_items);
Miao Xieda633a42012-12-20 11:19:09 +00004546 }
4547}
4548
Miao Xie18cd8ea2013-11-04 23:13:22 +08004549static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim)
4550{
4551 u64 bytes;
4552 int nr;
4553
4554 bytes = btrfs_calc_trans_metadata_size(root, 1);
4555 nr = (int)div64_u64(to_reclaim, bytes);
4556 if (!nr)
4557 nr = 1;
4558 return nr;
4559}
4560
Miao Xiec61a16a2013-11-04 23:13:23 +08004561#define EXTENT_SIZE_PER_ITEM (256 * 1024)
4562
Yan, Zheng5da9d012010-05-16 10:46:25 -04004563/*
4564 * shrink metadata reservation for delalloc
4565 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04004566static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4567 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004568{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004569 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004570 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004571 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004572 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004573 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004574 long time_left;
Miao Xied3ee29e32013-11-04 23:13:20 +08004575 unsigned long nr_pages;
4576 int loops;
Miao Xieb0244192013-11-04 23:13:25 +08004577 int items;
Miao Xie08e007d2012-10-16 11:33:38 +00004578 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004579
Miao Xiec61a16a2013-11-04 23:13:23 +08004580 /* Calc the number of the pages we need flush for space reservation */
Miao Xieb0244192013-11-04 23:13:25 +08004581 items = calc_reclaim_items_nr(root, to_reclaim);
4582 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004583
Josef Bacik663350a2011-11-03 22:54:25 -04004584 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004585 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004586 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04004587
Miao Xie963d6782013-01-29 10:10:51 +00004588 delalloc_bytes = percpu_counter_sum_positive(
4589 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004590 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004591 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004592 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004593 if (wait_ordered)
Miao Xieb0244192013-11-04 23:13:25 +08004594 btrfs_wait_ordered_roots(root->fs_info, items);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004595 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004596 }
4597
Miao Xied3ee29e32013-11-04 23:13:20 +08004598 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004599 while (delalloc_bytes && loops < 3) {
4600 max_reclaim = min(delalloc_bytes, to_reclaim);
4601 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xie6c255e62014-03-06 13:55:01 +08004602 btrfs_writeback_inodes_sb_nr(root, nr_pages, items);
Josef Bacikdea31f52012-09-06 16:47:00 -04004603 /*
4604 * We need to wait for the async pages to actually start before
4605 * we do anything.
4606 */
Miao Xie9f3a0742013-11-04 23:13:24 +08004607 max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages);
4608 if (!max_reclaim)
4609 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004610
Miao Xie9f3a0742013-11-04 23:13:24 +08004611 if (max_reclaim <= nr_pages)
4612 max_reclaim = 0;
4613 else
4614 max_reclaim -= nr_pages;
4615
4616 wait_event(root->fs_info->async_submit_wait,
4617 atomic_read(&root->fs_info->async_delalloc_pages) <=
4618 (int)max_reclaim);
4619skip_async:
Miao Xie08e007d2012-10-16 11:33:38 +00004620 if (!trans)
4621 flush = BTRFS_RESERVE_FLUSH_ALL;
4622 else
4623 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04004624 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00004625 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004626 spin_unlock(&space_info->lock);
4627 break;
4628 }
Josef Bacik0019f102010-10-15 15:18:40 -04004629 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004630
Chris Mason36e39c42011-03-12 07:08:42 -05004631 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004632 if (wait_ordered && !trans) {
Miao Xieb0244192013-11-04 23:13:25 +08004633 btrfs_wait_ordered_roots(root->fs_info, items);
Josef Bacikf104d042011-10-14 13:56:58 -04004634 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004635 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004636 if (time_left)
4637 break;
4638 }
Miao Xie963d6782013-01-29 10:10:51 +00004639 delalloc_bytes = percpu_counter_sum_positive(
4640 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004641 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004642}
4643
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004644/**
Josef Bacik663350a2011-11-03 22:54:25 -04004645 * maybe_commit_transaction - possibly commit the transaction if its ok to
4646 * @root - the root we're allocating for
4647 * @bytes - the number of bytes we want to reserve
4648 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004649 *
Josef Bacik663350a2011-11-03 22:54:25 -04004650 * This will check to make sure that committing the transaction will actually
4651 * get us somewhere and then commit the transaction if it does. Otherwise it
4652 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004653 */
Josef Bacik663350a2011-11-03 22:54:25 -04004654static int may_commit_transaction(struct btrfs_root *root,
4655 struct btrfs_space_info *space_info,
4656 u64 bytes, int force)
4657{
4658 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4659 struct btrfs_trans_handle *trans;
4660
4661 trans = (struct btrfs_trans_handle *)current->journal_info;
4662 if (trans)
4663 return -EAGAIN;
4664
4665 if (force)
4666 goto commit;
4667
4668 /* See if there is enough pinned space to make this reservation */
Josef Bacikb150a4f2013-06-19 15:00:04 -04004669 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Miao Xie0424c542014-03-06 13:54:59 +08004670 bytes) >= 0)
Josef Bacik663350a2011-11-03 22:54:25 -04004671 goto commit;
Josef Bacik663350a2011-11-03 22:54:25 -04004672
4673 /*
4674 * See if there is some space in the delayed insertion reservation for
4675 * this reservation.
4676 */
4677 if (space_info != delayed_rsv->space_info)
4678 return -ENOSPC;
4679
4680 spin_lock(&delayed_rsv->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004681 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4682 bytes - delayed_rsv->size) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004683 spin_unlock(&delayed_rsv->lock);
4684 return -ENOSPC;
4685 }
4686 spin_unlock(&delayed_rsv->lock);
4687
4688commit:
4689 trans = btrfs_join_transaction(root);
4690 if (IS_ERR(trans))
4691 return -ENOSPC;
4692
4693 return btrfs_commit_transaction(trans, root);
4694}
4695
Josef Bacik96c3f432012-06-21 14:05:49 -04004696enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04004697 FLUSH_DELAYED_ITEMS_NR = 1,
4698 FLUSH_DELAYED_ITEMS = 2,
4699 FLUSH_DELALLOC = 3,
4700 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04004701 ALLOC_CHUNK = 5,
4702 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04004703};
4704
4705static int flush_space(struct btrfs_root *root,
4706 struct btrfs_space_info *space_info, u64 num_bytes,
4707 u64 orig_bytes, int state)
4708{
4709 struct btrfs_trans_handle *trans;
4710 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004711 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004712
4713 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004714 case FLUSH_DELAYED_ITEMS_NR:
4715 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08004716 if (state == FLUSH_DELAYED_ITEMS_NR)
4717 nr = calc_reclaim_items_nr(root, num_bytes) * 2;
4718 else
Josef Bacik96c3f432012-06-21 14:05:49 -04004719 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004720
Josef Bacik96c3f432012-06-21 14:05:49 -04004721 trans = btrfs_join_transaction(root);
4722 if (IS_ERR(trans)) {
4723 ret = PTR_ERR(trans);
4724 break;
4725 }
4726 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4727 btrfs_end_transaction(trans, root);
4728 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004729 case FLUSH_DELALLOC:
4730 case FLUSH_DELALLOC_WAIT:
Miao Xie24af7dd2014-03-06 13:55:00 +08004731 shrink_delalloc(root, num_bytes * 2, orig_bytes,
Josef Bacik67b0fd62012-09-24 13:42:00 -04004732 state == FLUSH_DELALLOC_WAIT);
4733 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004734 case ALLOC_CHUNK:
4735 trans = btrfs_join_transaction(root);
4736 if (IS_ERR(trans)) {
4737 ret = PTR_ERR(trans);
4738 break;
4739 }
4740 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004741 btrfs_get_alloc_profile(root, 0),
4742 CHUNK_ALLOC_NO_FORCE);
4743 btrfs_end_transaction(trans, root);
4744 if (ret == -ENOSPC)
4745 ret = 0;
4746 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004747 case COMMIT_TRANS:
4748 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4749 break;
4750 default:
4751 ret = -ENOSPC;
4752 break;
4753 }
4754
4755 return ret;
4756}
Miao Xie21c7e752014-05-13 17:29:04 -07004757
4758static inline u64
4759btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
4760 struct btrfs_space_info *space_info)
4761{
4762 u64 used;
4763 u64 expected;
4764 u64 to_reclaim;
4765
4766 to_reclaim = min_t(u64, num_online_cpus() * 1024 * 1024,
4767 16 * 1024 * 1024);
4768 spin_lock(&space_info->lock);
4769 if (can_overcommit(root, space_info, to_reclaim,
4770 BTRFS_RESERVE_FLUSH_ALL)) {
4771 to_reclaim = 0;
4772 goto out;
4773 }
4774
4775 used = space_info->bytes_used + space_info->bytes_reserved +
4776 space_info->bytes_pinned + space_info->bytes_readonly +
4777 space_info->bytes_may_use;
4778 if (can_overcommit(root, space_info, 1024 * 1024,
4779 BTRFS_RESERVE_FLUSH_ALL))
4780 expected = div_factor_fine(space_info->total_bytes, 95);
4781 else
4782 expected = div_factor_fine(space_info->total_bytes, 90);
4783
4784 if (used > expected)
4785 to_reclaim = used - expected;
4786 else
4787 to_reclaim = 0;
4788 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
4789 space_info->bytes_reserved);
4790out:
4791 spin_unlock(&space_info->lock);
4792
4793 return to_reclaim;
4794}
4795
4796static inline int need_do_async_reclaim(struct btrfs_space_info *space_info,
4797 struct btrfs_fs_info *fs_info, u64 used)
4798{
Josef Bacik365c5312015-02-18 13:58:15 -08004799 u64 thresh = div_factor_fine(space_info->total_bytes, 98);
4800
4801 /* If we're just plain full then async reclaim just slows us down. */
4802 if (space_info->bytes_used >= thresh)
4803 return 0;
4804
4805 return (used >= thresh && !btrfs_fs_closing(fs_info) &&
Miao Xie21c7e752014-05-13 17:29:04 -07004806 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
4807}
4808
4809static int btrfs_need_do_async_reclaim(struct btrfs_space_info *space_info,
Liu Bo25ce4592014-09-10 12:58:50 +08004810 struct btrfs_fs_info *fs_info,
4811 int flush_state)
Miao Xie21c7e752014-05-13 17:29:04 -07004812{
4813 u64 used;
4814
4815 spin_lock(&space_info->lock);
Liu Bo25ce4592014-09-10 12:58:50 +08004816 /*
4817 * We run out of space and have not got any free space via flush_space,
4818 * so don't bother doing async reclaim.
4819 */
4820 if (flush_state > COMMIT_TRANS && space_info->full) {
4821 spin_unlock(&space_info->lock);
4822 return 0;
4823 }
4824
Miao Xie21c7e752014-05-13 17:29:04 -07004825 used = space_info->bytes_used + space_info->bytes_reserved +
4826 space_info->bytes_pinned + space_info->bytes_readonly +
4827 space_info->bytes_may_use;
4828 if (need_do_async_reclaim(space_info, fs_info, used)) {
4829 spin_unlock(&space_info->lock);
4830 return 1;
4831 }
4832 spin_unlock(&space_info->lock);
4833
4834 return 0;
4835}
4836
4837static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
4838{
4839 struct btrfs_fs_info *fs_info;
4840 struct btrfs_space_info *space_info;
4841 u64 to_reclaim;
4842 int flush_state;
4843
4844 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
4845 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4846
4847 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
4848 space_info);
4849 if (!to_reclaim)
4850 return;
4851
4852 flush_state = FLUSH_DELAYED_ITEMS_NR;
4853 do {
4854 flush_space(fs_info->fs_root, space_info, to_reclaim,
4855 to_reclaim, flush_state);
4856 flush_state++;
Liu Bo25ce4592014-09-10 12:58:50 +08004857 if (!btrfs_need_do_async_reclaim(space_info, fs_info,
4858 flush_state))
Miao Xie21c7e752014-05-13 17:29:04 -07004859 return;
Josef Bacik365c5312015-02-18 13:58:15 -08004860 } while (flush_state < COMMIT_TRANS);
Miao Xie21c7e752014-05-13 17:29:04 -07004861}
4862
4863void btrfs_init_async_reclaim_work(struct work_struct *work)
4864{
4865 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
4866}
4867
Josef Bacik663350a2011-11-03 22:54:25 -04004868/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004869 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4870 * @root - the root we're allocating for
4871 * @block_rsv - the block_rsv we're allocating for
4872 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004873 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004874 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004875 * This will reserve orgi_bytes number of bytes from the space info associated
4876 * with the block_rsv. If there is not enough space it will make an attempt to
4877 * flush out space to make room. It will do this by flushing delalloc if
4878 * possible or committing the transaction. If flush is 0 then no attempts to
4879 * regain reservations will be made and this will fail if there is not enough
4880 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004881 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004882static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004883 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004884 u64 orig_bytes,
4885 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004886{
4887 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004888 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004889 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004890 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004891 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004892 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004893
4894again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004895 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004896 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004897 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004898 * We only want to wait if somebody other than us is flushing and we
4899 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004900 */
Miao Xie08e007d2012-10-16 11:33:38 +00004901 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4902 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004903 spin_unlock(&space_info->lock);
4904 /*
4905 * If we have a trans handle we can't wait because the flusher
4906 * may have to commit the transaction, which would mean we would
4907 * deadlock since we are waiting for the flusher to finish, but
4908 * hold the current transaction open.
4909 */
Josef Bacik663350a2011-11-03 22:54:25 -04004910 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004911 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004912 ret = wait_event_killable(space_info->wait, !space_info->flush);
4913 /* Must have been killed, return */
4914 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004915 return -EINTR;
4916
4917 spin_lock(&space_info->lock);
4918 }
4919
4920 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004921 used = space_info->bytes_used + space_info->bytes_reserved +
4922 space_info->bytes_pinned + space_info->bytes_readonly +
4923 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004924
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004925 /*
4926 * The idea here is that we've not already over-reserved the block group
4927 * then we can go ahead and save our reservation first and then start
4928 * flushing if we need to. Otherwise if we've already overcommitted
4929 * lets start flushing stuff first and then come back and try to make
4930 * our reservation.
4931 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004932 if (used <= space_info->total_bytes) {
4933 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004934 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004935 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004936 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004937 ret = 0;
4938 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004939 /*
4940 * Ok set num_bytes to orig_bytes since we aren't
4941 * overocmmitted, this way we only try and reclaim what
4942 * we need.
4943 */
4944 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004945 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004946 } else {
4947 /*
4948 * Ok we're over committed, set num_bytes to the overcommitted
4949 * amount plus the amount of bytes that we need for this
4950 * reservation.
4951 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004952 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004953 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004954 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004955
Josef Bacik44734ed2012-09-28 16:04:19 -04004956 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4957 space_info->bytes_may_use += orig_bytes;
4958 trace_btrfs_space_reservation(root->fs_info, "space_info",
4959 space_info->flags, orig_bytes,
4960 1);
4961 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004962 }
4963
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004964 /*
4965 * Couldn't make our reservation, save our place so while we're trying
4966 * to reclaim space we can actually use it instead of somebody else
4967 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004968 *
4969 * We make the other tasks wait for the flush only when we can flush
4970 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004971 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004972 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004973 flushing = true;
4974 space_info->flush = 1;
Miao Xie21c7e752014-05-13 17:29:04 -07004975 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
4976 used += orig_bytes;
Josef Bacikf6acfd52014-09-18 11:27:17 -04004977 /*
4978 * We will do the space reservation dance during log replay,
4979 * which means we won't have fs_info->fs_root set, so don't do
4980 * the async reclaim as we will panic.
4981 */
4982 if (!root->fs_info->log_root_recovering &&
4983 need_do_async_reclaim(space_info, root->fs_info, used) &&
Miao Xie21c7e752014-05-13 17:29:04 -07004984 !work_busy(&root->fs_info->async_reclaim_work))
4985 queue_work(system_unbound_wq,
4986 &root->fs_info->async_reclaim_work);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004987 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004988 spin_unlock(&space_info->lock);
4989
Miao Xie08e007d2012-10-16 11:33:38 +00004990 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004991 goto out;
4992
Josef Bacik96c3f432012-06-21 14:05:49 -04004993 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4994 flush_state);
4995 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004996
4997 /*
4998 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4999 * would happen. So skip delalloc flush.
5000 */
5001 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
5002 (flush_state == FLUSH_DELALLOC ||
5003 flush_state == FLUSH_DELALLOC_WAIT))
5004 flush_state = ALLOC_CHUNK;
5005
Josef Bacik96c3f432012-06-21 14:05:49 -04005006 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005007 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00005008 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
5009 flush_state < COMMIT_TRANS)
5010 goto again;
5011 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
5012 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005013 goto again;
5014
5015out:
Josef Bacik5d803662013-02-07 16:06:02 -05005016 if (ret == -ENOSPC &&
5017 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
5018 struct btrfs_block_rsv *global_rsv =
5019 &root->fs_info->global_block_rsv;
5020
5021 if (block_rsv != global_rsv &&
5022 !block_rsv_use_bytes(global_rsv, orig_bytes))
5023 ret = 0;
5024 }
Jeff Mahoneycab45e22013-10-16 16:27:01 -04005025 if (ret == -ENOSPC)
5026 trace_btrfs_space_reservation(root->fs_info,
5027 "space_info:enospc",
5028 space_info->flags, orig_bytes, 1);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04005029 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005030 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04005031 space_info->flush = 0;
5032 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005033 spin_unlock(&space_info->lock);
5034 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005035 return ret;
5036}
5037
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005038static struct btrfs_block_rsv *get_block_rsv(
5039 const struct btrfs_trans_handle *trans,
5040 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005041{
Josef Bacik4c13d752011-08-30 11:31:29 -04005042 struct btrfs_block_rsv *block_rsv = NULL;
5043
Alexandru Moisee9cf4392015-09-09 00:18:50 +00005044 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
5045 (root == root->fs_info->csum_root && trans->adding_csums) ||
5046 (root == root->fs_info->uuid_root))
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02005047 block_rsv = trans->block_rsv;
5048
Josef Bacik4c13d752011-08-30 11:31:29 -04005049 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005050 block_rsv = root->block_rsv;
5051
5052 if (!block_rsv)
5053 block_rsv = &root->fs_info->empty_block_rsv;
5054
5055 return block_rsv;
5056}
5057
5058static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
5059 u64 num_bytes)
5060{
5061 int ret = -ENOSPC;
5062 spin_lock(&block_rsv->lock);
5063 if (block_rsv->reserved >= num_bytes) {
5064 block_rsv->reserved -= num_bytes;
5065 if (block_rsv->reserved < block_rsv->size)
5066 block_rsv->full = 0;
5067 ret = 0;
5068 }
5069 spin_unlock(&block_rsv->lock);
5070 return ret;
5071}
5072
5073static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
5074 u64 num_bytes, int update_size)
5075{
5076 spin_lock(&block_rsv->lock);
5077 block_rsv->reserved += num_bytes;
5078 if (update_size)
5079 block_rsv->size += num_bytes;
5080 else if (block_rsv->reserved >= block_rsv->size)
5081 block_rsv->full = 1;
5082 spin_unlock(&block_rsv->lock);
5083}
5084
Josef Bacikd52be812013-05-29 14:54:47 -04005085int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
5086 struct btrfs_block_rsv *dest, u64 num_bytes,
5087 int min_factor)
5088{
5089 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5090 u64 min_bytes;
5091
5092 if (global_rsv->space_info != dest->space_info)
5093 return -ENOSPC;
5094
5095 spin_lock(&global_rsv->lock);
5096 min_bytes = div_factor(global_rsv->size, min_factor);
5097 if (global_rsv->reserved < min_bytes + num_bytes) {
5098 spin_unlock(&global_rsv->lock);
5099 return -ENOSPC;
5100 }
5101 global_rsv->reserved -= num_bytes;
5102 if (global_rsv->reserved < global_rsv->size)
5103 global_rsv->full = 0;
5104 spin_unlock(&global_rsv->lock);
5105
5106 block_rsv_add_bytes(dest, num_bytes, 1);
5107 return 0;
5108}
5109
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005110static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
5111 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02005112 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005113{
5114 struct btrfs_space_info *space_info = block_rsv->space_info;
5115
5116 spin_lock(&block_rsv->lock);
5117 if (num_bytes == (u64)-1)
5118 num_bytes = block_rsv->size;
5119 block_rsv->size -= num_bytes;
5120 if (block_rsv->reserved >= block_rsv->size) {
5121 num_bytes = block_rsv->reserved - block_rsv->size;
5122 block_rsv->reserved = block_rsv->size;
5123 block_rsv->full = 1;
5124 } else {
5125 num_bytes = 0;
5126 }
5127 spin_unlock(&block_rsv->lock);
5128
5129 if (num_bytes > 0) {
5130 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00005131 spin_lock(&dest->lock);
5132 if (!dest->full) {
5133 u64 bytes_to_add;
5134
5135 bytes_to_add = dest->size - dest->reserved;
5136 bytes_to_add = min(num_bytes, bytes_to_add);
5137 dest->reserved += bytes_to_add;
5138 if (dest->reserved >= dest->size)
5139 dest->full = 1;
5140 num_bytes -= bytes_to_add;
5141 }
5142 spin_unlock(&dest->lock);
5143 }
5144 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005145 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04005146 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005147 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005148 space_info->flags, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005149 spin_unlock(&space_info->lock);
5150 }
5151 }
5152}
5153
5154static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
5155 struct btrfs_block_rsv *dst, u64 num_bytes)
5156{
5157 int ret;
5158
5159 ret = block_rsv_use_bytes(src, num_bytes);
5160 if (ret)
5161 return ret;
5162
5163 block_rsv_add_bytes(dst, num_bytes, 1);
5164 return 0;
5165}
5166
Miao Xie66d8f3d2012-09-06 04:02:28 -06005167void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005168{
5169 memset(rsv, 0, sizeof(*rsv));
5170 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06005171 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005172}
5173
Miao Xie66d8f3d2012-09-06 04:02:28 -06005174struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
5175 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005176{
5177 struct btrfs_block_rsv *block_rsv;
5178 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005179
5180 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
5181 if (!block_rsv)
5182 return NULL;
5183
Miao Xie66d8f3d2012-09-06 04:02:28 -06005184 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005185 block_rsv->space_info = __find_space_info(fs_info,
5186 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005187 return block_rsv;
5188}
5189
5190void btrfs_free_block_rsv(struct btrfs_root *root,
5191 struct btrfs_block_rsv *rsv)
5192{
Josef Bacik2aaa6652012-08-29 14:27:18 -04005193 if (!rsv)
5194 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04005195 btrfs_block_rsv_release(root, rsv, (u64)-1);
5196 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005197}
5198
Chris Masoncdfb0802015-04-06 18:17:00 -07005199void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
5200{
5201 kfree(rsv);
5202}
5203
Miao Xie08e007d2012-10-16 11:33:38 +00005204int btrfs_block_rsv_add(struct btrfs_root *root,
5205 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
5206 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005207{
5208 int ret;
5209
5210 if (num_bytes == 0)
5211 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005212
Miao Xie61b520a2011-11-10 20:45:05 -05005213 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005214 if (!ret) {
5215 block_rsv_add_bytes(block_rsv, num_bytes, 1);
5216 return 0;
5217 }
5218
Yan, Zhengf0486c62010-05-16 10:46:25 -04005219 return ret;
5220}
5221
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005222int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04005223 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005224{
5225 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005226 int ret = -ENOSPC;
5227
5228 if (!block_rsv)
5229 return 0;
5230
5231 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04005232 num_bytes = div_factor(block_rsv->size, min_factor);
5233 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005234 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005235 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005236
Josef Bacik36ba0222011-10-18 12:15:48 -04005237 return ret;
5238}
5239
Miao Xie08e007d2012-10-16 11:33:38 +00005240int btrfs_block_rsv_refill(struct btrfs_root *root,
5241 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
5242 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04005243{
5244 u64 num_bytes = 0;
5245 int ret = -ENOSPC;
5246
5247 if (!block_rsv)
5248 return 0;
5249
5250 spin_lock(&block_rsv->lock);
5251 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04005252 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005253 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04005254 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04005255 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005256 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04005257
Yan, Zhengf0486c62010-05-16 10:46:25 -04005258 if (!ret)
5259 return 0;
5260
Miao Xieaa38a712011-11-18 17:43:00 +08005261 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04005262 if (!ret) {
5263 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005264 return 0;
5265 }
5266
Josef Bacik13553e52011-08-08 13:33:21 -04005267 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005268}
5269
5270int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
5271 struct btrfs_block_rsv *dst_rsv,
5272 u64 num_bytes)
5273{
5274 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
5275}
5276
5277void btrfs_block_rsv_release(struct btrfs_root *root,
5278 struct btrfs_block_rsv *block_rsv,
5279 u64 num_bytes)
5280{
5281 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Liu Bo17504582013-12-29 21:44:50 +08005282 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04005283 block_rsv->space_info != global_rsv->space_info)
5284 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005285 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
5286 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005287}
5288
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005289/*
5290 * helper to calculate size of global block reservation.
5291 * the desired value is sum of space used by extent tree,
5292 * checksum tree and root tree
5293 */
5294static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
5295{
5296 struct btrfs_space_info *sinfo;
5297 u64 num_bytes;
5298 u64 meta_used;
5299 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02005300 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005301
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005302 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
5303 spin_lock(&sinfo->lock);
5304 data_used = sinfo->bytes_used;
5305 spin_unlock(&sinfo->lock);
5306
5307 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
5308 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04005309 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
5310 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005311 meta_used = sinfo->bytes_used;
5312 spin_unlock(&sinfo->lock);
5313
5314 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
5315 csum_size * 2;
David Sterbaf8c269d2015-01-16 17:21:12 +01005316 num_bytes += div_u64(data_used + meta_used, 50);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005317
5318 if (num_bytes * 3 > meta_used)
David Sterbaf8c269d2015-01-16 17:21:12 +01005319 num_bytes = div_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005320
David Sterba707e8a02014-06-04 19:22:26 +02005321 return ALIGN(num_bytes, fs_info->extent_root->nodesize << 10);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005322}
5323
5324static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
5325{
5326 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
5327 struct btrfs_space_info *sinfo = block_rsv->space_info;
5328 u64 num_bytes;
5329
5330 num_bytes = calc_global_metadata_size(fs_info);
5331
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005332 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005333 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005334
Josef Bacikfdf30d12013-03-26 15:31:45 -04005335 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005336
5337 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04005338 sinfo->bytes_reserved + sinfo->bytes_readonly +
5339 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005340
5341 if (sinfo->total_bytes > num_bytes) {
5342 num_bytes = sinfo->total_bytes - num_bytes;
5343 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04005344 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005345 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005346 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005347 }
5348
5349 if (block_rsv->reserved >= block_rsv->size) {
5350 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04005351 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005352 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005353 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005354 block_rsv->reserved = block_rsv->size;
5355 block_rsv->full = 1;
5356 }
David Sterba182608c2011-05-05 13:13:16 +02005357
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005358 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005359 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005360}
5361
Yan, Zhengf0486c62010-05-16 10:46:25 -04005362static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
5363{
5364 struct btrfs_space_info *space_info;
5365
5366 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
5367 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005368
5369 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005370 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005371 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005372 fs_info->trans_block_rsv.space_info = space_info;
5373 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04005374 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005375
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005376 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
5377 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
5378 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
5379 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00005380 if (fs_info->quota_root)
5381 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005382 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005383
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005384 update_global_block_rsv(fs_info);
5385}
5386
5387static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
5388{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005389 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
5390 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005391 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
5392 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
5393 WARN_ON(fs_info->trans_block_rsv.size > 0);
5394 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
5395 WARN_ON(fs_info->chunk_block_rsv.size > 0);
5396 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04005397 WARN_ON(fs_info->delayed_block_rsv.size > 0);
5398 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04005399}
5400
Yan, Zhenga22285a2010-05-16 10:48:46 -04005401void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
5402 struct btrfs_root *root)
5403{
Josef Bacik0e721102012-06-26 16:13:18 -04005404 if (!trans->block_rsv)
5405 return;
5406
Yan, Zhenga22285a2010-05-16 10:48:46 -04005407 if (!trans->bytes_reserved)
5408 return;
5409
Chris Masone77266e2012-02-24 10:39:05 -05005410 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04005411 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04005412 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005413 trans->bytes_reserved = 0;
5414}
5415
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005416/*
5417 * To be called after all the new block groups attached to the transaction
5418 * handle have been created (btrfs_create_pending_block_groups()).
5419 */
5420void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
5421{
5422 struct btrfs_fs_info *fs_info = trans->root->fs_info;
5423
5424 if (!trans->chunk_bytes_reserved)
5425 return;
5426
5427 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
5428
5429 block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL,
5430 trans->chunk_bytes_reserved);
5431 trans->chunk_bytes_reserved = 0;
5432}
5433
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005434/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04005435int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
5436 struct inode *inode)
5437{
5438 struct btrfs_root *root = BTRFS_I(inode)->root;
5439 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
5440 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
5441
5442 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04005443 * We need to hold space in order to delete our orphan item once we've
5444 * added it, so this takes the reservation so we can release it later
5445 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04005446 */
Chris Masonff5714c2011-05-28 07:00:39 -04005447 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005448 trace_btrfs_space_reservation(root->fs_info, "orphan",
5449 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005450 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
5451}
5452
5453void btrfs_orphan_release_metadata(struct inode *inode)
5454{
5455 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04005456 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005457 trace_btrfs_space_reservation(root->fs_info, "orphan",
5458 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005459 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
5460}
5461
Miao Xied5c12072013-02-28 10:04:33 +00005462/*
5463 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
5464 * root: the root of the parent directory
5465 * rsv: block reservation
5466 * items: the number of items that we need do reservation
5467 * qgroup_reserved: used to return the reserved size in qgroup
5468 *
5469 * This function is used to reserve the space for snapshot/subvolume
5470 * creation and deletion. Those operations are different with the
5471 * common file/directory operations, they change two fs/file trees
5472 * and root tree, the number of items that the qgroup reserves is
5473 * different with the free space reservation. So we can not use
5474 * the space reseravtion mechanism in start_transaction().
5475 */
5476int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
5477 struct btrfs_block_rsv *rsv,
5478 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005479 u64 *qgroup_reserved,
5480 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005481{
Miao Xied5c12072013-02-28 10:04:33 +00005482 u64 num_bytes;
5483 int ret;
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005484 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00005485
5486 if (root->fs_info->quota_enabled) {
5487 /* One for parent inode, two for dir entries */
David Sterba707e8a02014-06-04 19:22:26 +02005488 num_bytes = 3 * root->nodesize;
Qu Wenruo71741092015-09-08 17:22:41 +08005489 ret = btrfs_qgroup_reserve_meta(root, num_bytes);
Miao Xied5c12072013-02-28 10:04:33 +00005490 if (ret)
5491 return ret;
5492 } else {
5493 num_bytes = 0;
5494 }
5495
5496 *qgroup_reserved = num_bytes;
5497
5498 num_bytes = btrfs_calc_trans_metadata_size(root, items);
5499 rsv->space_info = __find_space_info(root->fs_info,
5500 BTRFS_BLOCK_GROUP_METADATA);
5501 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
5502 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005503
5504 if (ret == -ENOSPC && use_global_rsv)
5505 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
5506
Qu Wenruo71741092015-09-08 17:22:41 +08005507 if (ret && *qgroup_reserved)
5508 btrfs_qgroup_free_meta(root, *qgroup_reserved);
Miao Xied5c12072013-02-28 10:04:33 +00005509
5510 return ret;
5511}
5512
5513void btrfs_subvolume_release_metadata(struct btrfs_root *root,
5514 struct btrfs_block_rsv *rsv,
5515 u64 qgroup_reserved)
5516{
5517 btrfs_block_rsv_release(root, rsv, (u64)-1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005518}
5519
Josef Bacik7709cde2011-08-04 10:25:02 -04005520/**
5521 * drop_outstanding_extent - drop an outstanding extent
5522 * @inode: the inode we're dropping the extent for
Josef Bacikdcab6a32015-02-11 15:08:59 -05005523 * @num_bytes: the number of bytes we're relaseing.
Josef Bacik7709cde2011-08-04 10:25:02 -04005524 *
5525 * This is called when we are freeing up an outstanding extent, either called
5526 * after an error or after an extent is written. This will return the number of
5527 * reserved extents that need to be freed. This must be called with
5528 * BTRFS_I(inode)->lock held.
5529 */
Josef Bacikdcab6a32015-02-11 15:08:59 -05005530static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes)
Josef Bacik9e0baf62011-07-15 15:16:44 +00005531{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005532 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005533 unsigned dropped_extents = 0;
Josef Bacikdcab6a32015-02-11 15:08:59 -05005534 unsigned num_extents = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005535
Josef Bacikdcab6a32015-02-11 15:08:59 -05005536 num_extents = (unsigned)div64_u64(num_bytes +
5537 BTRFS_MAX_EXTENT_SIZE - 1,
5538 BTRFS_MAX_EXTENT_SIZE);
5539 ASSERT(num_extents);
5540 ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents);
5541 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005542
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005543 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04005544 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5545 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005546 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005547
Josef Bacik9e0baf62011-07-15 15:16:44 +00005548 /*
5549 * If we have more or the same amount of outsanding extents than we have
5550 * reserved then we need to leave the reserved extents count alone.
5551 */
5552 if (BTRFS_I(inode)->outstanding_extents >=
5553 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005554 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005555
5556 dropped_extents = BTRFS_I(inode)->reserved_extents -
5557 BTRFS_I(inode)->outstanding_extents;
5558 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005559 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005560}
5561
Josef Bacik7709cde2011-08-04 10:25:02 -04005562/**
5563 * calc_csum_metadata_size - return the amount of metada space that must be
5564 * reserved/free'd for the given bytes.
5565 * @inode: the inode we're manipulating
5566 * @num_bytes: the number of bytes in question
5567 * @reserve: 1 if we are reserving space, 0 if we are freeing space
5568 *
5569 * This adjusts the number of csum_bytes in the inode and then returns the
5570 * correct amount of metadata that must either be reserved or freed. We
5571 * calculate how many checksums we can fit into one leaf and then divide the
5572 * number of bytes that will need to be checksumed by this value to figure out
5573 * how many checksums will be required. If we are adding bytes then the number
5574 * may go up and we will return the number of additional bytes that must be
5575 * reserved. If it is going down we will return the number of bytes that must
5576 * be freed.
5577 *
5578 * This must be called with BTRFS_I(inode)->lock held.
5579 */
5580static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
5581 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005582{
Josef Bacik7709cde2011-08-04 10:25:02 -04005583 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik12621332015-02-03 07:50:16 -08005584 u64 old_csums, num_csums;
Josef Bacik7709cde2011-08-04 10:25:02 -04005585
5586 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
5587 BTRFS_I(inode)->csum_bytes == 0)
5588 return 0;
5589
Chris Mason28f75a02015-02-04 06:59:29 -08005590 old_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04005591 if (reserve)
5592 BTRFS_I(inode)->csum_bytes += num_bytes;
5593 else
5594 BTRFS_I(inode)->csum_bytes -= num_bytes;
Chris Mason28f75a02015-02-04 06:59:29 -08005595 num_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04005596
5597 /* No change, no need to reserve more */
5598 if (old_csums == num_csums)
5599 return 0;
5600
5601 if (reserve)
5602 return btrfs_calc_trans_metadata_size(root,
5603 num_csums - old_csums);
5604
5605 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005606}
5607
5608int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
5609{
5610 struct btrfs_root *root = BTRFS_I(inode)->root;
5611 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005612 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05005613 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005614 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05005615 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00005616 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07005617 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005618 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00005619 u64 to_free = 0;
5620 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005621
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005622 /* If we are a free space inode we need to not flush since we will be in
5623 * the middle of a transaction commit. We also don't need the delalloc
5624 * mutex since we won't race with anybody. We need this mostly to make
5625 * lockdep shut its filthy mouth.
5626 */
5627 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00005628 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005629 delalloc_lock = false;
5630 }
Josef Bacikc09544e2011-08-30 10:19:10 -04005631
Miao Xie08e007d2012-10-16 11:33:38 +00005632 if (flush != BTRFS_RESERVE_NO_FLUSH &&
5633 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005634 schedule_timeout(1);
5635
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005636 if (delalloc_lock)
5637 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
5638
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005639 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005640
Josef Bacik9e0baf62011-07-15 15:16:44 +00005641 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik6a41dd02015-03-13 15:12:23 -04005642 nr_extents = (unsigned)div64_u64(num_bytes +
5643 BTRFS_MAX_EXTENT_SIZE - 1,
5644 BTRFS_MAX_EXTENT_SIZE);
5645 BTRFS_I(inode)->outstanding_extents += nr_extents;
5646 nr_extents = 0;
Josef Bacik57a45ced2011-01-25 16:30:38 -05005647
Josef Bacik9e0baf62011-07-15 15:16:44 +00005648 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05005649 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00005650 nr_extents = BTRFS_I(inode)->outstanding_extents -
5651 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005652
5653 /*
5654 * Add an item to reserve for updating the inode when we complete the
5655 * delalloc io.
5656 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04005657 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5658 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005659 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05005660 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005661 }
5662
5663 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04005664 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05005665 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005666 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05005667
Wang Shilong88e081bf2013-03-01 11:36:01 +00005668 if (root->fs_info->quota_enabled) {
Qu Wenruo71741092015-09-08 17:22:41 +08005669 ret = btrfs_qgroup_reserve_meta(root,
5670 nr_extents * root->nodesize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005671 if (ret)
5672 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00005673 }
Arne Jansenc5567232011-09-14 15:44:05 +02005674
Wang Shilong88e081bf2013-03-01 11:36:01 +00005675 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
5676 if (unlikely(ret)) {
Qu Wenruo71741092015-09-08 17:22:41 +08005677 btrfs_qgroup_free_meta(root, nr_extents * root->nodesize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005678 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005679 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005680
Josef Bacik660d3f62011-12-09 11:18:51 -05005681 spin_lock(&BTRFS_I(inode)->lock);
5682 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04005683 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5684 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05005685 nr_extents--;
5686 }
5687 BTRFS_I(inode)->reserved_extents += nr_extents;
5688 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005689
5690 if (delalloc_lock)
5691 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05005692
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005693 if (to_reserve)
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05305694 trace_btrfs_space_reservation(root->fs_info, "delalloc",
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005695 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005696 block_rsv_add_bytes(block_rsv, to_reserve, 1);
5697
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005698 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00005699
5700out_fail:
5701 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05005702 dropped = drop_outstanding_extent(inode, num_bytes);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005703 /*
5704 * If the inodes csum_bytes is the same as the original
5705 * csum_bytes then we know we haven't raced with any free()ers
5706 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00005707 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04005708 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00005709 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04005710 } else {
5711 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
5712 u64 bytes;
5713
5714 /*
5715 * This is tricky, but first we need to figure out how much we
5716 * free'd from any free-ers that occured during this
5717 * reservation, so we reset ->csum_bytes to the csum_bytes
5718 * before we dropped our lock, and then call the free for the
5719 * number of bytes that were freed while we were trying our
5720 * reservation.
5721 */
5722 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
5723 BTRFS_I(inode)->csum_bytes = csum_bytes;
5724 to_free = calc_csum_metadata_size(inode, bytes, 0);
5725
5726
5727 /*
5728 * Now we need to see how much we would have freed had we not
5729 * been making this reservation and our ->csum_bytes were not
5730 * artificially inflated.
5731 */
5732 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
5733 bytes = csum_bytes - orig_csum_bytes;
5734 bytes = calc_csum_metadata_size(inode, bytes, 0);
5735
5736 /*
5737 * Now reset ->csum_bytes to what it should be. If bytes is
5738 * more than to_free then we would have free'd more space had we
5739 * not had an artificially high ->csum_bytes, so we need to free
5740 * the remainder. If bytes is the same or less then we don't
5741 * need to do anything, the other free-ers did the correct
5742 * thing.
5743 */
5744 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
5745 if (bytes > to_free)
5746 to_free = bytes - to_free;
5747 else
5748 to_free = 0;
5749 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00005750 spin_unlock(&BTRFS_I(inode)->lock);
Dongsheng Yange2d1f922015-02-06 10:26:52 -05005751 if (dropped)
Wang Shilong88e081bf2013-03-01 11:36:01 +00005752 to_free += btrfs_calc_trans_metadata_size(root, dropped);
5753
5754 if (to_free) {
5755 btrfs_block_rsv_release(root, block_rsv, to_free);
5756 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5757 btrfs_ino(inode), to_free, 0);
5758 }
5759 if (delalloc_lock)
5760 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
5761 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005762}
5763
Josef Bacik7709cde2011-08-04 10:25:02 -04005764/**
5765 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5766 * @inode: the inode to release the reservation for
5767 * @num_bytes: the number of bytes we're releasing
5768 *
5769 * This will release the metadata reservation for an inode. This can be called
5770 * once we complete IO for a given set of bytes to release their metadata
5771 * reservations.
5772 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005773void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
5774{
5775 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005776 u64 to_free = 0;
5777 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005778
5779 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04005780 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05005781 dropped = drop_outstanding_extent(inode, num_bytes);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005782
Miao Xie09348562013-02-07 10:12:07 +00005783 if (num_bytes)
5784 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04005785 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005786 if (dropped > 0)
5787 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005788
Josef Bacik6a3891c2015-03-16 17:38:52 -04005789 if (btrfs_test_is_dummy_root(root))
5790 return;
5791
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005792 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5793 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02005794
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005795 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
5796 to_free);
5797}
5798
Josef Bacik7709cde2011-08-04 10:25:02 -04005799/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08005800 * btrfs_delalloc_reserve_space - reserve data and metadata space for
Qu Wenruo1ada3a62015-09-08 17:25:53 +08005801 * delalloc
5802 * @inode: inode we're writing to
5803 * @start: start range we are writing to
5804 * @len: how long the range we are writing to
5805 *
5806 * TODO: This function will finally replace old btrfs_delalloc_reserve_space()
5807 *
5808 * This will do the following things
5809 *
5810 * o reserve space in data space info for num bytes
5811 * and reserve precious corresponding qgroup space
5812 * (Done in check_data_free_space)
5813 *
5814 * o reserve space for metadata space, based on the number of outstanding
5815 * extents and how much csums will be needed
5816 * also reserve metadata space in a per root over-reserve method.
5817 * o add to the inodes->delalloc_bytes
5818 * o add it to the fs_info's delalloc inodes list.
5819 * (Above 3 all done in delalloc_reserve_metadata)
5820 *
5821 * Return 0 for success
5822 * Return <0 for error(-ENOSPC or -EQUOT)
5823 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08005824int btrfs_delalloc_reserve_space(struct inode *inode, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08005825{
5826 int ret;
5827
Qu Wenruo7cf5b972015-09-08 17:25:55 +08005828 ret = btrfs_check_data_free_space(inode, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08005829 if (ret < 0)
5830 return ret;
5831 ret = btrfs_delalloc_reserve_metadata(inode, len);
5832 if (ret < 0)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08005833 btrfs_free_reserved_data_space(inode, start, len);
Qu Wenruo1ada3a62015-09-08 17:25:53 +08005834 return ret;
5835}
5836
5837/**
Qu Wenruo7cf5b972015-09-08 17:25:55 +08005838 * btrfs_delalloc_release_space - release data and metadata space for delalloc
Qu Wenruo1ada3a62015-09-08 17:25:53 +08005839 * @inode: inode we're releasing space for
5840 * @start: start position of the space already reserved
5841 * @len: the len of the space already reserved
5842 *
5843 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5844 * called in the case that we don't need the metadata AND data reservations
5845 * anymore. So if there is an error or we insert an inline extent.
5846 *
5847 * This function will release the metadata space that was not used and will
5848 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5849 * list if there are no delalloc bytes left.
5850 * Also it will handle the qgroup reserved space.
5851 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08005852void btrfs_delalloc_release_space(struct inode *inode, u64 start, u64 len)
Qu Wenruo1ada3a62015-09-08 17:25:53 +08005853{
5854 btrfs_delalloc_release_metadata(inode, len);
Qu Wenruo7cf5b972015-09-08 17:25:55 +08005855 btrfs_free_reserved_data_space(inode, start, len);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005856}
5857
Josef Bacikce93ec52014-11-17 15:45:48 -05005858static int update_block_group(struct btrfs_trans_handle *trans,
5859 struct btrfs_root *root, u64 bytenr,
5860 u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04005861{
Josef Bacik0af3d002010-06-21 14:48:16 -04005862 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04005863 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04005864 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005865 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04005866 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04005867 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04005868
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005869 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00005870 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02005871 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005872 if (alloc)
5873 old_val += num_bytes;
5874 else
5875 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02005876 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00005877 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005878
Chris Masond3977122009-01-05 21:25:51 -05005879 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04005880 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005881 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005882 return -ENOENT;
Yan, Zhengb742bb822010-05-16 10:46:24 -04005883 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5884 BTRFS_BLOCK_GROUP_RAID1 |
5885 BTRFS_BLOCK_GROUP_RAID10))
5886 factor = 2;
5887 else
5888 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04005889 /*
5890 * If this block group has free space cache written out, we
5891 * need to make sure to load it if we are removing space. This
5892 * is because we need the unpinning stage to actually add the
5893 * space back to the block group, otherwise we will leak space.
5894 */
5895 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00005896 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005897
Chris Masondb945352007-10-15 16:15:53 -04005898 byte_in_group = bytenr - cache->key.objectid;
5899 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005900
Josef Bacik25179202008-10-29 14:49:05 -04005901 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005902 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005903
Josef Bacik73bc1872011-10-03 14:07:49 -04005904 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005905 cache->disk_cache_state < BTRFS_DC_CLEAR)
5906 cache->disk_cache_state = BTRFS_DC_CLEAR;
5907
Chris Mason9078a3e2007-04-26 16:46:15 -04005908 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005909 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005910 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005911 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005912 btrfs_set_block_group_used(&cache->item, old_val);
5913 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005914 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb822010-05-16 10:46:24 -04005915 cache->space_info->bytes_used += num_bytes;
5916 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005917 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005918 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005919 } else {
Chris Masondb945352007-10-15 16:15:53 -04005920 old_val -= num_bytes;
Filipe Mananaae0ab002014-11-26 15:28:52 +00005921 btrfs_set_block_group_used(&cache->item, old_val);
5922 cache->pinned += num_bytes;
5923 cache->space_info->bytes_pinned += num_bytes;
5924 cache->space_info->bytes_used -= num_bytes;
5925 cache->space_info->disk_used -= num_bytes * factor;
5926 spin_unlock(&cache->lock);
5927 spin_unlock(&cache->space_info->lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04005928
Filipe Mananaae0ab002014-11-26 15:28:52 +00005929 set_extent_dirty(info->pinned_extents,
5930 bytenr, bytenr + num_bytes - 1,
5931 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04005932 }
Chris Mason1bbc6212015-04-06 12:46:08 -07005933
5934 spin_lock(&trans->transaction->dirty_bgs_lock);
5935 if (list_empty(&cache->dirty_list)) {
5936 list_add_tail(&cache->dirty_list,
5937 &trans->transaction->dirty_bgs);
5938 trans->transaction->num_dirty_bgs++;
5939 btrfs_get_block_group(cache);
5940 }
5941 spin_unlock(&trans->transaction->dirty_bgs_lock);
5942
Filipe Manana036a9342015-11-23 15:25:16 +00005943 /*
5944 * No longer have used bytes in this block group, queue it for
5945 * deletion. We do this after adding the block group to the
5946 * dirty list to avoid races between cleaner kthread and space
5947 * cache writeout.
5948 */
5949 if (!alloc && old_val == 0) {
5950 spin_lock(&info->unused_bgs_lock);
5951 if (list_empty(&cache->bg_list)) {
5952 btrfs_get_block_group(cache);
5953 list_add_tail(&cache->bg_list,
5954 &info->unused_bgs);
5955 }
5956 spin_unlock(&info->unused_bgs_lock);
5957 }
5958
Chris Masonfa9c0d792009-04-03 09:47:43 -04005959 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005960 total -= num_bytes;
5961 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005962 }
5963 return 0;
5964}
Chris Mason6324fbf2008-03-24 15:01:59 -04005965
Chris Masona061fc82008-05-07 11:43:44 -04005966static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5967{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005968 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005969 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005970
Liu Boa1897fd2012-12-27 09:01:23 +00005971 spin_lock(&root->fs_info->block_group_cache_lock);
5972 bytenr = root->fs_info->first_logical_byte;
5973 spin_unlock(&root->fs_info->block_group_cache_lock);
5974
5975 if (bytenr < (u64)-1)
5976 return bytenr;
5977
Josef Bacik0f9dd462008-09-23 13:14:11 -04005978 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5979 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005980 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005981
Yan Zhengd2fb3432008-12-11 16:30:39 -05005982 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005983 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005984
5985 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005986}
5987
Yan, Zhengf0486c62010-05-16 10:46:25 -04005988static int pin_down_extent(struct btrfs_root *root,
5989 struct btrfs_block_group_cache *cache,
5990 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005991{
Yan Zheng11833d62009-09-11 16:11:19 -04005992 spin_lock(&cache->space_info->lock);
5993 spin_lock(&cache->lock);
5994 cache->pinned += num_bytes;
5995 cache->space_info->bytes_pinned += num_bytes;
5996 if (reserved) {
5997 cache->reserved -= num_bytes;
5998 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005999 }
Yan Zheng11833d62009-09-11 16:11:19 -04006000 spin_unlock(&cache->lock);
6001 spin_unlock(&cache->space_info->lock);
6002
Yan, Zhengf0486c62010-05-16 10:46:25 -04006003 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
6004 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Dongsheng Yange2d1f922015-02-06 10:26:52 -05006005 if (reserved)
Josef Bacik0be5dc62013-10-07 15:18:52 -04006006 trace_btrfs_reserved_extent_free(root, bytenr, num_bytes);
Yan324ae4d2007-11-16 14:57:08 -05006007 return 0;
6008}
Chris Mason9078a3e2007-04-26 16:46:15 -04006009
Yan, Zhengf0486c62010-05-16 10:46:25 -04006010/*
6011 * this function must be called within transaction
6012 */
6013int btrfs_pin_extent(struct btrfs_root *root,
6014 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04006015{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006016 struct btrfs_block_group_cache *cache;
6017
6018 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006019 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006020
6021 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
6022
6023 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04006024 return 0;
6025}
Zheng Yane8569812008-09-26 10:05:48 -04006026
Yan, Zhengf0486c62010-05-16 10:46:25 -04006027/*
Chris Masone688b7252011-10-31 20:52:39 -04006028 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04006029 */
Liu Bodcfac412012-12-27 09:01:20 +00006030int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b7252011-10-31 20:52:39 -04006031 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006032{
Chris Masone688b7252011-10-31 20:52:39 -04006033 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04006034 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04006035
6036 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006037 if (!cache)
6038 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04006039
6040 /*
6041 * pull in the free space cache (if any) so that our pin
6042 * removes the free space from the cache. We have load_only set
6043 * to one because the slow code to read in the free extents does check
6044 * the pinned extents.
6045 */
Liu Bof6373bf2012-12-27 09:01:18 +00006046 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04006047
6048 pin_down_extent(root, cache, bytenr, num_bytes, 0);
6049
6050 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04006051 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04006052 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006053 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04006054}
6055
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006056static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
6057{
6058 int ret;
6059 struct btrfs_block_group_cache *block_group;
6060 struct btrfs_caching_control *caching_ctl;
6061
6062 block_group = btrfs_lookup_block_group(root->fs_info, start);
6063 if (!block_group)
6064 return -EINVAL;
6065
6066 cache_block_group(block_group, 0);
6067 caching_ctl = get_caching_control(block_group);
6068
6069 if (!caching_ctl) {
6070 /* Logic error */
6071 BUG_ON(!block_group_cache_done(block_group));
6072 ret = btrfs_remove_free_space(block_group, start, num_bytes);
6073 } else {
6074 mutex_lock(&caching_ctl->mutex);
6075
6076 if (start >= caching_ctl->progress) {
6077 ret = add_excluded_extent(root, start, num_bytes);
6078 } else if (start + num_bytes <= caching_ctl->progress) {
6079 ret = btrfs_remove_free_space(block_group,
6080 start, num_bytes);
6081 } else {
6082 num_bytes = caching_ctl->progress - start;
6083 ret = btrfs_remove_free_space(block_group,
6084 start, num_bytes);
6085 if (ret)
6086 goto out_lock;
6087
6088 num_bytes = (start + num_bytes) -
6089 caching_ctl->progress;
6090 start = caching_ctl->progress;
6091 ret = add_excluded_extent(root, start, num_bytes);
6092 }
6093out_lock:
6094 mutex_unlock(&caching_ctl->mutex);
6095 put_caching_control(caching_ctl);
6096 }
6097 btrfs_put_block_group(block_group);
6098 return ret;
6099}
6100
6101int btrfs_exclude_logged_extents(struct btrfs_root *log,
6102 struct extent_buffer *eb)
6103{
6104 struct btrfs_file_extent_item *item;
6105 struct btrfs_key key;
6106 int found_type;
6107 int i;
6108
6109 if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
6110 return 0;
6111
6112 for (i = 0; i < btrfs_header_nritems(eb); i++) {
6113 btrfs_item_key_to_cpu(eb, &key, i);
6114 if (key.type != BTRFS_EXTENT_DATA_KEY)
6115 continue;
6116 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
6117 found_type = btrfs_file_extent_type(eb, item);
6118 if (found_type == BTRFS_FILE_EXTENT_INLINE)
6119 continue;
6120 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
6121 continue;
6122 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
6123 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
6124 __exclude_logged_extent(log, key.objectid, key.offset);
6125 }
6126
6127 return 0;
6128}
6129
Josef Bacikfb25e912011-07-26 17:00:46 -04006130/**
6131 * btrfs_update_reserved_bytes - update the block_group and space info counters
6132 * @cache: The cache we are manipulating
6133 * @num_bytes: The number of bytes in question
6134 * @reserve: One of the reservation enums
Miao Xiee570fd22014-06-19 10:42:50 +08006135 * @delalloc: The blocks are allocated for the delalloc write
Josef Bacikfb25e912011-07-26 17:00:46 -04006136 *
6137 * This is called by the allocator when it reserves space, or by somebody who is
6138 * freeing space that was never actually used on disk. For example if you
6139 * reserve some space for a new leaf in transaction A and before transaction A
6140 * commits you free that leaf, you call this with reserve set to 0 in order to
6141 * clear the reservation.
6142 *
6143 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
6144 * ENOSPC accounting. For data we handle the reservation through clearing the
6145 * delalloc bits in the io_tree. We have to do this since we could end up
6146 * allocating less disk space for the amount of data we have reserved in the
6147 * case of compression.
6148 *
6149 * If this is a reservation and the block group has become read only we cannot
6150 * make the reservation and return -EAGAIN, otherwise this function always
6151 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04006152 */
Josef Bacikfb25e912011-07-26 17:00:46 -04006153static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08006154 u64 num_bytes, int reserve, int delalloc)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006155{
Josef Bacikfb25e912011-07-26 17:00:46 -04006156 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006157 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006158
Josef Bacikfb25e912011-07-26 17:00:46 -04006159 spin_lock(&space_info->lock);
6160 spin_lock(&cache->lock);
6161 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04006162 if (cache->ro) {
6163 ret = -EAGAIN;
6164 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04006165 cache->reserved += num_bytes;
6166 space_info->bytes_reserved += num_bytes;
6167 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006168 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04006169 "space_info", space_info->flags,
6170 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04006171 space_info->bytes_may_use -= num_bytes;
6172 }
Miao Xiee570fd22014-06-19 10:42:50 +08006173
6174 if (delalloc)
6175 cache->delalloc_bytes += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006176 }
Josef Bacikfb25e912011-07-26 17:00:46 -04006177 } else {
6178 if (cache->ro)
6179 space_info->bytes_readonly += num_bytes;
6180 cache->reserved -= num_bytes;
6181 space_info->bytes_reserved -= num_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006182
6183 if (delalloc)
6184 cache->delalloc_bytes -= num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006185 }
Josef Bacikfb25e912011-07-26 17:00:46 -04006186 spin_unlock(&cache->lock);
6187 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006188 return ret;
6189}
6190
Jeff Mahoney143bede2012-03-01 14:56:26 +01006191void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04006192 struct btrfs_root *root)
6193{
6194 struct btrfs_fs_info *fs_info = root->fs_info;
6195 struct btrfs_caching_control *next;
6196 struct btrfs_caching_control *caching_ctl;
6197 struct btrfs_block_group_cache *cache;
6198
Josef Bacik9e351cc2014-03-13 15:42:13 -04006199 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04006200
6201 list_for_each_entry_safe(caching_ctl, next,
6202 &fs_info->caching_block_groups, list) {
6203 cache = caching_ctl->block_group;
6204 if (block_group_cache_done(cache)) {
6205 cache->last_byte_to_unpin = (u64)-1;
6206 list_del_init(&caching_ctl->list);
6207 put_caching_control(caching_ctl);
6208 } else {
6209 cache->last_byte_to_unpin = caching_ctl->progress;
6210 }
6211 }
6212
6213 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6214 fs_info->pinned_extents = &fs_info->freed_extents[1];
6215 else
6216 fs_info->pinned_extents = &fs_info->freed_extents[0];
6217
Josef Bacik9e351cc2014-03-13 15:42:13 -04006218 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006219
6220 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04006221}
6222
Josef Bacikc759c4e2015-10-02 15:25:10 -04006223/*
6224 * Returns the free cluster for the given space info and sets empty_cluster to
6225 * what it should be based on the mount options.
6226 */
6227static struct btrfs_free_cluster *
6228fetch_cluster_info(struct btrfs_root *root, struct btrfs_space_info *space_info,
6229 u64 *empty_cluster)
6230{
6231 struct btrfs_free_cluster *ret = NULL;
6232 bool ssd = btrfs_test_opt(root, SSD);
6233
6234 *empty_cluster = 0;
6235 if (btrfs_mixed_space_info(space_info))
6236 return ret;
6237
6238 if (ssd)
6239 *empty_cluster = 2 * 1024 * 1024;
6240 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
6241 ret = &root->fs_info->meta_alloc_cluster;
6242 if (!ssd)
6243 *empty_cluster = 64 * 1024;
6244 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && ssd) {
6245 ret = &root->fs_info->data_alloc_cluster;
6246 }
6247
6248 return ret;
6249}
6250
Filipe Manana678886b2014-12-07 21:31:47 +00006251static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end,
6252 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04006253{
6254 struct btrfs_fs_info *fs_info = root->fs_info;
6255 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04006256 struct btrfs_space_info *space_info;
6257 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006258 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04006259 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006260 u64 total_unpinned = 0;
6261 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04006262 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04006263
6264 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04006265 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04006266 if (!cache ||
6267 start >= cache->key.objectid + cache->key.offset) {
6268 if (cache)
6269 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04006270 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006271 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006272 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04006273
6274 cluster = fetch_cluster_info(root,
6275 cache->space_info,
6276 &empty_cluster);
6277 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04006278 }
6279
6280 len = cache->key.objectid + cache->key.offset - start;
6281 len = min(len, end + 1 - start);
6282
6283 if (start < cache->last_byte_to_unpin) {
6284 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00006285 if (return_free_space)
6286 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04006287 }
Josef Bacik25179202008-10-29 14:49:05 -04006288
Yan, Zhengf0486c62010-05-16 10:46:25 -04006289 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006290 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006291 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006292
Josef Bacikc759c4e2015-10-02 15:25:10 -04006293 /*
6294 * If this space cluster has been marked as fragmented and we've
6295 * unpinned enough in this block group to potentially allow a
6296 * cluster to be created inside of it go ahead and clear the
6297 * fragmented check.
6298 */
6299 if (cluster && cluster->fragmented &&
6300 total_unpinned > empty_cluster) {
6301 spin_lock(&cluster->lock);
6302 cluster->fragmented = 0;
6303 spin_unlock(&cluster->lock);
6304 }
6305
Josef Bacik7b398f82012-10-22 15:52:28 -04006306 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006307 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006308 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006309 space_info->bytes_pinned -= len;
Josef Bacik4f4db212015-09-29 11:40:47 -04006310 space_info->max_extent_size = 0;
Liu Bod288db52014-07-02 16:58:01 +08006311 percpu_counter_add(&space_info->total_bytes_pinned, -len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006312 if (cache->ro) {
6313 space_info->bytes_readonly += len;
6314 readonly = true;
6315 }
Josef Bacik25179202008-10-29 14:49:05 -04006316 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04006317 if (!readonly && global_rsv->space_info == space_info) {
6318 spin_lock(&global_rsv->lock);
6319 if (!global_rsv->full) {
6320 len = min(len, global_rsv->size -
6321 global_rsv->reserved);
6322 global_rsv->reserved += len;
6323 space_info->bytes_may_use += len;
6324 if (global_rsv->reserved >= global_rsv->size)
6325 global_rsv->full = 1;
6326 }
6327 spin_unlock(&global_rsv->lock);
6328 }
6329 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006330 }
6331
6332 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006333 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04006334 return 0;
6335}
6336
6337int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04006338 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05006339{
Yan Zheng11833d62009-09-11 16:11:19 -04006340 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006341 struct btrfs_block_group_cache *block_group, *tmp;
6342 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04006343 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04006344 u64 start;
6345 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05006346 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05006347
Yan Zheng11833d62009-09-11 16:11:19 -04006348 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6349 unpin = &fs_info->freed_extents[1];
6350 else
6351 unpin = &fs_info->freed_extents[0];
6352
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006353 while (!trans->aborted) {
Filipe Mananad4b450c2015-01-29 19:18:25 +00006354 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04006355 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04006356 EXTENT_DIRTY, NULL);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006357 if (ret) {
6358 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05006359 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00006360 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006361
Li Dongyang5378e602011-03-24 10:24:27 +00006362 if (btrfs_test_opt(root, DISCARD))
6363 ret = btrfs_discard_extent(root, start,
6364 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006365
Chris Mason1a5bc162007-10-15 16:15:26 -04006366 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Filipe Manana678886b2014-12-07 21:31:47 +00006367 unpin_extent_range(root, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006368 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masonb9473432009-03-13 11:00:37 -04006369 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05006370 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006371
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006372 /*
6373 * Transaction is finished. We don't need the lock anymore. We
6374 * do need to clean up the block groups in case of a transaction
6375 * abort.
6376 */
6377 deleted_bgs = &trans->transaction->deleted_bgs;
6378 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
6379 u64 trimmed = 0;
6380
6381 ret = -EROFS;
6382 if (!trans->aborted)
6383 ret = btrfs_discard_extent(root,
6384 block_group->key.objectid,
6385 block_group->key.offset,
6386 &trimmed);
6387
6388 list_del_init(&block_group->bg_list);
6389 btrfs_put_block_group_trimming(block_group);
6390 btrfs_put_block_group(block_group);
6391
6392 if (ret) {
6393 const char *errstr = btrfs_decode_error(ret);
6394 btrfs_warn(fs_info,
6395 "Discard failed while removing blockgroup: errno=%d %s\n",
6396 ret, errstr);
6397 }
6398 }
6399
Chris Masone20d96d2007-03-22 12:13:20 -04006400 return 0;
6401}
6402
Josef Bacikb150a4f2013-06-19 15:00:04 -04006403static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
6404 u64 owner, u64 root_objectid)
6405{
6406 struct btrfs_space_info *space_info;
6407 u64 flags;
6408
6409 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
6410 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
6411 flags = BTRFS_BLOCK_GROUP_SYSTEM;
6412 else
6413 flags = BTRFS_BLOCK_GROUP_METADATA;
6414 } else {
6415 flags = BTRFS_BLOCK_GROUP_DATA;
6416 }
6417
6418 space_info = __find_space_info(fs_info, flags);
6419 BUG_ON(!space_info); /* Logic bug */
6420 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
6421}
6422
6423
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006424static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
6425 struct btrfs_root *root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006426 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006427 u64 root_objectid, u64 owner_objectid,
6428 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006429 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05006430{
Chris Masone2fa7222007-03-12 16:22:34 -04006431 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006432 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04006433 struct btrfs_fs_info *info = root->fs_info;
6434 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04006435 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006436 struct btrfs_extent_item *ei;
6437 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05006438 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006439 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05006440 int extent_slot = 0;
6441 int found_extent = 0;
6442 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006443 u32 item_size;
6444 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006445 u64 bytenr = node->bytenr;
6446 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07006447 int last_ref = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05006448 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6449 SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05006450
Chris Mason5caf2a02007-04-02 11:20:42 -04006451 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04006452 if (!path)
6453 return -ENOMEM;
6454
Chris Mason3c12ac72008-04-21 12:01:38 -04006455 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04006456 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006457
6458 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
6459 BUG_ON(!is_data && refs_to_drop != 1);
6460
Josef Bacik3173a182013-03-07 14:22:04 -05006461 if (is_data)
6462 skinny_metadata = 0;
6463
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006464 ret = lookup_extent_backref(trans, extent_root, path, &iref,
6465 bytenr, num_bytes, parent,
6466 root_objectid, owner_objectid,
6467 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05006468 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05006469 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006470 while (extent_slot >= 0) {
6471 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05006472 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006473 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05006474 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006475 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
6476 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05006477 found_extent = 1;
6478 break;
6479 }
Josef Bacik3173a182013-03-07 14:22:04 -05006480 if (key.type == BTRFS_METADATA_ITEM_KEY &&
6481 key.offset == owner_objectid) {
6482 found_extent = 1;
6483 break;
6484 }
Chris Mason952fcca2008-02-18 16:33:44 -05006485 if (path->slots[0] - extent_slot > 5)
6486 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006487 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05006488 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006489#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6490 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
6491 if (found_extent && item_size < sizeof(*ei))
6492 found_extent = 0;
6493#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04006494 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006495 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04006496 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006497 NULL, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07006498 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06006499 if (ret) {
6500 btrfs_abort_transaction(trans, extent_root, ret);
6501 goto out;
6502 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006503 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04006504 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006505
6506 key.objectid = bytenr;
6507 key.type = BTRFS_EXTENT_ITEM_KEY;
6508 key.offset = num_bytes;
6509
Josef Bacik3173a182013-03-07 14:22:04 -05006510 if (!is_data && skinny_metadata) {
6511 key.type = BTRFS_METADATA_ITEM_KEY;
6512 key.offset = owner_objectid;
6513 }
6514
Zheng Yan31840ae2008-09-23 13:14:14 -04006515 ret = btrfs_search_slot(trans, extent_root,
6516 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05006517 if (ret > 0 && skinny_metadata && path->slots[0]) {
6518 /*
6519 * Couldn't find our skinny metadata item,
6520 * see if we have ye olde extent item.
6521 */
6522 path->slots[0]--;
6523 btrfs_item_key_to_cpu(path->nodes[0], &key,
6524 path->slots[0]);
6525 if (key.objectid == bytenr &&
6526 key.type == BTRFS_EXTENT_ITEM_KEY &&
6527 key.offset == num_bytes)
6528 ret = 0;
6529 }
6530
6531 if (ret > 0 && skinny_metadata) {
6532 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01006533 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05006534 key.type = BTRFS_EXTENT_ITEM_KEY;
6535 key.offset = num_bytes;
6536 btrfs_release_path(path);
6537 ret = btrfs_search_slot(trans, extent_root,
6538 &key, path, -1, 1);
6539 }
6540
Josef Bacikf3465ca2008-11-12 14:19:50 -05006541 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006542 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006543 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00006544 if (ret > 0)
6545 btrfs_print_leaf(extent_root,
6546 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006547 }
David Sterba005d6422012-09-18 07:52:32 -06006548 if (ret < 0) {
6549 btrfs_abort_transaction(trans, extent_root, ret);
6550 goto out;
6551 }
Zheng Yan31840ae2008-09-23 13:14:14 -04006552 extent_slot = path->slots[0];
6553 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306554 } else if (WARN_ON(ret == -ENOENT)) {
Chris Mason7bb86312007-12-11 09:25:06 -05006555 btrfs_print_leaf(extent_root, path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006556 btrfs_err(info,
6557 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006558 bytenr, parent, root_objectid, owner_objectid,
6559 owner_offset);
Josef Bacikc4a050b2014-03-14 16:36:53 -04006560 btrfs_abort_transaction(trans, extent_root, ret);
6561 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006562 } else {
David Sterba005d6422012-09-18 07:52:32 -06006563 btrfs_abort_transaction(trans, extent_root, ret);
6564 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05006565 }
Chris Mason5f39d392007-10-15 16:14:19 -04006566
6567 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006568 item_size = btrfs_item_size_nr(leaf, extent_slot);
6569#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6570 if (item_size < sizeof(*ei)) {
6571 BUG_ON(found_extent || extent_slot != path->slots[0]);
6572 ret = convert_extent_item_v0(trans, extent_root, path,
6573 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06006574 if (ret < 0) {
6575 btrfs_abort_transaction(trans, extent_root, ret);
6576 goto out;
6577 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006578
David Sterbab3b4aa72011-04-21 01:20:15 +02006579 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006580 path->leave_spinning = 1;
6581
6582 key.objectid = bytenr;
6583 key.type = BTRFS_EXTENT_ITEM_KEY;
6584 key.offset = num_bytes;
6585
6586 ret = btrfs_search_slot(trans, extent_root, &key, path,
6587 -1, 1);
6588 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006589 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006590 ret, bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006591 btrfs_print_leaf(extent_root, path->nodes[0]);
6592 }
David Sterba005d6422012-09-18 07:52:32 -06006593 if (ret < 0) {
6594 btrfs_abort_transaction(trans, extent_root, ret);
6595 goto out;
6596 }
6597
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006598 extent_slot = path->slots[0];
6599 leaf = path->nodes[0];
6600 item_size = btrfs_item_size_nr(leaf, extent_slot);
6601 }
6602#endif
6603 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05006604 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04006605 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05006606 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
6607 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006608 struct btrfs_tree_block_info *bi;
6609 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
6610 bi = (struct btrfs_tree_block_info *)(ei + 1);
6611 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05006612 }
6613
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006614 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04006615 if (refs < refs_to_drop) {
6616 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
David Sterba351fd352014-05-15 16:48:20 +02006617 "for bytenr %Lu", refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04006618 ret = -EINVAL;
6619 btrfs_abort_transaction(trans, extent_root, ret);
6620 goto out;
6621 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006622 refs -= refs_to_drop;
6623
6624 if (refs > 0) {
6625 if (extent_op)
6626 __run_delayed_extent_op(extent_op, leaf, ei);
6627 /*
6628 * In the case of inline back ref, reference count will
6629 * be updated by remove_extent_backref
6630 */
6631 if (iref) {
6632 BUG_ON(!found_extent);
6633 } else {
6634 btrfs_set_extent_refs(leaf, ei, refs);
6635 btrfs_mark_buffer_dirty(leaf);
6636 }
6637 if (found_extent) {
6638 ret = remove_extent_backref(trans, extent_root, path,
6639 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07006640 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06006641 if (ret) {
6642 btrfs_abort_transaction(trans, extent_root, ret);
6643 goto out;
6644 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006645 }
Josef Bacikb150a4f2013-06-19 15:00:04 -04006646 add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
6647 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006648 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006649 if (found_extent) {
6650 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08006651 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006652 if (iref) {
6653 BUG_ON(path->slots[0] != extent_slot);
6654 } else {
6655 BUG_ON(path->slots[0] != extent_slot + 1);
6656 path->slots[0] = extent_slot;
6657 num_to_del = 2;
6658 }
Chris Mason78fae272007-03-25 11:35:08 -04006659 }
Chris Masonb9473432009-03-13 11:00:37 -04006660
Josef Bacikfcebe452014-05-13 17:30:47 -07006661 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05006662 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
6663 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06006664 if (ret) {
6665 btrfs_abort_transaction(trans, extent_root, ret);
6666 goto out;
6667 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006668 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01006669
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006670 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05006671 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06006672 if (ret) {
6673 btrfs_abort_transaction(trans, extent_root, ret);
6674 goto out;
6675 }
Chris Mason459931e2008-12-10 09:10:46 -05006676 }
6677
Omar Sandoval1e144fb2015-09-29 20:50:37 -07006678 ret = add_to_free_space_tree(trans, root->fs_info, bytenr,
6679 num_bytes);
6680 if (ret) {
6681 btrfs_abort_transaction(trans, extent_root, ret);
6682 goto out;
6683 }
6684
Josef Bacikce93ec52014-11-17 15:45:48 -05006685 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06006686 if (ret) {
6687 btrfs_abort_transaction(trans, extent_root, ret);
6688 goto out;
6689 }
Chris Masona28ec192007-03-06 20:08:01 -05006690 }
Josef Bacikfcebe452014-05-13 17:30:47 -07006691 btrfs_release_path(path);
6692
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006693out:
Chris Mason5caf2a02007-04-02 11:20:42 -04006694 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05006695 return ret;
6696}
6697
6698/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006699 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04006700 * delayed ref for that extent as well. This searches the delayed ref tree for
6701 * a given extent, and if there are no other delayed refs to be processed, it
6702 * removes it from the tree.
6703 */
6704static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
6705 struct btrfs_root *root, u64 bytenr)
6706{
6707 struct btrfs_delayed_ref_head *head;
6708 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006709 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04006710
6711 delayed_refs = &trans->transaction->delayed_refs;
6712 spin_lock(&delayed_refs->lock);
6713 head = btrfs_find_delayed_ref_head(trans, bytenr);
6714 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08006715 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04006716
Josef Bacikd7df2c72014-01-23 09:21:38 -05006717 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08006718 if (!list_empty(&head->ref_list))
Chris Mason1887be62009-03-13 10:11:24 -04006719 goto out;
6720
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006721 if (head->extent_op) {
6722 if (!head->must_insert_reserved)
6723 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00006724 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006725 head->extent_op = NULL;
6726 }
6727
Chris Mason1887be62009-03-13 10:11:24 -04006728 /*
6729 * waiting for the lock here would deadlock. If someone else has it
6730 * locked they are already in the process of dropping it anyway
6731 */
6732 if (!mutex_trylock(&head->mutex))
6733 goto out;
6734
6735 /*
6736 * at this point we have a head with no other entries. Go
6737 * ahead and process it.
6738 */
6739 head->node.in_tree = 0;
Liu Boc46effa2013-10-14 12:59:45 +08006740 rb_erase(&head->href_node, &delayed_refs->href_root);
Chris Masonc3e69d52009-03-13 10:17:05 -04006741
Josef Bacikd7df2c72014-01-23 09:21:38 -05006742 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04006743
6744 /*
6745 * we don't take a ref on the node because we're removing it from the
6746 * tree, so we just steal the ref the tree was holding.
6747 */
Chris Masonc3e69d52009-03-13 10:17:05 -04006748 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05006749 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04006750 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05006751 head->processing = 0;
6752 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04006753 spin_unlock(&delayed_refs->lock);
6754
Yan, Zhengf0486c62010-05-16 10:46:25 -04006755 BUG_ON(head->extent_op);
6756 if (head->must_insert_reserved)
6757 ret = 1;
6758
6759 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04006760 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006761 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04006762out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05006763 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08006764
6765out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04006766 spin_unlock(&delayed_refs->lock);
6767 return 0;
6768}
6769
Yan, Zhengf0486c62010-05-16 10:46:25 -04006770void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
6771 struct btrfs_root *root,
6772 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02006773 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006774{
Josef Bacikb150a4f2013-06-19 15:00:04 -04006775 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006776 int ret;
6777
6778 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006779 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6780 buf->start, buf->len,
6781 parent, root->root_key.objectid,
6782 btrfs_header_level(buf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01006783 BTRFS_DROP_DELAYED_REF, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006784 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006785 }
6786
6787 if (!last_ref)
6788 return;
6789
Yan, Zhengf0486c62010-05-16 10:46:25 -04006790 if (btrfs_header_generation(buf) == trans->transid) {
Filipe Manana62198722015-01-06 20:18:45 +00006791 struct btrfs_block_group_cache *cache;
6792
Yan, Zhengf0486c62010-05-16 10:46:25 -04006793 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
6794 ret = check_ref_cleanup(trans, root, buf->start);
6795 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04006796 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006797 }
6798
Filipe Manana62198722015-01-06 20:18:45 +00006799 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
6800
Yan, Zhengf0486c62010-05-16 10:46:25 -04006801 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
6802 pin_down_extent(root, cache, buf->start, buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00006803 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04006804 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006805 }
6806
6807 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
6808
6809 btrfs_add_free_space(cache, buf->start, buf->len);
Miao Xiee570fd22014-06-19 10:42:50 +08006810 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0);
Filipe Manana62198722015-01-06 20:18:45 +00006811 btrfs_put_block_group(cache);
Josef Bacik0be5dc62013-10-07 15:18:52 -04006812 trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
Josef Bacikb150a4f2013-06-19 15:00:04 -04006813 pin = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006814 }
6815out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04006816 if (pin)
6817 add_pinned_bytes(root->fs_info, buf->len,
6818 btrfs_header_level(buf),
6819 root->root_key.objectid);
6820
Josef Bacika826d6d2011-03-16 13:42:43 -04006821 /*
6822 * Deleting the buffer, clear the corrupt flag since it doesn't matter
6823 * anymore.
6824 */
6825 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006826}
6827
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006828/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006829int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6830 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01006831 u64 owner, u64 offset)
Chris Mason925baed2008-06-25 16:01:30 -04006832{
6833 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006834 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04006835
David Sterbafccb84c2014-09-29 23:53:21 +02006836 if (btrfs_test_is_dummy_root(root))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04006837 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02006838
Josef Bacikb150a4f2013-06-19 15:00:04 -04006839 add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
6840
Chris Mason56bec292009-03-13 10:10:06 -04006841 /*
6842 * tree log blocks never actually go into the extent allocation
6843 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04006844 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006845 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
6846 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04006847 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04006848 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04006849 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006850 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006851 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
6852 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006853 parent, root_objectid, (int)owner,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01006854 BTRFS_DROP_DELAYED_REF, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006855 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006856 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
6857 num_bytes,
6858 parent, root_objectid, owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08006859 offset, 0,
6860 BTRFS_DROP_DELAYED_REF, NULL);
Chris Mason56bec292009-03-13 10:10:06 -04006861 }
Chris Mason925baed2008-06-25 16:01:30 -04006862 return ret;
6863}
6864
Chris Masonfec577f2007-02-26 10:40:21 -05006865/*
Josef Bacik817d52f2009-07-13 21:29:25 -04006866 * when we wait for progress in the block group caching, its because
6867 * our allocation attempt failed at least once. So, we must sleep
6868 * and let some progress happen before we try again.
6869 *
6870 * This function will sleep at least once waiting for new free space to
6871 * show up, and then it will check the block group free space numbers
6872 * for our min num_bytes. Another option is to have it go ahead
6873 * and look in the rbtree for a free extent of a given size, but this
6874 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04006875 *
6876 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
6877 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04006878 */
Josef Bacik36cce922013-08-05 11:15:21 -04006879static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04006880wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
6881 u64 num_bytes)
6882{
Yan Zheng11833d62009-09-11 16:11:19 -04006883 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04006884
Yan Zheng11833d62009-09-11 16:11:19 -04006885 caching_ctl = get_caching_control(cache);
6886 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04006887 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04006888
Yan Zheng11833d62009-09-11 16:11:19 -04006889 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08006890 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04006891
6892 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04006893}
6894
6895static noinline int
6896wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
6897{
6898 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04006899 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006900
6901 caching_ctl = get_caching_control(cache);
6902 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04006903 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006904
6905 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04006906 if (cache->cached == BTRFS_CACHE_ERROR)
6907 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04006908 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04006909 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04006910}
6911
Liu Bo31e50222012-11-21 14:18:10 +00006912int __get_raid_index(u64 flags)
Yan, Zhengb742bb822010-05-16 10:46:24 -04006913{
Ilya Dryomov7738a532012-03-27 17:09:17 +03006914 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00006915 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006916 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00006917 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006918 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00006919 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006920 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00006921 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006922 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05006923 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05006924 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05006925 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006926
Chris Masone942f882013-02-20 14:06:05 -05006927 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb822010-05-16 10:46:24 -04006928}
6929
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006930int get_block_group_index(struct btrfs_block_group_cache *cache)
Ilya Dryomov7738a532012-03-27 17:09:17 +03006931{
Liu Bo31e50222012-11-21 14:18:10 +00006932 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03006933}
6934
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006935static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
6936 [BTRFS_RAID_RAID10] = "raid10",
6937 [BTRFS_RAID_RAID1] = "raid1",
6938 [BTRFS_RAID_DUP] = "dup",
6939 [BTRFS_RAID_RAID0] = "raid0",
6940 [BTRFS_RAID_SINGLE] = "single",
6941 [BTRFS_RAID_RAID5] = "raid5",
6942 [BTRFS_RAID_RAID6] = "raid6",
6943};
6944
Jeff Mahoney1b8e5df2013-11-20 16:50:23 -05006945static const char *get_raid_name(enum btrfs_raid_types type)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006946{
6947 if (type >= BTRFS_NR_RAID_TYPES)
6948 return NULL;
6949
6950 return btrfs_raid_type_names[type];
6951}
6952
Josef Bacik817d52f2009-07-13 21:29:25 -04006953enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05006954 LOOP_CACHING_NOWAIT = 0,
6955 LOOP_CACHING_WAIT = 1,
6956 LOOP_ALLOC_CHUNK = 2,
6957 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04006958};
6959
Miao Xiee570fd22014-06-19 10:42:50 +08006960static inline void
6961btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
6962 int delalloc)
6963{
6964 if (delalloc)
6965 down_read(&cache->data_rwsem);
6966}
6967
6968static inline void
6969btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
6970 int delalloc)
6971{
6972 btrfs_get_block_group(cache);
6973 if (delalloc)
6974 down_read(&cache->data_rwsem);
6975}
6976
6977static struct btrfs_block_group_cache *
6978btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
6979 struct btrfs_free_cluster *cluster,
6980 int delalloc)
6981{
6982 struct btrfs_block_group_cache *used_bg;
6983 bool locked = false;
6984again:
6985 spin_lock(&cluster->refill_lock);
6986 if (locked) {
6987 if (used_bg == cluster->block_group)
6988 return used_bg;
6989
6990 up_read(&used_bg->data_rwsem);
6991 btrfs_put_block_group(used_bg);
6992 }
6993
6994 used_bg = cluster->block_group;
6995 if (!used_bg)
6996 return NULL;
6997
6998 if (used_bg == block_group)
6999 return used_bg;
7000
7001 btrfs_get_block_group(used_bg);
7002
7003 if (!delalloc)
7004 return used_bg;
7005
7006 if (down_read_trylock(&used_bg->data_rwsem))
7007 return used_bg;
7008
7009 spin_unlock(&cluster->refill_lock);
7010 down_read(&used_bg->data_rwsem);
7011 locked = true;
7012 goto again;
7013}
7014
7015static inline void
7016btrfs_release_block_group(struct btrfs_block_group_cache *cache,
7017 int delalloc)
7018{
7019 if (delalloc)
7020 up_read(&cache->data_rwsem);
7021 btrfs_put_block_group(cache);
7022}
7023
Josef Bacik817d52f2009-07-13 21:29:25 -04007024/*
Chris Masonfec577f2007-02-26 10:40:21 -05007025 * walks the btree of allocated extents and find a hole of a given size.
7026 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08007027 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04007028 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08007029 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05007030 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08007031 *
7032 * If there is no suitable free space, we will record the max size of
7033 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05007034 */
Josef Bacik00361582013-08-14 14:02:47 -04007035static noinline int find_free_extent(struct btrfs_root *orig_root,
Chris Mason98ed5172008-01-03 10:01:48 -05007036 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05007037 u64 hint_byte, struct btrfs_key *ins,
Miao Xiee570fd22014-06-19 10:42:50 +08007038 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007039{
Josef Bacik80eb2342008-10-29 14:49:05 -04007040 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05007041 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007042 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04007043 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05007044 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08007045 u64 max_extent_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007046 u64 empty_cluster = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04007047 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007048 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00007049 int index = __get_raid_index(flags);
7050 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
Josef Bacikfb25e912011-07-26 17:00:46 -04007051 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik0a243252009-09-11 16:11:20 -04007052 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007053 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04007054 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08007055 bool have_caching_bg = false;
chandan13a0db52015-11-02 13:59:46 +05307056 bool orig_have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007057 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05007058
Chris Masondb945352007-10-15 16:15:53 -04007059 WARN_ON(num_bytes < root->sectorsize);
David Sterba962a2982014-06-04 18:41:45 +02007060 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04007061 ins->objectid = 0;
7062 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04007063
David Sterbab6919a52013-04-29 13:39:40 +00007064 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05007065
David Sterbab6919a52013-04-29 13:39:40 +00007066 space_info = __find_space_info(root->fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007067 if (!space_info) {
David Sterbab6919a52013-04-29 13:39:40 +00007068 btrfs_err(root->fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00007069 return -ENOSPC;
7070 }
Josef Bacik2552d172009-04-03 10:14:19 -04007071
Josef Bacik67377732010-09-16 16:19:09 -04007072 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04007073 * If our free space is heavily fragmented we may not be able to make
7074 * big contiguous allocations, so instead of doing the expensive search
7075 * for free space, simply return ENOSPC with our max_extent_size so we
7076 * can go ahead and search for a more manageable chunk.
7077 *
7078 * If our max_extent_size is large enough for our allocation simply
7079 * disable clustering since we will likely not be able to find enough
7080 * space to create a cluster and induce latency trying.
Josef Bacik67377732010-09-16 16:19:09 -04007081 */
Josef Bacik4f4db212015-09-29 11:40:47 -04007082 if (unlikely(space_info->max_extent_size)) {
7083 spin_lock(&space_info->lock);
7084 if (space_info->max_extent_size &&
7085 num_bytes > space_info->max_extent_size) {
7086 ins->offset = space_info->max_extent_size;
7087 spin_unlock(&space_info->lock);
7088 return -ENOSPC;
7089 } else if (space_info->max_extent_size) {
7090 use_cluster = false;
7091 }
7092 spin_unlock(&space_info->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007093 }
7094
Josef Bacikc759c4e2015-10-02 15:25:10 -04007095 last_ptr = fetch_cluster_info(orig_root, space_info, &empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04007096 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007097 spin_lock(&last_ptr->lock);
7098 if (last_ptr->block_group)
7099 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007100 if (last_ptr->fragmented) {
7101 /*
7102 * We still set window_start so we can keep track of the
7103 * last place we found an allocation to try and save
7104 * some time.
7105 */
7106 hint_byte = last_ptr->window_start;
7107 use_cluster = false;
7108 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007109 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007110 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007111
Chris Masona061fc82008-05-07 11:43:44 -04007112 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04007113 search_start = max(search_start, hint_byte);
Josef Bacik2552d172009-04-03 10:14:19 -04007114 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04007115 block_group = btrfs_lookup_block_group(root->fs_info,
7116 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04007117 /*
7118 * we don't want to use the block group if it doesn't match our
7119 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05007120 *
7121 * However if we are re-searching with an ideal block group
7122 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04007123 */
David Sterbab6919a52013-04-29 13:39:40 +00007124 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05007125 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04007126 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007127 if (list_empty(&block_group->list) ||
7128 block_group->ro) {
7129 /*
7130 * someone is removing this block group,
7131 * we can't jump into the have_block_group
7132 * target because our list pointers are not
7133 * valid
7134 */
7135 btrfs_put_block_group(block_group);
7136 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05007137 } else {
Yan, Zhengb742bb822010-05-16 10:46:24 -04007138 index = get_block_group_index(block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007139 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04007140 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05007141 }
Josef Bacik2552d172009-04-03 10:14:19 -04007142 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007143 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04007144 }
Chris Mason42e70e72008-11-07 18:17:11 -05007145 }
Josef Bacik2552d172009-04-03 10:14:19 -04007146search:
Miao Xie60d2adb2011-09-09 17:34:35 +08007147 have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007148 if (index == 0 || index == __get_raid_index(flags))
7149 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04007150 down_read(&space_info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007151 list_for_each_entry(block_group, &space_info->block_groups[index],
7152 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04007153 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04007154 int cached;
Chris Mason8a1413a22008-11-10 16:13:54 -05007155
Miao Xiee570fd22014-06-19 10:42:50 +08007156 btrfs_grab_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007157 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05007158
Chris Mason83a50de2010-12-13 15:06:46 -05007159 /*
7160 * this can happen if we end up cycling through all the
7161 * raid types, but we want to make sure we only allocate
7162 * for the proper type.
7163 */
David Sterbab6919a52013-04-29 13:39:40 +00007164 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05007165 u64 extra = BTRFS_BLOCK_GROUP_DUP |
7166 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007167 BTRFS_BLOCK_GROUP_RAID5 |
7168 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05007169 BTRFS_BLOCK_GROUP_RAID10;
7170
7171 /*
7172 * if they asked for extra copies and this block group
7173 * doesn't provide them, bail. This does allow us to
7174 * fill raid0 from raid1.
7175 */
David Sterbab6919a52013-04-29 13:39:40 +00007176 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05007177 goto loop;
7178 }
7179
Josef Bacik2552d172009-04-03 10:14:19 -04007180have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05007181 cached = block_group_cache_done(block_group);
7182 if (unlikely(!cached)) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007183 have_caching_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00007184 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04007185 BUG_ON(ret < 0);
7186 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05007187 }
7188
Josef Bacik36cce922013-08-05 11:15:21 -04007189 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
7190 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05007191 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04007192 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007193
Josef Bacik0a243252009-09-11 16:11:20 -04007194 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007195 * Ok we want to try and use the cluster allocator, so
7196 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04007197 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04007198 if (last_ptr && use_cluster) {
Miao Xie215a63d2014-01-15 20:00:56 +08007199 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05007200 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007201 /*
7202 * the refill lock keeps out other
7203 * people trying to start a new cluster
7204 */
Miao Xiee570fd22014-06-19 10:42:50 +08007205 used_block_group = btrfs_lock_cluster(block_group,
7206 last_ptr,
7207 delalloc);
7208 if (!used_block_group)
Chris Mason44fb5512009-06-04 15:34:51 -04007209 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04007210
Miao Xiee570fd22014-06-19 10:42:50 +08007211 if (used_block_group != block_group &&
7212 (used_block_group->ro ||
7213 !block_group_bits(used_block_group, flags)))
7214 goto release_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007215
7216 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007217 last_ptr,
7218 num_bytes,
7219 used_block_group->key.objectid,
7220 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007221 if (offset) {
7222 /* we have a block, we're done */
7223 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05007224 trace_btrfs_reserve_extent_cluster(root,
Miao Xie89d43462014-01-15 20:00:57 +08007225 used_block_group,
7226 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08007227 if (used_block_group != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08007228 btrfs_release_block_group(block_group,
7229 delalloc);
Miao Xie215a63d2014-01-15 20:00:56 +08007230 block_group = used_block_group;
7231 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007232 goto checks;
7233 }
7234
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007235 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007236release_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007237 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7238 * set up a new clusters, so lets just skip it
7239 * and let the allocator find whatever block
7240 * it can find. If we reach this point, we
7241 * will have tried the cluster allocator
7242 * plenty of times and not have found
7243 * anything, so we are likely way too
7244 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007245 * anything.
7246 *
7247 * However, if the cluster is taken from the
7248 * current block group, release the cluster
7249 * first, so that we stand a better chance of
7250 * succeeding in the unclustered
7251 * allocation. */
7252 if (loop >= LOOP_NO_EMPTY_SIZE &&
Miao Xiee570fd22014-06-19 10:42:50 +08007253 used_block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007254 spin_unlock(&last_ptr->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007255 btrfs_release_block_group(used_block_group,
7256 delalloc);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007257 goto unclustered_alloc;
7258 }
7259
Chris Masonfa9c0d792009-04-03 09:47:43 -04007260 /*
7261 * this cluster didn't work out, free it and
7262 * start over
7263 */
7264 btrfs_return_cluster_to_free_space(NULL, last_ptr);
7265
Miao Xiee570fd22014-06-19 10:42:50 +08007266 if (used_block_group != block_group)
7267 btrfs_release_block_group(used_block_group,
7268 delalloc);
7269refill_cluster:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007270 if (loop >= LOOP_NO_EMPTY_SIZE) {
7271 spin_unlock(&last_ptr->refill_lock);
7272 goto unclustered_alloc;
7273 }
7274
Chris Mason8de972b2013-01-04 15:39:43 -05007275 aligned_cluster = max_t(unsigned long,
7276 empty_cluster + empty_size,
7277 block_group->full_stripe_len);
7278
Chris Masonfa9c0d792009-04-03 09:47:43 -04007279 /* allocate a cluster in this block group */
Josef Bacik00361582013-08-14 14:02:47 -04007280 ret = btrfs_find_space_cluster(root, block_group,
7281 last_ptr, search_start,
7282 num_bytes,
7283 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007284 if (ret == 0) {
7285 /*
7286 * now pull our allocation out of this
7287 * cluster
7288 */
7289 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007290 last_ptr,
7291 num_bytes,
7292 search_start,
7293 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007294 if (offset) {
7295 /* we found one, proceed */
7296 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05007297 trace_btrfs_reserve_extent_cluster(root,
7298 block_group, search_start,
7299 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007300 goto checks;
7301 }
Josef Bacik0a243252009-09-11 16:11:20 -04007302 } else if (!cached && loop > LOOP_CACHING_NOWAIT
7303 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007304 spin_unlock(&last_ptr->refill_lock);
7305
Josef Bacik0a243252009-09-11 16:11:20 -04007306 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007307 wait_block_group_cache_progress(block_group,
7308 num_bytes + empty_cluster + empty_size);
7309 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007310 }
Josef Bacik817d52f2009-07-13 21:29:25 -04007311
Chris Masonfa9c0d792009-04-03 09:47:43 -04007312 /*
7313 * at this point we either didn't find a cluster
7314 * or we weren't able to allocate a block from our
7315 * cluster. Free the cluster we've been trying
7316 * to use, and go to the next block group
7317 */
Josef Bacik0a243252009-09-11 16:11:20 -04007318 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007319 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04007320 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007321 }
7322
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007323unclustered_alloc:
Josef Bacikc759c4e2015-10-02 15:25:10 -04007324 /*
7325 * We are doing an unclustered alloc, set the fragmented flag so
7326 * we don't bother trying to setup a cluster again until we get
7327 * more space.
7328 */
7329 if (unlikely(last_ptr)) {
7330 spin_lock(&last_ptr->lock);
7331 last_ptr->fragmented = 1;
7332 spin_unlock(&last_ptr->lock);
7333 }
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007334 spin_lock(&block_group->free_space_ctl->tree_lock);
7335 if (cached &&
7336 block_group->free_space_ctl->free_space <
7337 num_bytes + empty_cluster + empty_size) {
Miao Xiea4820392013-09-09 13:19:42 +08007338 if (block_group->free_space_ctl->free_space >
7339 max_extent_size)
7340 max_extent_size =
7341 block_group->free_space_ctl->free_space;
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007342 spin_unlock(&block_group->free_space_ctl->tree_lock);
7343 goto loop;
7344 }
7345 spin_unlock(&block_group->free_space_ctl->tree_lock);
7346
Josef Bacik6226cb02009-04-03 10:14:18 -04007347 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08007348 num_bytes, empty_size,
7349 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007350 /*
7351 * If we didn't find a chunk, and we haven't failed on this
7352 * block group before, and this block group is in the middle of
7353 * caching and we are ok with waiting, then go ahead and wait
7354 * for progress to be made, and set failed_alloc to true.
7355 *
7356 * If failed_alloc is true then we've already waited on this
7357 * block group once and should move on to the next block group.
7358 */
7359 if (!offset && !failed_alloc && !cached &&
7360 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007361 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007362 num_bytes + empty_size);
7363 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007364 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007365 } else if (!offset) {
7366 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04007367 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007368checks:
David Sterba4e54b172014-06-05 01:39:19 +02007369 search_start = ALIGN(offset, root->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04007370
Josef Bacik2552d172009-04-03 10:14:19 -04007371 /* move on to the next group */
7372 if (search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08007373 block_group->key.objectid + block_group->key.offset) {
7374 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04007375 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04007376 }
Josef Bacik80eb2342008-10-29 14:49:05 -04007377
Josef Bacik6226cb02009-04-03 10:14:18 -04007378 if (offset < search_start)
Miao Xie215a63d2014-01-15 20:00:56 +08007379 btrfs_add_free_space(block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04007380 search_start - offset);
7381 BUG_ON(offset > search_start);
7382
Miao Xie215a63d2014-01-15 20:00:56 +08007383 ret = btrfs_update_reserved_bytes(block_group, num_bytes,
Miao Xiee570fd22014-06-19 10:42:50 +08007384 alloc_type, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007385 if (ret == -EAGAIN) {
Miao Xie215a63d2014-01-15 20:00:56 +08007386 btrfs_add_free_space(block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007387 goto loop;
7388 }
Yan Zheng11833d62009-09-11 16:11:19 -04007389
Josef Bacik2552d172009-04-03 10:14:19 -04007390 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007391 ins->objectid = search_start;
7392 ins->offset = num_bytes;
7393
Josef Bacik3f7de032011-11-10 08:29:20 -05007394 trace_btrfs_reserve_extent(orig_root, block_group,
7395 search_start, num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08007396 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007397 break;
7398loop:
Josef Bacik0a243252009-09-11 16:11:20 -04007399 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007400 failed_alloc = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04007401 BUG_ON(index != get_block_group_index(block_group));
Miao Xiee570fd22014-06-19 10:42:50 +08007402 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007403 }
7404 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05007405
chandan13a0db52015-11-02 13:59:46 +05307406 if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg
7407 && !orig_have_caching_bg)
7408 orig_have_caching_bg = true;
7409
Miao Xie60d2adb2011-09-09 17:34:35 +08007410 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
7411 goto search;
7412
Yan, Zhengb742bb822010-05-16 10:46:24 -04007413 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
7414 goto search;
7415
Josef Bacik285ff5a2012-01-13 15:27:45 -05007416 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05007417 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
7418 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04007419 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
7420 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
7421 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
7422 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04007423 */
Josef Bacik723bda22011-05-27 16:11:38 -04007424 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb822010-05-16 10:46:24 -04007425 index = 0;
Josef Bacika5e681d2015-10-01 14:54:10 -04007426 if (loop == LOOP_CACHING_NOWAIT) {
7427 /*
7428 * We want to skip the LOOP_CACHING_WAIT step if we
7429 * don't have any unached bgs and we've alrelady done a
7430 * full search through.
7431 */
chandan13a0db52015-11-02 13:59:46 +05307432 if (orig_have_caching_bg || !full_search)
Josef Bacika5e681d2015-10-01 14:54:10 -04007433 loop = LOOP_CACHING_WAIT;
7434 else
7435 loop = LOOP_ALLOC_CHUNK;
7436 } else {
7437 loop++;
7438 }
7439
Josef Bacik817d52f2009-07-13 21:29:25 -04007440 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04007441 struct btrfs_trans_handle *trans;
Wang Shilongf017f152014-03-13 13:19:47 +08007442 int exist = 0;
Josef Bacik00361582013-08-14 14:02:47 -04007443
Wang Shilongf017f152014-03-13 13:19:47 +08007444 trans = current->journal_info;
7445 if (trans)
7446 exist = 1;
7447 else
7448 trans = btrfs_join_transaction(root);
7449
Josef Bacik00361582013-08-14 14:02:47 -04007450 if (IS_ERR(trans)) {
7451 ret = PTR_ERR(trans);
7452 goto out;
7453 }
7454
David Sterbab6919a52013-04-29 13:39:40 +00007455 ret = do_chunk_alloc(trans, root, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04007456 CHUNK_ALLOC_FORCE);
Josef Bacika5e681d2015-10-01 14:54:10 -04007457
7458 /*
7459 * If we can't allocate a new chunk we've already looped
7460 * through at least once, move on to the NO_EMPTY_SIZE
7461 * case.
7462 */
7463 if (ret == -ENOSPC)
7464 loop = LOOP_NO_EMPTY_SIZE;
7465
Josef Bacikea658ba2012-09-11 16:57:25 -04007466 /*
7467 * Do not bail out on ENOSPC since we
7468 * can do more things.
7469 */
Josef Bacik00361582013-08-14 14:02:47 -04007470 if (ret < 0 && ret != -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04007471 btrfs_abort_transaction(trans,
7472 root, ret);
Josef Bacik00361582013-08-14 14:02:47 -04007473 else
7474 ret = 0;
Wang Shilongf017f152014-03-13 13:19:47 +08007475 if (!exist)
7476 btrfs_end_transaction(trans, root);
Josef Bacik00361582013-08-14 14:02:47 -04007477 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04007478 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04007479 }
7480
7481 if (loop == LOOP_NO_EMPTY_SIZE) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007482 /*
7483 * Don't loop again if we already have no empty_size and
7484 * no empty_cluster.
7485 */
7486 if (empty_size == 0 &&
7487 empty_cluster == 0) {
7488 ret = -ENOSPC;
7489 goto out;
7490 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007491 empty_size = 0;
7492 empty_cluster = 0;
7493 }
Chris Mason42e70e72008-11-07 18:17:11 -05007494
Josef Bacik723bda22011-05-27 16:11:38 -04007495 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04007496 } else if (!ins->objectid) {
7497 ret = -ENOSPC;
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007498 } else if (ins->objectid) {
Josef Bacikc759c4e2015-10-02 15:25:10 -04007499 if (!use_cluster && last_ptr) {
7500 spin_lock(&last_ptr->lock);
7501 last_ptr->window_start = ins->objectid;
7502 spin_unlock(&last_ptr->lock);
7503 }
Josef Bacik2552d172009-04-03 10:14:19 -04007504 ret = 0;
7505 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007506out:
Josef Bacik4f4db212015-09-29 11:40:47 -04007507 if (ret == -ENOSPC) {
7508 spin_lock(&space_info->lock);
7509 space_info->max_extent_size = max_extent_size;
7510 spin_unlock(&space_info->lock);
Miao Xiea4820392013-09-09 13:19:42 +08007511 ins->offset = max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04007512 }
Chris Mason0f70abe2007-02-28 16:46:22 -05007513 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05007514}
Chris Masonec44a352008-04-28 15:29:52 -04007515
Josef Bacik9ed74f22009-09-11 16:12:44 -04007516static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
7517 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04007518{
7519 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb822010-05-16 10:46:24 -04007520 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007521
Josef Bacik9ed74f22009-09-11 16:12:44 -04007522 spin_lock(&info->lock);
Frank Holtonefe120a2013-12-20 11:37:06 -05007523 printk(KERN_INFO "BTRFS: space_info %llu has %llu free, is %sfull\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007524 info->flags,
7525 info->total_bytes - info->bytes_used - info->bytes_pinned -
7526 info->bytes_reserved - info->bytes_readonly,
Chris Masond3977122009-01-05 21:25:51 -05007527 (info->full) ? "" : "not ");
Frank Holtonefe120a2013-12-20 11:37:06 -05007528 printk(KERN_INFO "BTRFS: space_info total=%llu, used=%llu, pinned=%llu, "
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04007529 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007530 info->total_bytes, info->bytes_used, info->bytes_pinned,
7531 info->bytes_reserved, info->bytes_may_use,
7532 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007533 spin_unlock(&info->lock);
7534
7535 if (!dump_block_groups)
7536 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007537
Josef Bacik80eb2342008-10-29 14:49:05 -04007538 down_read(&info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007539again:
7540 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04007541 spin_lock(&cache->lock);
Frank Holtonefe120a2013-12-20 11:37:06 -05007542 printk(KERN_INFO "BTRFS: "
7543 "block group %llu has %llu bytes, "
7544 "%llu used %llu pinned %llu reserved %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007545 cache->key.objectid, cache->key.offset,
7546 btrfs_block_group_used(&cache->item), cache->pinned,
7547 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04007548 btrfs_dump_free_space(cache, bytes);
7549 spin_unlock(&cache->lock);
7550 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04007551 if (++index < BTRFS_NR_RAID_TYPES)
7552 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04007553 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007554}
Zheng Yane8569812008-09-26 10:05:48 -04007555
Josef Bacik00361582013-08-14 14:02:47 -04007556int btrfs_reserve_extent(struct btrfs_root *root,
Yan Zheng11833d62009-09-11 16:11:19 -04007557 u64 num_bytes, u64 min_alloc_size,
7558 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08007559 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007560{
Josef Bacik36af4e02015-09-25 16:13:11 -04007561 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00007562 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05007563 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04007564
David Sterbab6919a52013-04-29 13:39:40 +00007565 flags = btrfs_get_alloc_profile(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04007566again:
Chris Masondb945352007-10-15 16:15:53 -04007567 WARN_ON(num_bytes < root->sectorsize);
Josef Bacik00361582013-08-14 14:02:47 -04007568 ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
Miao Xiee570fd22014-06-19 10:42:50 +08007569 flags, delalloc);
Chris Mason3b951512008-04-17 11:29:12 -04007570
Miao Xie9e622d62012-01-26 15:01:12 -05007571 if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08007572 if (!final_tried && ins->offset) {
7573 num_bytes = min(num_bytes >> 1, ins->offset);
Zach Brown24542bf2012-11-16 00:04:43 +00007574 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05007575 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05007576 if (num_bytes == min_alloc_size)
7577 final_tried = true;
7578 goto again;
7579 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
7580 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007581
David Sterbab6919a52013-04-29 13:39:40 +00007582 sinfo = __find_space_info(root->fs_info, flags);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007583 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007584 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01007585 if (sinfo)
7586 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05007587 }
Chris Mason925baed2008-06-25 16:01:30 -04007588 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007589
7590 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007591}
7592
Chris Masone688b7252011-10-31 20:52:39 -04007593static int __btrfs_free_reserved_extent(struct btrfs_root *root,
Miao Xiee570fd22014-06-19 10:42:50 +08007594 u64 start, u64 len,
7595 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04007596{
Josef Bacik0f9dd462008-09-23 13:14:11 -04007597 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05007598 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007599
Josef Bacik0f9dd462008-09-23 13:14:11 -04007600 cache = btrfs_lookup_block_group(root->fs_info, start);
7601 if (!cache) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007602 btrfs_err(root->fs_info, "Unable to find block group for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007603 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007604 return -ENOSPC;
7605 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05007606
Chris Masone688b7252011-10-31 20:52:39 -04007607 if (pin)
7608 pin_down_extent(root, cache, start, len, 1);
7609 else {
Filipe Mananadcc82f42015-03-23 14:07:40 +00007610 if (btrfs_test_opt(root, DISCARD))
7611 ret = btrfs_discard_extent(root, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04007612 btrfs_add_free_space(cache, start, len);
Miao Xiee570fd22014-06-19 10:42:50 +08007613 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04007614 }
Dongsheng Yang31193212014-12-12 16:44:35 +08007615
Chris Masonfa9c0d792009-04-03 09:47:43 -04007616 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007617
liubo1abe9b82011-03-24 11:18:59 +00007618 trace_btrfs_reserved_extent_free(root, start, len);
7619
Chris Masone6dcd2d2008-07-17 12:53:50 -04007620 return ret;
7621}
7622
Chris Masone688b7252011-10-31 20:52:39 -04007623int btrfs_free_reserved_extent(struct btrfs_root *root,
Miao Xiee570fd22014-06-19 10:42:50 +08007624 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04007625{
Miao Xiee570fd22014-06-19 10:42:50 +08007626 return __btrfs_free_reserved_extent(root, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04007627}
7628
7629int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
7630 u64 start, u64 len)
7631{
Miao Xiee570fd22014-06-19 10:42:50 +08007632 return __btrfs_free_reserved_extent(root, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04007633}
7634
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007635static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
7636 struct btrfs_root *root,
7637 u64 parent, u64 root_objectid,
7638 u64 flags, u64 owner, u64 offset,
7639 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007640{
7641 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007642 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007643 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007644 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007645 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007646 struct extent_buffer *leaf;
7647 int type;
7648 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04007649
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007650 if (parent > 0)
7651 type = BTRFS_SHARED_DATA_REF_KEY;
7652 else
7653 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04007654
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007655 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05007656
7657 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007658 if (!path)
7659 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05007660
Chris Masonb9473432009-03-13 11:00:37 -04007661 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007662 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
7663 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007664 if (ret) {
7665 btrfs_free_path(path);
7666 return ret;
7667 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007668
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007669 leaf = path->nodes[0];
7670 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05007671 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007672 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
7673 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
7674 btrfs_set_extent_flags(leaf, extent_item,
7675 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05007676
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007677 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
7678 btrfs_set_extent_inline_ref_type(leaf, iref, type);
7679 if (parent > 0) {
7680 struct btrfs_shared_data_ref *ref;
7681 ref = (struct btrfs_shared_data_ref *)(iref + 1);
7682 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
7683 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
7684 } else {
7685 struct btrfs_extent_data_ref *ref;
7686 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
7687 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
7688 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
7689 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
7690 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
7691 }
Chris Mason47e4bb92008-02-01 14:51:59 -05007692
7693 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05007694 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04007695
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007696 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
7697 ins->offset);
7698 if (ret)
7699 return ret;
7700
Josef Bacikce93ec52014-11-17 15:45:48 -05007701 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007702 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007703 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007704 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05007705 BUG();
7706 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04007707 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007708 return ret;
7709}
7710
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007711static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
7712 struct btrfs_root *root,
7713 u64 parent, u64 root_objectid,
7714 u64 flags, struct btrfs_disk_key *key,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01007715 int level, struct btrfs_key *ins)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007716{
7717 int ret;
7718 struct btrfs_fs_info *fs_info = root->fs_info;
7719 struct btrfs_extent_item *extent_item;
7720 struct btrfs_tree_block_info *block_info;
7721 struct btrfs_extent_inline_ref *iref;
7722 struct btrfs_path *path;
7723 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05007724 u32 size = sizeof(*extent_item) + sizeof(*iref);
Josef Bacikfcebe452014-05-13 17:30:47 -07007725 u64 num_bytes = ins->offset;
Josef Bacik3173a182013-03-07 14:22:04 -05007726 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
7727 SKINNY_METADATA);
7728
7729 if (!skinny_metadata)
7730 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007731
7732 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04007733 if (!path) {
7734 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
David Sterba707e8a02014-06-04 19:22:26 +02007735 root->nodesize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07007736 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04007737 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007738
7739 path->leave_spinning = 1;
7740 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
7741 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007742 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07007743 btrfs_free_path(path);
Josef Bacik857cc2f2013-10-07 15:21:08 -04007744 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
David Sterba707e8a02014-06-04 19:22:26 +02007745 root->nodesize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007746 return ret;
7747 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007748
7749 leaf = path->nodes[0];
7750 extent_item = btrfs_item_ptr(leaf, path->slots[0],
7751 struct btrfs_extent_item);
7752 btrfs_set_extent_refs(leaf, extent_item, 1);
7753 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
7754 btrfs_set_extent_flags(leaf, extent_item,
7755 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007756
Josef Bacik3173a182013-03-07 14:22:04 -05007757 if (skinny_metadata) {
7758 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
David Sterba707e8a02014-06-04 19:22:26 +02007759 num_bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05007760 } else {
7761 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
7762 btrfs_set_tree_block_key(leaf, block_info, key);
7763 btrfs_set_tree_block_level(leaf, block_info, level);
7764 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
7765 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007766
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007767 if (parent > 0) {
7768 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
7769 btrfs_set_extent_inline_ref_type(leaf, iref,
7770 BTRFS_SHARED_BLOCK_REF_KEY);
7771 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
7772 } else {
7773 btrfs_set_extent_inline_ref_type(leaf, iref,
7774 BTRFS_TREE_BLOCK_REF_KEY);
7775 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
7776 }
7777
7778 btrfs_mark_buffer_dirty(leaf);
7779 btrfs_free_path(path);
7780
Omar Sandoval1e144fb2015-09-29 20:50:37 -07007781 ret = remove_from_free_space_tree(trans, fs_info, ins->objectid,
7782 num_bytes);
7783 if (ret)
7784 return ret;
7785
Josef Bacikce93ec52014-11-17 15:45:48 -05007786 ret = update_block_group(trans, root, ins->objectid, root->nodesize,
7787 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007788 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007789 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007790 ins->objectid, ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007791 BUG();
7792 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04007793
David Sterba707e8a02014-06-04 19:22:26 +02007794 trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007795 return ret;
7796}
7797
7798int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
7799 struct btrfs_root *root,
7800 u64 root_objectid, u64 owner,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08007801 u64 offset, u64 ram_bytes,
7802 struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007803{
7804 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04007805
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007806 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04007807
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007808 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
7809 ins->offset, 0,
7810 root_objectid, owner, offset,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08007811 ram_bytes, BTRFS_ADD_DELAYED_EXTENT,
7812 NULL);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007813 return ret;
7814}
Chris Masone02119d2008-09-05 16:13:11 -04007815
7816/*
7817 * this is used by the tree logging recovery code. It records that
7818 * an extent has been allocated and makes sure to clear the free
7819 * space cache bits as well
7820 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007821int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
7822 struct btrfs_root *root,
7823 u64 root_objectid, u64 owner, u64 offset,
7824 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04007825{
7826 int ret;
7827 struct btrfs_block_group_cache *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04007828
7829 /*
7830 * Mixed block groups will exclude before processing the log so we only
7831 * need to do the exlude dance if this fs isn't mixed.
7832 */
7833 if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
7834 ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
7835 if (ret)
7836 return ret;
7837 }
Chris Masone02119d2008-09-05 16:13:11 -04007838
Chris Masone02119d2008-09-05 16:13:11 -04007839 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04007840 if (!block_group)
7841 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04007842
Josef Bacikfb25e912011-07-26 17:00:46 -04007843 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
Miao Xiee570fd22014-06-19 10:42:50 +08007844 RESERVE_ALLOC_NO_ACCOUNT, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007845 BUG_ON(ret); /* logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007846 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
7847 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04007848 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04007849 return ret;
7850}
7851
Eric Sandeen48a3b632013-04-25 20:41:01 +00007852static struct extent_buffer *
7853btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
David Sterbafe864572014-06-15 02:28:42 +02007854 u64 bytenr, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04007855{
7856 struct extent_buffer *buf;
7857
David Sterbaa83fffb2014-06-15 02:39:54 +02007858 buf = btrfs_find_create_tree_block(root, bytenr);
Chris Mason65b51a02008-08-01 15:11:20 -04007859 if (!buf)
7860 return ERR_PTR(-ENOMEM);
7861 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04007862 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04007863 btrfs_tree_lock(buf);
Daniel Dressler01d58472014-11-21 17:15:07 +09007864 clean_tree_block(trans, root->fs_info, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05007865 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05007866
7867 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04007868 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05007869
Chris Masond0c803c2008-09-11 16:17:57 -04007870 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01007871 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00007872 /*
7873 * we allow two log transactions at a time, use different
7874 * EXENT bit to differentiate dirty pages.
7875 */
Filipe Manana656f30d2014-09-26 12:25:56 +01007876 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00007877 set_extent_dirty(&root->dirty_log_pages, buf->start,
7878 buf->start + buf->len - 1, GFP_NOFS);
7879 else
7880 set_extent_new(&root->dirty_log_pages, buf->start,
7881 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04007882 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01007883 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04007884 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
7885 buf->start + buf->len - 1, GFP_NOFS);
7886 }
Chris Mason65b51a02008-08-01 15:11:20 -04007887 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05007888 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04007889 return buf;
7890}
7891
Yan, Zhengf0486c62010-05-16 10:46:25 -04007892static struct btrfs_block_rsv *
7893use_block_rsv(struct btrfs_trans_handle *trans,
7894 struct btrfs_root *root, u32 blocksize)
7895{
7896 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00007897 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007898 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00007899 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007900
7901 block_rsv = get_block_rsv(trans, root);
7902
Miao Xieb586b322013-05-13 13:55:10 +00007903 if (unlikely(block_rsv->size == 0))
7904 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00007905again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007906 ret = block_rsv_use_bytes(block_rsv, blocksize);
7907 if (!ret)
7908 return block_rsv;
7909
Miao Xieb586b322013-05-13 13:55:10 +00007910 if (block_rsv->failfast)
7911 return ERR_PTR(ret);
7912
Miao Xied88033d2013-05-13 13:55:12 +00007913 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
7914 global_updated = true;
7915 update_global_block_rsv(root->fs_info);
7916 goto again;
7917 }
7918
Miao Xieb586b322013-05-13 13:55:10 +00007919 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
7920 static DEFINE_RATELIMIT_STATE(_rs,
7921 DEFAULT_RATELIMIT_INTERVAL * 10,
7922 /*DEFAULT_RATELIMIT_BURST*/ 1);
7923 if (__ratelimit(&_rs))
7924 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05007925 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00007926 }
7927try_reserve:
7928 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
7929 BTRFS_RESERVE_NO_FLUSH);
7930 if (!ret)
7931 return block_rsv;
7932 /*
7933 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00007934 * the global reserve if its space type is the same as the global
7935 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00007936 */
Miao Xie5881cfc2013-05-13 13:55:11 +00007937 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
7938 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00007939 ret = block_rsv_use_bytes(global_rsv, blocksize);
7940 if (!ret)
7941 return global_rsv;
7942 }
7943 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007944}
7945
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007946static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
7947 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007948{
7949 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007950 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007951}
7952
Chris Masonfec577f2007-02-26 10:40:21 -05007953/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007954 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08007955 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05007956 */
David Sterba4d75f8a2014-06-15 01:54:12 +02007957struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
7958 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007959 u64 parent, u64 root_objectid,
7960 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02007961 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05007962{
Chris Masone2fa7222007-03-12 16:22:34 -04007963 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007964 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04007965 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08007966 struct btrfs_delayed_extent_op *extent_op;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007967 u64 flags = 0;
7968 int ret;
David Sterba4d75f8a2014-06-15 01:54:12 +02007969 u32 blocksize = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05007970 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
7971 SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007972
David Sterbafccb84c2014-09-29 23:53:21 +02007973 if (btrfs_test_is_dummy_root(root)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007974 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
David Sterbafe864572014-06-15 02:28:42 +02007975 level);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007976 if (!IS_ERR(buf))
7977 root->alloc_bytenr += blocksize;
7978 return buf;
7979 }
David Sterbafccb84c2014-09-29 23:53:21 +02007980
Yan, Zhengf0486c62010-05-16 10:46:25 -04007981 block_rsv = use_block_rsv(trans, root, blocksize);
7982 if (IS_ERR(block_rsv))
7983 return ERR_CAST(block_rsv);
7984
Josef Bacik00361582013-08-14 14:02:47 -04007985 ret = btrfs_reserve_extent(root, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08007986 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08007987 if (ret)
7988 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05007989
David Sterbafe864572014-06-15 02:28:42 +02007990 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
Omar Sandoval67b78592015-02-24 02:47:04 -08007991 if (IS_ERR(buf)) {
7992 ret = PTR_ERR(buf);
7993 goto out_free_reserved;
7994 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007995
7996 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
7997 if (parent == 0)
7998 parent = ins.objectid;
7999 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8000 } else
8001 BUG_ON(parent > 0);
8002
8003 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00008004 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08008005 if (!extent_op) {
8006 ret = -ENOMEM;
8007 goto out_free_buf;
8008 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008009 if (key)
8010 memcpy(&extent_op->key, key, sizeof(extent_op->key));
8011 else
8012 memset(&extent_op->key, 0, sizeof(extent_op->key));
8013 extent_op->flags_to_set = flags;
Josef Bacik3173a182013-03-07 14:22:04 -05008014 if (skinny_metadata)
8015 extent_op->update_key = 0;
8016 else
8017 extent_op->update_key = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008018 extent_op->update_flags = 1;
8019 extent_op->is_data = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04008020 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008021
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008022 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
Omar Sandoval67b78592015-02-24 02:47:04 -08008023 ins.objectid, ins.offset,
8024 parent, root_objectid, level,
8025 BTRFS_ADD_DELAYED_EXTENT,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01008026 extent_op);
Omar Sandoval67b78592015-02-24 02:47:04 -08008027 if (ret)
8028 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008029 }
Chris Masonfec577f2007-02-26 10:40:21 -05008030 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08008031
8032out_free_delayed:
8033 btrfs_free_delayed_extent_op(extent_op);
8034out_free_buf:
8035 free_extent_buffer(buf);
8036out_free_reserved:
8037 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 0);
8038out_unuse:
8039 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
8040 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05008041}
Chris Masona28ec192007-03-06 20:08:01 -05008042
Yan Zheng2c47e6052009-06-27 21:07:35 -04008043struct walk_control {
8044 u64 refs[BTRFS_MAX_LEVEL];
8045 u64 flags[BTRFS_MAX_LEVEL];
8046 struct btrfs_key update_progress;
8047 int stage;
8048 int level;
8049 int shared_level;
8050 int update_ref;
8051 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008052 int reada_slot;
8053 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008054 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008055};
8056
8057#define DROP_REFERENCE 1
8058#define UPDATE_BACKREF 2
8059
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008060static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8061 struct btrfs_root *root,
8062 struct walk_control *wc,
8063 struct btrfs_path *path)
8064{
8065 u64 bytenr;
8066 u64 generation;
8067 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008068 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008069 u32 nritems;
8070 u32 blocksize;
8071 struct btrfs_key key;
8072 struct extent_buffer *eb;
8073 int ret;
8074 int slot;
8075 int nread = 0;
8076
8077 if (path->slots[wc->level] < wc->reada_slot) {
8078 wc->reada_count = wc->reada_count * 2 / 3;
8079 wc->reada_count = max(wc->reada_count, 2);
8080 } else {
8081 wc->reada_count = wc->reada_count * 3 / 2;
8082 wc->reada_count = min_t(int, wc->reada_count,
8083 BTRFS_NODEPTRS_PER_BLOCK(root));
8084 }
8085
8086 eb = path->nodes[wc->level];
8087 nritems = btrfs_header_nritems(eb);
David Sterba707e8a02014-06-04 19:22:26 +02008088 blocksize = root->nodesize;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008089
8090 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
8091 if (nread >= wc->reada_count)
8092 break;
8093
8094 cond_resched();
8095 bytenr = btrfs_node_blockptr(eb, slot);
8096 generation = btrfs_node_ptr_generation(eb, slot);
8097
8098 if (slot == path->slots[wc->level])
8099 goto reada;
8100
8101 if (wc->stage == UPDATE_BACKREF &&
8102 generation <= root->root_key.offset)
8103 continue;
8104
Yan, Zheng94fcca92009-10-09 09:25:16 -04008105 /* We don't lock the tree block, it's OK to be racy here */
Josef Bacik3173a182013-03-07 14:22:04 -05008106 ret = btrfs_lookup_extent_info(trans, root, bytenr,
8107 wc->level - 1, 1, &refs,
8108 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008109 /* We don't care about errors in readahead. */
8110 if (ret < 0)
8111 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008112 BUG_ON(refs == 0);
8113
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008114 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008115 if (refs == 1)
8116 goto reada;
8117
Yan, Zheng94fcca92009-10-09 09:25:16 -04008118 if (wc->level == 1 &&
8119 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8120 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008121 if (!wc->update_ref ||
8122 generation <= root->root_key.offset)
8123 continue;
8124 btrfs_node_key_to_cpu(eb, &key, slot);
8125 ret = btrfs_comp_cpu_keys(&key,
8126 &wc->update_progress);
8127 if (ret < 0)
8128 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008129 } else {
8130 if (wc->level == 1 &&
8131 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8132 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008133 }
8134reada:
David Sterbad3e46fe2014-06-15 02:04:19 +02008135 readahead_tree_block(root, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008136 nread++;
8137 }
8138 wc->reada_slot = slot;
8139}
8140
Qu Wenruo0ed47922015-04-16 16:55:08 +08008141/*
Mark Fasheh82bd1012015-11-05 14:38:00 -08008142 * These may not be seen by the usual inc/dec ref code so we have to
8143 * add them here.
Qu Wenruo0ed47922015-04-16 16:55:08 +08008144 */
Mark Fasheh82bd1012015-11-05 14:38:00 -08008145static int record_one_subtree_extent(struct btrfs_trans_handle *trans,
8146 struct btrfs_root *root, u64 bytenr,
8147 u64 num_bytes)
8148{
8149 struct btrfs_qgroup_extent_record *qrecord;
8150 struct btrfs_delayed_ref_root *delayed_refs;
8151
8152 qrecord = kmalloc(sizeof(*qrecord), GFP_NOFS);
8153 if (!qrecord)
8154 return -ENOMEM;
8155
8156 qrecord->bytenr = bytenr;
8157 qrecord->num_bytes = num_bytes;
8158 qrecord->old_roots = NULL;
8159
8160 delayed_refs = &trans->transaction->delayed_refs;
8161 spin_lock(&delayed_refs->lock);
8162 if (btrfs_qgroup_insert_dirty_extent(delayed_refs, qrecord))
8163 kfree(qrecord);
8164 spin_unlock(&delayed_refs->lock);
8165
8166 return 0;
8167}
8168
Mark Fasheh11526512014-07-17 12:39:01 -07008169static int account_leaf_items(struct btrfs_trans_handle *trans,
8170 struct btrfs_root *root,
8171 struct extent_buffer *eb)
8172{
8173 int nr = btrfs_header_nritems(eb);
Mark Fasheh82bd1012015-11-05 14:38:00 -08008174 int i, extent_type, ret;
Mark Fasheh11526512014-07-17 12:39:01 -07008175 struct btrfs_key key;
8176 struct btrfs_file_extent_item *fi;
8177 u64 bytenr, num_bytes;
8178
Mark Fasheh82bd1012015-11-05 14:38:00 -08008179 /* We can be called directly from walk_up_proc() */
8180 if (!root->fs_info->quota_enabled)
8181 return 0;
8182
Mark Fasheh11526512014-07-17 12:39:01 -07008183 for (i = 0; i < nr; i++) {
8184 btrfs_item_key_to_cpu(eb, &key, i);
8185
8186 if (key.type != BTRFS_EXTENT_DATA_KEY)
8187 continue;
8188
8189 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
8190 /* filter out non qgroup-accountable extents */
8191 extent_type = btrfs_file_extent_type(eb, fi);
8192
8193 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
8194 continue;
8195
8196 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
8197 if (!bytenr)
8198 continue;
8199
8200 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
Mark Fasheh82bd1012015-11-05 14:38:00 -08008201
8202 ret = record_one_subtree_extent(trans, root, bytenr, num_bytes);
8203 if (ret)
8204 return ret;
Mark Fasheh11526512014-07-17 12:39:01 -07008205 }
8206 return 0;
8207}
8208
8209/*
8210 * Walk up the tree from the bottom, freeing leaves and any interior
8211 * nodes which have had all slots visited. If a node (leaf or
8212 * interior) is freed, the node above it will have it's slot
8213 * incremented. The root node will never be freed.
8214 *
8215 * At the end of this function, we should have a path which has all
8216 * slots incremented to the next position for a search. If we need to
8217 * read a new node it will be NULL and the node above it will have the
8218 * correct slot selected for a later read.
8219 *
8220 * If we increment the root nodes slot counter past the number of
8221 * elements, 1 is returned to signal completion of the search.
8222 */
8223static int adjust_slots_upwards(struct btrfs_root *root,
8224 struct btrfs_path *path, int root_level)
8225{
8226 int level = 0;
8227 int nr, slot;
8228 struct extent_buffer *eb;
8229
8230 if (root_level == 0)
8231 return 1;
8232
8233 while (level <= root_level) {
8234 eb = path->nodes[level];
8235 nr = btrfs_header_nritems(eb);
8236 path->slots[level]++;
8237 slot = path->slots[level];
8238 if (slot >= nr || level == 0) {
8239 /*
8240 * Don't free the root - we will detect this
8241 * condition after our loop and return a
8242 * positive value for caller to stop walking the tree.
8243 */
8244 if (level != root_level) {
8245 btrfs_tree_unlock_rw(eb, path->locks[level]);
8246 path->locks[level] = 0;
8247
8248 free_extent_buffer(eb);
8249 path->nodes[level] = NULL;
8250 path->slots[level] = 0;
8251 }
8252 } else {
8253 /*
8254 * We have a valid slot to walk back down
8255 * from. Stop here so caller can process these
8256 * new nodes.
8257 */
8258 break;
8259 }
8260
8261 level++;
8262 }
8263
8264 eb = path->nodes[root_level];
8265 if (path->slots[root_level] >= btrfs_header_nritems(eb))
8266 return 1;
8267
8268 return 0;
8269}
8270
8271/*
8272 * root_eb is the subtree root and is locked before this function is called.
8273 */
8274static int account_shared_subtree(struct btrfs_trans_handle *trans,
8275 struct btrfs_root *root,
8276 struct extent_buffer *root_eb,
8277 u64 root_gen,
8278 int root_level)
8279{
8280 int ret = 0;
8281 int level;
8282 struct extent_buffer *eb = root_eb;
8283 struct btrfs_path *path = NULL;
8284
8285 BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL);
8286 BUG_ON(root_eb == NULL);
8287
8288 if (!root->fs_info->quota_enabled)
8289 return 0;
8290
8291 if (!extent_buffer_uptodate(root_eb)) {
8292 ret = btrfs_read_buffer(root_eb, root_gen);
8293 if (ret)
8294 goto out;
8295 }
8296
8297 if (root_level == 0) {
8298 ret = account_leaf_items(trans, root, root_eb);
8299 goto out;
8300 }
8301
8302 path = btrfs_alloc_path();
8303 if (!path)
8304 return -ENOMEM;
8305
8306 /*
8307 * Walk down the tree. Missing extent blocks are filled in as
8308 * we go. Metadata is accounted every time we read a new
8309 * extent block.
8310 *
8311 * When we reach a leaf, we account for file extent items in it,
8312 * walk back up the tree (adjusting slot pointers as we go)
8313 * and restart the search process.
8314 */
8315 extent_buffer_get(root_eb); /* For path */
8316 path->nodes[root_level] = root_eb;
8317 path->slots[root_level] = 0;
8318 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
8319walk_down:
8320 level = root_level;
8321 while (level >= 0) {
8322 if (path->nodes[level] == NULL) {
Mark Fasheh11526512014-07-17 12:39:01 -07008323 int parent_slot;
8324 u64 child_gen;
8325 u64 child_bytenr;
8326
8327 /* We need to get child blockptr/gen from
8328 * parent before we can read it. */
8329 eb = path->nodes[level + 1];
8330 parent_slot = path->slots[level + 1];
8331 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
8332 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
8333
David Sterbace86cd52014-06-15 01:07:32 +02008334 eb = read_tree_block(root, child_bytenr, child_gen);
Liu Bo64c043d2015-05-25 17:30:15 +08008335 if (IS_ERR(eb)) {
8336 ret = PTR_ERR(eb);
8337 goto out;
8338 } else if (!extent_buffer_uptodate(eb)) {
Liu Bo8635eda2015-05-25 17:30:14 +08008339 free_extent_buffer(eb);
Liu Bo64c043d2015-05-25 17:30:15 +08008340 ret = -EIO;
Mark Fasheh11526512014-07-17 12:39:01 -07008341 goto out;
8342 }
8343
8344 path->nodes[level] = eb;
8345 path->slots[level] = 0;
8346
8347 btrfs_tree_read_lock(eb);
8348 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
8349 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
Mark Fasheh82bd1012015-11-05 14:38:00 -08008350
8351 ret = record_one_subtree_extent(trans, root, child_bytenr,
8352 root->nodesize);
8353 if (ret)
8354 goto out;
Mark Fasheh11526512014-07-17 12:39:01 -07008355 }
8356
8357 if (level == 0) {
8358 ret = account_leaf_items(trans, root, path->nodes[level]);
8359 if (ret)
8360 goto out;
8361
8362 /* Nonzero return here means we completed our search */
8363 ret = adjust_slots_upwards(root, path, root_level);
8364 if (ret)
8365 break;
8366
8367 /* Restart search with new slots */
8368 goto walk_down;
8369 }
8370
8371 level--;
8372 }
8373
8374 ret = 0;
8375out:
8376 btrfs_free_path(path);
8377
8378 return ret;
8379}
8380
Chris Mason9aca1d52007-03-13 11:09:37 -04008381/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008382 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008383 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04008384 * when wc->stage == UPDATE_BACKREF, this function updates
8385 * back refs for pointers in the block.
8386 *
8387 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04008388 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008389static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8390 struct btrfs_root *root,
8391 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008392 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04008393{
8394 int level = wc->level;
8395 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04008396 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8397 int ret;
8398
8399 if (wc->stage == UPDATE_BACKREF &&
8400 btrfs_header_owner(eb) != root->root_key.objectid)
8401 return 1;
8402
8403 /*
8404 * when reference count of tree block is 1, it won't increase
8405 * again. once full backref flag is set, we never clear it.
8406 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04008407 if (lookup_info &&
8408 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
8409 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008410 BUG_ON(!path->locks[level]);
8411 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05008412 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008413 &wc->refs[level],
8414 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008415 BUG_ON(ret == -ENOMEM);
8416 if (ret)
8417 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008418 BUG_ON(wc->refs[level] == 0);
8419 }
8420
Yan Zheng2c47e6052009-06-27 21:07:35 -04008421 if (wc->stage == DROP_REFERENCE) {
8422 if (wc->refs[level] > 1)
8423 return 1;
8424
8425 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04008426 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008427 path->locks[level] = 0;
8428 }
8429 return 0;
8430 }
8431
8432 /* wc->stage == UPDATE_BACKREF */
8433 if (!(wc->flags[level] & flag)) {
8434 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07008435 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008436 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07008437 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008438 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008439 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04008440 eb->len, flag,
8441 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008442 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008443 wc->flags[level] |= flag;
8444 }
8445
8446 /*
8447 * the block is shared by multiple trees, so it's not good to
8448 * keep the tree lock
8449 */
8450 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04008451 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008452 path->locks[level] = 0;
8453 }
8454 return 0;
8455}
8456
8457/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008458 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008459 *
8460 * when wc->stage == DROP_REFERENCE, this function checks
8461 * reference count of the block pointed to. if the block
8462 * is shared and we need update back refs for the subtree
8463 * rooted at the block, this function changes wc->stage to
8464 * UPDATE_BACKREF. if the block is shared and there is no
8465 * need to update back, this function drops the reference
8466 * to the block.
8467 *
8468 * NOTE: return value 1 means we should stop walking down.
8469 */
8470static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8471 struct btrfs_root *root,
8472 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008473 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008474{
8475 u64 bytenr;
8476 u64 generation;
8477 u64 parent;
8478 u32 blocksize;
8479 struct btrfs_key key;
8480 struct extent_buffer *next;
8481 int level = wc->level;
8482 int reada = 0;
8483 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07008484 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008485
8486 generation = btrfs_node_ptr_generation(path->nodes[level],
8487 path->slots[level]);
8488 /*
8489 * if the lower level block was created before the snapshot
8490 * was created, we know there is no need to update back refs
8491 * for the subtree
8492 */
8493 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04008494 generation <= root->root_key.offset) {
8495 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008496 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008497 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008498
8499 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
David Sterba707e8a02014-06-04 19:22:26 +02008500 blocksize = root->nodesize;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008501
Daniel Dressler01d58472014-11-21 17:15:07 +09008502 next = btrfs_find_tree_block(root->fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008503 if (!next) {
David Sterbaa83fffb2014-06-15 02:39:54 +02008504 next = btrfs_find_create_tree_block(root, bytenr);
Miao Xie90d2c51d2010-03-25 12:37:12 +00008505 if (!next)
8506 return -ENOMEM;
Josef Bacikb2aaaa32013-07-05 17:05:38 -04008507 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
8508 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008509 reada = 1;
8510 }
8511 btrfs_tree_lock(next);
8512 btrfs_set_lock_blocking(next);
8513
Josef Bacik3173a182013-03-07 14:22:04 -05008514 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008515 &wc->refs[level - 1],
8516 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008517 if (ret < 0) {
8518 btrfs_tree_unlock(next);
8519 return ret;
8520 }
8521
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008522 if (unlikely(wc->refs[level - 1] == 0)) {
8523 btrfs_err(root->fs_info, "Missing references.");
8524 BUG();
8525 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008526 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008527
Yan, Zheng94fcca92009-10-09 09:25:16 -04008528 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008529 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07008530 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008531 if (level == 1 &&
8532 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8533 goto skip;
8534
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008535 if (!wc->update_ref ||
8536 generation <= root->root_key.offset)
8537 goto skip;
8538
8539 btrfs_node_key_to_cpu(path->nodes[level], &key,
8540 path->slots[level]);
8541 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
8542 if (ret < 0)
8543 goto skip;
8544
8545 wc->stage = UPDATE_BACKREF;
8546 wc->shared_level = level - 1;
8547 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008548 } else {
8549 if (level == 1 &&
8550 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8551 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008552 }
8553
Chris Masonb9fab912012-05-06 07:23:47 -04008554 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008555 btrfs_tree_unlock(next);
8556 free_extent_buffer(next);
8557 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008558 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008559 }
8560
8561 if (!next) {
8562 if (reada && level == 1)
8563 reada_walk_down(trans, root, wc, path);
David Sterbace86cd52014-06-15 01:07:32 +02008564 next = read_tree_block(root, bytenr, generation);
Liu Bo64c043d2015-05-25 17:30:15 +08008565 if (IS_ERR(next)) {
8566 return PTR_ERR(next);
8567 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04008568 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008569 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04008570 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008571 btrfs_tree_lock(next);
8572 btrfs_set_lock_blocking(next);
8573 }
8574
8575 level--;
8576 BUG_ON(level != btrfs_header_level(next));
8577 path->nodes[level] = next;
8578 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008579 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008580 wc->level = level;
8581 if (wc->level == 1)
8582 wc->reada_slot = 0;
8583 return 0;
8584skip:
8585 wc->refs[level - 1] = 0;
8586 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008587 if (wc->stage == DROP_REFERENCE) {
8588 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
8589 parent = path->nodes[level]->start;
8590 } else {
8591 BUG_ON(root->root_key.objectid !=
8592 btrfs_header_owner(path->nodes[level]));
8593 parent = 0;
8594 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008595
Mark Fasheh11526512014-07-17 12:39:01 -07008596 if (need_account) {
8597 ret = account_shared_subtree(trans, root, next,
8598 generation, level - 1);
8599 if (ret) {
David Sterba94647322015-10-08 11:01:36 +02008600 btrfs_err_rl(root->fs_info,
8601 "Error "
Mark Fasheh11526512014-07-17 12:39:01 -07008602 "%d accounting shared subtree. Quota "
David Sterba94647322015-10-08 11:01:36 +02008603 "is out of sync, rescan required.",
8604 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008605 }
8606 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008607 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01008608 root->root_key.objectid, level - 1, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008609 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008610 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008611 btrfs_tree_unlock(next);
8612 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04008613 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008614 return 1;
8615}
8616
8617/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008618 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008619 *
8620 * when wc->stage == DROP_REFERENCE, this function drops
8621 * reference count on the block.
8622 *
8623 * when wc->stage == UPDATE_BACKREF, this function changes
8624 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8625 * to UPDATE_BACKREF previously while processing the block.
8626 *
8627 * NOTE: return value 1 means we should stop walking up.
8628 */
8629static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8630 struct btrfs_root *root,
8631 struct btrfs_path *path,
8632 struct walk_control *wc)
8633{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008634 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008635 int level = wc->level;
8636 struct extent_buffer *eb = path->nodes[level];
8637 u64 parent = 0;
8638
8639 if (wc->stage == UPDATE_BACKREF) {
8640 BUG_ON(wc->shared_level < level);
8641 if (level < wc->shared_level)
8642 goto out;
8643
Yan Zheng2c47e6052009-06-27 21:07:35 -04008644 ret = find_next_key(path, level + 1, &wc->update_progress);
8645 if (ret > 0)
8646 wc->update_ref = 0;
8647
8648 wc->stage = DROP_REFERENCE;
8649 wc->shared_level = -1;
8650 path->slots[level] = 0;
8651
8652 /*
8653 * check reference count again if the block isn't locked.
8654 * we should start walking down the tree again if reference
8655 * count is one.
8656 */
8657 if (!path->locks[level]) {
8658 BUG_ON(level == 0);
8659 btrfs_tree_lock(eb);
8660 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008661 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008662
8663 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05008664 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008665 &wc->refs[level],
8666 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008667 if (ret < 0) {
8668 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008669 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008670 return ret;
8671 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008672 BUG_ON(wc->refs[level] == 0);
8673 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04008674 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008675 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008676 return 1;
8677 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008678 }
8679 }
8680
8681 /* wc->stage == DROP_REFERENCE */
8682 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
8683
8684 if (wc->refs[level] == 1) {
8685 if (level == 0) {
8686 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07008687 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008688 else
Josef Bacike339a6b2014-07-02 10:54:25 -07008689 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008690 BUG_ON(ret); /* -ENOMEM */
Mark Fasheh11526512014-07-17 12:39:01 -07008691 ret = account_leaf_items(trans, root, eb);
8692 if (ret) {
David Sterba94647322015-10-08 11:01:36 +02008693 btrfs_err_rl(root->fs_info,
8694 "error "
Mark Fasheh11526512014-07-17 12:39:01 -07008695 "%d accounting leaf items. Quota "
David Sterba94647322015-10-08 11:01:36 +02008696 "is out of sync, rescan required.",
8697 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008698 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008699 }
8700 /* make block locked assertion in clean_tree_block happy */
8701 if (!path->locks[level] &&
8702 btrfs_header_generation(eb) == trans->transid) {
8703 btrfs_tree_lock(eb);
8704 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008705 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008706 }
Daniel Dressler01d58472014-11-21 17:15:07 +09008707 clean_tree_block(trans, root->fs_info, eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008708 }
8709
8710 if (eb == root->node) {
8711 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8712 parent = eb->start;
8713 else
8714 BUG_ON(root->root_key.objectid !=
8715 btrfs_header_owner(eb));
8716 } else {
8717 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8718 parent = path->nodes[level + 1]->start;
8719 else
8720 BUG_ON(root->root_key.objectid !=
8721 btrfs_header_owner(path->nodes[level + 1]));
8722 }
8723
Jan Schmidt5581a512012-05-16 17:04:52 +02008724 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008725out:
8726 wc->refs[level] = 0;
8727 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008728 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008729}
8730
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008731static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
8732 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008733 struct btrfs_path *path,
8734 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008735{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008736 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008737 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008738 int ret;
8739
Yan Zheng2c47e6052009-06-27 21:07:35 -04008740 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04008741 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008742 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008743 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008744
Yan Zheng2c47e6052009-06-27 21:07:35 -04008745 if (level == 0)
8746 break;
8747
Yan, Zheng7a7965f2010-02-01 02:41:17 +00008748 if (path->slots[level] >=
8749 btrfs_header_nritems(path->nodes[level]))
8750 break;
8751
Yan, Zheng94fcca92009-10-09 09:25:16 -04008752 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008753 if (ret > 0) {
8754 path->slots[level]++;
8755 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00008756 } else if (ret < 0)
8757 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008758 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008759 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04008760 return 0;
8761}
8762
Chris Masond3977122009-01-05 21:25:51 -05008763static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05008764 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04008765 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008766 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05008767{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008768 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05008769 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04008770
Yan Zheng2c47e6052009-06-27 21:07:35 -04008771 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
8772 while (level < max_level && path->nodes[level]) {
8773 wc->level = level;
8774 if (path->slots[level] + 1 <
8775 btrfs_header_nritems(path->nodes[level])) {
8776 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05008777 return 0;
8778 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008779 ret = walk_up_proc(trans, root, path, wc);
8780 if (ret > 0)
8781 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05008782
Yan Zheng2c47e6052009-06-27 21:07:35 -04008783 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04008784 btrfs_tree_unlock_rw(path->nodes[level],
8785 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008786 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008787 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008788 free_extent_buffer(path->nodes[level]);
8789 path->nodes[level] = NULL;
8790 level++;
Chris Mason20524f02007-03-10 06:35:47 -05008791 }
8792 }
8793 return 1;
8794}
8795
Chris Mason9aca1d52007-03-13 11:09:37 -04008796/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04008797 * drop a subvolume tree.
8798 *
8799 * this function traverses the tree freeing any blocks that only
8800 * referenced by the tree.
8801 *
8802 * when a shared tree block is found. this function decreases its
8803 * reference count by one. if update_ref is true, this function
8804 * also make sure backrefs for the shared block and all lower level
8805 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00008806 *
8807 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04008808 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04008809int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008810 struct btrfs_block_rsv *block_rsv, int update_ref,
8811 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05008812{
Chris Mason5caf2a02007-04-02 11:20:42 -04008813 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008814 struct btrfs_trans_handle *trans;
8815 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04008816 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008817 struct walk_control *wc;
8818 struct btrfs_key key;
8819 int err = 0;
8820 int ret;
8821 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04008822 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05008823
Mark Fasheh11526512014-07-17 12:39:01 -07008824 btrfs_debug(root->fs_info, "Drop subvolume %llu", root->objectid);
8825
Chris Mason5caf2a02007-04-02 11:20:42 -04008826 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00008827 if (!path) {
8828 err = -ENOMEM;
8829 goto out;
8830 }
Chris Mason20524f02007-03-10 06:35:47 -05008831
Yan Zheng2c47e6052009-06-27 21:07:35 -04008832 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07008833 if (!wc) {
8834 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00008835 err = -ENOMEM;
8836 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07008837 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008838
Yan, Zhenga22285a2010-05-16 10:48:46 -04008839 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008840 if (IS_ERR(trans)) {
8841 err = PTR_ERR(trans);
8842 goto out_free;
8843 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00008844
Yan, Zheng3fd0a552010-05-16 10:49:59 -04008845 if (block_rsv)
8846 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008847
Chris Mason9f3a7422007-08-07 15:52:19 -04008848 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008849 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008850 path->nodes[level] = btrfs_lock_root_node(root);
8851 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04008852 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008853 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008854 memset(&wc->update_progress, 0,
8855 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04008856 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04008857 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008858 memcpy(&wc->update_progress, &key,
8859 sizeof(wc->update_progress));
8860
Chris Mason6702ed42007-08-07 16:15:09 -04008861 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008862 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04008863 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008864 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
8865 path->lowest_level = 0;
8866 if (ret < 0) {
8867 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008868 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04008869 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008870 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008871
Chris Mason7d9eb122008-07-08 14:19:17 -04008872 /*
8873 * unlock our path, this is safe because only this
8874 * function is allowed to delete this snapshot
8875 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008876 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04008877
Yan Zheng2c47e6052009-06-27 21:07:35 -04008878 level = btrfs_header_level(root->node);
8879 while (1) {
8880 btrfs_tree_lock(path->nodes[level]);
8881 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04008882 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008883
8884 ret = btrfs_lookup_extent_info(trans, root,
8885 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05008886 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04008887 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008888 if (ret < 0) {
8889 err = ret;
8890 goto out_end_trans;
8891 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008892 BUG_ON(wc->refs[level] == 0);
8893
8894 if (level == root_item->drop_level)
8895 break;
8896
8897 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04008898 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008899 WARN_ON(wc->refs[level] != 1);
8900 level--;
8901 }
8902 }
8903
8904 wc->level = level;
8905 wc->shared_level = -1;
8906 wc->stage = DROP_REFERENCE;
8907 wc->update_ref = update_ref;
8908 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008909 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008910 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008911
8912 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00008913
Yan Zheng2c47e6052009-06-27 21:07:35 -04008914 ret = walk_down_tree(trans, root, path, wc);
8915 if (ret < 0) {
8916 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04008917 break;
8918 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008919
8920 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
8921 if (ret < 0) {
8922 err = ret;
8923 break;
8924 }
8925
8926 if (ret > 0) {
8927 BUG_ON(wc->stage != DROP_REFERENCE);
8928 break;
8929 }
8930
8931 if (wc->stage == DROP_REFERENCE) {
8932 level = wc->level;
8933 btrfs_node_key(path->nodes[level],
8934 &root_item->drop_progress,
8935 path->slots[level]);
8936 root_item->drop_level = level;
8937 }
8938
8939 BUG_ON(wc->level == 0);
Josef Bacik3c8f2422013-07-15 11:57:06 -04008940 if (btrfs_should_end_transaction(trans, tree_root) ||
8941 (!for_reloc && btrfs_need_cleaner_sleep(root))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008942 ret = btrfs_update_root(trans, tree_root,
8943 &root->root_key,
8944 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008945 if (ret) {
8946 btrfs_abort_transaction(trans, tree_root, ret);
8947 err = ret;
8948 goto out_end_trans;
8949 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008950
Yan, Zheng3fd0a552010-05-16 10:49:59 -04008951 btrfs_end_transaction_throttle(trans, tree_root);
Josef Bacik3c8f2422013-07-15 11:57:06 -04008952 if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05008953 pr_debug("BTRFS: drop snapshot early exit\n");
Josef Bacik3c8f2422013-07-15 11:57:06 -04008954 err = -EAGAIN;
8955 goto out_free;
8956 }
8957
Yan, Zhenga22285a2010-05-16 10:48:46 -04008958 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008959 if (IS_ERR(trans)) {
8960 err = PTR_ERR(trans);
8961 goto out_free;
8962 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04008963 if (block_rsv)
8964 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04008965 }
Chris Mason20524f02007-03-10 06:35:47 -05008966 }
David Sterbab3b4aa72011-04-21 01:20:15 +02008967 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008968 if (err)
8969 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008970
8971 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008972 if (ret) {
8973 btrfs_abort_transaction(trans, tree_root, ret);
8974 goto out_end_trans;
8975 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008976
Yan, Zheng76dda932009-09-21 16:00:26 -04008977 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00008978 ret = btrfs_find_root(tree_root, &root->root_key, path,
8979 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008980 if (ret < 0) {
8981 btrfs_abort_transaction(trans, tree_root, ret);
8982 err = ret;
8983 goto out_end_trans;
8984 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05008985 /* if we fail to delete the orphan item this time
8986 * around, it'll get picked up the next time.
8987 *
8988 * The most common failure here is just -ENOENT.
8989 */
8990 btrfs_del_orphan_item(trans, tree_root,
8991 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04008992 }
8993 }
8994
Miao Xie27cdeb72014-04-02 19:51:05 +08008995 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04008996 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04008997 } else {
8998 free_extent_buffer(root->node);
8999 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00009000 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04009001 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04009002 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009003out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04009004 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009005out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04009006 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04009007 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00009008out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04009009 /*
9010 * So if we need to stop dropping the snapshot for whatever reason we
9011 * need to make sure to add it back to the dead root list so that we
9012 * keep trying to do the work later. This also cleans up roots if we
9013 * don't have it in the radix (like when we recover after a power fail
9014 * or unmount) so we don't leak memory.
9015 */
Josef Bacikb37b39c2013-07-23 16:57:15 -04009016 if (!for_reloc && root_dropped == false)
Josef Bacikd29a9f62013-07-17 19:30:20 -04009017 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08009018 if (err && err != -EAGAIN)
Anand Jaina4553fe2015-09-25 14:43:01 +08009019 btrfs_std_error(root->fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04009020 return err;
Chris Mason20524f02007-03-10 06:35:47 -05009021}
Chris Mason9078a3e2007-04-26 16:46:15 -04009022
Yan Zheng2c47e6052009-06-27 21:07:35 -04009023/*
9024 * drop subtree rooted at tree block 'node'.
9025 *
9026 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009027 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04009028 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04009029int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
9030 struct btrfs_root *root,
9031 struct extent_buffer *node,
9032 struct extent_buffer *parent)
9033{
9034 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009035 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009036 int level;
9037 int parent_level;
9038 int ret = 0;
9039 int wret;
9040
Yan Zheng2c47e6052009-06-27 21:07:35 -04009041 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
9042
Yan Zhengf82d02d2008-10-29 14:49:05 -04009043 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009044 if (!path)
9045 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009046
Yan Zheng2c47e6052009-06-27 21:07:35 -04009047 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00009048 if (!wc) {
9049 btrfs_free_path(path);
9050 return -ENOMEM;
9051 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04009052
Chris Masonb9447ef82009-03-09 11:45:38 -04009053 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009054 parent_level = btrfs_header_level(parent);
9055 extent_buffer_get(parent);
9056 path->nodes[parent_level] = parent;
9057 path->slots[parent_level] = btrfs_header_nritems(parent);
9058
Chris Masonb9447ef82009-03-09 11:45:38 -04009059 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009060 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009061 path->nodes[level] = node;
9062 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04009063 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009064
9065 wc->refs[parent_level] = 1;
9066 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
9067 wc->level = level;
9068 wc->shared_level = -1;
9069 wc->stage = DROP_REFERENCE;
9070 wc->update_ref = 0;
9071 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02009072 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04009073 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009074
9075 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04009076 wret = walk_down_tree(trans, root, path, wc);
9077 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04009078 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04009079 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04009080 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04009081
Yan Zheng2c47e6052009-06-27 21:07:35 -04009082 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009083 if (wret < 0)
9084 ret = wret;
9085 if (wret != 0)
9086 break;
9087 }
9088
Yan Zheng2c47e6052009-06-27 21:07:35 -04009089 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04009090 btrfs_free_path(path);
9091 return ret;
9092}
9093
Chris Masonec44a352008-04-28 15:29:52 -04009094static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
9095{
9096 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009097 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04009098
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009099 /*
9100 * if restripe for this chunk_type is on pick target profile and
9101 * return, otherwise do the usual balance
9102 */
9103 stripped = get_restripe_target(root->fs_info, flags);
9104 if (stripped)
9105 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02009106
Miao Xie95669972014-07-24 11:37:14 +08009107 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05009108
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009109 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05009110 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03009111 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
9112
Chris Masonec44a352008-04-28 15:29:52 -04009113 if (num_devices == 1) {
9114 stripped |= BTRFS_BLOCK_GROUP_DUP;
9115 stripped = flags & ~stripped;
9116
9117 /* turn raid0 into single device chunks */
9118 if (flags & BTRFS_BLOCK_GROUP_RAID0)
9119 return stripped;
9120
9121 /* turn mirroring into duplication */
9122 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
9123 BTRFS_BLOCK_GROUP_RAID10))
9124 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04009125 } else {
9126 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04009127 if (flags & stripped)
9128 return flags;
9129
9130 stripped |= BTRFS_BLOCK_GROUP_DUP;
9131 stripped = flags & ~stripped;
9132
9133 /* switch duplicated blocks with raid1 */
9134 if (flags & BTRFS_BLOCK_GROUP_DUP)
9135 return stripped | BTRFS_BLOCK_GROUP_RAID1;
9136
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009137 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04009138 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009139
Chris Masonec44a352008-04-28 15:29:52 -04009140 return flags;
9141}
9142
Zhaolei868f4012015-08-05 16:43:27 +08009143static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04009144{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009145 struct btrfs_space_info *sinfo = cache->space_info;
9146 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00009147 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009148 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04009149
Miao Xie199c36e2011-07-15 10:34:36 +00009150 /*
9151 * We need some metadata space and system metadata space for
9152 * allocating chunks in some corner cases until we force to set
9153 * it to be readonly.
9154 */
9155 if ((sinfo->flags &
9156 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
9157 !force)
9158 min_allocable_bytes = 1 * 1024 * 1024;
9159 else
9160 min_allocable_bytes = 0;
9161
Yan, Zhengf0486c62010-05-16 10:46:25 -04009162 spin_lock(&sinfo->lock);
9163 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00009164
9165 if (cache->ro) {
Zhaolei868f4012015-08-05 16:43:27 +08009166 cache->ro++;
WuBo61cfea92011-07-26 03:30:11 +00009167 ret = 0;
9168 goto out;
9169 }
9170
Yan, Zhengf0486c62010-05-16 10:46:25 -04009171 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
9172 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04009173
Yan, Zhengf0486c62010-05-16 10:46:25 -04009174 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04009175 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
9176 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04009177 sinfo->bytes_readonly += num_bytes;
Zhaolei868f4012015-08-05 16:43:27 +08009178 cache->ro++;
Josef Bacik633c0aa2014-10-31 09:49:34 -04009179 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009180 ret = 0;
9181 }
WuBo61cfea92011-07-26 03:30:11 +00009182out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04009183 spin_unlock(&cache->lock);
9184 spin_unlock(&sinfo->lock);
9185 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04009186}
9187
Zhaolei868f4012015-08-05 16:43:27 +08009188int btrfs_inc_block_group_ro(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04009189 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009190
9191{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009192 struct btrfs_trans_handle *trans;
9193 u64 alloc_flags;
9194 int ret;
9195
Chris Mason1bbc6212015-04-06 12:46:08 -07009196again:
Josef Bacik7a7eaa42011-04-13 12:54:33 -04009197 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009198 if (IS_ERR(trans))
9199 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009200
Chris Mason1bbc6212015-04-06 12:46:08 -07009201 /*
9202 * we're not allowed to set block groups readonly after the dirty
9203 * block groups cache has started writing. If it already started,
9204 * back off and let this transaction commit
9205 */
9206 mutex_lock(&root->fs_info->ro_block_group_mutex);
Josef Bacik3204d332015-09-24 10:46:10 -04009207 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
Chris Mason1bbc6212015-04-06 12:46:08 -07009208 u64 transid = trans->transid;
9209
9210 mutex_unlock(&root->fs_info->ro_block_group_mutex);
9211 btrfs_end_transaction(trans, root);
9212
9213 ret = btrfs_wait_for_commit(root, transid);
9214 if (ret)
9215 return ret;
9216 goto again;
9217 }
9218
Chris Mason153c35b2015-05-19 18:54:41 -07009219 /*
9220 * if we are changing raid levels, try to allocate a corresponding
9221 * block group with the new raid level.
9222 */
9223 alloc_flags = update_block_group_flags(root, cache->flags);
9224 if (alloc_flags != cache->flags) {
9225 ret = do_chunk_alloc(trans, root, alloc_flags,
9226 CHUNK_ALLOC_FORCE);
9227 /*
9228 * ENOSPC is allowed here, we may have enough space
9229 * already allocated at the new raid level to
9230 * carry on
9231 */
9232 if (ret == -ENOSPC)
9233 ret = 0;
9234 if (ret < 0)
9235 goto out;
9236 }
Chris Mason1bbc6212015-04-06 12:46:08 -07009237
Zhaolei868f4012015-08-05 16:43:27 +08009238 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009239 if (!ret)
9240 goto out;
9241 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04009242 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04009243 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009244 if (ret < 0)
9245 goto out;
Zhaolei868f4012015-08-05 16:43:27 +08009246 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009247out:
Shaohua Li2f081082015-01-09 10:40:15 -08009248 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
9249 alloc_flags = update_block_group_flags(root, cache->flags);
Filipe Mananaa9629592015-05-18 19:11:40 +01009250 lock_chunks(root->fs_info->chunk_root);
Filipe Manana4617ea32015-06-09 17:48:21 +01009251 check_system_chunk(trans, root, alloc_flags);
Filipe Mananaa9629592015-05-18 19:11:40 +01009252 unlock_chunks(root->fs_info->chunk_root);
Shaohua Li2f081082015-01-09 10:40:15 -08009253 }
Chris Mason1bbc6212015-04-06 12:46:08 -07009254 mutex_unlock(&root->fs_info->ro_block_group_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009255
Yan, Zhengf0486c62010-05-16 10:46:25 -04009256 btrfs_end_transaction(trans, root);
9257 return ret;
9258}
9259
Chris Masonc87f08c2011-02-16 13:57:04 -05009260int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
9261 struct btrfs_root *root, u64 type)
9262{
9263 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04009264 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04009265 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05009266}
9267
Miao Xie6d07bce2011-01-05 10:07:31 +00009268/*
9269 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04009270 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00009271 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009272u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00009273{
9274 struct btrfs_block_group_cache *block_group;
9275 u64 free_bytes = 0;
9276 int factor;
9277
Josef Bacik633c0aa2014-10-31 09:49:34 -04009278 /* It's df, we don't care if it's racey */
9279 if (list_empty(&sinfo->ro_bgs))
9280 return 0;
9281
9282 spin_lock(&sinfo->lock);
9283 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00009284 spin_lock(&block_group->lock);
9285
9286 if (!block_group->ro) {
9287 spin_unlock(&block_group->lock);
9288 continue;
9289 }
9290
9291 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
9292 BTRFS_BLOCK_GROUP_RAID10 |
9293 BTRFS_BLOCK_GROUP_DUP))
9294 factor = 2;
9295 else
9296 factor = 1;
9297
9298 free_bytes += (block_group->key.offset -
9299 btrfs_block_group_used(&block_group->item)) *
9300 factor;
9301
9302 spin_unlock(&block_group->lock);
9303 }
Miao Xie6d07bce2011-01-05 10:07:31 +00009304 spin_unlock(&sinfo->lock);
9305
9306 return free_bytes;
9307}
9308
Zhaolei868f4012015-08-05 16:43:27 +08009309void btrfs_dec_block_group_ro(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04009310 struct btrfs_block_group_cache *cache)
9311{
9312 struct btrfs_space_info *sinfo = cache->space_info;
9313 u64 num_bytes;
9314
9315 BUG_ON(!cache->ro);
9316
9317 spin_lock(&sinfo->lock);
9318 spin_lock(&cache->lock);
Zhaolei868f4012015-08-05 16:43:27 +08009319 if (!--cache->ro) {
9320 num_bytes = cache->key.offset - cache->reserved -
9321 cache->pinned - cache->bytes_super -
9322 btrfs_block_group_used(&cache->item);
9323 sinfo->bytes_readonly -= num_bytes;
9324 list_del_init(&cache->ro_list);
9325 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009326 spin_unlock(&cache->lock);
9327 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009328}
9329
Josef Bacikba1bf482009-09-11 16:11:19 -04009330/*
9331 * checks to see if its even possible to relocate this block group.
9332 *
9333 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9334 * ok to go ahead and try.
9335 */
9336int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04009337{
Zheng Yan1a40e232008-09-26 10:09:34 -04009338 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04009339 struct btrfs_space_info *space_info;
9340 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
9341 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04009342 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00009343 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04009344 u64 dev_min = 1;
9345 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009346 u64 target;
liubocdcb7252011-08-03 10:15:25 +00009347 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04009348 int full = 0;
9349 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05009350
Josef Bacikba1bf482009-09-11 16:11:19 -04009351 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04009352
Josef Bacikba1bf482009-09-11 16:11:19 -04009353 /* odd, couldn't find the block group, leave it alone */
9354 if (!block_group)
9355 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05009356
liubocdcb7252011-08-03 10:15:25 +00009357 min_free = btrfs_block_group_used(&block_group->item);
9358
Josef Bacikba1bf482009-09-11 16:11:19 -04009359 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00009360 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04009361 goto out;
Chris Mason323da792008-05-09 11:46:48 -04009362
Josef Bacikba1bf482009-09-11 16:11:19 -04009363 space_info = block_group->space_info;
9364 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04009365
Josef Bacikba1bf482009-09-11 16:11:19 -04009366 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04009367
Josef Bacikba1bf482009-09-11 16:11:19 -04009368 /*
9369 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04009370 * relocate it unless we're able to allocate a new chunk below.
9371 *
9372 * Otherwise, we need to make sure we have room in the space to handle
9373 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04009374 */
Chris Mason7ce618d2009-09-22 14:48:44 -04009375 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00009376 (space_info->bytes_used + space_info->bytes_reserved +
9377 space_info->bytes_pinned + space_info->bytes_readonly +
9378 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04009379 spin_unlock(&space_info->lock);
9380 goto out;
9381 }
9382 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009383
Josef Bacikba1bf482009-09-11 16:11:19 -04009384 /*
9385 * ok we don't have enough space, but maybe we have free space on our
9386 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009387 * alloc devices and guess if we have enough space. if this block
9388 * group is going to be restriped, run checks against the target
9389 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04009390 */
9391 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05009392
liubocdcb7252011-08-03 10:15:25 +00009393 /*
9394 * index:
9395 * 0: raid10
9396 * 1: raid1
9397 * 2: dup
9398 * 3: raid0
9399 * 4: single
9400 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009401 target = get_restripe_target(root->fs_info, block_group->flags);
9402 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00009403 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009404 } else {
9405 /*
9406 * this is just a balance, so if we were marked as full
9407 * we know there is no space for a new chunk
9408 */
9409 if (full)
9410 goto out;
9411
9412 index = get_block_group_index(block_group);
9413 }
9414
Miao Xiee6ec7162013-01-17 05:38:51 +00009415 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00009416 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04009417 /* Divide by 2 */
9418 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009419 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00009420 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00009421 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04009422 /* Multiply by 2 */
9423 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009424 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00009425 dev_min = fs_devices->rw_devices;
David Sterba47c57132015-02-20 18:43:47 +01009426 min_free = div64_u64(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00009427 }
9428
Josef Bacik6df9a952013-06-27 13:22:46 -04009429 /* We need to do this so that we can look at pending chunks */
9430 trans = btrfs_join_transaction(root);
9431 if (IS_ERR(trans)) {
9432 ret = PTR_ERR(trans);
9433 goto out;
9434 }
9435
Josef Bacikba1bf482009-09-11 16:11:19 -04009436 mutex_lock(&root->fs_info->chunk_mutex);
9437 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00009438 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04009439
Josef Bacikba1bf482009-09-11 16:11:19 -04009440 /*
9441 * check to make sure we can actually find a chunk with enough
9442 * space to fit our block group in.
9443 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01009444 if (device->total_bytes > device->bytes_used + min_free &&
9445 !device->is_tgtdev_for_dev_replace) {
Josef Bacik6df9a952013-06-27 13:22:46 -04009446 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00009447 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04009448 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00009449 dev_nr++;
9450
9451 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05009452 break;
liubocdcb7252011-08-03 10:15:25 +00009453
Josef Bacikba1bf482009-09-11 16:11:19 -04009454 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05009455 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009456 }
Josef Bacikba1bf482009-09-11 16:11:19 -04009457 mutex_unlock(&root->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04009458 btrfs_end_transaction(trans, root);
Chris Masonedbd8d42007-12-21 16:27:24 -05009459out:
Josef Bacikba1bf482009-09-11 16:11:19 -04009460 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05009461 return ret;
9462}
9463
Christoph Hellwigb2950862008-12-02 09:54:17 -05009464static int find_first_block_group(struct btrfs_root *root,
9465 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04009466{
Chris Mason925baed2008-06-25 16:01:30 -04009467 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009468 struct btrfs_key found_key;
9469 struct extent_buffer *leaf;
9470 int slot;
9471
9472 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
9473 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009474 goto out;
9475
Chris Masond3977122009-01-05 21:25:51 -05009476 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009477 slot = path->slots[0];
9478 leaf = path->nodes[0];
9479 if (slot >= btrfs_header_nritems(leaf)) {
9480 ret = btrfs_next_leaf(root, path);
9481 if (ret == 0)
9482 continue;
9483 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009484 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04009485 break;
9486 }
9487 btrfs_item_key_to_cpu(leaf, &found_key, slot);
9488
9489 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04009490 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
9491 ret = 0;
9492 goto out;
9493 }
Chris Mason0b86a832008-03-24 15:01:56 -04009494 path->slots[0]++;
9495 }
Chris Mason925baed2008-06-25 16:01:30 -04009496out:
Chris Mason0b86a832008-03-24 15:01:56 -04009497 return ret;
9498}
9499
Josef Bacik0af3d002010-06-21 14:48:16 -04009500void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9501{
9502 struct btrfs_block_group_cache *block_group;
9503 u64 last = 0;
9504
9505 while (1) {
9506 struct inode *inode;
9507
9508 block_group = btrfs_lookup_first_block_group(info, last);
9509 while (block_group) {
9510 spin_lock(&block_group->lock);
9511 if (block_group->iref)
9512 break;
9513 spin_unlock(&block_group->lock);
9514 block_group = next_block_group(info->tree_root,
9515 block_group);
9516 }
9517 if (!block_group) {
9518 if (last == 0)
9519 break;
9520 last = 0;
9521 continue;
9522 }
9523
9524 inode = block_group->inode;
9525 block_group->iref = 0;
9526 block_group->inode = NULL;
9527 spin_unlock(&block_group->lock);
9528 iput(inode);
9529 last = block_group->key.objectid + block_group->key.offset;
9530 btrfs_put_block_group(block_group);
9531 }
9532}
9533
Zheng Yan1a40e232008-09-26 10:09:34 -04009534int btrfs_free_block_groups(struct btrfs_fs_info *info)
9535{
9536 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04009537 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04009538 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04009539 struct rb_node *n;
9540
Josef Bacik9e351cc2014-03-13 15:42:13 -04009541 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009542 while (!list_empty(&info->caching_block_groups)) {
9543 caching_ctl = list_entry(info->caching_block_groups.next,
9544 struct btrfs_caching_control, list);
9545 list_del(&caching_ctl->list);
9546 put_caching_control(caching_ctl);
9547 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04009548 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009549
Josef Bacik47ab2a62014-09-18 11:20:02 -04009550 spin_lock(&info->unused_bgs_lock);
9551 while (!list_empty(&info->unused_bgs)) {
9552 block_group = list_first_entry(&info->unused_bgs,
9553 struct btrfs_block_group_cache,
9554 bg_list);
9555 list_del_init(&block_group->bg_list);
9556 btrfs_put_block_group(block_group);
9557 }
9558 spin_unlock(&info->unused_bgs_lock);
9559
Zheng Yan1a40e232008-09-26 10:09:34 -04009560 spin_lock(&info->block_group_cache_lock);
9561 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
9562 block_group = rb_entry(n, struct btrfs_block_group_cache,
9563 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04009564 rb_erase(&block_group->cache_node,
9565 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009566 RB_CLEAR_NODE(&block_group->cache_node);
Yan Zhengd899e052008-10-30 14:25:28 -04009567 spin_unlock(&info->block_group_cache_lock);
9568
Josef Bacik80eb2342008-10-29 14:49:05 -04009569 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04009570 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04009571 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05009572
Josef Bacik817d52f2009-07-13 21:29:25 -04009573 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04009574 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04009575
Josef Bacik3c148742011-02-02 15:53:47 +00009576 /*
9577 * We haven't cached this block group, which means we could
9578 * possibly have excluded extents on this block group.
9579 */
Josef Bacik36cce922013-08-05 11:15:21 -04009580 if (block_group->cached == BTRFS_CACHE_NO ||
9581 block_group->cached == BTRFS_CACHE_ERROR)
Josef Bacik3c148742011-02-02 15:53:47 +00009582 free_excluded_extents(info->extent_root, block_group);
9583
Josef Bacik817d52f2009-07-13 21:29:25 -04009584 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00009585 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04009586
9587 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009588 }
9589 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04009590
9591 /* now that all the block groups are freed, go through and
9592 * free all the space_info structs. This is only called during
9593 * the final stages of unmount, and so we know nobody is
9594 * using them. We call synchronize_rcu() once before we start,
9595 * just to be on the safe side.
9596 */
9597 synchronize_rcu();
9598
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04009599 release_global_block_rsv(info);
9600
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309601 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009602 int i;
9603
Chris Mason4184ea72009-03-10 12:39:20 -04009604 space_info = list_entry(info->space_info.next,
9605 struct btrfs_space_info,
9606 list);
David Sterbab069e0c2013-02-08 21:28:17 +00009607 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309608 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00009609 space_info->bytes_reserved > 0 ||
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309610 space_info->bytes_may_use > 0)) {
David Sterbab069e0c2013-02-08 21:28:17 +00009611 dump_space_info(space_info, 0, 0);
9612 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009613 }
Chris Mason4184ea72009-03-10 12:39:20 -04009614 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009615 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
9616 struct kobject *kobj;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009617 kobj = space_info->block_group_kobjs[i];
9618 space_info->block_group_kobjs[i] = NULL;
9619 if (kobj) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009620 kobject_del(kobj);
9621 kobject_put(kobj);
9622 }
9623 }
9624 kobject_del(&space_info->kobj);
9625 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04009626 }
Zheng Yan1a40e232008-09-26 10:09:34 -04009627 return 0;
9628}
9629
Yan, Zhengb742bb822010-05-16 10:46:24 -04009630static void __link_block_group(struct btrfs_space_info *space_info,
9631 struct btrfs_block_group_cache *cache)
9632{
9633 int index = get_block_group_index(cache);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009634 bool first = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04009635
9636 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009637 if (list_empty(&space_info->block_groups[index]))
9638 first = true;
9639 list_add_tail(&cache->list, &space_info->block_groups[index]);
9640 up_write(&space_info->groups_sem);
9641
9642 if (first) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009643 struct raid_kobject *rkobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009644 int ret;
9645
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009646 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
9647 if (!rkobj)
9648 goto out_err;
9649 rkobj->raid_type = index;
9650 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
9651 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
9652 "%s", get_raid_name(index));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009653 if (ret) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009654 kobject_put(&rkobj->kobj);
9655 goto out_err;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009656 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009657 space_info->block_group_kobjs[index] = &rkobj->kobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009658 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009659
9660 return;
9661out_err:
9662 pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n");
Yan, Zhengb742bb822010-05-16 10:46:24 -04009663}
9664
Miao Xie920e4a52014-01-15 20:00:55 +08009665static struct btrfs_block_group_cache *
9666btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
9667{
9668 struct btrfs_block_group_cache *cache;
9669
9670 cache = kzalloc(sizeof(*cache), GFP_NOFS);
9671 if (!cache)
9672 return NULL;
9673
9674 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
9675 GFP_NOFS);
9676 if (!cache->free_space_ctl) {
9677 kfree(cache);
9678 return NULL;
9679 }
9680
9681 cache->key.objectid = start;
9682 cache->key.offset = size;
9683 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
9684
9685 cache->sectorsize = root->sectorsize;
9686 cache->fs_info = root->fs_info;
9687 cache->full_stripe_len = btrfs_full_stripe_len(root,
9688 &root->fs_info->mapping_tree,
9689 start);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07009690 set_free_space_tree_thresholds(cache);
9691
Miao Xie920e4a52014-01-15 20:00:55 +08009692 atomic_set(&cache->count, 1);
9693 spin_lock_init(&cache->lock);
Miao Xiee570fd22014-06-19 10:42:50 +08009694 init_rwsem(&cache->data_rwsem);
Miao Xie920e4a52014-01-15 20:00:55 +08009695 INIT_LIST_HEAD(&cache->list);
9696 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -04009697 INIT_LIST_HEAD(&cache->bg_list);
Josef Bacik633c0aa2014-10-31 09:49:34 -04009698 INIT_LIST_HEAD(&cache->ro_list);
Josef Bacikce93ec52014-11-17 15:45:48 -05009699 INIT_LIST_HEAD(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07009700 INIT_LIST_HEAD(&cache->io_list);
Miao Xie920e4a52014-01-15 20:00:55 +08009701 btrfs_init_free_space_ctl(cache);
Filipe Manana04216822014-11-27 21:14:15 +00009702 atomic_set(&cache->trimming, 0);
Omar Sandovala5ed9182015-09-29 20:50:35 -07009703 mutex_init(&cache->free_space_lock);
Miao Xie920e4a52014-01-15 20:00:55 +08009704
9705 return cache;
9706}
9707
Chris Mason9078a3e2007-04-26 16:46:15 -04009708int btrfs_read_block_groups(struct btrfs_root *root)
9709{
9710 struct btrfs_path *path;
9711 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04009712 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04009713 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04009714 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04009715 struct btrfs_key key;
9716 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04009717 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04009718 int need_clear = 0;
9719 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04009720
Chris Masonbe744172007-05-06 10:15:01 -04009721 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04009722 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009723 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009724 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Chris Mason9078a3e2007-04-26 16:46:15 -04009725 path = btrfs_alloc_path();
9726 if (!path)
9727 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04009728 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04009729
David Sterba6c417612011-04-13 15:41:04 +02009730 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04009731 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02009732 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04009733 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04009734 if (btrfs_test_opt(root, CLEAR_CACHE))
9735 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04009736
Chris Masond3977122009-01-05 21:25:51 -05009737 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009738 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb822010-05-16 10:46:24 -04009739 if (ret > 0)
9740 break;
Chris Mason0b86a832008-03-24 15:01:56 -04009741 if (ret != 0)
9742 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +08009743
Chris Mason5f39d392007-10-15 16:14:19 -04009744 leaf = path->nodes[0];
9745 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +08009746
9747 cache = btrfs_create_block_group_cache(root, found_key.objectid,
9748 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04009749 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04009750 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009751 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04009752 }
Josef Bacik96303082009-07-13 21:29:25 -04009753
Liu Bocf7c1ef2012-07-06 03:31:34 -06009754 if (need_clear) {
9755 /*
9756 * When we mount with old space cache, we need to
9757 * set BTRFS_DC_CLEAR and set dirty flag.
9758 *
9759 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
9760 * truncate the old free space cache inode and
9761 * setup a new one.
9762 * b) Setting 'dirty flag' makes sure that we flush
9763 * the new space cache info onto disk.
9764 */
Liu Bocf7c1ef2012-07-06 03:31:34 -06009765 if (btrfs_test_opt(root, SPACE_CACHE))
Josef Bacikce93ec52014-11-17 15:45:48 -05009766 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06009767 }
Josef Bacik0af3d002010-06-21 14:48:16 -04009768
Chris Mason5f39d392007-10-15 16:14:19 -04009769 read_extent_buffer(leaf, &cache->item,
9770 btrfs_item_ptr_offset(leaf, path->slots[0]),
9771 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +08009772 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason0b86a832008-03-24 15:01:56 -04009773
Chris Mason9078a3e2007-04-26 16:46:15 -04009774 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02009775 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +08009776
Josef Bacik817d52f2009-07-13 21:29:25 -04009777 /*
Josef Bacik3c148742011-02-02 15:53:47 +00009778 * We need to exclude the super stripes now so that the space
9779 * info has super bytes accounted for, otherwise we'll think
9780 * we have more space than we actually do.
9781 */
Josef Bacik835d9742013-03-19 12:13:25 -04009782 ret = exclude_super_stripes(root, cache);
9783 if (ret) {
9784 /*
9785 * We may have excluded something, so call this just in
9786 * case.
9787 */
9788 free_excluded_extents(root, cache);
Miao Xie920e4a52014-01-15 20:00:55 +08009789 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04009790 goto error;
9791 }
Josef Bacik3c148742011-02-02 15:53:47 +00009792
9793 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04009794 * check for two cases, either we are full, and therefore
9795 * don't need to bother with the caching work since we won't
9796 * find any space, or we are empty, and we can just add all
9797 * the space in and be done with it. This saves us _alot_ of
9798 * time, particularly in the full case.
9799 */
9800 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04009801 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04009802 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04009803 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04009804 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04009805 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04009806 cache->cached = BTRFS_CACHE_FINISHED;
9807 add_new_free_space(cache, root->fs_info,
9808 found_key.objectid,
9809 found_key.objectid +
9810 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04009811 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04009812 }
Chris Mason96b51792007-10-15 16:15:19 -04009813
Josef Bacik8c579fe2013-04-02 12:40:42 -04009814 ret = btrfs_add_block_group_cache(root->fs_info, cache);
9815 if (ret) {
9816 btrfs_remove_free_space_cache(cache);
9817 btrfs_put_block_group(cache);
9818 goto error;
9819 }
9820
Chris Mason6324fbf2008-03-24 15:01:59 -04009821 ret = update_space_info(info, cache->flags, found_key.offset,
9822 btrfs_block_group_used(&cache->item),
9823 &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04009824 if (ret) {
9825 btrfs_remove_free_space_cache(cache);
9826 spin_lock(&info->block_group_cache_lock);
9827 rb_erase(&cache->cache_node,
9828 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009829 RB_CLEAR_NODE(&cache->cache_node);
Josef Bacik8c579fe2013-04-02 12:40:42 -04009830 spin_unlock(&info->block_group_cache_lock);
9831 btrfs_put_block_group(cache);
9832 goto error;
9833 }
9834
Chris Mason6324fbf2008-03-24 15:01:59 -04009835 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04009836 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009837 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04009838 spin_unlock(&cache->space_info->lock);
9839
Yan, Zhengb742bb822010-05-16 10:46:24 -04009840 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04009841
Chris Mason75ccf472008-09-30 19:24:06 -04009842 set_avail_alloc_bits(root->fs_info, cache->flags);
Josef Bacik47ab2a62014-09-18 11:20:02 -04009843 if (btrfs_chunk_readonly(root, cache->key.objectid)) {
Zhaolei868f4012015-08-05 16:43:27 +08009844 inc_block_group_ro(cache, 1);
Josef Bacik47ab2a62014-09-18 11:20:02 -04009845 } else if (btrfs_block_group_used(&cache->item) == 0) {
9846 spin_lock(&info->unused_bgs_lock);
9847 /* Should always be true but just in case. */
9848 if (list_empty(&cache->bg_list)) {
9849 btrfs_get_block_group(cache);
9850 list_add_tail(&cache->bg_list,
9851 &info->unused_bgs);
9852 }
9853 spin_unlock(&info->unused_bgs_lock);
9854 }
Chris Mason9078a3e2007-04-26 16:46:15 -04009855 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04009856
9857 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
9858 if (!(get_alloc_profile(root, space_info->flags) &
9859 (BTRFS_BLOCK_GROUP_RAID10 |
9860 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05009861 BTRFS_BLOCK_GROUP_RAID5 |
9862 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb822010-05-16 10:46:24 -04009863 BTRFS_BLOCK_GROUP_DUP)))
9864 continue;
9865 /*
9866 * avoid allocating from un-mirrored block group if there are
9867 * mirrored block groups.
9868 */
chandan1095cc02013-07-16 12:28:56 +05309869 list_for_each_entry(cache,
9870 &space_info->block_groups[BTRFS_RAID_RAID0],
9871 list)
Zhaolei868f4012015-08-05 16:43:27 +08009872 inc_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +05309873 list_for_each_entry(cache,
9874 &space_info->block_groups[BTRFS_RAID_SINGLE],
9875 list)
Zhaolei868f4012015-08-05 16:43:27 +08009876 inc_block_group_ro(cache, 1);
Yan, Zhengb742bb822010-05-16 10:46:24 -04009877 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009878
9879 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04009880 ret = 0;
9881error:
Chris Mason9078a3e2007-04-26 16:46:15 -04009882 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04009883 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04009884}
Chris Mason6324fbf2008-03-24 15:01:59 -04009885
Josef Bacikea658ba2012-09-11 16:57:25 -04009886void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
9887 struct btrfs_root *root)
9888{
9889 struct btrfs_block_group_cache *block_group, *tmp;
9890 struct btrfs_root *extent_root = root->fs_info->extent_root;
9891 struct btrfs_block_group_item item;
9892 struct btrfs_key key;
9893 int ret = 0;
Filipe Mananad9a05402015-10-03 13:13:13 +01009894 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -04009895
Filipe Mananad9a05402015-10-03 13:13:13 +01009896 trans->can_flush_pending_bgs = false;
Josef Bacik47ab2a62014-09-18 11:20:02 -04009897 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
Josef Bacikea658ba2012-09-11 16:57:25 -04009898 if (ret)
Filipe Mananac92f6be2014-11-26 15:28:55 +00009899 goto next;
Josef Bacikea658ba2012-09-11 16:57:25 -04009900
9901 spin_lock(&block_group->lock);
9902 memcpy(&item, &block_group->item, sizeof(item));
9903 memcpy(&key, &block_group->key, sizeof(key));
9904 spin_unlock(&block_group->lock);
9905
9906 ret = btrfs_insert_item(trans, extent_root, &key, &item,
9907 sizeof(item));
9908 if (ret)
9909 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacik6df9a952013-06-27 13:22:46 -04009910 ret = btrfs_finish_chunk_alloc(trans, extent_root,
9911 key.objectid, key.offset);
9912 if (ret)
9913 btrfs_abort_transaction(trans, extent_root, ret);
Omar Sandoval1e144fb2015-09-29 20:50:37 -07009914 add_block_group_free_space(trans, root->fs_info, block_group);
9915 /* already aborted the transaction if it failed. */
Filipe Mananac92f6be2014-11-26 15:28:55 +00009916next:
9917 list_del_init(&block_group->bg_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04009918 }
Filipe Mananad9a05402015-10-03 13:13:13 +01009919 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -04009920}
9921
Chris Mason6324fbf2008-03-24 15:01:59 -04009922int btrfs_make_block_group(struct btrfs_trans_handle *trans,
9923 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04009924 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04009925 u64 size)
9926{
9927 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04009928 struct btrfs_root *extent_root;
9929 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04009930
9931 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04009932
Miao Xie995946d2014-04-02 19:51:06 +08009933 btrfs_set_log_full_commit(root->fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -04009934
Miao Xie920e4a52014-01-15 20:00:55 +08009935 cache = btrfs_create_block_group_cache(root, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -04009936 if (!cache)
9937 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08009938
Chris Mason6324fbf2008-03-24 15:01:59 -04009939 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04009940 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
Chris Mason6324fbf2008-03-24 15:01:59 -04009941 btrfs_set_block_group_flags(&cache->item, type);
9942
Miao Xie920e4a52014-01-15 20:00:55 +08009943 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -04009944 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04009945 cache->cached = BTRFS_CACHE_FINISHED;
Omar Sandoval1e144fb2015-09-29 20:50:37 -07009946 cache->needs_free_space = 1;
Josef Bacik835d9742013-03-19 12:13:25 -04009947 ret = exclude_super_stripes(root, cache);
9948 if (ret) {
9949 /*
9950 * We may have excluded something, so call this just in
9951 * case.
9952 */
9953 free_excluded_extents(root, cache);
Miao Xie920e4a52014-01-15 20:00:55 +08009954 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04009955 return ret;
9956 }
Josef Bacik96303082009-07-13 21:29:25 -04009957
Josef Bacik817d52f2009-07-13 21:29:25 -04009958 add_new_free_space(cache, root->fs_info, chunk_offset,
9959 chunk_offset + size);
9960
Yan Zheng11833d62009-09-11 16:11:19 -04009961 free_excluded_extents(root, cache);
9962
Josef Bacikd0bd4562015-09-23 14:54:14 -04009963#ifdef CONFIG_BTRFS_DEBUG
9964 if (btrfs_should_fragment_free_space(root, cache)) {
9965 u64 new_bytes_used = size - bytes_used;
9966
9967 bytes_used += new_bytes_used >> 1;
9968 fragment_free_space(root, cache);
9969 }
9970#endif
Filipe Manana2e6e5182015-05-12 00:28:11 +01009971 /*
9972 * Call to ensure the corresponding space_info object is created and
9973 * assigned to our block group, but don't update its counters just yet.
9974 * We want our bg to be added to the rbtree with its ->space_info set.
9975 */
9976 ret = update_space_info(root->fs_info, cache->flags, 0, 0,
9977 &cache->space_info);
9978 if (ret) {
9979 btrfs_remove_free_space_cache(cache);
9980 btrfs_put_block_group(cache);
9981 return ret;
9982 }
9983
Josef Bacik8c579fe2013-04-02 12:40:42 -04009984 ret = btrfs_add_block_group_cache(root->fs_info, cache);
9985 if (ret) {
9986 btrfs_remove_free_space_cache(cache);
9987 btrfs_put_block_group(cache);
9988 return ret;
9989 }
9990
Filipe Manana2e6e5182015-05-12 00:28:11 +01009991 /*
9992 * Now that our block group has its ->space_info set and is inserted in
9993 * the rbtree, update the space info's counters.
9994 */
Chris Mason6324fbf2008-03-24 15:01:59 -04009995 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
9996 &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04009997 if (ret) {
9998 btrfs_remove_free_space_cache(cache);
9999 spin_lock(&root->fs_info->block_group_cache_lock);
10000 rb_erase(&cache->cache_node,
10001 &root->fs_info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +000010002 RB_CLEAR_NODE(&cache->cache_node);
Josef Bacik8c579fe2013-04-02 12:40:42 -040010003 spin_unlock(&root->fs_info->block_group_cache_lock);
10004 btrfs_put_block_group(cache);
10005 return ret;
10006 }
Li Zefanc7c144d2011-12-07 10:39:22 +080010007 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -040010008
10009 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -040010010 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -040010011 spin_unlock(&cache->space_info->lock);
10012
Yan, Zhengb742bb822010-05-16 10:46:24 -040010013 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -040010014
Josef Bacik47ab2a62014-09-18 11:20:02 -040010015 list_add_tail(&cache->bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -040010016
Chris Masond18a2c42008-04-04 15:40:00 -040010017 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -040010018
Chris Mason6324fbf2008-03-24 15:01:59 -040010019 return 0;
10020}
Zheng Yan1a40e232008-09-26 10:09:34 -040010021
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010022static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
10023{
Ilya Dryomov899c81e2012-03-27 17:09:16 +030010024 u64 extra_flags = chunk_to_extended(flags) &
10025 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010026
Miao Xiede98ced2013-01-29 10:13:12 +000010027 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010028 if (flags & BTRFS_BLOCK_GROUP_DATA)
10029 fs_info->avail_data_alloc_bits &= ~extra_flags;
10030 if (flags & BTRFS_BLOCK_GROUP_METADATA)
10031 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
10032 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
10033 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +000010034 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010035}
10036
Zheng Yan1a40e232008-09-26 10:09:34 -040010037int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
Filipe Manana04216822014-11-27 21:14:15 +000010038 struct btrfs_root *root, u64 group_start,
10039 struct extent_map *em)
Zheng Yan1a40e232008-09-26 10:09:34 -040010040{
10041 struct btrfs_path *path;
10042 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -040010043 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -040010044 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -040010045 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -040010046 struct inode *inode;
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010047 struct kobject *kobj = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -040010048 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010049 int index;
Josef Bacik89a55892010-10-14 14:52:27 -040010050 int factor;
Filipe Manana4f69cb92014-11-26 15:28:51 +000010051 struct btrfs_caching_control *caching_ctl = NULL;
Filipe Manana04216822014-11-27 21:14:15 +000010052 bool remove_em;
Zheng Yan1a40e232008-09-26 10:09:34 -040010053
Zheng Yan1a40e232008-09-26 10:09:34 -040010054 root = root->fs_info->extent_root;
10055
10056 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
10057 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -050010058 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -040010059
liubo9f7c43c2011-03-07 02:13:33 +000010060 /*
10061 * Free the reserved super bytes from this block group before
10062 * remove it.
10063 */
10064 free_excluded_extents(root, block_group);
10065
Zheng Yan1a40e232008-09-26 10:09:34 -040010066 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010067 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -040010068 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
10069 BTRFS_BLOCK_GROUP_RAID1 |
10070 BTRFS_BLOCK_GROUP_RAID10))
10071 factor = 2;
10072 else
10073 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -040010074
Chris Mason44fb5512009-06-04 15:34:51 -040010075 /* make sure this block group isn't part of an allocation cluster */
10076 cluster = &root->fs_info->data_alloc_cluster;
10077 spin_lock(&cluster->refill_lock);
10078 btrfs_return_cluster_to_free_space(block_group, cluster);
10079 spin_unlock(&cluster->refill_lock);
10080
10081 /*
10082 * make sure this block group isn't part of a metadata
10083 * allocation cluster
10084 */
10085 cluster = &root->fs_info->meta_alloc_cluster;
10086 spin_lock(&cluster->refill_lock);
10087 btrfs_return_cluster_to_free_space(block_group, cluster);
10088 spin_unlock(&cluster->refill_lock);
10089
Zheng Yan1a40e232008-09-26 10:09:34 -040010090 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010091 if (!path) {
10092 ret = -ENOMEM;
10093 goto out;
10094 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010095
Chris Mason1bbc6212015-04-06 12:46:08 -070010096 /*
10097 * get the inode first so any iput calls done for the io_list
10098 * aren't the final iput (no unlinks allowed now)
10099 */
Ilya Dryomov10b2f342011-10-02 13:56:53 +030010100 inode = lookup_free_space_inode(tree_root, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -070010101
10102 mutex_lock(&trans->transaction->cache_write_mutex);
10103 /*
10104 * make sure our free spache cache IO is done before remove the
10105 * free space inode
10106 */
10107 spin_lock(&trans->transaction->dirty_bgs_lock);
10108 if (!list_empty(&block_group->io_list)) {
10109 list_del_init(&block_group->io_list);
10110
10111 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
10112
10113 spin_unlock(&trans->transaction->dirty_bgs_lock);
10114 btrfs_wait_cache_io(root, trans, block_group,
10115 &block_group->io_ctl, path,
10116 block_group->key.objectid);
10117 btrfs_put_block_group(block_group);
10118 spin_lock(&trans->transaction->dirty_bgs_lock);
10119 }
10120
10121 if (!list_empty(&block_group->dirty_list)) {
10122 list_del_init(&block_group->dirty_list);
10123 btrfs_put_block_group(block_group);
10124 }
10125 spin_unlock(&trans->transaction->dirty_bgs_lock);
10126 mutex_unlock(&trans->transaction->cache_write_mutex);
10127
Josef Bacik0af3d002010-06-21 14:48:16 -040010128 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +000010129 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010130 if (ret) {
10131 btrfs_add_delayed_iput(inode);
10132 goto out;
10133 }
Josef Bacik0af3d002010-06-21 14:48:16 -040010134 clear_nlink(inode);
10135 /* One for the block groups ref */
10136 spin_lock(&block_group->lock);
10137 if (block_group->iref) {
10138 block_group->iref = 0;
10139 block_group->inode = NULL;
10140 spin_unlock(&block_group->lock);
10141 iput(inode);
10142 } else {
10143 spin_unlock(&block_group->lock);
10144 }
10145 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -040010146 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -040010147 }
10148
10149 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
10150 key.offset = block_group->key.objectid;
10151 key.type = 0;
10152
10153 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
10154 if (ret < 0)
10155 goto out;
10156 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +020010157 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010158 if (ret == 0) {
10159 ret = btrfs_del_item(trans, tree_root, path);
10160 if (ret)
10161 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +020010162 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010163 }
10164
Yan Zheng3dfdb932009-01-21 10:49:16 -050010165 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -040010166 rb_erase(&block_group->cache_node,
10167 &root->fs_info->block_group_cache_tree);
Filipe Manana292cbd52014-11-26 15:28:50 +000010168 RB_CLEAR_NODE(&block_group->cache_node);
Liu Boa1897fd2012-12-27 09:01:23 +000010169
10170 if (root->fs_info->first_logical_byte == block_group->key.objectid)
10171 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -050010172 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010173
Josef Bacik80eb2342008-10-29 14:49:05 -040010174 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -040010175 /*
10176 * we must use list_del_init so people can check to see if they
10177 * are still on the list after taking the semaphore
10178 */
10179 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010180 if (list_empty(&block_group->space_info->block_groups[index])) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010181 kobj = block_group->space_info->block_group_kobjs[index];
10182 block_group->space_info->block_group_kobjs[index] = NULL;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010183 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010184 }
Josef Bacik80eb2342008-10-29 14:49:05 -040010185 up_write(&block_group->space_info->groups_sem);
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010186 if (kobj) {
10187 kobject_del(kobj);
10188 kobject_put(kobj);
10189 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010190
Filipe Manana4f69cb92014-11-26 15:28:51 +000010191 if (block_group->has_caching_ctl)
10192 caching_ctl = get_caching_control(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -040010193 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -040010194 wait_block_group_cache_done(block_group);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010195 if (block_group->has_caching_ctl) {
10196 down_write(&root->fs_info->commit_root_sem);
10197 if (!caching_ctl) {
10198 struct btrfs_caching_control *ctl;
10199
10200 list_for_each_entry(ctl,
10201 &root->fs_info->caching_block_groups, list)
10202 if (ctl->block_group == block_group) {
10203 caching_ctl = ctl;
10204 atomic_inc(&caching_ctl->count);
10205 break;
10206 }
10207 }
10208 if (caching_ctl)
10209 list_del_init(&caching_ctl->list);
10210 up_write(&root->fs_info->commit_root_sem);
10211 if (caching_ctl) {
10212 /* Once for the caching bgs list and once for us. */
10213 put_caching_control(caching_ctl);
10214 put_caching_control(caching_ctl);
10215 }
10216 }
Josef Bacik817d52f2009-07-13 21:29:25 -040010217
Josef Bacikce93ec52014-11-17 15:45:48 -050010218 spin_lock(&trans->transaction->dirty_bgs_lock);
10219 if (!list_empty(&block_group->dirty_list)) {
Chris Mason1bbc6212015-04-06 12:46:08 -070010220 WARN_ON(1);
10221 }
10222 if (!list_empty(&block_group->io_list)) {
10223 WARN_ON(1);
Josef Bacikce93ec52014-11-17 15:45:48 -050010224 }
10225 spin_unlock(&trans->transaction->dirty_bgs_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010226 btrfs_remove_free_space_cache(block_group);
10227
Yan Zhengc146afa2008-11-12 14:34:12 -050010228 spin_lock(&block_group->space_info->lock);
Filipe Manana75c68e92015-01-16 13:24:40 +000010229 list_del_init(&block_group->ro_list);
Zhao Lei18d018a2015-02-24 20:07:44 +080010230
10231 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
10232 WARN_ON(block_group->space_info->total_bytes
10233 < block_group->key.offset);
10234 WARN_ON(block_group->space_info->bytes_readonly
10235 < block_group->key.offset);
10236 WARN_ON(block_group->space_info->disk_total
10237 < block_group->key.offset * factor);
10238 }
Yan Zhengc146afa2008-11-12 14:34:12 -050010239 block_group->space_info->total_bytes -= block_group->key.offset;
10240 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -040010241 block_group->space_info->disk_total -= block_group->key.offset * factor;
Zhao Lei18d018a2015-02-24 20:07:44 +080010242
Yan Zhengc146afa2008-11-12 14:34:12 -050010243 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -040010244
Josef Bacik0af3d002010-06-21 14:48:16 -040010245 memcpy(&key, &block_group->key, sizeof(key));
10246
Filipe Manana04216822014-11-27 21:14:15 +000010247 lock_chunks(root);
Filipe Manana495e64f2014-12-02 18:07:30 +000010248 if (!list_empty(&em->list)) {
10249 /* We're in the transaction->pending_chunks list. */
10250 free_extent_map(em);
10251 }
Filipe Manana04216822014-11-27 21:14:15 +000010252 spin_lock(&block_group->lock);
10253 block_group->removed = 1;
10254 /*
10255 * At this point trimming can't start on this block group, because we
10256 * removed the block group from the tree fs_info->block_group_cache_tree
10257 * so no one can't find it anymore and even if someone already got this
10258 * block group before we removed it from the rbtree, they have already
10259 * incremented block_group->trimming - if they didn't, they won't find
10260 * any free space entries because we already removed them all when we
10261 * called btrfs_remove_free_space_cache().
10262 *
10263 * And we must not remove the extent map from the fs_info->mapping_tree
10264 * to prevent the same logical address range and physical device space
10265 * ranges from being reused for a new block group. This is because our
10266 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
10267 * completely transactionless, so while it is trimming a range the
10268 * currently running transaction might finish and a new one start,
10269 * allowing for new block groups to be created that can reuse the same
10270 * physical device locations unless we take this special care.
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010271 *
10272 * There may also be an implicit trim operation if the file system
10273 * is mounted with -odiscard. The same protections must remain
10274 * in place until the extents have been discarded completely when
10275 * the transaction commit has completed.
Filipe Manana04216822014-11-27 21:14:15 +000010276 */
10277 remove_em = (atomic_read(&block_group->trimming) == 0);
10278 /*
10279 * Make sure a trimmer task always sees the em in the pinned_chunks list
10280 * if it sees block_group->removed == 1 (needs to lock block_group->lock
10281 * before checking block_group->removed).
10282 */
10283 if (!remove_em) {
10284 /*
10285 * Our em might be in trans->transaction->pending_chunks which
10286 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10287 * and so is the fs_info->pinned_chunks list.
10288 *
10289 * So at this point we must be holding the chunk_mutex to avoid
10290 * any races with chunk allocation (more specifically at
10291 * volumes.c:contains_pending_extent()), to ensure it always
10292 * sees the em, either in the pending_chunks list or in the
10293 * pinned_chunks list.
10294 */
10295 list_move_tail(&em->list, &root->fs_info->pinned_chunks);
10296 }
10297 spin_unlock(&block_group->lock);
Filipe Manana04216822014-11-27 21:14:15 +000010298
10299 if (remove_em) {
10300 struct extent_map_tree *em_tree;
10301
10302 em_tree = &root->fs_info->mapping_tree.map_tree;
10303 write_lock(&em_tree->lock);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010304 /*
10305 * The em might be in the pending_chunks list, so make sure the
10306 * chunk mutex is locked, since remove_extent_mapping() will
10307 * delete us from that list.
10308 */
Filipe Manana04216822014-11-27 21:14:15 +000010309 remove_extent_mapping(em_tree, em);
10310 write_unlock(&em_tree->lock);
10311 /* once for the tree */
10312 free_extent_map(em);
10313 }
10314
Filipe Manana8dbcd102014-12-02 18:07:49 +000010315 unlock_chunks(root);
10316
Omar Sandoval1e144fb2015-09-29 20:50:37 -070010317 ret = remove_block_group_free_space(trans, root->fs_info, block_group);
10318 if (ret)
10319 goto out;
10320
Chris Masonfa9c0d792009-04-03 09:47:43 -040010321 btrfs_put_block_group(block_group);
10322 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -040010323
10324 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
10325 if (ret > 0)
10326 ret = -EIO;
10327 if (ret < 0)
10328 goto out;
10329
10330 ret = btrfs_del_item(trans, root, path);
10331out:
10332 btrfs_free_path(path);
10333 return ret;
10334}
liuboacce9522011-01-06 19:30:25 +080010335
Filipe Manana8eab77f2015-11-13 23:57:16 +000010336struct btrfs_trans_handle *
Filipe Manana7fd01182015-11-13 23:57:17 +000010337btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info,
10338 const u64 chunk_offset)
Filipe Manana8eab77f2015-11-13 23:57:16 +000010339{
Filipe Manana7fd01182015-11-13 23:57:17 +000010340 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
10341 struct extent_map *em;
10342 struct map_lookup *map;
10343 unsigned int num_items;
10344
10345 read_lock(&em_tree->lock);
10346 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
10347 read_unlock(&em_tree->lock);
10348 ASSERT(em && em->start == chunk_offset);
10349
Filipe Manana8eab77f2015-11-13 23:57:16 +000010350 /*
Filipe Manana7fd01182015-11-13 23:57:17 +000010351 * We need to reserve 3 + N units from the metadata space info in order
10352 * to remove a block group (done at btrfs_remove_chunk() and at
10353 * btrfs_remove_block_group()), which are used for:
10354 *
Filipe Manana8eab77f2015-11-13 23:57:16 +000010355 * 1 unit for adding the free space inode's orphan (located in the tree
10356 * of tree roots).
Filipe Manana7fd01182015-11-13 23:57:17 +000010357 * 1 unit for deleting the block group item (located in the extent
10358 * tree).
10359 * 1 unit for deleting the free space item (located in tree of tree
10360 * roots).
10361 * N units for deleting N device extent items corresponding to each
10362 * stripe (located in the device tree).
10363 *
10364 * In order to remove a block group we also need to reserve units in the
10365 * system space info in order to update the chunk tree (update one or
10366 * more device items and remove one chunk item), but this is done at
10367 * btrfs_remove_chunk() through a call to check_system_chunk().
Filipe Manana8eab77f2015-11-13 23:57:16 +000010368 */
Filipe Manana7fd01182015-11-13 23:57:17 +000010369 map = (struct map_lookup *)em->bdev;
10370 num_items = 3 + map->num_stripes;
10371 free_extent_map(em);
10372
Filipe Manana8eab77f2015-11-13 23:57:16 +000010373 return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root,
Filipe Manana7fd01182015-11-13 23:57:17 +000010374 num_items, 1);
Filipe Manana8eab77f2015-11-13 23:57:16 +000010375}
10376
Josef Bacik47ab2a62014-09-18 11:20:02 -040010377/*
10378 * Process the unused_bgs list and remove any that don't have any allocated
10379 * space inside of them.
10380 */
10381void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10382{
10383 struct btrfs_block_group_cache *block_group;
10384 struct btrfs_space_info *space_info;
10385 struct btrfs_root *root = fs_info->extent_root;
10386 struct btrfs_trans_handle *trans;
10387 int ret = 0;
10388
10389 if (!fs_info->open)
10390 return;
10391
10392 spin_lock(&fs_info->unused_bgs_lock);
10393 while (!list_empty(&fs_info->unused_bgs)) {
10394 u64 start, end;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010395 int trimming;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010396
10397 block_group = list_first_entry(&fs_info->unused_bgs,
10398 struct btrfs_block_group_cache,
10399 bg_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010400 list_del_init(&block_group->bg_list);
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010401
10402 space_info = block_group->space_info;
10403
Josef Bacik47ab2a62014-09-18 11:20:02 -040010404 if (ret || btrfs_mixed_space_info(space_info)) {
10405 btrfs_put_block_group(block_group);
10406 continue;
10407 }
10408 spin_unlock(&fs_info->unused_bgs_lock);
10409
Zhao Leid5f2e332015-10-08 18:46:44 +080010410 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +010010411
Josef Bacik47ab2a62014-09-18 11:20:02 -040010412 /* Don't want to race with allocators so take the groups_sem */
10413 down_write(&space_info->groups_sem);
10414 spin_lock(&block_group->lock);
10415 if (block_group->reserved ||
10416 btrfs_block_group_used(&block_group->item) ||
Zhao Leiaefbe9a2015-09-29 21:03:54 +080010417 block_group->ro ||
10418 list_is_singular(&block_group->list)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -040010419 /*
10420 * We want to bail if we made new allocations or have
10421 * outstanding allocations in this block group. We do
10422 * the ro check in case balance is currently acting on
10423 * this block group.
10424 */
10425 spin_unlock(&block_group->lock);
10426 up_write(&space_info->groups_sem);
10427 goto next;
10428 }
10429 spin_unlock(&block_group->lock);
10430
10431 /* We don't want to force the issue, only flip if it's ok. */
Zhaolei868f4012015-08-05 16:43:27 +080010432 ret = inc_block_group_ro(block_group, 0);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010433 up_write(&space_info->groups_sem);
10434 if (ret < 0) {
10435 ret = 0;
10436 goto next;
10437 }
10438
10439 /*
10440 * Want to do this before we do anything else so we can recover
10441 * properly if we fail to join the transaction.
10442 */
Filipe Manana7fd01182015-11-13 23:57:17 +000010443 trans = btrfs_start_trans_remove_block_group(fs_info,
10444 block_group->key.objectid);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010445 if (IS_ERR(trans)) {
Zhaolei868f4012015-08-05 16:43:27 +080010446 btrfs_dec_block_group_ro(root, block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010447 ret = PTR_ERR(trans);
10448 goto next;
10449 }
10450
10451 /*
10452 * We could have pending pinned extents for this block group,
10453 * just delete them, we don't care about them anymore.
10454 */
10455 start = block_group->key.objectid;
10456 end = start + block_group->key.offset - 1;
Filipe Mananad4b450c2015-01-29 19:18:25 +000010457 /*
10458 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10459 * btrfs_finish_extent_commit(). If we are at transaction N,
10460 * another task might be running finish_extent_commit() for the
10461 * previous transaction N - 1, and have seen a range belonging
10462 * to the block group in freed_extents[] before we were able to
10463 * clear the whole block group range from freed_extents[]. This
10464 * means that task can lookup for the block group after we
10465 * unpinned it from freed_extents[] and removed it, leading to
10466 * a BUG_ON() at btrfs_unpin_extent_range().
10467 */
10468 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana758eb512014-11-03 14:08:39 +000010469 ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
Josef Bacik47ab2a62014-09-18 11:20:02 -040010470 EXTENT_DIRTY, GFP_NOFS);
Filipe Manana758eb512014-11-03 14:08:39 +000010471 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010472 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Zhaolei868f4012015-08-05 16:43:27 +080010473 btrfs_dec_block_group_ro(root, block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010474 goto end_trans;
10475 }
10476 ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
Josef Bacik47ab2a62014-09-18 11:20:02 -040010477 EXTENT_DIRTY, GFP_NOFS);
Filipe Manana758eb512014-11-03 14:08:39 +000010478 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010479 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Zhaolei868f4012015-08-05 16:43:27 +080010480 btrfs_dec_block_group_ro(root, block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010481 goto end_trans;
10482 }
Filipe Mananad4b450c2015-01-29 19:18:25 +000010483 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010484
10485 /* Reset pinned so btrfs_put_block_group doesn't complain */
Zhao Leic30666d2015-02-25 14:17:20 +080010486 spin_lock(&space_info->lock);
10487 spin_lock(&block_group->lock);
10488
10489 space_info->bytes_pinned -= block_group->pinned;
10490 space_info->bytes_readonly += block_group->pinned;
10491 percpu_counter_add(&space_info->total_bytes_pinned,
10492 -block_group->pinned);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010493 block_group->pinned = 0;
10494
Zhao Leic30666d2015-02-25 14:17:20 +080010495 spin_unlock(&block_group->lock);
10496 spin_unlock(&space_info->lock);
10497
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010498 /* DISCARD can flip during remount */
10499 trimming = btrfs_test_opt(root, DISCARD);
10500
10501 /* Implicit trim during transaction commit. */
10502 if (trimming)
10503 btrfs_get_block_group_trimming(block_group);
10504
Josef Bacik47ab2a62014-09-18 11:20:02 -040010505 /*
10506 * Btrfs_remove_chunk will abort the transaction if things go
10507 * horribly wrong.
10508 */
10509 ret = btrfs_remove_chunk(trans, root,
10510 block_group->key.objectid);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010511
10512 if (ret) {
10513 if (trimming)
10514 btrfs_put_block_group_trimming(block_group);
10515 goto end_trans;
10516 }
10517
10518 /*
10519 * If we're not mounted with -odiscard, we can just forget
10520 * about this block group. Otherwise we'll need to wait
10521 * until transaction commit to do the actual discard.
10522 */
10523 if (trimming) {
10524 WARN_ON(!list_empty(&block_group->bg_list));
10525 spin_lock(&trans->transaction->deleted_bgs_lock);
10526 list_move(&block_group->bg_list,
10527 &trans->transaction->deleted_bgs);
10528 spin_unlock(&trans->transaction->deleted_bgs_lock);
10529 btrfs_get_block_group(block_group);
10530 }
Filipe Manana758eb512014-11-03 14:08:39 +000010531end_trans:
Josef Bacik47ab2a62014-09-18 11:20:02 -040010532 btrfs_end_transaction(trans, root);
10533next:
Zhao Leid5f2e332015-10-08 18:46:44 +080010534 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010535 btrfs_put_block_group(block_group);
10536 spin_lock(&fs_info->unused_bgs_lock);
10537 }
10538 spin_unlock(&fs_info->unused_bgs_lock);
10539}
10540
liuboc59021f2011-03-07 02:13:14 +000010541int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
10542{
10543 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +000010544 struct btrfs_super_block *disk_super;
10545 u64 features;
10546 u64 flags;
10547 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +000010548 int ret;
10549
David Sterba6c417612011-04-13 15:41:04 +020010550 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +000010551 if (!btrfs_super_root(disk_super))
10552 return 1;
liuboc59021f2011-03-07 02:13:14 +000010553
liubo1aba86d2011-04-08 08:44:37 +000010554 features = btrfs_super_incompat_flags(disk_super);
10555 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
10556 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +000010557
liubo1aba86d2011-04-08 08:44:37 +000010558 flags = BTRFS_BLOCK_GROUP_SYSTEM;
10559 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +000010560 if (ret)
liubo1aba86d2011-04-08 08:44:37 +000010561 goto out;
liuboc59021f2011-03-07 02:13:14 +000010562
liubo1aba86d2011-04-08 08:44:37 +000010563 if (mixed) {
10564 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
10565 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
10566 } else {
10567 flags = BTRFS_BLOCK_GROUP_METADATA;
10568 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
10569 if (ret)
10570 goto out;
10571
10572 flags = BTRFS_BLOCK_GROUP_DATA;
10573 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
10574 }
10575out:
liuboc59021f2011-03-07 02:13:14 +000010576 return ret;
10577}
10578
liuboacce9522011-01-06 19:30:25 +080010579int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
10580{
Filipe Manana678886b2014-12-07 21:31:47 +000010581 return unpin_extent_range(root, start, end, false);
liuboacce9522011-01-06 19:30:25 +080010582}
10583
Jeff Mahoney499f3772015-06-15 09:41:17 -040010584/*
10585 * It used to be that old block groups would be left around forever.
10586 * Iterating over them would be enough to trim unused space. Since we
10587 * now automatically remove them, we also need to iterate over unallocated
10588 * space.
10589 *
10590 * We don't want a transaction for this since the discard may take a
10591 * substantial amount of time. We don't require that a transaction be
10592 * running, but we do need to take a running transaction into account
10593 * to ensure that we're not discarding chunks that were released in
10594 * the current transaction.
10595 *
10596 * Holding the chunks lock will prevent other threads from allocating
10597 * or releasing chunks, but it won't prevent a running transaction
10598 * from committing and releasing the memory that the pending chunks
10599 * list head uses. For that, we need to take a reference to the
10600 * transaction.
10601 */
10602static int btrfs_trim_free_extents(struct btrfs_device *device,
10603 u64 minlen, u64 *trimmed)
10604{
10605 u64 start = 0, len = 0;
10606 int ret;
10607
10608 *trimmed = 0;
10609
10610 /* Not writeable = nothing to do. */
10611 if (!device->writeable)
10612 return 0;
10613
10614 /* No free space = nothing to do. */
10615 if (device->total_bytes <= device->bytes_used)
10616 return 0;
10617
10618 ret = 0;
10619
10620 while (1) {
10621 struct btrfs_fs_info *fs_info = device->dev_root->fs_info;
10622 struct btrfs_transaction *trans;
10623 u64 bytes;
10624
10625 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
10626 if (ret)
10627 return ret;
10628
10629 down_read(&fs_info->commit_root_sem);
10630
10631 spin_lock(&fs_info->trans_lock);
10632 trans = fs_info->running_transaction;
10633 if (trans)
10634 atomic_inc(&trans->use_count);
10635 spin_unlock(&fs_info->trans_lock);
10636
10637 ret = find_free_dev_extent_start(trans, device, minlen, start,
10638 &start, &len);
10639 if (trans)
10640 btrfs_put_transaction(trans);
10641
10642 if (ret) {
10643 up_read(&fs_info->commit_root_sem);
10644 mutex_unlock(&fs_info->chunk_mutex);
10645 if (ret == -ENOSPC)
10646 ret = 0;
10647 break;
10648 }
10649
10650 ret = btrfs_issue_discard(device->bdev, start, len, &bytes);
10651 up_read(&fs_info->commit_root_sem);
10652 mutex_unlock(&fs_info->chunk_mutex);
10653
10654 if (ret)
10655 break;
10656
10657 start += len;
10658 *trimmed += bytes;
10659
10660 if (fatal_signal_pending(current)) {
10661 ret = -ERESTARTSYS;
10662 break;
10663 }
10664
10665 cond_resched();
10666 }
10667
10668 return ret;
10669}
10670
Li Dongyangf7039b12011-03-24 10:24:28 +000010671int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
10672{
10673 struct btrfs_fs_info *fs_info = root->fs_info;
10674 struct btrfs_block_group_cache *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010675 struct btrfs_device *device;
10676 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +000010677 u64 group_trimmed;
10678 u64 start;
10679 u64 end;
10680 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +080010681 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +000010682 int ret = 0;
10683
Liu Bo2cac13e2012-02-09 18:17:41 +080010684 /*
10685 * try to trim all FS space, our block group may start from non-zero.
10686 */
10687 if (range->len == total_bytes)
10688 cache = btrfs_lookup_first_block_group(fs_info, range->start);
10689 else
10690 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +000010691
10692 while (cache) {
10693 if (cache->key.objectid >= (range->start + range->len)) {
10694 btrfs_put_block_group(cache);
10695 break;
10696 }
10697
10698 start = max(range->start, cache->key.objectid);
10699 end = min(range->start + range->len,
10700 cache->key.objectid + cache->key.offset);
10701
10702 if (end - start >= range->minlen) {
10703 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +000010704 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -040010705 if (ret) {
10706 btrfs_put_block_group(cache);
10707 break;
10708 }
10709 ret = wait_block_group_cache_done(cache);
10710 if (ret) {
10711 btrfs_put_block_group(cache);
10712 break;
10713 }
Li Dongyangf7039b12011-03-24 10:24:28 +000010714 }
10715 ret = btrfs_trim_block_group(cache,
10716 &group_trimmed,
10717 start,
10718 end,
10719 range->minlen);
10720
10721 trimmed += group_trimmed;
10722 if (ret) {
10723 btrfs_put_block_group(cache);
10724 break;
10725 }
10726 }
10727
10728 cache = next_block_group(fs_info->tree_root, cache);
10729 }
10730
Jeff Mahoney499f3772015-06-15 09:41:17 -040010731 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
10732 devices = &root->fs_info->fs_devices->alloc_list;
10733 list_for_each_entry(device, devices, dev_alloc_list) {
10734 ret = btrfs_trim_free_extents(device, range->minlen,
10735 &group_trimmed);
10736 if (ret)
10737 break;
10738
10739 trimmed += group_trimmed;
10740 }
10741 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
10742
Li Dongyangf7039b12011-03-24 10:24:28 +000010743 range->len = trimmed;
10744 return ret;
10745}
Miao Xie8257b2d2014-03-06 13:38:19 +080010746
10747/*
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010748 * btrfs_{start,end}_write_no_snapshoting() are similar to
10749 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
10750 * data into the page cache through nocow before the subvolume is snapshoted,
10751 * but flush the data into disk after the snapshot creation, or to prevent
10752 * operations while snapshoting is ongoing and that cause the snapshot to be
10753 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +080010754 */
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010755void btrfs_end_write_no_snapshoting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080010756{
10757 percpu_counter_dec(&root->subv_writers->counter);
10758 /*
David Sterbaa83342a2015-02-16 19:36:47 +010010759 * Make sure counter is updated before we wake up waiters.
Miao Xie8257b2d2014-03-06 13:38:19 +080010760 */
10761 smp_mb();
10762 if (waitqueue_active(&root->subv_writers->wait))
10763 wake_up(&root->subv_writers->wait);
10764}
10765
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010766int btrfs_start_write_no_snapshoting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080010767{
David Sterbaee39b432014-09-30 01:33:33 +020010768 if (atomic_read(&root->will_be_snapshoted))
Miao Xie8257b2d2014-03-06 13:38:19 +080010769 return 0;
10770
10771 percpu_counter_inc(&root->subv_writers->counter);
10772 /*
10773 * Make sure counter is updated before we check for snapshot creation.
10774 */
10775 smp_mb();
David Sterbaee39b432014-09-30 01:33:33 +020010776 if (atomic_read(&root->will_be_snapshoted)) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010777 btrfs_end_write_no_snapshoting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +080010778 return 0;
10779 }
10780 return 1;
10781}