blob: 475764502d42019edf3a152c45b3700169af3d09 [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"
Miao Xie3fed40c2012-09-13 04:51:36 -060036#include "math.h"
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040037#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070038#include "qgroup.h"
Chris Masonfec577f2007-02-26 10:40:21 -050039
Arne Jansen709c0482011-09-12 12:22:57 +020040#undef SCRAMBLE_DELAYED_REFS
41
Miao Xie9e622d62012-01-26 15:01:12 -050042/*
43 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040044 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
45 * if we really need one.
46 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040047 * CHUNK_ALLOC_LIMITED means to only try and allocate one
48 * if we have very few chunks already allocated. This is
49 * used as part of the clustering code to help make sure
50 * we have a good pool of storage to cluster in, without
51 * filling the FS with empty chunks
52 *
Miao Xie9e622d62012-01-26 15:01:12 -050053 * CHUNK_ALLOC_FORCE means it must try to allocate one
54 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040055 */
56enum {
57 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050058 CHUNK_ALLOC_LIMITED = 1,
59 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040060};
61
Josef Bacikfb25e912011-07-26 17:00:46 -040062/*
63 * Control how reservations are dealt with.
64 *
65 * RESERVE_FREE - freeing a reservation.
66 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
67 * ENOSPC accounting
68 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
69 * bytes_may_use as the ENOSPC accounting is done elsewhere
70 */
71enum {
72 RESERVE_FREE = 0,
73 RESERVE_ALLOC = 1,
74 RESERVE_ALLOC_NO_ACCOUNT = 2,
75};
76
Josef Bacikce93ec52014-11-17 15:45:48 -050077static int update_block_group(struct btrfs_trans_handle *trans,
78 struct btrfs_root *root, u64 bytenr,
79 u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040080static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
81 struct btrfs_root *root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080082 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -040083 u64 root_objectid, u64 owner_objectid,
84 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +080085 struct btrfs_delayed_extent_op *extra_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040086static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
87 struct extent_buffer *leaf,
88 struct btrfs_extent_item *ei);
89static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
90 struct btrfs_root *root,
91 u64 parent, u64 root_objectid,
92 u64 flags, u64 owner, u64 offset,
93 struct btrfs_key *ins, int ref_mod);
94static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
95 struct btrfs_root *root,
96 u64 parent, u64 root_objectid,
97 u64 flags, struct btrfs_disk_key *key,
Josef Bacikfcebe452014-05-13 17:30:47 -070098 int level, struct btrfs_key *ins,
99 int no_quota);
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
David Sterba62a45b62011-04-20 15:52:26 +0200128static void 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 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400361static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400362 struct btrfs_fs_info *info, u64 start, u64 end)
363{
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
Qu Wenruod458b052014-02-28 10:46:19 +0800399static noinline void caching_thread(struct btrfs_work *work)
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;
403 struct btrfs_caching_control *caching_ctl;
404 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400405 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400406 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400407 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400408 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400409 u64 last = 0;
410 u32 nritems;
Josef Bacik36cce922013-08-05 11:15:21 -0400411 int ret = -ENOMEM;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400412 bool wakeup = true;
Chris Masonf510cfe2007-10-15 16:14:48 -0400413
Josef Bacikbab39bf2011-06-30 14:42:28 -0400414 caching_ctl = container_of(work, struct btrfs_caching_control, work);
415 block_group = caching_ctl->block_group;
416 fs_info = block_group->fs_info;
417 extent_root = fs_info->extent_root;
418
Chris Masone37c9e62007-05-09 20:13:14 -0400419 path = btrfs_alloc_path();
420 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400421 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400422
Josef Bacik817d52f2009-07-13 21:29:25 -0400423 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400424
Josef Bacikd0bd4562015-09-23 14:54:14 -0400425#ifdef CONFIG_BTRFS_DEBUG
426 /*
427 * If we're fragmenting we don't want to make anybody think we can
428 * allocate from this block group until we've had a chance to fragment
429 * the free space.
430 */
431 if (btrfs_should_fragment_free_space(extent_root, block_group))
432 wakeup = false;
433#endif
Chris Mason5cd57b22008-06-25 16:01:30 -0400434 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400435 * We don't want to deadlock with somebody trying to allocate a new
436 * extent for the extent root while also trying to search the extent
437 * root to add free space. So we skip locking and search the commit
438 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400439 */
440 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400441 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400442 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400443
Yan Zhenge4404d62008-12-12 10:03:26 -0500444 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400445 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400446 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400447again:
Yan Zheng11833d62009-09-11 16:11:19 -0400448 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400449 /* need to make sure the commit_root doesn't disappear */
Josef Bacik9e351cc2014-03-13 15:42:13 -0400450 down_read(&fs_info->commit_root_sem);
Chris Mason013f1b12009-07-31 14:57:55 -0400451
Liu Bo52ee28d2013-07-11 17:51:15 +0800452next:
Yan Zheng11833d62009-09-11 16:11:19 -0400453 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400454 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400455 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500456
Yan Zheng11833d62009-09-11 16:11:19 -0400457 leaf = path->nodes[0];
458 nritems = btrfs_header_nritems(leaf);
459
Chris Masond3977122009-01-05 21:25:51 -0500460 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200461 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400462 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400463 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400464 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400465
Yan Zheng11833d62009-09-11 16:11:19 -0400466 if (path->slots[0] < nritems) {
467 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
468 } else {
469 ret = find_next_key(path, 0, &key);
470 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400471 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400472
Josef Bacikc9ea7b22013-09-19 10:02:11 -0400473 if (need_resched() ||
Josef Bacik9e351cc2014-03-13 15:42:13 -0400474 rwsem_is_contended(&fs_info->commit_root_sem)) {
Josef Bacikd0bd4562015-09-23 14:54:14 -0400475 if (wakeup)
476 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400477 btrfs_release_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400478 up_read(&fs_info->commit_root_sem);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400479 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400480 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400481 goto again;
482 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400483
484 ret = btrfs_next_leaf(extent_root, path);
485 if (ret < 0)
486 goto err;
487 if (ret)
488 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400489 leaf = path->nodes[0];
490 nritems = btrfs_header_nritems(leaf);
491 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400492 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400493
Liu Bo52ee28d2013-07-11 17:51:15 +0800494 if (key.objectid < last) {
495 key.objectid = last;
496 key.offset = 0;
497 key.type = BTRFS_EXTENT_ITEM_KEY;
498
Josef Bacikd0bd4562015-09-23 14:54:14 -0400499 if (wakeup)
500 caching_ctl->progress = last;
Liu Bo52ee28d2013-07-11 17:51:15 +0800501 btrfs_release_path(path);
502 goto next;
503 }
504
Yan Zheng11833d62009-09-11 16:11:19 -0400505 if (key.objectid < block_group->key.objectid) {
506 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400507 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400508 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400509
Chris Masone37c9e62007-05-09 20:13:14 -0400510 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400511 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400512 break;
Yan7d7d6062007-09-14 16:15:28 -0400513
Josef Bacik3173a182013-03-07 14:22:04 -0500514 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
515 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400516 total_found += add_new_free_space(block_group,
517 fs_info, last,
518 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500519 if (key.type == BTRFS_METADATA_ITEM_KEY)
520 last = key.objectid +
David Sterba707e8a02014-06-04 19:22:26 +0200521 fs_info->tree_root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500522 else
523 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400524
Yan Zheng11833d62009-09-11 16:11:19 -0400525 if (total_found > (1024 * 1024 * 2)) {
526 total_found = 0;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400527 if (wakeup)
528 wake_up(&caching_ctl->wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400529 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400530 }
Chris Masone37c9e62007-05-09 20:13:14 -0400531 path->slots[0]++;
532 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400533 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400534
535 total_found += add_new_free_space(block_group, fs_info, last,
536 block_group->key.objectid +
537 block_group->key.offset);
Josef Bacik817d52f2009-07-13 21:29:25 -0400538 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400539 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400540 block_group->cached = BTRFS_CACHE_FINISHED;
541 spin_unlock(&block_group->lock);
542
Josef Bacikd0bd4562015-09-23 14:54:14 -0400543#ifdef CONFIG_BTRFS_DEBUG
544 if (btrfs_should_fragment_free_space(extent_root, block_group)) {
545 u64 bytes_used;
546
547 spin_lock(&block_group->space_info->lock);
548 spin_lock(&block_group->lock);
549 bytes_used = block_group->key.offset -
550 btrfs_block_group_used(&block_group->item);
551 block_group->space_info->bytes_used += bytes_used >> 1;
552 spin_unlock(&block_group->lock);
553 spin_unlock(&block_group->space_info->lock);
554 fragment_free_space(extent_root, block_group);
555 }
556#endif
557
558 caching_ctl->progress = (u64)-1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400559err:
Chris Masone37c9e62007-05-09 20:13:14 -0400560 btrfs_free_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400561 up_read(&fs_info->commit_root_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400562
Yan Zheng11833d62009-09-11 16:11:19 -0400563 free_excluded_extents(extent_root, block_group);
564
565 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400566out:
Josef Bacik36cce922013-08-05 11:15:21 -0400567 if (ret) {
568 spin_lock(&block_group->lock);
569 block_group->caching_ctl = NULL;
570 block_group->cached = BTRFS_CACHE_ERROR;
571 spin_unlock(&block_group->lock);
572 }
Yan Zheng11833d62009-09-11 16:11:19 -0400573 wake_up(&caching_ctl->wait);
574
575 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000576 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400577}
578
Josef Bacik9d66e232010-08-25 16:54:15 -0400579static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400580 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400581{
Josef Bacik291c7d22011-11-14 13:52:14 -0500582 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400583 struct btrfs_fs_info *fs_info = cache->fs_info;
584 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400585 int ret = 0;
586
Josef Bacik291c7d22011-11-14 13:52:14 -0500587 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100588 if (!caching_ctl)
589 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500590
591 INIT_LIST_HEAD(&caching_ctl->list);
592 mutex_init(&caching_ctl->mutex);
593 init_waitqueue_head(&caching_ctl->wait);
594 caching_ctl->block_group = cache;
595 caching_ctl->progress = cache->key.objectid;
596 atomic_set(&caching_ctl->count, 1);
Liu Bo9e0af232014-08-15 23:36:53 +0800597 btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
598 caching_thread, NULL, NULL);
Josef Bacik291c7d22011-11-14 13:52:14 -0500599
600 spin_lock(&cache->lock);
601 /*
602 * This should be a rare occasion, but this could happen I think in the
603 * case where one thread starts to load the space cache info, and then
604 * some other thread starts a transaction commit which tries to do an
605 * allocation while the other thread is still loading the space cache
606 * info. The previous loop should have kept us from choosing this block
607 * group, but if we've moved to the state where we will wait on caching
608 * block groups we need to first check if we're doing a fast load here,
609 * so we can wait for it to finish, otherwise we could end up allocating
610 * from a block group who's cache gets evicted for one reason or
611 * another.
612 */
613 while (cache->cached == BTRFS_CACHE_FAST) {
614 struct btrfs_caching_control *ctl;
615
616 ctl = cache->caching_ctl;
617 atomic_inc(&ctl->count);
618 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
619 spin_unlock(&cache->lock);
620
621 schedule();
622
623 finish_wait(&ctl->wait, &wait);
624 put_caching_control(ctl);
625 spin_lock(&cache->lock);
626 }
627
628 if (cache->cached != BTRFS_CACHE_NO) {
629 spin_unlock(&cache->lock);
630 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400631 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500632 }
633 WARN_ON(cache->caching_ctl);
634 cache->caching_ctl = caching_ctl;
635 cache->cached = BTRFS_CACHE_FAST;
636 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400637
Josef Bacikd53ba472012-04-12 16:03:57 -0400638 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500639 mutex_lock(&caching_ctl->mutex);
Josef Bacik9d66e232010-08-25 16:54:15 -0400640 ret = load_free_space_cache(fs_info, cache);
641
642 spin_lock(&cache->lock);
643 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500644 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400645 cache->cached = BTRFS_CACHE_FINISHED;
646 cache->last_byte_to_unpin = (u64)-1;
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500647 caching_ctl->progress = (u64)-1;
Josef Bacik9d66e232010-08-25 16:54:15 -0400648 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500649 if (load_cache_only) {
650 cache->caching_ctl = NULL;
651 cache->cached = BTRFS_CACHE_NO;
652 } else {
653 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000654 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500655 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400656 }
657 spin_unlock(&cache->lock);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400658#ifdef CONFIG_BTRFS_DEBUG
659 if (ret == 1 &&
660 btrfs_should_fragment_free_space(fs_info->extent_root,
661 cache)) {
662 u64 bytes_used;
663
664 spin_lock(&cache->space_info->lock);
665 spin_lock(&cache->lock);
666 bytes_used = cache->key.offset -
667 btrfs_block_group_used(&cache->item);
668 cache->space_info->bytes_used += bytes_used >> 1;
669 spin_unlock(&cache->lock);
670 spin_unlock(&cache->space_info->lock);
671 fragment_free_space(fs_info->extent_root, cache);
672 }
673#endif
Josef Bacikcb83b7b2014-11-26 11:52:54 -0500674 mutex_unlock(&caching_ctl->mutex);
675
Josef Bacik291c7d22011-11-14 13:52:14 -0500676 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000677 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500678 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000679 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400680 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000681 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500682 } else {
683 /*
684 * We are not going to do the fast caching, set cached to the
685 * appropriate value and wakeup any waiters.
686 */
687 spin_lock(&cache->lock);
688 if (load_cache_only) {
689 cache->caching_ctl = NULL;
690 cache->cached = BTRFS_CACHE_NO;
691 } else {
692 cache->cached = BTRFS_CACHE_STARTED;
Filipe Manana4f69cb92014-11-26 15:28:51 +0000693 cache->has_caching_ctl = 1;
Josef Bacik291c7d22011-11-14 13:52:14 -0500694 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400695 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500696 wake_up(&caching_ctl->wait);
697 }
698
699 if (load_cache_only) {
700 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400701 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400702 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400703
Josef Bacik9e351cc2014-03-13 15:42:13 -0400704 down_write(&fs_info->commit_root_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500705 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400706 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400707 up_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -0400708
Josef Bacik11dfe352009-11-13 20:12:59 +0000709 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400710
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800711 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400712
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400713 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400714}
715
Josef Bacik0f9dd462008-09-23 13:14:11 -0400716/*
717 * return the block group that starts at or after bytenr
718 */
Chris Masond3977122009-01-05 21:25:51 -0500719static struct btrfs_block_group_cache *
720btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400721{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400722 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400723
Josef Bacik0f9dd462008-09-23 13:14:11 -0400724 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400725
Josef Bacik0f9dd462008-09-23 13:14:11 -0400726 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400727}
728
Josef Bacik0f9dd462008-09-23 13:14:11 -0400729/*
Sankar P9f556842009-05-14 13:52:22 -0400730 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400731 */
Chris Masond3977122009-01-05 21:25:51 -0500732struct btrfs_block_group_cache *btrfs_lookup_block_group(
733 struct btrfs_fs_info *info,
734 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400735{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400736 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400737
Josef Bacik0f9dd462008-09-23 13:14:11 -0400738 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400739
Josef Bacik0f9dd462008-09-23 13:14:11 -0400740 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400741}
Chris Mason0b86a832008-03-24 15:01:56 -0400742
Josef Bacik0f9dd462008-09-23 13:14:11 -0400743static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
744 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400745{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400746 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400747 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400748
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200749 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb822010-05-16 10:46:24 -0400750
Chris Mason4184ea72009-03-10 12:39:20 -0400751 rcu_read_lock();
752 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400753 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400754 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400755 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400756 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400757 }
Chris Mason4184ea72009-03-10 12:39:20 -0400758 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400759 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400760}
761
Chris Mason4184ea72009-03-10 12:39:20 -0400762/*
763 * after adding space to the filesystem, we need to clear the full flags
764 * on all the space infos.
765 */
766void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
767{
768 struct list_head *head = &info->space_info;
769 struct btrfs_space_info *found;
770
771 rcu_read_lock();
772 list_for_each_entry_rcu(found, head, list)
773 found->full = 0;
774 rcu_read_unlock();
775}
776
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000777/* simple helper to search for an existing data extent at a given offset */
778int btrfs_lookup_data_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400779{
780 int ret;
781 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400782 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400783
Zheng Yan31840ae2008-09-23 13:14:14 -0400784 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700785 if (!path)
786 return -ENOMEM;
787
Chris Masone02119d2008-09-05 16:13:11 -0400788 key.objectid = start;
789 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500790 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400791 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
792 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400793 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500794 return ret;
795}
796
Chris Masond8d5f3e2007-12-11 12:42:00 -0500797/*
Josef Bacik3173a182013-03-07 14:22:04 -0500798 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400799 *
800 * the head node for delayed ref is used to store the sum of all the
801 * reference count modifications queued up in the rbtree. the head
802 * node may also store the extent flags to set. This way you can check
803 * to see what the reference count and extent flags would be if all of
804 * the delayed refs are not processed.
805 */
806int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
807 struct btrfs_root *root, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500808 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400809{
810 struct btrfs_delayed_ref_head *head;
811 struct btrfs_delayed_ref_root *delayed_refs;
812 struct btrfs_path *path;
813 struct btrfs_extent_item *ei;
814 struct extent_buffer *leaf;
815 struct btrfs_key key;
816 u32 item_size;
817 u64 num_refs;
818 u64 extent_flags;
819 int ret;
820
Josef Bacik3173a182013-03-07 14:22:04 -0500821 /*
822 * If we don't have skinny metadata, don't bother doing anything
823 * different
824 */
825 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
David Sterba707e8a02014-06-04 19:22:26 +0200826 offset = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -0500827 metadata = 0;
828 }
829
Yan, Zhenga22285a2010-05-16 10:48:46 -0400830 path = btrfs_alloc_path();
831 if (!path)
832 return -ENOMEM;
833
Yan, Zhenga22285a2010-05-16 10:48:46 -0400834 if (!trans) {
835 path->skip_locking = 1;
836 path->search_commit_root = 1;
837 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000838
839search_again:
840 key.objectid = bytenr;
841 key.offset = offset;
842 if (metadata)
843 key.type = BTRFS_METADATA_ITEM_KEY;
844 else
845 key.type = BTRFS_EXTENT_ITEM_KEY;
846
Yan, Zhenga22285a2010-05-16 10:48:46 -0400847 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
848 &key, path, 0, 0);
849 if (ret < 0)
850 goto out_free;
851
Josef Bacik3173a182013-03-07 14:22:04 -0500852 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100853 if (path->slots[0]) {
854 path->slots[0]--;
855 btrfs_item_key_to_cpu(path->nodes[0], &key,
856 path->slots[0]);
857 if (key.objectid == bytenr &&
858 key.type == BTRFS_EXTENT_ITEM_KEY &&
David Sterba707e8a02014-06-04 19:22:26 +0200859 key.offset == root->nodesize)
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100860 ret = 0;
861 }
Josef Bacik3173a182013-03-07 14:22:04 -0500862 }
863
Yan, Zhenga22285a2010-05-16 10:48:46 -0400864 if (ret == 0) {
865 leaf = path->nodes[0];
866 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
867 if (item_size >= sizeof(*ei)) {
868 ei = btrfs_item_ptr(leaf, path->slots[0],
869 struct btrfs_extent_item);
870 num_refs = btrfs_extent_refs(leaf, ei);
871 extent_flags = btrfs_extent_flags(leaf, ei);
872 } else {
873#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
874 struct btrfs_extent_item_v0 *ei0;
875 BUG_ON(item_size != sizeof(*ei0));
876 ei0 = btrfs_item_ptr(leaf, path->slots[0],
877 struct btrfs_extent_item_v0);
878 num_refs = btrfs_extent_refs_v0(leaf, ei0);
879 /* FIXME: this isn't correct for data */
880 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
881#else
882 BUG();
883#endif
884 }
885 BUG_ON(num_refs == 0);
886 } else {
887 num_refs = 0;
888 extent_flags = 0;
889 ret = 0;
890 }
891
892 if (!trans)
893 goto out;
894
895 delayed_refs = &trans->transaction->delayed_refs;
896 spin_lock(&delayed_refs->lock);
897 head = btrfs_find_delayed_ref_head(trans, bytenr);
898 if (head) {
899 if (!mutex_trylock(&head->mutex)) {
900 atomic_inc(&head->node.refs);
901 spin_unlock(&delayed_refs->lock);
902
David Sterbab3b4aa72011-04-21 01:20:15 +0200903 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400904
David Sterba8cc33e52011-05-02 15:29:25 +0200905 /*
906 * Mutex was contended, block until it's released and try
907 * again
908 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400909 mutex_lock(&head->mutex);
910 mutex_unlock(&head->mutex);
911 btrfs_put_delayed_ref(&head->node);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000912 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400913 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500914 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400915 if (head->extent_op && head->extent_op->update_flags)
916 extent_flags |= head->extent_op->flags_to_set;
917 else
918 BUG_ON(num_refs == 0);
919
920 num_refs += head->node.ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500921 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400922 mutex_unlock(&head->mutex);
923 }
924 spin_unlock(&delayed_refs->lock);
925out:
926 WARN_ON(num_refs == 0);
927 if (refs)
928 *refs = num_refs;
929 if (flags)
930 *flags = extent_flags;
931out_free:
932 btrfs_free_path(path);
933 return ret;
934}
935
936/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500937 * Back reference rules. Back refs have three main goals:
938 *
939 * 1) differentiate between all holders of references to an extent so that
940 * when a reference is dropped we can make sure it was a valid reference
941 * before freeing the extent.
942 *
943 * 2) Provide enough information to quickly find the holders of an extent
944 * if we notice a given block is corrupted or bad.
945 *
946 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
947 * maintenance. This is actually the same as #2, but with a slightly
948 * different use case.
949 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 * There are two kinds of back refs. The implicit back refs is optimized
951 * for pointers in non-shared tree blocks. For a given pointer in a block,
952 * back refs of this kind provide information about the block's owner tree
953 * and the pointer's key. These information allow us to find the block by
954 * b-tree searching. The full back refs is for pointers in tree blocks not
955 * referenced by their owner trees. The location of tree block is recorded
956 * in the back refs. Actually the full back refs is generic, and can be
957 * used in all cases the implicit back refs is used. The major shortcoming
958 * of the full back refs is its overhead. Every time a tree block gets
959 * COWed, we have to update back refs entry for all pointers in it.
960 *
961 * For a newly allocated tree block, we use implicit back refs for
962 * pointers in it. This means most tree related operations only involve
963 * implicit back refs. For a tree block created in old transaction, the
964 * only way to drop a reference to it is COW it. So we can detect the
965 * event that tree block loses its owner tree's reference and do the
966 * back refs conversion.
967 *
968 * When a tree block is COW'd through a tree, there are four cases:
969 *
970 * The reference count of the block is one and the tree is the block's
971 * owner tree. Nothing to do in this case.
972 *
973 * The reference count of the block is one and the tree is not the
974 * block's owner tree. In this case, full back refs is used for pointers
975 * in the block. Remove these full back refs, add implicit back refs for
976 * every pointers in the new block.
977 *
978 * The reference count of the block is greater than one and the tree is
979 * the block's owner tree. In this case, implicit back refs is used for
980 * pointers in the block. Add full back refs for every pointers in the
981 * block, increase lower level extents' reference counts. The original
982 * implicit back refs are entailed to the new block.
983 *
984 * The reference count of the block is greater than one and the tree is
985 * not the block's owner tree. Add implicit back refs for every pointer in
986 * the new block, increase lower level extents' reference count.
987 *
988 * Back Reference Key composing:
989 *
990 * The key objectid corresponds to the first byte in the extent,
991 * The key type is used to differentiate between types of back refs.
992 * There are different meanings of the key offset for different types
993 * of back refs.
994 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500995 * File extents can be referenced by:
996 *
997 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400998 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500999 * - different offsets inside a file (bookend extents in file.c)
1000 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001001 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001002 *
1003 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -05001004 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001005 * - original offset in the file
1006 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -04001007 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001008 * The key offset for the implicit back refs is hash of the first
1009 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001010 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001011 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001012 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001013 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001014 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001015 * The key offset for the implicit back refs is the first byte of
1016 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -05001017 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001018 * When a file extent is allocated, The implicit back refs is used.
1019 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -05001020 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021 * (root_key.objectid, inode objectid, offset in file, 1)
1022 *
1023 * When a file extent is removed file truncation, we find the
1024 * corresponding implicit back refs and check the following fields:
1025 *
1026 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -05001027 *
1028 * Btree extents can be referenced by:
1029 *
1030 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -05001031 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001032 * Both the implicit back refs and the full back refs for tree blocks
1033 * only consist of key. The key offset for the implicit back refs is
1034 * objectid of block's owner tree. The key offset for the full back refs
1035 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001036 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037 * When implicit back refs is used, information about the lowest key and
1038 * level of the tree block are required. These information are stored in
1039 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -05001040 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001041
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001042#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1043static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
1044 struct btrfs_root *root,
1045 struct btrfs_path *path,
1046 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -05001047{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001048 struct btrfs_extent_item *item;
1049 struct btrfs_extent_item_v0 *ei0;
1050 struct btrfs_extent_ref_v0 *ref0;
1051 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -04001052 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001053 struct btrfs_key key;
1054 struct btrfs_key found_key;
1055 u32 new_size = sizeof(*item);
1056 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -05001057 int ret;
1058
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001059 leaf = path->nodes[0];
1060 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -05001061
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001062 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1063 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1064 struct btrfs_extent_item_v0);
1065 refs = btrfs_extent_refs_v0(leaf, ei0);
1066
1067 if (owner == (u64)-1) {
1068 while (1) {
1069 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1070 ret = btrfs_next_leaf(root, path);
1071 if (ret < 0)
1072 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001073 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001074 leaf = path->nodes[0];
1075 }
1076 btrfs_item_key_to_cpu(leaf, &found_key,
1077 path->slots[0]);
1078 BUG_ON(key.objectid != found_key.objectid);
1079 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1080 path->slots[0]++;
1081 continue;
1082 }
1083 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1084 struct btrfs_extent_ref_v0);
1085 owner = btrfs_ref_objectid_v0(leaf, ref0);
1086 break;
1087 }
1088 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001089 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001090
1091 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1092 new_size += sizeof(*bi);
1093
1094 new_size -= sizeof(*ei0);
1095 ret = btrfs_search_slot(trans, root, &key, path,
1096 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001097 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001098 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001099 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001100
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001101 btrfs_extend_item(root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001102
1103 leaf = path->nodes[0];
1104 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1105 btrfs_set_extent_refs(leaf, item, refs);
1106 /* FIXME: get real generation */
1107 btrfs_set_extent_generation(leaf, item, 0);
1108 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1109 btrfs_set_extent_flags(leaf, item,
1110 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1111 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1112 bi = (struct btrfs_tree_block_info *)(item + 1);
1113 /* FIXME: get first key of the block */
1114 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1115 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1116 } else {
1117 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1118 }
1119 btrfs_mark_buffer_dirty(leaf);
1120 return 0;
1121}
1122#endif
1123
1124static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1125{
1126 u32 high_crc = ~(u32)0;
1127 u32 low_crc = ~(u32)0;
1128 __le64 lenum;
1129
1130 lenum = cpu_to_le64(root_objectid);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001131 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001132 lenum = cpu_to_le64(owner);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001133 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001134 lenum = cpu_to_le64(offset);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001135 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001136
1137 return ((u64)high_crc << 31) ^ (u64)low_crc;
1138}
1139
1140static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1141 struct btrfs_extent_data_ref *ref)
1142{
1143 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1144 btrfs_extent_data_ref_objectid(leaf, ref),
1145 btrfs_extent_data_ref_offset(leaf, ref));
1146}
1147
1148static int match_extent_data_ref(struct extent_buffer *leaf,
1149 struct btrfs_extent_data_ref *ref,
1150 u64 root_objectid, u64 owner, u64 offset)
1151{
1152 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1153 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1154 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1155 return 0;
1156 return 1;
1157}
1158
1159static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1160 struct btrfs_root *root,
1161 struct btrfs_path *path,
1162 u64 bytenr, u64 parent,
1163 u64 root_objectid,
1164 u64 owner, u64 offset)
1165{
1166 struct btrfs_key key;
1167 struct btrfs_extent_data_ref *ref;
1168 struct extent_buffer *leaf;
1169 u32 nritems;
1170 int ret;
1171 int recow;
1172 int err = -ENOENT;
1173
1174 key.objectid = bytenr;
1175 if (parent) {
1176 key.type = BTRFS_SHARED_DATA_REF_KEY;
1177 key.offset = parent;
1178 } else {
1179 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1180 key.offset = hash_extent_data_ref(root_objectid,
1181 owner, offset);
1182 }
1183again:
1184 recow = 0;
1185 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1186 if (ret < 0) {
1187 err = ret;
1188 goto fail;
1189 }
1190
1191 if (parent) {
1192 if (!ret)
1193 return 0;
1194#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1195 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001196 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001197 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1198 if (ret < 0) {
1199 err = ret;
1200 goto fail;
1201 }
1202 if (!ret)
1203 return 0;
1204#endif
1205 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001206 }
1207
1208 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001209 nritems = btrfs_header_nritems(leaf);
1210 while (1) {
1211 if (path->slots[0] >= nritems) {
1212 ret = btrfs_next_leaf(root, path);
1213 if (ret < 0)
1214 err = ret;
1215 if (ret)
1216 goto fail;
1217
1218 leaf = path->nodes[0];
1219 nritems = btrfs_header_nritems(leaf);
1220 recow = 1;
1221 }
1222
1223 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1224 if (key.objectid != bytenr ||
1225 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1226 goto fail;
1227
1228 ref = btrfs_item_ptr(leaf, path->slots[0],
1229 struct btrfs_extent_data_ref);
1230
1231 if (match_extent_data_ref(leaf, ref, root_objectid,
1232 owner, offset)) {
1233 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001234 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001235 goto again;
1236 }
1237 err = 0;
1238 break;
1239 }
1240 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001241 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001242fail:
1243 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001244}
1245
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001246static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1247 struct btrfs_root *root,
1248 struct btrfs_path *path,
1249 u64 bytenr, u64 parent,
1250 u64 root_objectid, u64 owner,
1251 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001252{
1253 struct btrfs_key key;
1254 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001255 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001256 u32 num_refs;
1257 int ret;
1258
1259 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001260 if (parent) {
1261 key.type = BTRFS_SHARED_DATA_REF_KEY;
1262 key.offset = parent;
1263 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001264 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001265 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1266 key.offset = hash_extent_data_ref(root_objectid,
1267 owner, offset);
1268 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001269 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001270
1271 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1272 if (ret && ret != -EEXIST)
1273 goto fail;
1274
1275 leaf = path->nodes[0];
1276 if (parent) {
1277 struct btrfs_shared_data_ref *ref;
1278 ref = btrfs_item_ptr(leaf, path->slots[0],
1279 struct btrfs_shared_data_ref);
1280 if (ret == 0) {
1281 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1282 } else {
1283 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1284 num_refs += refs_to_add;
1285 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1286 }
1287 } else {
1288 struct btrfs_extent_data_ref *ref;
1289 while (ret == -EEXIST) {
1290 ref = btrfs_item_ptr(leaf, path->slots[0],
1291 struct btrfs_extent_data_ref);
1292 if (match_extent_data_ref(leaf, ref, root_objectid,
1293 owner, offset))
1294 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001295 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001296 key.offset++;
1297 ret = btrfs_insert_empty_item(trans, root, path, &key,
1298 size);
1299 if (ret && ret != -EEXIST)
1300 goto fail;
1301
1302 leaf = path->nodes[0];
1303 }
1304 ref = btrfs_item_ptr(leaf, path->slots[0],
1305 struct btrfs_extent_data_ref);
1306 if (ret == 0) {
1307 btrfs_set_extent_data_ref_root(leaf, ref,
1308 root_objectid);
1309 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1310 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1311 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1312 } else {
1313 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1314 num_refs += refs_to_add;
1315 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1316 }
1317 }
1318 btrfs_mark_buffer_dirty(leaf);
1319 ret = 0;
1320fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001321 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001322 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001323}
1324
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001325static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1326 struct btrfs_root *root,
1327 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -07001328 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001329{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001330 struct btrfs_key key;
1331 struct btrfs_extent_data_ref *ref1 = NULL;
1332 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001333 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001334 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001335 int ret = 0;
1336
1337 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001338 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1339
1340 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1341 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1342 struct btrfs_extent_data_ref);
1343 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1344 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1345 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1346 struct btrfs_shared_data_ref);
1347 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1348#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1349 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1350 struct btrfs_extent_ref_v0 *ref0;
1351 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1352 struct btrfs_extent_ref_v0);
1353 num_refs = btrfs_ref_count_v0(leaf, ref0);
1354#endif
1355 } else {
1356 BUG();
1357 }
1358
Chris Mason56bec292009-03-13 10:10:06 -04001359 BUG_ON(num_refs < refs_to_drop);
1360 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001361
Zheng Yan31840ae2008-09-23 13:14:14 -04001362 if (num_refs == 0) {
1363 ret = btrfs_del_item(trans, root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -07001364 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001365 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001366 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1367 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1368 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1369 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1370#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1371 else {
1372 struct btrfs_extent_ref_v0 *ref0;
1373 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1374 struct btrfs_extent_ref_v0);
1375 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1376 }
1377#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001378 btrfs_mark_buffer_dirty(leaf);
1379 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001380 return ret;
1381}
1382
Zhaolei9ed0dea2015-08-06 22:16:24 +08001383static noinline u32 extent_data_ref_count(struct btrfs_path *path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001384 struct btrfs_extent_inline_ref *iref)
1385{
1386 struct btrfs_key key;
1387 struct extent_buffer *leaf;
1388 struct btrfs_extent_data_ref *ref1;
1389 struct btrfs_shared_data_ref *ref2;
1390 u32 num_refs = 0;
1391
1392 leaf = path->nodes[0];
1393 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1394 if (iref) {
1395 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1396 BTRFS_EXTENT_DATA_REF_KEY) {
1397 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1398 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1399 } else {
1400 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1401 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1402 }
1403 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1404 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1405 struct btrfs_extent_data_ref);
1406 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1407 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1408 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1409 struct btrfs_shared_data_ref);
1410 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1411#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1412 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1413 struct btrfs_extent_ref_v0 *ref0;
1414 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1415 struct btrfs_extent_ref_v0);
1416 num_refs = btrfs_ref_count_v0(leaf, ref0);
1417#endif
1418 } else {
1419 WARN_ON(1);
1420 }
1421 return num_refs;
1422}
1423
1424static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1425 struct btrfs_root *root,
1426 struct btrfs_path *path,
1427 u64 bytenr, u64 parent,
1428 u64 root_objectid)
1429{
1430 struct btrfs_key key;
1431 int ret;
1432
1433 key.objectid = bytenr;
1434 if (parent) {
1435 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1436 key.offset = parent;
1437 } else {
1438 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1439 key.offset = root_objectid;
1440 }
1441
1442 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1443 if (ret > 0)
1444 ret = -ENOENT;
1445#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1446 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001447 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001448 key.type = BTRFS_EXTENT_REF_V0_KEY;
1449 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1450 if (ret > 0)
1451 ret = -ENOENT;
1452 }
1453#endif
1454 return ret;
1455}
1456
1457static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1458 struct btrfs_root *root,
1459 struct btrfs_path *path,
1460 u64 bytenr, u64 parent,
1461 u64 root_objectid)
1462{
1463 struct btrfs_key key;
1464 int ret;
1465
1466 key.objectid = bytenr;
1467 if (parent) {
1468 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1469 key.offset = parent;
1470 } else {
1471 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1472 key.offset = root_objectid;
1473 }
1474
1475 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001476 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001477 return ret;
1478}
1479
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001480static inline int extent_ref_type(u64 parent, u64 owner)
1481{
1482 int type;
1483 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1484 if (parent > 0)
1485 type = BTRFS_SHARED_BLOCK_REF_KEY;
1486 else
1487 type = BTRFS_TREE_BLOCK_REF_KEY;
1488 } else {
1489 if (parent > 0)
1490 type = BTRFS_SHARED_DATA_REF_KEY;
1491 else
1492 type = BTRFS_EXTENT_DATA_REF_KEY;
1493 }
1494 return type;
1495}
1496
Yan Zheng2c47e6052009-06-27 21:07:35 -04001497static int find_next_key(struct btrfs_path *path, int level,
1498 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001499
1500{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001501 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001502 if (!path->nodes[level])
1503 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001504 if (path->slots[level] + 1 >=
1505 btrfs_header_nritems(path->nodes[level]))
1506 continue;
1507 if (level == 0)
1508 btrfs_item_key_to_cpu(path->nodes[level], key,
1509 path->slots[level] + 1);
1510 else
1511 btrfs_node_key_to_cpu(path->nodes[level], key,
1512 path->slots[level] + 1);
1513 return 0;
1514 }
1515 return 1;
1516}
1517
1518/*
1519 * look for inline back ref. if back ref is found, *ref_ret is set
1520 * to the address of inline back ref, and 0 is returned.
1521 *
1522 * if back ref isn't found, *ref_ret is set to the address where it
1523 * should be inserted, and -ENOENT is returned.
1524 *
1525 * if insert is true and there are too many inline back refs, the path
1526 * points to the extent item, and -EAGAIN is returned.
1527 *
1528 * NOTE: inline back refs are ordered in the same way that back ref
1529 * items in the tree are ordered.
1530 */
1531static noinline_for_stack
1532int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1533 struct btrfs_root *root,
1534 struct btrfs_path *path,
1535 struct btrfs_extent_inline_ref **ref_ret,
1536 u64 bytenr, u64 num_bytes,
1537 u64 parent, u64 root_objectid,
1538 u64 owner, u64 offset, int insert)
1539{
1540 struct btrfs_key key;
1541 struct extent_buffer *leaf;
1542 struct btrfs_extent_item *ei;
1543 struct btrfs_extent_inline_ref *iref;
1544 u64 flags;
1545 u64 item_size;
1546 unsigned long ptr;
1547 unsigned long end;
1548 int extra_size;
1549 int type;
1550 int want;
1551 int ret;
1552 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001553 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1554 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001555
1556 key.objectid = bytenr;
1557 key.type = BTRFS_EXTENT_ITEM_KEY;
1558 key.offset = num_bytes;
1559
1560 want = extent_ref_type(parent, owner);
1561 if (insert) {
1562 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001563 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001564 } else
1565 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001566
1567 /*
1568 * Owner is our parent level, so we can just add one to get the level
1569 * for the block we are interested in.
1570 */
1571 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1572 key.type = BTRFS_METADATA_ITEM_KEY;
1573 key.offset = owner;
1574 }
1575
1576again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001577 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1578 if (ret < 0) {
1579 err = ret;
1580 goto out;
1581 }
Josef Bacik3173a182013-03-07 14:22:04 -05001582
1583 /*
1584 * We may be a newly converted file system which still has the old fat
1585 * extent entries for metadata, so try and see if we have one of those.
1586 */
1587 if (ret > 0 && skinny_metadata) {
1588 skinny_metadata = false;
1589 if (path->slots[0]) {
1590 path->slots[0]--;
1591 btrfs_item_key_to_cpu(path->nodes[0], &key,
1592 path->slots[0]);
1593 if (key.objectid == bytenr &&
1594 key.type == BTRFS_EXTENT_ITEM_KEY &&
1595 key.offset == num_bytes)
1596 ret = 0;
1597 }
1598 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +01001599 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05001600 key.type = BTRFS_EXTENT_ITEM_KEY;
1601 key.offset = num_bytes;
1602 btrfs_release_path(path);
1603 goto again;
1604 }
1605 }
1606
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001607 if (ret && !insert) {
1608 err = -ENOENT;
1609 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301610 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -05001611 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -05001612 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001613 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001614
1615 leaf = path->nodes[0];
1616 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1617#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1618 if (item_size < sizeof(*ei)) {
1619 if (!insert) {
1620 err = -ENOENT;
1621 goto out;
1622 }
1623 ret = convert_extent_item_v0(trans, root, path, owner,
1624 extra_size);
1625 if (ret < 0) {
1626 err = ret;
1627 goto out;
1628 }
1629 leaf = path->nodes[0];
1630 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1631 }
1632#endif
1633 BUG_ON(item_size < sizeof(*ei));
1634
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001635 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1636 flags = btrfs_extent_flags(leaf, ei);
1637
1638 ptr = (unsigned long)(ei + 1);
1639 end = (unsigned long)ei + item_size;
1640
Josef Bacik3173a182013-03-07 14:22:04 -05001641 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001642 ptr += sizeof(struct btrfs_tree_block_info);
1643 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001644 }
1645
1646 err = -ENOENT;
1647 while (1) {
1648 if (ptr >= end) {
1649 WARN_ON(ptr > end);
1650 break;
1651 }
1652 iref = (struct btrfs_extent_inline_ref *)ptr;
1653 type = btrfs_extent_inline_ref_type(leaf, iref);
1654 if (want < type)
1655 break;
1656 if (want > type) {
1657 ptr += btrfs_extent_inline_ref_size(type);
1658 continue;
1659 }
1660
1661 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1662 struct btrfs_extent_data_ref *dref;
1663 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1664 if (match_extent_data_ref(leaf, dref, root_objectid,
1665 owner, offset)) {
1666 err = 0;
1667 break;
1668 }
1669 if (hash_extent_data_ref_item(leaf, dref) <
1670 hash_extent_data_ref(root_objectid, owner, offset))
1671 break;
1672 } else {
1673 u64 ref_offset;
1674 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1675 if (parent > 0) {
1676 if (parent == ref_offset) {
1677 err = 0;
1678 break;
1679 }
1680 if (ref_offset < parent)
1681 break;
1682 } else {
1683 if (root_objectid == ref_offset) {
1684 err = 0;
1685 break;
1686 }
1687 if (ref_offset < root_objectid)
1688 break;
1689 }
1690 }
1691 ptr += btrfs_extent_inline_ref_size(type);
1692 }
1693 if (err == -ENOENT && insert) {
1694 if (item_size + extra_size >=
1695 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1696 err = -EAGAIN;
1697 goto out;
1698 }
1699 /*
1700 * To add new inline back ref, we have to make sure
1701 * there is no corresponding back ref item.
1702 * For simplicity, we just do not add new inline back
1703 * ref if there is any kind of item for this block
1704 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001705 if (find_next_key(path, 0, &key) == 0 &&
1706 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001707 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001708 err = -EAGAIN;
1709 goto out;
1710 }
1711 }
1712 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1713out:
Yan Zheng85d41982009-06-11 08:51:10 -04001714 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001715 path->keep_locks = 0;
1716 btrfs_unlock_up_safe(path, 1);
1717 }
1718 return err;
1719}
1720
1721/*
1722 * helper to add new inline back ref
1723 */
1724static noinline_for_stack
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001725void setup_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001726 struct btrfs_path *path,
1727 struct btrfs_extent_inline_ref *iref,
1728 u64 parent, u64 root_objectid,
1729 u64 owner, u64 offset, int refs_to_add,
1730 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001731{
1732 struct extent_buffer *leaf;
1733 struct btrfs_extent_item *ei;
1734 unsigned long ptr;
1735 unsigned long end;
1736 unsigned long item_offset;
1737 u64 refs;
1738 int size;
1739 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001740
1741 leaf = path->nodes[0];
1742 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1743 item_offset = (unsigned long)iref - (unsigned long)ei;
1744
1745 type = extent_ref_type(parent, owner);
1746 size = btrfs_extent_inline_ref_size(type);
1747
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001748 btrfs_extend_item(root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001749
1750 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1751 refs = btrfs_extent_refs(leaf, ei);
1752 refs += refs_to_add;
1753 btrfs_set_extent_refs(leaf, ei, refs);
1754 if (extent_op)
1755 __run_delayed_extent_op(extent_op, leaf, ei);
1756
1757 ptr = (unsigned long)ei + item_offset;
1758 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1759 if (ptr < end - size)
1760 memmove_extent_buffer(leaf, ptr + size, ptr,
1761 end - size - ptr);
1762
1763 iref = (struct btrfs_extent_inline_ref *)ptr;
1764 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1765 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1766 struct btrfs_extent_data_ref *dref;
1767 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1768 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1769 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1770 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1771 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1772 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1773 struct btrfs_shared_data_ref *sref;
1774 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1775 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1776 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1777 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1778 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1779 } else {
1780 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1781 }
1782 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001783}
1784
1785static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1786 struct btrfs_root *root,
1787 struct btrfs_path *path,
1788 struct btrfs_extent_inline_ref **ref_ret,
1789 u64 bytenr, u64 num_bytes, u64 parent,
1790 u64 root_objectid, u64 owner, u64 offset)
1791{
1792 int ret;
1793
1794 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1795 bytenr, num_bytes, parent,
1796 root_objectid, owner, offset, 0);
1797 if (ret != -ENOENT)
1798 return ret;
1799
David Sterbab3b4aa72011-04-21 01:20:15 +02001800 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001801 *ref_ret = NULL;
1802
1803 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1804 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1805 root_objectid);
1806 } else {
1807 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1808 root_objectid, owner, offset);
1809 }
1810 return ret;
1811}
1812
1813/*
1814 * helper to update/remove inline back ref
1815 */
1816static noinline_for_stack
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001817void update_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001818 struct btrfs_path *path,
1819 struct btrfs_extent_inline_ref *iref,
1820 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001821 struct btrfs_delayed_extent_op *extent_op,
1822 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001823{
1824 struct extent_buffer *leaf;
1825 struct btrfs_extent_item *ei;
1826 struct btrfs_extent_data_ref *dref = NULL;
1827 struct btrfs_shared_data_ref *sref = NULL;
1828 unsigned long ptr;
1829 unsigned long end;
1830 u32 item_size;
1831 int size;
1832 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001833 u64 refs;
1834
1835 leaf = path->nodes[0];
1836 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1837 refs = btrfs_extent_refs(leaf, ei);
1838 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1839 refs += refs_to_mod;
1840 btrfs_set_extent_refs(leaf, ei, refs);
1841 if (extent_op)
1842 __run_delayed_extent_op(extent_op, leaf, ei);
1843
1844 type = btrfs_extent_inline_ref_type(leaf, iref);
1845
1846 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1847 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1848 refs = btrfs_extent_data_ref_count(leaf, dref);
1849 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1850 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1851 refs = btrfs_shared_data_ref_count(leaf, sref);
1852 } else {
1853 refs = 1;
1854 BUG_ON(refs_to_mod != -1);
1855 }
1856
1857 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1858 refs += refs_to_mod;
1859
1860 if (refs > 0) {
1861 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1862 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1863 else
1864 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1865 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001866 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001867 size = btrfs_extent_inline_ref_size(type);
1868 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1869 ptr = (unsigned long)iref;
1870 end = (unsigned long)ei + item_size;
1871 if (ptr + size < end)
1872 memmove_extent_buffer(leaf, ptr, ptr + size,
1873 end - ptr - size);
1874 item_size -= size;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001875 btrfs_truncate_item(root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001876 }
1877 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001878}
1879
1880static noinline_for_stack
1881int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1882 struct btrfs_root *root,
1883 struct btrfs_path *path,
1884 u64 bytenr, u64 num_bytes, u64 parent,
1885 u64 root_objectid, u64 owner,
1886 u64 offset, int refs_to_add,
1887 struct btrfs_delayed_extent_op *extent_op)
1888{
1889 struct btrfs_extent_inline_ref *iref;
1890 int ret;
1891
1892 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1893 bytenr, num_bytes, parent,
1894 root_objectid, owner, offset, 1);
1895 if (ret == 0) {
1896 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001897 update_inline_extent_backref(root, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001898 refs_to_add, extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001899 } else if (ret == -ENOENT) {
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001900 setup_inline_extent_backref(root, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001901 root_objectid, owner, offset,
1902 refs_to_add, extent_op);
1903 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001904 }
1905 return ret;
1906}
1907
1908static int insert_extent_backref(struct btrfs_trans_handle *trans,
1909 struct btrfs_root *root,
1910 struct btrfs_path *path,
1911 u64 bytenr, u64 parent, u64 root_objectid,
1912 u64 owner, u64 offset, int refs_to_add)
1913{
1914 int ret;
1915 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1916 BUG_ON(refs_to_add != 1);
1917 ret = insert_tree_block_ref(trans, root, path, bytenr,
1918 parent, root_objectid);
1919 } else {
1920 ret = insert_extent_data_ref(trans, root, path, bytenr,
1921 parent, root_objectid,
1922 owner, offset, refs_to_add);
1923 }
1924 return ret;
1925}
1926
1927static int remove_extent_backref(struct btrfs_trans_handle *trans,
1928 struct btrfs_root *root,
1929 struct btrfs_path *path,
1930 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001931 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001932{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001933 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001934
1935 BUG_ON(!is_data && refs_to_drop != 1);
1936 if (iref) {
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001937 update_inline_extent_backref(root, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001938 -refs_to_drop, NULL, last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001939 } else if (is_data) {
Josef Bacikfcebe452014-05-13 17:30:47 -07001940 ret = remove_extent_data_ref(trans, root, path, refs_to_drop,
1941 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001942 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001943 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001944 ret = btrfs_del_item(trans, root, path);
1945 }
1946 return ret;
1947}
1948
Jeff Mahoney86557862015-06-15 09:41:16 -04001949#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001950static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
1951 u64 *discarded_bytes)
Chris Mason15916de2008-11-19 21:17:22 -05001952{
Jeff Mahoney86557862015-06-15 09:41:16 -04001953 int j, ret = 0;
1954 u64 bytes_left, end;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04001955 u64 aligned_start = ALIGN(start, 1 << 9);
1956
1957 if (WARN_ON(start != aligned_start)) {
1958 len -= aligned_start - start;
1959 len = round_down(len, 1 << 9);
1960 start = aligned_start;
1961 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04001962
1963 *discarded_bytes = 0;
Jeff Mahoney86557862015-06-15 09:41:16 -04001964
1965 if (!len)
1966 return 0;
1967
1968 end = start + len;
1969 bytes_left = len;
1970
1971 /* Skip any superblocks on this device. */
1972 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
1973 u64 sb_start = btrfs_sb_offset(j);
1974 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
1975 u64 size = sb_start - start;
1976
1977 if (!in_range(sb_start, start, bytes_left) &&
1978 !in_range(sb_end, start, bytes_left) &&
1979 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
1980 continue;
1981
1982 /*
1983 * Superblock spans beginning of range. Adjust start and
1984 * try again.
1985 */
1986 if (sb_start <= start) {
1987 start += sb_end - start;
1988 if (start > end) {
1989 bytes_left = 0;
1990 break;
1991 }
1992 bytes_left = end - start;
1993 continue;
1994 }
1995
1996 if (size) {
1997 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
1998 GFP_NOFS, 0);
1999 if (!ret)
2000 *discarded_bytes += size;
2001 else if (ret != -EOPNOTSUPP)
2002 return ret;
2003 }
2004
2005 start = sb_end;
2006 if (start > end) {
2007 bytes_left = 0;
2008 break;
2009 }
2010 bytes_left = end - start;
2011 }
2012
2013 if (bytes_left) {
2014 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002015 GFP_NOFS, 0);
2016 if (!ret)
Jeff Mahoney86557862015-06-15 09:41:16 -04002017 *discarded_bytes += bytes_left;
Jeff Mahoney4d89d372015-06-15 09:41:15 -04002018 }
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002019 return ret;
Chris Mason15916de2008-11-19 21:17:22 -05002020}
Chris Mason15916de2008-11-19 21:17:22 -05002021
Filipe Manana1edb647b2014-12-08 14:01:12 +00002022int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
2023 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05002024{
Liu Hui1f3c79a2009-01-05 15:57:51 -05002025 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00002026 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02002027 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002028
Christoph Hellwige244a0a2009-10-14 09:24:59 -04002029
Liu Hui1f3c79a2009-01-05 15:57:51 -05002030 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002031 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02002032 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002033 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05002034 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02002035 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002036 int i;
2037
Liu Hui1f3c79a2009-01-05 15:57:51 -05002038
Jan Schmidta1d3c472011-08-04 17:15:33 +02002039 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002040 u64 bytes;
Josef Bacikd5e20032011-08-04 14:52:27 +00002041 if (!stripe->dev->can_discard)
2042 continue;
2043
Li Dongyang5378e602011-03-24 10:24:27 +00002044 ret = btrfs_issue_discard(stripe->dev->bdev,
2045 stripe->physical,
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002046 stripe->length,
2047 &bytes);
Li Dongyang5378e602011-03-24 10:24:27 +00002048 if (!ret)
Jeff Mahoneyd04c6b82015-06-15 09:41:14 -04002049 discarded_bytes += bytes;
Li Dongyang5378e602011-03-24 10:24:27 +00002050 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002051 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00002052
2053 /*
2054 * Just in case we get back EOPNOTSUPP for some reason,
2055 * just ignore the return value so we don't screw up
2056 * people calling discard_extent.
2057 */
2058 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002059 }
Zhao Lei6e9606d2015-01-20 15:11:34 +08002060 btrfs_put_bbio(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002061 }
Li Dongyang5378e602011-03-24 10:24:27 +00002062
2063 if (actual_bytes)
2064 *actual_bytes = discarded_bytes;
2065
Liu Hui1f3c79a2009-01-05 15:57:51 -05002066
David Woodhouse53b381b2013-01-29 18:40:14 -05002067 if (ret == -EOPNOTSUPP)
2068 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002069 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002070}
2071
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002072/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002073int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2074 struct btrfs_root *root,
2075 u64 bytenr, u64 num_bytes, u64 parent,
Josef Bacikfcebe452014-05-13 17:30:47 -07002076 u64 root_objectid, u64 owner, u64 offset,
2077 int no_quota)
Zheng Yan31840ae2008-09-23 13:14:14 -04002078{
2079 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002080 struct btrfs_fs_info *fs_info = root->fs_info;
2081
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002082 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
2083 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04002084
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002085 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002086 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
2087 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002088 parent, root_objectid, (int)owner,
Josef Bacikfcebe452014-05-13 17:30:47 -07002089 BTRFS_ADD_DELAYED_REF, NULL, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002090 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002091 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
2092 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002093 parent, root_objectid, owner, offset,
Josef Bacikfcebe452014-05-13 17:30:47 -07002094 BTRFS_ADD_DELAYED_REF, NULL, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002095 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002096 return ret;
2097}
2098
Chris Mason925baed2008-06-25 16:01:30 -04002099static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002100 struct btrfs_root *root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002101 struct btrfs_delayed_ref_node *node,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002102 u64 parent, u64 root_objectid,
2103 u64 owner, u64 offset, int refs_to_add,
2104 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04002105{
Josef Bacikfcebe452014-05-13 17:30:47 -07002106 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04002107 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002108 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04002109 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07002110 struct btrfs_key key;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002111 u64 bytenr = node->bytenr;
2112 u64 num_bytes = node->num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002113 u64 refs;
2114 int ret;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002115 int no_quota = node->no_quota;
Chris Mason037e6392007-03-07 11:50:24 -05002116
Chris Mason5caf2a02007-04-02 11:20:42 -04002117 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002118 if (!path)
2119 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04002120
Josef Bacikfcebe452014-05-13 17:30:47 -07002121 if (!is_fstree(root_objectid) || !root->fs_info->quota_enabled)
2122 no_quota = 1;
2123
Chris Mason3c12ac72008-04-21 12:01:38 -04002124 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002125 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002126 /* this will setup the path even if it fails to insert the back ref */
Josef Bacikfcebe452014-05-13 17:30:47 -07002127 ret = insert_inline_extent_backref(trans, fs_info->extent_root, path,
2128 bytenr, num_bytes, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002129 root_objectid, owner, offset,
2130 refs_to_add, extent_op);
Qu Wenruo0ed47922015-04-16 16:55:08 +08002131 if ((ret < 0 && ret != -EAGAIN) || !ret)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002132 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07002133
2134 /*
2135 * Ok we had -EAGAIN which means we didn't have space to insert and
2136 * inline extent ref, so just update the reference count and add a
2137 * normal backref.
2138 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002139 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07002140 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002141 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2142 refs = btrfs_extent_refs(leaf, item);
2143 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2144 if (extent_op)
2145 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002146
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002147 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002148 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002149
Chris Mason3c12ac72008-04-21 12:01:38 -04002150 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002151 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04002152 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002153 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002154 path, bytenr, parent, root_objectid,
2155 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002156 if (ret)
2157 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002158out:
Chris Mason74493f72007-12-11 09:25:06 -05002159 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08002160 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002161}
2162
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002163static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2164 struct btrfs_root *root,
2165 struct btrfs_delayed_ref_node *node,
2166 struct btrfs_delayed_extent_op *extent_op,
2167 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002168{
Chris Mason56bec292009-03-13 10:10:06 -04002169 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002170 struct btrfs_delayed_data_ref *ref;
2171 struct btrfs_key ins;
2172 u64 parent = 0;
2173 u64 ref_root = 0;
2174 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002175
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002176 ins.objectid = node->bytenr;
2177 ins.offset = node->num_bytes;
2178 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002179
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002180 ref = btrfs_delayed_node_to_data_ref(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002181 trace_run_delayed_data_ref(node, ref, node->action);
2182
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002183 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2184 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002185 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002186
2187 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002188 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002189 flags |= extent_op->flags_to_set;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002190 ret = alloc_reserved_file_extent(trans, root,
2191 parent, ref_root, flags,
2192 ref->objectid, ref->offset,
2193 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002194 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002195 ret = __btrfs_inc_extent_ref(trans, root, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002196 ref_root, ref->objectid,
2197 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002198 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002199 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002200 ret = __btrfs_free_extent(trans, root, node, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002201 ref_root, ref->objectid,
2202 ref->offset, node->ref_mod,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002203 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002204 } else {
2205 BUG();
2206 }
Chris Mason56bec292009-03-13 10:10:06 -04002207 return ret;
2208}
2209
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002210static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2211 struct extent_buffer *leaf,
2212 struct btrfs_extent_item *ei)
2213{
2214 u64 flags = btrfs_extent_flags(leaf, ei);
2215 if (extent_op->update_flags) {
2216 flags |= extent_op->flags_to_set;
2217 btrfs_set_extent_flags(leaf, ei, flags);
2218 }
2219
2220 if (extent_op->update_key) {
2221 struct btrfs_tree_block_info *bi;
2222 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2223 bi = (struct btrfs_tree_block_info *)(ei + 1);
2224 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2225 }
2226}
2227
2228static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2229 struct btrfs_root *root,
2230 struct btrfs_delayed_ref_node *node,
2231 struct btrfs_delayed_extent_op *extent_op)
2232{
2233 struct btrfs_key key;
2234 struct btrfs_path *path;
2235 struct btrfs_extent_item *ei;
2236 struct extent_buffer *leaf;
2237 u32 item_size;
2238 int ret;
2239 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002240 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002241
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002242 if (trans->aborted)
2243 return 0;
2244
Josef Bacik3173a182013-03-07 14:22:04 -05002245 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2246 metadata = 0;
2247
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002248 path = btrfs_alloc_path();
2249 if (!path)
2250 return -ENOMEM;
2251
2252 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002253
Josef Bacik3173a182013-03-07 14:22:04 -05002254 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002255 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002256 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002257 } else {
2258 key.type = BTRFS_EXTENT_ITEM_KEY;
2259 key.offset = node->num_bytes;
2260 }
2261
2262again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002263 path->reada = 1;
2264 path->leave_spinning = 1;
2265 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2266 path, 0, 1);
2267 if (ret < 0) {
2268 err = ret;
2269 goto out;
2270 }
2271 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002272 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002273 if (path->slots[0] > 0) {
2274 path->slots[0]--;
2275 btrfs_item_key_to_cpu(path->nodes[0], &key,
2276 path->slots[0]);
2277 if (key.objectid == node->bytenr &&
2278 key.type == BTRFS_EXTENT_ITEM_KEY &&
2279 key.offset == node->num_bytes)
2280 ret = 0;
2281 }
2282 if (ret > 0) {
2283 btrfs_release_path(path);
2284 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002285
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002286 key.objectid = node->bytenr;
2287 key.offset = node->num_bytes;
2288 key.type = BTRFS_EXTENT_ITEM_KEY;
2289 goto again;
2290 }
2291 } else {
2292 err = -EIO;
2293 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002294 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002295 }
2296
2297 leaf = path->nodes[0];
2298 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2299#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2300 if (item_size < sizeof(*ei)) {
2301 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2302 path, (u64)-1, 0);
2303 if (ret < 0) {
2304 err = ret;
2305 goto out;
2306 }
2307 leaf = path->nodes[0];
2308 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2309 }
2310#endif
2311 BUG_ON(item_size < sizeof(*ei));
2312 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2313 __run_delayed_extent_op(extent_op, leaf, ei);
2314
2315 btrfs_mark_buffer_dirty(leaf);
2316out:
2317 btrfs_free_path(path);
2318 return err;
2319}
2320
2321static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2322 struct btrfs_root *root,
2323 struct btrfs_delayed_ref_node *node,
2324 struct btrfs_delayed_extent_op *extent_op,
2325 int insert_reserved)
2326{
2327 int ret = 0;
2328 struct btrfs_delayed_tree_ref *ref;
2329 struct btrfs_key ins;
2330 u64 parent = 0;
2331 u64 ref_root = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002332 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2333 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002334
2335 ref = btrfs_delayed_node_to_tree_ref(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002336 trace_run_delayed_tree_ref(node, ref, node->action);
2337
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002338 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2339 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002340 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002341
Josef Bacik3173a182013-03-07 14:22:04 -05002342 ins.objectid = node->bytenr;
2343 if (skinny_metadata) {
2344 ins.offset = ref->level;
2345 ins.type = BTRFS_METADATA_ITEM_KEY;
2346 } else {
2347 ins.offset = node->num_bytes;
2348 ins.type = BTRFS_EXTENT_ITEM_KEY;
2349 }
2350
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002351 BUG_ON(node->ref_mod != 1);
2352 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002353 BUG_ON(!extent_op || !extent_op->update_flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002354 ret = alloc_reserved_tree_block(trans, root,
2355 parent, ref_root,
2356 extent_op->flags_to_set,
2357 &extent_op->key,
Josef Bacikfcebe452014-05-13 17:30:47 -07002358 ref->level, &ins,
2359 node->no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002360 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002361 ret = __btrfs_inc_extent_ref(trans, root, node,
2362 parent, ref_root,
2363 ref->level, 0, 1,
Josef Bacikfcebe452014-05-13 17:30:47 -07002364 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002365 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
Qu Wenruoc682f9b2015-03-17 16:59:47 +08002366 ret = __btrfs_free_extent(trans, root, node,
2367 parent, ref_root,
2368 ref->level, 0, 1, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002369 } else {
2370 BUG();
2371 }
2372 return ret;
2373}
2374
Chris Mason56bec292009-03-13 10:10:06 -04002375/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002376static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2377 struct btrfs_root *root,
2378 struct btrfs_delayed_ref_node *node,
2379 struct btrfs_delayed_extent_op *extent_op,
2380 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002381{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002382 int ret = 0;
2383
Josef Bacik857cc2f2013-10-07 15:21:08 -04002384 if (trans->aborted) {
2385 if (insert_reserved)
2386 btrfs_pin_extent(root, node->bytenr,
2387 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002388 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002389 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002390
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002391 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002392 struct btrfs_delayed_ref_head *head;
2393 /*
2394 * we've hit the end of the chain and we were supposed
2395 * to insert this extent into the tree. But, it got
2396 * deleted before we ever needed to insert it, so all
2397 * we have to do is clean up the accounting
2398 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002399 BUG_ON(extent_op);
2400 head = btrfs_delayed_node_to_head(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002401 trace_run_delayed_ref_head(node, head, node->action);
2402
Chris Mason56bec292009-03-13 10:10:06 -04002403 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002404 btrfs_pin_extent(root, node->bytenr,
2405 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002406 if (head->is_data) {
2407 ret = btrfs_del_csums(trans, root,
2408 node->bytenr,
2409 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002410 }
Chris Mason56bec292009-03-13 10:10:06 -04002411 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002412 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002413 }
Josef Bacikeb099672009-02-12 09:27:38 -05002414
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002415 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2416 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2417 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2418 insert_reserved);
2419 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2420 node->type == BTRFS_SHARED_DATA_REF_KEY)
2421 ret = run_delayed_data_ref(trans, root, node, extent_op,
2422 insert_reserved);
2423 else
2424 BUG();
2425 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002426}
2427
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002428static inline struct btrfs_delayed_ref_node *
Chris Mason56bec292009-03-13 10:10:06 -04002429select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002430{
Filipe Mananacffc3372015-07-09 13:13:44 +01002431 struct btrfs_delayed_ref_node *ref;
2432
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002433 if (list_empty(&head->ref_list))
2434 return NULL;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002435
Filipe Mananacffc3372015-07-09 13:13:44 +01002436 /*
2437 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
2438 * This is to prevent a ref count from going down to zero, which deletes
2439 * the extent item from the extent tree, when there still are references
2440 * to add, which would fail because they would not find the extent item.
2441 */
2442 list_for_each_entry(ref, &head->ref_list, list) {
2443 if (ref->action == BTRFS_ADD_DELAYED_REF)
2444 return ref;
2445 }
2446
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002447 return list_entry(head->ref_list.next, struct btrfs_delayed_ref_node,
2448 list);
Chris Mason56bec292009-03-13 10:10:06 -04002449}
2450
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002451/*
2452 * Returns 0 on success or if called with an already aborted transaction.
2453 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2454 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002455static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2456 struct btrfs_root *root,
2457 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002458{
Chris Mason56bec292009-03-13 10:10:06 -04002459 struct btrfs_delayed_ref_root *delayed_refs;
2460 struct btrfs_delayed_ref_node *ref;
2461 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002462 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002463 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002464 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002465 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002466 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002467 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002468 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002469
2470 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002471 while (1) {
2472 if (!locked_ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002473 if (count >= nr)
Chris Mason56bec292009-03-13 10:10:06 -04002474 break;
Chris Mason56bec292009-03-13 10:10:06 -04002475
Josef Bacikd7df2c72014-01-23 09:21:38 -05002476 spin_lock(&delayed_refs->lock);
2477 locked_ref = btrfs_select_ref_head(trans);
2478 if (!locked_ref) {
2479 spin_unlock(&delayed_refs->lock);
2480 break;
2481 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002482
2483 /* grab the lock that says we are going to process
2484 * all the refs for this head */
2485 ret = btrfs_delayed_ref_lock(trans, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002486 spin_unlock(&delayed_refs->lock);
Chris Masonc3e69d52009-03-13 10:17:05 -04002487 /*
2488 * we may have dropped the spin lock to get the head
2489 * mutex lock, and that might have given someone else
2490 * time to free the head. If that's true, it has been
2491 * removed from our list and we can move on.
2492 */
2493 if (ret == -EAGAIN) {
2494 locked_ref = NULL;
2495 count++;
2496 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002497 }
2498 }
2499
Josef Bacikd7df2c72014-01-23 09:21:38 -05002500 spin_lock(&locked_ref->lock);
Josef Bacikae1e2062012-08-07 16:00:32 -04002501
2502 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002503 * locked_ref is the head node, so we have to go one
2504 * node back for any delayed ref updates
2505 */
2506 ref = select_delayed_ref(locked_ref);
2507
2508 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002509 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002510 spin_unlock(&locked_ref->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002511 btrfs_delayed_ref_unlock(locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002512 spin_lock(&delayed_refs->lock);
2513 locked_ref->processing = 0;
Arne Jansend1270cd2011-09-13 15:16:43 +02002514 delayed_refs->num_heads_ready++;
2515 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002516 locked_ref = NULL;
Arne Jansend1270cd2011-09-13 15:16:43 +02002517 cond_resched();
Josef Bacik27a377d2014-02-07 13:57:59 -05002518 count++;
Arne Jansend1270cd2011-09-13 15:16:43 +02002519 continue;
2520 }
2521
2522 /*
Chris Mason56bec292009-03-13 10:10:06 -04002523 * record the must insert reserved flag before we
2524 * drop the spin lock.
2525 */
2526 must_insert_reserved = locked_ref->must_insert_reserved;
2527 locked_ref->must_insert_reserved = 0;
2528
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002529 extent_op = locked_ref->extent_op;
2530 locked_ref->extent_op = NULL;
2531
Chris Mason56bec292009-03-13 10:10:06 -04002532 if (!ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002533
2534
Chris Mason56bec292009-03-13 10:10:06 -04002535 /* All delayed refs have been processed, Go ahead
2536 * and send the head node to run_one_delayed_ref,
2537 * so that any accounting fixes can happen
2538 */
2539 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002540
2541 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002542 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002543 extent_op = NULL;
2544 }
2545
2546 if (extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002547 spin_unlock(&locked_ref->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002548 ret = run_delayed_extent_op(trans, root,
2549 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002550 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002551
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002552 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04002553 /*
2554 * Need to reset must_insert_reserved if
2555 * there was an error so the abort stuff
2556 * can cleanup the reserved space
2557 * properly.
2558 */
2559 if (must_insert_reserved)
2560 locked_ref->must_insert_reserved = 1;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002561 locked_ref->processing = 0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002562 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
Miao Xie093486c2012-12-19 08:10:10 +00002563 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002564 return ret;
2565 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002566 continue;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002567 }
Chris Mason56bec292009-03-13 10:10:06 -04002568
Josef Bacikd7df2c72014-01-23 09:21:38 -05002569 /*
2570 * Need to drop our head ref lock and re-aqcuire the
2571 * delayed ref lock and then re-check to make sure
2572 * nobody got added.
2573 */
2574 spin_unlock(&locked_ref->lock);
2575 spin_lock(&delayed_refs->lock);
2576 spin_lock(&locked_ref->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002577 if (!list_empty(&locked_ref->ref_list) ||
Josef Bacik573a0752014-03-27 19:41:34 -04002578 locked_ref->extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002579 spin_unlock(&locked_ref->lock);
2580 spin_unlock(&delayed_refs->lock);
2581 continue;
2582 }
2583 ref->in_tree = 0;
2584 delayed_refs->num_heads--;
Liu Boc46effa2013-10-14 12:59:45 +08002585 rb_erase(&locked_ref->href_node,
2586 &delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002587 spin_unlock(&delayed_refs->lock);
2588 } else {
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002589 actual_count++;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002590 ref->in_tree = 0;
Qu Wenruoc6fc2452015-03-30 17:03:00 +08002591 list_del(&ref->list);
Liu Boc46effa2013-10-14 12:59:45 +08002592 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002593 atomic_dec(&delayed_refs->num_entries);
2594
Miao Xie093486c2012-12-19 08:10:10 +00002595 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002596 /*
2597 * when we play the delayed ref, also correct the
2598 * ref_mod on head
2599 */
2600 switch (ref->action) {
2601 case BTRFS_ADD_DELAYED_REF:
2602 case BTRFS_ADD_DELAYED_EXTENT:
2603 locked_ref->node.ref_mod -= ref->ref_mod;
2604 break;
2605 case BTRFS_DROP_DELAYED_REF:
2606 locked_ref->node.ref_mod += ref->ref_mod;
2607 break;
2608 default:
2609 WARN_ON(1);
2610 }
2611 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002612 spin_unlock(&locked_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002613
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002614 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002615 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002616
Miao Xie78a61842012-11-21 02:21:28 +00002617 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002618 if (ret) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002619 locked_ref->processing = 0;
Miao Xie093486c2012-12-19 08:10:10 +00002620 btrfs_delayed_ref_unlock(locked_ref);
2621 btrfs_put_delayed_ref(ref);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002622 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002623 return ret;
2624 }
2625
Miao Xie093486c2012-12-19 08:10:10 +00002626 /*
2627 * If this node is a head, that means all the refs in this head
2628 * have been dealt with, and we will pick the next head to deal
2629 * with, so we must unlock the head and drop it from the cluster
2630 * list before we release it.
2631 */
2632 if (btrfs_delayed_ref_is_head(ref)) {
Josef Bacik12621332015-02-03 07:50:16 -08002633 if (locked_ref->is_data &&
2634 locked_ref->total_ref_mod < 0) {
2635 spin_lock(&delayed_refs->lock);
2636 delayed_refs->pending_csums -= ref->num_bytes;
2637 spin_unlock(&delayed_refs->lock);
2638 }
Miao Xie093486c2012-12-19 08:10:10 +00002639 btrfs_delayed_ref_unlock(locked_ref);
2640 locked_ref = NULL;
2641 }
2642 btrfs_put_delayed_ref(ref);
2643 count++;
Chris Mason1887be62009-03-13 10:11:24 -04002644 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002645 }
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002646
2647 /*
2648 * We don't want to include ref heads since we can have empty ref heads
2649 * and those will drastically skew our runtime down since we just do
2650 * accounting, no actual extent tree updates.
2651 */
2652 if (actual_count > 0) {
2653 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2654 u64 avg;
2655
2656 /*
2657 * We weigh the current average higher than our current runtime
2658 * to avoid large swings in the average.
2659 */
2660 spin_lock(&delayed_refs->lock);
2661 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
David Sterbaf8c269d2015-01-16 17:21:12 +01002662 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002663 spin_unlock(&delayed_refs->lock);
2664 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002665 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002666}
2667
Arne Jansen709c0482011-09-12 12:22:57 +02002668#ifdef SCRAMBLE_DELAYED_REFS
2669/*
2670 * Normally delayed refs get processed in ascending bytenr order. This
2671 * correlates in most cases to the order added. To expose dependencies on this
2672 * order, we start to process the tree in the middle instead of the beginning
2673 */
2674static u64 find_middle(struct rb_root *root)
2675{
2676 struct rb_node *n = root->rb_node;
2677 struct btrfs_delayed_ref_node *entry;
2678 int alt = 1;
2679 u64 middle;
2680 u64 first = 0, last = 0;
2681
2682 n = rb_first(root);
2683 if (n) {
2684 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2685 first = entry->bytenr;
2686 }
2687 n = rb_last(root);
2688 if (n) {
2689 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2690 last = entry->bytenr;
2691 }
2692 n = root->rb_node;
2693
2694 while (n) {
2695 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2696 WARN_ON(!entry->in_tree);
2697
2698 middle = entry->bytenr;
2699
2700 if (alt)
2701 n = n->rb_left;
2702 else
2703 n = n->rb_right;
2704
2705 alt = 1 - alt;
2706 }
2707 return middle;
2708}
2709#endif
2710
Josef Bacik1be41b72013-06-12 13:56:06 -04002711static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2712{
2713 u64 num_bytes;
2714
2715 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2716 sizeof(struct btrfs_extent_inline_ref));
2717 if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2718 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2719
2720 /*
2721 * We don't ever fill up leaves all the way so multiply by 2 just to be
2722 * closer to what we're really going to want to ouse.
2723 */
David Sterbaf8c269d2015-01-16 17:21:12 +01002724 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
Josef Bacik1be41b72013-06-12 13:56:06 -04002725}
2726
Josef Bacik12621332015-02-03 07:50:16 -08002727/*
2728 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2729 * would require to store the csums for that many bytes.
2730 */
Chris Mason28f75a02015-02-04 06:59:29 -08002731u64 btrfs_csum_bytes_to_leaves(struct btrfs_root *root, u64 csum_bytes)
Josef Bacik12621332015-02-03 07:50:16 -08002732{
2733 u64 csum_size;
2734 u64 num_csums_per_leaf;
2735 u64 num_csums;
2736
2737 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
2738 num_csums_per_leaf = div64_u64(csum_size,
2739 (u64)btrfs_super_csum_size(root->fs_info->super_copy));
2740 num_csums = div64_u64(csum_bytes, root->sectorsize);
2741 num_csums += num_csums_per_leaf - 1;
2742 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2743 return num_csums;
2744}
2745
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002746int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacik1be41b72013-06-12 13:56:06 -04002747 struct btrfs_root *root)
2748{
2749 struct btrfs_block_rsv *global_rsv;
2750 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
Josef Bacik12621332015-02-03 07:50:16 -08002751 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
Josef Bacikcb723e42015-02-18 08:06:57 -08002752 u64 num_dirty_bgs = trans->transaction->num_dirty_bgs;
2753 u64 num_bytes, num_dirty_bgs_bytes;
Josef Bacik1be41b72013-06-12 13:56:06 -04002754 int ret = 0;
2755
2756 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
2757 num_heads = heads_to_leaves(root, num_heads);
2758 if (num_heads > 1)
David Sterba707e8a02014-06-04 19:22:26 +02002759 num_bytes += (num_heads - 1) * root->nodesize;
Josef Bacik1be41b72013-06-12 13:56:06 -04002760 num_bytes <<= 1;
Chris Mason28f75a02015-02-04 06:59:29 -08002761 num_bytes += btrfs_csum_bytes_to_leaves(root, csum_bytes) * root->nodesize;
Josef Bacikcb723e42015-02-18 08:06:57 -08002762 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(root,
2763 num_dirty_bgs);
Josef Bacik1be41b72013-06-12 13:56:06 -04002764 global_rsv = &root->fs_info->global_block_rsv;
2765
2766 /*
2767 * If we can't allocate any more chunks lets make sure we have _lots_ of
2768 * wiggle room since running delayed refs can create more delayed refs.
2769 */
Josef Bacikcb723e42015-02-18 08:06:57 -08002770 if (global_rsv->space_info->full) {
2771 num_dirty_bgs_bytes <<= 1;
Josef Bacik1be41b72013-06-12 13:56:06 -04002772 num_bytes <<= 1;
Josef Bacikcb723e42015-02-18 08:06:57 -08002773 }
Josef Bacik1be41b72013-06-12 13:56:06 -04002774
2775 spin_lock(&global_rsv->lock);
Josef Bacikcb723e42015-02-18 08:06:57 -08002776 if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes)
Josef Bacik1be41b72013-06-12 13:56:06 -04002777 ret = 1;
2778 spin_unlock(&global_rsv->lock);
2779 return ret;
2780}
2781
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002782int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2783 struct btrfs_root *root)
2784{
2785 struct btrfs_fs_info *fs_info = root->fs_info;
2786 u64 num_entries =
2787 atomic_read(&trans->transaction->delayed_refs.num_entries);
2788 u64 avg_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002789 u64 val;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002790
2791 smp_mb();
2792 avg_runtime = fs_info->avg_delayed_ref_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002793 val = num_entries * avg_runtime;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002794 if (num_entries * avg_runtime >= NSEC_PER_SEC)
2795 return 1;
Chris Masona79b7d42014-05-22 16:18:52 -07002796 if (val >= NSEC_PER_SEC / 2)
2797 return 2;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002798
2799 return btrfs_check_space_for_delayed_refs(trans, root);
2800}
2801
Chris Masona79b7d42014-05-22 16:18:52 -07002802struct async_delayed_refs {
2803 struct btrfs_root *root;
2804 int count;
2805 int error;
2806 int sync;
2807 struct completion wait;
2808 struct btrfs_work work;
2809};
2810
2811static void delayed_ref_async_start(struct btrfs_work *work)
2812{
2813 struct async_delayed_refs *async;
2814 struct btrfs_trans_handle *trans;
2815 int ret;
2816
2817 async = container_of(work, struct async_delayed_refs, work);
2818
2819 trans = btrfs_join_transaction(async->root);
2820 if (IS_ERR(trans)) {
2821 async->error = PTR_ERR(trans);
2822 goto done;
2823 }
2824
2825 /*
2826 * trans->sync means that when we call end_transaciton, we won't
2827 * wait on delayed refs
2828 */
2829 trans->sync = true;
2830 ret = btrfs_run_delayed_refs(trans, async->root, async->count);
2831 if (ret)
2832 async->error = ret;
2833
2834 ret = btrfs_end_transaction(trans, async->root);
2835 if (ret && !async->error)
2836 async->error = ret;
2837done:
2838 if (async->sync)
2839 complete(&async->wait);
2840 else
2841 kfree(async);
2842}
2843
2844int btrfs_async_run_delayed_refs(struct btrfs_root *root,
2845 unsigned long count, int wait)
2846{
2847 struct async_delayed_refs *async;
2848 int ret;
2849
2850 async = kmalloc(sizeof(*async), GFP_NOFS);
2851 if (!async)
2852 return -ENOMEM;
2853
2854 async->root = root->fs_info->tree_root;
2855 async->count = count;
2856 async->error = 0;
2857 if (wait)
2858 async->sync = 1;
2859 else
2860 async->sync = 0;
2861 init_completion(&async->wait);
2862
Liu Bo9e0af232014-08-15 23:36:53 +08002863 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
2864 delayed_ref_async_start, NULL, NULL);
Chris Masona79b7d42014-05-22 16:18:52 -07002865
2866 btrfs_queue_work(root->fs_info->extent_workers, &async->work);
2867
2868 if (wait) {
2869 wait_for_completion(&async->wait);
2870 ret = async->error;
2871 kfree(async);
2872 return ret;
2873 }
2874 return 0;
2875}
2876
Chris Masonc3e69d52009-03-13 10:17:05 -04002877/*
2878 * this starts processing the delayed reference count updates and
2879 * extent insertions we have queued up so far. count can be
2880 * 0, which means to process everything in the tree at the start
2881 * of the run (but not newly added entries), or it can be some target
2882 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002883 *
2884 * Returns 0 on success or if called with an aborted transaction
2885 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002886 */
2887int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2888 struct btrfs_root *root, unsigned long count)
2889{
2890 struct rb_node *node;
2891 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002892 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002893 int ret;
2894 int run_all = count == (unsigned long)-1;
Filipe Mananad9a05402015-10-03 13:13:13 +01002895 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Chris Masonc3e69d52009-03-13 10:17:05 -04002896
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002897 /* We'll clean this up in btrfs_cleanup_transaction */
2898 if (trans->aborted)
2899 return 0;
2900
Chris Masonc3e69d52009-03-13 10:17:05 -04002901 if (root == root->fs_info->extent_root)
2902 root = root->fs_info->tree_root;
2903
2904 delayed_refs = &trans->transaction->delayed_refs;
Liu Bo26455d32014-12-17 16:14:09 +08002905 if (count == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -05002906 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002907
Chris Masonc3e69d52009-03-13 10:17:05 -04002908again:
Arne Jansen709c0482011-09-12 12:22:57 +02002909#ifdef SCRAMBLE_DELAYED_REFS
2910 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2911#endif
Filipe Mananad9a05402015-10-03 13:13:13 +01002912 trans->can_flush_pending_bgs = false;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002913 ret = __btrfs_run_delayed_refs(trans, root, count);
2914 if (ret < 0) {
2915 btrfs_abort_transaction(trans, root, ret);
2916 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002917 }
2918
Chris Mason56bec292009-03-13 10:10:06 -04002919 if (run_all) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002920 if (!list_empty(&trans->new_bgs))
Josef Bacikea658ba2012-09-11 16:57:25 -04002921 btrfs_create_pending_block_groups(trans, root);
Josef Bacikea658ba2012-09-11 16:57:25 -04002922
Josef Bacikd7df2c72014-01-23 09:21:38 -05002923 spin_lock(&delayed_refs->lock);
Liu Boc46effa2013-10-14 12:59:45 +08002924 node = rb_first(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002925 if (!node) {
2926 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002927 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002928 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002929 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002930
2931 while (node) {
Liu Boc46effa2013-10-14 12:59:45 +08002932 head = rb_entry(node, struct btrfs_delayed_ref_head,
2933 href_node);
2934 if (btrfs_delayed_ref_is_head(&head->node)) {
2935 struct btrfs_delayed_ref_node *ref;
Chris Mason56bec292009-03-13 10:10:06 -04002936
Liu Boc46effa2013-10-14 12:59:45 +08002937 ref = &head->node;
Chris Mason56bec292009-03-13 10:10:06 -04002938 atomic_inc(&ref->refs);
2939
2940 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002941 /*
2942 * Mutex was contended, block until it's
2943 * released and try again
2944 */
Chris Mason56bec292009-03-13 10:10:06 -04002945 mutex_lock(&head->mutex);
2946 mutex_unlock(&head->mutex);
2947
2948 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002949 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002950 goto again;
Liu Boc46effa2013-10-14 12:59:45 +08002951 } else {
2952 WARN_ON(1);
Chris Mason56bec292009-03-13 10:10:06 -04002953 }
2954 node = rb_next(node);
2955 }
2956 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002957 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002958 goto again;
2959 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002960out:
Jan Schmidtedf39272012-06-28 18:04:55 +02002961 assert_qgroups_uptodate(trans);
Filipe Mananad9a05402015-10-03 13:13:13 +01002962 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Chris Masona28ec192007-03-06 20:08:01 -05002963 return 0;
2964}
2965
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002966int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2967 struct btrfs_root *root,
2968 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002969 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002970{
2971 struct btrfs_delayed_extent_op *extent_op;
2972 int ret;
2973
Miao Xie78a61842012-11-21 02:21:28 +00002974 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002975 if (!extent_op)
2976 return -ENOMEM;
2977
2978 extent_op->flags_to_set = flags;
2979 extent_op->update_flags = 1;
2980 extent_op->update_key = 0;
2981 extent_op->is_data = is_data ? 1 : 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002982 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002983
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002984 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2985 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002986 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002987 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002988 return ret;
2989}
2990
2991static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2992 struct btrfs_root *root,
2993 struct btrfs_path *path,
2994 u64 objectid, u64 offset, u64 bytenr)
2995{
2996 struct btrfs_delayed_ref_head *head;
2997 struct btrfs_delayed_ref_node *ref;
2998 struct btrfs_delayed_data_ref *data_ref;
2999 struct btrfs_delayed_ref_root *delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003000 int ret = 0;
3001
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003002 delayed_refs = &trans->transaction->delayed_refs;
3003 spin_lock(&delayed_refs->lock);
3004 head = btrfs_find_delayed_ref_head(trans, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003005 if (!head) {
3006 spin_unlock(&delayed_refs->lock);
3007 return 0;
3008 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003009
3010 if (!mutex_trylock(&head->mutex)) {
3011 atomic_inc(&head->node.refs);
3012 spin_unlock(&delayed_refs->lock);
3013
David Sterbab3b4aa72011-04-21 01:20:15 +02003014 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003015
David Sterba8cc33e52011-05-02 15:29:25 +02003016 /*
3017 * Mutex was contended, block until it's released and let
3018 * caller try again
3019 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003020 mutex_lock(&head->mutex);
3021 mutex_unlock(&head->mutex);
3022 btrfs_put_delayed_ref(&head->node);
3023 return -EAGAIN;
3024 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003025 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05003026
3027 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08003028 list_for_each_entry(ref, &head->ref_list, list) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05003029 /* If it's a shared ref we know a cross reference exists */
3030 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
3031 ret = 1;
3032 break;
3033 }
3034
3035 data_ref = btrfs_delayed_node_to_data_ref(ref);
3036
3037 /*
3038 * If our ref doesn't match the one we're currently looking at
3039 * then we have a cross reference.
3040 */
3041 if (data_ref->root != root->root_key.objectid ||
3042 data_ref->objectid != objectid ||
3043 data_ref->offset != offset) {
3044 ret = 1;
3045 break;
3046 }
3047 }
3048 spin_unlock(&head->lock);
3049 mutex_unlock(&head->mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003050 return ret;
3051}
3052
3053static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
3054 struct btrfs_root *root,
3055 struct btrfs_path *path,
3056 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05003057{
3058 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04003059 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003060 struct btrfs_extent_data_ref *ref;
3061 struct btrfs_extent_inline_ref *iref;
3062 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05003063 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003064 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04003065 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05003066
Chris Masonbe20aa92007-12-17 20:14:01 -05003067 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04003068 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04003069 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05003070
Chris Masonbe20aa92007-12-17 20:14:01 -05003071 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3072 if (ret < 0)
3073 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003074 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04003075
3076 ret = -ENOENT;
3077 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04003078 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003079
Zheng Yan31840ae2008-09-23 13:14:14 -04003080 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04003081 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003082 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05003083
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003084 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05003085 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003086
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003087 ret = 1;
3088 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3089#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3090 if (item_size < sizeof(*ei)) {
3091 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3092 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05003093 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003094#endif
3095 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
3096
3097 if (item_size != sizeof(*ei) +
3098 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
3099 goto out;
3100
3101 if (btrfs_extent_generation(leaf, ei) <=
3102 btrfs_root_last_snapshot(&root->root_item))
3103 goto out;
3104
3105 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
3106 if (btrfs_extent_inline_ref_type(leaf, iref) !=
3107 BTRFS_EXTENT_DATA_REF_KEY)
3108 goto out;
3109
3110 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3111 if (btrfs_extent_refs(leaf, ei) !=
3112 btrfs_extent_data_ref_count(leaf, ref) ||
3113 btrfs_extent_data_ref_root(leaf, ref) !=
3114 root->root_key.objectid ||
3115 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3116 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3117 goto out;
3118
Yan Zhengf321e492008-07-30 09:26:11 -04003119 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05003120out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003121 return ret;
3122}
3123
3124int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
3125 struct btrfs_root *root,
3126 u64 objectid, u64 offset, u64 bytenr)
3127{
3128 struct btrfs_path *path;
3129 int ret;
3130 int ret2;
3131
3132 path = btrfs_alloc_path();
3133 if (!path)
3134 return -ENOENT;
3135
3136 do {
3137 ret = check_committed_ref(trans, root, path, objectid,
3138 offset, bytenr);
3139 if (ret && ret != -ENOENT)
3140 goto out;
3141
3142 ret2 = check_delayed_ref(trans, root, path, objectid,
3143 offset, bytenr);
3144 } while (ret2 == -EAGAIN);
3145
3146 if (ret2 && ret2 != -ENOENT) {
3147 ret = ret2;
3148 goto out;
3149 }
3150
3151 if (ret != -ENOENT || ret2 != -ENOENT)
3152 ret = 0;
3153out:
Yan Zhengf321e492008-07-30 09:26:11 -04003154 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003155 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3156 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04003157 return ret;
3158}
3159
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003160static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05003161 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003162 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07003163 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04003164{
3165 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003166 u64 num_bytes;
3167 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003168 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04003169 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003170 struct btrfs_key key;
3171 struct btrfs_file_extent_item *fi;
3172 int i;
3173 int level;
3174 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003175 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003176 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04003177
David Sterbafccb84c2014-09-29 23:53:21 +02003178
3179 if (btrfs_test_is_dummy_root(root))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003180 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02003181
Zheng Yan31840ae2008-09-23 13:14:14 -04003182 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003183 nritems = btrfs_header_nritems(buf);
3184 level = btrfs_header_level(buf);
3185
Miao Xie27cdeb72014-04-02 19:51:05 +08003186 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003187 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003188
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003189 if (inc)
3190 process_func = btrfs_inc_extent_ref;
3191 else
3192 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003193
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003194 if (full_backref)
3195 parent = buf->start;
3196 else
3197 parent = 0;
3198
Zheng Yan31840ae2008-09-23 13:14:14 -04003199 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003200 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003201 btrfs_item_key_to_cpu(buf, &key, i);
David Sterba962a2982014-06-04 18:41:45 +02003202 if (key.type != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003203 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003204 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003205 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003206 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003207 BTRFS_FILE_EXTENT_INLINE)
3208 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003209 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3210 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003211 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003212
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003213 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3214 key.offset -= btrfs_file_extent_offset(buf, fi);
3215 ret = process_func(trans, root, bytenr, num_bytes,
3216 parent, ref_root, key.objectid,
Josef Bacike339a6b2014-07-02 10:54:25 -07003217 key.offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003218 if (ret)
3219 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003220 } else {
3221 bytenr = btrfs_node_blockptr(buf, i);
David Sterba707e8a02014-06-04 19:22:26 +02003222 num_bytes = root->nodesize;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003223 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003224 parent, ref_root, level - 1, 0,
Josef Bacike339a6b2014-07-02 10:54:25 -07003225 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003226 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003227 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003228 }
3229 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003230 return 0;
3231fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003232 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003233}
3234
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003235int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003236 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04003237{
Josef Bacike339a6b2014-07-02 10:54:25 -07003238 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003239}
Zheng Yan31840ae2008-09-23 13:14:14 -04003240
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003241int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003242 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003243{
Josef Bacike339a6b2014-07-02 10:54:25 -07003244 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003245}
3246
Chris Mason9078a3e2007-04-26 16:46:15 -04003247static int write_one_cache_group(struct btrfs_trans_handle *trans,
3248 struct btrfs_root *root,
3249 struct btrfs_path *path,
3250 struct btrfs_block_group_cache *cache)
3251{
3252 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003253 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003254 unsigned long bi;
3255 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003256
Chris Mason9078a3e2007-04-26 16:46:15 -04003257 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003258 if (ret) {
3259 if (ret > 0)
3260 ret = -ENOENT;
Chris Mason54aa1f42007-06-22 14:16:25 -04003261 goto fail;
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003262 }
Chris Mason5f39d392007-10-15 16:14:19 -04003263
3264 leaf = path->nodes[0];
3265 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3266 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3267 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -04003268fail:
Filipe Manana24b89d02015-04-25 18:31:05 +01003269 btrfs_release_path(path);
Josef Bacikdf95e7f2014-12-12 16:02:20 -05003270 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003271
3272}
3273
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003274static struct btrfs_block_group_cache *
3275next_block_group(struct btrfs_root *root,
3276 struct btrfs_block_group_cache *cache)
3277{
3278 struct rb_node *node;
Filipe Manana292cbd52014-11-26 15:28:50 +00003279
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003280 spin_lock(&root->fs_info->block_group_cache_lock);
Filipe Manana292cbd52014-11-26 15:28:50 +00003281
3282 /* If our block group was removed, we need a full search. */
3283 if (RB_EMPTY_NODE(&cache->cache_node)) {
3284 const u64 next_bytenr = cache->key.objectid + cache->key.offset;
3285
3286 spin_unlock(&root->fs_info->block_group_cache_lock);
3287 btrfs_put_block_group(cache);
3288 cache = btrfs_lookup_first_block_group(root->fs_info,
3289 next_bytenr);
3290 return cache;
3291 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003292 node = rb_next(&cache->cache_node);
3293 btrfs_put_block_group(cache);
3294 if (node) {
3295 cache = rb_entry(node, struct btrfs_block_group_cache,
3296 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003297 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003298 } else
3299 cache = NULL;
3300 spin_unlock(&root->fs_info->block_group_cache_lock);
3301 return cache;
3302}
3303
Josef Bacik0af3d002010-06-21 14:48:16 -04003304static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3305 struct btrfs_trans_handle *trans,
3306 struct btrfs_path *path)
3307{
3308 struct btrfs_root *root = block_group->fs_info->tree_root;
3309 struct inode *inode = NULL;
3310 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003311 int dcs = BTRFS_DC_ERROR;
David Sterbaf8c269d2015-01-16 17:21:12 +01003312 u64 num_pages = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003313 int retries = 0;
3314 int ret = 0;
3315
3316 /*
3317 * If this block group is smaller than 100 megs don't bother caching the
3318 * block group.
3319 */
3320 if (block_group->key.offset < (100 * 1024 * 1024)) {
3321 spin_lock(&block_group->lock);
3322 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3323 spin_unlock(&block_group->lock);
3324 return 0;
3325 }
3326
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003327 if (trans->aborted)
3328 return 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04003329again:
3330 inode = lookup_free_space_inode(root, block_group, path);
3331 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3332 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003333 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003334 goto out;
3335 }
3336
3337 if (IS_ERR(inode)) {
3338 BUG_ON(retries);
3339 retries++;
3340
3341 if (block_group->ro)
3342 goto out_free;
3343
3344 ret = create_free_space_inode(root, trans, block_group, path);
3345 if (ret)
3346 goto out_free;
3347 goto again;
3348 }
3349
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003350 /* We've already setup this transaction, go ahead and exit */
3351 if (block_group->cache_generation == trans->transid &&
3352 i_size_read(inode)) {
3353 dcs = BTRFS_DC_SETUP;
3354 goto out_put;
3355 }
3356
Josef Bacik0af3d002010-06-21 14:48:16 -04003357 /*
3358 * We want to set the generation to 0, that way if anything goes wrong
3359 * from here on out we know not to trust this cache when we load up next
3360 * time.
3361 */
3362 BTRFS_I(inode)->generation = 0;
3363 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik0c0ef4b2015-02-12 09:43:51 -05003364 if (ret) {
3365 /*
3366 * So theoretically we could recover from this, simply set the
3367 * super cache generation to 0 so we know to invalidate the
3368 * cache, but then we'd have to keep track of the block groups
3369 * that fail this way so we know we _have_ to reset this cache
3370 * before the next commit or risk reading stale cache. So to
3371 * limit our exposure to horrible edge cases lets just abort the
3372 * transaction, this only happens in really bad situations
3373 * anyway.
3374 */
3375 btrfs_abort_transaction(trans, root, ret);
3376 goto out_put;
3377 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003378 WARN_ON(ret);
3379
3380 if (i_size_read(inode) > 0) {
Miao Xie7b61cd92013-05-13 13:55:09 +00003381 ret = btrfs_check_trunc_cache_free_space(root,
3382 &root->fs_info->global_block_rsv);
3383 if (ret)
3384 goto out_put;
3385
Chris Mason1bbc6212015-04-06 12:46:08 -07003386 ret = btrfs_truncate_free_space_cache(root, trans, NULL, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003387 if (ret)
3388 goto out_put;
3389 }
3390
3391 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003392 if (block_group->cached != BTRFS_CACHE_FINISHED ||
Chris Masone4c88f02015-04-18 05:22:48 -07003393 !btrfs_test_opt(root, SPACE_CACHE)) {
Liu Bocf7c1ef2012-07-06 03:31:34 -06003394 /*
3395 * don't bother trying to write stuff out _if_
3396 * a) we're not cached,
3397 * b) we're with nospace_cache mount option.
3398 */
Josef Bacik2b209822010-12-03 13:17:53 -05003399 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003400 spin_unlock(&block_group->lock);
3401 goto out_put;
3402 }
3403 spin_unlock(&block_group->lock);
3404
Josef Bacik6fc823b2012-08-06 13:46:38 -06003405 /*
Josef Bacik2968b1f2015-10-01 12:55:18 -04003406 * We hit an ENOSPC when setting up the cache in this transaction, just
3407 * skip doing the setup, we've already cleared the cache so we're safe.
3408 */
3409 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) {
3410 ret = -ENOSPC;
3411 goto out_put;
3412 }
3413
3414 /*
Josef Bacik6fc823b2012-08-06 13:46:38 -06003415 * Try to preallocate enough space based on how big the block group is.
3416 * Keep in mind this has to include any pinned space which could end up
3417 * taking up quite a bit since it's not folded into the other space
3418 * cache.
3419 */
David Sterbaf8c269d2015-01-16 17:21:12 +01003420 num_pages = div_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003421 if (!num_pages)
3422 num_pages = 1;
3423
Josef Bacik0af3d002010-06-21 14:48:16 -04003424 num_pages *= 16;
3425 num_pages *= PAGE_CACHE_SIZE;
3426
Dongsheng Yange2d1f922015-02-06 10:26:52 -05003427 ret = btrfs_check_data_free_space(inode, num_pages, num_pages);
Josef Bacik0af3d002010-06-21 14:48:16 -04003428 if (ret)
3429 goto out_put;
3430
3431 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3432 num_pages, num_pages,
3433 &alloc_hint);
Josef Bacik2968b1f2015-10-01 12:55:18 -04003434 /*
3435 * Our cache requires contiguous chunks so that we don't modify a bunch
3436 * of metadata or split extents when writing the cache out, which means
3437 * we can enospc if we are heavily fragmented in addition to just normal
3438 * out of space conditions. So if we hit this just skip setting up any
3439 * other block groups for this transaction, maybe we'll unpin enough
3440 * space the next time around.
3441 */
Josef Bacik2b209822010-12-03 13:17:53 -05003442 if (!ret)
3443 dcs = BTRFS_DC_SETUP;
Josef Bacik2968b1f2015-10-01 12:55:18 -04003444 else if (ret == -ENOSPC)
3445 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags);
Josef Bacik0af3d002010-06-21 14:48:16 -04003446 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003447
Josef Bacik0af3d002010-06-21 14:48:16 -04003448out_put:
3449 iput(inode);
3450out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003451 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003452out:
3453 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003454 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003455 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003456 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003457 spin_unlock(&block_group->lock);
3458
3459 return ret;
3460}
3461
Josef Bacikdcdf7f62015-03-02 16:37:31 -05003462int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
3463 struct btrfs_root *root)
3464{
3465 struct btrfs_block_group_cache *cache, *tmp;
3466 struct btrfs_transaction *cur_trans = trans->transaction;
3467 struct btrfs_path *path;
3468
3469 if (list_empty(&cur_trans->dirty_bgs) ||
3470 !btrfs_test_opt(root, SPACE_CACHE))
3471 return 0;
3472
3473 path = btrfs_alloc_path();
3474 if (!path)
3475 return -ENOMEM;
3476
3477 /* Could add new block groups, use _safe just in case */
3478 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs,
3479 dirty_list) {
3480 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3481 cache_save_setup(cache, trans, path);
3482 }
3483
3484 btrfs_free_path(path);
3485 return 0;
3486}
3487
Chris Mason1bbc6212015-04-06 12:46:08 -07003488/*
3489 * transaction commit does final block group cache writeback during a
3490 * critical section where nothing is allowed to change the FS. This is
3491 * required in order for the cache to actually match the block group,
3492 * but can introduce a lot of latency into the commit.
3493 *
3494 * So, btrfs_start_dirty_block_groups is here to kick off block group
3495 * cache IO. There's a chance we'll have to redo some of it if the
3496 * block group changes again during the commit, but it greatly reduces
3497 * the commit latency by getting rid of the easy block groups while
3498 * we're still allowing others to join the commit.
3499 */
3500int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans,
3501 struct btrfs_root *root)
3502{
3503 struct btrfs_block_group_cache *cache;
3504 struct btrfs_transaction *cur_trans = trans->transaction;
3505 int ret = 0;
3506 int should_put;
3507 struct btrfs_path *path = NULL;
3508 LIST_HEAD(dirty);
3509 struct list_head *io = &cur_trans->io_bgs;
3510 int num_started = 0;
3511 int loops = 0;
3512
3513 spin_lock(&cur_trans->dirty_bgs_lock);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003514 if (list_empty(&cur_trans->dirty_bgs)) {
3515 spin_unlock(&cur_trans->dirty_bgs_lock);
3516 return 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003517 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003518 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Chris Mason1bbc6212015-04-06 12:46:08 -07003519 spin_unlock(&cur_trans->dirty_bgs_lock);
3520
3521again:
Chris Mason1bbc6212015-04-06 12:46:08 -07003522 /*
3523 * make sure all the block groups on our dirty list actually
3524 * exist
3525 */
3526 btrfs_create_pending_block_groups(trans, root);
3527
3528 if (!path) {
3529 path = btrfs_alloc_path();
3530 if (!path)
3531 return -ENOMEM;
3532 }
3533
Filipe Mananab58d1a92015-04-25 18:29:16 +01003534 /*
3535 * cache_write_mutex is here only to save us from balance or automatic
3536 * removal of empty block groups deleting this block group while we are
3537 * writing out the cache
3538 */
3539 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003540 while (!list_empty(&dirty)) {
3541 cache = list_first_entry(&dirty,
3542 struct btrfs_block_group_cache,
3543 dirty_list);
Chris Mason1bbc6212015-04-06 12:46:08 -07003544 /*
3545 * this can happen if something re-dirties a block
3546 * group that is already under IO. Just wait for it to
3547 * finish and then do it all again
3548 */
3549 if (!list_empty(&cache->io_list)) {
3550 list_del_init(&cache->io_list);
3551 btrfs_wait_cache_io(root, trans, cache,
3552 &cache->io_ctl, path,
3553 cache->key.objectid);
3554 btrfs_put_block_group(cache);
3555 }
3556
3557
3558 /*
3559 * btrfs_wait_cache_io uses the cache->dirty_list to decide
3560 * if it should update the cache_state. Don't delete
3561 * until after we wait.
3562 *
3563 * Since we're not running in the commit critical section
3564 * we need the dirty_bgs_lock to protect from update_block_group
3565 */
3566 spin_lock(&cur_trans->dirty_bgs_lock);
3567 list_del_init(&cache->dirty_list);
3568 spin_unlock(&cur_trans->dirty_bgs_lock);
3569
3570 should_put = 1;
3571
3572 cache_save_setup(cache, trans, path);
3573
3574 if (cache->disk_cache_state == BTRFS_DC_SETUP) {
3575 cache->io_ctl.inode = NULL;
3576 ret = btrfs_write_out_cache(root, trans, cache, path);
3577 if (ret == 0 && cache->io_ctl.inode) {
3578 num_started++;
3579 should_put = 0;
3580
3581 /*
3582 * the cache_write_mutex is protecting
3583 * the io_list
3584 */
3585 list_add_tail(&cache->io_list, io);
3586 } else {
3587 /*
3588 * if we failed to write the cache, the
3589 * generation will be bad and life goes on
3590 */
3591 ret = 0;
3592 }
3593 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003594 if (!ret) {
Chris Mason1bbc6212015-04-06 12:46:08 -07003595 ret = write_one_cache_group(trans, root, path, cache);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003596 /*
3597 * Our block group might still be attached to the list
3598 * of new block groups in the transaction handle of some
3599 * other task (struct btrfs_trans_handle->new_bgs). This
3600 * means its block group item isn't yet in the extent
3601 * tree. If this happens ignore the error, as we will
3602 * try again later in the critical section of the
3603 * transaction commit.
3604 */
3605 if (ret == -ENOENT) {
3606 ret = 0;
3607 spin_lock(&cur_trans->dirty_bgs_lock);
3608 if (list_empty(&cache->dirty_list)) {
3609 list_add_tail(&cache->dirty_list,
3610 &cur_trans->dirty_bgs);
3611 btrfs_get_block_group(cache);
3612 }
3613 spin_unlock(&cur_trans->dirty_bgs_lock);
3614 } else if (ret) {
3615 btrfs_abort_transaction(trans, root, ret);
3616 }
3617 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003618
3619 /* if its not on the io list, we need to put the block group */
3620 if (should_put)
3621 btrfs_put_block_group(cache);
3622
3623 if (ret)
3624 break;
Filipe Mananab58d1a92015-04-25 18:29:16 +01003625
3626 /*
3627 * Avoid blocking other tasks for too long. It might even save
3628 * us from writing caches for block groups that are going to be
3629 * removed.
3630 */
3631 mutex_unlock(&trans->transaction->cache_write_mutex);
3632 mutex_lock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003633 }
Filipe Mananab58d1a92015-04-25 18:29:16 +01003634 mutex_unlock(&trans->transaction->cache_write_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07003635
3636 /*
3637 * go through delayed refs for all the stuff we've just kicked off
3638 * and then loop back (just once)
3639 */
3640 ret = btrfs_run_delayed_refs(trans, root, 0);
3641 if (!ret && loops == 0) {
3642 loops++;
3643 spin_lock(&cur_trans->dirty_bgs_lock);
3644 list_splice_init(&cur_trans->dirty_bgs, &dirty);
Filipe Mananab58d1a92015-04-25 18:29:16 +01003645 /*
3646 * dirty_bgs_lock protects us from concurrent block group
3647 * deletes too (not just cache_write_mutex).
3648 */
3649 if (!list_empty(&dirty)) {
3650 spin_unlock(&cur_trans->dirty_bgs_lock);
3651 goto again;
3652 }
Chris Mason1bbc6212015-04-06 12:46:08 -07003653 spin_unlock(&cur_trans->dirty_bgs_lock);
Chris Mason1bbc6212015-04-06 12:46:08 -07003654 }
3655
3656 btrfs_free_path(path);
3657 return ret;
3658}
3659
Chris Mason96b51792007-10-15 16:15:19 -04003660int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3661 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003662{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003663 struct btrfs_block_group_cache *cache;
Josef Bacikce93ec52014-11-17 15:45:48 -05003664 struct btrfs_transaction *cur_trans = trans->transaction;
3665 int ret = 0;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003666 int should_put;
Chris Mason9078a3e2007-04-26 16:46:15 -04003667 struct btrfs_path *path;
Chris Mason1bbc6212015-04-06 12:46:08 -07003668 struct list_head *io = &cur_trans->io_bgs;
Chris Masonc9dc4c62015-04-04 17:14:42 -07003669 int num_started = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003670
3671 path = btrfs_alloc_path();
3672 if (!path)
3673 return -ENOMEM;
3674
Josef Bacikce93ec52014-11-17 15:45:48 -05003675 /*
3676 * We don't need the lock here since we are protected by the transaction
3677 * commit. We want to do the cache_save_setup first and then run the
3678 * delayed refs to make sure we have the best chance at doing this all
3679 * in one shot.
3680 */
3681 while (!list_empty(&cur_trans->dirty_bgs)) {
3682 cache = list_first_entry(&cur_trans->dirty_bgs,
3683 struct btrfs_block_group_cache,
3684 dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003685
3686 /*
3687 * this can happen if cache_save_setup re-dirties a block
3688 * group that is already under IO. Just wait for it to
3689 * finish and then do it all again
3690 */
3691 if (!list_empty(&cache->io_list)) {
3692 list_del_init(&cache->io_list);
3693 btrfs_wait_cache_io(root, trans, cache,
3694 &cache->io_ctl, path,
3695 cache->key.objectid);
3696 btrfs_put_block_group(cache);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003697 }
3698
Chris Mason1bbc6212015-04-06 12:46:08 -07003699 /*
3700 * don't remove from the dirty list until after we've waited
3701 * on any pending IO
3702 */
Josef Bacikce93ec52014-11-17 15:45:48 -05003703 list_del_init(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003704 should_put = 1;
3705
Chris Mason1bbc6212015-04-06 12:46:08 -07003706 cache_save_setup(cache, trans, path);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003707
Josef Bacikce93ec52014-11-17 15:45:48 -05003708 if (!ret)
Chris Masonc9dc4c62015-04-04 17:14:42 -07003709 ret = btrfs_run_delayed_refs(trans, root, (unsigned long) -1);
3710
3711 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3712 cache->io_ctl.inode = NULL;
3713 ret = btrfs_write_out_cache(root, trans, cache, path);
3714 if (ret == 0 && cache->io_ctl.inode) {
3715 num_started++;
3716 should_put = 0;
Chris Mason1bbc6212015-04-06 12:46:08 -07003717 list_add_tail(&cache->io_list, io);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003718 } else {
3719 /*
3720 * if we failed to write the cache, the
3721 * generation will be bad and life goes on
3722 */
3723 ret = 0;
3724 }
3725 }
Filipe Mananaff1f8252015-05-06 16:15:09 +01003726 if (!ret) {
Josef Bacikce93ec52014-11-17 15:45:48 -05003727 ret = write_one_cache_group(trans, root, path, cache);
Filipe Mananaff1f8252015-05-06 16:15:09 +01003728 if (ret)
3729 btrfs_abort_transaction(trans, root, ret);
3730 }
Chris Masonc9dc4c62015-04-04 17:14:42 -07003731
3732 /* if its not on the io list, we need to put the block group */
3733 if (should_put)
3734 btrfs_put_block_group(cache);
3735 }
3736
Chris Mason1bbc6212015-04-06 12:46:08 -07003737 while (!list_empty(io)) {
3738 cache = list_first_entry(io, struct btrfs_block_group_cache,
Chris Masonc9dc4c62015-04-04 17:14:42 -07003739 io_list);
3740 list_del_init(&cache->io_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07003741 btrfs_wait_cache_io(root, trans, cache,
3742 &cache->io_ctl, path, cache->key.objectid);
Josef Bacik0af3d002010-06-21 14:48:16 -04003743 btrfs_put_block_group(cache);
3744 }
3745
Chris Mason9078a3e2007-04-26 16:46:15 -04003746 btrfs_free_path(path);
Josef Bacikce93ec52014-11-17 15:45:48 -05003747 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003748}
3749
Yan Zhengd2fb3432008-12-11 16:30:39 -05003750int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3751{
3752 struct btrfs_block_group_cache *block_group;
3753 int readonly = 0;
3754
3755 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3756 if (!block_group || block_group->ro)
3757 readonly = 1;
3758 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003759 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003760 return readonly;
3761}
3762
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003763static const char *alloc_name(u64 flags)
3764{
3765 switch (flags) {
3766 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
3767 return "mixed";
3768 case BTRFS_BLOCK_GROUP_METADATA:
3769 return "metadata";
3770 case BTRFS_BLOCK_GROUP_DATA:
3771 return "data";
3772 case BTRFS_BLOCK_GROUP_SYSTEM:
3773 return "system";
3774 default:
3775 WARN_ON(1);
3776 return "invalid-combination";
3777 };
3778}
3779
Chris Mason593060d2008-03-25 16:50:33 -04003780static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3781 u64 total_bytes, u64 bytes_used,
3782 struct btrfs_space_info **space_info)
3783{
3784 struct btrfs_space_info *found;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003785 int i;
3786 int factor;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003787 int ret;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003788
3789 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3790 BTRFS_BLOCK_GROUP_RAID10))
3791 factor = 2;
3792 else
3793 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003794
3795 found = __find_space_info(info, flags);
3796 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003797 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003798 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003799 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003800 found->bytes_used += bytes_used;
Yan, Zhengb742bb822010-05-16 10:46:24 -04003801 found->disk_used += bytes_used * factor;
Filipe Manana2e6e5182015-05-12 00:28:11 +01003802 if (total_bytes > 0)
3803 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003804 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003805 *space_info = found;
3806 return 0;
3807 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003808 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003809 if (!found)
3810 return -ENOMEM;
3811
Tejun Heo908c7f12014-09-08 09:51:29 +09003812 ret = percpu_counter_init(&found->total_bytes_pinned, 0, GFP_KERNEL);
Josef Bacikb150a4f2013-06-19 15:00:04 -04003813 if (ret) {
3814 kfree(found);
3815 return ret;
3816 }
3817
Jeff Mahoneyc1895442014-05-27 12:59:57 -04003818 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
Yan, Zhengb742bb822010-05-16 10:46:24 -04003819 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003820 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003821 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003822 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
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;
Chris Mason593060d2008-03-25 16:50:33 -04003827 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003828 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003829 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003830 found->bytes_may_use = 0;
Filipe Manana6af3e3a2015-09-07 10:41:12 +01003831 found->full = 0;
Josef Bacik4f4db212015-09-29 11:40:47 -04003832 found->max_extent_size = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003833 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003834 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003835 found->flush = 0;
3836 init_waitqueue_head(&found->wait);
Josef Bacik633c0aa2014-10-31 09:49:34 -04003837 INIT_LIST_HEAD(&found->ro_bgs);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003838
3839 ret = kobject_init_and_add(&found->kobj, &space_info_ktype,
3840 info->space_info_kobj, "%s",
3841 alloc_name(found->flags));
3842 if (ret) {
3843 kfree(found);
3844 return ret;
3845 }
3846
Chris Mason593060d2008-03-25 16:50:33 -04003847 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003848 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003849 if (flags & BTRFS_BLOCK_GROUP_DATA)
3850 info->data_sinfo = found;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003851
3852 return ret;
Chris Mason593060d2008-03-25 16:50:33 -04003853}
3854
Chris Mason8790d502008-04-03 16:29:03 -04003855static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3856{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003857 u64 extra_flags = chunk_to_extended(flags) &
3858 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003859
Miao Xiede98ced2013-01-29 10:13:12 +00003860 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003861 if (flags & BTRFS_BLOCK_GROUP_DATA)
3862 fs_info->avail_data_alloc_bits |= extra_flags;
3863 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3864 fs_info->avail_metadata_alloc_bits |= extra_flags;
3865 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3866 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003867 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003868}
Chris Mason593060d2008-03-25 16:50:33 -04003869
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003870/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003871 * returns target flags in extended format or 0 if restripe for this
3872 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003873 *
3874 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003875 */
3876static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3877{
3878 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3879 u64 target = 0;
3880
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003881 if (!bctl)
3882 return 0;
3883
3884 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3885 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3886 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3887 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3888 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3889 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3890 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3891 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3892 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3893 }
3894
3895 return target;
3896}
3897
3898/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003899 * @flags: available profiles in extended format (see ctree.h)
3900 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003901 * Returns reduced profile in chunk format. If profile changing is in
3902 * progress (either running or paused) picks the target profile (if it's
3903 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003904 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003905static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003906{
Miao Xie95669972014-07-24 11:37:14 +08003907 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003908 u64 target;
Zhao Lei9c170b22015-09-15 21:08:08 +08003909 u64 raid_type;
3910 u64 allowed = 0;
Chris Masona061fc82008-05-07 11:43:44 -04003911
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003912 /*
3913 * see if restripe for this chunk_type is in progress, if so
3914 * try to reduce to the target profile
3915 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003916 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003917 target = get_restripe_target(root->fs_info, flags);
3918 if (target) {
3919 /* pick target profile only if it's already available */
3920 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003921 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003922 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003923 }
3924 }
3925 spin_unlock(&root->fs_info->balance_lock);
3926
David Woodhouse53b381b2013-01-29 18:40:14 -05003927 /* First, mask out the RAID levels which aren't possible */
Zhao Lei9c170b22015-09-15 21:08:08 +08003928 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
3929 if (num_devices >= btrfs_raid_array[raid_type].devs_min)
3930 allowed |= btrfs_raid_group[raid_type];
3931 }
3932 allowed &= flags;
Chris Masona061fc82008-05-07 11:43:44 -04003933
Zhao Lei9c170b22015-09-15 21:08:08 +08003934 if (allowed & BTRFS_BLOCK_GROUP_RAID6)
3935 allowed = BTRFS_BLOCK_GROUP_RAID6;
3936 else if (allowed & BTRFS_BLOCK_GROUP_RAID5)
3937 allowed = BTRFS_BLOCK_GROUP_RAID5;
3938 else if (allowed & BTRFS_BLOCK_GROUP_RAID10)
3939 allowed = BTRFS_BLOCK_GROUP_RAID10;
3940 else if (allowed & BTRFS_BLOCK_GROUP_RAID1)
3941 allowed = BTRFS_BLOCK_GROUP_RAID1;
3942 else if (allowed & BTRFS_BLOCK_GROUP_RAID0)
3943 allowed = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003944
Zhao Lei9c170b22015-09-15 21:08:08 +08003945 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK;
Chris Masonec44a352008-04-28 15:29:52 -04003946
Zhao Lei9c170b22015-09-15 21:08:08 +08003947 return extended_to_chunk(flags | allowed);
Chris Masonec44a352008-04-28 15:29:52 -04003948}
3949
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003950static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003951{
Miao Xiede98ced2013-01-29 10:13:12 +00003952 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003953 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003954
3955 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003956 flags = orig_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003957 seq = read_seqbegin(&root->fs_info->profiles_lock);
3958
3959 if (flags & BTRFS_BLOCK_GROUP_DATA)
3960 flags |= root->fs_info->avail_data_alloc_bits;
3961 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3962 flags |= root->fs_info->avail_system_alloc_bits;
3963 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3964 flags |= root->fs_info->avail_metadata_alloc_bits;
3965 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003966
Yan, Zhengb742bb822010-05-16 10:46:24 -04003967 return btrfs_reduce_alloc_profile(root, flags);
3968}
Josef Bacik6a632092009-02-20 11:00:09 -05003969
Miao Xie6d07bce2011-01-05 10:07:31 +00003970u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb822010-05-16 10:46:24 -04003971{
3972 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003973 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003974
Yan, Zhengb742bb822010-05-16 10:46:24 -04003975 if (data)
3976 flags = BTRFS_BLOCK_GROUP_DATA;
3977 else if (root == root->fs_info->chunk_root)
3978 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3979 else
3980 flags = BTRFS_BLOCK_GROUP_METADATA;
3981
David Woodhouse53b381b2013-01-29 18:40:14 -05003982 ret = get_alloc_profile(root, flags);
3983 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003984}
3985
Josef Bacik6a632092009-02-20 11:00:09 -05003986/*
3987 * This will check the space that the inode allocates from to make sure we have
3988 * enough space for bytes.
3989 */
Dongsheng Yange2d1f922015-02-06 10:26:52 -05003990int btrfs_check_data_free_space(struct inode *inode, u64 bytes, u64 write_bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003991{
3992 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003993 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003994 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003995 u64 used;
Zhao Lei94b947b2015-02-14 13:23:45 +08003996 int ret = 0;
Zhao Leic99f1b02015-03-02 19:32:20 +08003997 int need_commit = 2;
3998 int have_pinned_space;
Josef Bacik6a632092009-02-20 11:00:09 -05003999
4000 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00004001 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05004002
Miao Xie9dced182013-10-25 17:33:36 +08004003 if (btrfs_is_free_space_inode(inode)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004004 need_commit = 0;
Miao Xie9dced182013-10-25 17:33:36 +08004005 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04004006 }
4007
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004008 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04004009 if (!data_sinfo)
4010 goto alloc;
4011
Josef Bacik6a632092009-02-20 11:00:09 -05004012again:
4013 /* make sure we have enough space to handle the data first */
4014 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004015 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
4016 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
4017 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00004018
4019 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004020 struct btrfs_trans_handle *trans;
4021
Josef Bacik6a632092009-02-20 11:00:09 -05004022 /*
4023 * if we don't have enough free bytes in this space then we need
4024 * to alloc a new chunk.
4025 */
Zhao Leib9fd47c2015-02-09 14:40:20 +08004026 if (!data_sinfo->full) {
Josef Bacik6a632092009-02-20 11:00:09 -05004027 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05004028
Chris Mason0e4f8f82011-04-15 16:05:44 -04004029 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05004030 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04004031alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05004032 alloc_target = btrfs_get_alloc_profile(root, 1);
Miao Xie9dced182013-10-25 17:33:36 +08004033 /*
4034 * It is ugly that we don't call nolock join
4035 * transaction for the free space inode case here.
4036 * But it is safe because we only do the data space
4037 * reservation for the free space cache in the
4038 * transaction context, the common join transaction
4039 * just increase the counter of the current transaction
4040 * handler, doesn't try to acquire the trans_lock of
4041 * the fs.
4042 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004043 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004044 if (IS_ERR(trans))
4045 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05004046
4047 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04004048 alloc_target,
4049 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05004050 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00004051 if (ret < 0) {
4052 if (ret != -ENOSPC)
4053 return ret;
Zhao Leic99f1b02015-03-02 19:32:20 +08004054 else {
4055 have_pinned_space = 1;
Miao Xied52a5b52011-01-05 10:07:18 +00004056 goto commit_trans;
Zhao Leic99f1b02015-03-02 19:32:20 +08004057 }
Miao Xied52a5b52011-01-05 10:07:18 +00004058 }
Chris Mason33b4d472009-09-22 14:45:50 -04004059
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004060 if (!data_sinfo)
4061 data_sinfo = fs_info->data_sinfo;
4062
Josef Bacik6a632092009-02-20 11:00:09 -05004063 goto again;
4064 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004065
4066 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04004067 * If we don't have enough pinned space to deal with this
Zhao Lei94b947b2015-02-14 13:23:45 +08004068 * allocation, and no removed chunk in current transaction,
4069 * don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04004070 */
Zhao Leic99f1b02015-03-02 19:32:20 +08004071 have_pinned_space = percpu_counter_compare(
4072 &data_sinfo->total_bytes_pinned,
4073 used + bytes - data_sinfo->total_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05004074 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004075
4076 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00004077commit_trans:
Zhao Leic99f1b02015-03-02 19:32:20 +08004078 if (need_commit &&
Josef Bacika4abeea2011-04-11 17:25:13 -04004079 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Zhao Leic99f1b02015-03-02 19:32:20 +08004080 need_commit--;
Josef Bacikb150a4f2013-06-19 15:00:04 -04004081
Zhao Lei9a4e7272015-04-09 12:34:43 +08004082 if (need_commit > 0)
4083 btrfs_wait_ordered_roots(fs_info, -1);
4084
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004085 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004086 if (IS_ERR(trans))
4087 return PTR_ERR(trans);
Zhao Leic99f1b02015-03-02 19:32:20 +08004088 if (have_pinned_space >= 0 ||
Josef Bacik3204d332015-09-24 10:46:10 -04004089 test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
4090 &trans->transaction->flags) ||
Zhao Leic99f1b02015-03-02 19:32:20 +08004091 need_commit > 0) {
Zhao Lei94b947b2015-02-14 13:23:45 +08004092 ret = btrfs_commit_transaction(trans, root);
4093 if (ret)
4094 return ret;
Zhao Leid7c15172015-02-26 10:49:20 +08004095 /*
4096 * make sure that all running delayed iput are
4097 * done
4098 */
4099 down_write(&root->fs_info->delayed_iput_sem);
4100 up_write(&root->fs_info->delayed_iput_sem);
Zhao Lei94b947b2015-02-14 13:23:45 +08004101 goto again;
4102 } else {
4103 btrfs_end_transaction(trans, root);
4104 }
Josef Bacik4e06bdd2009-02-20 10:59:53 -05004105 }
4106
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004107 trace_btrfs_space_reservation(root->fs_info,
4108 "space_info:enospc",
4109 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05004110 return -ENOSPC;
4111 }
Dongsheng Yange2d1f922015-02-06 10:26:52 -05004112 ret = btrfs_qgroup_reserve(root, write_bytes);
Dongsheng Yang237c0e92014-12-29 06:23:05 -05004113 if (ret)
4114 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -05004115 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004116 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004117 data_sinfo->flags, bytes, 1);
Dongsheng Yang237c0e92014-12-29 06:23:05 -05004118out:
Josef Bacik6a632092009-02-20 11:00:09 -05004119 spin_unlock(&data_sinfo->lock);
4120
Dongsheng Yang237c0e92014-12-29 06:23:05 -05004121 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05004122}
4123
4124/*
Josef Bacikfb25e912011-07-26 17:00:46 -04004125 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05004126 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004127void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05004128{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004129 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004130 struct btrfs_space_info *data_sinfo;
4131
4132 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00004133 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05004134
Li Zefanb4d7c3c2012-07-09 20:21:07 -06004135 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05004136 spin_lock(&data_sinfo->lock);
Josef Bacik7ee9e442013-06-21 16:37:03 -04004137 WARN_ON(data_sinfo->bytes_may_use < bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05004138 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004139 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004140 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05004141 spin_unlock(&data_sinfo->lock);
4142}
4143
Josef Bacik97e728d2009-04-21 17:40:57 -04004144static void force_metadata_allocation(struct btrfs_fs_info *info)
4145{
4146 struct list_head *head = &info->space_info;
4147 struct btrfs_space_info *found;
4148
4149 rcu_read_lock();
4150 list_for_each_entry_rcu(found, head, list) {
4151 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004152 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04004153 }
4154 rcu_read_unlock();
4155}
4156
Miao Xie3c76cd82013-04-25 10:12:38 +00004157static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
4158{
4159 return (global->size << 1);
4160}
4161
Chris Masone5bc2452010-10-26 13:37:56 -04004162static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04004163 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04004164{
Josef Bacikfb25e912011-07-26 17:00:46 -04004165 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04004166 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04004167 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04004168 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04004169
Chris Mason0e4f8f82011-04-15 16:05:44 -04004170 if (force == CHUNK_ALLOC_FORCE)
4171 return 1;
4172
4173 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04004174 * We need to take into account the global rsv because for all intents
4175 * and purposes it's used space. Don't worry about locking the
4176 * global_rsv, it doesn't change except when the transaction commits.
4177 */
Josef Bacik54338b52012-08-14 16:20:52 -04004178 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Miao Xie3c76cd82013-04-25 10:12:38 +00004179 num_allocated += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04004180
4181 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04004182 * in limited mode, we want to have some free space up to
4183 * about 1% of the FS size.
4184 */
4185 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02004186 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04004187 thresh = max_t(u64, 64 * 1024 * 1024,
4188 div_factor_fine(thresh, 1));
4189
4190 if (num_bytes - num_allocated < thresh)
4191 return 1;
4192 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004193
Josef Bacik698d0082012-09-12 14:08:47 -04004194 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04004195 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04004196 return 1;
4197}
4198
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004199static u64 get_profile_num_devs(struct btrfs_root *root, u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004200{
4201 u64 num_dev;
4202
David Woodhouse53b381b2013-01-29 18:40:14 -05004203 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
4204 BTRFS_BLOCK_GROUP_RAID0 |
4205 BTRFS_BLOCK_GROUP_RAID5 |
4206 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04004207 num_dev = root->fs_info->fs_devices->rw_devices;
4208 else if (type & BTRFS_BLOCK_GROUP_RAID1)
4209 num_dev = 2;
4210 else
4211 num_dev = 1; /* DUP or single */
4212
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004213 return num_dev;
Liu Bo15d1ff82012-03-29 09:57:44 -04004214}
4215
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004216/*
4217 * If @is_allocation is true, reserve space in the system space info necessary
4218 * for allocating a chunk, otherwise if it's false, reserve space necessary for
4219 * removing a chunk.
4220 */
4221void check_system_chunk(struct btrfs_trans_handle *trans,
4222 struct btrfs_root *root,
Filipe Manana4617ea32015-06-09 17:48:21 +01004223 u64 type)
Liu Bo15d1ff82012-03-29 09:57:44 -04004224{
4225 struct btrfs_space_info *info;
4226 u64 left;
4227 u64 thresh;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004228 int ret = 0;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004229 u64 num_devs;
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004230
4231 /*
4232 * Needed because we can end up allocating a system chunk and for an
4233 * atomic and race free space reservation in the chunk block reserve.
4234 */
4235 ASSERT(mutex_is_locked(&root->fs_info->chunk_mutex));
Liu Bo15d1ff82012-03-29 09:57:44 -04004236
4237 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4238 spin_lock(&info->lock);
4239 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004240 info->bytes_reserved - info->bytes_readonly -
4241 info->bytes_may_use;
Liu Bo15d1ff82012-03-29 09:57:44 -04004242 spin_unlock(&info->lock);
4243
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004244 num_devs = get_profile_num_devs(root, type);
4245
4246 /* num_devs device items to update and 1 chunk item to add or remove */
Filipe Manana4617ea32015-06-09 17:48:21 +01004247 thresh = btrfs_calc_trunc_metadata_size(root, num_devs) +
4248 btrfs_calc_trans_metadata_size(root, 1);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01004249
Liu Bo15d1ff82012-03-29 09:57:44 -04004250 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004251 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
4252 left, thresh, type);
Liu Bo15d1ff82012-03-29 09:57:44 -04004253 dump_space_info(info, 0, 0);
4254 }
4255
4256 if (left < thresh) {
4257 u64 flags;
4258
4259 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
Filipe Manana4fbcdf62015-05-20 14:01:54 +01004260 /*
4261 * Ignore failure to create system chunk. We might end up not
4262 * needing it, as we might not need to COW all nodes/leafs from
4263 * the paths we visit in the chunk tree (they were already COWed
4264 * or created in the current transaction for example).
4265 */
4266 ret = btrfs_alloc_chunk(trans, root, flags);
4267 }
4268
4269 if (!ret) {
4270 ret = btrfs_block_rsv_add(root->fs_info->chunk_root,
4271 &root->fs_info->chunk_block_rsv,
4272 thresh, BTRFS_RESERVE_NO_FLUSH);
4273 if (!ret)
4274 trans->chunk_bytes_reserved += thresh;
Liu Bo15d1ff82012-03-29 09:57:44 -04004275 }
4276}
4277
Chris Mason6324fbf2008-03-24 15:01:59 -04004278static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04004279 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04004280{
4281 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04004282 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04004283 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05004284 int ret = 0;
4285
Josef Bacikc6b305a2012-12-18 09:16:16 -05004286 /* Don't re-enter if we're already allocating a chunk */
4287 if (trans->allocating_chunk)
4288 return -ENOSPC;
4289
Chris Mason6324fbf2008-03-24 15:01:59 -04004290 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04004291 if (!space_info) {
4292 ret = update_space_info(extent_root->fs_info, flags,
4293 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004294 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04004295 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004296 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04004297
Josef Bacik6d741192011-04-11 20:20:11 -04004298again:
Josef Bacik25179202008-10-29 14:49:05 -04004299 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05004300 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04004301 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04004302 if (space_info->full) {
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004303 if (should_alloc_chunk(extent_root, space_info, force))
4304 ret = -ENOSPC;
4305 else
4306 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04004307 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01004308 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04004309 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004310
Josef Bacik698d0082012-09-12 14:08:47 -04004311 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04004312 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04004313 return 0;
4314 } else if (space_info->chunk_alloc) {
4315 wait_for_alloc = 1;
4316 } else {
4317 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04004318 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04004319
Josef Bacik25179202008-10-29 14:49:05 -04004320 spin_unlock(&space_info->lock);
4321
Josef Bacik6d741192011-04-11 20:20:11 -04004322 mutex_lock(&fs_info->chunk_mutex);
4323
4324 /*
4325 * The chunk_mutex is held throughout the entirety of a chunk
4326 * allocation, so once we've acquired the chunk_mutex we know that the
4327 * other guy is done and we need to recheck and see if we should
4328 * allocate.
4329 */
4330 if (wait_for_alloc) {
4331 mutex_unlock(&fs_info->chunk_mutex);
4332 wait_for_alloc = 0;
4333 goto again;
4334 }
4335
Josef Bacikc6b305a2012-12-18 09:16:16 -05004336 trans->allocating_chunk = true;
4337
Josef Bacik97e728d2009-04-21 17:40:57 -04004338 /*
Josef Bacik67377732010-09-16 16:19:09 -04004339 * If we have mixed data/metadata chunks we want to make sure we keep
4340 * allocating mixed chunks instead of individual chunks.
4341 */
4342 if (btrfs_mixed_space_info(space_info))
4343 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
4344
4345 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04004346 * if we're doing a data chunk, go ahead and make sure that
4347 * we keep a reasonable number of metadata chunks allocated in the
4348 * FS as well.
4349 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04004350 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04004351 fs_info->data_chunk_allocations++;
4352 if (!(fs_info->data_chunk_allocations %
4353 fs_info->metadata_ratio))
4354 force_metadata_allocation(fs_info);
4355 }
4356
Liu Bo15d1ff82012-03-29 09:57:44 -04004357 /*
4358 * Check if we have enough space in SYSTEM chunk because we may need
4359 * to update devices.
4360 */
Filipe Manana4617ea32015-06-09 17:48:21 +01004361 check_system_chunk(trans, extent_root, flags);
Liu Bo15d1ff82012-03-29 09:57:44 -04004362
Yan Zheng2b820322008-11-17 21:11:30 -05004363 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05004364 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004365
Josef Bacik9ed74f22009-09-11 16:12:44 -04004366 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004367 if (ret < 0 && ret != -ENOSPC)
4368 goto out;
Chris Masond3977122009-01-05 21:25:51 -05004369 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04004370 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04004371 else
4372 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04004373
Chris Mason0e4f8f82011-04-15 16:05:44 -04004374 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004375out:
Josef Bacik6d741192011-04-11 20:20:11 -04004376 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004377 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03004378 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana00d80e32015-07-20 14:56:20 +01004379 /*
4380 * When we allocate a new chunk we reserve space in the chunk block
4381 * reserve to make sure we can COW nodes/leafs in the chunk tree or
4382 * add new nodes/leafs to it if we end up needing to do it when
4383 * inserting the chunk item and updating device items as part of the
4384 * second phase of chunk allocation, performed by
4385 * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a
4386 * large number of new block groups to create in our transaction
4387 * handle's new_bgs list to avoid exhausting the chunk block reserve
4388 * in extreme cases - like having a single transaction create many new
4389 * block groups when starting to write out the free space caches of all
4390 * the block groups that were made dirty during the lifetime of the
4391 * transaction.
4392 */
Filipe Mananad9a05402015-10-03 13:13:13 +01004393 if (trans->can_flush_pending_bgs &&
4394 trans->chunk_bytes_reserved >= (2 * 1024 * 1024ull)) {
Filipe Manana00d80e32015-07-20 14:56:20 +01004395 btrfs_create_pending_block_groups(trans, trans->root);
4396 btrfs_trans_release_chunk_metadata(trans);
4397 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04004398 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04004399}
4400
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004401static int can_overcommit(struct btrfs_root *root,
4402 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00004403 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004404{
Josef Bacik96f1bb52013-01-30 17:02:51 -05004405 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004406 u64 profile = btrfs_get_alloc_profile(root, 0);
Miao Xie3c76cd82013-04-25 10:12:38 +00004407 u64 space_size;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004408 u64 avail;
4409 u64 used;
4410
4411 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05004412 space_info->bytes_pinned + space_info->bytes_readonly;
4413
Josef Bacik96f1bb52013-01-30 17:02:51 -05004414 /*
4415 * We only want to allow over committing if we have lots of actual space
4416 * free, but if we don't have enough space to handle the global reserve
4417 * space then we could end up having a real enospc problem when trying
4418 * to allocate a chunk or some other such important allocation.
4419 */
Miao Xie3c76cd82013-04-25 10:12:38 +00004420 spin_lock(&global_rsv->lock);
4421 space_size = calc_global_rsv_need_space(global_rsv);
4422 spin_unlock(&global_rsv->lock);
4423 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05004424 return 0;
4425
4426 used += space_info->bytes_may_use;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004427
4428 spin_lock(&root->fs_info->free_chunk_lock);
4429 avail = root->fs_info->free_chunk_space;
4430 spin_unlock(&root->fs_info->free_chunk_lock);
4431
4432 /*
4433 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05004434 * space is actually useable. For raid56, the space info used
4435 * doesn't include the parity drive, so we don't have to
4436 * change the math
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004437 */
4438 if (profile & (BTRFS_BLOCK_GROUP_DUP |
4439 BTRFS_BLOCK_GROUP_RAID1 |
4440 BTRFS_BLOCK_GROUP_RAID10))
4441 avail >>= 1;
4442
4443 /*
Miao Xie561c2942012-10-16 11:32:18 +00004444 * If we aren't flushing all things, let us overcommit up to
4445 * 1/2th of the space. If we can flush, don't let us overcommit
4446 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004447 */
Miao Xie08e007d2012-10-16 11:33:38 +00004448 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04004449 avail >>= 3;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004450 else
Josef Bacik14575ae2013-09-17 10:48:00 -04004451 avail >>= 1;
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004452
Josef Bacik14575ae2013-09-17 10:48:00 -04004453 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dcf2012-09-06 16:59:33 -04004454 return 1;
4455 return 0;
4456}
4457
Eric Sandeen48a3b632013-04-25 20:41:01 +00004458static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
Miao Xie6c255e62014-03-06 13:55:01 +08004459 unsigned long nr_pages, int nr_items)
Miao Xieda633a42012-12-20 11:19:09 +00004460{
4461 struct super_block *sb = root->fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00004462
Josef Bacik925a6ef2013-06-20 12:31:27 -04004463 if (down_read_trylock(&sb->s_umount)) {
4464 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4465 up_read(&sb->s_umount);
4466 } else {
Miao Xieda633a42012-12-20 11:19:09 +00004467 /*
4468 * We needn't worry the filesystem going from r/w to r/o though
4469 * we don't acquire ->s_umount mutex, because the filesystem
4470 * should guarantee the delalloc inodes list be empty after
4471 * the filesystem is readonly(all dirty pages are written to
4472 * the disk).
4473 */
Miao Xie6c255e62014-03-06 13:55:01 +08004474 btrfs_start_delalloc_roots(root->fs_info, 0, nr_items);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004475 if (!current->journal_info)
Miao Xie6c255e62014-03-06 13:55:01 +08004476 btrfs_wait_ordered_roots(root->fs_info, nr_items);
Miao Xieda633a42012-12-20 11:19:09 +00004477 }
4478}
4479
Miao Xie18cd8ea2013-11-04 23:13:22 +08004480static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim)
4481{
4482 u64 bytes;
4483 int nr;
4484
4485 bytes = btrfs_calc_trans_metadata_size(root, 1);
4486 nr = (int)div64_u64(to_reclaim, bytes);
4487 if (!nr)
4488 nr = 1;
4489 return nr;
4490}
4491
Miao Xiec61a16a2013-11-04 23:13:23 +08004492#define EXTENT_SIZE_PER_ITEM (256 * 1024)
4493
Yan, Zheng5da9d012010-05-16 10:46:25 -04004494/*
4495 * shrink metadata reservation for delalloc
4496 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04004497static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4498 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004499{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004500 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004501 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004502 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004503 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004504 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004505 long time_left;
Miao Xied3ee29e32013-11-04 23:13:20 +08004506 unsigned long nr_pages;
4507 int loops;
Miao Xieb0244192013-11-04 23:13:25 +08004508 int items;
Miao Xie08e007d2012-10-16 11:33:38 +00004509 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004510
Miao Xiec61a16a2013-11-04 23:13:23 +08004511 /* Calc the number of the pages we need flush for space reservation */
Miao Xieb0244192013-11-04 23:13:25 +08004512 items = calc_reclaim_items_nr(root, to_reclaim);
4513 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004514
Josef Bacik663350a2011-11-03 22:54:25 -04004515 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004516 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004517 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04004518
Miao Xie963d6782013-01-29 10:10:51 +00004519 delalloc_bytes = percpu_counter_sum_positive(
4520 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004521 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004522 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004523 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004524 if (wait_ordered)
Miao Xieb0244192013-11-04 23:13:25 +08004525 btrfs_wait_ordered_roots(root->fs_info, items);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004526 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004527 }
4528
Miao Xied3ee29e32013-11-04 23:13:20 +08004529 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004530 while (delalloc_bytes && loops < 3) {
4531 max_reclaim = min(delalloc_bytes, to_reclaim);
4532 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xie6c255e62014-03-06 13:55:01 +08004533 btrfs_writeback_inodes_sb_nr(root, nr_pages, items);
Josef Bacikdea31f52012-09-06 16:47:00 -04004534 /*
4535 * We need to wait for the async pages to actually start before
4536 * we do anything.
4537 */
Miao Xie9f3a0742013-11-04 23:13:24 +08004538 max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages);
4539 if (!max_reclaim)
4540 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004541
Miao Xie9f3a0742013-11-04 23:13:24 +08004542 if (max_reclaim <= nr_pages)
4543 max_reclaim = 0;
4544 else
4545 max_reclaim -= nr_pages;
4546
4547 wait_event(root->fs_info->async_submit_wait,
4548 atomic_read(&root->fs_info->async_delalloc_pages) <=
4549 (int)max_reclaim);
4550skip_async:
Miao Xie08e007d2012-10-16 11:33:38 +00004551 if (!trans)
4552 flush = BTRFS_RESERVE_FLUSH_ALL;
4553 else
4554 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04004555 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00004556 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004557 spin_unlock(&space_info->lock);
4558 break;
4559 }
Josef Bacik0019f102010-10-15 15:18:40 -04004560 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004561
Chris Mason36e39c42011-03-12 07:08:42 -05004562 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004563 if (wait_ordered && !trans) {
Miao Xieb0244192013-11-04 23:13:25 +08004564 btrfs_wait_ordered_roots(root->fs_info, items);
Josef Bacikf104d042011-10-14 13:56:58 -04004565 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004566 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004567 if (time_left)
4568 break;
4569 }
Miao Xie963d6782013-01-29 10:10:51 +00004570 delalloc_bytes = percpu_counter_sum_positive(
4571 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004572 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004573}
4574
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004575/**
Josef Bacik663350a2011-11-03 22:54:25 -04004576 * maybe_commit_transaction - possibly commit the transaction if its ok to
4577 * @root - the root we're allocating for
4578 * @bytes - the number of bytes we want to reserve
4579 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004580 *
Josef Bacik663350a2011-11-03 22:54:25 -04004581 * This will check to make sure that committing the transaction will actually
4582 * get us somewhere and then commit the transaction if it does. Otherwise it
4583 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004584 */
Josef Bacik663350a2011-11-03 22:54:25 -04004585static int may_commit_transaction(struct btrfs_root *root,
4586 struct btrfs_space_info *space_info,
4587 u64 bytes, int force)
4588{
4589 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4590 struct btrfs_trans_handle *trans;
4591
4592 trans = (struct btrfs_trans_handle *)current->journal_info;
4593 if (trans)
4594 return -EAGAIN;
4595
4596 if (force)
4597 goto commit;
4598
4599 /* See if there is enough pinned space to make this reservation */
Josef Bacikb150a4f2013-06-19 15:00:04 -04004600 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Miao Xie0424c542014-03-06 13:54:59 +08004601 bytes) >= 0)
Josef Bacik663350a2011-11-03 22:54:25 -04004602 goto commit;
Josef Bacik663350a2011-11-03 22:54:25 -04004603
4604 /*
4605 * See if there is some space in the delayed insertion reservation for
4606 * this reservation.
4607 */
4608 if (space_info != delayed_rsv->space_info)
4609 return -ENOSPC;
4610
4611 spin_lock(&delayed_rsv->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004612 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4613 bytes - delayed_rsv->size) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004614 spin_unlock(&delayed_rsv->lock);
4615 return -ENOSPC;
4616 }
4617 spin_unlock(&delayed_rsv->lock);
4618
4619commit:
4620 trans = btrfs_join_transaction(root);
4621 if (IS_ERR(trans))
4622 return -ENOSPC;
4623
4624 return btrfs_commit_transaction(trans, root);
4625}
4626
Josef Bacik96c3f432012-06-21 14:05:49 -04004627enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04004628 FLUSH_DELAYED_ITEMS_NR = 1,
4629 FLUSH_DELAYED_ITEMS = 2,
4630 FLUSH_DELALLOC = 3,
4631 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04004632 ALLOC_CHUNK = 5,
4633 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04004634};
4635
4636static int flush_space(struct btrfs_root *root,
4637 struct btrfs_space_info *space_info, u64 num_bytes,
4638 u64 orig_bytes, int state)
4639{
4640 struct btrfs_trans_handle *trans;
4641 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004642 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004643
4644 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004645 case FLUSH_DELAYED_ITEMS_NR:
4646 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08004647 if (state == FLUSH_DELAYED_ITEMS_NR)
4648 nr = calc_reclaim_items_nr(root, num_bytes) * 2;
4649 else
Josef Bacik96c3f432012-06-21 14:05:49 -04004650 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004651
Josef Bacik96c3f432012-06-21 14:05:49 -04004652 trans = btrfs_join_transaction(root);
4653 if (IS_ERR(trans)) {
4654 ret = PTR_ERR(trans);
4655 break;
4656 }
4657 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4658 btrfs_end_transaction(trans, root);
4659 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004660 case FLUSH_DELALLOC:
4661 case FLUSH_DELALLOC_WAIT:
Miao Xie24af7dd2014-03-06 13:55:00 +08004662 shrink_delalloc(root, num_bytes * 2, orig_bytes,
Josef Bacik67b0fd62012-09-24 13:42:00 -04004663 state == FLUSH_DELALLOC_WAIT);
4664 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004665 case ALLOC_CHUNK:
4666 trans = btrfs_join_transaction(root);
4667 if (IS_ERR(trans)) {
4668 ret = PTR_ERR(trans);
4669 break;
4670 }
4671 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004672 btrfs_get_alloc_profile(root, 0),
4673 CHUNK_ALLOC_NO_FORCE);
4674 btrfs_end_transaction(trans, root);
4675 if (ret == -ENOSPC)
4676 ret = 0;
4677 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004678 case COMMIT_TRANS:
4679 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4680 break;
4681 default:
4682 ret = -ENOSPC;
4683 break;
4684 }
4685
4686 return ret;
4687}
Miao Xie21c7e752014-05-13 17:29:04 -07004688
4689static inline u64
4690btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
4691 struct btrfs_space_info *space_info)
4692{
4693 u64 used;
4694 u64 expected;
4695 u64 to_reclaim;
4696
4697 to_reclaim = min_t(u64, num_online_cpus() * 1024 * 1024,
4698 16 * 1024 * 1024);
4699 spin_lock(&space_info->lock);
4700 if (can_overcommit(root, space_info, to_reclaim,
4701 BTRFS_RESERVE_FLUSH_ALL)) {
4702 to_reclaim = 0;
4703 goto out;
4704 }
4705
4706 used = space_info->bytes_used + space_info->bytes_reserved +
4707 space_info->bytes_pinned + space_info->bytes_readonly +
4708 space_info->bytes_may_use;
4709 if (can_overcommit(root, space_info, 1024 * 1024,
4710 BTRFS_RESERVE_FLUSH_ALL))
4711 expected = div_factor_fine(space_info->total_bytes, 95);
4712 else
4713 expected = div_factor_fine(space_info->total_bytes, 90);
4714
4715 if (used > expected)
4716 to_reclaim = used - expected;
4717 else
4718 to_reclaim = 0;
4719 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
4720 space_info->bytes_reserved);
4721out:
4722 spin_unlock(&space_info->lock);
4723
4724 return to_reclaim;
4725}
4726
4727static inline int need_do_async_reclaim(struct btrfs_space_info *space_info,
4728 struct btrfs_fs_info *fs_info, u64 used)
4729{
Josef Bacik365c5312015-02-18 13:58:15 -08004730 u64 thresh = div_factor_fine(space_info->total_bytes, 98);
4731
4732 /* If we're just plain full then async reclaim just slows us down. */
4733 if (space_info->bytes_used >= thresh)
4734 return 0;
4735
4736 return (used >= thresh && !btrfs_fs_closing(fs_info) &&
Miao Xie21c7e752014-05-13 17:29:04 -07004737 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
4738}
4739
4740static int btrfs_need_do_async_reclaim(struct btrfs_space_info *space_info,
Liu Bo25ce4592014-09-10 12:58:50 +08004741 struct btrfs_fs_info *fs_info,
4742 int flush_state)
Miao Xie21c7e752014-05-13 17:29:04 -07004743{
4744 u64 used;
4745
4746 spin_lock(&space_info->lock);
Liu Bo25ce4592014-09-10 12:58:50 +08004747 /*
4748 * We run out of space and have not got any free space via flush_space,
4749 * so don't bother doing async reclaim.
4750 */
4751 if (flush_state > COMMIT_TRANS && space_info->full) {
4752 spin_unlock(&space_info->lock);
4753 return 0;
4754 }
4755
Miao Xie21c7e752014-05-13 17:29:04 -07004756 used = space_info->bytes_used + space_info->bytes_reserved +
4757 space_info->bytes_pinned + space_info->bytes_readonly +
4758 space_info->bytes_may_use;
4759 if (need_do_async_reclaim(space_info, fs_info, used)) {
4760 spin_unlock(&space_info->lock);
4761 return 1;
4762 }
4763 spin_unlock(&space_info->lock);
4764
4765 return 0;
4766}
4767
4768static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
4769{
4770 struct btrfs_fs_info *fs_info;
4771 struct btrfs_space_info *space_info;
4772 u64 to_reclaim;
4773 int flush_state;
4774
4775 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
4776 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4777
4778 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
4779 space_info);
4780 if (!to_reclaim)
4781 return;
4782
4783 flush_state = FLUSH_DELAYED_ITEMS_NR;
4784 do {
4785 flush_space(fs_info->fs_root, space_info, to_reclaim,
4786 to_reclaim, flush_state);
4787 flush_state++;
Liu Bo25ce4592014-09-10 12:58:50 +08004788 if (!btrfs_need_do_async_reclaim(space_info, fs_info,
4789 flush_state))
Miao Xie21c7e752014-05-13 17:29:04 -07004790 return;
Josef Bacik365c5312015-02-18 13:58:15 -08004791 } while (flush_state < COMMIT_TRANS);
Miao Xie21c7e752014-05-13 17:29:04 -07004792}
4793
4794void btrfs_init_async_reclaim_work(struct work_struct *work)
4795{
4796 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
4797}
4798
Josef Bacik663350a2011-11-03 22:54:25 -04004799/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004800 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4801 * @root - the root we're allocating for
4802 * @block_rsv - the block_rsv we're allocating for
4803 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004804 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004805 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004806 * This will reserve orgi_bytes number of bytes from the space info associated
4807 * with the block_rsv. If there is not enough space it will make an attempt to
4808 * flush out space to make room. It will do this by flushing delalloc if
4809 * possible or committing the transaction. If flush is 0 then no attempts to
4810 * regain reservations will be made and this will fail if there is not enough
4811 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004812 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004813static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004814 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004815 u64 orig_bytes,
4816 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004817{
4818 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004819 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004820 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004821 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004822 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004823 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004824
4825again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004826 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004827 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004828 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004829 * We only want to wait if somebody other than us is flushing and we
4830 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004831 */
Miao Xie08e007d2012-10-16 11:33:38 +00004832 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4833 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004834 spin_unlock(&space_info->lock);
4835 /*
4836 * If we have a trans handle we can't wait because the flusher
4837 * may have to commit the transaction, which would mean we would
4838 * deadlock since we are waiting for the flusher to finish, but
4839 * hold the current transaction open.
4840 */
Josef Bacik663350a2011-11-03 22:54:25 -04004841 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004842 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004843 ret = wait_event_killable(space_info->wait, !space_info->flush);
4844 /* Must have been killed, return */
4845 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004846 return -EINTR;
4847
4848 spin_lock(&space_info->lock);
4849 }
4850
4851 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004852 used = space_info->bytes_used + space_info->bytes_reserved +
4853 space_info->bytes_pinned + space_info->bytes_readonly +
4854 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004855
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004856 /*
4857 * The idea here is that we've not already over-reserved the block group
4858 * then we can go ahead and save our reservation first and then start
4859 * flushing if we need to. Otherwise if we've already overcommitted
4860 * lets start flushing stuff first and then come back and try to make
4861 * our reservation.
4862 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004863 if (used <= space_info->total_bytes) {
4864 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004865 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004866 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004867 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004868 ret = 0;
4869 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004870 /*
4871 * Ok set num_bytes to orig_bytes since we aren't
4872 * overocmmitted, this way we only try and reclaim what
4873 * we need.
4874 */
4875 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004876 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004877 } else {
4878 /*
4879 * Ok we're over committed, set num_bytes to the overcommitted
4880 * amount plus the amount of bytes that we need for this
4881 * reservation.
4882 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004883 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004884 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004885 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004886
Josef Bacik44734ed2012-09-28 16:04:19 -04004887 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4888 space_info->bytes_may_use += orig_bytes;
4889 trace_btrfs_space_reservation(root->fs_info, "space_info",
4890 space_info->flags, orig_bytes,
4891 1);
4892 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004893 }
4894
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004895 /*
4896 * Couldn't make our reservation, save our place so while we're trying
4897 * to reclaim space we can actually use it instead of somebody else
4898 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004899 *
4900 * We make the other tasks wait for the flush only when we can flush
4901 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004902 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004903 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004904 flushing = true;
4905 space_info->flush = 1;
Miao Xie21c7e752014-05-13 17:29:04 -07004906 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
4907 used += orig_bytes;
Josef Bacikf6acfd52014-09-18 11:27:17 -04004908 /*
4909 * We will do the space reservation dance during log replay,
4910 * which means we won't have fs_info->fs_root set, so don't do
4911 * the async reclaim as we will panic.
4912 */
4913 if (!root->fs_info->log_root_recovering &&
4914 need_do_async_reclaim(space_info, root->fs_info, used) &&
Miao Xie21c7e752014-05-13 17:29:04 -07004915 !work_busy(&root->fs_info->async_reclaim_work))
4916 queue_work(system_unbound_wq,
4917 &root->fs_info->async_reclaim_work);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004918 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004919 spin_unlock(&space_info->lock);
4920
Miao Xie08e007d2012-10-16 11:33:38 +00004921 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004922 goto out;
4923
Josef Bacik96c3f432012-06-21 14:05:49 -04004924 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4925 flush_state);
4926 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004927
4928 /*
4929 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4930 * would happen. So skip delalloc flush.
4931 */
4932 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4933 (flush_state == FLUSH_DELALLOC ||
4934 flush_state == FLUSH_DELALLOC_WAIT))
4935 flush_state = ALLOC_CHUNK;
4936
Josef Bacik96c3f432012-06-21 14:05:49 -04004937 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004938 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004939 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4940 flush_state < COMMIT_TRANS)
4941 goto again;
4942 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4943 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004944 goto again;
4945
4946out:
Josef Bacik5d803662013-02-07 16:06:02 -05004947 if (ret == -ENOSPC &&
4948 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4949 struct btrfs_block_rsv *global_rsv =
4950 &root->fs_info->global_block_rsv;
4951
4952 if (block_rsv != global_rsv &&
4953 !block_rsv_use_bytes(global_rsv, orig_bytes))
4954 ret = 0;
4955 }
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004956 if (ret == -ENOSPC)
4957 trace_btrfs_space_reservation(root->fs_info,
4958 "space_info:enospc",
4959 space_info->flags, orig_bytes, 1);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004960 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004961 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004962 space_info->flush = 0;
4963 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004964 spin_unlock(&space_info->lock);
4965 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004966 return ret;
4967}
4968
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004969static struct btrfs_block_rsv *get_block_rsv(
4970 const struct btrfs_trans_handle *trans,
4971 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004972{
Josef Bacik4c13d752011-08-30 11:31:29 -04004973 struct btrfs_block_rsv *block_rsv = NULL;
4974
Alexandru Moisee9cf4392015-09-09 00:18:50 +00004975 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4976 (root == root->fs_info->csum_root && trans->adding_csums) ||
4977 (root == root->fs_info->uuid_root))
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004978 block_rsv = trans->block_rsv;
4979
Josef Bacik4c13d752011-08-30 11:31:29 -04004980 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004981 block_rsv = root->block_rsv;
4982
4983 if (!block_rsv)
4984 block_rsv = &root->fs_info->empty_block_rsv;
4985
4986 return block_rsv;
4987}
4988
4989static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4990 u64 num_bytes)
4991{
4992 int ret = -ENOSPC;
4993 spin_lock(&block_rsv->lock);
4994 if (block_rsv->reserved >= num_bytes) {
4995 block_rsv->reserved -= num_bytes;
4996 if (block_rsv->reserved < block_rsv->size)
4997 block_rsv->full = 0;
4998 ret = 0;
4999 }
5000 spin_unlock(&block_rsv->lock);
5001 return ret;
5002}
5003
5004static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
5005 u64 num_bytes, int update_size)
5006{
5007 spin_lock(&block_rsv->lock);
5008 block_rsv->reserved += num_bytes;
5009 if (update_size)
5010 block_rsv->size += num_bytes;
5011 else if (block_rsv->reserved >= block_rsv->size)
5012 block_rsv->full = 1;
5013 spin_unlock(&block_rsv->lock);
5014}
5015
Josef Bacikd52be812013-05-29 14:54:47 -04005016int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
5017 struct btrfs_block_rsv *dest, u64 num_bytes,
5018 int min_factor)
5019{
5020 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5021 u64 min_bytes;
5022
5023 if (global_rsv->space_info != dest->space_info)
5024 return -ENOSPC;
5025
5026 spin_lock(&global_rsv->lock);
5027 min_bytes = div_factor(global_rsv->size, min_factor);
5028 if (global_rsv->reserved < min_bytes + num_bytes) {
5029 spin_unlock(&global_rsv->lock);
5030 return -ENOSPC;
5031 }
5032 global_rsv->reserved -= num_bytes;
5033 if (global_rsv->reserved < global_rsv->size)
5034 global_rsv->full = 0;
5035 spin_unlock(&global_rsv->lock);
5036
5037 block_rsv_add_bytes(dest, num_bytes, 1);
5038 return 0;
5039}
5040
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005041static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
5042 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02005043 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005044{
5045 struct btrfs_space_info *space_info = block_rsv->space_info;
5046
5047 spin_lock(&block_rsv->lock);
5048 if (num_bytes == (u64)-1)
5049 num_bytes = block_rsv->size;
5050 block_rsv->size -= num_bytes;
5051 if (block_rsv->reserved >= block_rsv->size) {
5052 num_bytes = block_rsv->reserved - block_rsv->size;
5053 block_rsv->reserved = block_rsv->size;
5054 block_rsv->full = 1;
5055 } else {
5056 num_bytes = 0;
5057 }
5058 spin_unlock(&block_rsv->lock);
5059
5060 if (num_bytes > 0) {
5061 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00005062 spin_lock(&dest->lock);
5063 if (!dest->full) {
5064 u64 bytes_to_add;
5065
5066 bytes_to_add = dest->size - dest->reserved;
5067 bytes_to_add = min(num_bytes, bytes_to_add);
5068 dest->reserved += bytes_to_add;
5069 if (dest->reserved >= dest->size)
5070 dest->full = 1;
5071 num_bytes -= bytes_to_add;
5072 }
5073 spin_unlock(&dest->lock);
5074 }
5075 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005076 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04005077 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005078 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005079 space_info->flags, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005080 spin_unlock(&space_info->lock);
5081 }
5082 }
5083}
5084
5085static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
5086 struct btrfs_block_rsv *dst, u64 num_bytes)
5087{
5088 int ret;
5089
5090 ret = block_rsv_use_bytes(src, num_bytes);
5091 if (ret)
5092 return ret;
5093
5094 block_rsv_add_bytes(dst, num_bytes, 1);
5095 return 0;
5096}
5097
Miao Xie66d8f3d2012-09-06 04:02:28 -06005098void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005099{
5100 memset(rsv, 0, sizeof(*rsv));
5101 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06005102 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005103}
5104
Miao Xie66d8f3d2012-09-06 04:02:28 -06005105struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
5106 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005107{
5108 struct btrfs_block_rsv *block_rsv;
5109 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005110
5111 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
5112 if (!block_rsv)
5113 return NULL;
5114
Miao Xie66d8f3d2012-09-06 04:02:28 -06005115 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005116 block_rsv->space_info = __find_space_info(fs_info,
5117 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005118 return block_rsv;
5119}
5120
5121void btrfs_free_block_rsv(struct btrfs_root *root,
5122 struct btrfs_block_rsv *rsv)
5123{
Josef Bacik2aaa6652012-08-29 14:27:18 -04005124 if (!rsv)
5125 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04005126 btrfs_block_rsv_release(root, rsv, (u64)-1);
5127 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005128}
5129
Chris Masoncdfb0802015-04-06 18:17:00 -07005130void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
5131{
5132 kfree(rsv);
5133}
5134
Miao Xie08e007d2012-10-16 11:33:38 +00005135int btrfs_block_rsv_add(struct btrfs_root *root,
5136 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
5137 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005138{
5139 int ret;
5140
5141 if (num_bytes == 0)
5142 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005143
Miao Xie61b520a2011-11-10 20:45:05 -05005144 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005145 if (!ret) {
5146 block_rsv_add_bytes(block_rsv, num_bytes, 1);
5147 return 0;
5148 }
5149
Yan, Zhengf0486c62010-05-16 10:46:25 -04005150 return ret;
5151}
5152
Josef Bacik4a92b1b2011-08-30 12:34:28 -04005153int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04005154 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005155{
5156 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005157 int ret = -ENOSPC;
5158
5159 if (!block_rsv)
5160 return 0;
5161
5162 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04005163 num_bytes = div_factor(block_rsv->size, min_factor);
5164 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005165 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005166 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005167
Josef Bacik36ba0222011-10-18 12:15:48 -04005168 return ret;
5169}
5170
Miao Xie08e007d2012-10-16 11:33:38 +00005171int btrfs_block_rsv_refill(struct btrfs_root *root,
5172 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
5173 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04005174{
5175 u64 num_bytes = 0;
5176 int ret = -ENOSPC;
5177
5178 if (!block_rsv)
5179 return 0;
5180
5181 spin_lock(&block_rsv->lock);
5182 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04005183 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005184 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04005185 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04005186 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005187 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04005188
Yan, Zhengf0486c62010-05-16 10:46:25 -04005189 if (!ret)
5190 return 0;
5191
Miao Xieaa38a712011-11-18 17:43:00 +08005192 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04005193 if (!ret) {
5194 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005195 return 0;
5196 }
5197
Josef Bacik13553e52011-08-08 13:33:21 -04005198 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005199}
5200
5201int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
5202 struct btrfs_block_rsv *dst_rsv,
5203 u64 num_bytes)
5204{
5205 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
5206}
5207
5208void btrfs_block_rsv_release(struct btrfs_root *root,
5209 struct btrfs_block_rsv *block_rsv,
5210 u64 num_bytes)
5211{
5212 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Liu Bo17504582013-12-29 21:44:50 +08005213 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04005214 block_rsv->space_info != global_rsv->space_info)
5215 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005216 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
5217 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005218}
5219
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005220/*
5221 * helper to calculate size of global block reservation.
5222 * the desired value is sum of space used by extent tree,
5223 * checksum tree and root tree
5224 */
5225static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
5226{
5227 struct btrfs_space_info *sinfo;
5228 u64 num_bytes;
5229 u64 meta_used;
5230 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02005231 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005232
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005233 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
5234 spin_lock(&sinfo->lock);
5235 data_used = sinfo->bytes_used;
5236 spin_unlock(&sinfo->lock);
5237
5238 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
5239 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04005240 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
5241 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005242 meta_used = sinfo->bytes_used;
5243 spin_unlock(&sinfo->lock);
5244
5245 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
5246 csum_size * 2;
David Sterbaf8c269d2015-01-16 17:21:12 +01005247 num_bytes += div_u64(data_used + meta_used, 50);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005248
5249 if (num_bytes * 3 > meta_used)
David Sterbaf8c269d2015-01-16 17:21:12 +01005250 num_bytes = div_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005251
David Sterba707e8a02014-06-04 19:22:26 +02005252 return ALIGN(num_bytes, fs_info->extent_root->nodesize << 10);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005253}
5254
5255static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
5256{
5257 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
5258 struct btrfs_space_info *sinfo = block_rsv->space_info;
5259 u64 num_bytes;
5260
5261 num_bytes = calc_global_metadata_size(fs_info);
5262
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005263 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005264 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005265
Josef Bacikfdf30d12013-03-26 15:31:45 -04005266 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005267
5268 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04005269 sinfo->bytes_reserved + sinfo->bytes_readonly +
5270 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005271
5272 if (sinfo->total_bytes > num_bytes) {
5273 num_bytes = sinfo->total_bytes - num_bytes;
5274 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04005275 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005276 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005277 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005278 }
5279
5280 if (block_rsv->reserved >= block_rsv->size) {
5281 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04005282 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005283 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04005284 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005285 block_rsv->reserved = block_rsv->size;
5286 block_rsv->full = 1;
5287 }
David Sterba182608c2011-05-05 13:13:16 +02005288
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005289 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04005290 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005291}
5292
Yan, Zhengf0486c62010-05-16 10:46:25 -04005293static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
5294{
5295 struct btrfs_space_info *space_info;
5296
5297 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
5298 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005299
5300 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005301 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005302 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005303 fs_info->trans_block_rsv.space_info = space_info;
5304 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04005305 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005306
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005307 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
5308 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
5309 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
5310 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00005311 if (fs_info->quota_root)
5312 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005313 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005314
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005315 update_global_block_rsv(fs_info);
5316}
5317
5318static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
5319{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005320 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
5321 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005322 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
5323 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
5324 WARN_ON(fs_info->trans_block_rsv.size > 0);
5325 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
5326 WARN_ON(fs_info->chunk_block_rsv.size > 0);
5327 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04005328 WARN_ON(fs_info->delayed_block_rsv.size > 0);
5329 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04005330}
5331
Yan, Zhenga22285a2010-05-16 10:48:46 -04005332void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
5333 struct btrfs_root *root)
5334{
Josef Bacik0e721102012-06-26 16:13:18 -04005335 if (!trans->block_rsv)
5336 return;
5337
Yan, Zhenga22285a2010-05-16 10:48:46 -04005338 if (!trans->bytes_reserved)
5339 return;
5340
Chris Masone77266e2012-02-24 10:39:05 -05005341 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04005342 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04005343 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005344 trans->bytes_reserved = 0;
5345}
5346
Filipe Manana4fbcdf62015-05-20 14:01:54 +01005347/*
5348 * To be called after all the new block groups attached to the transaction
5349 * handle have been created (btrfs_create_pending_block_groups()).
5350 */
5351void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
5352{
5353 struct btrfs_fs_info *fs_info = trans->root->fs_info;
5354
5355 if (!trans->chunk_bytes_reserved)
5356 return;
5357
5358 WARN_ON_ONCE(!list_empty(&trans->new_bgs));
5359
5360 block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL,
5361 trans->chunk_bytes_reserved);
5362 trans->chunk_bytes_reserved = 0;
5363}
5364
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005365/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04005366int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
5367 struct inode *inode)
5368{
5369 struct btrfs_root *root = BTRFS_I(inode)->root;
5370 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
5371 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
5372
5373 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04005374 * We need to hold space in order to delete our orphan item once we've
5375 * added it, so this takes the reservation so we can release it later
5376 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04005377 */
Chris Masonff5714c2011-05-28 07:00:39 -04005378 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005379 trace_btrfs_space_reservation(root->fs_info, "orphan",
5380 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005381 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
5382}
5383
5384void btrfs_orphan_release_metadata(struct inode *inode)
5385{
5386 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04005387 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005388 trace_btrfs_space_reservation(root->fs_info, "orphan",
5389 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04005390 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
5391}
5392
Miao Xied5c12072013-02-28 10:04:33 +00005393/*
5394 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
5395 * root: the root of the parent directory
5396 * rsv: block reservation
5397 * items: the number of items that we need do reservation
5398 * qgroup_reserved: used to return the reserved size in qgroup
5399 *
5400 * This function is used to reserve the space for snapshot/subvolume
5401 * creation and deletion. Those operations are different with the
5402 * common file/directory operations, they change two fs/file trees
5403 * and root tree, the number of items that the qgroup reserves is
5404 * different with the free space reservation. So we can not use
5405 * the space reseravtion mechanism in start_transaction().
5406 */
5407int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
5408 struct btrfs_block_rsv *rsv,
5409 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005410 u64 *qgroup_reserved,
5411 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04005412{
Miao Xied5c12072013-02-28 10:04:33 +00005413 u64 num_bytes;
5414 int ret;
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005415 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00005416
5417 if (root->fs_info->quota_enabled) {
5418 /* One for parent inode, two for dir entries */
David Sterba707e8a02014-06-04 19:22:26 +02005419 num_bytes = 3 * root->nodesize;
Miao Xied5c12072013-02-28 10:04:33 +00005420 ret = btrfs_qgroup_reserve(root, num_bytes);
5421 if (ret)
5422 return ret;
5423 } else {
5424 num_bytes = 0;
5425 }
5426
5427 *qgroup_reserved = num_bytes;
5428
5429 num_bytes = btrfs_calc_trans_metadata_size(root, items);
5430 rsv->space_info = __find_space_info(root->fs_info,
5431 BTRFS_BLOCK_GROUP_METADATA);
5432 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
5433 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005434
5435 if (ret == -ENOSPC && use_global_rsv)
5436 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
5437
Miao Xied5c12072013-02-28 10:04:33 +00005438 if (ret) {
5439 if (*qgroup_reserved)
5440 btrfs_qgroup_free(root, *qgroup_reserved);
5441 }
5442
5443 return ret;
5444}
5445
5446void btrfs_subvolume_release_metadata(struct btrfs_root *root,
5447 struct btrfs_block_rsv *rsv,
5448 u64 qgroup_reserved)
5449{
5450 btrfs_block_rsv_release(root, rsv, (u64)-1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005451}
5452
Josef Bacik7709cde2011-08-04 10:25:02 -04005453/**
5454 * drop_outstanding_extent - drop an outstanding extent
5455 * @inode: the inode we're dropping the extent for
Josef Bacikdcab6a32015-02-11 15:08:59 -05005456 * @num_bytes: the number of bytes we're relaseing.
Josef Bacik7709cde2011-08-04 10:25:02 -04005457 *
5458 * This is called when we are freeing up an outstanding extent, either called
5459 * after an error or after an extent is written. This will return the number of
5460 * reserved extents that need to be freed. This must be called with
5461 * BTRFS_I(inode)->lock held.
5462 */
Josef Bacikdcab6a32015-02-11 15:08:59 -05005463static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes)
Josef Bacik9e0baf62011-07-15 15:16:44 +00005464{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005465 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005466 unsigned dropped_extents = 0;
Josef Bacikdcab6a32015-02-11 15:08:59 -05005467 unsigned num_extents = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005468
Josef Bacikdcab6a32015-02-11 15:08:59 -05005469 num_extents = (unsigned)div64_u64(num_bytes +
5470 BTRFS_MAX_EXTENT_SIZE - 1,
5471 BTRFS_MAX_EXTENT_SIZE);
5472 ASSERT(num_extents);
5473 ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents);
5474 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005475
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005476 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04005477 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5478 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005479 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005480
Josef Bacik9e0baf62011-07-15 15:16:44 +00005481 /*
5482 * If we have more or the same amount of outsanding extents than we have
5483 * reserved then we need to leave the reserved extents count alone.
5484 */
5485 if (BTRFS_I(inode)->outstanding_extents >=
5486 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005487 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005488
5489 dropped_extents = BTRFS_I(inode)->reserved_extents -
5490 BTRFS_I(inode)->outstanding_extents;
5491 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005492 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005493}
5494
Josef Bacik7709cde2011-08-04 10:25:02 -04005495/**
5496 * calc_csum_metadata_size - return the amount of metada space that must be
5497 * reserved/free'd for the given bytes.
5498 * @inode: the inode we're manipulating
5499 * @num_bytes: the number of bytes in question
5500 * @reserve: 1 if we are reserving space, 0 if we are freeing space
5501 *
5502 * This adjusts the number of csum_bytes in the inode and then returns the
5503 * correct amount of metadata that must either be reserved or freed. We
5504 * calculate how many checksums we can fit into one leaf and then divide the
5505 * number of bytes that will need to be checksumed by this value to figure out
5506 * how many checksums will be required. If we are adding bytes then the number
5507 * may go up and we will return the number of additional bytes that must be
5508 * reserved. If it is going down we will return the number of bytes that must
5509 * be freed.
5510 *
5511 * This must be called with BTRFS_I(inode)->lock held.
5512 */
5513static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
5514 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005515{
Josef Bacik7709cde2011-08-04 10:25:02 -04005516 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik12621332015-02-03 07:50:16 -08005517 u64 old_csums, num_csums;
Josef Bacik7709cde2011-08-04 10:25:02 -04005518
5519 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
5520 BTRFS_I(inode)->csum_bytes == 0)
5521 return 0;
5522
Chris Mason28f75a02015-02-04 06:59:29 -08005523 old_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04005524 if (reserve)
5525 BTRFS_I(inode)->csum_bytes += num_bytes;
5526 else
5527 BTRFS_I(inode)->csum_bytes -= num_bytes;
Chris Mason28f75a02015-02-04 06:59:29 -08005528 num_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04005529
5530 /* No change, no need to reserve more */
5531 if (old_csums == num_csums)
5532 return 0;
5533
5534 if (reserve)
5535 return btrfs_calc_trans_metadata_size(root,
5536 num_csums - old_csums);
5537
5538 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005539}
5540
5541int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
5542{
5543 struct btrfs_root *root = BTRFS_I(inode)->root;
5544 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005545 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05005546 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005547 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05005548 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00005549 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07005550 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005551 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00005552 u64 to_free = 0;
5553 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005554
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005555 /* If we are a free space inode we need to not flush since we will be in
5556 * the middle of a transaction commit. We also don't need the delalloc
5557 * mutex since we won't race with anybody. We need this mostly to make
5558 * lockdep shut its filthy mouth.
5559 */
5560 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00005561 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005562 delalloc_lock = false;
5563 }
Josef Bacikc09544e2011-08-30 10:19:10 -04005564
Miao Xie08e007d2012-10-16 11:33:38 +00005565 if (flush != BTRFS_RESERVE_NO_FLUSH &&
5566 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005567 schedule_timeout(1);
5568
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005569 if (delalloc_lock)
5570 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
5571
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005572 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005573
Josef Bacik9e0baf62011-07-15 15:16:44 +00005574 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik6a41dd02015-03-13 15:12:23 -04005575 nr_extents = (unsigned)div64_u64(num_bytes +
5576 BTRFS_MAX_EXTENT_SIZE - 1,
5577 BTRFS_MAX_EXTENT_SIZE);
5578 BTRFS_I(inode)->outstanding_extents += nr_extents;
5579 nr_extents = 0;
Josef Bacik57a45ced2011-01-25 16:30:38 -05005580
Josef Bacik9e0baf62011-07-15 15:16:44 +00005581 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05005582 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00005583 nr_extents = BTRFS_I(inode)->outstanding_extents -
5584 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005585
5586 /*
5587 * Add an item to reserve for updating the inode when we complete the
5588 * delalloc io.
5589 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04005590 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5591 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005592 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05005593 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005594 }
5595
5596 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04005597 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05005598 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005599 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05005600
Wang Shilong88e081bf2013-03-01 11:36:01 +00005601 if (root->fs_info->quota_enabled) {
Dongsheng Yang237c0e92014-12-29 06:23:05 -05005602 ret = btrfs_qgroup_reserve(root, nr_extents * root->nodesize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005603 if (ret)
5604 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00005605 }
Arne Jansenc5567232011-09-14 15:44:05 +02005606
Wang Shilong88e081bf2013-03-01 11:36:01 +00005607 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
5608 if (unlikely(ret)) {
5609 if (root->fs_info->quota_enabled)
Dongsheng Yang237c0e92014-12-29 06:23:05 -05005610 btrfs_qgroup_free(root, nr_extents * root->nodesize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005611 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005612 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005613
Josef Bacik660d3f62011-12-09 11:18:51 -05005614 spin_lock(&BTRFS_I(inode)->lock);
5615 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04005616 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5617 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05005618 nr_extents--;
5619 }
5620 BTRFS_I(inode)->reserved_extents += nr_extents;
5621 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005622
5623 if (delalloc_lock)
5624 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05005625
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005626 if (to_reserve)
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05305627 trace_btrfs_space_reservation(root->fs_info, "delalloc",
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005628 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005629 block_rsv_add_bytes(block_rsv, to_reserve, 1);
5630
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005631 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00005632
5633out_fail:
5634 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05005635 dropped = drop_outstanding_extent(inode, num_bytes);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005636 /*
5637 * If the inodes csum_bytes is the same as the original
5638 * csum_bytes then we know we haven't raced with any free()ers
5639 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00005640 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04005641 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00005642 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04005643 } else {
5644 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
5645 u64 bytes;
5646
5647 /*
5648 * This is tricky, but first we need to figure out how much we
5649 * free'd from any free-ers that occured during this
5650 * reservation, so we reset ->csum_bytes to the csum_bytes
5651 * before we dropped our lock, and then call the free for the
5652 * number of bytes that were freed while we were trying our
5653 * reservation.
5654 */
5655 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
5656 BTRFS_I(inode)->csum_bytes = csum_bytes;
5657 to_free = calc_csum_metadata_size(inode, bytes, 0);
5658
5659
5660 /*
5661 * Now we need to see how much we would have freed had we not
5662 * been making this reservation and our ->csum_bytes were not
5663 * artificially inflated.
5664 */
5665 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
5666 bytes = csum_bytes - orig_csum_bytes;
5667 bytes = calc_csum_metadata_size(inode, bytes, 0);
5668
5669 /*
5670 * Now reset ->csum_bytes to what it should be. If bytes is
5671 * more than to_free then we would have free'd more space had we
5672 * not had an artificially high ->csum_bytes, so we need to free
5673 * the remainder. If bytes is the same or less then we don't
5674 * need to do anything, the other free-ers did the correct
5675 * thing.
5676 */
5677 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
5678 if (bytes > to_free)
5679 to_free = bytes - to_free;
5680 else
5681 to_free = 0;
5682 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00005683 spin_unlock(&BTRFS_I(inode)->lock);
Dongsheng Yange2d1f922015-02-06 10:26:52 -05005684 if (dropped)
Wang Shilong88e081bf2013-03-01 11:36:01 +00005685 to_free += btrfs_calc_trans_metadata_size(root, dropped);
5686
5687 if (to_free) {
5688 btrfs_block_rsv_release(root, block_rsv, to_free);
5689 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5690 btrfs_ino(inode), to_free, 0);
5691 }
5692 if (delalloc_lock)
5693 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
5694 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005695}
5696
Josef Bacik7709cde2011-08-04 10:25:02 -04005697/**
5698 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5699 * @inode: the inode to release the reservation for
5700 * @num_bytes: the number of bytes we're releasing
5701 *
5702 * This will release the metadata reservation for an inode. This can be called
5703 * once we complete IO for a given set of bytes to release their metadata
5704 * reservations.
5705 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005706void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
5707{
5708 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005709 u64 to_free = 0;
5710 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005711
5712 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04005713 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05005714 dropped = drop_outstanding_extent(inode, num_bytes);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005715
Miao Xie09348562013-02-07 10:12:07 +00005716 if (num_bytes)
5717 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04005718 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005719 if (dropped > 0)
5720 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005721
Josef Bacik6a3891c2015-03-16 17:38:52 -04005722 if (btrfs_test_is_dummy_root(root))
5723 return;
5724
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005725 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5726 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02005727
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005728 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
5729 to_free);
5730}
5731
Josef Bacik7709cde2011-08-04 10:25:02 -04005732/**
5733 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5734 * @inode: inode we're writing to
5735 * @num_bytes: the number of bytes we want to allocate
5736 *
5737 * This will do the following things
5738 *
5739 * o reserve space in the data space info for num_bytes
5740 * o reserve space in the metadata space info based on number of outstanding
5741 * extents and how much csums will be needed
5742 * o add to the inodes ->delalloc_bytes
5743 * o add it to the fs_info's delalloc inodes list.
5744 *
5745 * This will return 0 for success and -ENOSPC if there is no space left.
5746 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005747int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5748{
5749 int ret;
5750
Dongsheng Yange2d1f922015-02-06 10:26:52 -05005751 ret = btrfs_check_data_free_space(inode, num_bytes, num_bytes);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005752 if (ret)
5753 return ret;
5754
5755 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5756 if (ret) {
5757 btrfs_free_reserved_data_space(inode, num_bytes);
5758 return ret;
5759 }
5760
5761 return 0;
5762}
5763
Josef Bacik7709cde2011-08-04 10:25:02 -04005764/**
5765 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5766 * @inode: inode we're releasing space for
5767 * @num_bytes: the number of bytes we want to free up
5768 *
5769 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5770 * called in the case that we don't need the metadata AND data reservations
5771 * anymore. So if there is an error or we insert an inline extent.
5772 *
5773 * This function will release the metadata space that was not used and will
5774 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5775 * list if there are no delalloc bytes left.
5776 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005777void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5778{
5779 btrfs_delalloc_release_metadata(inode, num_bytes);
5780 btrfs_free_reserved_data_space(inode, num_bytes);
5781}
5782
Josef Bacikce93ec52014-11-17 15:45:48 -05005783static int update_block_group(struct btrfs_trans_handle *trans,
5784 struct btrfs_root *root, u64 bytenr,
5785 u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04005786{
Josef Bacik0af3d002010-06-21 14:48:16 -04005787 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04005788 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04005789 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005790 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04005791 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04005792 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04005793
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005794 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00005795 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02005796 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005797 if (alloc)
5798 old_val += num_bytes;
5799 else
5800 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02005801 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00005802 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005803
Chris Masond3977122009-01-05 21:25:51 -05005804 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04005805 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005806 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005807 return -ENOENT;
Yan, Zhengb742bb822010-05-16 10:46:24 -04005808 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5809 BTRFS_BLOCK_GROUP_RAID1 |
5810 BTRFS_BLOCK_GROUP_RAID10))
5811 factor = 2;
5812 else
5813 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04005814 /*
5815 * If this block group has free space cache written out, we
5816 * need to make sure to load it if we are removing space. This
5817 * is because we need the unpinning stage to actually add the
5818 * space back to the block group, otherwise we will leak space.
5819 */
5820 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00005821 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005822
Chris Masondb945352007-10-15 16:15:53 -04005823 byte_in_group = bytenr - cache->key.objectid;
5824 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005825
Josef Bacik25179202008-10-29 14:49:05 -04005826 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005827 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005828
Josef Bacik73bc1872011-10-03 14:07:49 -04005829 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005830 cache->disk_cache_state < BTRFS_DC_CLEAR)
5831 cache->disk_cache_state = BTRFS_DC_CLEAR;
5832
Chris Mason9078a3e2007-04-26 16:46:15 -04005833 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005834 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005835 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005836 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005837 btrfs_set_block_group_used(&cache->item, old_val);
5838 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005839 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb822010-05-16 10:46:24 -04005840 cache->space_info->bytes_used += num_bytes;
5841 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005842 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005843 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005844 } else {
Chris Masondb945352007-10-15 16:15:53 -04005845 old_val -= num_bytes;
Filipe Mananaae0ab002014-11-26 15:28:52 +00005846 btrfs_set_block_group_used(&cache->item, old_val);
5847 cache->pinned += num_bytes;
5848 cache->space_info->bytes_pinned += num_bytes;
5849 cache->space_info->bytes_used -= num_bytes;
5850 cache->space_info->disk_used -= num_bytes * factor;
5851 spin_unlock(&cache->lock);
5852 spin_unlock(&cache->space_info->lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04005853
Filipe Mananaae0ab002014-11-26 15:28:52 +00005854 set_extent_dirty(info->pinned_extents,
5855 bytenr, bytenr + num_bytes - 1,
5856 GFP_NOFS | __GFP_NOFAIL);
Josef Bacik47ab2a62014-09-18 11:20:02 -04005857 /*
5858 * No longer have used bytes in this block group, queue
5859 * it for deletion.
5860 */
5861 if (old_val == 0) {
5862 spin_lock(&info->unused_bgs_lock);
5863 if (list_empty(&cache->bg_list)) {
5864 btrfs_get_block_group(cache);
5865 list_add_tail(&cache->bg_list,
5866 &info->unused_bgs);
5867 }
5868 spin_unlock(&info->unused_bgs_lock);
5869 }
Chris Masoncd1bc462007-04-27 10:08:34 -04005870 }
Chris Mason1bbc6212015-04-06 12:46:08 -07005871
5872 spin_lock(&trans->transaction->dirty_bgs_lock);
5873 if (list_empty(&cache->dirty_list)) {
5874 list_add_tail(&cache->dirty_list,
5875 &trans->transaction->dirty_bgs);
5876 trans->transaction->num_dirty_bgs++;
5877 btrfs_get_block_group(cache);
5878 }
5879 spin_unlock(&trans->transaction->dirty_bgs_lock);
5880
Chris Masonfa9c0d792009-04-03 09:47:43 -04005881 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005882 total -= num_bytes;
5883 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005884 }
5885 return 0;
5886}
Chris Mason6324fbf2008-03-24 15:01:59 -04005887
Chris Masona061fc82008-05-07 11:43:44 -04005888static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5889{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005890 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005891 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005892
Liu Boa1897fd2012-12-27 09:01:23 +00005893 spin_lock(&root->fs_info->block_group_cache_lock);
5894 bytenr = root->fs_info->first_logical_byte;
5895 spin_unlock(&root->fs_info->block_group_cache_lock);
5896
5897 if (bytenr < (u64)-1)
5898 return bytenr;
5899
Josef Bacik0f9dd462008-09-23 13:14:11 -04005900 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5901 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005902 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005903
Yan Zhengd2fb3432008-12-11 16:30:39 -05005904 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005905 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005906
5907 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005908}
5909
Yan, Zhengf0486c62010-05-16 10:46:25 -04005910static int pin_down_extent(struct btrfs_root *root,
5911 struct btrfs_block_group_cache *cache,
5912 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005913{
Yan Zheng11833d62009-09-11 16:11:19 -04005914 spin_lock(&cache->space_info->lock);
5915 spin_lock(&cache->lock);
5916 cache->pinned += num_bytes;
5917 cache->space_info->bytes_pinned += num_bytes;
5918 if (reserved) {
5919 cache->reserved -= num_bytes;
5920 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005921 }
Yan Zheng11833d62009-09-11 16:11:19 -04005922 spin_unlock(&cache->lock);
5923 spin_unlock(&cache->space_info->lock);
5924
Yan, Zhengf0486c62010-05-16 10:46:25 -04005925 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5926 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Dongsheng Yange2d1f922015-02-06 10:26:52 -05005927 if (reserved)
Josef Bacik0be5dc62013-10-07 15:18:52 -04005928 trace_btrfs_reserved_extent_free(root, bytenr, num_bytes);
Yan324ae4d2007-11-16 14:57:08 -05005929 return 0;
5930}
Chris Mason9078a3e2007-04-26 16:46:15 -04005931
Yan, Zhengf0486c62010-05-16 10:46:25 -04005932/*
5933 * this function must be called within transaction
5934 */
5935int btrfs_pin_extent(struct btrfs_root *root,
5936 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005937{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005938 struct btrfs_block_group_cache *cache;
5939
5940 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005941 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005942
5943 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5944
5945 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005946 return 0;
5947}
Zheng Yane8569812008-09-26 10:05:48 -04005948
Yan, Zhengf0486c62010-05-16 10:46:25 -04005949/*
Chris Masone688b7252011-10-31 20:52:39 -04005950 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005951 */
Liu Bodcfac412012-12-27 09:01:20 +00005952int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b7252011-10-31 20:52:39 -04005953 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005954{
Chris Masone688b7252011-10-31 20:52:39 -04005955 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04005956 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04005957
5958 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005959 if (!cache)
5960 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04005961
5962 /*
5963 * pull in the free space cache (if any) so that our pin
5964 * removes the free space from the cache. We have load_only set
5965 * to one because the slow code to read in the free extents does check
5966 * the pinned extents.
5967 */
Liu Bof6373bf2012-12-27 09:01:18 +00005968 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04005969
5970 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5971
5972 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04005973 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04005974 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005975 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04005976}
5977
Josef Bacik8c2a1a32013-06-06 13:19:32 -04005978static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
5979{
5980 int ret;
5981 struct btrfs_block_group_cache *block_group;
5982 struct btrfs_caching_control *caching_ctl;
5983
5984 block_group = btrfs_lookup_block_group(root->fs_info, start);
5985 if (!block_group)
5986 return -EINVAL;
5987
5988 cache_block_group(block_group, 0);
5989 caching_ctl = get_caching_control(block_group);
5990
5991 if (!caching_ctl) {
5992 /* Logic error */
5993 BUG_ON(!block_group_cache_done(block_group));
5994 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5995 } else {
5996 mutex_lock(&caching_ctl->mutex);
5997
5998 if (start >= caching_ctl->progress) {
5999 ret = add_excluded_extent(root, start, num_bytes);
6000 } else if (start + num_bytes <= caching_ctl->progress) {
6001 ret = btrfs_remove_free_space(block_group,
6002 start, num_bytes);
6003 } else {
6004 num_bytes = caching_ctl->progress - start;
6005 ret = btrfs_remove_free_space(block_group,
6006 start, num_bytes);
6007 if (ret)
6008 goto out_lock;
6009
6010 num_bytes = (start + num_bytes) -
6011 caching_ctl->progress;
6012 start = caching_ctl->progress;
6013 ret = add_excluded_extent(root, start, num_bytes);
6014 }
6015out_lock:
6016 mutex_unlock(&caching_ctl->mutex);
6017 put_caching_control(caching_ctl);
6018 }
6019 btrfs_put_block_group(block_group);
6020 return ret;
6021}
6022
6023int btrfs_exclude_logged_extents(struct btrfs_root *log,
6024 struct extent_buffer *eb)
6025{
6026 struct btrfs_file_extent_item *item;
6027 struct btrfs_key key;
6028 int found_type;
6029 int i;
6030
6031 if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
6032 return 0;
6033
6034 for (i = 0; i < btrfs_header_nritems(eb); i++) {
6035 btrfs_item_key_to_cpu(eb, &key, i);
6036 if (key.type != BTRFS_EXTENT_DATA_KEY)
6037 continue;
6038 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
6039 found_type = btrfs_file_extent_type(eb, item);
6040 if (found_type == BTRFS_FILE_EXTENT_INLINE)
6041 continue;
6042 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
6043 continue;
6044 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
6045 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
6046 __exclude_logged_extent(log, key.objectid, key.offset);
6047 }
6048
6049 return 0;
6050}
6051
Josef Bacikfb25e912011-07-26 17:00:46 -04006052/**
6053 * btrfs_update_reserved_bytes - update the block_group and space info counters
6054 * @cache: The cache we are manipulating
6055 * @num_bytes: The number of bytes in question
6056 * @reserve: One of the reservation enums
Miao Xiee570fd22014-06-19 10:42:50 +08006057 * @delalloc: The blocks are allocated for the delalloc write
Josef Bacikfb25e912011-07-26 17:00:46 -04006058 *
6059 * This is called by the allocator when it reserves space, or by somebody who is
6060 * freeing space that was never actually used on disk. For example if you
6061 * reserve some space for a new leaf in transaction A and before transaction A
6062 * commits you free that leaf, you call this with reserve set to 0 in order to
6063 * clear the reservation.
6064 *
6065 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
6066 * ENOSPC accounting. For data we handle the reservation through clearing the
6067 * delalloc bits in the io_tree. We have to do this since we could end up
6068 * allocating less disk space for the amount of data we have reserved in the
6069 * case of compression.
6070 *
6071 * If this is a reservation and the block group has become read only we cannot
6072 * make the reservation and return -EAGAIN, otherwise this function always
6073 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04006074 */
Josef Bacikfb25e912011-07-26 17:00:46 -04006075static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08006076 u64 num_bytes, int reserve, int delalloc)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006077{
Josef Bacikfb25e912011-07-26 17:00:46 -04006078 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006079 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006080
Josef Bacikfb25e912011-07-26 17:00:46 -04006081 spin_lock(&space_info->lock);
6082 spin_lock(&cache->lock);
6083 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04006084 if (cache->ro) {
6085 ret = -EAGAIN;
6086 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04006087 cache->reserved += num_bytes;
6088 space_info->bytes_reserved += num_bytes;
6089 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006090 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04006091 "space_info", space_info->flags,
6092 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04006093 space_info->bytes_may_use -= num_bytes;
6094 }
Miao Xiee570fd22014-06-19 10:42:50 +08006095
6096 if (delalloc)
6097 cache->delalloc_bytes += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006098 }
Josef Bacikfb25e912011-07-26 17:00:46 -04006099 } else {
6100 if (cache->ro)
6101 space_info->bytes_readonly += num_bytes;
6102 cache->reserved -= num_bytes;
6103 space_info->bytes_reserved -= num_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08006104
6105 if (delalloc)
6106 cache->delalloc_bytes -= num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006107 }
Josef Bacikfb25e912011-07-26 17:00:46 -04006108 spin_unlock(&cache->lock);
6109 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006110 return ret;
6111}
6112
Jeff Mahoney143bede2012-03-01 14:56:26 +01006113void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04006114 struct btrfs_root *root)
6115{
6116 struct btrfs_fs_info *fs_info = root->fs_info;
6117 struct btrfs_caching_control *next;
6118 struct btrfs_caching_control *caching_ctl;
6119 struct btrfs_block_group_cache *cache;
6120
Josef Bacik9e351cc2014-03-13 15:42:13 -04006121 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04006122
6123 list_for_each_entry_safe(caching_ctl, next,
6124 &fs_info->caching_block_groups, list) {
6125 cache = caching_ctl->block_group;
6126 if (block_group_cache_done(cache)) {
6127 cache->last_byte_to_unpin = (u64)-1;
6128 list_del_init(&caching_ctl->list);
6129 put_caching_control(caching_ctl);
6130 } else {
6131 cache->last_byte_to_unpin = caching_ctl->progress;
6132 }
6133 }
6134
6135 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6136 fs_info->pinned_extents = &fs_info->freed_extents[1];
6137 else
6138 fs_info->pinned_extents = &fs_info->freed_extents[0];
6139
Josef Bacik9e351cc2014-03-13 15:42:13 -04006140 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006141
6142 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04006143}
6144
Josef Bacikc759c4e2015-10-02 15:25:10 -04006145/*
6146 * Returns the free cluster for the given space info and sets empty_cluster to
6147 * what it should be based on the mount options.
6148 */
6149static struct btrfs_free_cluster *
6150fetch_cluster_info(struct btrfs_root *root, struct btrfs_space_info *space_info,
6151 u64 *empty_cluster)
6152{
6153 struct btrfs_free_cluster *ret = NULL;
6154 bool ssd = btrfs_test_opt(root, SSD);
6155
6156 *empty_cluster = 0;
6157 if (btrfs_mixed_space_info(space_info))
6158 return ret;
6159
6160 if (ssd)
6161 *empty_cluster = 2 * 1024 * 1024;
6162 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
6163 ret = &root->fs_info->meta_alloc_cluster;
6164 if (!ssd)
6165 *empty_cluster = 64 * 1024;
6166 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && ssd) {
6167 ret = &root->fs_info->data_alloc_cluster;
6168 }
6169
6170 return ret;
6171}
6172
Filipe Manana678886b2014-12-07 21:31:47 +00006173static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end,
6174 const bool return_free_space)
Yan Zheng11833d62009-09-11 16:11:19 -04006175{
6176 struct btrfs_fs_info *fs_info = root->fs_info;
6177 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04006178 struct btrfs_space_info *space_info;
6179 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006180 struct btrfs_free_cluster *cluster = NULL;
Yan Zheng11833d62009-09-11 16:11:19 -04006181 u64 len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006182 u64 total_unpinned = 0;
6183 u64 empty_cluster = 0;
Josef Bacik7b398f82012-10-22 15:52:28 -04006184 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04006185
6186 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04006187 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04006188 if (!cache ||
6189 start >= cache->key.objectid + cache->key.offset) {
6190 if (cache)
6191 btrfs_put_block_group(cache);
Josef Bacikc759c4e2015-10-02 15:25:10 -04006192 total_unpinned = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006193 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006194 BUG_ON(!cache); /* Logic error */
Josef Bacikc759c4e2015-10-02 15:25:10 -04006195
6196 cluster = fetch_cluster_info(root,
6197 cache->space_info,
6198 &empty_cluster);
6199 empty_cluster <<= 1;
Yan Zheng11833d62009-09-11 16:11:19 -04006200 }
6201
6202 len = cache->key.objectid + cache->key.offset - start;
6203 len = min(len, end + 1 - start);
6204
6205 if (start < cache->last_byte_to_unpin) {
6206 len = min(len, cache->last_byte_to_unpin - start);
Filipe Manana678886b2014-12-07 21:31:47 +00006207 if (return_free_space)
6208 btrfs_add_free_space(cache, start, len);
Yan Zheng11833d62009-09-11 16:11:19 -04006209 }
Josef Bacik25179202008-10-29 14:49:05 -04006210
Yan, Zhengf0486c62010-05-16 10:46:25 -04006211 start += len;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006212 total_unpinned += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006213 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006214
Josef Bacikc759c4e2015-10-02 15:25:10 -04006215 /*
6216 * If this space cluster has been marked as fragmented and we've
6217 * unpinned enough in this block group to potentially allow a
6218 * cluster to be created inside of it go ahead and clear the
6219 * fragmented check.
6220 */
6221 if (cluster && cluster->fragmented &&
6222 total_unpinned > empty_cluster) {
6223 spin_lock(&cluster->lock);
6224 cluster->fragmented = 0;
6225 spin_unlock(&cluster->lock);
6226 }
6227
Josef Bacik7b398f82012-10-22 15:52:28 -04006228 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006229 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006230 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04006231 space_info->bytes_pinned -= len;
Josef Bacik4f4db212015-09-29 11:40:47 -04006232 space_info->max_extent_size = 0;
Liu Bod288db52014-07-02 16:58:01 +08006233 percpu_counter_add(&space_info->total_bytes_pinned, -len);
Josef Bacik7b398f82012-10-22 15:52:28 -04006234 if (cache->ro) {
6235 space_info->bytes_readonly += len;
6236 readonly = true;
6237 }
Josef Bacik25179202008-10-29 14:49:05 -04006238 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04006239 if (!readonly && global_rsv->space_info == space_info) {
6240 spin_lock(&global_rsv->lock);
6241 if (!global_rsv->full) {
6242 len = min(len, global_rsv->size -
6243 global_rsv->reserved);
6244 global_rsv->reserved += len;
6245 space_info->bytes_may_use += len;
6246 if (global_rsv->reserved >= global_rsv->size)
6247 global_rsv->full = 1;
6248 }
6249 spin_unlock(&global_rsv->lock);
6250 }
6251 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04006252 }
6253
6254 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006255 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04006256 return 0;
6257}
6258
6259int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04006260 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05006261{
Yan Zheng11833d62009-09-11 16:11:19 -04006262 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006263 struct btrfs_block_group_cache *block_group, *tmp;
6264 struct list_head *deleted_bgs;
Yan Zheng11833d62009-09-11 16:11:19 -04006265 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04006266 u64 start;
6267 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05006268 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05006269
Yan Zheng11833d62009-09-11 16:11:19 -04006270 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
6271 unpin = &fs_info->freed_extents[1];
6272 else
6273 unpin = &fs_info->freed_extents[0];
6274
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006275 while (!trans->aborted) {
Filipe Mananad4b450c2015-01-29 19:18:25 +00006276 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04006277 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04006278 EXTENT_DIRTY, NULL);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006279 if (ret) {
6280 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05006281 break;
Filipe Mananad4b450c2015-01-29 19:18:25 +00006282 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006283
Li Dongyang5378e602011-03-24 10:24:27 +00006284 if (btrfs_test_opt(root, DISCARD))
6285 ret = btrfs_discard_extent(root, start,
6286 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006287
Chris Mason1a5bc162007-10-15 16:15:26 -04006288 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Filipe Manana678886b2014-12-07 21:31:47 +00006289 unpin_extent_range(root, start, end, true);
Filipe Mananad4b450c2015-01-29 19:18:25 +00006290 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Chris Masonb9473432009-03-13 11:00:37 -04006291 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05006292 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006293
Jeff Mahoneye33e17e2015-06-15 09:41:19 -04006294 /*
6295 * Transaction is finished. We don't need the lock anymore. We
6296 * do need to clean up the block groups in case of a transaction
6297 * abort.
6298 */
6299 deleted_bgs = &trans->transaction->deleted_bgs;
6300 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
6301 u64 trimmed = 0;
6302
6303 ret = -EROFS;
6304 if (!trans->aborted)
6305 ret = btrfs_discard_extent(root,
6306 block_group->key.objectid,
6307 block_group->key.offset,
6308 &trimmed);
6309
6310 list_del_init(&block_group->bg_list);
6311 btrfs_put_block_group_trimming(block_group);
6312 btrfs_put_block_group(block_group);
6313
6314 if (ret) {
6315 const char *errstr = btrfs_decode_error(ret);
6316 btrfs_warn(fs_info,
6317 "Discard failed while removing blockgroup: errno=%d %s\n",
6318 ret, errstr);
6319 }
6320 }
6321
Chris Masone20d96d2007-03-22 12:13:20 -04006322 return 0;
6323}
6324
Josef Bacikb150a4f2013-06-19 15:00:04 -04006325static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
6326 u64 owner, u64 root_objectid)
6327{
6328 struct btrfs_space_info *space_info;
6329 u64 flags;
6330
6331 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
6332 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
6333 flags = BTRFS_BLOCK_GROUP_SYSTEM;
6334 else
6335 flags = BTRFS_BLOCK_GROUP_METADATA;
6336 } else {
6337 flags = BTRFS_BLOCK_GROUP_DATA;
6338 }
6339
6340 space_info = __find_space_info(fs_info, flags);
6341 BUG_ON(!space_info); /* Logic bug */
6342 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
6343}
6344
6345
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006346static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
6347 struct btrfs_root *root,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006348 struct btrfs_delayed_ref_node *node, u64 parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006349 u64 root_objectid, u64 owner_objectid,
6350 u64 owner_offset, int refs_to_drop,
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006351 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05006352{
Chris Masone2fa7222007-03-12 16:22:34 -04006353 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006354 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04006355 struct btrfs_fs_info *info = root->fs_info;
6356 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04006357 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006358 struct btrfs_extent_item *ei;
6359 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05006360 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006361 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05006362 int extent_slot = 0;
6363 int found_extent = 0;
6364 int num_to_del = 1;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006365 int no_quota = node->no_quota;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006366 u32 item_size;
6367 u64 refs;
Qu Wenruoc682f9b2015-03-17 16:59:47 +08006368 u64 bytenr = node->bytenr;
6369 u64 num_bytes = node->num_bytes;
Josef Bacikfcebe452014-05-13 17:30:47 -07006370 int last_ref = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05006371 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6372 SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05006373
Josef Bacikfcebe452014-05-13 17:30:47 -07006374 if (!info->quota_enabled || !is_fstree(root_objectid))
6375 no_quota = 1;
6376
Chris Mason5caf2a02007-04-02 11:20:42 -04006377 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04006378 if (!path)
6379 return -ENOMEM;
6380
Chris Mason3c12ac72008-04-21 12:01:38 -04006381 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04006382 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006383
6384 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
6385 BUG_ON(!is_data && refs_to_drop != 1);
6386
Josef Bacik3173a182013-03-07 14:22:04 -05006387 if (is_data)
6388 skinny_metadata = 0;
6389
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006390 ret = lookup_extent_backref(trans, extent_root, path, &iref,
6391 bytenr, num_bytes, parent,
6392 root_objectid, owner_objectid,
6393 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05006394 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05006395 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006396 while (extent_slot >= 0) {
6397 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05006398 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006399 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05006400 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006401 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
6402 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05006403 found_extent = 1;
6404 break;
6405 }
Josef Bacik3173a182013-03-07 14:22:04 -05006406 if (key.type == BTRFS_METADATA_ITEM_KEY &&
6407 key.offset == owner_objectid) {
6408 found_extent = 1;
6409 break;
6410 }
Chris Mason952fcca2008-02-18 16:33:44 -05006411 if (path->slots[0] - extent_slot > 5)
6412 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006413 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05006414 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006415#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6416 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
6417 if (found_extent && item_size < sizeof(*ei))
6418 found_extent = 0;
6419#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04006420 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006421 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04006422 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006423 NULL, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07006424 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06006425 if (ret) {
6426 btrfs_abort_transaction(trans, extent_root, ret);
6427 goto out;
6428 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006429 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04006430 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006431
6432 key.objectid = bytenr;
6433 key.type = BTRFS_EXTENT_ITEM_KEY;
6434 key.offset = num_bytes;
6435
Josef Bacik3173a182013-03-07 14:22:04 -05006436 if (!is_data && skinny_metadata) {
6437 key.type = BTRFS_METADATA_ITEM_KEY;
6438 key.offset = owner_objectid;
6439 }
6440
Zheng Yan31840ae2008-09-23 13:14:14 -04006441 ret = btrfs_search_slot(trans, extent_root,
6442 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05006443 if (ret > 0 && skinny_metadata && path->slots[0]) {
6444 /*
6445 * Couldn't find our skinny metadata item,
6446 * see if we have ye olde extent item.
6447 */
6448 path->slots[0]--;
6449 btrfs_item_key_to_cpu(path->nodes[0], &key,
6450 path->slots[0]);
6451 if (key.objectid == bytenr &&
6452 key.type == BTRFS_EXTENT_ITEM_KEY &&
6453 key.offset == num_bytes)
6454 ret = 0;
6455 }
6456
6457 if (ret > 0 && skinny_metadata) {
6458 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01006459 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05006460 key.type = BTRFS_EXTENT_ITEM_KEY;
6461 key.offset = num_bytes;
6462 btrfs_release_path(path);
6463 ret = btrfs_search_slot(trans, extent_root,
6464 &key, path, -1, 1);
6465 }
6466
Josef Bacikf3465ca2008-11-12 14:19:50 -05006467 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006468 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006469 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00006470 if (ret > 0)
6471 btrfs_print_leaf(extent_root,
6472 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05006473 }
David Sterba005d6422012-09-18 07:52:32 -06006474 if (ret < 0) {
6475 btrfs_abort_transaction(trans, extent_root, ret);
6476 goto out;
6477 }
Zheng Yan31840ae2008-09-23 13:14:14 -04006478 extent_slot = path->slots[0];
6479 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306480 } else if (WARN_ON(ret == -ENOENT)) {
Chris Mason7bb86312007-12-11 09:25:06 -05006481 btrfs_print_leaf(extent_root, path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006482 btrfs_err(info,
6483 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006484 bytenr, parent, root_objectid, owner_objectid,
6485 owner_offset);
Josef Bacikc4a050b2014-03-14 16:36:53 -04006486 btrfs_abort_transaction(trans, extent_root, ret);
6487 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006488 } else {
David Sterba005d6422012-09-18 07:52:32 -06006489 btrfs_abort_transaction(trans, extent_root, ret);
6490 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05006491 }
Chris Mason5f39d392007-10-15 16:14:19 -04006492
6493 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006494 item_size = btrfs_item_size_nr(leaf, extent_slot);
6495#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6496 if (item_size < sizeof(*ei)) {
6497 BUG_ON(found_extent || extent_slot != path->slots[0]);
6498 ret = convert_extent_item_v0(trans, extent_root, path,
6499 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06006500 if (ret < 0) {
6501 btrfs_abort_transaction(trans, extent_root, ret);
6502 goto out;
6503 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006504
David Sterbab3b4aa72011-04-21 01:20:15 +02006505 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006506 path->leave_spinning = 1;
6507
6508 key.objectid = bytenr;
6509 key.type = BTRFS_EXTENT_ITEM_KEY;
6510 key.offset = num_bytes;
6511
6512 ret = btrfs_search_slot(trans, extent_root, &key, path,
6513 -1, 1);
6514 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006515 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006516 ret, bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006517 btrfs_print_leaf(extent_root, path->nodes[0]);
6518 }
David Sterba005d6422012-09-18 07:52:32 -06006519 if (ret < 0) {
6520 btrfs_abort_transaction(trans, extent_root, ret);
6521 goto out;
6522 }
6523
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006524 extent_slot = path->slots[0];
6525 leaf = path->nodes[0];
6526 item_size = btrfs_item_size_nr(leaf, extent_slot);
6527 }
6528#endif
6529 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05006530 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04006531 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05006532 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
6533 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006534 struct btrfs_tree_block_info *bi;
6535 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
6536 bi = (struct btrfs_tree_block_info *)(ei + 1);
6537 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05006538 }
6539
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006540 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04006541 if (refs < refs_to_drop) {
6542 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
David Sterba351fd352014-05-15 16:48:20 +02006543 "for bytenr %Lu", refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04006544 ret = -EINVAL;
6545 btrfs_abort_transaction(trans, extent_root, ret);
6546 goto out;
6547 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006548 refs -= refs_to_drop;
6549
6550 if (refs > 0) {
6551 if (extent_op)
6552 __run_delayed_extent_op(extent_op, leaf, ei);
6553 /*
6554 * In the case of inline back ref, reference count will
6555 * be updated by remove_extent_backref
6556 */
6557 if (iref) {
6558 BUG_ON(!found_extent);
6559 } else {
6560 btrfs_set_extent_refs(leaf, ei, refs);
6561 btrfs_mark_buffer_dirty(leaf);
6562 }
6563 if (found_extent) {
6564 ret = remove_extent_backref(trans, extent_root, path,
6565 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07006566 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06006567 if (ret) {
6568 btrfs_abort_transaction(trans, extent_root, ret);
6569 goto out;
6570 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006571 }
Josef Bacikb150a4f2013-06-19 15:00:04 -04006572 add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
6573 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006574 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006575 if (found_extent) {
6576 BUG_ON(is_data && refs_to_drop !=
Zhaolei9ed0dea2015-08-06 22:16:24 +08006577 extent_data_ref_count(path, iref));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006578 if (iref) {
6579 BUG_ON(path->slots[0] != extent_slot);
6580 } else {
6581 BUG_ON(path->slots[0] != extent_slot + 1);
6582 path->slots[0] = extent_slot;
6583 num_to_del = 2;
6584 }
Chris Mason78fae272007-03-25 11:35:08 -04006585 }
Chris Masonb9473432009-03-13 11:00:37 -04006586
Josef Bacikfcebe452014-05-13 17:30:47 -07006587 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05006588 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
6589 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06006590 if (ret) {
6591 btrfs_abort_transaction(trans, extent_root, ret);
6592 goto out;
6593 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006594 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01006595
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006596 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05006597 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06006598 if (ret) {
6599 btrfs_abort_transaction(trans, extent_root, ret);
6600 goto out;
6601 }
Chris Mason459931e2008-12-10 09:10:46 -05006602 }
6603
Josef Bacikce93ec52014-11-17 15:45:48 -05006604 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06006605 if (ret) {
6606 btrfs_abort_transaction(trans, extent_root, ret);
6607 goto out;
6608 }
Chris Masona28ec192007-03-06 20:08:01 -05006609 }
Josef Bacikfcebe452014-05-13 17:30:47 -07006610 btrfs_release_path(path);
6611
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006612out:
Chris Mason5caf2a02007-04-02 11:20:42 -04006613 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05006614 return ret;
6615}
6616
6617/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006618 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04006619 * delayed ref for that extent as well. This searches the delayed ref tree for
6620 * a given extent, and if there are no other delayed refs to be processed, it
6621 * removes it from the tree.
6622 */
6623static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
6624 struct btrfs_root *root, u64 bytenr)
6625{
6626 struct btrfs_delayed_ref_head *head;
6627 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006628 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04006629
6630 delayed_refs = &trans->transaction->delayed_refs;
6631 spin_lock(&delayed_refs->lock);
6632 head = btrfs_find_delayed_ref_head(trans, bytenr);
6633 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08006634 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04006635
Josef Bacikd7df2c72014-01-23 09:21:38 -05006636 spin_lock(&head->lock);
Qu Wenruoc6fc2452015-03-30 17:03:00 +08006637 if (!list_empty(&head->ref_list))
Chris Mason1887be62009-03-13 10:11:24 -04006638 goto out;
6639
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006640 if (head->extent_op) {
6641 if (!head->must_insert_reserved)
6642 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00006643 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006644 head->extent_op = NULL;
6645 }
6646
Chris Mason1887be62009-03-13 10:11:24 -04006647 /*
6648 * waiting for the lock here would deadlock. If someone else has it
6649 * locked they are already in the process of dropping it anyway
6650 */
6651 if (!mutex_trylock(&head->mutex))
6652 goto out;
6653
6654 /*
6655 * at this point we have a head with no other entries. Go
6656 * ahead and process it.
6657 */
6658 head->node.in_tree = 0;
Liu Boc46effa2013-10-14 12:59:45 +08006659 rb_erase(&head->href_node, &delayed_refs->href_root);
Chris Masonc3e69d52009-03-13 10:17:05 -04006660
Josef Bacikd7df2c72014-01-23 09:21:38 -05006661 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04006662
6663 /*
6664 * we don't take a ref on the node because we're removing it from the
6665 * tree, so we just steal the ref the tree was holding.
6666 */
Chris Masonc3e69d52009-03-13 10:17:05 -04006667 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05006668 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04006669 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05006670 head->processing = 0;
6671 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04006672 spin_unlock(&delayed_refs->lock);
6673
Yan, Zhengf0486c62010-05-16 10:46:25 -04006674 BUG_ON(head->extent_op);
6675 if (head->must_insert_reserved)
6676 ret = 1;
6677
6678 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04006679 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006680 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04006681out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05006682 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08006683
6684out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04006685 spin_unlock(&delayed_refs->lock);
6686 return 0;
6687}
6688
Yan, Zhengf0486c62010-05-16 10:46:25 -04006689void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
6690 struct btrfs_root *root,
6691 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02006692 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006693{
Josef Bacikb150a4f2013-06-19 15:00:04 -04006694 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006695 int ret;
6696
6697 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006698 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6699 buf->start, buf->len,
6700 parent, root->root_key.objectid,
6701 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02006702 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006703 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006704 }
6705
6706 if (!last_ref)
6707 return;
6708
Yan, Zhengf0486c62010-05-16 10:46:25 -04006709 if (btrfs_header_generation(buf) == trans->transid) {
Filipe Manana62198722015-01-06 20:18:45 +00006710 struct btrfs_block_group_cache *cache;
6711
Yan, Zhengf0486c62010-05-16 10:46:25 -04006712 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
6713 ret = check_ref_cleanup(trans, root, buf->start);
6714 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04006715 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006716 }
6717
Filipe Manana62198722015-01-06 20:18:45 +00006718 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
6719
Yan, Zhengf0486c62010-05-16 10:46:25 -04006720 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
6721 pin_down_extent(root, cache, buf->start, buf->len, 1);
Filipe Manana62198722015-01-06 20:18:45 +00006722 btrfs_put_block_group(cache);
Josef Bacik37be25b2011-08-05 10:25:38 -04006723 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006724 }
6725
6726 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
6727
6728 btrfs_add_free_space(cache, buf->start, buf->len);
Miao Xiee570fd22014-06-19 10:42:50 +08006729 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0);
Filipe Manana62198722015-01-06 20:18:45 +00006730 btrfs_put_block_group(cache);
Josef Bacik0be5dc62013-10-07 15:18:52 -04006731 trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
Josef Bacikb150a4f2013-06-19 15:00:04 -04006732 pin = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006733 }
6734out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04006735 if (pin)
6736 add_pinned_bytes(root->fs_info, buf->len,
6737 btrfs_header_level(buf),
6738 root->root_key.objectid);
6739
Josef Bacika826d6d2011-03-16 13:42:43 -04006740 /*
6741 * Deleting the buffer, clear the corrupt flag since it doesn't matter
6742 * anymore.
6743 */
6744 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006745}
6746
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006747/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006748int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6749 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Josef Bacikfcebe452014-05-13 17:30:47 -07006750 u64 owner, u64 offset, int no_quota)
Chris Mason925baed2008-06-25 16:01:30 -04006751{
6752 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006753 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04006754
David Sterbafccb84c2014-09-29 23:53:21 +02006755 if (btrfs_test_is_dummy_root(root))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04006756 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02006757
Josef Bacikb150a4f2013-06-19 15:00:04 -04006758 add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
6759
Chris Mason56bec292009-03-13 10:10:06 -04006760 /*
6761 * tree log blocks never actually go into the extent allocation
6762 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04006763 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006764 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
6765 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04006766 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04006767 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04006768 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006769 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006770 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
6771 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006772 parent, root_objectid, (int)owner,
Josef Bacikfcebe452014-05-13 17:30:47 -07006773 BTRFS_DROP_DELAYED_REF, NULL, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006774 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006775 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
6776 num_bytes,
6777 parent, root_objectid, owner,
6778 offset, BTRFS_DROP_DELAYED_REF,
Josef Bacikfcebe452014-05-13 17:30:47 -07006779 NULL, no_quota);
Chris Mason56bec292009-03-13 10:10:06 -04006780 }
Chris Mason925baed2008-06-25 16:01:30 -04006781 return ret;
6782}
6783
Chris Masonfec577f2007-02-26 10:40:21 -05006784/*
Josef Bacik817d52f2009-07-13 21:29:25 -04006785 * when we wait for progress in the block group caching, its because
6786 * our allocation attempt failed at least once. So, we must sleep
6787 * and let some progress happen before we try again.
6788 *
6789 * This function will sleep at least once waiting for new free space to
6790 * show up, and then it will check the block group free space numbers
6791 * for our min num_bytes. Another option is to have it go ahead
6792 * and look in the rbtree for a free extent of a given size, but this
6793 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04006794 *
6795 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
6796 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04006797 */
Josef Bacik36cce922013-08-05 11:15:21 -04006798static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04006799wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
6800 u64 num_bytes)
6801{
Yan Zheng11833d62009-09-11 16:11:19 -04006802 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04006803
Yan Zheng11833d62009-09-11 16:11:19 -04006804 caching_ctl = get_caching_control(cache);
6805 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04006806 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04006807
Yan Zheng11833d62009-09-11 16:11:19 -04006808 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08006809 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04006810
6811 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04006812}
6813
6814static noinline int
6815wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
6816{
6817 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04006818 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006819
6820 caching_ctl = get_caching_control(cache);
6821 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04006822 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006823
6824 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04006825 if (cache->cached == BTRFS_CACHE_ERROR)
6826 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04006827 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04006828 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04006829}
6830
Liu Bo31e50222012-11-21 14:18:10 +00006831int __get_raid_index(u64 flags)
Yan, Zhengb742bb822010-05-16 10:46:24 -04006832{
Ilya Dryomov7738a532012-03-27 17:09:17 +03006833 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00006834 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006835 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00006836 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006837 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00006838 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006839 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00006840 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006841 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05006842 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05006843 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05006844 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006845
Chris Masone942f882013-02-20 14:06:05 -05006846 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb822010-05-16 10:46:24 -04006847}
6848
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006849int get_block_group_index(struct btrfs_block_group_cache *cache)
Ilya Dryomov7738a532012-03-27 17:09:17 +03006850{
Liu Bo31e50222012-11-21 14:18:10 +00006851 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03006852}
6853
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006854static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
6855 [BTRFS_RAID_RAID10] = "raid10",
6856 [BTRFS_RAID_RAID1] = "raid1",
6857 [BTRFS_RAID_DUP] = "dup",
6858 [BTRFS_RAID_RAID0] = "raid0",
6859 [BTRFS_RAID_SINGLE] = "single",
6860 [BTRFS_RAID_RAID5] = "raid5",
6861 [BTRFS_RAID_RAID6] = "raid6",
6862};
6863
Jeff Mahoney1b8e5df2013-11-20 16:50:23 -05006864static const char *get_raid_name(enum btrfs_raid_types type)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006865{
6866 if (type >= BTRFS_NR_RAID_TYPES)
6867 return NULL;
6868
6869 return btrfs_raid_type_names[type];
6870}
6871
Josef Bacik817d52f2009-07-13 21:29:25 -04006872enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05006873 LOOP_CACHING_NOWAIT = 0,
6874 LOOP_CACHING_WAIT = 1,
6875 LOOP_ALLOC_CHUNK = 2,
6876 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04006877};
6878
Miao Xiee570fd22014-06-19 10:42:50 +08006879static inline void
6880btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
6881 int delalloc)
6882{
6883 if (delalloc)
6884 down_read(&cache->data_rwsem);
6885}
6886
6887static inline void
6888btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
6889 int delalloc)
6890{
6891 btrfs_get_block_group(cache);
6892 if (delalloc)
6893 down_read(&cache->data_rwsem);
6894}
6895
6896static struct btrfs_block_group_cache *
6897btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
6898 struct btrfs_free_cluster *cluster,
6899 int delalloc)
6900{
6901 struct btrfs_block_group_cache *used_bg;
6902 bool locked = false;
6903again:
6904 spin_lock(&cluster->refill_lock);
6905 if (locked) {
6906 if (used_bg == cluster->block_group)
6907 return used_bg;
6908
6909 up_read(&used_bg->data_rwsem);
6910 btrfs_put_block_group(used_bg);
6911 }
6912
6913 used_bg = cluster->block_group;
6914 if (!used_bg)
6915 return NULL;
6916
6917 if (used_bg == block_group)
6918 return used_bg;
6919
6920 btrfs_get_block_group(used_bg);
6921
6922 if (!delalloc)
6923 return used_bg;
6924
6925 if (down_read_trylock(&used_bg->data_rwsem))
6926 return used_bg;
6927
6928 spin_unlock(&cluster->refill_lock);
6929 down_read(&used_bg->data_rwsem);
6930 locked = true;
6931 goto again;
6932}
6933
6934static inline void
6935btrfs_release_block_group(struct btrfs_block_group_cache *cache,
6936 int delalloc)
6937{
6938 if (delalloc)
6939 up_read(&cache->data_rwsem);
6940 btrfs_put_block_group(cache);
6941}
6942
Josef Bacik817d52f2009-07-13 21:29:25 -04006943/*
Chris Masonfec577f2007-02-26 10:40:21 -05006944 * walks the btree of allocated extents and find a hole of a given size.
6945 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08006946 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04006947 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08006948 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05006949 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08006950 *
6951 * If there is no suitable free space, we will record the max size of
6952 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05006953 */
Josef Bacik00361582013-08-14 14:02:47 -04006954static noinline int find_free_extent(struct btrfs_root *orig_root,
Chris Mason98ed5172008-01-03 10:01:48 -05006955 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05006956 u64 hint_byte, struct btrfs_key *ins,
Miao Xiee570fd22014-06-19 10:42:50 +08006957 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05006958{
Josef Bacik80eb2342008-10-29 14:49:05 -04006959 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05006960 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006961 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04006962 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05006963 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08006964 u64 max_extent_size = 0;
Josef Bacikc759c4e2015-10-02 15:25:10 -04006965 u64 empty_cluster = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04006966 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006967 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00006968 int index = __get_raid_index(flags);
6969 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
Josef Bacikfb25e912011-07-26 17:00:46 -04006970 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik0a243252009-09-11 16:11:20 -04006971 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006972 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04006973 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08006974 bool have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04006975 bool full_search = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006976
Chris Masondb945352007-10-15 16:15:53 -04006977 WARN_ON(num_bytes < root->sectorsize);
David Sterba962a2982014-06-04 18:41:45 +02006978 ins->type = BTRFS_EXTENT_ITEM_KEY;
Josef Bacik80eb2342008-10-29 14:49:05 -04006979 ins->objectid = 0;
6980 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04006981
David Sterbab6919a52013-04-29 13:39:40 +00006982 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05006983
David Sterbab6919a52013-04-29 13:39:40 +00006984 space_info = __find_space_info(root->fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006985 if (!space_info) {
David Sterbab6919a52013-04-29 13:39:40 +00006986 btrfs_err(root->fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006987 return -ENOSPC;
6988 }
Josef Bacik2552d172009-04-03 10:14:19 -04006989
Josef Bacik67377732010-09-16 16:19:09 -04006990 /*
Josef Bacik4f4db212015-09-29 11:40:47 -04006991 * If our free space is heavily fragmented we may not be able to make
6992 * big contiguous allocations, so instead of doing the expensive search
6993 * for free space, simply return ENOSPC with our max_extent_size so we
6994 * can go ahead and search for a more manageable chunk.
6995 *
6996 * If our max_extent_size is large enough for our allocation simply
6997 * disable clustering since we will likely not be able to find enough
6998 * space to create a cluster and induce latency trying.
6999 */
7000 if (unlikely(space_info->max_extent_size)) {
7001 spin_lock(&space_info->lock);
7002 if (space_info->max_extent_size &&
7003 num_bytes > space_info->max_extent_size) {
7004 ins->offset = space_info->max_extent_size;
7005 spin_unlock(&space_info->lock);
7006 return -ENOSPC;
7007 } else if (space_info->max_extent_size) {
7008 use_cluster = false;
7009 }
7010 spin_unlock(&space_info->lock);
7011 }
7012
Josef Bacikc759c4e2015-10-02 15:25:10 -04007013 last_ptr = fetch_cluster_info(orig_root, space_info, &empty_cluster);
Chris Mason239b14b2008-03-24 15:02:07 -04007014 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007015 spin_lock(&last_ptr->lock);
7016 if (last_ptr->block_group)
7017 hint_byte = last_ptr->window_start;
Josef Bacikc759c4e2015-10-02 15:25:10 -04007018 if (last_ptr->fragmented) {
7019 /*
7020 * We still set window_start so we can keep track of the
7021 * last place we found an allocation to try and save
7022 * some time.
7023 */
7024 hint_byte = last_ptr->window_start;
7025 use_cluster = false;
7026 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007027 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04007028 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007029
Chris Masona061fc82008-05-07 11:43:44 -04007030 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04007031 search_start = max(search_start, hint_byte);
Josef Bacik2552d172009-04-03 10:14:19 -04007032 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04007033 block_group = btrfs_lookup_block_group(root->fs_info,
7034 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04007035 /*
7036 * we don't want to use the block group if it doesn't match our
7037 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05007038 *
7039 * However if we are re-searching with an ideal block group
7040 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04007041 */
David Sterbab6919a52013-04-29 13:39:40 +00007042 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05007043 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04007044 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04007045 if (list_empty(&block_group->list) ||
7046 block_group->ro) {
7047 /*
7048 * someone is removing this block group,
7049 * we can't jump into the have_block_group
7050 * target because our list pointers are not
7051 * valid
7052 */
7053 btrfs_put_block_group(block_group);
7054 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05007055 } else {
Yan, Zhengb742bb822010-05-16 10:46:24 -04007056 index = get_block_group_index(block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007057 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04007058 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05007059 }
Josef Bacik2552d172009-04-03 10:14:19 -04007060 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04007061 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04007062 }
Chris Mason42e70e72008-11-07 18:17:11 -05007063 }
Josef Bacik2552d172009-04-03 10:14:19 -04007064search:
Miao Xie60d2adb2011-09-09 17:34:35 +08007065 have_caching_bg = false;
Josef Bacika5e681d2015-10-01 14:54:10 -04007066 if (index == 0 || index == __get_raid_index(flags))
7067 full_search = true;
Josef Bacik80eb2342008-10-29 14:49:05 -04007068 down_read(&space_info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007069 list_for_each_entry(block_group, &space_info->block_groups[index],
7070 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04007071 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04007072 int cached;
Chris Mason8a1413a22008-11-10 16:13:54 -05007073
Miao Xiee570fd22014-06-19 10:42:50 +08007074 btrfs_grab_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007075 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05007076
Chris Mason83a50de2010-12-13 15:06:46 -05007077 /*
7078 * this can happen if we end up cycling through all the
7079 * raid types, but we want to make sure we only allocate
7080 * for the proper type.
7081 */
David Sterbab6919a52013-04-29 13:39:40 +00007082 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05007083 u64 extra = BTRFS_BLOCK_GROUP_DUP |
7084 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007085 BTRFS_BLOCK_GROUP_RAID5 |
7086 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05007087 BTRFS_BLOCK_GROUP_RAID10;
7088
7089 /*
7090 * if they asked for extra copies and this block group
7091 * doesn't provide them, bail. This does allow us to
7092 * fill raid0 from raid1.
7093 */
David Sterbab6919a52013-04-29 13:39:40 +00007094 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05007095 goto loop;
7096 }
7097
Josef Bacik2552d172009-04-03 10:14:19 -04007098have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05007099 cached = block_group_cache_done(block_group);
7100 if (unlikely(!cached)) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007101 have_caching_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00007102 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04007103 BUG_ON(ret < 0);
7104 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05007105 }
7106
Josef Bacik36cce922013-08-05 11:15:21 -04007107 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
7108 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05007109 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04007110 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007111
Josef Bacik0a243252009-09-11 16:11:20 -04007112 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007113 * Ok we want to try and use the cluster allocator, so
7114 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04007115 */
Josef Bacikc759c4e2015-10-02 15:25:10 -04007116 if (last_ptr && use_cluster) {
Miao Xie215a63d2014-01-15 20:00:56 +08007117 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05007118 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007119 /*
7120 * the refill lock keeps out other
7121 * people trying to start a new cluster
7122 */
Miao Xiee570fd22014-06-19 10:42:50 +08007123 used_block_group = btrfs_lock_cluster(block_group,
7124 last_ptr,
7125 delalloc);
7126 if (!used_block_group)
Chris Mason44fb5512009-06-04 15:34:51 -04007127 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04007128
Miao Xiee570fd22014-06-19 10:42:50 +08007129 if (used_block_group != block_group &&
7130 (used_block_group->ro ||
7131 !block_group_bits(used_block_group, flags)))
7132 goto release_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007133
7134 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007135 last_ptr,
7136 num_bytes,
7137 used_block_group->key.objectid,
7138 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007139 if (offset) {
7140 /* we have a block, we're done */
7141 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05007142 trace_btrfs_reserve_extent_cluster(root,
Miao Xie89d43462014-01-15 20:00:57 +08007143 used_block_group,
7144 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08007145 if (used_block_group != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08007146 btrfs_release_block_group(block_group,
7147 delalloc);
Miao Xie215a63d2014-01-15 20:00:56 +08007148 block_group = used_block_group;
7149 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007150 goto checks;
7151 }
7152
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05007153 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08007154release_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007155 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
7156 * set up a new clusters, so lets just skip it
7157 * and let the allocator find whatever block
7158 * it can find. If we reach this point, we
7159 * will have tried the cluster allocator
7160 * plenty of times and not have found
7161 * anything, so we are likely way too
7162 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007163 * anything.
7164 *
7165 * However, if the cluster is taken from the
7166 * current block group, release the cluster
7167 * first, so that we stand a better chance of
7168 * succeeding in the unclustered
7169 * allocation. */
7170 if (loop >= LOOP_NO_EMPTY_SIZE &&
Miao Xiee570fd22014-06-19 10:42:50 +08007171 used_block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007172 spin_unlock(&last_ptr->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08007173 btrfs_release_block_group(used_block_group,
7174 delalloc);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007175 goto unclustered_alloc;
7176 }
7177
Chris Masonfa9c0d792009-04-03 09:47:43 -04007178 /*
7179 * this cluster didn't work out, free it and
7180 * start over
7181 */
7182 btrfs_return_cluster_to_free_space(NULL, last_ptr);
7183
Miao Xiee570fd22014-06-19 10:42:50 +08007184 if (used_block_group != block_group)
7185 btrfs_release_block_group(used_block_group,
7186 delalloc);
7187refill_cluster:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007188 if (loop >= LOOP_NO_EMPTY_SIZE) {
7189 spin_unlock(&last_ptr->refill_lock);
7190 goto unclustered_alloc;
7191 }
7192
Chris Mason8de972b2013-01-04 15:39:43 -05007193 aligned_cluster = max_t(unsigned long,
7194 empty_cluster + empty_size,
7195 block_group->full_stripe_len);
7196
Chris Masonfa9c0d792009-04-03 09:47:43 -04007197 /* allocate a cluster in this block group */
Josef Bacik00361582013-08-14 14:02:47 -04007198 ret = btrfs_find_space_cluster(root, block_group,
7199 last_ptr, search_start,
7200 num_bytes,
7201 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007202 if (ret == 0) {
7203 /*
7204 * now pull our allocation out of this
7205 * cluster
7206 */
7207 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08007208 last_ptr,
7209 num_bytes,
7210 search_start,
7211 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007212 if (offset) {
7213 /* we found one, proceed */
7214 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05007215 trace_btrfs_reserve_extent_cluster(root,
7216 block_group, search_start,
7217 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007218 goto checks;
7219 }
Josef Bacik0a243252009-09-11 16:11:20 -04007220 } else if (!cached && loop > LOOP_CACHING_NOWAIT
7221 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007222 spin_unlock(&last_ptr->refill_lock);
7223
Josef Bacik0a243252009-09-11 16:11:20 -04007224 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007225 wait_block_group_cache_progress(block_group,
7226 num_bytes + empty_cluster + empty_size);
7227 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007228 }
Josef Bacik817d52f2009-07-13 21:29:25 -04007229
Chris Masonfa9c0d792009-04-03 09:47:43 -04007230 /*
7231 * at this point we either didn't find a cluster
7232 * or we weren't able to allocate a block from our
7233 * cluster. Free the cluster we've been trying
7234 * to use, and go to the next block group
7235 */
Josef Bacik0a243252009-09-11 16:11:20 -04007236 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04007237 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04007238 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04007239 }
7240
Alexandre Oliva062c05c2011-12-07 19:50:42 -05007241unclustered_alloc:
Josef Bacikc759c4e2015-10-02 15:25:10 -04007242 /*
7243 * We are doing an unclustered alloc, set the fragmented flag so
7244 * we don't bother trying to setup a cluster again until we get
7245 * more space.
7246 */
7247 if (unlikely(last_ptr)) {
7248 spin_lock(&last_ptr->lock);
7249 last_ptr->fragmented = 1;
7250 spin_unlock(&last_ptr->lock);
7251 }
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007252 spin_lock(&block_group->free_space_ctl->tree_lock);
7253 if (cached &&
7254 block_group->free_space_ctl->free_space <
7255 num_bytes + empty_cluster + empty_size) {
Miao Xiea4820392013-09-09 13:19:42 +08007256 if (block_group->free_space_ctl->free_space >
7257 max_extent_size)
7258 max_extent_size =
7259 block_group->free_space_ctl->free_space;
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02007260 spin_unlock(&block_group->free_space_ctl->tree_lock);
7261 goto loop;
7262 }
7263 spin_unlock(&block_group->free_space_ctl->tree_lock);
7264
Josef Bacik6226cb02009-04-03 10:14:18 -04007265 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08007266 num_bytes, empty_size,
7267 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007268 /*
7269 * If we didn't find a chunk, and we haven't failed on this
7270 * block group before, and this block group is in the middle of
7271 * caching and we are ok with waiting, then go ahead and wait
7272 * for progress to be made, and set failed_alloc to true.
7273 *
7274 * If failed_alloc is true then we've already waited on this
7275 * block group once and should move on to the next block group.
7276 */
7277 if (!offset && !failed_alloc && !cached &&
7278 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04007279 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007280 num_bytes + empty_size);
7281 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04007282 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007283 } else if (!offset) {
7284 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04007285 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007286checks:
David Sterba4e54b172014-06-05 01:39:19 +02007287 search_start = ALIGN(offset, root->stripesize);
Chris Masone37c9e62007-05-09 20:13:14 -04007288
Josef Bacik2552d172009-04-03 10:14:19 -04007289 /* move on to the next group */
7290 if (search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08007291 block_group->key.objectid + block_group->key.offset) {
7292 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04007293 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04007294 }
Josef Bacik80eb2342008-10-29 14:49:05 -04007295
Josef Bacik6226cb02009-04-03 10:14:18 -04007296 if (offset < search_start)
Miao Xie215a63d2014-01-15 20:00:56 +08007297 btrfs_add_free_space(block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04007298 search_start - offset);
7299 BUG_ON(offset > search_start);
7300
Miao Xie215a63d2014-01-15 20:00:56 +08007301 ret = btrfs_update_reserved_bytes(block_group, num_bytes,
Miao Xiee570fd22014-06-19 10:42:50 +08007302 alloc_type, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007303 if (ret == -EAGAIN) {
Miao Xie215a63d2014-01-15 20:00:56 +08007304 btrfs_add_free_space(block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007305 goto loop;
7306 }
Yan Zheng11833d62009-09-11 16:11:19 -04007307
Josef Bacik2552d172009-04-03 10:14:19 -04007308 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007309 ins->objectid = search_start;
7310 ins->offset = num_bytes;
7311
Josef Bacik3f7de032011-11-10 08:29:20 -05007312 trace_btrfs_reserve_extent(orig_root, block_group,
7313 search_start, num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08007314 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007315 break;
7316loop:
Josef Bacik0a243252009-09-11 16:11:20 -04007317 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04007318 failed_alloc = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04007319 BUG_ON(index != get_block_group_index(block_group));
Miao Xiee570fd22014-06-19 10:42:50 +08007320 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04007321 }
7322 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05007323
Miao Xie60d2adb2011-09-09 17:34:35 +08007324 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
7325 goto search;
7326
Yan, Zhengb742bb822010-05-16 10:46:24 -04007327 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
7328 goto search;
7329
Josef Bacik285ff5a2012-01-13 15:27:45 -05007330 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05007331 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
7332 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04007333 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
7334 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
7335 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
7336 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04007337 */
Josef Bacik723bda22011-05-27 16:11:38 -04007338 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb822010-05-16 10:46:24 -04007339 index = 0;
Josef Bacika5e681d2015-10-01 14:54:10 -04007340 if (loop == LOOP_CACHING_NOWAIT) {
7341 /*
7342 * We want to skip the LOOP_CACHING_WAIT step if we
7343 * don't have any unached bgs and we've alrelady done a
7344 * full search through.
7345 */
7346 if (have_caching_bg || !full_search)
7347 loop = LOOP_CACHING_WAIT;
7348 else
7349 loop = LOOP_ALLOC_CHUNK;
7350 } else {
7351 loop++;
7352 }
7353
Josef Bacik817d52f2009-07-13 21:29:25 -04007354 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04007355 struct btrfs_trans_handle *trans;
Wang Shilongf017f152014-03-13 13:19:47 +08007356 int exist = 0;
Josef Bacik00361582013-08-14 14:02:47 -04007357
Wang Shilongf017f152014-03-13 13:19:47 +08007358 trans = current->journal_info;
7359 if (trans)
7360 exist = 1;
7361 else
7362 trans = btrfs_join_transaction(root);
7363
Josef Bacik00361582013-08-14 14:02:47 -04007364 if (IS_ERR(trans)) {
7365 ret = PTR_ERR(trans);
7366 goto out;
7367 }
7368
David Sterbab6919a52013-04-29 13:39:40 +00007369 ret = do_chunk_alloc(trans, root, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04007370 CHUNK_ALLOC_FORCE);
Josef Bacika5e681d2015-10-01 14:54:10 -04007371
7372 /*
7373 * If we can't allocate a new chunk we've already looped
7374 * through at least once, move on to the NO_EMPTY_SIZE
7375 * case.
7376 */
7377 if (ret == -ENOSPC)
7378 loop = LOOP_NO_EMPTY_SIZE;
7379
Josef Bacikea658ba2012-09-11 16:57:25 -04007380 /*
7381 * Do not bail out on ENOSPC since we
7382 * can do more things.
7383 */
Josef Bacik00361582013-08-14 14:02:47 -04007384 if (ret < 0 && ret != -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04007385 btrfs_abort_transaction(trans,
7386 root, ret);
Josef Bacik00361582013-08-14 14:02:47 -04007387 else
7388 ret = 0;
Wang Shilongf017f152014-03-13 13:19:47 +08007389 if (!exist)
7390 btrfs_end_transaction(trans, root);
Josef Bacik00361582013-08-14 14:02:47 -04007391 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04007392 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04007393 }
7394
7395 if (loop == LOOP_NO_EMPTY_SIZE) {
Josef Bacika5e681d2015-10-01 14:54:10 -04007396 /*
7397 * Don't loop again if we already have no empty_size and
7398 * no empty_cluster.
7399 */
7400 if (empty_size == 0 &&
7401 empty_cluster == 0) {
7402 ret = -ENOSPC;
7403 goto out;
7404 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04007405 empty_size = 0;
7406 empty_cluster = 0;
7407 }
Chris Mason42e70e72008-11-07 18:17:11 -05007408
Josef Bacik723bda22011-05-27 16:11:38 -04007409 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04007410 } else if (!ins->objectid) {
7411 ret = -ENOSPC;
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007412 } else if (ins->objectid) {
Josef Bacikc759c4e2015-10-02 15:25:10 -04007413 if (!use_cluster && last_ptr) {
7414 spin_lock(&last_ptr->lock);
7415 last_ptr->window_start = ins->objectid;
7416 spin_unlock(&last_ptr->lock);
7417 }
Josef Bacik2552d172009-04-03 10:14:19 -04007418 ret = 0;
7419 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007420out:
Josef Bacik4f4db212015-09-29 11:40:47 -04007421 if (ret == -ENOSPC) {
7422 spin_lock(&space_info->lock);
7423 space_info->max_extent_size = max_extent_size;
7424 spin_unlock(&space_info->lock);
Miao Xiea4820392013-09-09 13:19:42 +08007425 ins->offset = max_extent_size;
Josef Bacik4f4db212015-09-29 11:40:47 -04007426 }
Chris Mason0f70abe2007-02-28 16:46:22 -05007427 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05007428}
Chris Masonec44a352008-04-28 15:29:52 -04007429
Josef Bacik9ed74f22009-09-11 16:12:44 -04007430static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
7431 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04007432{
7433 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb822010-05-16 10:46:24 -04007434 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007435
Josef Bacik9ed74f22009-09-11 16:12:44 -04007436 spin_lock(&info->lock);
Frank Holtonefe120a2013-12-20 11:37:06 -05007437 printk(KERN_INFO "BTRFS: space_info %llu has %llu free, is %sfull\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007438 info->flags,
7439 info->total_bytes - info->bytes_used - info->bytes_pinned -
7440 info->bytes_reserved - info->bytes_readonly,
Chris Masond3977122009-01-05 21:25:51 -05007441 (info->full) ? "" : "not ");
Frank Holtonefe120a2013-12-20 11:37:06 -05007442 printk(KERN_INFO "BTRFS: space_info total=%llu, used=%llu, pinned=%llu, "
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04007443 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007444 info->total_bytes, info->bytes_used, info->bytes_pinned,
7445 info->bytes_reserved, info->bytes_may_use,
7446 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007447 spin_unlock(&info->lock);
7448
7449 if (!dump_block_groups)
7450 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007451
Josef Bacik80eb2342008-10-29 14:49:05 -04007452 down_read(&info->groups_sem);
Yan, Zhengb742bb822010-05-16 10:46:24 -04007453again:
7454 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04007455 spin_lock(&cache->lock);
Frank Holtonefe120a2013-12-20 11:37:06 -05007456 printk(KERN_INFO "BTRFS: "
7457 "block group %llu has %llu bytes, "
7458 "%llu used %llu pinned %llu reserved %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007459 cache->key.objectid, cache->key.offset,
7460 btrfs_block_group_used(&cache->item), cache->pinned,
7461 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04007462 btrfs_dump_free_space(cache, bytes);
7463 spin_unlock(&cache->lock);
7464 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04007465 if (++index < BTRFS_NR_RAID_TYPES)
7466 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04007467 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007468}
Zheng Yane8569812008-09-26 10:05:48 -04007469
Josef Bacik00361582013-08-14 14:02:47 -04007470int btrfs_reserve_extent(struct btrfs_root *root,
Yan Zheng11833d62009-09-11 16:11:19 -04007471 u64 num_bytes, u64 min_alloc_size,
7472 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08007473 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05007474{
Josef Bacik36af4e02015-09-25 16:13:11 -04007475 bool final_tried = num_bytes == min_alloc_size;
David Sterbab6919a52013-04-29 13:39:40 +00007476 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05007477 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04007478
David Sterbab6919a52013-04-29 13:39:40 +00007479 flags = btrfs_get_alloc_profile(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04007480again:
Chris Masondb945352007-10-15 16:15:53 -04007481 WARN_ON(num_bytes < root->sectorsize);
Josef Bacik00361582013-08-14 14:02:47 -04007482 ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
Miao Xiee570fd22014-06-19 10:42:50 +08007483 flags, delalloc);
Chris Mason3b951512008-04-17 11:29:12 -04007484
Miao Xie9e622d62012-01-26 15:01:12 -05007485 if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08007486 if (!final_tried && ins->offset) {
7487 num_bytes = min(num_bytes >> 1, ins->offset);
Zach Brown24542bf2012-11-16 00:04:43 +00007488 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05007489 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05007490 if (num_bytes == min_alloc_size)
7491 final_tried = true;
7492 goto again;
7493 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
7494 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007495
David Sterbab6919a52013-04-29 13:39:40 +00007496 sinfo = __find_space_info(root->fs_info, flags);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007497 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007498 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01007499 if (sinfo)
7500 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05007501 }
Chris Mason925baed2008-06-25 16:01:30 -04007502 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007503
7504 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007505}
7506
Chris Masone688b7252011-10-31 20:52:39 -04007507static int __btrfs_free_reserved_extent(struct btrfs_root *root,
Miao Xiee570fd22014-06-19 10:42:50 +08007508 u64 start, u64 len,
7509 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04007510{
Josef Bacik0f9dd462008-09-23 13:14:11 -04007511 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05007512 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04007513
Josef Bacik0f9dd462008-09-23 13:14:11 -04007514 cache = btrfs_lookup_block_group(root->fs_info, start);
7515 if (!cache) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007516 btrfs_err(root->fs_info, "Unable to find block group for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007517 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04007518 return -ENOSPC;
7519 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05007520
Chris Masone688b7252011-10-31 20:52:39 -04007521 if (pin)
7522 pin_down_extent(root, cache, start, len, 1);
7523 else {
Filipe Mananadcc82f42015-03-23 14:07:40 +00007524 if (btrfs_test_opt(root, DISCARD))
7525 ret = btrfs_discard_extent(root, start, len, NULL);
Chris Masone688b7252011-10-31 20:52:39 -04007526 btrfs_add_free_space(cache, start, len);
Miao Xiee570fd22014-06-19 10:42:50 +08007527 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04007528 }
Dongsheng Yang31193212014-12-12 16:44:35 +08007529
Chris Masonfa9c0d792009-04-03 09:47:43 -04007530 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04007531
liubo1abe9b82011-03-24 11:18:59 +00007532 trace_btrfs_reserved_extent_free(root, start, len);
7533
Chris Masone6dcd2d2008-07-17 12:53:50 -04007534 return ret;
7535}
7536
Chris Masone688b7252011-10-31 20:52:39 -04007537int btrfs_free_reserved_extent(struct btrfs_root *root,
Miao Xiee570fd22014-06-19 10:42:50 +08007538 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04007539{
Miao Xiee570fd22014-06-19 10:42:50 +08007540 return __btrfs_free_reserved_extent(root, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04007541}
7542
7543int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
7544 u64 start, u64 len)
7545{
Miao Xiee570fd22014-06-19 10:42:50 +08007546 return __btrfs_free_reserved_extent(root, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04007547}
7548
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007549static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
7550 struct btrfs_root *root,
7551 u64 parent, u64 root_objectid,
7552 u64 flags, u64 owner, u64 offset,
7553 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007554{
7555 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007556 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007557 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007558 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007559 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007560 struct extent_buffer *leaf;
7561 int type;
7562 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04007563
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007564 if (parent > 0)
7565 type = BTRFS_SHARED_DATA_REF_KEY;
7566 else
7567 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04007568
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007569 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05007570
7571 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007572 if (!path)
7573 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05007574
Chris Masonb9473432009-03-13 11:00:37 -04007575 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007576 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
7577 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007578 if (ret) {
7579 btrfs_free_path(path);
7580 return ret;
7581 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007582
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007583 leaf = path->nodes[0];
7584 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05007585 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007586 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
7587 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
7588 btrfs_set_extent_flags(leaf, extent_item,
7589 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05007590
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007591 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
7592 btrfs_set_extent_inline_ref_type(leaf, iref, type);
7593 if (parent > 0) {
7594 struct btrfs_shared_data_ref *ref;
7595 ref = (struct btrfs_shared_data_ref *)(iref + 1);
7596 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
7597 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
7598 } else {
7599 struct btrfs_extent_data_ref *ref;
7600 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
7601 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
7602 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
7603 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
7604 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
7605 }
Chris Mason47e4bb92008-02-01 14:51:59 -05007606
7607 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05007608 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04007609
Josef Bacikce93ec52014-11-17 15:45:48 -05007610 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007611 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007612 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007613 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05007614 BUG();
7615 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04007616 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007617 return ret;
7618}
7619
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007620static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
7621 struct btrfs_root *root,
7622 u64 parent, u64 root_objectid,
7623 u64 flags, struct btrfs_disk_key *key,
Josef Bacikfcebe452014-05-13 17:30:47 -07007624 int level, struct btrfs_key *ins,
7625 int no_quota)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007626{
7627 int ret;
7628 struct btrfs_fs_info *fs_info = root->fs_info;
7629 struct btrfs_extent_item *extent_item;
7630 struct btrfs_tree_block_info *block_info;
7631 struct btrfs_extent_inline_ref *iref;
7632 struct btrfs_path *path;
7633 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05007634 u32 size = sizeof(*extent_item) + sizeof(*iref);
Josef Bacikfcebe452014-05-13 17:30:47 -07007635 u64 num_bytes = ins->offset;
Josef Bacik3173a182013-03-07 14:22:04 -05007636 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
7637 SKINNY_METADATA);
7638
7639 if (!skinny_metadata)
7640 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007641
7642 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04007643 if (!path) {
7644 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
David Sterba707e8a02014-06-04 19:22:26 +02007645 root->nodesize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07007646 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04007647 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007648
7649 path->leave_spinning = 1;
7650 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
7651 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007652 if (ret) {
Chris Masondd825252015-04-01 08:36:05 -07007653 btrfs_free_path(path);
Josef Bacik857cc2f2013-10-07 15:21:08 -04007654 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
David Sterba707e8a02014-06-04 19:22:26 +02007655 root->nodesize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007656 return ret;
7657 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007658
7659 leaf = path->nodes[0];
7660 extent_item = btrfs_item_ptr(leaf, path->slots[0],
7661 struct btrfs_extent_item);
7662 btrfs_set_extent_refs(leaf, extent_item, 1);
7663 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
7664 btrfs_set_extent_flags(leaf, extent_item,
7665 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007666
Josef Bacik3173a182013-03-07 14:22:04 -05007667 if (skinny_metadata) {
7668 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
David Sterba707e8a02014-06-04 19:22:26 +02007669 num_bytes = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05007670 } else {
7671 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
7672 btrfs_set_tree_block_key(leaf, block_info, key);
7673 btrfs_set_tree_block_level(leaf, block_info, level);
7674 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
7675 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007676
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007677 if (parent > 0) {
7678 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
7679 btrfs_set_extent_inline_ref_type(leaf, iref,
7680 BTRFS_SHARED_BLOCK_REF_KEY);
7681 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
7682 } else {
7683 btrfs_set_extent_inline_ref_type(leaf, iref,
7684 BTRFS_TREE_BLOCK_REF_KEY);
7685 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
7686 }
7687
7688 btrfs_mark_buffer_dirty(leaf);
7689 btrfs_free_path(path);
7690
Josef Bacikce93ec52014-11-17 15:45:48 -05007691 ret = update_block_group(trans, root, ins->objectid, root->nodesize,
7692 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007693 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007694 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007695 ins->objectid, ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007696 BUG();
7697 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04007698
David Sterba707e8a02014-06-04 19:22:26 +02007699 trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->nodesize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007700 return ret;
7701}
7702
7703int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
7704 struct btrfs_root *root,
7705 u64 root_objectid, u64 owner,
7706 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007707{
7708 int ret;
Chris Mason1c2308f82008-09-23 13:14:13 -04007709
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007710 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04007711
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007712 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
7713 ins->offset, 0,
7714 root_objectid, owner, offset,
7715 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007716 return ret;
7717}
Chris Masone02119d2008-09-05 16:13:11 -04007718
7719/*
7720 * this is used by the tree logging recovery code. It records that
7721 * an extent has been allocated and makes sure to clear the free
7722 * space cache bits as well
7723 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007724int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
7725 struct btrfs_root *root,
7726 u64 root_objectid, u64 owner, u64 offset,
7727 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04007728{
7729 int ret;
7730 struct btrfs_block_group_cache *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04007731
7732 /*
7733 * Mixed block groups will exclude before processing the log so we only
7734 * need to do the exlude dance if this fs isn't mixed.
7735 */
7736 if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
7737 ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
7738 if (ret)
7739 return ret;
7740 }
Chris Masone02119d2008-09-05 16:13:11 -04007741
Chris Masone02119d2008-09-05 16:13:11 -04007742 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04007743 if (!block_group)
7744 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04007745
Josef Bacikfb25e912011-07-26 17:00:46 -04007746 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
Miao Xiee570fd22014-06-19 10:42:50 +08007747 RESERVE_ALLOC_NO_ACCOUNT, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007748 BUG_ON(ret); /* logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007749 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
7750 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04007751 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04007752 return ret;
7753}
7754
Eric Sandeen48a3b632013-04-25 20:41:01 +00007755static struct extent_buffer *
7756btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
David Sterbafe864572014-06-15 02:28:42 +02007757 u64 bytenr, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04007758{
7759 struct extent_buffer *buf;
7760
David Sterbaa83fffb2014-06-15 02:39:54 +02007761 buf = btrfs_find_create_tree_block(root, bytenr);
Chris Mason65b51a02008-08-01 15:11:20 -04007762 if (!buf)
7763 return ERR_PTR(-ENOMEM);
7764 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04007765 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04007766 btrfs_tree_lock(buf);
Daniel Dressler01d58472014-11-21 17:15:07 +09007767 clean_tree_block(trans, root->fs_info, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05007768 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05007769
7770 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04007771 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05007772
Chris Masond0c803c2008-09-11 16:17:57 -04007773 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Filipe Manana656f30d2014-09-26 12:25:56 +01007774 buf->log_index = root->log_transid % 2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00007775 /*
7776 * we allow two log transactions at a time, use different
7777 * EXENT bit to differentiate dirty pages.
7778 */
Filipe Manana656f30d2014-09-26 12:25:56 +01007779 if (buf->log_index == 0)
Yan, Zheng8cef4e12009-11-12 09:33:26 +00007780 set_extent_dirty(&root->dirty_log_pages, buf->start,
7781 buf->start + buf->len - 1, GFP_NOFS);
7782 else
7783 set_extent_new(&root->dirty_log_pages, buf->start,
7784 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04007785 } else {
Filipe Manana656f30d2014-09-26 12:25:56 +01007786 buf->log_index = -1;
Chris Masond0c803c2008-09-11 16:17:57 -04007787 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
7788 buf->start + buf->len - 1, GFP_NOFS);
7789 }
Chris Mason65b51a02008-08-01 15:11:20 -04007790 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05007791 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04007792 return buf;
7793}
7794
Yan, Zhengf0486c62010-05-16 10:46:25 -04007795static struct btrfs_block_rsv *
7796use_block_rsv(struct btrfs_trans_handle *trans,
7797 struct btrfs_root *root, u32 blocksize)
7798{
7799 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00007800 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007801 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00007802 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007803
7804 block_rsv = get_block_rsv(trans, root);
7805
Miao Xieb586b322013-05-13 13:55:10 +00007806 if (unlikely(block_rsv->size == 0))
7807 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00007808again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007809 ret = block_rsv_use_bytes(block_rsv, blocksize);
7810 if (!ret)
7811 return block_rsv;
7812
Miao Xieb586b322013-05-13 13:55:10 +00007813 if (block_rsv->failfast)
7814 return ERR_PTR(ret);
7815
Miao Xied88033d2013-05-13 13:55:12 +00007816 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
7817 global_updated = true;
7818 update_global_block_rsv(root->fs_info);
7819 goto again;
7820 }
7821
Miao Xieb586b322013-05-13 13:55:10 +00007822 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
7823 static DEFINE_RATELIMIT_STATE(_rs,
7824 DEFAULT_RATELIMIT_INTERVAL * 10,
7825 /*DEFAULT_RATELIMIT_BURST*/ 1);
7826 if (__ratelimit(&_rs))
7827 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05007828 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00007829 }
7830try_reserve:
7831 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
7832 BTRFS_RESERVE_NO_FLUSH);
7833 if (!ret)
7834 return block_rsv;
7835 /*
7836 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00007837 * the global reserve if its space type is the same as the global
7838 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00007839 */
Miao Xie5881cfc2013-05-13 13:55:11 +00007840 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
7841 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00007842 ret = block_rsv_use_bytes(global_rsv, blocksize);
7843 if (!ret)
7844 return global_rsv;
7845 }
7846 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007847}
7848
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007849static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
7850 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007851{
7852 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007853 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007854}
7855
Chris Masonfec577f2007-02-26 10:40:21 -05007856/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007857 * finds a free extent and does all the dirty work required for allocation
Omar Sandoval67b78592015-02-24 02:47:04 -08007858 * returns the tree buffer or an ERR_PTR on error.
Chris Masonfec577f2007-02-26 10:40:21 -05007859 */
David Sterba4d75f8a2014-06-15 01:54:12 +02007860struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
7861 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007862 u64 parent, u64 root_objectid,
7863 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02007864 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05007865{
Chris Masone2fa7222007-03-12 16:22:34 -04007866 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007867 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04007868 struct extent_buffer *buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08007869 struct btrfs_delayed_extent_op *extent_op;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007870 u64 flags = 0;
7871 int ret;
David Sterba4d75f8a2014-06-15 01:54:12 +02007872 u32 blocksize = root->nodesize;
Josef Bacik3173a182013-03-07 14:22:04 -05007873 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
7874 SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007875
David Sterbafccb84c2014-09-29 23:53:21 +02007876 if (btrfs_test_is_dummy_root(root)) {
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007877 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
David Sterbafe864572014-06-15 02:28:42 +02007878 level);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007879 if (!IS_ERR(buf))
7880 root->alloc_bytenr += blocksize;
7881 return buf;
7882 }
David Sterbafccb84c2014-09-29 23:53:21 +02007883
Yan, Zhengf0486c62010-05-16 10:46:25 -04007884 block_rsv = use_block_rsv(trans, root, blocksize);
7885 if (IS_ERR(block_rsv))
7886 return ERR_CAST(block_rsv);
7887
Josef Bacik00361582013-08-14 14:02:47 -04007888 ret = btrfs_reserve_extent(root, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08007889 empty_size, hint, &ins, 0, 0);
Omar Sandoval67b78592015-02-24 02:47:04 -08007890 if (ret)
7891 goto out_unuse;
Chris Mason55c69072008-01-09 15:55:33 -05007892
David Sterbafe864572014-06-15 02:28:42 +02007893 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
Omar Sandoval67b78592015-02-24 02:47:04 -08007894 if (IS_ERR(buf)) {
7895 ret = PTR_ERR(buf);
7896 goto out_free_reserved;
7897 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007898
7899 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
7900 if (parent == 0)
7901 parent = ins.objectid;
7902 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7903 } else
7904 BUG_ON(parent > 0);
7905
7906 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie78a61842012-11-21 02:21:28 +00007907 extent_op = btrfs_alloc_delayed_extent_op();
Omar Sandoval67b78592015-02-24 02:47:04 -08007908 if (!extent_op) {
7909 ret = -ENOMEM;
7910 goto out_free_buf;
7911 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007912 if (key)
7913 memcpy(&extent_op->key, key, sizeof(extent_op->key));
7914 else
7915 memset(&extent_op->key, 0, sizeof(extent_op->key));
7916 extent_op->flags_to_set = flags;
Josef Bacik3173a182013-03-07 14:22:04 -05007917 if (skinny_metadata)
7918 extent_op->update_key = 0;
7919 else
7920 extent_op->update_key = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007921 extent_op->update_flags = 1;
7922 extent_op->is_data = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04007923 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007924
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007925 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
Omar Sandoval67b78592015-02-24 02:47:04 -08007926 ins.objectid, ins.offset,
7927 parent, root_objectid, level,
7928 BTRFS_ADD_DELAYED_EXTENT,
7929 extent_op, 0);
7930 if (ret)
7931 goto out_free_delayed;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007932 }
Chris Masonfec577f2007-02-26 10:40:21 -05007933 return buf;
Omar Sandoval67b78592015-02-24 02:47:04 -08007934
7935out_free_delayed:
7936 btrfs_free_delayed_extent_op(extent_op);
7937out_free_buf:
7938 free_extent_buffer(buf);
7939out_free_reserved:
7940 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 0);
7941out_unuse:
7942 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
7943 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05007944}
Chris Masona28ec192007-03-06 20:08:01 -05007945
Yan Zheng2c47e6052009-06-27 21:07:35 -04007946struct walk_control {
7947 u64 refs[BTRFS_MAX_LEVEL];
7948 u64 flags[BTRFS_MAX_LEVEL];
7949 struct btrfs_key update_progress;
7950 int stage;
7951 int level;
7952 int shared_level;
7953 int update_ref;
7954 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007955 int reada_slot;
7956 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007957 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007958};
7959
7960#define DROP_REFERENCE 1
7961#define UPDATE_BACKREF 2
7962
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007963static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
7964 struct btrfs_root *root,
7965 struct walk_control *wc,
7966 struct btrfs_path *path)
7967{
7968 u64 bytenr;
7969 u64 generation;
7970 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007971 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007972 u32 nritems;
7973 u32 blocksize;
7974 struct btrfs_key key;
7975 struct extent_buffer *eb;
7976 int ret;
7977 int slot;
7978 int nread = 0;
7979
7980 if (path->slots[wc->level] < wc->reada_slot) {
7981 wc->reada_count = wc->reada_count * 2 / 3;
7982 wc->reada_count = max(wc->reada_count, 2);
7983 } else {
7984 wc->reada_count = wc->reada_count * 3 / 2;
7985 wc->reada_count = min_t(int, wc->reada_count,
7986 BTRFS_NODEPTRS_PER_BLOCK(root));
7987 }
7988
7989 eb = path->nodes[wc->level];
7990 nritems = btrfs_header_nritems(eb);
David Sterba707e8a02014-06-04 19:22:26 +02007991 blocksize = root->nodesize;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007992
7993 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
7994 if (nread >= wc->reada_count)
7995 break;
7996
7997 cond_resched();
7998 bytenr = btrfs_node_blockptr(eb, slot);
7999 generation = btrfs_node_ptr_generation(eb, slot);
8000
8001 if (slot == path->slots[wc->level])
8002 goto reada;
8003
8004 if (wc->stage == UPDATE_BACKREF &&
8005 generation <= root->root_key.offset)
8006 continue;
8007
Yan, Zheng94fcca92009-10-09 09:25:16 -04008008 /* We don't lock the tree block, it's OK to be racy here */
Josef Bacik3173a182013-03-07 14:22:04 -05008009 ret = btrfs_lookup_extent_info(trans, root, bytenr,
8010 wc->level - 1, 1, &refs,
8011 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008012 /* We don't care about errors in readahead. */
8013 if (ret < 0)
8014 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008015 BUG_ON(refs == 0);
8016
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008017 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008018 if (refs == 1)
8019 goto reada;
8020
Yan, Zheng94fcca92009-10-09 09:25:16 -04008021 if (wc->level == 1 &&
8022 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8023 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008024 if (!wc->update_ref ||
8025 generation <= root->root_key.offset)
8026 continue;
8027 btrfs_node_key_to_cpu(eb, &key, slot);
8028 ret = btrfs_comp_cpu_keys(&key,
8029 &wc->update_progress);
8030 if (ret < 0)
8031 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008032 } else {
8033 if (wc->level == 1 &&
8034 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8035 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008036 }
8037reada:
David Sterbad3e46fe2014-06-15 02:04:19 +02008038 readahead_tree_block(root, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008039 nread++;
8040 }
8041 wc->reada_slot = slot;
8042}
8043
Qu Wenruo0ed47922015-04-16 16:55:08 +08008044/*
8045 * TODO: Modify related function to add related node/leaf to dirty_extent_root,
8046 * for later qgroup accounting.
8047 *
8048 * Current, this function does nothing.
8049 */
Mark Fasheh11526512014-07-17 12:39:01 -07008050static int account_leaf_items(struct btrfs_trans_handle *trans,
8051 struct btrfs_root *root,
8052 struct extent_buffer *eb)
8053{
8054 int nr = btrfs_header_nritems(eb);
Qu Wenruo0ed47922015-04-16 16:55:08 +08008055 int i, extent_type;
Mark Fasheh11526512014-07-17 12:39:01 -07008056 struct btrfs_key key;
8057 struct btrfs_file_extent_item *fi;
8058 u64 bytenr, num_bytes;
8059
8060 for (i = 0; i < nr; i++) {
8061 btrfs_item_key_to_cpu(eb, &key, i);
8062
8063 if (key.type != BTRFS_EXTENT_DATA_KEY)
8064 continue;
8065
8066 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
8067 /* filter out non qgroup-accountable extents */
8068 extent_type = btrfs_file_extent_type(eb, fi);
8069
8070 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
8071 continue;
8072
8073 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
8074 if (!bytenr)
8075 continue;
8076
8077 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
Mark Fasheh11526512014-07-17 12:39:01 -07008078 }
8079 return 0;
8080}
8081
8082/*
8083 * Walk up the tree from the bottom, freeing leaves and any interior
8084 * nodes which have had all slots visited. If a node (leaf or
8085 * interior) is freed, the node above it will have it's slot
8086 * incremented. The root node will never be freed.
8087 *
8088 * At the end of this function, we should have a path which has all
8089 * slots incremented to the next position for a search. If we need to
8090 * read a new node it will be NULL and the node above it will have the
8091 * correct slot selected for a later read.
8092 *
8093 * If we increment the root nodes slot counter past the number of
8094 * elements, 1 is returned to signal completion of the search.
8095 */
8096static int adjust_slots_upwards(struct btrfs_root *root,
8097 struct btrfs_path *path, int root_level)
8098{
8099 int level = 0;
8100 int nr, slot;
8101 struct extent_buffer *eb;
8102
8103 if (root_level == 0)
8104 return 1;
8105
8106 while (level <= root_level) {
8107 eb = path->nodes[level];
8108 nr = btrfs_header_nritems(eb);
8109 path->slots[level]++;
8110 slot = path->slots[level];
8111 if (slot >= nr || level == 0) {
8112 /*
8113 * Don't free the root - we will detect this
8114 * condition after our loop and return a
8115 * positive value for caller to stop walking the tree.
8116 */
8117 if (level != root_level) {
8118 btrfs_tree_unlock_rw(eb, path->locks[level]);
8119 path->locks[level] = 0;
8120
8121 free_extent_buffer(eb);
8122 path->nodes[level] = NULL;
8123 path->slots[level] = 0;
8124 }
8125 } else {
8126 /*
8127 * We have a valid slot to walk back down
8128 * from. Stop here so caller can process these
8129 * new nodes.
8130 */
8131 break;
8132 }
8133
8134 level++;
8135 }
8136
8137 eb = path->nodes[root_level];
8138 if (path->slots[root_level] >= btrfs_header_nritems(eb))
8139 return 1;
8140
8141 return 0;
8142}
8143
8144/*
8145 * root_eb is the subtree root and is locked before this function is called.
Qu Wenruo0ed47922015-04-16 16:55:08 +08008146 * TODO: Modify this function to mark all (including complete shared node)
8147 * to dirty_extent_root to allow it get accounted in qgroup.
Mark Fasheh11526512014-07-17 12:39:01 -07008148 */
8149static int account_shared_subtree(struct btrfs_trans_handle *trans,
8150 struct btrfs_root *root,
8151 struct extent_buffer *root_eb,
8152 u64 root_gen,
8153 int root_level)
8154{
8155 int ret = 0;
8156 int level;
8157 struct extent_buffer *eb = root_eb;
8158 struct btrfs_path *path = NULL;
8159
8160 BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL);
8161 BUG_ON(root_eb == NULL);
8162
8163 if (!root->fs_info->quota_enabled)
8164 return 0;
8165
8166 if (!extent_buffer_uptodate(root_eb)) {
8167 ret = btrfs_read_buffer(root_eb, root_gen);
8168 if (ret)
8169 goto out;
8170 }
8171
8172 if (root_level == 0) {
8173 ret = account_leaf_items(trans, root, root_eb);
8174 goto out;
8175 }
8176
8177 path = btrfs_alloc_path();
8178 if (!path)
8179 return -ENOMEM;
8180
8181 /*
8182 * Walk down the tree. Missing extent blocks are filled in as
8183 * we go. Metadata is accounted every time we read a new
8184 * extent block.
8185 *
8186 * When we reach a leaf, we account for file extent items in it,
8187 * walk back up the tree (adjusting slot pointers as we go)
8188 * and restart the search process.
8189 */
8190 extent_buffer_get(root_eb); /* For path */
8191 path->nodes[root_level] = root_eb;
8192 path->slots[root_level] = 0;
8193 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
8194walk_down:
8195 level = root_level;
8196 while (level >= 0) {
8197 if (path->nodes[level] == NULL) {
Mark Fasheh11526512014-07-17 12:39:01 -07008198 int parent_slot;
8199 u64 child_gen;
8200 u64 child_bytenr;
8201
8202 /* We need to get child blockptr/gen from
8203 * parent before we can read it. */
8204 eb = path->nodes[level + 1];
8205 parent_slot = path->slots[level + 1];
8206 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
8207 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
8208
David Sterbace86cd52014-06-15 01:07:32 +02008209 eb = read_tree_block(root, child_bytenr, child_gen);
Liu Bo64c043d2015-05-25 17:30:15 +08008210 if (IS_ERR(eb)) {
8211 ret = PTR_ERR(eb);
8212 goto out;
8213 } else if (!extent_buffer_uptodate(eb)) {
Liu Bo8635eda2015-05-25 17:30:14 +08008214 free_extent_buffer(eb);
Liu Bo64c043d2015-05-25 17:30:15 +08008215 ret = -EIO;
Mark Fasheh11526512014-07-17 12:39:01 -07008216 goto out;
8217 }
8218
8219 path->nodes[level] = eb;
8220 path->slots[level] = 0;
8221
8222 btrfs_tree_read_lock(eb);
8223 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
8224 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
Mark Fasheh11526512014-07-17 12:39:01 -07008225 }
8226
8227 if (level == 0) {
8228 ret = account_leaf_items(trans, root, path->nodes[level]);
8229 if (ret)
8230 goto out;
8231
8232 /* Nonzero return here means we completed our search */
8233 ret = adjust_slots_upwards(root, path, root_level);
8234 if (ret)
8235 break;
8236
8237 /* Restart search with new slots */
8238 goto walk_down;
8239 }
8240
8241 level--;
8242 }
8243
8244 ret = 0;
8245out:
8246 btrfs_free_path(path);
8247
8248 return ret;
8249}
8250
Chris Mason9aca1d52007-03-13 11:09:37 -04008251/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008252 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008253 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04008254 * when wc->stage == UPDATE_BACKREF, this function updates
8255 * back refs for pointers in the block.
8256 *
8257 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04008258 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008259static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8260 struct btrfs_root *root,
8261 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008262 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04008263{
8264 int level = wc->level;
8265 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04008266 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8267 int ret;
8268
8269 if (wc->stage == UPDATE_BACKREF &&
8270 btrfs_header_owner(eb) != root->root_key.objectid)
8271 return 1;
8272
8273 /*
8274 * when reference count of tree block is 1, it won't increase
8275 * again. once full backref flag is set, we never clear it.
8276 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04008277 if (lookup_info &&
8278 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
8279 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008280 BUG_ON(!path->locks[level]);
8281 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05008282 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008283 &wc->refs[level],
8284 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008285 BUG_ON(ret == -ENOMEM);
8286 if (ret)
8287 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008288 BUG_ON(wc->refs[level] == 0);
8289 }
8290
Yan Zheng2c47e6052009-06-27 21:07:35 -04008291 if (wc->stage == DROP_REFERENCE) {
8292 if (wc->refs[level] > 1)
8293 return 1;
8294
8295 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04008296 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008297 path->locks[level] = 0;
8298 }
8299 return 0;
8300 }
8301
8302 /* wc->stage == UPDATE_BACKREF */
8303 if (!(wc->flags[level] & flag)) {
8304 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07008305 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008306 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07008307 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008308 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008309 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04008310 eb->len, flag,
8311 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008312 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04008313 wc->flags[level] |= flag;
8314 }
8315
8316 /*
8317 * the block is shared by multiple trees, so it's not good to
8318 * keep the tree lock
8319 */
8320 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04008321 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008322 path->locks[level] = 0;
8323 }
8324 return 0;
8325}
8326
8327/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008328 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008329 *
8330 * when wc->stage == DROP_REFERENCE, this function checks
8331 * reference count of the block pointed to. if the block
8332 * is shared and we need update back refs for the subtree
8333 * rooted at the block, this function changes wc->stage to
8334 * UPDATE_BACKREF. if the block is shared and there is no
8335 * need to update back, this function drops the reference
8336 * to the block.
8337 *
8338 * NOTE: return value 1 means we should stop walking down.
8339 */
8340static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8341 struct btrfs_root *root,
8342 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008343 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008344{
8345 u64 bytenr;
8346 u64 generation;
8347 u64 parent;
8348 u32 blocksize;
8349 struct btrfs_key key;
8350 struct extent_buffer *next;
8351 int level = wc->level;
8352 int reada = 0;
8353 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07008354 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008355
8356 generation = btrfs_node_ptr_generation(path->nodes[level],
8357 path->slots[level]);
8358 /*
8359 * if the lower level block was created before the snapshot
8360 * was created, we know there is no need to update back refs
8361 * for the subtree
8362 */
8363 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04008364 generation <= root->root_key.offset) {
8365 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008366 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008367 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008368
8369 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
David Sterba707e8a02014-06-04 19:22:26 +02008370 blocksize = root->nodesize;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008371
Daniel Dressler01d58472014-11-21 17:15:07 +09008372 next = btrfs_find_tree_block(root->fs_info, bytenr);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008373 if (!next) {
David Sterbaa83fffb2014-06-15 02:39:54 +02008374 next = btrfs_find_create_tree_block(root, bytenr);
Miao Xie90d2c51d2010-03-25 12:37:12 +00008375 if (!next)
8376 return -ENOMEM;
Josef Bacikb2aaaa32013-07-05 17:05:38 -04008377 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
8378 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008379 reada = 1;
8380 }
8381 btrfs_tree_lock(next);
8382 btrfs_set_lock_blocking(next);
8383
Josef Bacik3173a182013-03-07 14:22:04 -05008384 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04008385 &wc->refs[level - 1],
8386 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008387 if (ret < 0) {
8388 btrfs_tree_unlock(next);
8389 return ret;
8390 }
8391
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00008392 if (unlikely(wc->refs[level - 1] == 0)) {
8393 btrfs_err(root->fs_info, "Missing references.");
8394 BUG();
8395 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008396 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008397
Yan, Zheng94fcca92009-10-09 09:25:16 -04008398 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008399 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07008400 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008401 if (level == 1 &&
8402 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8403 goto skip;
8404
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008405 if (!wc->update_ref ||
8406 generation <= root->root_key.offset)
8407 goto skip;
8408
8409 btrfs_node_key_to_cpu(path->nodes[level], &key,
8410 path->slots[level]);
8411 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
8412 if (ret < 0)
8413 goto skip;
8414
8415 wc->stage = UPDATE_BACKREF;
8416 wc->shared_level = level - 1;
8417 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008418 } else {
8419 if (level == 1 &&
8420 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
8421 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008422 }
8423
Chris Masonb9fab912012-05-06 07:23:47 -04008424 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008425 btrfs_tree_unlock(next);
8426 free_extent_buffer(next);
8427 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008428 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008429 }
8430
8431 if (!next) {
8432 if (reada && level == 1)
8433 reada_walk_down(trans, root, wc, path);
David Sterbace86cd52014-06-15 01:07:32 +02008434 next = read_tree_block(root, bytenr, generation);
Liu Bo64c043d2015-05-25 17:30:15 +08008435 if (IS_ERR(next)) {
8436 return PTR_ERR(next);
8437 } else if (!extent_buffer_uptodate(next)) {
Josef Bacik416bc652013-04-23 14:17:42 -04008438 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00008439 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04008440 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008441 btrfs_tree_lock(next);
8442 btrfs_set_lock_blocking(next);
8443 }
8444
8445 level--;
8446 BUG_ON(level != btrfs_header_level(next));
8447 path->nodes[level] = next;
8448 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008449 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008450 wc->level = level;
8451 if (wc->level == 1)
8452 wc->reada_slot = 0;
8453 return 0;
8454skip:
8455 wc->refs[level - 1] = 0;
8456 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008457 if (wc->stage == DROP_REFERENCE) {
8458 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
8459 parent = path->nodes[level]->start;
8460 } else {
8461 BUG_ON(root->root_key.objectid !=
8462 btrfs_header_owner(path->nodes[level]));
8463 parent = 0;
8464 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008465
Mark Fasheh11526512014-07-17 12:39:01 -07008466 if (need_account) {
8467 ret = account_shared_subtree(trans, root, next,
8468 generation, level - 1);
8469 if (ret) {
David Sterba94647322015-10-08 11:01:36 +02008470 btrfs_err_rl(root->fs_info,
8471 "Error "
Mark Fasheh11526512014-07-17 12:39:01 -07008472 "%d accounting shared subtree. Quota "
David Sterba94647322015-10-08 11:01:36 +02008473 "is out of sync, rescan required.",
8474 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008475 }
8476 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04008477 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008478 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008479 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008480 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008481 btrfs_tree_unlock(next);
8482 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04008483 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008484 return 1;
8485}
8486
8487/*
Liu Bo2c016dc2012-12-26 15:32:17 +08008488 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04008489 *
8490 * when wc->stage == DROP_REFERENCE, this function drops
8491 * reference count on the block.
8492 *
8493 * when wc->stage == UPDATE_BACKREF, this function changes
8494 * wc->stage back to DROP_REFERENCE if we changed wc->stage
8495 * to UPDATE_BACKREF previously while processing the block.
8496 *
8497 * NOTE: return value 1 means we should stop walking up.
8498 */
8499static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8500 struct btrfs_root *root,
8501 struct btrfs_path *path,
8502 struct walk_control *wc)
8503{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008504 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008505 int level = wc->level;
8506 struct extent_buffer *eb = path->nodes[level];
8507 u64 parent = 0;
8508
8509 if (wc->stage == UPDATE_BACKREF) {
8510 BUG_ON(wc->shared_level < level);
8511 if (level < wc->shared_level)
8512 goto out;
8513
Yan Zheng2c47e6052009-06-27 21:07:35 -04008514 ret = find_next_key(path, level + 1, &wc->update_progress);
8515 if (ret > 0)
8516 wc->update_ref = 0;
8517
8518 wc->stage = DROP_REFERENCE;
8519 wc->shared_level = -1;
8520 path->slots[level] = 0;
8521
8522 /*
8523 * check reference count again if the block isn't locked.
8524 * we should start walking down the tree again if reference
8525 * count is one.
8526 */
8527 if (!path->locks[level]) {
8528 BUG_ON(level == 0);
8529 btrfs_tree_lock(eb);
8530 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008531 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008532
8533 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05008534 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008535 &wc->refs[level],
8536 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008537 if (ret < 0) {
8538 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008539 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008540 return ret;
8541 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008542 BUG_ON(wc->refs[level] == 0);
8543 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04008544 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00008545 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008546 return 1;
8547 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008548 }
8549 }
8550
8551 /* wc->stage == DROP_REFERENCE */
8552 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
8553
8554 if (wc->refs[level] == 1) {
8555 if (level == 0) {
8556 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07008557 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008558 else
Josef Bacike339a6b2014-07-02 10:54:25 -07008559 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008560 BUG_ON(ret); /* -ENOMEM */
Mark Fasheh11526512014-07-17 12:39:01 -07008561 ret = account_leaf_items(trans, root, eb);
8562 if (ret) {
David Sterba94647322015-10-08 11:01:36 +02008563 btrfs_err_rl(root->fs_info,
8564 "error "
Mark Fasheh11526512014-07-17 12:39:01 -07008565 "%d accounting leaf items. Quota "
David Sterba94647322015-10-08 11:01:36 +02008566 "is out of sync, rescan required.",
8567 ret);
Mark Fasheh11526512014-07-17 12:39:01 -07008568 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008569 }
8570 /* make block locked assertion in clean_tree_block happy */
8571 if (!path->locks[level] &&
8572 btrfs_header_generation(eb) == trans->transid) {
8573 btrfs_tree_lock(eb);
8574 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008575 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008576 }
Daniel Dressler01d58472014-11-21 17:15:07 +09008577 clean_tree_block(trans, root->fs_info, eb);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008578 }
8579
8580 if (eb == root->node) {
8581 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8582 parent = eb->start;
8583 else
8584 BUG_ON(root->root_key.objectid !=
8585 btrfs_header_owner(eb));
8586 } else {
8587 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8588 parent = path->nodes[level + 1]->start;
8589 else
8590 BUG_ON(root->root_key.objectid !=
8591 btrfs_header_owner(path->nodes[level + 1]));
8592 }
8593
Jan Schmidt5581a512012-05-16 17:04:52 +02008594 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008595out:
8596 wc->refs[level] = 0;
8597 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008598 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008599}
8600
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008601static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
8602 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008603 struct btrfs_path *path,
8604 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008605{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008606 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008607 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008608 int ret;
8609
Yan Zheng2c47e6052009-06-27 21:07:35 -04008610 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04008611 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008612 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008613 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008614
Yan Zheng2c47e6052009-06-27 21:07:35 -04008615 if (level == 0)
8616 break;
8617
Yan, Zheng7a7965f2010-02-01 02:41:17 +00008618 if (path->slots[level] >=
8619 btrfs_header_nritems(path->nodes[level]))
8620 break;
8621
Yan, Zheng94fcca92009-10-09 09:25:16 -04008622 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008623 if (ret > 0) {
8624 path->slots[level]++;
8625 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00008626 } else if (ret < 0)
8627 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008628 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008629 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04008630 return 0;
8631}
8632
Chris Masond3977122009-01-05 21:25:51 -05008633static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05008634 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04008635 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008636 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05008637{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008638 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05008639 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04008640
Yan Zheng2c47e6052009-06-27 21:07:35 -04008641 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
8642 while (level < max_level && path->nodes[level]) {
8643 wc->level = level;
8644 if (path->slots[level] + 1 <
8645 btrfs_header_nritems(path->nodes[level])) {
8646 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05008647 return 0;
8648 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008649 ret = walk_up_proc(trans, root, path, wc);
8650 if (ret > 0)
8651 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05008652
Yan Zheng2c47e6052009-06-27 21:07:35 -04008653 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04008654 btrfs_tree_unlock_rw(path->nodes[level],
8655 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008656 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008657 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008658 free_extent_buffer(path->nodes[level]);
8659 path->nodes[level] = NULL;
8660 level++;
Chris Mason20524f02007-03-10 06:35:47 -05008661 }
8662 }
8663 return 1;
8664}
8665
Chris Mason9aca1d52007-03-13 11:09:37 -04008666/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04008667 * drop a subvolume tree.
8668 *
8669 * this function traverses the tree freeing any blocks that only
8670 * referenced by the tree.
8671 *
8672 * when a shared tree block is found. this function decreases its
8673 * reference count by one. if update_ref is true, this function
8674 * also make sure backrefs for the shared block and all lower level
8675 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00008676 *
8677 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04008678 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04008679int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008680 struct btrfs_block_rsv *block_rsv, int update_ref,
8681 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05008682{
Chris Mason5caf2a02007-04-02 11:20:42 -04008683 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008684 struct btrfs_trans_handle *trans;
8685 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04008686 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008687 struct walk_control *wc;
8688 struct btrfs_key key;
8689 int err = 0;
8690 int ret;
8691 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04008692 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05008693
Mark Fasheh11526512014-07-17 12:39:01 -07008694 btrfs_debug(root->fs_info, "Drop subvolume %llu", root->objectid);
8695
Chris Mason5caf2a02007-04-02 11:20:42 -04008696 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00008697 if (!path) {
8698 err = -ENOMEM;
8699 goto out;
8700 }
Chris Mason20524f02007-03-10 06:35:47 -05008701
Yan Zheng2c47e6052009-06-27 21:07:35 -04008702 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07008703 if (!wc) {
8704 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00008705 err = -ENOMEM;
8706 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07008707 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008708
Yan, Zhenga22285a2010-05-16 10:48:46 -04008709 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008710 if (IS_ERR(trans)) {
8711 err = PTR_ERR(trans);
8712 goto out_free;
8713 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00008714
Yan, Zheng3fd0a552010-05-16 10:49:59 -04008715 if (block_rsv)
8716 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008717
Chris Mason9f3a7422007-08-07 15:52:19 -04008718 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008719 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008720 path->nodes[level] = btrfs_lock_root_node(root);
8721 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04008722 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008723 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008724 memset(&wc->update_progress, 0,
8725 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04008726 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04008727 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008728 memcpy(&wc->update_progress, &key,
8729 sizeof(wc->update_progress));
8730
Chris Mason6702ed42007-08-07 16:15:09 -04008731 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008732 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04008733 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008734 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
8735 path->lowest_level = 0;
8736 if (ret < 0) {
8737 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008738 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04008739 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008740 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008741
Chris Mason7d9eb122008-07-08 14:19:17 -04008742 /*
8743 * unlock our path, this is safe because only this
8744 * function is allowed to delete this snapshot
8745 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008746 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04008747
Yan Zheng2c47e6052009-06-27 21:07:35 -04008748 level = btrfs_header_level(root->node);
8749 while (1) {
8750 btrfs_tree_lock(path->nodes[level]);
8751 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04008752 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008753
8754 ret = btrfs_lookup_extent_info(trans, root,
8755 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05008756 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04008757 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008758 if (ret < 0) {
8759 err = ret;
8760 goto out_end_trans;
8761 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008762 BUG_ON(wc->refs[level] == 0);
8763
8764 if (level == root_item->drop_level)
8765 break;
8766
8767 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04008768 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008769 WARN_ON(wc->refs[level] != 1);
8770 level--;
8771 }
8772 }
8773
8774 wc->level = level;
8775 wc->shared_level = -1;
8776 wc->stage = DROP_REFERENCE;
8777 wc->update_ref = update_ref;
8778 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008779 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008780 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008781
8782 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00008783
Yan Zheng2c47e6052009-06-27 21:07:35 -04008784 ret = walk_down_tree(trans, root, path, wc);
8785 if (ret < 0) {
8786 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04008787 break;
8788 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008789
8790 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
8791 if (ret < 0) {
8792 err = ret;
8793 break;
8794 }
8795
8796 if (ret > 0) {
8797 BUG_ON(wc->stage != DROP_REFERENCE);
8798 break;
8799 }
8800
8801 if (wc->stage == DROP_REFERENCE) {
8802 level = wc->level;
8803 btrfs_node_key(path->nodes[level],
8804 &root_item->drop_progress,
8805 path->slots[level]);
8806 root_item->drop_level = level;
8807 }
8808
8809 BUG_ON(wc->level == 0);
Josef Bacik3c8f2422013-07-15 11:57:06 -04008810 if (btrfs_should_end_transaction(trans, tree_root) ||
8811 (!for_reloc && btrfs_need_cleaner_sleep(root))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008812 ret = btrfs_update_root(trans, tree_root,
8813 &root->root_key,
8814 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008815 if (ret) {
8816 btrfs_abort_transaction(trans, tree_root, ret);
8817 err = ret;
8818 goto out_end_trans;
8819 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008820
Yan, Zheng3fd0a552010-05-16 10:49:59 -04008821 btrfs_end_transaction_throttle(trans, tree_root);
Josef Bacik3c8f2422013-07-15 11:57:06 -04008822 if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05008823 pr_debug("BTRFS: drop snapshot early exit\n");
Josef Bacik3c8f2422013-07-15 11:57:06 -04008824 err = -EAGAIN;
8825 goto out_free;
8826 }
8827
Yan, Zhenga22285a2010-05-16 10:48:46 -04008828 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008829 if (IS_ERR(trans)) {
8830 err = PTR_ERR(trans);
8831 goto out_free;
8832 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04008833 if (block_rsv)
8834 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04008835 }
Chris Mason20524f02007-03-10 06:35:47 -05008836 }
David Sterbab3b4aa72011-04-21 01:20:15 +02008837 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008838 if (err)
8839 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008840
8841 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008842 if (ret) {
8843 btrfs_abort_transaction(trans, tree_root, ret);
8844 goto out_end_trans;
8845 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008846
Yan, Zheng76dda932009-09-21 16:00:26 -04008847 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00008848 ret = btrfs_find_root(tree_root, &root->root_key, path,
8849 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008850 if (ret < 0) {
8851 btrfs_abort_transaction(trans, tree_root, ret);
8852 err = ret;
8853 goto out_end_trans;
8854 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05008855 /* if we fail to delete the orphan item this time
8856 * around, it'll get picked up the next time.
8857 *
8858 * The most common failure here is just -ENOENT.
8859 */
8860 btrfs_del_orphan_item(trans, tree_root,
8861 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04008862 }
8863 }
8864
Miao Xie27cdeb72014-04-02 19:51:05 +08008865 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Josef Bacik2b9dbef2015-09-15 10:07:04 -04008866 btrfs_add_dropped_root(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04008867 } else {
8868 free_extent_buffer(root->node);
8869 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00008870 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04008871 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04008872 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008873out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04008874 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008875out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04008876 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04008877 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00008878out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04008879 /*
8880 * So if we need to stop dropping the snapshot for whatever reason we
8881 * need to make sure to add it back to the dead root list so that we
8882 * keep trying to do the work later. This also cleans up roots if we
8883 * don't have it in the radix (like when we recover after a power fail
8884 * or unmount) so we don't leak memory.
8885 */
Josef Bacikb37b39c2013-07-23 16:57:15 -04008886 if (!for_reloc && root_dropped == false)
Josef Bacikd29a9f62013-07-17 19:30:20 -04008887 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08008888 if (err && err != -EAGAIN)
Anand Jaina4553fe2015-09-25 14:43:01 +08008889 btrfs_std_error(root->fs_info, err, NULL);
Jeff Mahoney2c536792011-10-03 23:22:41 -04008890 return err;
Chris Mason20524f02007-03-10 06:35:47 -05008891}
Chris Mason9078a3e2007-04-26 16:46:15 -04008892
Yan Zheng2c47e6052009-06-27 21:07:35 -04008893/*
8894 * drop subtree rooted at tree block 'node'.
8895 *
8896 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008897 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04008898 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04008899int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
8900 struct btrfs_root *root,
8901 struct extent_buffer *node,
8902 struct extent_buffer *parent)
8903{
8904 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008905 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008906 int level;
8907 int parent_level;
8908 int ret = 0;
8909 int wret;
8910
Yan Zheng2c47e6052009-06-27 21:07:35 -04008911 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
8912
Yan Zhengf82d02d2008-10-29 14:49:05 -04008913 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00008914 if (!path)
8915 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008916
Yan Zheng2c47e6052009-06-27 21:07:35 -04008917 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00008918 if (!wc) {
8919 btrfs_free_path(path);
8920 return -ENOMEM;
8921 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008922
Chris Masonb9447ef82009-03-09 11:45:38 -04008923 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008924 parent_level = btrfs_header_level(parent);
8925 extent_buffer_get(parent);
8926 path->nodes[parent_level] = parent;
8927 path->slots[parent_level] = btrfs_header_nritems(parent);
8928
Chris Masonb9447ef82009-03-09 11:45:38 -04008929 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008930 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008931 path->nodes[level] = node;
8932 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008933 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008934
8935 wc->refs[parent_level] = 1;
8936 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8937 wc->level = level;
8938 wc->shared_level = -1;
8939 wc->stage = DROP_REFERENCE;
8940 wc->update_ref = 0;
8941 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008942 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008943 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008944
8945 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008946 wret = walk_down_tree(trans, root, path, wc);
8947 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04008948 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008949 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008950 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04008951
Yan Zheng2c47e6052009-06-27 21:07:35 -04008952 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008953 if (wret < 0)
8954 ret = wret;
8955 if (wret != 0)
8956 break;
8957 }
8958
Yan Zheng2c47e6052009-06-27 21:07:35 -04008959 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008960 btrfs_free_path(path);
8961 return ret;
8962}
8963
Chris Masonec44a352008-04-28 15:29:52 -04008964static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8965{
8966 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008967 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04008968
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008969 /*
8970 * if restripe for this chunk_type is on pick target profile and
8971 * return, otherwise do the usual balance
8972 */
8973 stripped = get_restripe_target(root->fs_info, flags);
8974 if (stripped)
8975 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02008976
Miao Xie95669972014-07-24 11:37:14 +08008977 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05008978
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008979 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008980 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008981 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8982
Chris Masonec44a352008-04-28 15:29:52 -04008983 if (num_devices == 1) {
8984 stripped |= BTRFS_BLOCK_GROUP_DUP;
8985 stripped = flags & ~stripped;
8986
8987 /* turn raid0 into single device chunks */
8988 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8989 return stripped;
8990
8991 /* turn mirroring into duplication */
8992 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8993 BTRFS_BLOCK_GROUP_RAID10))
8994 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04008995 } else {
8996 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04008997 if (flags & stripped)
8998 return flags;
8999
9000 stripped |= BTRFS_BLOCK_GROUP_DUP;
9001 stripped = flags & ~stripped;
9002
9003 /* switch duplicated blocks with raid1 */
9004 if (flags & BTRFS_BLOCK_GROUP_DUP)
9005 return stripped | BTRFS_BLOCK_GROUP_RAID1;
9006
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009007 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04009008 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03009009
Chris Masonec44a352008-04-28 15:29:52 -04009010 return flags;
9011}
9012
Zhaolei868f4012015-08-05 16:43:27 +08009013static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04009014{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009015 struct btrfs_space_info *sinfo = cache->space_info;
9016 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00009017 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009018 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04009019
Miao Xie199c36e2011-07-15 10:34:36 +00009020 /*
9021 * We need some metadata space and system metadata space for
9022 * allocating chunks in some corner cases until we force to set
9023 * it to be readonly.
9024 */
9025 if ((sinfo->flags &
9026 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
9027 !force)
9028 min_allocable_bytes = 1 * 1024 * 1024;
9029 else
9030 min_allocable_bytes = 0;
9031
Yan, Zhengf0486c62010-05-16 10:46:25 -04009032 spin_lock(&sinfo->lock);
9033 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00009034
9035 if (cache->ro) {
Zhaolei868f4012015-08-05 16:43:27 +08009036 cache->ro++;
WuBo61cfea92011-07-26 03:30:11 +00009037 ret = 0;
9038 goto out;
9039 }
9040
Yan, Zhengf0486c62010-05-16 10:46:25 -04009041 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
9042 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04009043
Yan, Zhengf0486c62010-05-16 10:46:25 -04009044 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04009045 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
9046 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04009047 sinfo->bytes_readonly += num_bytes;
Zhaolei868f4012015-08-05 16:43:27 +08009048 cache->ro++;
Josef Bacik633c0aa2014-10-31 09:49:34 -04009049 list_add_tail(&cache->ro_list, &sinfo->ro_bgs);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009050 ret = 0;
9051 }
WuBo61cfea92011-07-26 03:30:11 +00009052out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04009053 spin_unlock(&cache->lock);
9054 spin_unlock(&sinfo->lock);
9055 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04009056}
9057
Zhaolei868f4012015-08-05 16:43:27 +08009058int btrfs_inc_block_group_ro(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04009059 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009060
9061{
Yan, Zhengf0486c62010-05-16 10:46:25 -04009062 struct btrfs_trans_handle *trans;
9063 u64 alloc_flags;
9064 int ret;
9065
Chris Mason1bbc6212015-04-06 12:46:08 -07009066again:
Josef Bacik7a7eaa42011-04-13 12:54:33 -04009067 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009068 if (IS_ERR(trans))
9069 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009070
Chris Mason1bbc6212015-04-06 12:46:08 -07009071 /*
9072 * we're not allowed to set block groups readonly after the dirty
9073 * block groups cache has started writing. If it already started,
9074 * back off and let this transaction commit
9075 */
9076 mutex_lock(&root->fs_info->ro_block_group_mutex);
Josef Bacik3204d332015-09-24 10:46:10 -04009077 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
Chris Mason1bbc6212015-04-06 12:46:08 -07009078 u64 transid = trans->transid;
9079
9080 mutex_unlock(&root->fs_info->ro_block_group_mutex);
9081 btrfs_end_transaction(trans, root);
9082
9083 ret = btrfs_wait_for_commit(root, transid);
9084 if (ret)
9085 return ret;
9086 goto again;
9087 }
9088
Chris Mason153c35b2015-05-19 18:54:41 -07009089 /*
9090 * if we are changing raid levels, try to allocate a corresponding
9091 * block group with the new raid level.
9092 */
9093 alloc_flags = update_block_group_flags(root, cache->flags);
9094 if (alloc_flags != cache->flags) {
9095 ret = do_chunk_alloc(trans, root, alloc_flags,
9096 CHUNK_ALLOC_FORCE);
9097 /*
9098 * ENOSPC is allowed here, we may have enough space
9099 * already allocated at the new raid level to
9100 * carry on
9101 */
9102 if (ret == -ENOSPC)
9103 ret = 0;
9104 if (ret < 0)
9105 goto out;
9106 }
Chris Mason1bbc6212015-04-06 12:46:08 -07009107
Zhaolei868f4012015-08-05 16:43:27 +08009108 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009109 if (!ret)
9110 goto out;
9111 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04009112 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04009113 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009114 if (ret < 0)
9115 goto out;
Zhaolei868f4012015-08-05 16:43:27 +08009116 ret = inc_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009117out:
Shaohua Li2f081082015-01-09 10:40:15 -08009118 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
9119 alloc_flags = update_block_group_flags(root, cache->flags);
Filipe Mananaa9629592015-05-18 19:11:40 +01009120 lock_chunks(root->fs_info->chunk_root);
Filipe Manana4617ea32015-06-09 17:48:21 +01009121 check_system_chunk(trans, root, alloc_flags);
Filipe Mananaa9629592015-05-18 19:11:40 +01009122 unlock_chunks(root->fs_info->chunk_root);
Shaohua Li2f081082015-01-09 10:40:15 -08009123 }
Chris Mason1bbc6212015-04-06 12:46:08 -07009124 mutex_unlock(&root->fs_info->ro_block_group_mutex);
Shaohua Li2f081082015-01-09 10:40:15 -08009125
Yan, Zhengf0486c62010-05-16 10:46:25 -04009126 btrfs_end_transaction(trans, root);
9127 return ret;
9128}
9129
Chris Masonc87f08c2011-02-16 13:57:04 -05009130int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
9131 struct btrfs_root *root, u64 type)
9132{
9133 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04009134 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04009135 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05009136}
9137
Miao Xie6d07bce2011-01-05 10:07:31 +00009138/*
9139 * helper to account the unused space of all the readonly block group in the
Josef Bacik633c0aa2014-10-31 09:49:34 -04009140 * space_info. takes mirrors into account.
Miao Xie6d07bce2011-01-05 10:07:31 +00009141 */
Josef Bacik633c0aa2014-10-31 09:49:34 -04009142u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
Miao Xie6d07bce2011-01-05 10:07:31 +00009143{
9144 struct btrfs_block_group_cache *block_group;
9145 u64 free_bytes = 0;
9146 int factor;
9147
Josef Bacik633c0aa2014-10-31 09:49:34 -04009148 /* It's df, we don't care if it's racey */
9149 if (list_empty(&sinfo->ro_bgs))
9150 return 0;
9151
9152 spin_lock(&sinfo->lock);
9153 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00009154 spin_lock(&block_group->lock);
9155
9156 if (!block_group->ro) {
9157 spin_unlock(&block_group->lock);
9158 continue;
9159 }
9160
9161 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
9162 BTRFS_BLOCK_GROUP_RAID10 |
9163 BTRFS_BLOCK_GROUP_DUP))
9164 factor = 2;
9165 else
9166 factor = 1;
9167
9168 free_bytes += (block_group->key.offset -
9169 btrfs_block_group_used(&block_group->item)) *
9170 factor;
9171
9172 spin_unlock(&block_group->lock);
9173 }
Miao Xie6d07bce2011-01-05 10:07:31 +00009174 spin_unlock(&sinfo->lock);
9175
9176 return free_bytes;
9177}
9178
Zhaolei868f4012015-08-05 16:43:27 +08009179void btrfs_dec_block_group_ro(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04009180 struct btrfs_block_group_cache *cache)
9181{
9182 struct btrfs_space_info *sinfo = cache->space_info;
9183 u64 num_bytes;
9184
9185 BUG_ON(!cache->ro);
9186
9187 spin_lock(&sinfo->lock);
9188 spin_lock(&cache->lock);
Zhaolei868f4012015-08-05 16:43:27 +08009189 if (!--cache->ro) {
9190 num_bytes = cache->key.offset - cache->reserved -
9191 cache->pinned - cache->bytes_super -
9192 btrfs_block_group_used(&cache->item);
9193 sinfo->bytes_readonly -= num_bytes;
9194 list_del_init(&cache->ro_list);
9195 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009196 spin_unlock(&cache->lock);
9197 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009198}
9199
Josef Bacikba1bf482009-09-11 16:11:19 -04009200/*
9201 * checks to see if its even possible to relocate this block group.
9202 *
9203 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
9204 * ok to go ahead and try.
9205 */
9206int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04009207{
Zheng Yan1a40e232008-09-26 10:09:34 -04009208 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04009209 struct btrfs_space_info *space_info;
9210 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
9211 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04009212 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00009213 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04009214 u64 dev_min = 1;
9215 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009216 u64 target;
liubocdcb7252011-08-03 10:15:25 +00009217 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04009218 int full = 0;
9219 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05009220
Josef Bacikba1bf482009-09-11 16:11:19 -04009221 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04009222
Josef Bacikba1bf482009-09-11 16:11:19 -04009223 /* odd, couldn't find the block group, leave it alone */
9224 if (!block_group)
9225 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05009226
liubocdcb7252011-08-03 10:15:25 +00009227 min_free = btrfs_block_group_used(&block_group->item);
9228
Josef Bacikba1bf482009-09-11 16:11:19 -04009229 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00009230 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04009231 goto out;
Chris Mason323da792008-05-09 11:46:48 -04009232
Josef Bacikba1bf482009-09-11 16:11:19 -04009233 space_info = block_group->space_info;
9234 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04009235
Josef Bacikba1bf482009-09-11 16:11:19 -04009236 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04009237
Josef Bacikba1bf482009-09-11 16:11:19 -04009238 /*
9239 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04009240 * relocate it unless we're able to allocate a new chunk below.
9241 *
9242 * Otherwise, we need to make sure we have room in the space to handle
9243 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04009244 */
Chris Mason7ce618d2009-09-22 14:48:44 -04009245 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00009246 (space_info->bytes_used + space_info->bytes_reserved +
9247 space_info->bytes_pinned + space_info->bytes_readonly +
9248 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04009249 spin_unlock(&space_info->lock);
9250 goto out;
9251 }
9252 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009253
Josef Bacikba1bf482009-09-11 16:11:19 -04009254 /*
9255 * ok we don't have enough space, but maybe we have free space on our
9256 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009257 * alloc devices and guess if we have enough space. if this block
9258 * group is going to be restriped, run checks against the target
9259 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04009260 */
9261 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05009262
liubocdcb7252011-08-03 10:15:25 +00009263 /*
9264 * index:
9265 * 0: raid10
9266 * 1: raid1
9267 * 2: dup
9268 * 3: raid0
9269 * 4: single
9270 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009271 target = get_restripe_target(root->fs_info, block_group->flags);
9272 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00009273 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03009274 } else {
9275 /*
9276 * this is just a balance, so if we were marked as full
9277 * we know there is no space for a new chunk
9278 */
9279 if (full)
9280 goto out;
9281
9282 index = get_block_group_index(block_group);
9283 }
9284
Miao Xiee6ec7162013-01-17 05:38:51 +00009285 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00009286 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04009287 /* Divide by 2 */
9288 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009289 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00009290 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00009291 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04009292 /* Multiply by 2 */
9293 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00009294 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00009295 dev_min = fs_devices->rw_devices;
David Sterba47c57132015-02-20 18:43:47 +01009296 min_free = div64_u64(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00009297 }
9298
Josef Bacik6df9a952013-06-27 13:22:46 -04009299 /* We need to do this so that we can look at pending chunks */
9300 trans = btrfs_join_transaction(root);
9301 if (IS_ERR(trans)) {
9302 ret = PTR_ERR(trans);
9303 goto out;
9304 }
9305
Josef Bacikba1bf482009-09-11 16:11:19 -04009306 mutex_lock(&root->fs_info->chunk_mutex);
9307 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00009308 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04009309
Josef Bacikba1bf482009-09-11 16:11:19 -04009310 /*
9311 * check to make sure we can actually find a chunk with enough
9312 * space to fit our block group in.
9313 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01009314 if (device->total_bytes > device->bytes_used + min_free &&
9315 !device->is_tgtdev_for_dev_replace) {
Josef Bacik6df9a952013-06-27 13:22:46 -04009316 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00009317 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04009318 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00009319 dev_nr++;
9320
9321 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05009322 break;
liubocdcb7252011-08-03 10:15:25 +00009323
Josef Bacikba1bf482009-09-11 16:11:19 -04009324 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05009325 }
Chris Masonedbd8d42007-12-21 16:27:24 -05009326 }
Josef Bacikba1bf482009-09-11 16:11:19 -04009327 mutex_unlock(&root->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04009328 btrfs_end_transaction(trans, root);
Chris Masonedbd8d42007-12-21 16:27:24 -05009329out:
Josef Bacikba1bf482009-09-11 16:11:19 -04009330 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05009331 return ret;
9332}
9333
Christoph Hellwigb2950862008-12-02 09:54:17 -05009334static int find_first_block_group(struct btrfs_root *root,
9335 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04009336{
Chris Mason925baed2008-06-25 16:01:30 -04009337 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009338 struct btrfs_key found_key;
9339 struct extent_buffer *leaf;
9340 int slot;
9341
9342 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
9343 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009344 goto out;
9345
Chris Masond3977122009-01-05 21:25:51 -05009346 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009347 slot = path->slots[0];
9348 leaf = path->nodes[0];
9349 if (slot >= btrfs_header_nritems(leaf)) {
9350 ret = btrfs_next_leaf(root, path);
9351 if (ret == 0)
9352 continue;
9353 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04009354 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04009355 break;
9356 }
9357 btrfs_item_key_to_cpu(leaf, &found_key, slot);
9358
9359 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04009360 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
9361 ret = 0;
9362 goto out;
9363 }
Chris Mason0b86a832008-03-24 15:01:56 -04009364 path->slots[0]++;
9365 }
Chris Mason925baed2008-06-25 16:01:30 -04009366out:
Chris Mason0b86a832008-03-24 15:01:56 -04009367 return ret;
9368}
9369
Josef Bacik0af3d002010-06-21 14:48:16 -04009370void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9371{
9372 struct btrfs_block_group_cache *block_group;
9373 u64 last = 0;
9374
9375 while (1) {
9376 struct inode *inode;
9377
9378 block_group = btrfs_lookup_first_block_group(info, last);
9379 while (block_group) {
9380 spin_lock(&block_group->lock);
9381 if (block_group->iref)
9382 break;
9383 spin_unlock(&block_group->lock);
9384 block_group = next_block_group(info->tree_root,
9385 block_group);
9386 }
9387 if (!block_group) {
9388 if (last == 0)
9389 break;
9390 last = 0;
9391 continue;
9392 }
9393
9394 inode = block_group->inode;
9395 block_group->iref = 0;
9396 block_group->inode = NULL;
9397 spin_unlock(&block_group->lock);
9398 iput(inode);
9399 last = block_group->key.objectid + block_group->key.offset;
9400 btrfs_put_block_group(block_group);
9401 }
9402}
9403
Zheng Yan1a40e232008-09-26 10:09:34 -04009404int btrfs_free_block_groups(struct btrfs_fs_info *info)
9405{
9406 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04009407 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04009408 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04009409 struct rb_node *n;
9410
Josef Bacik9e351cc2014-03-13 15:42:13 -04009411 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009412 while (!list_empty(&info->caching_block_groups)) {
9413 caching_ctl = list_entry(info->caching_block_groups.next,
9414 struct btrfs_caching_control, list);
9415 list_del(&caching_ctl->list);
9416 put_caching_control(caching_ctl);
9417 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04009418 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04009419
Josef Bacik47ab2a62014-09-18 11:20:02 -04009420 spin_lock(&info->unused_bgs_lock);
9421 while (!list_empty(&info->unused_bgs)) {
9422 block_group = list_first_entry(&info->unused_bgs,
9423 struct btrfs_block_group_cache,
9424 bg_list);
9425 list_del_init(&block_group->bg_list);
9426 btrfs_put_block_group(block_group);
9427 }
9428 spin_unlock(&info->unused_bgs_lock);
9429
Zheng Yan1a40e232008-09-26 10:09:34 -04009430 spin_lock(&info->block_group_cache_lock);
9431 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
9432 block_group = rb_entry(n, struct btrfs_block_group_cache,
9433 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04009434 rb_erase(&block_group->cache_node,
9435 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009436 RB_CLEAR_NODE(&block_group->cache_node);
Yan Zhengd899e052008-10-30 14:25:28 -04009437 spin_unlock(&info->block_group_cache_lock);
9438
Josef Bacik80eb2342008-10-29 14:49:05 -04009439 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04009440 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04009441 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05009442
Josef Bacik817d52f2009-07-13 21:29:25 -04009443 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04009444 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04009445
Josef Bacik3c148742011-02-02 15:53:47 +00009446 /*
9447 * We haven't cached this block group, which means we could
9448 * possibly have excluded extents on this block group.
9449 */
Josef Bacik36cce922013-08-05 11:15:21 -04009450 if (block_group->cached == BTRFS_CACHE_NO ||
9451 block_group->cached == BTRFS_CACHE_ERROR)
Josef Bacik3c148742011-02-02 15:53:47 +00009452 free_excluded_extents(info->extent_root, block_group);
9453
Josef Bacik817d52f2009-07-13 21:29:25 -04009454 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00009455 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04009456
9457 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009458 }
9459 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04009460
9461 /* now that all the block groups are freed, go through and
9462 * free all the space_info structs. This is only called during
9463 * the final stages of unmount, and so we know nobody is
9464 * using them. We call synchronize_rcu() once before we start,
9465 * just to be on the safe side.
9466 */
9467 synchronize_rcu();
9468
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04009469 release_global_block_rsv(info);
9470
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309471 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009472 int i;
9473
Chris Mason4184ea72009-03-10 12:39:20 -04009474 space_info = list_entry(info->space_info.next,
9475 struct btrfs_space_info,
9476 list);
David Sterbab069e0c2013-02-08 21:28:17 +00009477 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309478 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00009479 space_info->bytes_reserved > 0 ||
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309480 space_info->bytes_may_use > 0)) {
David Sterbab069e0c2013-02-08 21:28:17 +00009481 dump_space_info(space_info, 0, 0);
9482 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009483 }
Chris Mason4184ea72009-03-10 12:39:20 -04009484 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009485 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
9486 struct kobject *kobj;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009487 kobj = space_info->block_group_kobjs[i];
9488 space_info->block_group_kobjs[i] = NULL;
9489 if (kobj) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009490 kobject_del(kobj);
9491 kobject_put(kobj);
9492 }
9493 }
9494 kobject_del(&space_info->kobj);
9495 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04009496 }
Zheng Yan1a40e232008-09-26 10:09:34 -04009497 return 0;
9498}
9499
Yan, Zhengb742bb822010-05-16 10:46:24 -04009500static void __link_block_group(struct btrfs_space_info *space_info,
9501 struct btrfs_block_group_cache *cache)
9502{
9503 int index = get_block_group_index(cache);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009504 bool first = false;
Yan, Zhengb742bb822010-05-16 10:46:24 -04009505
9506 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04009507 if (list_empty(&space_info->block_groups[index]))
9508 first = true;
9509 list_add_tail(&cache->list, &space_info->block_groups[index]);
9510 up_write(&space_info->groups_sem);
9511
9512 if (first) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009513 struct raid_kobject *rkobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009514 int ret;
9515
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009516 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
9517 if (!rkobj)
9518 goto out_err;
9519 rkobj->raid_type = index;
9520 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
9521 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
9522 "%s", get_raid_name(index));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009523 if (ret) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009524 kobject_put(&rkobj->kobj);
9525 goto out_err;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009526 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009527 space_info->block_group_kobjs[index] = &rkobj->kobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009528 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009529
9530 return;
9531out_err:
9532 pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n");
Yan, Zhengb742bb822010-05-16 10:46:24 -04009533}
9534
Miao Xie920e4a52014-01-15 20:00:55 +08009535static struct btrfs_block_group_cache *
9536btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
9537{
9538 struct btrfs_block_group_cache *cache;
9539
9540 cache = kzalloc(sizeof(*cache), GFP_NOFS);
9541 if (!cache)
9542 return NULL;
9543
9544 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
9545 GFP_NOFS);
9546 if (!cache->free_space_ctl) {
9547 kfree(cache);
9548 return NULL;
9549 }
9550
9551 cache->key.objectid = start;
9552 cache->key.offset = size;
9553 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
9554
9555 cache->sectorsize = root->sectorsize;
9556 cache->fs_info = root->fs_info;
9557 cache->full_stripe_len = btrfs_full_stripe_len(root,
9558 &root->fs_info->mapping_tree,
9559 start);
9560 atomic_set(&cache->count, 1);
9561 spin_lock_init(&cache->lock);
Miao Xiee570fd22014-06-19 10:42:50 +08009562 init_rwsem(&cache->data_rwsem);
Miao Xie920e4a52014-01-15 20:00:55 +08009563 INIT_LIST_HEAD(&cache->list);
9564 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -04009565 INIT_LIST_HEAD(&cache->bg_list);
Josef Bacik633c0aa2014-10-31 09:49:34 -04009566 INIT_LIST_HEAD(&cache->ro_list);
Josef Bacikce93ec52014-11-17 15:45:48 -05009567 INIT_LIST_HEAD(&cache->dirty_list);
Chris Masonc9dc4c62015-04-04 17:14:42 -07009568 INIT_LIST_HEAD(&cache->io_list);
Miao Xie920e4a52014-01-15 20:00:55 +08009569 btrfs_init_free_space_ctl(cache);
Filipe Manana04216822014-11-27 21:14:15 +00009570 atomic_set(&cache->trimming, 0);
Miao Xie920e4a52014-01-15 20:00:55 +08009571
9572 return cache;
9573}
9574
Chris Mason9078a3e2007-04-26 16:46:15 -04009575int btrfs_read_block_groups(struct btrfs_root *root)
9576{
9577 struct btrfs_path *path;
9578 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04009579 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04009580 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04009581 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04009582 struct btrfs_key key;
9583 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04009584 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04009585 int need_clear = 0;
9586 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04009587
Chris Masonbe744172007-05-06 10:15:01 -04009588 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04009589 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009590 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009591 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Chris Mason9078a3e2007-04-26 16:46:15 -04009592 path = btrfs_alloc_path();
9593 if (!path)
9594 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04009595 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04009596
David Sterba6c417612011-04-13 15:41:04 +02009597 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04009598 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02009599 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04009600 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04009601 if (btrfs_test_opt(root, CLEAR_CACHE))
9602 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04009603
Chris Masond3977122009-01-05 21:25:51 -05009604 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009605 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb822010-05-16 10:46:24 -04009606 if (ret > 0)
9607 break;
Chris Mason0b86a832008-03-24 15:01:56 -04009608 if (ret != 0)
9609 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +08009610
Chris Mason5f39d392007-10-15 16:14:19 -04009611 leaf = path->nodes[0];
9612 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +08009613
9614 cache = btrfs_create_block_group_cache(root, found_key.objectid,
9615 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04009616 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04009617 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009618 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04009619 }
Josef Bacik96303082009-07-13 21:29:25 -04009620
Liu Bocf7c1ef2012-07-06 03:31:34 -06009621 if (need_clear) {
9622 /*
9623 * When we mount with old space cache, we need to
9624 * set BTRFS_DC_CLEAR and set dirty flag.
9625 *
9626 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
9627 * truncate the old free space cache inode and
9628 * setup a new one.
9629 * b) Setting 'dirty flag' makes sure that we flush
9630 * the new space cache info onto disk.
9631 */
Liu Bocf7c1ef2012-07-06 03:31:34 -06009632 if (btrfs_test_opt(root, SPACE_CACHE))
Josef Bacikce93ec52014-11-17 15:45:48 -05009633 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06009634 }
Josef Bacik0af3d002010-06-21 14:48:16 -04009635
Chris Mason5f39d392007-10-15 16:14:19 -04009636 read_extent_buffer(leaf, &cache->item,
9637 btrfs_item_ptr_offset(leaf, path->slots[0]),
9638 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +08009639 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason0b86a832008-03-24 15:01:56 -04009640
Chris Mason9078a3e2007-04-26 16:46:15 -04009641 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02009642 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +08009643
Josef Bacik817d52f2009-07-13 21:29:25 -04009644 /*
Josef Bacik3c148742011-02-02 15:53:47 +00009645 * We need to exclude the super stripes now so that the space
9646 * info has super bytes accounted for, otherwise we'll think
9647 * we have more space than we actually do.
9648 */
Josef Bacik835d9742013-03-19 12:13:25 -04009649 ret = exclude_super_stripes(root, cache);
9650 if (ret) {
9651 /*
9652 * We may have excluded something, so call this just in
9653 * case.
9654 */
9655 free_excluded_extents(root, cache);
Miao Xie920e4a52014-01-15 20:00:55 +08009656 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04009657 goto error;
9658 }
Josef Bacik3c148742011-02-02 15:53:47 +00009659
9660 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04009661 * check for two cases, either we are full, and therefore
9662 * don't need to bother with the caching work since we won't
9663 * find any space, or we are empty, and we can just add all
9664 * the space in and be done with it. This saves us _alot_ of
9665 * time, particularly in the full case.
9666 */
9667 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04009668 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04009669 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04009670 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04009671 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04009672 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04009673 cache->cached = BTRFS_CACHE_FINISHED;
9674 add_new_free_space(cache, root->fs_info,
9675 found_key.objectid,
9676 found_key.objectid +
9677 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04009678 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04009679 }
Chris Mason96b51792007-10-15 16:15:19 -04009680
Josef Bacik8c579fe2013-04-02 12:40:42 -04009681 ret = btrfs_add_block_group_cache(root->fs_info, cache);
9682 if (ret) {
9683 btrfs_remove_free_space_cache(cache);
9684 btrfs_put_block_group(cache);
9685 goto error;
9686 }
9687
Chris Mason6324fbf2008-03-24 15:01:59 -04009688 ret = update_space_info(info, cache->flags, found_key.offset,
9689 btrfs_block_group_used(&cache->item),
9690 &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04009691 if (ret) {
9692 btrfs_remove_free_space_cache(cache);
9693 spin_lock(&info->block_group_cache_lock);
9694 rb_erase(&cache->cache_node,
9695 &info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009696 RB_CLEAR_NODE(&cache->cache_node);
Josef Bacik8c579fe2013-04-02 12:40:42 -04009697 spin_unlock(&info->block_group_cache_lock);
9698 btrfs_put_block_group(cache);
9699 goto error;
9700 }
9701
Chris Mason6324fbf2008-03-24 15:01:59 -04009702 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04009703 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009704 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04009705 spin_unlock(&cache->space_info->lock);
9706
Yan, Zhengb742bb822010-05-16 10:46:24 -04009707 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04009708
Chris Mason75ccf472008-09-30 19:24:06 -04009709 set_avail_alloc_bits(root->fs_info, cache->flags);
Josef Bacik47ab2a62014-09-18 11:20:02 -04009710 if (btrfs_chunk_readonly(root, cache->key.objectid)) {
Zhaolei868f4012015-08-05 16:43:27 +08009711 inc_block_group_ro(cache, 1);
Josef Bacik47ab2a62014-09-18 11:20:02 -04009712 } else if (btrfs_block_group_used(&cache->item) == 0) {
9713 spin_lock(&info->unused_bgs_lock);
9714 /* Should always be true but just in case. */
9715 if (list_empty(&cache->bg_list)) {
9716 btrfs_get_block_group(cache);
9717 list_add_tail(&cache->bg_list,
9718 &info->unused_bgs);
9719 }
9720 spin_unlock(&info->unused_bgs_lock);
9721 }
Chris Mason9078a3e2007-04-26 16:46:15 -04009722 }
Yan, Zhengb742bb822010-05-16 10:46:24 -04009723
9724 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
9725 if (!(get_alloc_profile(root, space_info->flags) &
9726 (BTRFS_BLOCK_GROUP_RAID10 |
9727 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05009728 BTRFS_BLOCK_GROUP_RAID5 |
9729 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb822010-05-16 10:46:24 -04009730 BTRFS_BLOCK_GROUP_DUP)))
9731 continue;
9732 /*
9733 * avoid allocating from un-mirrored block group if there are
9734 * mirrored block groups.
9735 */
chandan1095cc02013-07-16 12:28:56 +05309736 list_for_each_entry(cache,
9737 &space_info->block_groups[BTRFS_RAID_RAID0],
9738 list)
Zhaolei868f4012015-08-05 16:43:27 +08009739 inc_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +05309740 list_for_each_entry(cache,
9741 &space_info->block_groups[BTRFS_RAID_SINGLE],
9742 list)
Zhaolei868f4012015-08-05 16:43:27 +08009743 inc_block_group_ro(cache, 1);
Yan, Zhengb742bb822010-05-16 10:46:24 -04009744 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009745
9746 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04009747 ret = 0;
9748error:
Chris Mason9078a3e2007-04-26 16:46:15 -04009749 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04009750 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04009751}
Chris Mason6324fbf2008-03-24 15:01:59 -04009752
Josef Bacikea658ba2012-09-11 16:57:25 -04009753void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
9754 struct btrfs_root *root)
9755{
9756 struct btrfs_block_group_cache *block_group, *tmp;
9757 struct btrfs_root *extent_root = root->fs_info->extent_root;
9758 struct btrfs_block_group_item item;
9759 struct btrfs_key key;
9760 int ret = 0;
Filipe Mananad9a05402015-10-03 13:13:13 +01009761 bool can_flush_pending_bgs = trans->can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -04009762
Filipe Mananad9a05402015-10-03 13:13:13 +01009763 trans->can_flush_pending_bgs = false;
Josef Bacik47ab2a62014-09-18 11:20:02 -04009764 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
Josef Bacikea658ba2012-09-11 16:57:25 -04009765 if (ret)
Filipe Mananac92f6be2014-11-26 15:28:55 +00009766 goto next;
Josef Bacikea658ba2012-09-11 16:57:25 -04009767
9768 spin_lock(&block_group->lock);
9769 memcpy(&item, &block_group->item, sizeof(item));
9770 memcpy(&key, &block_group->key, sizeof(key));
9771 spin_unlock(&block_group->lock);
9772
9773 ret = btrfs_insert_item(trans, extent_root, &key, &item,
9774 sizeof(item));
9775 if (ret)
9776 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacik6df9a952013-06-27 13:22:46 -04009777 ret = btrfs_finish_chunk_alloc(trans, extent_root,
9778 key.objectid, key.offset);
9779 if (ret)
9780 btrfs_abort_transaction(trans, extent_root, ret);
Filipe Mananac92f6be2014-11-26 15:28:55 +00009781next:
9782 list_del_init(&block_group->bg_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04009783 }
Filipe Mananad9a05402015-10-03 13:13:13 +01009784 trans->can_flush_pending_bgs = can_flush_pending_bgs;
Josef Bacikea658ba2012-09-11 16:57:25 -04009785}
9786
Chris Mason6324fbf2008-03-24 15:01:59 -04009787int btrfs_make_block_group(struct btrfs_trans_handle *trans,
9788 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04009789 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04009790 u64 size)
9791{
9792 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04009793 struct btrfs_root *extent_root;
9794 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04009795
9796 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04009797
Miao Xie995946d2014-04-02 19:51:06 +08009798 btrfs_set_log_full_commit(root->fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -04009799
Miao Xie920e4a52014-01-15 20:00:55 +08009800 cache = btrfs_create_block_group_cache(root, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -04009801 if (!cache)
9802 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08009803
Chris Mason6324fbf2008-03-24 15:01:59 -04009804 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04009805 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
Chris Mason6324fbf2008-03-24 15:01:59 -04009806 btrfs_set_block_group_flags(&cache->item, type);
9807
Miao Xie920e4a52014-01-15 20:00:55 +08009808 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -04009809 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04009810 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik835d9742013-03-19 12:13:25 -04009811 ret = exclude_super_stripes(root, cache);
9812 if (ret) {
9813 /*
9814 * We may have excluded something, so call this just in
9815 * case.
9816 */
9817 free_excluded_extents(root, cache);
Miao Xie920e4a52014-01-15 20:00:55 +08009818 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04009819 return ret;
9820 }
Josef Bacik96303082009-07-13 21:29:25 -04009821
Josef Bacik817d52f2009-07-13 21:29:25 -04009822 add_new_free_space(cache, root->fs_info, chunk_offset,
9823 chunk_offset + size);
9824
Yan Zheng11833d62009-09-11 16:11:19 -04009825 free_excluded_extents(root, cache);
9826
Josef Bacikd0bd4562015-09-23 14:54:14 -04009827#ifdef CONFIG_BTRFS_DEBUG
9828 if (btrfs_should_fragment_free_space(root, cache)) {
9829 u64 new_bytes_used = size - bytes_used;
9830
9831 bytes_used += new_bytes_used >> 1;
9832 fragment_free_space(root, cache);
9833 }
9834#endif
Filipe Manana2e6e5182015-05-12 00:28:11 +01009835 /*
9836 * Call to ensure the corresponding space_info object is created and
9837 * assigned to our block group, but don't update its counters just yet.
9838 * We want our bg to be added to the rbtree with its ->space_info set.
9839 */
9840 ret = update_space_info(root->fs_info, cache->flags, 0, 0,
9841 &cache->space_info);
9842 if (ret) {
9843 btrfs_remove_free_space_cache(cache);
9844 btrfs_put_block_group(cache);
9845 return ret;
9846 }
9847
Josef Bacik8c579fe2013-04-02 12:40:42 -04009848 ret = btrfs_add_block_group_cache(root->fs_info, cache);
9849 if (ret) {
9850 btrfs_remove_free_space_cache(cache);
9851 btrfs_put_block_group(cache);
9852 return ret;
9853 }
9854
Filipe Manana2e6e5182015-05-12 00:28:11 +01009855 /*
9856 * Now that our block group has its ->space_info set and is inserted in
9857 * the rbtree, update the space info's counters.
9858 */
Chris Mason6324fbf2008-03-24 15:01:59 -04009859 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
9860 &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04009861 if (ret) {
9862 btrfs_remove_free_space_cache(cache);
9863 spin_lock(&root->fs_info->block_group_cache_lock);
9864 rb_erase(&cache->cache_node,
9865 &root->fs_info->block_group_cache_tree);
Filipe Manana01eacb22014-12-04 18:38:30 +00009866 RB_CLEAR_NODE(&cache->cache_node);
Josef Bacik8c579fe2013-04-02 12:40:42 -04009867 spin_unlock(&root->fs_info->block_group_cache_lock);
9868 btrfs_put_block_group(cache);
9869 return ret;
9870 }
Li Zefanc7c144d2011-12-07 10:39:22 +08009871 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04009872
9873 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009874 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04009875 spin_unlock(&cache->space_info->lock);
9876
Yan, Zhengb742bb822010-05-16 10:46:24 -04009877 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04009878
Josef Bacik47ab2a62014-09-18 11:20:02 -04009879 list_add_tail(&cache->bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04009880
Chris Masond18a2c42008-04-04 15:40:00 -04009881 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04009882
Chris Mason6324fbf2008-03-24 15:01:59 -04009883 return 0;
9884}
Zheng Yan1a40e232008-09-26 10:09:34 -04009885
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02009886static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
9887{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03009888 u64 extra_flags = chunk_to_extended(flags) &
9889 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02009890
Miao Xiede98ced2013-01-29 10:13:12 +00009891 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02009892 if (flags & BTRFS_BLOCK_GROUP_DATA)
9893 fs_info->avail_data_alloc_bits &= ~extra_flags;
9894 if (flags & BTRFS_BLOCK_GROUP_METADATA)
9895 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
9896 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
9897 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00009898 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02009899}
9900
Zheng Yan1a40e232008-09-26 10:09:34 -04009901int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
Filipe Manana04216822014-11-27 21:14:15 +00009902 struct btrfs_root *root, u64 group_start,
9903 struct extent_map *em)
Zheng Yan1a40e232008-09-26 10:09:34 -04009904{
9905 struct btrfs_path *path;
9906 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04009907 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04009908 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04009909 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04009910 struct inode *inode;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009911 struct kobject *kobj = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04009912 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02009913 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04009914 int factor;
Filipe Manana4f69cb92014-11-26 15:28:51 +00009915 struct btrfs_caching_control *caching_ctl = NULL;
Filipe Manana04216822014-11-27 21:14:15 +00009916 bool remove_em;
Zheng Yan1a40e232008-09-26 10:09:34 -04009917
Zheng Yan1a40e232008-09-26 10:09:34 -04009918 root = root->fs_info->extent_root;
9919
9920 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
9921 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05009922 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04009923
liubo9f7c43c2011-03-07 02:13:33 +00009924 /*
9925 * Free the reserved super bytes from this block group before
9926 * remove it.
9927 */
9928 free_excluded_extents(root, block_group);
9929
Zheng Yan1a40e232008-09-26 10:09:34 -04009930 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02009931 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04009932 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
9933 BTRFS_BLOCK_GROUP_RAID1 |
9934 BTRFS_BLOCK_GROUP_RAID10))
9935 factor = 2;
9936 else
9937 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04009938
Chris Mason44fb5512009-06-04 15:34:51 -04009939 /* make sure this block group isn't part of an allocation cluster */
9940 cluster = &root->fs_info->data_alloc_cluster;
9941 spin_lock(&cluster->refill_lock);
9942 btrfs_return_cluster_to_free_space(block_group, cluster);
9943 spin_unlock(&cluster->refill_lock);
9944
9945 /*
9946 * make sure this block group isn't part of a metadata
9947 * allocation cluster
9948 */
9949 cluster = &root->fs_info->meta_alloc_cluster;
9950 spin_lock(&cluster->refill_lock);
9951 btrfs_return_cluster_to_free_space(block_group, cluster);
9952 spin_unlock(&cluster->refill_lock);
9953
Zheng Yan1a40e232008-09-26 10:09:34 -04009954 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009955 if (!path) {
9956 ret = -ENOMEM;
9957 goto out;
9958 }
Zheng Yan1a40e232008-09-26 10:09:34 -04009959
Chris Mason1bbc6212015-04-06 12:46:08 -07009960 /*
9961 * get the inode first so any iput calls done for the io_list
9962 * aren't the final iput (no unlinks allowed now)
9963 */
Ilya Dryomov10b2f342011-10-02 13:56:53 +03009964 inode = lookup_free_space_inode(tree_root, block_group, path);
Chris Mason1bbc6212015-04-06 12:46:08 -07009965
9966 mutex_lock(&trans->transaction->cache_write_mutex);
9967 /*
9968 * make sure our free spache cache IO is done before remove the
9969 * free space inode
9970 */
9971 spin_lock(&trans->transaction->dirty_bgs_lock);
9972 if (!list_empty(&block_group->io_list)) {
9973 list_del_init(&block_group->io_list);
9974
9975 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode);
9976
9977 spin_unlock(&trans->transaction->dirty_bgs_lock);
9978 btrfs_wait_cache_io(root, trans, block_group,
9979 &block_group->io_ctl, path,
9980 block_group->key.objectid);
9981 btrfs_put_block_group(block_group);
9982 spin_lock(&trans->transaction->dirty_bgs_lock);
9983 }
9984
9985 if (!list_empty(&block_group->dirty_list)) {
9986 list_del_init(&block_group->dirty_list);
9987 btrfs_put_block_group(block_group);
9988 }
9989 spin_unlock(&trans->transaction->dirty_bgs_lock);
9990 mutex_unlock(&trans->transaction->cache_write_mutex);
9991
Josef Bacik0af3d002010-06-21 14:48:16 -04009992 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00009993 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009994 if (ret) {
9995 btrfs_add_delayed_iput(inode);
9996 goto out;
9997 }
Josef Bacik0af3d002010-06-21 14:48:16 -04009998 clear_nlink(inode);
9999 /* One for the block groups ref */
10000 spin_lock(&block_group->lock);
10001 if (block_group->iref) {
10002 block_group->iref = 0;
10003 block_group->inode = NULL;
10004 spin_unlock(&block_group->lock);
10005 iput(inode);
10006 } else {
10007 spin_unlock(&block_group->lock);
10008 }
10009 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -040010010 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -040010011 }
10012
10013 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
10014 key.offset = block_group->key.objectid;
10015 key.type = 0;
10016
10017 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
10018 if (ret < 0)
10019 goto out;
10020 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +020010021 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010022 if (ret == 0) {
10023 ret = btrfs_del_item(trans, tree_root, path);
10024 if (ret)
10025 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +020010026 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040010027 }
10028
Yan Zheng3dfdb932009-01-21 10:49:16 -050010029 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -040010030 rb_erase(&block_group->cache_node,
10031 &root->fs_info->block_group_cache_tree);
Filipe Manana292cbd52014-11-26 15:28:50 +000010032 RB_CLEAR_NODE(&block_group->cache_node);
Liu Boa1897fd2012-12-27 09:01:23 +000010033
10034 if (root->fs_info->first_logical_byte == block_group->key.objectid)
10035 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -050010036 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010037
Josef Bacik80eb2342008-10-29 14:49:05 -040010038 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -040010039 /*
10040 * we must use list_del_init so people can check to see if they
10041 * are still on the list after taking the semaphore
10042 */
10043 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010044 if (list_empty(&block_group->space_info->block_groups[index])) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010045 kobj = block_group->space_info->block_group_kobjs[index];
10046 block_group->space_info->block_group_kobjs[index] = NULL;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +020010047 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040010048 }
Josef Bacik80eb2342008-10-29 14:49:05 -040010049 up_write(&block_group->space_info->groups_sem);
Jeff Mahoneyc1895442014-05-27 12:59:57 -040010050 if (kobj) {
10051 kobject_del(kobj);
10052 kobject_put(kobj);
10053 }
Zheng Yan1a40e232008-09-26 10:09:34 -040010054
Filipe Manana4f69cb92014-11-26 15:28:51 +000010055 if (block_group->has_caching_ctl)
10056 caching_ctl = get_caching_control(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -040010057 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -040010058 wait_block_group_cache_done(block_group);
Filipe Manana4f69cb92014-11-26 15:28:51 +000010059 if (block_group->has_caching_ctl) {
10060 down_write(&root->fs_info->commit_root_sem);
10061 if (!caching_ctl) {
10062 struct btrfs_caching_control *ctl;
10063
10064 list_for_each_entry(ctl,
10065 &root->fs_info->caching_block_groups, list)
10066 if (ctl->block_group == block_group) {
10067 caching_ctl = ctl;
10068 atomic_inc(&caching_ctl->count);
10069 break;
10070 }
10071 }
10072 if (caching_ctl)
10073 list_del_init(&caching_ctl->list);
10074 up_write(&root->fs_info->commit_root_sem);
10075 if (caching_ctl) {
10076 /* Once for the caching bgs list and once for us. */
10077 put_caching_control(caching_ctl);
10078 put_caching_control(caching_ctl);
10079 }
10080 }
Josef Bacik817d52f2009-07-13 21:29:25 -040010081
Josef Bacikce93ec52014-11-17 15:45:48 -050010082 spin_lock(&trans->transaction->dirty_bgs_lock);
10083 if (!list_empty(&block_group->dirty_list)) {
Chris Mason1bbc6212015-04-06 12:46:08 -070010084 WARN_ON(1);
10085 }
10086 if (!list_empty(&block_group->io_list)) {
10087 WARN_ON(1);
Josef Bacikce93ec52014-11-17 15:45:48 -050010088 }
10089 spin_unlock(&trans->transaction->dirty_bgs_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -040010090 btrfs_remove_free_space_cache(block_group);
10091
Yan Zhengc146afa2008-11-12 14:34:12 -050010092 spin_lock(&block_group->space_info->lock);
Filipe Manana75c68e92015-01-16 13:24:40 +000010093 list_del_init(&block_group->ro_list);
Zhao Lei18d018a2015-02-24 20:07:44 +080010094
10095 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
10096 WARN_ON(block_group->space_info->total_bytes
10097 < block_group->key.offset);
10098 WARN_ON(block_group->space_info->bytes_readonly
10099 < block_group->key.offset);
10100 WARN_ON(block_group->space_info->disk_total
10101 < block_group->key.offset * factor);
10102 }
Yan Zhengc146afa2008-11-12 14:34:12 -050010103 block_group->space_info->total_bytes -= block_group->key.offset;
10104 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -040010105 block_group->space_info->disk_total -= block_group->key.offset * factor;
Zhao Lei18d018a2015-02-24 20:07:44 +080010106
Yan Zhengc146afa2008-11-12 14:34:12 -050010107 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -040010108
Josef Bacik0af3d002010-06-21 14:48:16 -040010109 memcpy(&key, &block_group->key, sizeof(key));
10110
Filipe Manana04216822014-11-27 21:14:15 +000010111 lock_chunks(root);
Filipe Manana495e64f2014-12-02 18:07:30 +000010112 if (!list_empty(&em->list)) {
10113 /* We're in the transaction->pending_chunks list. */
10114 free_extent_map(em);
10115 }
Filipe Manana04216822014-11-27 21:14:15 +000010116 spin_lock(&block_group->lock);
10117 block_group->removed = 1;
10118 /*
10119 * At this point trimming can't start on this block group, because we
10120 * removed the block group from the tree fs_info->block_group_cache_tree
10121 * so no one can't find it anymore and even if someone already got this
10122 * block group before we removed it from the rbtree, they have already
10123 * incremented block_group->trimming - if they didn't, they won't find
10124 * any free space entries because we already removed them all when we
10125 * called btrfs_remove_free_space_cache().
10126 *
10127 * And we must not remove the extent map from the fs_info->mapping_tree
10128 * to prevent the same logical address range and physical device space
10129 * ranges from being reused for a new block group. This is because our
10130 * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is
10131 * completely transactionless, so while it is trimming a range the
10132 * currently running transaction might finish and a new one start,
10133 * allowing for new block groups to be created that can reuse the same
10134 * physical device locations unless we take this special care.
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010135 *
10136 * There may also be an implicit trim operation if the file system
10137 * is mounted with -odiscard. The same protections must remain
10138 * in place until the extents have been discarded completely when
10139 * the transaction commit has completed.
Filipe Manana04216822014-11-27 21:14:15 +000010140 */
10141 remove_em = (atomic_read(&block_group->trimming) == 0);
10142 /*
10143 * Make sure a trimmer task always sees the em in the pinned_chunks list
10144 * if it sees block_group->removed == 1 (needs to lock block_group->lock
10145 * before checking block_group->removed).
10146 */
10147 if (!remove_em) {
10148 /*
10149 * Our em might be in trans->transaction->pending_chunks which
10150 * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks),
10151 * and so is the fs_info->pinned_chunks list.
10152 *
10153 * So at this point we must be holding the chunk_mutex to avoid
10154 * any races with chunk allocation (more specifically at
10155 * volumes.c:contains_pending_extent()), to ensure it always
10156 * sees the em, either in the pending_chunks list or in the
10157 * pinned_chunks list.
10158 */
10159 list_move_tail(&em->list, &root->fs_info->pinned_chunks);
10160 }
10161 spin_unlock(&block_group->lock);
Filipe Manana04216822014-11-27 21:14:15 +000010162
10163 if (remove_em) {
10164 struct extent_map_tree *em_tree;
10165
10166 em_tree = &root->fs_info->mapping_tree.map_tree;
10167 write_lock(&em_tree->lock);
Filipe Manana8dbcd102014-12-02 18:07:49 +000010168 /*
10169 * The em might be in the pending_chunks list, so make sure the
10170 * chunk mutex is locked, since remove_extent_mapping() will
10171 * delete us from that list.
10172 */
Filipe Manana04216822014-11-27 21:14:15 +000010173 remove_extent_mapping(em_tree, em);
10174 write_unlock(&em_tree->lock);
10175 /* once for the tree */
10176 free_extent_map(em);
10177 }
10178
Filipe Manana8dbcd102014-12-02 18:07:49 +000010179 unlock_chunks(root);
10180
Chris Masonfa9c0d792009-04-03 09:47:43 -040010181 btrfs_put_block_group(block_group);
10182 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -040010183
10184 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
10185 if (ret > 0)
10186 ret = -EIO;
10187 if (ret < 0)
10188 goto out;
10189
10190 ret = btrfs_del_item(trans, root, path);
10191out:
10192 btrfs_free_path(path);
10193 return ret;
10194}
liuboacce9522011-01-06 19:30:25 +080010195
Josef Bacik47ab2a62014-09-18 11:20:02 -040010196/*
10197 * Process the unused_bgs list and remove any that don't have any allocated
10198 * space inside of them.
10199 */
10200void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10201{
10202 struct btrfs_block_group_cache *block_group;
10203 struct btrfs_space_info *space_info;
10204 struct btrfs_root *root = fs_info->extent_root;
10205 struct btrfs_trans_handle *trans;
10206 int ret = 0;
10207
10208 if (!fs_info->open)
10209 return;
10210
10211 spin_lock(&fs_info->unused_bgs_lock);
10212 while (!list_empty(&fs_info->unused_bgs)) {
10213 u64 start, end;
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010214 int trimming;
Josef Bacik47ab2a62014-09-18 11:20:02 -040010215
10216 block_group = list_first_entry(&fs_info->unused_bgs,
10217 struct btrfs_block_group_cache,
10218 bg_list);
10219 space_info = block_group->space_info;
10220 list_del_init(&block_group->bg_list);
10221 if (ret || btrfs_mixed_space_info(space_info)) {
10222 btrfs_put_block_group(block_group);
10223 continue;
10224 }
10225 spin_unlock(&fs_info->unused_bgs_lock);
10226
Filipe Manana67c5e7d2015-06-11 00:58:53 +010010227 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
10228
Josef Bacik47ab2a62014-09-18 11:20:02 -040010229 /* Don't want to race with allocators so take the groups_sem */
10230 down_write(&space_info->groups_sem);
10231 spin_lock(&block_group->lock);
10232 if (block_group->reserved ||
10233 btrfs_block_group_used(&block_group->item) ||
10234 block_group->ro) {
10235 /*
10236 * We want to bail if we made new allocations or have
10237 * outstanding allocations in this block group. We do
10238 * the ro check in case balance is currently acting on
10239 * this block group.
10240 */
10241 spin_unlock(&block_group->lock);
10242 up_write(&space_info->groups_sem);
10243 goto next;
10244 }
10245 spin_unlock(&block_group->lock);
10246
10247 /* We don't want to force the issue, only flip if it's ok. */
Zhaolei868f4012015-08-05 16:43:27 +080010248 ret = inc_block_group_ro(block_group, 0);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010249 up_write(&space_info->groups_sem);
10250 if (ret < 0) {
10251 ret = 0;
10252 goto next;
10253 }
10254
10255 /*
10256 * Want to do this before we do anything else so we can recover
10257 * properly if we fail to join the transaction.
10258 */
Forrest Liu3d84be72015-02-11 14:24:12 +080010259 /* 1 for btrfs_orphan_reserve_metadata() */
10260 trans = btrfs_start_transaction(root, 1);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010261 if (IS_ERR(trans)) {
Zhaolei868f4012015-08-05 16:43:27 +080010262 btrfs_dec_block_group_ro(root, block_group);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010263 ret = PTR_ERR(trans);
10264 goto next;
10265 }
10266
10267 /*
10268 * We could have pending pinned extents for this block group,
10269 * just delete them, we don't care about them anymore.
10270 */
10271 start = block_group->key.objectid;
10272 end = start + block_group->key.offset - 1;
Filipe Mananad4b450c2015-01-29 19:18:25 +000010273 /*
10274 * Hold the unused_bg_unpin_mutex lock to avoid racing with
10275 * btrfs_finish_extent_commit(). If we are at transaction N,
10276 * another task might be running finish_extent_commit() for the
10277 * previous transaction N - 1, and have seen a range belonging
10278 * to the block group in freed_extents[] before we were able to
10279 * clear the whole block group range from freed_extents[]. This
10280 * means that task can lookup for the block group after we
10281 * unpinned it from freed_extents[] and removed it, leading to
10282 * a BUG_ON() at btrfs_unpin_extent_range().
10283 */
10284 mutex_lock(&fs_info->unused_bg_unpin_mutex);
Filipe Manana758eb512014-11-03 14:08:39 +000010285 ret = clear_extent_bits(&fs_info->freed_extents[0], start, end,
Josef Bacik47ab2a62014-09-18 11:20:02 -040010286 EXTENT_DIRTY, GFP_NOFS);
Filipe Manana758eb512014-11-03 14:08:39 +000010287 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010288 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Zhaolei868f4012015-08-05 16:43:27 +080010289 btrfs_dec_block_group_ro(root, block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010290 goto end_trans;
10291 }
10292 ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
Josef Bacik47ab2a62014-09-18 11:20:02 -040010293 EXTENT_DIRTY, GFP_NOFS);
Filipe Manana758eb512014-11-03 14:08:39 +000010294 if (ret) {
Filipe Mananad4b450c2015-01-29 19:18:25 +000010295 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Zhaolei868f4012015-08-05 16:43:27 +080010296 btrfs_dec_block_group_ro(root, block_group);
Filipe Manana758eb512014-11-03 14:08:39 +000010297 goto end_trans;
10298 }
Filipe Mananad4b450c2015-01-29 19:18:25 +000010299 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010300
10301 /* Reset pinned so btrfs_put_block_group doesn't complain */
Zhao Leic30666d2015-02-25 14:17:20 +080010302 spin_lock(&space_info->lock);
10303 spin_lock(&block_group->lock);
10304
10305 space_info->bytes_pinned -= block_group->pinned;
10306 space_info->bytes_readonly += block_group->pinned;
10307 percpu_counter_add(&space_info->total_bytes_pinned,
10308 -block_group->pinned);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010309 block_group->pinned = 0;
10310
Zhao Leic30666d2015-02-25 14:17:20 +080010311 spin_unlock(&block_group->lock);
10312 spin_unlock(&space_info->lock);
10313
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010314 /* DISCARD can flip during remount */
10315 trimming = btrfs_test_opt(root, DISCARD);
10316
10317 /* Implicit trim during transaction commit. */
10318 if (trimming)
10319 btrfs_get_block_group_trimming(block_group);
10320
Josef Bacik47ab2a62014-09-18 11:20:02 -040010321 /*
10322 * Btrfs_remove_chunk will abort the transaction if things go
10323 * horribly wrong.
10324 */
10325 ret = btrfs_remove_chunk(trans, root,
10326 block_group->key.objectid);
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040010327
10328 if (ret) {
10329 if (trimming)
10330 btrfs_put_block_group_trimming(block_group);
10331 goto end_trans;
10332 }
10333
10334 /*
10335 * If we're not mounted with -odiscard, we can just forget
10336 * about this block group. Otherwise we'll need to wait
10337 * until transaction commit to do the actual discard.
10338 */
10339 if (trimming) {
10340 WARN_ON(!list_empty(&block_group->bg_list));
10341 spin_lock(&trans->transaction->deleted_bgs_lock);
10342 list_move(&block_group->bg_list,
10343 &trans->transaction->deleted_bgs);
10344 spin_unlock(&trans->transaction->deleted_bgs_lock);
10345 btrfs_get_block_group(block_group);
10346 }
Filipe Manana758eb512014-11-03 14:08:39 +000010347end_trans:
Josef Bacik47ab2a62014-09-18 11:20:02 -040010348 btrfs_end_transaction(trans, root);
10349next:
Filipe Manana67c5e7d2015-06-11 00:58:53 +010010350 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -040010351 btrfs_put_block_group(block_group);
10352 spin_lock(&fs_info->unused_bgs_lock);
10353 }
10354 spin_unlock(&fs_info->unused_bgs_lock);
10355}
10356
liuboc59021f2011-03-07 02:13:14 +000010357int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
10358{
10359 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +000010360 struct btrfs_super_block *disk_super;
10361 u64 features;
10362 u64 flags;
10363 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +000010364 int ret;
10365
David Sterba6c417612011-04-13 15:41:04 +020010366 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +000010367 if (!btrfs_super_root(disk_super))
10368 return 1;
liuboc59021f2011-03-07 02:13:14 +000010369
liubo1aba86d2011-04-08 08:44:37 +000010370 features = btrfs_super_incompat_flags(disk_super);
10371 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
10372 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +000010373
liubo1aba86d2011-04-08 08:44:37 +000010374 flags = BTRFS_BLOCK_GROUP_SYSTEM;
10375 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +000010376 if (ret)
liubo1aba86d2011-04-08 08:44:37 +000010377 goto out;
liuboc59021f2011-03-07 02:13:14 +000010378
liubo1aba86d2011-04-08 08:44:37 +000010379 if (mixed) {
10380 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
10381 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
10382 } else {
10383 flags = BTRFS_BLOCK_GROUP_METADATA;
10384 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
10385 if (ret)
10386 goto out;
10387
10388 flags = BTRFS_BLOCK_GROUP_DATA;
10389 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
10390 }
10391out:
liuboc59021f2011-03-07 02:13:14 +000010392 return ret;
10393}
10394
liuboacce9522011-01-06 19:30:25 +080010395int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
10396{
Filipe Manana678886b2014-12-07 21:31:47 +000010397 return unpin_extent_range(root, start, end, false);
liuboacce9522011-01-06 19:30:25 +080010398}
10399
Jeff Mahoney499f3772015-06-15 09:41:17 -040010400/*
10401 * It used to be that old block groups would be left around forever.
10402 * Iterating over them would be enough to trim unused space. Since we
10403 * now automatically remove them, we also need to iterate over unallocated
10404 * space.
10405 *
10406 * We don't want a transaction for this since the discard may take a
10407 * substantial amount of time. We don't require that a transaction be
10408 * running, but we do need to take a running transaction into account
10409 * to ensure that we're not discarding chunks that were released in
10410 * the current transaction.
10411 *
10412 * Holding the chunks lock will prevent other threads from allocating
10413 * or releasing chunks, but it won't prevent a running transaction
10414 * from committing and releasing the memory that the pending chunks
10415 * list head uses. For that, we need to take a reference to the
10416 * transaction.
10417 */
10418static int btrfs_trim_free_extents(struct btrfs_device *device,
10419 u64 minlen, u64 *trimmed)
10420{
10421 u64 start = 0, len = 0;
10422 int ret;
10423
10424 *trimmed = 0;
10425
10426 /* Not writeable = nothing to do. */
10427 if (!device->writeable)
10428 return 0;
10429
10430 /* No free space = nothing to do. */
10431 if (device->total_bytes <= device->bytes_used)
10432 return 0;
10433
10434 ret = 0;
10435
10436 while (1) {
10437 struct btrfs_fs_info *fs_info = device->dev_root->fs_info;
10438 struct btrfs_transaction *trans;
10439 u64 bytes;
10440
10441 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
10442 if (ret)
10443 return ret;
10444
10445 down_read(&fs_info->commit_root_sem);
10446
10447 spin_lock(&fs_info->trans_lock);
10448 trans = fs_info->running_transaction;
10449 if (trans)
10450 atomic_inc(&trans->use_count);
10451 spin_unlock(&fs_info->trans_lock);
10452
10453 ret = find_free_dev_extent_start(trans, device, minlen, start,
10454 &start, &len);
10455 if (trans)
10456 btrfs_put_transaction(trans);
10457
10458 if (ret) {
10459 up_read(&fs_info->commit_root_sem);
10460 mutex_unlock(&fs_info->chunk_mutex);
10461 if (ret == -ENOSPC)
10462 ret = 0;
10463 break;
10464 }
10465
10466 ret = btrfs_issue_discard(device->bdev, start, len, &bytes);
10467 up_read(&fs_info->commit_root_sem);
10468 mutex_unlock(&fs_info->chunk_mutex);
10469
10470 if (ret)
10471 break;
10472
10473 start += len;
10474 *trimmed += bytes;
10475
10476 if (fatal_signal_pending(current)) {
10477 ret = -ERESTARTSYS;
10478 break;
10479 }
10480
10481 cond_resched();
10482 }
10483
10484 return ret;
10485}
10486
Li Dongyangf7039b12011-03-24 10:24:28 +000010487int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
10488{
10489 struct btrfs_fs_info *fs_info = root->fs_info;
10490 struct btrfs_block_group_cache *cache = NULL;
Jeff Mahoney499f3772015-06-15 09:41:17 -040010491 struct btrfs_device *device;
10492 struct list_head *devices;
Li Dongyangf7039b12011-03-24 10:24:28 +000010493 u64 group_trimmed;
10494 u64 start;
10495 u64 end;
10496 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +080010497 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +000010498 int ret = 0;
10499
Liu Bo2cac13e2012-02-09 18:17:41 +080010500 /*
10501 * try to trim all FS space, our block group may start from non-zero.
10502 */
10503 if (range->len == total_bytes)
10504 cache = btrfs_lookup_first_block_group(fs_info, range->start);
10505 else
10506 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +000010507
10508 while (cache) {
10509 if (cache->key.objectid >= (range->start + range->len)) {
10510 btrfs_put_block_group(cache);
10511 break;
10512 }
10513
10514 start = max(range->start, cache->key.objectid);
10515 end = min(range->start + range->len,
10516 cache->key.objectid + cache->key.offset);
10517
10518 if (end - start >= range->minlen) {
10519 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +000010520 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -040010521 if (ret) {
10522 btrfs_put_block_group(cache);
10523 break;
10524 }
10525 ret = wait_block_group_cache_done(cache);
10526 if (ret) {
10527 btrfs_put_block_group(cache);
10528 break;
10529 }
Li Dongyangf7039b12011-03-24 10:24:28 +000010530 }
10531 ret = btrfs_trim_block_group(cache,
10532 &group_trimmed,
10533 start,
10534 end,
10535 range->minlen);
10536
10537 trimmed += group_trimmed;
10538 if (ret) {
10539 btrfs_put_block_group(cache);
10540 break;
10541 }
10542 }
10543
10544 cache = next_block_group(fs_info->tree_root, cache);
10545 }
10546
Jeff Mahoney499f3772015-06-15 09:41:17 -040010547 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
10548 devices = &root->fs_info->fs_devices->alloc_list;
10549 list_for_each_entry(device, devices, dev_alloc_list) {
10550 ret = btrfs_trim_free_extents(device, range->minlen,
10551 &group_trimmed);
10552 if (ret)
10553 break;
10554
10555 trimmed += group_trimmed;
10556 }
10557 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
10558
Li Dongyangf7039b12011-03-24 10:24:28 +000010559 range->len = trimmed;
10560 return ret;
10561}
Miao Xie8257b2d2014-03-06 13:38:19 +080010562
10563/*
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010564 * btrfs_{start,end}_write_no_snapshoting() are similar to
10565 * mnt_{want,drop}_write(), they are used to prevent some tasks from writing
10566 * data into the page cache through nocow before the subvolume is snapshoted,
10567 * but flush the data into disk after the snapshot creation, or to prevent
10568 * operations while snapshoting is ongoing and that cause the snapshot to be
10569 * inconsistent (writes followed by expanding truncates for example).
Miao Xie8257b2d2014-03-06 13:38:19 +080010570 */
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010571void btrfs_end_write_no_snapshoting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080010572{
10573 percpu_counter_dec(&root->subv_writers->counter);
10574 /*
David Sterbaa83342a2015-02-16 19:36:47 +010010575 * Make sure counter is updated before we wake up waiters.
Miao Xie8257b2d2014-03-06 13:38:19 +080010576 */
10577 smp_mb();
10578 if (waitqueue_active(&root->subv_writers->wait))
10579 wake_up(&root->subv_writers->wait);
10580}
10581
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010582int btrfs_start_write_no_snapshoting(struct btrfs_root *root)
Miao Xie8257b2d2014-03-06 13:38:19 +080010583{
David Sterbaee39b432014-09-30 01:33:33 +020010584 if (atomic_read(&root->will_be_snapshoted))
Miao Xie8257b2d2014-03-06 13:38:19 +080010585 return 0;
10586
10587 percpu_counter_inc(&root->subv_writers->counter);
10588 /*
10589 * Make sure counter is updated before we check for snapshot creation.
10590 */
10591 smp_mb();
David Sterbaee39b432014-09-30 01:33:33 +020010592 if (atomic_read(&root->will_be_snapshoted)) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +000010593 btrfs_end_write_no_snapshoting(root);
Miao Xie8257b2d2014-03-06 13:38:19 +080010594 return 0;
10595 }
10596 return 1;
10597}