blob: 5524434da05935d66bcac69ec0db0e48d93442b0 [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
Liu Boc53d6132012-12-27 09:01:19 +000077static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040078 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040079static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
80 struct btrfs_root *root,
81 u64 bytenr, u64 num_bytes, u64 parent,
82 u64 root_objectid, u64 owner_objectid,
83 u64 owner_offset, int refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -070084 struct btrfs_delayed_extent_op *extra_op,
85 int no_quota);
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);
318 if (cache->cached != BTRFS_CACHE_STARTED) {
319 spin_unlock(&cache->lock);
320 return NULL;
321 }
322
Josef Bacikdde5abe2010-09-16 16:17:03 -0400323 /* We're loading it the fast way, so we don't have a caching_ctl. */
324 if (!cache->caching_ctl) {
325 spin_unlock(&cache->lock);
326 return NULL;
327 }
328
Yan Zheng11833d62009-09-11 16:11:19 -0400329 ctl = cache->caching_ctl;
330 atomic_inc(&ctl->count);
331 spin_unlock(&cache->lock);
332 return ctl;
333}
334
335static void put_caching_control(struct btrfs_caching_control *ctl)
336{
337 if (atomic_dec_and_test(&ctl->count))
338 kfree(ctl);
339}
340
Josef Bacik0f9dd462008-09-23 13:14:11 -0400341/*
342 * this is only called by cache_block_group, since we could have freed extents
343 * we need to check the pinned_extents for any extents that can't be used yet
344 * since their free space will be released as soon as the transaction commits.
345 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400346static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400347 struct btrfs_fs_info *info, u64 start, u64 end)
348{
Josef Bacik817d52f2009-07-13 21:29:25 -0400349 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400350 int ret;
351
352 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400353 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400354 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400355 EXTENT_DIRTY | EXTENT_UPTODATE,
356 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400357 if (ret)
358 break;
359
Yan, Zheng06b23312009-11-26 09:31:11 +0000360 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400361 start = extent_end + 1;
362 } else if (extent_start > start && extent_start < end) {
363 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400364 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500365 ret = btrfs_add_free_space(block_group, start,
366 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100367 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400368 start = extent_end + 1;
369 } else {
370 break;
371 }
372 }
373
374 if (start < end) {
375 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400376 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500377 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100378 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400379 }
380
Josef Bacik817d52f2009-07-13 21:29:25 -0400381 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400382}
383
Qu Wenruod458b052014-02-28 10:46:19 +0800384static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400385{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400386 struct btrfs_block_group_cache *block_group;
387 struct btrfs_fs_info *fs_info;
388 struct btrfs_caching_control *caching_ctl;
389 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400390 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400391 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400392 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400393 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400394 u64 last = 0;
395 u32 nritems;
Josef Bacik36cce922013-08-05 11:15:21 -0400396 int ret = -ENOMEM;
Chris Masonf510cfe2007-10-15 16:14:48 -0400397
Josef Bacikbab39bf2011-06-30 14:42:28 -0400398 caching_ctl = container_of(work, struct btrfs_caching_control, work);
399 block_group = caching_ctl->block_group;
400 fs_info = block_group->fs_info;
401 extent_root = fs_info->extent_root;
402
Chris Masone37c9e62007-05-09 20:13:14 -0400403 path = btrfs_alloc_path();
404 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400405 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400406
Josef Bacik817d52f2009-07-13 21:29:25 -0400407 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400408
Chris Mason5cd57b22008-06-25 16:01:30 -0400409 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400410 * We don't want to deadlock with somebody trying to allocate a new
411 * extent for the extent root while also trying to search the extent
412 * root to add free space. So we skip locking and search the commit
413 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400414 */
415 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400416 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400417 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400418
Yan Zhenge4404d62008-12-12 10:03:26 -0500419 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400420 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400421 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400422again:
Yan Zheng11833d62009-09-11 16:11:19 -0400423 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400424 /* need to make sure the commit_root doesn't disappear */
Josef Bacik9e351cc2014-03-13 15:42:13 -0400425 down_read(&fs_info->commit_root_sem);
Chris Mason013f1b12009-07-31 14:57:55 -0400426
Liu Bo52ee28d2013-07-11 17:51:15 +0800427next:
Yan Zheng11833d62009-09-11 16:11:19 -0400428 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400429 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400430 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500431
Yan Zheng11833d62009-09-11 16:11:19 -0400432 leaf = path->nodes[0];
433 nritems = btrfs_header_nritems(leaf);
434
Chris Masond3977122009-01-05 21:25:51 -0500435 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200436 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400437 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400438 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400439 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400440
Yan Zheng11833d62009-09-11 16:11:19 -0400441 if (path->slots[0] < nritems) {
442 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
443 } else {
444 ret = find_next_key(path, 0, &key);
445 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400446 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400447
Josef Bacikc9ea7b22013-09-19 10:02:11 -0400448 if (need_resched() ||
Josef Bacik9e351cc2014-03-13 15:42:13 -0400449 rwsem_is_contended(&fs_info->commit_root_sem)) {
Josef Bacik589d8ad2011-05-11 17:30:53 -0400450 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400451 btrfs_release_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400452 up_read(&fs_info->commit_root_sem);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400453 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400454 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400455 goto again;
456 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400457
458 ret = btrfs_next_leaf(extent_root, path);
459 if (ret < 0)
460 goto err;
461 if (ret)
462 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400463 leaf = path->nodes[0];
464 nritems = btrfs_header_nritems(leaf);
465 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400466 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400467
Liu Bo52ee28d2013-07-11 17:51:15 +0800468 if (key.objectid < last) {
469 key.objectid = last;
470 key.offset = 0;
471 key.type = BTRFS_EXTENT_ITEM_KEY;
472
473 caching_ctl->progress = last;
474 btrfs_release_path(path);
475 goto next;
476 }
477
Yan Zheng11833d62009-09-11 16:11:19 -0400478 if (key.objectid < block_group->key.objectid) {
479 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400480 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400481 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400482
Chris Masone37c9e62007-05-09 20:13:14 -0400483 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400484 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400485 break;
Yan7d7d6062007-09-14 16:15:28 -0400486
Josef Bacik3173a182013-03-07 14:22:04 -0500487 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
488 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400489 total_found += add_new_free_space(block_group,
490 fs_info, last,
491 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500492 if (key.type == BTRFS_METADATA_ITEM_KEY)
493 last = key.objectid +
494 fs_info->tree_root->leafsize;
495 else
496 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400497
Yan Zheng11833d62009-09-11 16:11:19 -0400498 if (total_found > (1024 * 1024 * 2)) {
499 total_found = 0;
500 wake_up(&caching_ctl->wait);
501 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400502 }
Chris Masone37c9e62007-05-09 20:13:14 -0400503 path->slots[0]++;
504 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400505 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400506
507 total_found += add_new_free_space(block_group, fs_info, last,
508 block_group->key.objectid +
509 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400510 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400511
512 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400513 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400514 block_group->cached = BTRFS_CACHE_FINISHED;
515 spin_unlock(&block_group->lock);
516
Chris Mason54aa1f42007-06-22 14:16:25 -0400517err:
Chris Masone37c9e62007-05-09 20:13:14 -0400518 btrfs_free_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400519 up_read(&fs_info->commit_root_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400520
Yan Zheng11833d62009-09-11 16:11:19 -0400521 free_excluded_extents(extent_root, block_group);
522
523 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400524out:
Josef Bacik36cce922013-08-05 11:15:21 -0400525 if (ret) {
526 spin_lock(&block_group->lock);
527 block_group->caching_ctl = NULL;
528 block_group->cached = BTRFS_CACHE_ERROR;
529 spin_unlock(&block_group->lock);
530 }
Yan Zheng11833d62009-09-11 16:11:19 -0400531 wake_up(&caching_ctl->wait);
532
533 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000534 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400535}
536
Josef Bacik9d66e232010-08-25 16:54:15 -0400537static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400538 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400539{
Josef Bacik291c7d22011-11-14 13:52:14 -0500540 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400541 struct btrfs_fs_info *fs_info = cache->fs_info;
542 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400543 int ret = 0;
544
Josef Bacik291c7d22011-11-14 13:52:14 -0500545 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100546 if (!caching_ctl)
547 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500548
549 INIT_LIST_HEAD(&caching_ctl->list);
550 mutex_init(&caching_ctl->mutex);
551 init_waitqueue_head(&caching_ctl->wait);
552 caching_ctl->block_group = cache;
553 caching_ctl->progress = cache->key.objectid;
554 atomic_set(&caching_ctl->count, 1);
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800555 btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL);
Josef Bacik291c7d22011-11-14 13:52:14 -0500556
557 spin_lock(&cache->lock);
558 /*
559 * This should be a rare occasion, but this could happen I think in the
560 * case where one thread starts to load the space cache info, and then
561 * some other thread starts a transaction commit which tries to do an
562 * allocation while the other thread is still loading the space cache
563 * info. The previous loop should have kept us from choosing this block
564 * group, but if we've moved to the state where we will wait on caching
565 * block groups we need to first check if we're doing a fast load here,
566 * so we can wait for it to finish, otherwise we could end up allocating
567 * from a block group who's cache gets evicted for one reason or
568 * another.
569 */
570 while (cache->cached == BTRFS_CACHE_FAST) {
571 struct btrfs_caching_control *ctl;
572
573 ctl = cache->caching_ctl;
574 atomic_inc(&ctl->count);
575 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
576 spin_unlock(&cache->lock);
577
578 schedule();
579
580 finish_wait(&ctl->wait, &wait);
581 put_caching_control(ctl);
582 spin_lock(&cache->lock);
583 }
584
585 if (cache->cached != BTRFS_CACHE_NO) {
586 spin_unlock(&cache->lock);
587 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400588 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500589 }
590 WARN_ON(cache->caching_ctl);
591 cache->caching_ctl = caching_ctl;
592 cache->cached = BTRFS_CACHE_FAST;
593 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400594
Josef Bacikd53ba472012-04-12 16:03:57 -0400595 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400596 ret = load_free_space_cache(fs_info, cache);
597
598 spin_lock(&cache->lock);
599 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500600 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400601 cache->cached = BTRFS_CACHE_FINISHED;
602 cache->last_byte_to_unpin = (u64)-1;
603 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500604 if (load_cache_only) {
605 cache->caching_ctl = NULL;
606 cache->cached = BTRFS_CACHE_NO;
607 } else {
608 cache->cached = BTRFS_CACHE_STARTED;
609 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400610 }
611 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500612 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000613 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500614 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000615 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400616 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000617 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500618 } else {
619 /*
620 * We are not going to do the fast caching, set cached to the
621 * appropriate value and wakeup any waiters.
622 */
623 spin_lock(&cache->lock);
624 if (load_cache_only) {
625 cache->caching_ctl = NULL;
626 cache->cached = BTRFS_CACHE_NO;
627 } else {
628 cache->cached = BTRFS_CACHE_STARTED;
629 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400630 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500631 wake_up(&caching_ctl->wait);
632 }
633
634 if (load_cache_only) {
635 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400636 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400637 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400638
Josef Bacik9e351cc2014-03-13 15:42:13 -0400639 down_write(&fs_info->commit_root_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500640 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400641 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400642 up_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -0400643
Josef Bacik11dfe352009-11-13 20:12:59 +0000644 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400645
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800646 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400647
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400648 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400649}
650
Josef Bacik0f9dd462008-09-23 13:14:11 -0400651/*
652 * return the block group that starts at or after bytenr
653 */
Chris Masond3977122009-01-05 21:25:51 -0500654static struct btrfs_block_group_cache *
655btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400656{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400657 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400658
Josef Bacik0f9dd462008-09-23 13:14:11 -0400659 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400660
Josef Bacik0f9dd462008-09-23 13:14:11 -0400661 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400662}
663
Josef Bacik0f9dd462008-09-23 13:14:11 -0400664/*
Sankar P9f556842009-05-14 13:52:22 -0400665 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400666 */
Chris Masond3977122009-01-05 21:25:51 -0500667struct btrfs_block_group_cache *btrfs_lookup_block_group(
668 struct btrfs_fs_info *info,
669 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400670{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400671 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400672
Josef Bacik0f9dd462008-09-23 13:14:11 -0400673 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400674
Josef Bacik0f9dd462008-09-23 13:14:11 -0400675 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400676}
Chris Mason0b86a832008-03-24 15:01:56 -0400677
Josef Bacik0f9dd462008-09-23 13:14:11 -0400678static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
679 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400680{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400681 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400682 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400683
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200684 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400685
Chris Mason4184ea72009-03-10 12:39:20 -0400686 rcu_read_lock();
687 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400688 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400689 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400690 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400691 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400692 }
Chris Mason4184ea72009-03-10 12:39:20 -0400693 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400694 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400695}
696
Chris Mason4184ea72009-03-10 12:39:20 -0400697/*
698 * after adding space to the filesystem, we need to clear the full flags
699 * on all the space infos.
700 */
701void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
702{
703 struct list_head *head = &info->space_info;
704 struct btrfs_space_info *found;
705
706 rcu_read_lock();
707 list_for_each_entry_rcu(found, head, list)
708 found->full = 0;
709 rcu_read_unlock();
710}
711
Chris Masone02119d2008-09-05 16:13:11 -0400712/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400713int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400714{
715 int ret;
716 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400717 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400718
Zheng Yan31840ae2008-09-23 13:14:14 -0400719 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700720 if (!path)
721 return -ENOMEM;
722
Chris Masone02119d2008-09-05 16:13:11 -0400723 key.objectid = start;
724 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500725 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400726 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
727 0, 0);
Josef Bacik3173a182013-03-07 14:22:04 -0500728 if (ret > 0) {
729 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
730 if (key.objectid == start &&
731 key.type == BTRFS_METADATA_ITEM_KEY)
732 ret = 0;
733 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400734 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500735 return ret;
736}
737
Chris Masond8d5f3e2007-12-11 12:42:00 -0500738/*
Josef Bacik3173a182013-03-07 14:22:04 -0500739 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400740 *
741 * the head node for delayed ref is used to store the sum of all the
742 * reference count modifications queued up in the rbtree. the head
743 * node may also store the extent flags to set. This way you can check
744 * to see what the reference count and extent flags would be if all of
745 * the delayed refs are not processed.
746 */
747int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
748 struct btrfs_root *root, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500749 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400750{
751 struct btrfs_delayed_ref_head *head;
752 struct btrfs_delayed_ref_root *delayed_refs;
753 struct btrfs_path *path;
754 struct btrfs_extent_item *ei;
755 struct extent_buffer *leaf;
756 struct btrfs_key key;
757 u32 item_size;
758 u64 num_refs;
759 u64 extent_flags;
760 int ret;
761
Josef Bacik3173a182013-03-07 14:22:04 -0500762 /*
763 * If we don't have skinny metadata, don't bother doing anything
764 * different
765 */
766 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
767 offset = root->leafsize;
768 metadata = 0;
769 }
770
Yan, Zhenga22285a2010-05-16 10:48:46 -0400771 path = btrfs_alloc_path();
772 if (!path)
773 return -ENOMEM;
774
Yan, Zhenga22285a2010-05-16 10:48:46 -0400775 if (!trans) {
776 path->skip_locking = 1;
777 path->search_commit_root = 1;
778 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000779
780search_again:
781 key.objectid = bytenr;
782 key.offset = offset;
783 if (metadata)
784 key.type = BTRFS_METADATA_ITEM_KEY;
785 else
786 key.type = BTRFS_EXTENT_ITEM_KEY;
787
Yan, Zhenga22285a2010-05-16 10:48:46 -0400788again:
789 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
790 &key, path, 0, 0);
791 if (ret < 0)
792 goto out_free;
793
Josef Bacik3173a182013-03-07 14:22:04 -0500794 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100795 if (path->slots[0]) {
796 path->slots[0]--;
797 btrfs_item_key_to_cpu(path->nodes[0], &key,
798 path->slots[0]);
799 if (key.objectid == bytenr &&
800 key.type == BTRFS_EXTENT_ITEM_KEY &&
801 key.offset == root->leafsize)
802 ret = 0;
803 }
804 if (ret) {
805 key.objectid = bytenr;
806 key.type = BTRFS_EXTENT_ITEM_KEY;
807 key.offset = root->leafsize;
808 btrfs_release_path(path);
809 goto again;
810 }
Josef Bacik3173a182013-03-07 14:22:04 -0500811 }
812
Yan, Zhenga22285a2010-05-16 10:48:46 -0400813 if (ret == 0) {
814 leaf = path->nodes[0];
815 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
816 if (item_size >= sizeof(*ei)) {
817 ei = btrfs_item_ptr(leaf, path->slots[0],
818 struct btrfs_extent_item);
819 num_refs = btrfs_extent_refs(leaf, ei);
820 extent_flags = btrfs_extent_flags(leaf, ei);
821 } else {
822#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
823 struct btrfs_extent_item_v0 *ei0;
824 BUG_ON(item_size != sizeof(*ei0));
825 ei0 = btrfs_item_ptr(leaf, path->slots[0],
826 struct btrfs_extent_item_v0);
827 num_refs = btrfs_extent_refs_v0(leaf, ei0);
828 /* FIXME: this isn't correct for data */
829 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
830#else
831 BUG();
832#endif
833 }
834 BUG_ON(num_refs == 0);
835 } else {
836 num_refs = 0;
837 extent_flags = 0;
838 ret = 0;
839 }
840
841 if (!trans)
842 goto out;
843
844 delayed_refs = &trans->transaction->delayed_refs;
845 spin_lock(&delayed_refs->lock);
846 head = btrfs_find_delayed_ref_head(trans, bytenr);
847 if (head) {
848 if (!mutex_trylock(&head->mutex)) {
849 atomic_inc(&head->node.refs);
850 spin_unlock(&delayed_refs->lock);
851
David Sterbab3b4aa72011-04-21 01:20:15 +0200852 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400853
David Sterba8cc33e52011-05-02 15:29:25 +0200854 /*
855 * Mutex was contended, block until it's released and try
856 * again
857 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400858 mutex_lock(&head->mutex);
859 mutex_unlock(&head->mutex);
860 btrfs_put_delayed_ref(&head->node);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000861 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400862 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500863 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400864 if (head->extent_op && head->extent_op->update_flags)
865 extent_flags |= head->extent_op->flags_to_set;
866 else
867 BUG_ON(num_refs == 0);
868
869 num_refs += head->node.ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500870 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400871 mutex_unlock(&head->mutex);
872 }
873 spin_unlock(&delayed_refs->lock);
874out:
875 WARN_ON(num_refs == 0);
876 if (refs)
877 *refs = num_refs;
878 if (flags)
879 *flags = extent_flags;
880out_free:
881 btrfs_free_path(path);
882 return ret;
883}
884
885/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500886 * Back reference rules. Back refs have three main goals:
887 *
888 * 1) differentiate between all holders of references to an extent so that
889 * when a reference is dropped we can make sure it was a valid reference
890 * before freeing the extent.
891 *
892 * 2) Provide enough information to quickly find the holders of an extent
893 * if we notice a given block is corrupted or bad.
894 *
895 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
896 * maintenance. This is actually the same as #2, but with a slightly
897 * different use case.
898 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 * There are two kinds of back refs. The implicit back refs is optimized
900 * for pointers in non-shared tree blocks. For a given pointer in a block,
901 * back refs of this kind provide information about the block's owner tree
902 * and the pointer's key. These information allow us to find the block by
903 * b-tree searching. The full back refs is for pointers in tree blocks not
904 * referenced by their owner trees. The location of tree block is recorded
905 * in the back refs. Actually the full back refs is generic, and can be
906 * used in all cases the implicit back refs is used. The major shortcoming
907 * of the full back refs is its overhead. Every time a tree block gets
908 * COWed, we have to update back refs entry for all pointers in it.
909 *
910 * For a newly allocated tree block, we use implicit back refs for
911 * pointers in it. This means most tree related operations only involve
912 * implicit back refs. For a tree block created in old transaction, the
913 * only way to drop a reference to it is COW it. So we can detect the
914 * event that tree block loses its owner tree's reference and do the
915 * back refs conversion.
916 *
917 * When a tree block is COW'd through a tree, there are four cases:
918 *
919 * The reference count of the block is one and the tree is the block's
920 * owner tree. Nothing to do in this case.
921 *
922 * The reference count of the block is one and the tree is not the
923 * block's owner tree. In this case, full back refs is used for pointers
924 * in the block. Remove these full back refs, add implicit back refs for
925 * every pointers in the new block.
926 *
927 * The reference count of the block is greater than one and the tree is
928 * the block's owner tree. In this case, implicit back refs is used for
929 * pointers in the block. Add full back refs for every pointers in the
930 * block, increase lower level extents' reference counts. The original
931 * implicit back refs are entailed to the new block.
932 *
933 * The reference count of the block is greater than one and the tree is
934 * not the block's owner tree. Add implicit back refs for every pointer in
935 * the new block, increase lower level extents' reference count.
936 *
937 * Back Reference Key composing:
938 *
939 * The key objectid corresponds to the first byte in the extent,
940 * The key type is used to differentiate between types of back refs.
941 * There are different meanings of the key offset for different types
942 * of back refs.
943 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500944 * File extents can be referenced by:
945 *
946 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400947 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500948 * - different offsets inside a file (bookend extents in file.c)
949 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500951 *
952 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500953 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400954 * - original offset in the file
955 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400956 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400957 * The key offset for the implicit back refs is hash of the first
958 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500959 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400960 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500961 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400962 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500963 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400964 * The key offset for the implicit back refs is the first byte of
965 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500966 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400967 * When a file extent is allocated, The implicit back refs is used.
968 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500969 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400970 * (root_key.objectid, inode objectid, offset in file, 1)
971 *
972 * When a file extent is removed file truncation, we find the
973 * corresponding implicit back refs and check the following fields:
974 *
975 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500976 *
977 * Btree extents can be referenced by:
978 *
979 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500980 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400981 * Both the implicit back refs and the full back refs for tree blocks
982 * only consist of key. The key offset for the implicit back refs is
983 * objectid of block's owner tree. The key offset for the full back refs
984 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500985 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400986 * When implicit back refs is used, information about the lowest key and
987 * level of the tree block are required. These information are stored in
988 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500989 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400990
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400991#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
992static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
993 struct btrfs_root *root,
994 struct btrfs_path *path,
995 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500996{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400997 struct btrfs_extent_item *item;
998 struct btrfs_extent_item_v0 *ei0;
999 struct btrfs_extent_ref_v0 *ref0;
1000 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -04001001 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001002 struct btrfs_key key;
1003 struct btrfs_key found_key;
1004 u32 new_size = sizeof(*item);
1005 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -05001006 int ret;
1007
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001008 leaf = path->nodes[0];
1009 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -05001010
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001011 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1012 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1013 struct btrfs_extent_item_v0);
1014 refs = btrfs_extent_refs_v0(leaf, ei0);
1015
1016 if (owner == (u64)-1) {
1017 while (1) {
1018 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1019 ret = btrfs_next_leaf(root, path);
1020 if (ret < 0)
1021 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001022 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001023 leaf = path->nodes[0];
1024 }
1025 btrfs_item_key_to_cpu(leaf, &found_key,
1026 path->slots[0]);
1027 BUG_ON(key.objectid != found_key.objectid);
1028 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1029 path->slots[0]++;
1030 continue;
1031 }
1032 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1033 struct btrfs_extent_ref_v0);
1034 owner = btrfs_ref_objectid_v0(leaf, ref0);
1035 break;
1036 }
1037 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001038 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001039
1040 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1041 new_size += sizeof(*bi);
1042
1043 new_size -= sizeof(*ei0);
1044 ret = btrfs_search_slot(trans, root, &key, path,
1045 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001046 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001047 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001048 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001049
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001050 btrfs_extend_item(root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001051
1052 leaf = path->nodes[0];
1053 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1054 btrfs_set_extent_refs(leaf, item, refs);
1055 /* FIXME: get real generation */
1056 btrfs_set_extent_generation(leaf, item, 0);
1057 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1058 btrfs_set_extent_flags(leaf, item,
1059 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1060 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1061 bi = (struct btrfs_tree_block_info *)(item + 1);
1062 /* FIXME: get first key of the block */
1063 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1064 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1065 } else {
1066 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1067 }
1068 btrfs_mark_buffer_dirty(leaf);
1069 return 0;
1070}
1071#endif
1072
1073static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1074{
1075 u32 high_crc = ~(u32)0;
1076 u32 low_crc = ~(u32)0;
1077 __le64 lenum;
1078
1079 lenum = cpu_to_le64(root_objectid);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001080 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001081 lenum = cpu_to_le64(owner);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001082 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001083 lenum = cpu_to_le64(offset);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001084 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001085
1086 return ((u64)high_crc << 31) ^ (u64)low_crc;
1087}
1088
1089static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1090 struct btrfs_extent_data_ref *ref)
1091{
1092 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1093 btrfs_extent_data_ref_objectid(leaf, ref),
1094 btrfs_extent_data_ref_offset(leaf, ref));
1095}
1096
1097static int match_extent_data_ref(struct extent_buffer *leaf,
1098 struct btrfs_extent_data_ref *ref,
1099 u64 root_objectid, u64 owner, u64 offset)
1100{
1101 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1102 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1103 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1104 return 0;
1105 return 1;
1106}
1107
1108static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1109 struct btrfs_root *root,
1110 struct btrfs_path *path,
1111 u64 bytenr, u64 parent,
1112 u64 root_objectid,
1113 u64 owner, u64 offset)
1114{
1115 struct btrfs_key key;
1116 struct btrfs_extent_data_ref *ref;
1117 struct extent_buffer *leaf;
1118 u32 nritems;
1119 int ret;
1120 int recow;
1121 int err = -ENOENT;
1122
1123 key.objectid = bytenr;
1124 if (parent) {
1125 key.type = BTRFS_SHARED_DATA_REF_KEY;
1126 key.offset = parent;
1127 } else {
1128 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1129 key.offset = hash_extent_data_ref(root_objectid,
1130 owner, offset);
1131 }
1132again:
1133 recow = 0;
1134 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1135 if (ret < 0) {
1136 err = ret;
1137 goto fail;
1138 }
1139
1140 if (parent) {
1141 if (!ret)
1142 return 0;
1143#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1144 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001145 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001146 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1147 if (ret < 0) {
1148 err = ret;
1149 goto fail;
1150 }
1151 if (!ret)
1152 return 0;
1153#endif
1154 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001155 }
1156
1157 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001158 nritems = btrfs_header_nritems(leaf);
1159 while (1) {
1160 if (path->slots[0] >= nritems) {
1161 ret = btrfs_next_leaf(root, path);
1162 if (ret < 0)
1163 err = ret;
1164 if (ret)
1165 goto fail;
1166
1167 leaf = path->nodes[0];
1168 nritems = btrfs_header_nritems(leaf);
1169 recow = 1;
1170 }
1171
1172 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1173 if (key.objectid != bytenr ||
1174 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1175 goto fail;
1176
1177 ref = btrfs_item_ptr(leaf, path->slots[0],
1178 struct btrfs_extent_data_ref);
1179
1180 if (match_extent_data_ref(leaf, ref, root_objectid,
1181 owner, offset)) {
1182 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001183 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001184 goto again;
1185 }
1186 err = 0;
1187 break;
1188 }
1189 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001190 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001191fail:
1192 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001193}
1194
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001195static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1196 struct btrfs_root *root,
1197 struct btrfs_path *path,
1198 u64 bytenr, u64 parent,
1199 u64 root_objectid, u64 owner,
1200 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001201{
1202 struct btrfs_key key;
1203 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001204 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001205 u32 num_refs;
1206 int ret;
1207
1208 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001209 if (parent) {
1210 key.type = BTRFS_SHARED_DATA_REF_KEY;
1211 key.offset = parent;
1212 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001213 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001214 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1215 key.offset = hash_extent_data_ref(root_objectid,
1216 owner, offset);
1217 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001218 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001219
1220 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1221 if (ret && ret != -EEXIST)
1222 goto fail;
1223
1224 leaf = path->nodes[0];
1225 if (parent) {
1226 struct btrfs_shared_data_ref *ref;
1227 ref = btrfs_item_ptr(leaf, path->slots[0],
1228 struct btrfs_shared_data_ref);
1229 if (ret == 0) {
1230 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1231 } else {
1232 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1233 num_refs += refs_to_add;
1234 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1235 }
1236 } else {
1237 struct btrfs_extent_data_ref *ref;
1238 while (ret == -EEXIST) {
1239 ref = btrfs_item_ptr(leaf, path->slots[0],
1240 struct btrfs_extent_data_ref);
1241 if (match_extent_data_ref(leaf, ref, root_objectid,
1242 owner, offset))
1243 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001244 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001245 key.offset++;
1246 ret = btrfs_insert_empty_item(trans, root, path, &key,
1247 size);
1248 if (ret && ret != -EEXIST)
1249 goto fail;
1250
1251 leaf = path->nodes[0];
1252 }
1253 ref = btrfs_item_ptr(leaf, path->slots[0],
1254 struct btrfs_extent_data_ref);
1255 if (ret == 0) {
1256 btrfs_set_extent_data_ref_root(leaf, ref,
1257 root_objectid);
1258 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1259 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1260 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1261 } else {
1262 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1263 num_refs += refs_to_add;
1264 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1265 }
1266 }
1267 btrfs_mark_buffer_dirty(leaf);
1268 ret = 0;
1269fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001270 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001271 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001272}
1273
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001274static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1275 struct btrfs_root *root,
1276 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -07001277 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001278{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001279 struct btrfs_key key;
1280 struct btrfs_extent_data_ref *ref1 = NULL;
1281 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001282 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001283 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001284 int ret = 0;
1285
1286 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001287 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1288
1289 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1290 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1291 struct btrfs_extent_data_ref);
1292 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1293 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1294 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1295 struct btrfs_shared_data_ref);
1296 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1297#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1298 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1299 struct btrfs_extent_ref_v0 *ref0;
1300 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1301 struct btrfs_extent_ref_v0);
1302 num_refs = btrfs_ref_count_v0(leaf, ref0);
1303#endif
1304 } else {
1305 BUG();
1306 }
1307
Chris Mason56bec292009-03-13 10:10:06 -04001308 BUG_ON(num_refs < refs_to_drop);
1309 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001310
Zheng Yan31840ae2008-09-23 13:14:14 -04001311 if (num_refs == 0) {
1312 ret = btrfs_del_item(trans, root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -07001313 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001314 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001315 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1316 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1317 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1318 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1319#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1320 else {
1321 struct btrfs_extent_ref_v0 *ref0;
1322 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1323 struct btrfs_extent_ref_v0);
1324 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1325 }
1326#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001327 btrfs_mark_buffer_dirty(leaf);
1328 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001329 return ret;
1330}
1331
1332static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1333 struct btrfs_path *path,
1334 struct btrfs_extent_inline_ref *iref)
1335{
1336 struct btrfs_key key;
1337 struct extent_buffer *leaf;
1338 struct btrfs_extent_data_ref *ref1;
1339 struct btrfs_shared_data_ref *ref2;
1340 u32 num_refs = 0;
1341
1342 leaf = path->nodes[0];
1343 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1344 if (iref) {
1345 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1346 BTRFS_EXTENT_DATA_REF_KEY) {
1347 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1348 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1349 } else {
1350 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1351 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1352 }
1353 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1354 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1355 struct btrfs_extent_data_ref);
1356 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1357 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1358 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1359 struct btrfs_shared_data_ref);
1360 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1361#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1362 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1363 struct btrfs_extent_ref_v0 *ref0;
1364 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1365 struct btrfs_extent_ref_v0);
1366 num_refs = btrfs_ref_count_v0(leaf, ref0);
1367#endif
1368 } else {
1369 WARN_ON(1);
1370 }
1371 return num_refs;
1372}
1373
1374static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1375 struct btrfs_root *root,
1376 struct btrfs_path *path,
1377 u64 bytenr, u64 parent,
1378 u64 root_objectid)
1379{
1380 struct btrfs_key key;
1381 int ret;
1382
1383 key.objectid = bytenr;
1384 if (parent) {
1385 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1386 key.offset = parent;
1387 } else {
1388 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1389 key.offset = root_objectid;
1390 }
1391
1392 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1393 if (ret > 0)
1394 ret = -ENOENT;
1395#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1396 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001397 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001398 key.type = BTRFS_EXTENT_REF_V0_KEY;
1399 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1400 if (ret > 0)
1401 ret = -ENOENT;
1402 }
1403#endif
1404 return ret;
1405}
1406
1407static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1408 struct btrfs_root *root,
1409 struct btrfs_path *path,
1410 u64 bytenr, u64 parent,
1411 u64 root_objectid)
1412{
1413 struct btrfs_key key;
1414 int ret;
1415
1416 key.objectid = bytenr;
1417 if (parent) {
1418 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1419 key.offset = parent;
1420 } else {
1421 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1422 key.offset = root_objectid;
1423 }
1424
1425 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001426 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001427 return ret;
1428}
1429
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001430static inline int extent_ref_type(u64 parent, u64 owner)
1431{
1432 int type;
1433 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1434 if (parent > 0)
1435 type = BTRFS_SHARED_BLOCK_REF_KEY;
1436 else
1437 type = BTRFS_TREE_BLOCK_REF_KEY;
1438 } else {
1439 if (parent > 0)
1440 type = BTRFS_SHARED_DATA_REF_KEY;
1441 else
1442 type = BTRFS_EXTENT_DATA_REF_KEY;
1443 }
1444 return type;
1445}
1446
Yan Zheng2c47e6052009-06-27 21:07:35 -04001447static int find_next_key(struct btrfs_path *path, int level,
1448 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001449
1450{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001451 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001452 if (!path->nodes[level])
1453 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001454 if (path->slots[level] + 1 >=
1455 btrfs_header_nritems(path->nodes[level]))
1456 continue;
1457 if (level == 0)
1458 btrfs_item_key_to_cpu(path->nodes[level], key,
1459 path->slots[level] + 1);
1460 else
1461 btrfs_node_key_to_cpu(path->nodes[level], key,
1462 path->slots[level] + 1);
1463 return 0;
1464 }
1465 return 1;
1466}
1467
1468/*
1469 * look for inline back ref. if back ref is found, *ref_ret is set
1470 * to the address of inline back ref, and 0 is returned.
1471 *
1472 * if back ref isn't found, *ref_ret is set to the address where it
1473 * should be inserted, and -ENOENT is returned.
1474 *
1475 * if insert is true and there are too many inline back refs, the path
1476 * points to the extent item, and -EAGAIN is returned.
1477 *
1478 * NOTE: inline back refs are ordered in the same way that back ref
1479 * items in the tree are ordered.
1480 */
1481static noinline_for_stack
1482int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1483 struct btrfs_root *root,
1484 struct btrfs_path *path,
1485 struct btrfs_extent_inline_ref **ref_ret,
1486 u64 bytenr, u64 num_bytes,
1487 u64 parent, u64 root_objectid,
1488 u64 owner, u64 offset, int insert)
1489{
1490 struct btrfs_key key;
1491 struct extent_buffer *leaf;
1492 struct btrfs_extent_item *ei;
1493 struct btrfs_extent_inline_ref *iref;
1494 u64 flags;
1495 u64 item_size;
1496 unsigned long ptr;
1497 unsigned long end;
1498 int extra_size;
1499 int type;
1500 int want;
1501 int ret;
1502 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001503 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1504 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001505
1506 key.objectid = bytenr;
1507 key.type = BTRFS_EXTENT_ITEM_KEY;
1508 key.offset = num_bytes;
1509
1510 want = extent_ref_type(parent, owner);
1511 if (insert) {
1512 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001513 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001514 } else
1515 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001516
1517 /*
1518 * Owner is our parent level, so we can just add one to get the level
1519 * for the block we are interested in.
1520 */
1521 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1522 key.type = BTRFS_METADATA_ITEM_KEY;
1523 key.offset = owner;
1524 }
1525
1526again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001527 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1528 if (ret < 0) {
1529 err = ret;
1530 goto out;
1531 }
Josef Bacik3173a182013-03-07 14:22:04 -05001532
1533 /*
1534 * We may be a newly converted file system which still has the old fat
1535 * extent entries for metadata, so try and see if we have one of those.
1536 */
1537 if (ret > 0 && skinny_metadata) {
1538 skinny_metadata = false;
1539 if (path->slots[0]) {
1540 path->slots[0]--;
1541 btrfs_item_key_to_cpu(path->nodes[0], &key,
1542 path->slots[0]);
1543 if (key.objectid == bytenr &&
1544 key.type == BTRFS_EXTENT_ITEM_KEY &&
1545 key.offset == num_bytes)
1546 ret = 0;
1547 }
1548 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +01001549 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05001550 key.type = BTRFS_EXTENT_ITEM_KEY;
1551 key.offset = num_bytes;
1552 btrfs_release_path(path);
1553 goto again;
1554 }
1555 }
1556
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001557 if (ret && !insert) {
1558 err = -ENOENT;
1559 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301560 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -05001561 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -05001562 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001563 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001564
1565 leaf = path->nodes[0];
1566 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1567#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1568 if (item_size < sizeof(*ei)) {
1569 if (!insert) {
1570 err = -ENOENT;
1571 goto out;
1572 }
1573 ret = convert_extent_item_v0(trans, root, path, owner,
1574 extra_size);
1575 if (ret < 0) {
1576 err = ret;
1577 goto out;
1578 }
1579 leaf = path->nodes[0];
1580 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1581 }
1582#endif
1583 BUG_ON(item_size < sizeof(*ei));
1584
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001585 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1586 flags = btrfs_extent_flags(leaf, ei);
1587
1588 ptr = (unsigned long)(ei + 1);
1589 end = (unsigned long)ei + item_size;
1590
Josef Bacik3173a182013-03-07 14:22:04 -05001591 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001592 ptr += sizeof(struct btrfs_tree_block_info);
1593 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001594 }
1595
1596 err = -ENOENT;
1597 while (1) {
1598 if (ptr >= end) {
1599 WARN_ON(ptr > end);
1600 break;
1601 }
1602 iref = (struct btrfs_extent_inline_ref *)ptr;
1603 type = btrfs_extent_inline_ref_type(leaf, iref);
1604 if (want < type)
1605 break;
1606 if (want > type) {
1607 ptr += btrfs_extent_inline_ref_size(type);
1608 continue;
1609 }
1610
1611 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1612 struct btrfs_extent_data_ref *dref;
1613 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1614 if (match_extent_data_ref(leaf, dref, root_objectid,
1615 owner, offset)) {
1616 err = 0;
1617 break;
1618 }
1619 if (hash_extent_data_ref_item(leaf, dref) <
1620 hash_extent_data_ref(root_objectid, owner, offset))
1621 break;
1622 } else {
1623 u64 ref_offset;
1624 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1625 if (parent > 0) {
1626 if (parent == ref_offset) {
1627 err = 0;
1628 break;
1629 }
1630 if (ref_offset < parent)
1631 break;
1632 } else {
1633 if (root_objectid == ref_offset) {
1634 err = 0;
1635 break;
1636 }
1637 if (ref_offset < root_objectid)
1638 break;
1639 }
1640 }
1641 ptr += btrfs_extent_inline_ref_size(type);
1642 }
1643 if (err == -ENOENT && insert) {
1644 if (item_size + extra_size >=
1645 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1646 err = -EAGAIN;
1647 goto out;
1648 }
1649 /*
1650 * To add new inline back ref, we have to make sure
1651 * there is no corresponding back ref item.
1652 * For simplicity, we just do not add new inline back
1653 * ref if there is any kind of item for this block
1654 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001655 if (find_next_key(path, 0, &key) == 0 &&
1656 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001657 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001658 err = -EAGAIN;
1659 goto out;
1660 }
1661 }
1662 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1663out:
Yan Zheng85d41982009-06-11 08:51:10 -04001664 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001665 path->keep_locks = 0;
1666 btrfs_unlock_up_safe(path, 1);
1667 }
1668 return err;
1669}
1670
1671/*
1672 * helper to add new inline back ref
1673 */
1674static noinline_for_stack
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001675void setup_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001676 struct btrfs_path *path,
1677 struct btrfs_extent_inline_ref *iref,
1678 u64 parent, u64 root_objectid,
1679 u64 owner, u64 offset, int refs_to_add,
1680 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001681{
1682 struct extent_buffer *leaf;
1683 struct btrfs_extent_item *ei;
1684 unsigned long ptr;
1685 unsigned long end;
1686 unsigned long item_offset;
1687 u64 refs;
1688 int size;
1689 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001690
1691 leaf = path->nodes[0];
1692 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1693 item_offset = (unsigned long)iref - (unsigned long)ei;
1694
1695 type = extent_ref_type(parent, owner);
1696 size = btrfs_extent_inline_ref_size(type);
1697
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001698 btrfs_extend_item(root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001699
1700 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1701 refs = btrfs_extent_refs(leaf, ei);
1702 refs += refs_to_add;
1703 btrfs_set_extent_refs(leaf, ei, refs);
1704 if (extent_op)
1705 __run_delayed_extent_op(extent_op, leaf, ei);
1706
1707 ptr = (unsigned long)ei + item_offset;
1708 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1709 if (ptr < end - size)
1710 memmove_extent_buffer(leaf, ptr + size, ptr,
1711 end - size - ptr);
1712
1713 iref = (struct btrfs_extent_inline_ref *)ptr;
1714 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1715 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1716 struct btrfs_extent_data_ref *dref;
1717 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1718 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1719 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1720 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1721 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1722 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1723 struct btrfs_shared_data_ref *sref;
1724 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1725 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1726 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1727 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1728 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1729 } else {
1730 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1731 }
1732 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001733}
1734
1735static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1736 struct btrfs_root *root,
1737 struct btrfs_path *path,
1738 struct btrfs_extent_inline_ref **ref_ret,
1739 u64 bytenr, u64 num_bytes, u64 parent,
1740 u64 root_objectid, u64 owner, u64 offset)
1741{
1742 int ret;
1743
1744 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1745 bytenr, num_bytes, parent,
1746 root_objectid, owner, offset, 0);
1747 if (ret != -ENOENT)
1748 return ret;
1749
David Sterbab3b4aa72011-04-21 01:20:15 +02001750 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001751 *ref_ret = NULL;
1752
1753 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1754 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1755 root_objectid);
1756 } else {
1757 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1758 root_objectid, owner, offset);
1759 }
1760 return ret;
1761}
1762
1763/*
1764 * helper to update/remove inline back ref
1765 */
1766static noinline_for_stack
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001767void update_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001768 struct btrfs_path *path,
1769 struct btrfs_extent_inline_ref *iref,
1770 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001771 struct btrfs_delayed_extent_op *extent_op,
1772 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001773{
1774 struct extent_buffer *leaf;
1775 struct btrfs_extent_item *ei;
1776 struct btrfs_extent_data_ref *dref = NULL;
1777 struct btrfs_shared_data_ref *sref = NULL;
1778 unsigned long ptr;
1779 unsigned long end;
1780 u32 item_size;
1781 int size;
1782 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001783 u64 refs;
1784
1785 leaf = path->nodes[0];
1786 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1787 refs = btrfs_extent_refs(leaf, ei);
1788 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1789 refs += refs_to_mod;
1790 btrfs_set_extent_refs(leaf, ei, refs);
1791 if (extent_op)
1792 __run_delayed_extent_op(extent_op, leaf, ei);
1793
1794 type = btrfs_extent_inline_ref_type(leaf, iref);
1795
1796 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1797 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1798 refs = btrfs_extent_data_ref_count(leaf, dref);
1799 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1800 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1801 refs = btrfs_shared_data_ref_count(leaf, sref);
1802 } else {
1803 refs = 1;
1804 BUG_ON(refs_to_mod != -1);
1805 }
1806
1807 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1808 refs += refs_to_mod;
1809
1810 if (refs > 0) {
1811 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1812 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1813 else
1814 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1815 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001816 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001817 size = btrfs_extent_inline_ref_size(type);
1818 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1819 ptr = (unsigned long)iref;
1820 end = (unsigned long)ei + item_size;
1821 if (ptr + size < end)
1822 memmove_extent_buffer(leaf, ptr, ptr + size,
1823 end - ptr - size);
1824 item_size -= size;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001825 btrfs_truncate_item(root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001826 }
1827 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001828}
1829
1830static noinline_for_stack
1831int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1832 struct btrfs_root *root,
1833 struct btrfs_path *path,
1834 u64 bytenr, u64 num_bytes, u64 parent,
1835 u64 root_objectid, u64 owner,
1836 u64 offset, int refs_to_add,
1837 struct btrfs_delayed_extent_op *extent_op)
1838{
1839 struct btrfs_extent_inline_ref *iref;
1840 int ret;
1841
1842 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1843 bytenr, num_bytes, parent,
1844 root_objectid, owner, offset, 1);
1845 if (ret == 0) {
1846 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001847 update_inline_extent_backref(root, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001848 refs_to_add, extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001849 } else if (ret == -ENOENT) {
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001850 setup_inline_extent_backref(root, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001851 root_objectid, owner, offset,
1852 refs_to_add, extent_op);
1853 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001854 }
1855 return ret;
1856}
1857
1858static int insert_extent_backref(struct btrfs_trans_handle *trans,
1859 struct btrfs_root *root,
1860 struct btrfs_path *path,
1861 u64 bytenr, u64 parent, u64 root_objectid,
1862 u64 owner, u64 offset, int refs_to_add)
1863{
1864 int ret;
1865 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1866 BUG_ON(refs_to_add != 1);
1867 ret = insert_tree_block_ref(trans, root, path, bytenr,
1868 parent, root_objectid);
1869 } else {
1870 ret = insert_extent_data_ref(trans, root, path, bytenr,
1871 parent, root_objectid,
1872 owner, offset, refs_to_add);
1873 }
1874 return ret;
1875}
1876
1877static int remove_extent_backref(struct btrfs_trans_handle *trans,
1878 struct btrfs_root *root,
1879 struct btrfs_path *path,
1880 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001881 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001882{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001883 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001884
1885 BUG_ON(!is_data && refs_to_drop != 1);
1886 if (iref) {
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001887 update_inline_extent_backref(root, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001888 -refs_to_drop, NULL, last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001889 } else if (is_data) {
Josef Bacikfcebe452014-05-13 17:30:47 -07001890 ret = remove_extent_data_ref(trans, root, path, refs_to_drop,
1891 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001892 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001893 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001894 ret = btrfs_del_item(trans, root, path);
1895 }
1896 return ret;
1897}
1898
Li Dongyang5378e602011-03-24 10:24:27 +00001899static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001900 u64 start, u64 len)
1901{
Li Dongyang5378e602011-03-24 10:24:27 +00001902 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001903}
Chris Mason15916de2008-11-19 21:17:22 -05001904
Liu Hui1f3c79a2009-01-05 15:57:51 -05001905static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001906 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001907{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001908 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001909 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001910 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001911
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001912
Liu Hui1f3c79a2009-01-05 15:57:51 -05001913 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001914 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001915 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001916 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001917 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001918 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001919 int i;
1920
Liu Hui1f3c79a2009-01-05 15:57:51 -05001921
Jan Schmidta1d3c472011-08-04 17:15:33 +02001922 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001923 if (!stripe->dev->can_discard)
1924 continue;
1925
Li Dongyang5378e602011-03-24 10:24:27 +00001926 ret = btrfs_issue_discard(stripe->dev->bdev,
1927 stripe->physical,
1928 stripe->length);
1929 if (!ret)
1930 discarded_bytes += stripe->length;
1931 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001932 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001933
1934 /*
1935 * Just in case we get back EOPNOTSUPP for some reason,
1936 * just ignore the return value so we don't screw up
1937 * people calling discard_extent.
1938 */
1939 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001940 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001941 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001942 }
Li Dongyang5378e602011-03-24 10:24:27 +00001943
1944 if (actual_bytes)
1945 *actual_bytes = discarded_bytes;
1946
Liu Hui1f3c79a2009-01-05 15:57:51 -05001947
David Woodhouse53b381b2013-01-29 18:40:14 -05001948 if (ret == -EOPNOTSUPP)
1949 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001950 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001951}
1952
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001953/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001954int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1955 struct btrfs_root *root,
1956 u64 bytenr, u64 num_bytes, u64 parent,
Josef Bacikfcebe452014-05-13 17:30:47 -07001957 u64 root_objectid, u64 owner, u64 offset,
1958 int no_quota)
Zheng Yan31840ae2008-09-23 13:14:14 -04001959{
1960 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001961 struct btrfs_fs_info *fs_info = root->fs_info;
1962
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001963 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1964 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001965
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001966 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001967 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1968 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001969 parent, root_objectid, (int)owner,
Josef Bacikfcebe452014-05-13 17:30:47 -07001970 BTRFS_ADD_DELAYED_REF, NULL, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001971 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001972 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1973 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001974 parent, root_objectid, owner, offset,
Josef Bacikfcebe452014-05-13 17:30:47 -07001975 BTRFS_ADD_DELAYED_REF, NULL, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001976 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001977 return ret;
1978}
1979
Chris Mason925baed2008-06-25 16:01:30 -04001980static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001981 struct btrfs_root *root,
1982 u64 bytenr, u64 num_bytes,
1983 u64 parent, u64 root_objectid,
1984 u64 owner, u64 offset, int refs_to_add,
Josef Bacikfcebe452014-05-13 17:30:47 -07001985 int no_quota,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001986 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001987{
Josef Bacikfcebe452014-05-13 17:30:47 -07001988 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04001989 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001990 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001991 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07001992 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001993 u64 refs;
1994 int ret;
Josef Bacikfcebe452014-05-13 17:30:47 -07001995 enum btrfs_qgroup_operation_type type = BTRFS_QGROUP_OPER_ADD_EXCL;
Chris Mason037e6392007-03-07 11:50:24 -05001996
Chris Mason5caf2a02007-04-02 11:20:42 -04001997 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001998 if (!path)
1999 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04002000
Josef Bacikfcebe452014-05-13 17:30:47 -07002001 if (!is_fstree(root_objectid) || !root->fs_info->quota_enabled)
2002 no_quota = 1;
2003
Chris Mason3c12ac72008-04-21 12:01:38 -04002004 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002005 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002006 /* this will setup the path even if it fails to insert the back ref */
Josef Bacikfcebe452014-05-13 17:30:47 -07002007 ret = insert_inline_extent_backref(trans, fs_info->extent_root, path,
2008 bytenr, num_bytes, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002009 root_objectid, owner, offset,
2010 refs_to_add, extent_op);
Josef Bacikfcebe452014-05-13 17:30:47 -07002011 if ((ret < 0 && ret != -EAGAIN) || (!ret && no_quota))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002012 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07002013 /*
2014 * Ok we were able to insert an inline extent and it appears to be a new
2015 * reference, deal with the qgroup accounting.
2016 */
2017 if (!ret && !no_quota) {
2018 ASSERT(root->fs_info->quota_enabled);
2019 leaf = path->nodes[0];
2020 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2021 item = btrfs_item_ptr(leaf, path->slots[0],
2022 struct btrfs_extent_item);
2023 if (btrfs_extent_refs(leaf, item) > (u64)refs_to_add)
2024 type = BTRFS_QGROUP_OPER_ADD_SHARED;
2025 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002026
Josef Bacikfcebe452014-05-13 17:30:47 -07002027 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
2028 bytenr, num_bytes, type, 0);
2029 goto out;
2030 }
2031
2032 /*
2033 * Ok we had -EAGAIN which means we didn't have space to insert and
2034 * inline extent ref, so just update the reference count and add a
2035 * normal backref.
2036 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002037 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07002038 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002039 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2040 refs = btrfs_extent_refs(leaf, item);
Josef Bacikfcebe452014-05-13 17:30:47 -07002041 if (refs)
2042 type = BTRFS_QGROUP_OPER_ADD_SHARED;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002043 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2044 if (extent_op)
2045 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002046
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002047 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002048 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002049
Josef Bacikfcebe452014-05-13 17:30:47 -07002050 if (!no_quota) {
2051 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
2052 bytenr, num_bytes, type, 0);
2053 if (ret)
2054 goto out;
2055 }
2056
Chris Mason3c12ac72008-04-21 12:01:38 -04002057 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002058 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04002059 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002060 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002061 path, bytenr, parent, root_objectid,
2062 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002063 if (ret)
2064 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002065out:
Chris Mason74493f72007-12-11 09:25:06 -05002066 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08002067 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002068}
2069
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002070static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2071 struct btrfs_root *root,
2072 struct btrfs_delayed_ref_node *node,
2073 struct btrfs_delayed_extent_op *extent_op,
2074 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002075{
Chris Mason56bec292009-03-13 10:10:06 -04002076 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002077 struct btrfs_delayed_data_ref *ref;
2078 struct btrfs_key ins;
2079 u64 parent = 0;
2080 u64 ref_root = 0;
2081 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002082
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002083 ins.objectid = node->bytenr;
2084 ins.offset = node->num_bytes;
2085 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002086
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002087 ref = btrfs_delayed_node_to_data_ref(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002088 trace_run_delayed_data_ref(node, ref, node->action);
2089
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002090 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2091 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002092 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002093
2094 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002095 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002096 flags |= extent_op->flags_to_set;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002097 ret = alloc_reserved_file_extent(trans, root,
2098 parent, ref_root, flags,
2099 ref->objectid, ref->offset,
2100 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002101 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2102 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2103 node->num_bytes, parent,
2104 ref_root, ref->objectid,
2105 ref->offset, node->ref_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07002106 node->no_quota, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002107 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2108 ret = __btrfs_free_extent(trans, root, node->bytenr,
2109 node->num_bytes, parent,
2110 ref_root, ref->objectid,
2111 ref->offset, node->ref_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07002112 extent_op, node->no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002113 } else {
2114 BUG();
2115 }
Chris Mason56bec292009-03-13 10:10:06 -04002116 return ret;
2117}
2118
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002119static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2120 struct extent_buffer *leaf,
2121 struct btrfs_extent_item *ei)
2122{
2123 u64 flags = btrfs_extent_flags(leaf, ei);
2124 if (extent_op->update_flags) {
2125 flags |= extent_op->flags_to_set;
2126 btrfs_set_extent_flags(leaf, ei, flags);
2127 }
2128
2129 if (extent_op->update_key) {
2130 struct btrfs_tree_block_info *bi;
2131 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2132 bi = (struct btrfs_tree_block_info *)(ei + 1);
2133 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2134 }
2135}
2136
2137static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2138 struct btrfs_root *root,
2139 struct btrfs_delayed_ref_node *node,
2140 struct btrfs_delayed_extent_op *extent_op)
2141{
2142 struct btrfs_key key;
2143 struct btrfs_path *path;
2144 struct btrfs_extent_item *ei;
2145 struct extent_buffer *leaf;
2146 u32 item_size;
2147 int ret;
2148 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002149 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002150
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002151 if (trans->aborted)
2152 return 0;
2153
Josef Bacik3173a182013-03-07 14:22:04 -05002154 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2155 metadata = 0;
2156
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002157 path = btrfs_alloc_path();
2158 if (!path)
2159 return -ENOMEM;
2160
2161 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002162
Josef Bacik3173a182013-03-07 14:22:04 -05002163 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002164 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002165 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002166 } else {
2167 key.type = BTRFS_EXTENT_ITEM_KEY;
2168 key.offset = node->num_bytes;
2169 }
2170
2171again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002172 path->reada = 1;
2173 path->leave_spinning = 1;
2174 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2175 path, 0, 1);
2176 if (ret < 0) {
2177 err = ret;
2178 goto out;
2179 }
2180 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002181 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002182 if (path->slots[0] > 0) {
2183 path->slots[0]--;
2184 btrfs_item_key_to_cpu(path->nodes[0], &key,
2185 path->slots[0]);
2186 if (key.objectid == node->bytenr &&
2187 key.type == BTRFS_EXTENT_ITEM_KEY &&
2188 key.offset == node->num_bytes)
2189 ret = 0;
2190 }
2191 if (ret > 0) {
2192 btrfs_release_path(path);
2193 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002194
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002195 key.objectid = node->bytenr;
2196 key.offset = node->num_bytes;
2197 key.type = BTRFS_EXTENT_ITEM_KEY;
2198 goto again;
2199 }
2200 } else {
2201 err = -EIO;
2202 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002203 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002204 }
2205
2206 leaf = path->nodes[0];
2207 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2208#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2209 if (item_size < sizeof(*ei)) {
2210 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2211 path, (u64)-1, 0);
2212 if (ret < 0) {
2213 err = ret;
2214 goto out;
2215 }
2216 leaf = path->nodes[0];
2217 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2218 }
2219#endif
2220 BUG_ON(item_size < sizeof(*ei));
2221 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2222 __run_delayed_extent_op(extent_op, leaf, ei);
2223
2224 btrfs_mark_buffer_dirty(leaf);
2225out:
2226 btrfs_free_path(path);
2227 return err;
2228}
2229
2230static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2231 struct btrfs_root *root,
2232 struct btrfs_delayed_ref_node *node,
2233 struct btrfs_delayed_extent_op *extent_op,
2234 int insert_reserved)
2235{
2236 int ret = 0;
2237 struct btrfs_delayed_tree_ref *ref;
2238 struct btrfs_key ins;
2239 u64 parent = 0;
2240 u64 ref_root = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002241 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2242 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002243
2244 ref = btrfs_delayed_node_to_tree_ref(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002245 trace_run_delayed_tree_ref(node, ref, node->action);
2246
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002247 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2248 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002249 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002250
Josef Bacik3173a182013-03-07 14:22:04 -05002251 ins.objectid = node->bytenr;
2252 if (skinny_metadata) {
2253 ins.offset = ref->level;
2254 ins.type = BTRFS_METADATA_ITEM_KEY;
2255 } else {
2256 ins.offset = node->num_bytes;
2257 ins.type = BTRFS_EXTENT_ITEM_KEY;
2258 }
2259
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002260 BUG_ON(node->ref_mod != 1);
2261 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002262 BUG_ON(!extent_op || !extent_op->update_flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002263 ret = alloc_reserved_tree_block(trans, root,
2264 parent, ref_root,
2265 extent_op->flags_to_set,
2266 &extent_op->key,
Josef Bacikfcebe452014-05-13 17:30:47 -07002267 ref->level, &ins,
2268 node->no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002269 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2270 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2271 node->num_bytes, parent, ref_root,
Josef Bacikfcebe452014-05-13 17:30:47 -07002272 ref->level, 0, 1, node->no_quota,
2273 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002274 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2275 ret = __btrfs_free_extent(trans, root, node->bytenr,
2276 node->num_bytes, parent, ref_root,
Josef Bacikfcebe452014-05-13 17:30:47 -07002277 ref->level, 0, 1, extent_op,
2278 node->no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002279 } else {
2280 BUG();
2281 }
2282 return ret;
2283}
2284
Chris Mason56bec292009-03-13 10:10:06 -04002285/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002286static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2287 struct btrfs_root *root,
2288 struct btrfs_delayed_ref_node *node,
2289 struct btrfs_delayed_extent_op *extent_op,
2290 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002291{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002292 int ret = 0;
2293
Josef Bacik857cc2f2013-10-07 15:21:08 -04002294 if (trans->aborted) {
2295 if (insert_reserved)
2296 btrfs_pin_extent(root, node->bytenr,
2297 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002298 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002299 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002300
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002301 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002302 struct btrfs_delayed_ref_head *head;
2303 /*
2304 * we've hit the end of the chain and we were supposed
2305 * to insert this extent into the tree. But, it got
2306 * deleted before we ever needed to insert it, so all
2307 * we have to do is clean up the accounting
2308 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002309 BUG_ON(extent_op);
2310 head = btrfs_delayed_node_to_head(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002311 trace_run_delayed_ref_head(node, head, node->action);
2312
Chris Mason56bec292009-03-13 10:10:06 -04002313 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002314 btrfs_pin_extent(root, node->bytenr,
2315 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002316 if (head->is_data) {
2317 ret = btrfs_del_csums(trans, root,
2318 node->bytenr,
2319 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002320 }
Chris Mason56bec292009-03-13 10:10:06 -04002321 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002322 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002323 }
Josef Bacikeb099672009-02-12 09:27:38 -05002324
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002325 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2326 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2327 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2328 insert_reserved);
2329 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2330 node->type == BTRFS_SHARED_DATA_REF_KEY)
2331 ret = run_delayed_data_ref(trans, root, node, extent_op,
2332 insert_reserved);
2333 else
2334 BUG();
2335 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002336}
2337
Chris Mason56bec292009-03-13 10:10:06 -04002338static noinline struct btrfs_delayed_ref_node *
2339select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002340{
Chris Mason56bec292009-03-13 10:10:06 -04002341 struct rb_node *node;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002342 struct btrfs_delayed_ref_node *ref, *last = NULL;;
2343
Chris Mason56bec292009-03-13 10:10:06 -04002344 /*
2345 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2346 * this prevents ref count from going down to zero when
2347 * there still are pending delayed ref.
2348 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002349 node = rb_first(&head->ref_root);
2350 while (node) {
Chris Mason56bec292009-03-13 10:10:06 -04002351 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2352 rb_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002353 if (ref->action == BTRFS_ADD_DELAYED_REF)
Chris Mason56bec292009-03-13 10:10:06 -04002354 return ref;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002355 else if (last == NULL)
2356 last = ref;
2357 node = rb_next(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002358 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002359 return last;
Chris Mason56bec292009-03-13 10:10:06 -04002360}
2361
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002362/*
2363 * Returns 0 on success or if called with an already aborted transaction.
2364 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2365 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002366static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2367 struct btrfs_root *root,
2368 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002369{
Chris Mason56bec292009-03-13 10:10:06 -04002370 struct btrfs_delayed_ref_root *delayed_refs;
2371 struct btrfs_delayed_ref_node *ref;
2372 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002373 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002374 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002375 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002376 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002377 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002378 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002379 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002380
2381 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002382 while (1) {
2383 if (!locked_ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002384 if (count >= nr)
Chris Mason56bec292009-03-13 10:10:06 -04002385 break;
Chris Mason56bec292009-03-13 10:10:06 -04002386
Josef Bacikd7df2c72014-01-23 09:21:38 -05002387 spin_lock(&delayed_refs->lock);
2388 locked_ref = btrfs_select_ref_head(trans);
2389 if (!locked_ref) {
2390 spin_unlock(&delayed_refs->lock);
2391 break;
2392 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002393
2394 /* grab the lock that says we are going to process
2395 * all the refs for this head */
2396 ret = btrfs_delayed_ref_lock(trans, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002397 spin_unlock(&delayed_refs->lock);
Chris Masonc3e69d52009-03-13 10:17:05 -04002398 /*
2399 * we may have dropped the spin lock to get the head
2400 * mutex lock, and that might have given someone else
2401 * time to free the head. If that's true, it has been
2402 * removed from our list and we can move on.
2403 */
2404 if (ret == -EAGAIN) {
2405 locked_ref = NULL;
2406 count++;
2407 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002408 }
2409 }
2410
2411 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002412 * We need to try and merge add/drops of the same ref since we
2413 * can run into issues with relocate dropping the implicit ref
2414 * and then it being added back again before the drop can
2415 * finish. If we merged anything we need to re-loop so we can
2416 * get a good ref.
2417 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002418 spin_lock(&locked_ref->lock);
Josef Bacikae1e2062012-08-07 16:00:32 -04002419 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2420 locked_ref);
2421
2422 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002423 * locked_ref is the head node, so we have to go one
2424 * node back for any delayed ref updates
2425 */
2426 ref = select_delayed_ref(locked_ref);
2427
2428 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002429 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002430 spin_unlock(&locked_ref->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002431 btrfs_delayed_ref_unlock(locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002432 spin_lock(&delayed_refs->lock);
2433 locked_ref->processing = 0;
Arne Jansend1270cd2011-09-13 15:16:43 +02002434 delayed_refs->num_heads_ready++;
2435 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002436 locked_ref = NULL;
Arne Jansend1270cd2011-09-13 15:16:43 +02002437 cond_resched();
Josef Bacik27a377d2014-02-07 13:57:59 -05002438 count++;
Arne Jansend1270cd2011-09-13 15:16:43 +02002439 continue;
2440 }
2441
2442 /*
Chris Mason56bec292009-03-13 10:10:06 -04002443 * record the must insert reserved flag before we
2444 * drop the spin lock.
2445 */
2446 must_insert_reserved = locked_ref->must_insert_reserved;
2447 locked_ref->must_insert_reserved = 0;
2448
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002449 extent_op = locked_ref->extent_op;
2450 locked_ref->extent_op = NULL;
2451
Chris Mason56bec292009-03-13 10:10:06 -04002452 if (!ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002453
2454
Chris Mason56bec292009-03-13 10:10:06 -04002455 /* All delayed refs have been processed, Go ahead
2456 * and send the head node to run_one_delayed_ref,
2457 * so that any accounting fixes can happen
2458 */
2459 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002460
2461 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002462 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002463 extent_op = NULL;
2464 }
2465
2466 if (extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002467 spin_unlock(&locked_ref->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002468 ret = run_delayed_extent_op(trans, root,
2469 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002470 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002471
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002472 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04002473 /*
2474 * Need to reset must_insert_reserved if
2475 * there was an error so the abort stuff
2476 * can cleanup the reserved space
2477 * properly.
2478 */
2479 if (must_insert_reserved)
2480 locked_ref->must_insert_reserved = 1;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002481 locked_ref->processing = 0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002482 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
Miao Xie093486c2012-12-19 08:10:10 +00002483 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002484 return ret;
2485 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002486 continue;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002487 }
Chris Mason56bec292009-03-13 10:10:06 -04002488
Josef Bacikd7df2c72014-01-23 09:21:38 -05002489 /*
2490 * Need to drop our head ref lock and re-aqcuire the
2491 * delayed ref lock and then re-check to make sure
2492 * nobody got added.
2493 */
2494 spin_unlock(&locked_ref->lock);
2495 spin_lock(&delayed_refs->lock);
2496 spin_lock(&locked_ref->lock);
Josef Bacik573a0752014-03-27 19:41:34 -04002497 if (rb_first(&locked_ref->ref_root) ||
2498 locked_ref->extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002499 spin_unlock(&locked_ref->lock);
2500 spin_unlock(&delayed_refs->lock);
2501 continue;
2502 }
2503 ref->in_tree = 0;
2504 delayed_refs->num_heads--;
Liu Boc46effa2013-10-14 12:59:45 +08002505 rb_erase(&locked_ref->href_node,
2506 &delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002507 spin_unlock(&delayed_refs->lock);
2508 } else {
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002509 actual_count++;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002510 ref->in_tree = 0;
2511 rb_erase(&ref->rb_node, &locked_ref->ref_root);
Liu Boc46effa2013-10-14 12:59:45 +08002512 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002513 atomic_dec(&delayed_refs->num_entries);
2514
Miao Xie093486c2012-12-19 08:10:10 +00002515 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002516 /*
2517 * when we play the delayed ref, also correct the
2518 * ref_mod on head
2519 */
2520 switch (ref->action) {
2521 case BTRFS_ADD_DELAYED_REF:
2522 case BTRFS_ADD_DELAYED_EXTENT:
2523 locked_ref->node.ref_mod -= ref->ref_mod;
2524 break;
2525 case BTRFS_DROP_DELAYED_REF:
2526 locked_ref->node.ref_mod += ref->ref_mod;
2527 break;
2528 default:
2529 WARN_ON(1);
2530 }
2531 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002532 spin_unlock(&locked_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002533
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002534 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002535 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002536
Miao Xie78a61842012-11-21 02:21:28 +00002537 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002538 if (ret) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002539 locked_ref->processing = 0;
Miao Xie093486c2012-12-19 08:10:10 +00002540 btrfs_delayed_ref_unlock(locked_ref);
2541 btrfs_put_delayed_ref(ref);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002542 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002543 return ret;
2544 }
2545
Miao Xie093486c2012-12-19 08:10:10 +00002546 /*
2547 * If this node is a head, that means all the refs in this head
2548 * have been dealt with, and we will pick the next head to deal
2549 * with, so we must unlock the head and drop it from the cluster
2550 * list before we release it.
2551 */
2552 if (btrfs_delayed_ref_is_head(ref)) {
Miao Xie093486c2012-12-19 08:10:10 +00002553 btrfs_delayed_ref_unlock(locked_ref);
2554 locked_ref = NULL;
2555 }
2556 btrfs_put_delayed_ref(ref);
2557 count++;
Chris Mason1887be62009-03-13 10:11:24 -04002558 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002559 }
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002560
2561 /*
2562 * We don't want to include ref heads since we can have empty ref heads
2563 * and those will drastically skew our runtime down since we just do
2564 * accounting, no actual extent tree updates.
2565 */
2566 if (actual_count > 0) {
2567 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2568 u64 avg;
2569
2570 /*
2571 * We weigh the current average higher than our current runtime
2572 * to avoid large swings in the average.
2573 */
2574 spin_lock(&delayed_refs->lock);
2575 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
2576 avg = div64_u64(avg, 4);
2577 fs_info->avg_delayed_ref_runtime = avg;
2578 spin_unlock(&delayed_refs->lock);
2579 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002580 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002581}
2582
Arne Jansen709c0482011-09-12 12:22:57 +02002583#ifdef SCRAMBLE_DELAYED_REFS
2584/*
2585 * Normally delayed refs get processed in ascending bytenr order. This
2586 * correlates in most cases to the order added. To expose dependencies on this
2587 * order, we start to process the tree in the middle instead of the beginning
2588 */
2589static u64 find_middle(struct rb_root *root)
2590{
2591 struct rb_node *n = root->rb_node;
2592 struct btrfs_delayed_ref_node *entry;
2593 int alt = 1;
2594 u64 middle;
2595 u64 first = 0, last = 0;
2596
2597 n = rb_first(root);
2598 if (n) {
2599 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2600 first = entry->bytenr;
2601 }
2602 n = rb_last(root);
2603 if (n) {
2604 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2605 last = entry->bytenr;
2606 }
2607 n = root->rb_node;
2608
2609 while (n) {
2610 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2611 WARN_ON(!entry->in_tree);
2612
2613 middle = entry->bytenr;
2614
2615 if (alt)
2616 n = n->rb_left;
2617 else
2618 n = n->rb_right;
2619
2620 alt = 1 - alt;
2621 }
2622 return middle;
2623}
2624#endif
2625
Josef Bacik1be41b72013-06-12 13:56:06 -04002626static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2627{
2628 u64 num_bytes;
2629
2630 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2631 sizeof(struct btrfs_extent_inline_ref));
2632 if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2633 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2634
2635 /*
2636 * We don't ever fill up leaves all the way so multiply by 2 just to be
2637 * closer to what we're really going to want to ouse.
2638 */
2639 return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
2640}
2641
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002642int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacik1be41b72013-06-12 13:56:06 -04002643 struct btrfs_root *root)
2644{
2645 struct btrfs_block_rsv *global_rsv;
2646 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2647 u64 num_bytes;
2648 int ret = 0;
2649
2650 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
2651 num_heads = heads_to_leaves(root, num_heads);
2652 if (num_heads > 1)
2653 num_bytes += (num_heads - 1) * root->leafsize;
2654 num_bytes <<= 1;
2655 global_rsv = &root->fs_info->global_block_rsv;
2656
2657 /*
2658 * If we can't allocate any more chunks lets make sure we have _lots_ of
2659 * wiggle room since running delayed refs can create more delayed refs.
2660 */
2661 if (global_rsv->space_info->full)
2662 num_bytes <<= 1;
2663
2664 spin_lock(&global_rsv->lock);
2665 if (global_rsv->reserved <= num_bytes)
2666 ret = 1;
2667 spin_unlock(&global_rsv->lock);
2668 return ret;
2669}
2670
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002671int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2672 struct btrfs_root *root)
2673{
2674 struct btrfs_fs_info *fs_info = root->fs_info;
2675 u64 num_entries =
2676 atomic_read(&trans->transaction->delayed_refs.num_entries);
2677 u64 avg_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002678 u64 val;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002679
2680 smp_mb();
2681 avg_runtime = fs_info->avg_delayed_ref_runtime;
Chris Masona79b7d42014-05-22 16:18:52 -07002682 val = num_entries * avg_runtime;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002683 if (num_entries * avg_runtime >= NSEC_PER_SEC)
2684 return 1;
Chris Masona79b7d42014-05-22 16:18:52 -07002685 if (val >= NSEC_PER_SEC / 2)
2686 return 2;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002687
2688 return btrfs_check_space_for_delayed_refs(trans, root);
2689}
2690
Chris Masona79b7d42014-05-22 16:18:52 -07002691struct async_delayed_refs {
2692 struct btrfs_root *root;
2693 int count;
2694 int error;
2695 int sync;
2696 struct completion wait;
2697 struct btrfs_work work;
2698};
2699
2700static void delayed_ref_async_start(struct btrfs_work *work)
2701{
2702 struct async_delayed_refs *async;
2703 struct btrfs_trans_handle *trans;
2704 int ret;
2705
2706 async = container_of(work, struct async_delayed_refs, work);
2707
2708 trans = btrfs_join_transaction(async->root);
2709 if (IS_ERR(trans)) {
2710 async->error = PTR_ERR(trans);
2711 goto done;
2712 }
2713
2714 /*
2715 * trans->sync means that when we call end_transaciton, we won't
2716 * wait on delayed refs
2717 */
2718 trans->sync = true;
2719 ret = btrfs_run_delayed_refs(trans, async->root, async->count);
2720 if (ret)
2721 async->error = ret;
2722
2723 ret = btrfs_end_transaction(trans, async->root);
2724 if (ret && !async->error)
2725 async->error = ret;
2726done:
2727 if (async->sync)
2728 complete(&async->wait);
2729 else
2730 kfree(async);
2731}
2732
2733int btrfs_async_run_delayed_refs(struct btrfs_root *root,
2734 unsigned long count, int wait)
2735{
2736 struct async_delayed_refs *async;
2737 int ret;
2738
2739 async = kmalloc(sizeof(*async), GFP_NOFS);
2740 if (!async)
2741 return -ENOMEM;
2742
2743 async->root = root->fs_info->tree_root;
2744 async->count = count;
2745 async->error = 0;
2746 if (wait)
2747 async->sync = 1;
2748 else
2749 async->sync = 0;
2750 init_completion(&async->wait);
2751
2752 btrfs_init_work(&async->work, delayed_ref_async_start,
2753 NULL, NULL);
2754
2755 btrfs_queue_work(root->fs_info->extent_workers, &async->work);
2756
2757 if (wait) {
2758 wait_for_completion(&async->wait);
2759 ret = async->error;
2760 kfree(async);
2761 return ret;
2762 }
2763 return 0;
2764}
2765
Chris Masonc3e69d52009-03-13 10:17:05 -04002766/*
2767 * this starts processing the delayed reference count updates and
2768 * extent insertions we have queued up so far. count can be
2769 * 0, which means to process everything in the tree at the start
2770 * of the run (but not newly added entries), or it can be some target
2771 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002772 *
2773 * Returns 0 on success or if called with an aborted transaction
2774 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002775 */
2776int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2777 struct btrfs_root *root, unsigned long count)
2778{
2779 struct rb_node *node;
2780 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002781 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002782 int ret;
2783 int run_all = count == (unsigned long)-1;
2784 int run_most = 0;
2785
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002786 /* We'll clean this up in btrfs_cleanup_transaction */
2787 if (trans->aborted)
2788 return 0;
2789
Chris Masonc3e69d52009-03-13 10:17:05 -04002790 if (root == root->fs_info->extent_root)
2791 root = root->fs_info->tree_root;
2792
2793 delayed_refs = &trans->transaction->delayed_refs;
Chris Masonbb721702013-01-29 18:44:12 -05002794 if (count == 0) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002795 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002796 run_most = 1;
2797 }
2798
Chris Masonc3e69d52009-03-13 10:17:05 -04002799again:
Arne Jansen709c0482011-09-12 12:22:57 +02002800#ifdef SCRAMBLE_DELAYED_REFS
2801 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2802#endif
Josef Bacikd7df2c72014-01-23 09:21:38 -05002803 ret = __btrfs_run_delayed_refs(trans, root, count);
2804 if (ret < 0) {
2805 btrfs_abort_transaction(trans, root, ret);
2806 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002807 }
2808
Chris Mason56bec292009-03-13 10:10:06 -04002809 if (run_all) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002810 if (!list_empty(&trans->new_bgs))
Josef Bacikea658ba2012-09-11 16:57:25 -04002811 btrfs_create_pending_block_groups(trans, root);
Josef Bacikea658ba2012-09-11 16:57:25 -04002812
Josef Bacikd7df2c72014-01-23 09:21:38 -05002813 spin_lock(&delayed_refs->lock);
Liu Boc46effa2013-10-14 12:59:45 +08002814 node = rb_first(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002815 if (!node) {
2816 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002817 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002818 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002819 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002820
2821 while (node) {
Liu Boc46effa2013-10-14 12:59:45 +08002822 head = rb_entry(node, struct btrfs_delayed_ref_head,
2823 href_node);
2824 if (btrfs_delayed_ref_is_head(&head->node)) {
2825 struct btrfs_delayed_ref_node *ref;
Chris Mason56bec292009-03-13 10:10:06 -04002826
Liu Boc46effa2013-10-14 12:59:45 +08002827 ref = &head->node;
Chris Mason56bec292009-03-13 10:10:06 -04002828 atomic_inc(&ref->refs);
2829
2830 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002831 /*
2832 * Mutex was contended, block until it's
2833 * released and try again
2834 */
Chris Mason56bec292009-03-13 10:10:06 -04002835 mutex_lock(&head->mutex);
2836 mutex_unlock(&head->mutex);
2837
2838 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002839 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002840 goto again;
Liu Boc46effa2013-10-14 12:59:45 +08002841 } else {
2842 WARN_ON(1);
Chris Mason56bec292009-03-13 10:10:06 -04002843 }
2844 node = rb_next(node);
2845 }
2846 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002847 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002848 goto again;
2849 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002850out:
Josef Bacikfcebe452014-05-13 17:30:47 -07002851 ret = btrfs_delayed_qgroup_accounting(trans, root->fs_info);
2852 if (ret)
2853 return ret;
Jan Schmidtedf39272012-06-28 18:04:55 +02002854 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002855 return 0;
2856}
2857
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002858int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2859 struct btrfs_root *root,
2860 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002861 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002862{
2863 struct btrfs_delayed_extent_op *extent_op;
2864 int ret;
2865
Miao Xie78a61842012-11-21 02:21:28 +00002866 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002867 if (!extent_op)
2868 return -ENOMEM;
2869
2870 extent_op->flags_to_set = flags;
2871 extent_op->update_flags = 1;
2872 extent_op->update_key = 0;
2873 extent_op->is_data = is_data ? 1 : 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002874 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002875
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002876 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2877 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002878 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002879 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002880 return ret;
2881}
2882
2883static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2884 struct btrfs_root *root,
2885 struct btrfs_path *path,
2886 u64 objectid, u64 offset, u64 bytenr)
2887{
2888 struct btrfs_delayed_ref_head *head;
2889 struct btrfs_delayed_ref_node *ref;
2890 struct btrfs_delayed_data_ref *data_ref;
2891 struct btrfs_delayed_ref_root *delayed_refs;
2892 struct rb_node *node;
2893 int ret = 0;
2894
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002895 delayed_refs = &trans->transaction->delayed_refs;
2896 spin_lock(&delayed_refs->lock);
2897 head = btrfs_find_delayed_ref_head(trans, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002898 if (!head) {
2899 spin_unlock(&delayed_refs->lock);
2900 return 0;
2901 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002902
2903 if (!mutex_trylock(&head->mutex)) {
2904 atomic_inc(&head->node.refs);
2905 spin_unlock(&delayed_refs->lock);
2906
David Sterbab3b4aa72011-04-21 01:20:15 +02002907 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002908
David Sterba8cc33e52011-05-02 15:29:25 +02002909 /*
2910 * Mutex was contended, block until it's released and let
2911 * caller try again
2912 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002913 mutex_lock(&head->mutex);
2914 mutex_unlock(&head->mutex);
2915 btrfs_put_delayed_ref(&head->node);
2916 return -EAGAIN;
2917 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002918 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002919
2920 spin_lock(&head->lock);
2921 node = rb_first(&head->ref_root);
2922 while (node) {
2923 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2924 node = rb_next(node);
2925
2926 /* If it's a shared ref we know a cross reference exists */
2927 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
2928 ret = 1;
2929 break;
2930 }
2931
2932 data_ref = btrfs_delayed_node_to_data_ref(ref);
2933
2934 /*
2935 * If our ref doesn't match the one we're currently looking at
2936 * then we have a cross reference.
2937 */
2938 if (data_ref->root != root->root_key.objectid ||
2939 data_ref->objectid != objectid ||
2940 data_ref->offset != offset) {
2941 ret = 1;
2942 break;
2943 }
2944 }
2945 spin_unlock(&head->lock);
2946 mutex_unlock(&head->mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002947 return ret;
2948}
2949
2950static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2951 struct btrfs_root *root,
2952 struct btrfs_path *path,
2953 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002954{
2955 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002956 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002957 struct btrfs_extent_data_ref *ref;
2958 struct btrfs_extent_inline_ref *iref;
2959 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002960 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002961 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002962 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002963
Chris Masonbe20aa92007-12-17 20:14:01 -05002964 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002965 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002966 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002967
Chris Masonbe20aa92007-12-17 20:14:01 -05002968 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2969 if (ret < 0)
2970 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002971 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002972
2973 ret = -ENOENT;
2974 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002975 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002976
Zheng Yan31840ae2008-09-23 13:14:14 -04002977 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002978 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002979 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002980
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002981 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002982 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002983
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002984 ret = 1;
2985 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2986#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2987 if (item_size < sizeof(*ei)) {
2988 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2989 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002990 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002991#endif
2992 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2993
2994 if (item_size != sizeof(*ei) +
2995 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2996 goto out;
2997
2998 if (btrfs_extent_generation(leaf, ei) <=
2999 btrfs_root_last_snapshot(&root->root_item))
3000 goto out;
3001
3002 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
3003 if (btrfs_extent_inline_ref_type(leaf, iref) !=
3004 BTRFS_EXTENT_DATA_REF_KEY)
3005 goto out;
3006
3007 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3008 if (btrfs_extent_refs(leaf, ei) !=
3009 btrfs_extent_data_ref_count(leaf, ref) ||
3010 btrfs_extent_data_ref_root(leaf, ref) !=
3011 root->root_key.objectid ||
3012 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3013 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3014 goto out;
3015
Yan Zhengf321e492008-07-30 09:26:11 -04003016 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05003017out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003018 return ret;
3019}
3020
3021int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
3022 struct btrfs_root *root,
3023 u64 objectid, u64 offset, u64 bytenr)
3024{
3025 struct btrfs_path *path;
3026 int ret;
3027 int ret2;
3028
3029 path = btrfs_alloc_path();
3030 if (!path)
3031 return -ENOENT;
3032
3033 do {
3034 ret = check_committed_ref(trans, root, path, objectid,
3035 offset, bytenr);
3036 if (ret && ret != -ENOENT)
3037 goto out;
3038
3039 ret2 = check_delayed_ref(trans, root, path, objectid,
3040 offset, bytenr);
3041 } while (ret2 == -EAGAIN);
3042
3043 if (ret2 && ret2 != -ENOENT) {
3044 ret = ret2;
3045 goto out;
3046 }
3047
3048 if (ret != -ENOENT || ret2 != -ENOENT)
3049 ret = 0;
3050out:
Yan Zhengf321e492008-07-30 09:26:11 -04003051 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003052 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3053 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04003054 return ret;
3055}
3056
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003057static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05003058 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003059 struct extent_buffer *buf,
Josef Bacike339a6b2014-07-02 10:54:25 -07003060 int full_backref, int inc)
Zheng Yan31840ae2008-09-23 13:14:14 -04003061{
3062 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003063 u64 num_bytes;
3064 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003065 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04003066 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003067 struct btrfs_key key;
3068 struct btrfs_file_extent_item *fi;
3069 int i;
3070 int level;
3071 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003072 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003073 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04003074
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003075#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3076 if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state)))
3077 return 0;
3078#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04003079 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003080 nritems = btrfs_header_nritems(buf);
3081 level = btrfs_header_level(buf);
3082
Miao Xie27cdeb72014-04-02 19:51:05 +08003083 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003084 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003085
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003086 if (inc)
3087 process_func = btrfs_inc_extent_ref;
3088 else
3089 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003090
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003091 if (full_backref)
3092 parent = buf->start;
3093 else
3094 parent = 0;
3095
Zheng Yan31840ae2008-09-23 13:14:14 -04003096 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003097 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003098 btrfs_item_key_to_cpu(buf, &key, i);
3099 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003100 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003101 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003102 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003103 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003104 BTRFS_FILE_EXTENT_INLINE)
3105 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003106 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3107 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003108 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003109
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003110 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3111 key.offset -= btrfs_file_extent_offset(buf, fi);
3112 ret = process_func(trans, root, bytenr, num_bytes,
3113 parent, ref_root, key.objectid,
Josef Bacike339a6b2014-07-02 10:54:25 -07003114 key.offset, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003115 if (ret)
3116 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003117 } else {
3118 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003119 num_bytes = btrfs_level_size(root, level - 1);
3120 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003121 parent, ref_root, level - 1, 0,
Josef Bacike339a6b2014-07-02 10:54:25 -07003122 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003123 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003124 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003125 }
3126 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003127 return 0;
3128fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003129 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003130}
3131
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003132int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003133 struct extent_buffer *buf, int full_backref)
Zheng Yan31840ae2008-09-23 13:14:14 -04003134{
Josef Bacike339a6b2014-07-02 10:54:25 -07003135 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003136}
Zheng Yan31840ae2008-09-23 13:14:14 -04003137
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003138int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacike339a6b2014-07-02 10:54:25 -07003139 struct extent_buffer *buf, int full_backref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003140{
Josef Bacike339a6b2014-07-02 10:54:25 -07003141 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003142}
3143
Chris Mason9078a3e2007-04-26 16:46:15 -04003144static int write_one_cache_group(struct btrfs_trans_handle *trans,
3145 struct btrfs_root *root,
3146 struct btrfs_path *path,
3147 struct btrfs_block_group_cache *cache)
3148{
3149 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003150 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003151 unsigned long bi;
3152 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003153
Chris Mason9078a3e2007-04-26 16:46:15 -04003154 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003155 if (ret < 0)
3156 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003157 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04003158
3159 leaf = path->nodes[0];
3160 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3161 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3162 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003163 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04003164fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003165 if (ret) {
3166 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04003167 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003168 }
Chris Mason9078a3e2007-04-26 16:46:15 -04003169 return 0;
3170
3171}
3172
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003173static struct btrfs_block_group_cache *
3174next_block_group(struct btrfs_root *root,
3175 struct btrfs_block_group_cache *cache)
3176{
3177 struct rb_node *node;
3178 spin_lock(&root->fs_info->block_group_cache_lock);
3179 node = rb_next(&cache->cache_node);
3180 btrfs_put_block_group(cache);
3181 if (node) {
3182 cache = rb_entry(node, struct btrfs_block_group_cache,
3183 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003184 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003185 } else
3186 cache = NULL;
3187 spin_unlock(&root->fs_info->block_group_cache_lock);
3188 return cache;
3189}
3190
Josef Bacik0af3d002010-06-21 14:48:16 -04003191static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3192 struct btrfs_trans_handle *trans,
3193 struct btrfs_path *path)
3194{
3195 struct btrfs_root *root = block_group->fs_info->tree_root;
3196 struct inode *inode = NULL;
3197 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003198 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04003199 int num_pages = 0;
3200 int retries = 0;
3201 int ret = 0;
3202
3203 /*
3204 * If this block group is smaller than 100 megs don't bother caching the
3205 * block group.
3206 */
3207 if (block_group->key.offset < (100 * 1024 * 1024)) {
3208 spin_lock(&block_group->lock);
3209 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3210 spin_unlock(&block_group->lock);
3211 return 0;
3212 }
3213
3214again:
3215 inode = lookup_free_space_inode(root, block_group, path);
3216 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3217 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003218 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003219 goto out;
3220 }
3221
3222 if (IS_ERR(inode)) {
3223 BUG_ON(retries);
3224 retries++;
3225
3226 if (block_group->ro)
3227 goto out_free;
3228
3229 ret = create_free_space_inode(root, trans, block_group, path);
3230 if (ret)
3231 goto out_free;
3232 goto again;
3233 }
3234
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003235 /* We've already setup this transaction, go ahead and exit */
3236 if (block_group->cache_generation == trans->transid &&
3237 i_size_read(inode)) {
3238 dcs = BTRFS_DC_SETUP;
3239 goto out_put;
3240 }
3241
Josef Bacik0af3d002010-06-21 14:48:16 -04003242 /*
3243 * We want to set the generation to 0, that way if anything goes wrong
3244 * from here on out we know not to trust this cache when we load up next
3245 * time.
3246 */
3247 BTRFS_I(inode)->generation = 0;
3248 ret = btrfs_update_inode(trans, root, inode);
3249 WARN_ON(ret);
3250
3251 if (i_size_read(inode) > 0) {
Miao Xie7b61cd92013-05-13 13:55:09 +00003252 ret = btrfs_check_trunc_cache_free_space(root,
3253 &root->fs_info->global_block_rsv);
3254 if (ret)
3255 goto out_put;
3256
Filipe David Borba Manana74514322013-09-20 14:46:51 +01003257 ret = btrfs_truncate_free_space_cache(root, trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003258 if (ret)
3259 goto out_put;
3260 }
3261
3262 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003263 if (block_group->cached != BTRFS_CACHE_FINISHED ||
Miao Xiee570fd22014-06-19 10:42:50 +08003264 !btrfs_test_opt(root, SPACE_CACHE) ||
3265 block_group->delalloc_bytes) {
Liu Bocf7c1ef2012-07-06 03:31:34 -06003266 /*
3267 * don't bother trying to write stuff out _if_
3268 * a) we're not cached,
3269 * b) we're with nospace_cache mount option.
3270 */
Josef Bacik2b209822010-12-03 13:17:53 -05003271 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003272 spin_unlock(&block_group->lock);
3273 goto out_put;
3274 }
3275 spin_unlock(&block_group->lock);
3276
Josef Bacik6fc823b2012-08-06 13:46:38 -06003277 /*
3278 * Try to preallocate enough space based on how big the block group is.
3279 * Keep in mind this has to include any pinned space which could end up
3280 * taking up quite a bit since it's not folded into the other space
3281 * cache.
3282 */
3283 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003284 if (!num_pages)
3285 num_pages = 1;
3286
Josef Bacik0af3d002010-06-21 14:48:16 -04003287 num_pages *= 16;
3288 num_pages *= PAGE_CACHE_SIZE;
3289
3290 ret = btrfs_check_data_free_space(inode, num_pages);
3291 if (ret)
3292 goto out_put;
3293
3294 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3295 num_pages, num_pages,
3296 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003297 if (!ret)
3298 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003299 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003300
Josef Bacik0af3d002010-06-21 14:48:16 -04003301out_put:
3302 iput(inode);
3303out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003304 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003305out:
3306 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003307 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003308 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003309 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003310 spin_unlock(&block_group->lock);
3311
3312 return ret;
3313}
3314
Chris Mason96b51792007-10-15 16:15:19 -04003315int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3316 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003317{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003318 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003319 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003320 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003321 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003322
3323 path = btrfs_alloc_path();
3324 if (!path)
3325 return -ENOMEM;
3326
Josef Bacik0af3d002010-06-21 14:48:16 -04003327again:
3328 while (1) {
3329 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3330 while (cache) {
3331 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3332 break;
3333 cache = next_block_group(root, cache);
3334 }
3335 if (!cache) {
3336 if (last == 0)
3337 break;
3338 last = 0;
3339 continue;
3340 }
3341 err = cache_save_setup(cache, trans, path);
3342 last = cache->key.objectid + cache->key.offset;
3343 btrfs_put_block_group(cache);
3344 }
3345
Chris Masond3977122009-01-05 21:25:51 -05003346 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003347 if (last == 0) {
3348 err = btrfs_run_delayed_refs(trans, root,
3349 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003350 if (err) /* File system offline */
3351 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003352 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003353
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003354 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3355 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003356 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3357 btrfs_put_block_group(cache);
3358 goto again;
3359 }
3360
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003361 if (cache->dirty)
3362 break;
3363 cache = next_block_group(root, cache);
3364 }
3365 if (!cache) {
3366 if (last == 0)
3367 break;
3368 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003369 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003370 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003371
Josef Bacik0cb59c92010-07-02 12:14:14 -04003372 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3373 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003374 cache->dirty = 0;
3375 last = cache->key.objectid + cache->key.offset;
3376
3377 err = write_one_cache_group(trans, root, path, cache);
Filipe David Borba Mananae84cc142013-09-09 19:49:43 +01003378 btrfs_put_block_group(cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003379 if (err) /* File system offline */
3380 goto out;
Chris Mason9078a3e2007-04-26 16:46:15 -04003381 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003382
Josef Bacik0cb59c92010-07-02 12:14:14 -04003383 while (1) {
3384 /*
3385 * I don't think this is needed since we're just marking our
3386 * preallocated extent as written, but just in case it can't
3387 * hurt.
3388 */
3389 if (last == 0) {
3390 err = btrfs_run_delayed_refs(trans, root,
3391 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003392 if (err) /* File system offline */
3393 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003394 }
3395
3396 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3397 while (cache) {
3398 /*
3399 * Really this shouldn't happen, but it could if we
3400 * couldn't write the entire preallocated extent and
3401 * splitting the extent resulted in a new block.
3402 */
3403 if (cache->dirty) {
3404 btrfs_put_block_group(cache);
3405 goto again;
3406 }
3407 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3408 break;
3409 cache = next_block_group(root, cache);
3410 }
3411 if (!cache) {
3412 if (last == 0)
3413 break;
3414 last = 0;
3415 continue;
3416 }
3417
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003418 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003419
3420 /*
3421 * If we didn't have an error then the cache state is still
3422 * NEED_WRITE, so we can set it to WRITTEN.
3423 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003424 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003425 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3426 last = cache->key.objectid + cache->key.offset;
3427 btrfs_put_block_group(cache);
3428 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003429out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003430
Chris Mason9078a3e2007-04-26 16:46:15 -04003431 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003432 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003433}
3434
Yan Zhengd2fb3432008-12-11 16:30:39 -05003435int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3436{
3437 struct btrfs_block_group_cache *block_group;
3438 int readonly = 0;
3439
3440 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3441 if (!block_group || block_group->ro)
3442 readonly = 1;
3443 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003444 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003445 return readonly;
3446}
3447
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003448static const char *alloc_name(u64 flags)
3449{
3450 switch (flags) {
3451 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
3452 return "mixed";
3453 case BTRFS_BLOCK_GROUP_METADATA:
3454 return "metadata";
3455 case BTRFS_BLOCK_GROUP_DATA:
3456 return "data";
3457 case BTRFS_BLOCK_GROUP_SYSTEM:
3458 return "system";
3459 default:
3460 WARN_ON(1);
3461 return "invalid-combination";
3462 };
3463}
3464
Chris Mason593060d2008-03-25 16:50:33 -04003465static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3466 u64 total_bytes, u64 bytes_used,
3467 struct btrfs_space_info **space_info)
3468{
3469 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003470 int i;
3471 int factor;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003472 int ret;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003473
3474 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3475 BTRFS_BLOCK_GROUP_RAID10))
3476 factor = 2;
3477 else
3478 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003479
3480 found = __find_space_info(info, flags);
3481 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003482 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003483 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003484 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003485 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003486 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003487 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003488 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003489 *space_info = found;
3490 return 0;
3491 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003492 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003493 if (!found)
3494 return -ENOMEM;
3495
Josef Bacikb150a4f2013-06-19 15:00:04 -04003496 ret = percpu_counter_init(&found->total_bytes_pinned, 0);
3497 if (ret) {
3498 kfree(found);
3499 return ret;
3500 }
3501
Jeff Mahoneyc1895442014-05-27 12:59:57 -04003502 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003503 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003504 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003505 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003506 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003507 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003508 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003509 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003510 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003511 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003512 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003513 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003514 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003515 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003516 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003517 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003518 found->flush = 0;
3519 init_waitqueue_head(&found->wait);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003520
3521 ret = kobject_init_and_add(&found->kobj, &space_info_ktype,
3522 info->space_info_kobj, "%s",
3523 alloc_name(found->flags));
3524 if (ret) {
3525 kfree(found);
3526 return ret;
3527 }
3528
Chris Mason593060d2008-03-25 16:50:33 -04003529 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003530 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003531 if (flags & BTRFS_BLOCK_GROUP_DATA)
3532 info->data_sinfo = found;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003533
3534 return ret;
Chris Mason593060d2008-03-25 16:50:33 -04003535}
3536
Chris Mason8790d502008-04-03 16:29:03 -04003537static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3538{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003539 u64 extra_flags = chunk_to_extended(flags) &
3540 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003541
Miao Xiede98ced2013-01-29 10:13:12 +00003542 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003543 if (flags & BTRFS_BLOCK_GROUP_DATA)
3544 fs_info->avail_data_alloc_bits |= extra_flags;
3545 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3546 fs_info->avail_metadata_alloc_bits |= extra_flags;
3547 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3548 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003549 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003550}
Chris Mason593060d2008-03-25 16:50:33 -04003551
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003552/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003553 * returns target flags in extended format or 0 if restripe for this
3554 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003555 *
3556 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003557 */
3558static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3559{
3560 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3561 u64 target = 0;
3562
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003563 if (!bctl)
3564 return 0;
3565
3566 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3567 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3568 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3569 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3570 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3571 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3572 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3573 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3574 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3575 }
3576
3577 return target;
3578}
3579
3580/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003581 * @flags: available profiles in extended format (see ctree.h)
3582 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003583 * Returns reduced profile in chunk format. If profile changing is in
3584 * progress (either running or paused) picks the target profile (if it's
3585 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003586 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003587static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003588{
Miao Xie95669972014-07-24 11:37:14 +08003589 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003590 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003591 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003592
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003593 /*
3594 * see if restripe for this chunk_type is in progress, if so
3595 * try to reduce to the target profile
3596 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003597 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003598 target = get_restripe_target(root->fs_info, flags);
3599 if (target) {
3600 /* pick target profile only if it's already available */
3601 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003602 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003603 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003604 }
3605 }
3606 spin_unlock(&root->fs_info->balance_lock);
3607
David Woodhouse53b381b2013-01-29 18:40:14 -05003608 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003609 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003610 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3611 BTRFS_BLOCK_GROUP_RAID5);
3612 if (num_devices < 3)
3613 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003614 if (num_devices < 4)
3615 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3616
David Woodhouse53b381b2013-01-29 18:40:14 -05003617 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3618 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3619 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3620 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003621
David Woodhouse53b381b2013-01-29 18:40:14 -05003622 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3623 tmp = BTRFS_BLOCK_GROUP_RAID6;
3624 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3625 tmp = BTRFS_BLOCK_GROUP_RAID5;
3626 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3627 tmp = BTRFS_BLOCK_GROUP_RAID10;
3628 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3629 tmp = BTRFS_BLOCK_GROUP_RAID1;
3630 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3631 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003632
David Woodhouse53b381b2013-01-29 18:40:14 -05003633 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003634}
3635
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003636static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003637{
Miao Xiede98ced2013-01-29 10:13:12 +00003638 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003639 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003640
3641 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003642 flags = orig_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003643 seq = read_seqbegin(&root->fs_info->profiles_lock);
3644
3645 if (flags & BTRFS_BLOCK_GROUP_DATA)
3646 flags |= root->fs_info->avail_data_alloc_bits;
3647 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3648 flags |= root->fs_info->avail_system_alloc_bits;
3649 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3650 flags |= root->fs_info->avail_metadata_alloc_bits;
3651 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003652
Yan, Zhengb742bb82010-05-16 10:46:24 -04003653 return btrfs_reduce_alloc_profile(root, flags);
3654}
Josef Bacik6a632092009-02-20 11:00:09 -05003655
Miao Xie6d07bce2011-01-05 10:07:31 +00003656u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003657{
3658 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003659 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003660
Yan, Zhengb742bb82010-05-16 10:46:24 -04003661 if (data)
3662 flags = BTRFS_BLOCK_GROUP_DATA;
3663 else if (root == root->fs_info->chunk_root)
3664 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3665 else
3666 flags = BTRFS_BLOCK_GROUP_METADATA;
3667
David Woodhouse53b381b2013-01-29 18:40:14 -05003668 ret = get_alloc_profile(root, flags);
3669 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003670}
3671
Josef Bacik6a632092009-02-20 11:00:09 -05003672/*
3673 * This will check the space that the inode allocates from to make sure we have
3674 * enough space for bytes.
3675 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003676int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003677{
3678 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003679 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003680 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003681 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003682 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003683
3684 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003685 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003686
Miao Xie9dced182013-10-25 17:33:36 +08003687 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003688 committed = 1;
Miao Xie9dced182013-10-25 17:33:36 +08003689 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04003690 }
3691
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003692 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003693 if (!data_sinfo)
3694 goto alloc;
3695
Josef Bacik6a632092009-02-20 11:00:09 -05003696again:
3697 /* make sure we have enough space to handle the data first */
3698 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003699 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3700 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3701 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003702
3703 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003704 struct btrfs_trans_handle *trans;
3705
Josef Bacik6a632092009-02-20 11:00:09 -05003706 /*
3707 * if we don't have enough free bytes in this space then we need
3708 * to alloc a new chunk.
3709 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003710 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003711 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003712
Chris Mason0e4f8f82011-04-15 16:05:44 -04003713 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003714 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003715alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003716 alloc_target = btrfs_get_alloc_profile(root, 1);
Miao Xie9dced182013-10-25 17:33:36 +08003717 /*
3718 * It is ugly that we don't call nolock join
3719 * transaction for the free space inode case here.
3720 * But it is safe because we only do the data space
3721 * reservation for the free space cache in the
3722 * transaction context, the common join transaction
3723 * just increase the counter of the current transaction
3724 * handler, doesn't try to acquire the trans_lock of
3725 * the fs.
3726 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003727 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003728 if (IS_ERR(trans))
3729 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003730
3731 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003732 alloc_target,
3733 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003734 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003735 if (ret < 0) {
3736 if (ret != -ENOSPC)
3737 return ret;
3738 else
3739 goto commit_trans;
3740 }
Chris Mason33b4d472009-09-22 14:45:50 -04003741
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003742 if (!data_sinfo)
3743 data_sinfo = fs_info->data_sinfo;
3744
Josef Bacik6a632092009-02-20 11:00:09 -05003745 goto again;
3746 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003747
3748 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04003749 * If we don't have enough pinned space to deal with this
3750 * allocation don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003751 */
Josef Bacikb150a4f2013-06-19 15:00:04 -04003752 if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
3753 bytes) < 0)
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003754 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003755 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003756
3757 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003758commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003759 if (!committed &&
3760 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003761 committed = 1;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003762
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003763 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003764 if (IS_ERR(trans))
3765 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003766 ret = btrfs_commit_transaction(trans, root);
3767 if (ret)
3768 return ret;
3769 goto again;
3770 }
3771
Jeff Mahoneycab45e22013-10-16 16:27:01 -04003772 trace_btrfs_space_reservation(root->fs_info,
3773 "space_info:enospc",
3774 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003775 return -ENOSPC;
3776 }
3777 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003778 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003779 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003780 spin_unlock(&data_sinfo->lock);
3781
Josef Bacik9ed74f22009-09-11 16:12:44 -04003782 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003783}
3784
3785/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003786 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003787 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003788void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003789{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003790 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003791 struct btrfs_space_info *data_sinfo;
3792
3793 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003794 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003795
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003796 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003797 spin_lock(&data_sinfo->lock);
Josef Bacik7ee9e442013-06-21 16:37:03 -04003798 WARN_ON(data_sinfo->bytes_may_use < bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05003799 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003800 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003801 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003802 spin_unlock(&data_sinfo->lock);
3803}
3804
Josef Bacik97e728d2009-04-21 17:40:57 -04003805static void force_metadata_allocation(struct btrfs_fs_info *info)
3806{
3807 struct list_head *head = &info->space_info;
3808 struct btrfs_space_info *found;
3809
3810 rcu_read_lock();
3811 list_for_each_entry_rcu(found, head, list) {
3812 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003813 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003814 }
3815 rcu_read_unlock();
3816}
3817
Miao Xie3c76cd82013-04-25 10:12:38 +00003818static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
3819{
3820 return (global->size << 1);
3821}
3822
Chris Masone5bc2452010-10-26 13:37:56 -04003823static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003824 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003825{
Josef Bacikfb25e912011-07-26 17:00:46 -04003826 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003827 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003828 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003829 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003830
Chris Mason0e4f8f82011-04-15 16:05:44 -04003831 if (force == CHUNK_ALLOC_FORCE)
3832 return 1;
3833
3834 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003835 * We need to take into account the global rsv because for all intents
3836 * and purposes it's used space. Don't worry about locking the
3837 * global_rsv, it doesn't change except when the transaction commits.
3838 */
Josef Bacik54338b52012-08-14 16:20:52 -04003839 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Miao Xie3c76cd82013-04-25 10:12:38 +00003840 num_allocated += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04003841
3842 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003843 * in limited mode, we want to have some free space up to
3844 * about 1% of the FS size.
3845 */
3846 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003847 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003848 thresh = max_t(u64, 64 * 1024 * 1024,
3849 div_factor_fine(thresh, 1));
3850
3851 if (num_bytes - num_allocated < thresh)
3852 return 1;
3853 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003854
Josef Bacik698d0082012-09-12 14:08:47 -04003855 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003856 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003857 return 1;
3858}
3859
Liu Bo15d1ff82012-03-29 09:57:44 -04003860static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3861{
3862 u64 num_dev;
3863
David Woodhouse53b381b2013-01-29 18:40:14 -05003864 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3865 BTRFS_BLOCK_GROUP_RAID0 |
3866 BTRFS_BLOCK_GROUP_RAID5 |
3867 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003868 num_dev = root->fs_info->fs_devices->rw_devices;
3869 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3870 num_dev = 2;
3871 else
3872 num_dev = 1; /* DUP or single */
3873
3874 /* metadata for updaing devices and chunk tree */
3875 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3876}
3877
3878static void check_system_chunk(struct btrfs_trans_handle *trans,
3879 struct btrfs_root *root, u64 type)
3880{
3881 struct btrfs_space_info *info;
3882 u64 left;
3883 u64 thresh;
3884
3885 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3886 spin_lock(&info->lock);
3887 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3888 info->bytes_reserved - info->bytes_readonly;
3889 spin_unlock(&info->lock);
3890
3891 thresh = get_system_chunk_thresh(root, type);
3892 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003893 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3894 left, thresh, type);
Liu Bo15d1ff82012-03-29 09:57:44 -04003895 dump_space_info(info, 0, 0);
3896 }
3897
3898 if (left < thresh) {
3899 u64 flags;
3900
3901 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3902 btrfs_alloc_chunk(trans, root, flags);
3903 }
3904}
3905
Chris Mason6324fbf2008-03-24 15:01:59 -04003906static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003907 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003908{
3909 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003910 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003911 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003912 int ret = 0;
3913
Josef Bacikc6b305a2012-12-18 09:16:16 -05003914 /* Don't re-enter if we're already allocating a chunk */
3915 if (trans->allocating_chunk)
3916 return -ENOSPC;
3917
Chris Mason6324fbf2008-03-24 15:01:59 -04003918 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003919 if (!space_info) {
3920 ret = update_space_info(extent_root->fs_info, flags,
3921 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003922 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003923 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003924 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003925
Josef Bacik6d741192011-04-11 20:20:11 -04003926again:
Josef Bacik25179202008-10-29 14:49:05 -04003927 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003928 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003929 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003930 if (space_info->full) {
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01003931 if (should_alloc_chunk(extent_root, space_info, force))
3932 ret = -ENOSPC;
3933 else
3934 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003935 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01003936 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04003937 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003938
Josef Bacik698d0082012-09-12 14:08:47 -04003939 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003940 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003941 return 0;
3942 } else if (space_info->chunk_alloc) {
3943 wait_for_alloc = 1;
3944 } else {
3945 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003946 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003947
Josef Bacik25179202008-10-29 14:49:05 -04003948 spin_unlock(&space_info->lock);
3949
Josef Bacik6d741192011-04-11 20:20:11 -04003950 mutex_lock(&fs_info->chunk_mutex);
3951
3952 /*
3953 * The chunk_mutex is held throughout the entirety of a chunk
3954 * allocation, so once we've acquired the chunk_mutex we know that the
3955 * other guy is done and we need to recheck and see if we should
3956 * allocate.
3957 */
3958 if (wait_for_alloc) {
3959 mutex_unlock(&fs_info->chunk_mutex);
3960 wait_for_alloc = 0;
3961 goto again;
3962 }
3963
Josef Bacikc6b305a2012-12-18 09:16:16 -05003964 trans->allocating_chunk = true;
3965
Josef Bacik97e728d2009-04-21 17:40:57 -04003966 /*
Josef Bacik67377732010-09-16 16:19:09 -04003967 * If we have mixed data/metadata chunks we want to make sure we keep
3968 * allocating mixed chunks instead of individual chunks.
3969 */
3970 if (btrfs_mixed_space_info(space_info))
3971 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3972
3973 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003974 * if we're doing a data chunk, go ahead and make sure that
3975 * we keep a reasonable number of metadata chunks allocated in the
3976 * FS as well.
3977 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003978 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003979 fs_info->data_chunk_allocations++;
3980 if (!(fs_info->data_chunk_allocations %
3981 fs_info->metadata_ratio))
3982 force_metadata_allocation(fs_info);
3983 }
3984
Liu Bo15d1ff82012-03-29 09:57:44 -04003985 /*
3986 * Check if we have enough space in SYSTEM chunk because we may need
3987 * to update devices.
3988 */
3989 check_system_chunk(trans, extent_root, flags);
3990
Yan Zheng2b820322008-11-17 21:11:30 -05003991 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003992 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003993
Josef Bacik9ed74f22009-09-11 16:12:44 -04003994 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003995 if (ret < 0 && ret != -ENOSPC)
3996 goto out;
Chris Masond3977122009-01-05 21:25:51 -05003997 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003998 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003999 else
4000 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04004001
Chris Mason0e4f8f82011-04-15 16:05:44 -04004002 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00004003out:
Josef Bacik6d741192011-04-11 20:20:11 -04004004 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004005 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03004006 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04004007 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04004008}
4009
Josef Bacika80c8dc2012-09-06 16:59:33 -04004010static int can_overcommit(struct btrfs_root *root,
4011 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00004012 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004013{
Josef Bacik96f1bb52013-01-30 17:02:51 -05004014 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004015 u64 profile = btrfs_get_alloc_profile(root, 0);
Miao Xie3c76cd82013-04-25 10:12:38 +00004016 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004017 u64 avail;
4018 u64 used;
4019
4020 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05004021 space_info->bytes_pinned + space_info->bytes_readonly;
4022
Josef Bacik96f1bb52013-01-30 17:02:51 -05004023 /*
4024 * We only want to allow over committing if we have lots of actual space
4025 * free, but if we don't have enough space to handle the global reserve
4026 * space then we could end up having a real enospc problem when trying
4027 * to allocate a chunk or some other such important allocation.
4028 */
Miao Xie3c76cd82013-04-25 10:12:38 +00004029 spin_lock(&global_rsv->lock);
4030 space_size = calc_global_rsv_need_space(global_rsv);
4031 spin_unlock(&global_rsv->lock);
4032 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05004033 return 0;
4034
4035 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004036
4037 spin_lock(&root->fs_info->free_chunk_lock);
4038 avail = root->fs_info->free_chunk_space;
4039 spin_unlock(&root->fs_info->free_chunk_lock);
4040
4041 /*
4042 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05004043 * space is actually useable. For raid56, the space info used
4044 * doesn't include the parity drive, so we don't have to
4045 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04004046 */
4047 if (profile & (BTRFS_BLOCK_GROUP_DUP |
4048 BTRFS_BLOCK_GROUP_RAID1 |
4049 BTRFS_BLOCK_GROUP_RAID10))
4050 avail >>= 1;
4051
4052 /*
Miao Xie561c2942012-10-16 11:32:18 +00004053 * If we aren't flushing all things, let us overcommit up to
4054 * 1/2th of the space. If we can flush, don't let us overcommit
4055 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04004056 */
Miao Xie08e007d2012-10-16 11:33:38 +00004057 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04004058 avail >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004059 else
Josef Bacik14575ae2013-09-17 10:48:00 -04004060 avail >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04004061
Josef Bacik14575ae2013-09-17 10:48:00 -04004062 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dc2012-09-06 16:59:33 -04004063 return 1;
4064 return 0;
4065}
4066
Eric Sandeen48a3b632013-04-25 20:41:01 +00004067static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
Miao Xie6c255e62014-03-06 13:55:01 +08004068 unsigned long nr_pages, int nr_items)
Miao Xieda633a42012-12-20 11:19:09 +00004069{
4070 struct super_block *sb = root->fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00004071
Josef Bacik925a6ef2013-06-20 12:31:27 -04004072 if (down_read_trylock(&sb->s_umount)) {
4073 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4074 up_read(&sb->s_umount);
4075 } else {
Miao Xieda633a42012-12-20 11:19:09 +00004076 /*
4077 * We needn't worry the filesystem going from r/w to r/o though
4078 * we don't acquire ->s_umount mutex, because the filesystem
4079 * should guarantee the delalloc inodes list be empty after
4080 * the filesystem is readonly(all dirty pages are written to
4081 * the disk).
4082 */
Miao Xie6c255e62014-03-06 13:55:01 +08004083 btrfs_start_delalloc_roots(root->fs_info, 0, nr_items);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004084 if (!current->journal_info)
Miao Xie6c255e62014-03-06 13:55:01 +08004085 btrfs_wait_ordered_roots(root->fs_info, nr_items);
Miao Xieda633a42012-12-20 11:19:09 +00004086 }
4087}
4088
Miao Xie18cd8ea2013-11-04 23:13:22 +08004089static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim)
4090{
4091 u64 bytes;
4092 int nr;
4093
4094 bytes = btrfs_calc_trans_metadata_size(root, 1);
4095 nr = (int)div64_u64(to_reclaim, bytes);
4096 if (!nr)
4097 nr = 1;
4098 return nr;
4099}
4100
Miao Xiec61a16a2013-11-04 23:13:23 +08004101#define EXTENT_SIZE_PER_ITEM (256 * 1024)
4102
Yan, Zheng5da9d012010-05-16 10:46:25 -04004103/*
4104 * shrink metadata reservation for delalloc
4105 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04004106static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4107 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004108{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004109 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004110 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004111 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004112 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004113 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004114 long time_left;
Miao Xied3ee29e32013-11-04 23:13:20 +08004115 unsigned long nr_pages;
4116 int loops;
Miao Xieb0244192013-11-04 23:13:25 +08004117 int items;
Miao Xie08e007d2012-10-16 11:33:38 +00004118 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004119
Miao Xiec61a16a2013-11-04 23:13:23 +08004120 /* Calc the number of the pages we need flush for space reservation */
Miao Xieb0244192013-11-04 23:13:25 +08004121 items = calc_reclaim_items_nr(root, to_reclaim);
4122 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004123
Josef Bacik663350a2011-11-03 22:54:25 -04004124 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004125 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004126 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04004127
Miao Xie963d6782013-01-29 10:10:51 +00004128 delalloc_bytes = percpu_counter_sum_positive(
4129 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004130 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004131 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004132 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004133 if (wait_ordered)
Miao Xieb0244192013-11-04 23:13:25 +08004134 btrfs_wait_ordered_roots(root->fs_info, items);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004135 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004136 }
4137
Miao Xied3ee29e32013-11-04 23:13:20 +08004138 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004139 while (delalloc_bytes && loops < 3) {
4140 max_reclaim = min(delalloc_bytes, to_reclaim);
4141 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xie6c255e62014-03-06 13:55:01 +08004142 btrfs_writeback_inodes_sb_nr(root, nr_pages, items);
Josef Bacikdea31f52012-09-06 16:47:00 -04004143 /*
4144 * We need to wait for the async pages to actually start before
4145 * we do anything.
4146 */
Miao Xie9f3a0742013-11-04 23:13:24 +08004147 max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages);
4148 if (!max_reclaim)
4149 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004150
Miao Xie9f3a0742013-11-04 23:13:24 +08004151 if (max_reclaim <= nr_pages)
4152 max_reclaim = 0;
4153 else
4154 max_reclaim -= nr_pages;
4155
4156 wait_event(root->fs_info->async_submit_wait,
4157 atomic_read(&root->fs_info->async_delalloc_pages) <=
4158 (int)max_reclaim);
4159skip_async:
Miao Xie08e007d2012-10-16 11:33:38 +00004160 if (!trans)
4161 flush = BTRFS_RESERVE_FLUSH_ALL;
4162 else
4163 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04004164 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00004165 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004166 spin_unlock(&space_info->lock);
4167 break;
4168 }
Josef Bacik0019f102010-10-15 15:18:40 -04004169 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004170
Chris Mason36e39c42011-03-12 07:08:42 -05004171 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004172 if (wait_ordered && !trans) {
Miao Xieb0244192013-11-04 23:13:25 +08004173 btrfs_wait_ordered_roots(root->fs_info, items);
Josef Bacikf104d042011-10-14 13:56:58 -04004174 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004175 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004176 if (time_left)
4177 break;
4178 }
Miao Xie963d6782013-01-29 10:10:51 +00004179 delalloc_bytes = percpu_counter_sum_positive(
4180 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004181 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004182}
4183
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004184/**
Josef Bacik663350a2011-11-03 22:54:25 -04004185 * maybe_commit_transaction - possibly commit the transaction if its ok to
4186 * @root - the root we're allocating for
4187 * @bytes - the number of bytes we want to reserve
4188 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004189 *
Josef Bacik663350a2011-11-03 22:54:25 -04004190 * This will check to make sure that committing the transaction will actually
4191 * get us somewhere and then commit the transaction if it does. Otherwise it
4192 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004193 */
Josef Bacik663350a2011-11-03 22:54:25 -04004194static int may_commit_transaction(struct btrfs_root *root,
4195 struct btrfs_space_info *space_info,
4196 u64 bytes, int force)
4197{
4198 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4199 struct btrfs_trans_handle *trans;
4200
4201 trans = (struct btrfs_trans_handle *)current->journal_info;
4202 if (trans)
4203 return -EAGAIN;
4204
4205 if (force)
4206 goto commit;
4207
4208 /* See if there is enough pinned space to make this reservation */
Josef Bacikb150a4f2013-06-19 15:00:04 -04004209 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Miao Xie0424c542014-03-06 13:54:59 +08004210 bytes) >= 0)
Josef Bacik663350a2011-11-03 22:54:25 -04004211 goto commit;
Josef Bacik663350a2011-11-03 22:54:25 -04004212
4213 /*
4214 * See if there is some space in the delayed insertion reservation for
4215 * this reservation.
4216 */
4217 if (space_info != delayed_rsv->space_info)
4218 return -ENOSPC;
4219
4220 spin_lock(&delayed_rsv->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004221 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4222 bytes - delayed_rsv->size) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004223 spin_unlock(&delayed_rsv->lock);
4224 return -ENOSPC;
4225 }
4226 spin_unlock(&delayed_rsv->lock);
4227
4228commit:
4229 trans = btrfs_join_transaction(root);
4230 if (IS_ERR(trans))
4231 return -ENOSPC;
4232
4233 return btrfs_commit_transaction(trans, root);
4234}
4235
Josef Bacik96c3f432012-06-21 14:05:49 -04004236enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04004237 FLUSH_DELAYED_ITEMS_NR = 1,
4238 FLUSH_DELAYED_ITEMS = 2,
4239 FLUSH_DELALLOC = 3,
4240 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04004241 ALLOC_CHUNK = 5,
4242 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04004243};
4244
4245static int flush_space(struct btrfs_root *root,
4246 struct btrfs_space_info *space_info, u64 num_bytes,
4247 u64 orig_bytes, int state)
4248{
4249 struct btrfs_trans_handle *trans;
4250 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004251 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004252
4253 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004254 case FLUSH_DELAYED_ITEMS_NR:
4255 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08004256 if (state == FLUSH_DELAYED_ITEMS_NR)
4257 nr = calc_reclaim_items_nr(root, num_bytes) * 2;
4258 else
Josef Bacik96c3f432012-06-21 14:05:49 -04004259 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004260
Josef Bacik96c3f432012-06-21 14:05:49 -04004261 trans = btrfs_join_transaction(root);
4262 if (IS_ERR(trans)) {
4263 ret = PTR_ERR(trans);
4264 break;
4265 }
4266 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4267 btrfs_end_transaction(trans, root);
4268 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004269 case FLUSH_DELALLOC:
4270 case FLUSH_DELALLOC_WAIT:
Miao Xie24af7dd2014-03-06 13:55:00 +08004271 shrink_delalloc(root, num_bytes * 2, orig_bytes,
Josef Bacik67b0fd62012-09-24 13:42:00 -04004272 state == FLUSH_DELALLOC_WAIT);
4273 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004274 case ALLOC_CHUNK:
4275 trans = btrfs_join_transaction(root);
4276 if (IS_ERR(trans)) {
4277 ret = PTR_ERR(trans);
4278 break;
4279 }
4280 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004281 btrfs_get_alloc_profile(root, 0),
4282 CHUNK_ALLOC_NO_FORCE);
4283 btrfs_end_transaction(trans, root);
4284 if (ret == -ENOSPC)
4285 ret = 0;
4286 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004287 case COMMIT_TRANS:
4288 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4289 break;
4290 default:
4291 ret = -ENOSPC;
4292 break;
4293 }
4294
4295 return ret;
4296}
Miao Xie21c7e752014-05-13 17:29:04 -07004297
4298static inline u64
4299btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
4300 struct btrfs_space_info *space_info)
4301{
4302 u64 used;
4303 u64 expected;
4304 u64 to_reclaim;
4305
4306 to_reclaim = min_t(u64, num_online_cpus() * 1024 * 1024,
4307 16 * 1024 * 1024);
4308 spin_lock(&space_info->lock);
4309 if (can_overcommit(root, space_info, to_reclaim,
4310 BTRFS_RESERVE_FLUSH_ALL)) {
4311 to_reclaim = 0;
4312 goto out;
4313 }
4314
4315 used = space_info->bytes_used + space_info->bytes_reserved +
4316 space_info->bytes_pinned + space_info->bytes_readonly +
4317 space_info->bytes_may_use;
4318 if (can_overcommit(root, space_info, 1024 * 1024,
4319 BTRFS_RESERVE_FLUSH_ALL))
4320 expected = div_factor_fine(space_info->total_bytes, 95);
4321 else
4322 expected = div_factor_fine(space_info->total_bytes, 90);
4323
4324 if (used > expected)
4325 to_reclaim = used - expected;
4326 else
4327 to_reclaim = 0;
4328 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
4329 space_info->bytes_reserved);
4330out:
4331 spin_unlock(&space_info->lock);
4332
4333 return to_reclaim;
4334}
4335
4336static inline int need_do_async_reclaim(struct btrfs_space_info *space_info,
4337 struct btrfs_fs_info *fs_info, u64 used)
4338{
4339 return (used >= div_factor_fine(space_info->total_bytes, 98) &&
4340 !btrfs_fs_closing(fs_info) &&
4341 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
4342}
4343
4344static int btrfs_need_do_async_reclaim(struct btrfs_space_info *space_info,
4345 struct btrfs_fs_info *fs_info)
4346{
4347 u64 used;
4348
4349 spin_lock(&space_info->lock);
4350 used = space_info->bytes_used + space_info->bytes_reserved +
4351 space_info->bytes_pinned + space_info->bytes_readonly +
4352 space_info->bytes_may_use;
4353 if (need_do_async_reclaim(space_info, fs_info, used)) {
4354 spin_unlock(&space_info->lock);
4355 return 1;
4356 }
4357 spin_unlock(&space_info->lock);
4358
4359 return 0;
4360}
4361
4362static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
4363{
4364 struct btrfs_fs_info *fs_info;
4365 struct btrfs_space_info *space_info;
4366 u64 to_reclaim;
4367 int flush_state;
4368
4369 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
4370 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4371
4372 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
4373 space_info);
4374 if (!to_reclaim)
4375 return;
4376
4377 flush_state = FLUSH_DELAYED_ITEMS_NR;
4378 do {
4379 flush_space(fs_info->fs_root, space_info, to_reclaim,
4380 to_reclaim, flush_state);
4381 flush_state++;
4382 if (!btrfs_need_do_async_reclaim(space_info, fs_info))
4383 return;
4384 } while (flush_state <= COMMIT_TRANS);
4385
4386 if (btrfs_need_do_async_reclaim(space_info, fs_info))
4387 queue_work(system_unbound_wq, work);
4388}
4389
4390void btrfs_init_async_reclaim_work(struct work_struct *work)
4391{
4392 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
4393}
4394
Josef Bacik663350a2011-11-03 22:54:25 -04004395/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004396 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4397 * @root - the root we're allocating for
4398 * @block_rsv - the block_rsv we're allocating for
4399 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004400 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004401 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004402 * This will reserve orgi_bytes number of bytes from the space info associated
4403 * with the block_rsv. If there is not enough space it will make an attempt to
4404 * flush out space to make room. It will do this by flushing delalloc if
4405 * possible or committing the transaction. If flush is 0 then no attempts to
4406 * regain reservations will be made and this will fail if there is not enough
4407 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004408 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004409static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004410 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004411 u64 orig_bytes,
4412 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004413{
4414 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004415 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004416 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004417 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004418 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004419 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004420
4421again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004422 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004423 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004424 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004425 * We only want to wait if somebody other than us is flushing and we
4426 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004427 */
Miao Xie08e007d2012-10-16 11:33:38 +00004428 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4429 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004430 spin_unlock(&space_info->lock);
4431 /*
4432 * If we have a trans handle we can't wait because the flusher
4433 * may have to commit the transaction, which would mean we would
4434 * deadlock since we are waiting for the flusher to finish, but
4435 * hold the current transaction open.
4436 */
Josef Bacik663350a2011-11-03 22:54:25 -04004437 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004438 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004439 ret = wait_event_killable(space_info->wait, !space_info->flush);
4440 /* Must have been killed, return */
4441 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004442 return -EINTR;
4443
4444 spin_lock(&space_info->lock);
4445 }
4446
4447 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004448 used = space_info->bytes_used + space_info->bytes_reserved +
4449 space_info->bytes_pinned + space_info->bytes_readonly +
4450 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004451
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004452 /*
4453 * The idea here is that we've not already over-reserved the block group
4454 * then we can go ahead and save our reservation first and then start
4455 * flushing if we need to. Otherwise if we've already overcommitted
4456 * lets start flushing stuff first and then come back and try to make
4457 * our reservation.
4458 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004459 if (used <= space_info->total_bytes) {
4460 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004461 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004462 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004463 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004464 ret = 0;
4465 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004466 /*
4467 * Ok set num_bytes to orig_bytes since we aren't
4468 * overocmmitted, this way we only try and reclaim what
4469 * we need.
4470 */
4471 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004472 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004473 } else {
4474 /*
4475 * Ok we're over committed, set num_bytes to the overcommitted
4476 * amount plus the amount of bytes that we need for this
4477 * reservation.
4478 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004479 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004480 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004481 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004482
Josef Bacik44734ed2012-09-28 16:04:19 -04004483 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4484 space_info->bytes_may_use += orig_bytes;
4485 trace_btrfs_space_reservation(root->fs_info, "space_info",
4486 space_info->flags, orig_bytes,
4487 1);
4488 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004489 }
4490
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004491 /*
4492 * Couldn't make our reservation, save our place so while we're trying
4493 * to reclaim space we can actually use it instead of somebody else
4494 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004495 *
4496 * We make the other tasks wait for the flush only when we can flush
4497 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004498 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004499 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004500 flushing = true;
4501 space_info->flush = 1;
Miao Xie21c7e752014-05-13 17:29:04 -07004502 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
4503 used += orig_bytes;
4504 if (need_do_async_reclaim(space_info, root->fs_info, used) &&
4505 !work_busy(&root->fs_info->async_reclaim_work))
4506 queue_work(system_unbound_wq,
4507 &root->fs_info->async_reclaim_work);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004508 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004509 spin_unlock(&space_info->lock);
4510
Miao Xie08e007d2012-10-16 11:33:38 +00004511 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004512 goto out;
4513
Josef Bacik96c3f432012-06-21 14:05:49 -04004514 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4515 flush_state);
4516 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004517
4518 /*
4519 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4520 * would happen. So skip delalloc flush.
4521 */
4522 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4523 (flush_state == FLUSH_DELALLOC ||
4524 flush_state == FLUSH_DELALLOC_WAIT))
4525 flush_state = ALLOC_CHUNK;
4526
Josef Bacik96c3f432012-06-21 14:05:49 -04004527 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004528 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004529 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4530 flush_state < COMMIT_TRANS)
4531 goto again;
4532 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4533 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004534 goto again;
4535
4536out:
Josef Bacik5d803662013-02-07 16:06:02 -05004537 if (ret == -ENOSPC &&
4538 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4539 struct btrfs_block_rsv *global_rsv =
4540 &root->fs_info->global_block_rsv;
4541
4542 if (block_rsv != global_rsv &&
4543 !block_rsv_use_bytes(global_rsv, orig_bytes))
4544 ret = 0;
4545 }
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004546 if (ret == -ENOSPC)
4547 trace_btrfs_space_reservation(root->fs_info,
4548 "space_info:enospc",
4549 space_info->flags, orig_bytes, 1);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004550 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004551 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004552 space_info->flush = 0;
4553 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004554 spin_unlock(&space_info->lock);
4555 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004556 return ret;
4557}
4558
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004559static struct btrfs_block_rsv *get_block_rsv(
4560 const struct btrfs_trans_handle *trans,
4561 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004562{
Josef Bacik4c13d752011-08-30 11:31:29 -04004563 struct btrfs_block_rsv *block_rsv = NULL;
4564
Miao Xie27cdeb72014-04-02 19:51:05 +08004565 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Josef Bacik0e721102012-06-26 16:13:18 -04004566 block_rsv = trans->block_rsv;
4567
4568 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004569 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004570
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004571 if (root == root->fs_info->uuid_root)
4572 block_rsv = trans->block_rsv;
4573
Josef Bacik4c13d752011-08-30 11:31:29 -04004574 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004575 block_rsv = root->block_rsv;
4576
4577 if (!block_rsv)
4578 block_rsv = &root->fs_info->empty_block_rsv;
4579
4580 return block_rsv;
4581}
4582
4583static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4584 u64 num_bytes)
4585{
4586 int ret = -ENOSPC;
4587 spin_lock(&block_rsv->lock);
4588 if (block_rsv->reserved >= num_bytes) {
4589 block_rsv->reserved -= num_bytes;
4590 if (block_rsv->reserved < block_rsv->size)
4591 block_rsv->full = 0;
4592 ret = 0;
4593 }
4594 spin_unlock(&block_rsv->lock);
4595 return ret;
4596}
4597
4598static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4599 u64 num_bytes, int update_size)
4600{
4601 spin_lock(&block_rsv->lock);
4602 block_rsv->reserved += num_bytes;
4603 if (update_size)
4604 block_rsv->size += num_bytes;
4605 else if (block_rsv->reserved >= block_rsv->size)
4606 block_rsv->full = 1;
4607 spin_unlock(&block_rsv->lock);
4608}
4609
Josef Bacikd52be812013-05-29 14:54:47 -04004610int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
4611 struct btrfs_block_rsv *dest, u64 num_bytes,
4612 int min_factor)
4613{
4614 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4615 u64 min_bytes;
4616
4617 if (global_rsv->space_info != dest->space_info)
4618 return -ENOSPC;
4619
4620 spin_lock(&global_rsv->lock);
4621 min_bytes = div_factor(global_rsv->size, min_factor);
4622 if (global_rsv->reserved < min_bytes + num_bytes) {
4623 spin_unlock(&global_rsv->lock);
4624 return -ENOSPC;
4625 }
4626 global_rsv->reserved -= num_bytes;
4627 if (global_rsv->reserved < global_rsv->size)
4628 global_rsv->full = 0;
4629 spin_unlock(&global_rsv->lock);
4630
4631 block_rsv_add_bytes(dest, num_bytes, 1);
4632 return 0;
4633}
4634
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004635static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4636 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004637 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004638{
4639 struct btrfs_space_info *space_info = block_rsv->space_info;
4640
4641 spin_lock(&block_rsv->lock);
4642 if (num_bytes == (u64)-1)
4643 num_bytes = block_rsv->size;
4644 block_rsv->size -= num_bytes;
4645 if (block_rsv->reserved >= block_rsv->size) {
4646 num_bytes = block_rsv->reserved - block_rsv->size;
4647 block_rsv->reserved = block_rsv->size;
4648 block_rsv->full = 1;
4649 } else {
4650 num_bytes = 0;
4651 }
4652 spin_unlock(&block_rsv->lock);
4653
4654 if (num_bytes > 0) {
4655 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004656 spin_lock(&dest->lock);
4657 if (!dest->full) {
4658 u64 bytes_to_add;
4659
4660 bytes_to_add = dest->size - dest->reserved;
4661 bytes_to_add = min(num_bytes, bytes_to_add);
4662 dest->reserved += bytes_to_add;
4663 if (dest->reserved >= dest->size)
4664 dest->full = 1;
4665 num_bytes -= bytes_to_add;
4666 }
4667 spin_unlock(&dest->lock);
4668 }
4669 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004670 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004671 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004672 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004673 space_info->flags, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004674 spin_unlock(&space_info->lock);
4675 }
4676 }
4677}
4678
4679static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4680 struct btrfs_block_rsv *dst, u64 num_bytes)
4681{
4682 int ret;
4683
4684 ret = block_rsv_use_bytes(src, num_bytes);
4685 if (ret)
4686 return ret;
4687
4688 block_rsv_add_bytes(dst, num_bytes, 1);
4689 return 0;
4690}
4691
Miao Xie66d8f3d2012-09-06 04:02:28 -06004692void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004693{
4694 memset(rsv, 0, sizeof(*rsv));
4695 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004696 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004697}
4698
Miao Xie66d8f3d2012-09-06 04:02:28 -06004699struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4700 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004701{
4702 struct btrfs_block_rsv *block_rsv;
4703 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004704
4705 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4706 if (!block_rsv)
4707 return NULL;
4708
Miao Xie66d8f3d2012-09-06 04:02:28 -06004709 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004710 block_rsv->space_info = __find_space_info(fs_info,
4711 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004712 return block_rsv;
4713}
4714
4715void btrfs_free_block_rsv(struct btrfs_root *root,
4716 struct btrfs_block_rsv *rsv)
4717{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004718 if (!rsv)
4719 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004720 btrfs_block_rsv_release(root, rsv, (u64)-1);
4721 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004722}
4723
Miao Xie08e007d2012-10-16 11:33:38 +00004724int btrfs_block_rsv_add(struct btrfs_root *root,
4725 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4726 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004727{
4728 int ret;
4729
4730 if (num_bytes == 0)
4731 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004732
Miao Xie61b520a2011-11-10 20:45:05 -05004733 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004734 if (!ret) {
4735 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4736 return 0;
4737 }
4738
Yan, Zhengf0486c62010-05-16 10:46:25 -04004739 return ret;
4740}
4741
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004742int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004743 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004744{
4745 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004746 int ret = -ENOSPC;
4747
4748 if (!block_rsv)
4749 return 0;
4750
4751 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004752 num_bytes = div_factor(block_rsv->size, min_factor);
4753 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004754 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004755 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004756
Josef Bacik36ba0222011-10-18 12:15:48 -04004757 return ret;
4758}
4759
Miao Xie08e007d2012-10-16 11:33:38 +00004760int btrfs_block_rsv_refill(struct btrfs_root *root,
4761 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4762 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004763{
4764 u64 num_bytes = 0;
4765 int ret = -ENOSPC;
4766
4767 if (!block_rsv)
4768 return 0;
4769
4770 spin_lock(&block_rsv->lock);
4771 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004772 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004773 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004774 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004775 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004776 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004777
Yan, Zhengf0486c62010-05-16 10:46:25 -04004778 if (!ret)
4779 return 0;
4780
Miao Xieaa38a712011-11-18 17:43:00 +08004781 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004782 if (!ret) {
4783 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004784 return 0;
4785 }
4786
Josef Bacik13553e52011-08-08 13:33:21 -04004787 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004788}
4789
4790int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4791 struct btrfs_block_rsv *dst_rsv,
4792 u64 num_bytes)
4793{
4794 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4795}
4796
4797void btrfs_block_rsv_release(struct btrfs_root *root,
4798 struct btrfs_block_rsv *block_rsv,
4799 u64 num_bytes)
4800{
4801 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Liu Bo17504582013-12-29 21:44:50 +08004802 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04004803 block_rsv->space_info != global_rsv->space_info)
4804 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004805 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4806 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004807}
4808
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004809/*
4810 * helper to calculate size of global block reservation.
4811 * the desired value is sum of space used by extent tree,
4812 * checksum tree and root tree
4813 */
4814static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4815{
4816 struct btrfs_space_info *sinfo;
4817 u64 num_bytes;
4818 u64 meta_used;
4819 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004820 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004821
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004822 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4823 spin_lock(&sinfo->lock);
4824 data_used = sinfo->bytes_used;
4825 spin_unlock(&sinfo->lock);
4826
4827 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4828 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004829 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4830 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004831 meta_used = sinfo->bytes_used;
4832 spin_unlock(&sinfo->lock);
4833
4834 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4835 csum_size * 2;
4836 num_bytes += div64_u64(data_used + meta_used, 50);
4837
4838 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004839 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004840
4841 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4842}
4843
4844static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4845{
4846 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4847 struct btrfs_space_info *sinfo = block_rsv->space_info;
4848 u64 num_bytes;
4849
4850 num_bytes = calc_global_metadata_size(fs_info);
4851
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004852 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004853 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004854
Josef Bacikfdf30d12013-03-26 15:31:45 -04004855 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004856
4857 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004858 sinfo->bytes_reserved + sinfo->bytes_readonly +
4859 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004860
4861 if (sinfo->total_bytes > num_bytes) {
4862 num_bytes = sinfo->total_bytes - num_bytes;
4863 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004864 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004865 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004866 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004867 }
4868
4869 if (block_rsv->reserved >= block_rsv->size) {
4870 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004871 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004872 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004873 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004874 block_rsv->reserved = block_rsv->size;
4875 block_rsv->full = 1;
4876 }
David Sterba182608c2011-05-05 13:13:16 +02004877
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004878 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004879 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004880}
4881
Yan, Zhengf0486c62010-05-16 10:46:25 -04004882static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4883{
4884 struct btrfs_space_info *space_info;
4885
4886 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4887 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004888
4889 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004890 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004891 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004892 fs_info->trans_block_rsv.space_info = space_info;
4893 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004894 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004895
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004896 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4897 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4898 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4899 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00004900 if (fs_info->quota_root)
4901 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004902 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004903
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004904 update_global_block_rsv(fs_info);
4905}
4906
4907static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4908{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004909 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4910 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004911 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4912 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4913 WARN_ON(fs_info->trans_block_rsv.size > 0);
4914 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4915 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4916 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004917 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4918 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004919}
4920
Yan, Zhenga22285a2010-05-16 10:48:46 -04004921void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4922 struct btrfs_root *root)
4923{
Josef Bacik0e721102012-06-26 16:13:18 -04004924 if (!trans->block_rsv)
4925 return;
4926
Yan, Zhenga22285a2010-05-16 10:48:46 -04004927 if (!trans->bytes_reserved)
4928 return;
4929
Chris Masone77266e2012-02-24 10:39:05 -05004930 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004931 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004932 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004933 trans->bytes_reserved = 0;
4934}
4935
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004936/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004937int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4938 struct inode *inode)
4939{
4940 struct btrfs_root *root = BTRFS_I(inode)->root;
4941 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4942 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4943
4944 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004945 * We need to hold space in order to delete our orphan item once we've
4946 * added it, so this takes the reservation so we can release it later
4947 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004948 */
Chris Masonff5714c2011-05-28 07:00:39 -04004949 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004950 trace_btrfs_space_reservation(root->fs_info, "orphan",
4951 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004952 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4953}
4954
4955void btrfs_orphan_release_metadata(struct inode *inode)
4956{
4957 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004958 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004959 trace_btrfs_space_reservation(root->fs_info, "orphan",
4960 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004961 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4962}
4963
Miao Xied5c12072013-02-28 10:04:33 +00004964/*
4965 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4966 * root: the root of the parent directory
4967 * rsv: block reservation
4968 * items: the number of items that we need do reservation
4969 * qgroup_reserved: used to return the reserved size in qgroup
4970 *
4971 * This function is used to reserve the space for snapshot/subvolume
4972 * creation and deletion. Those operations are different with the
4973 * common file/directory operations, they change two fs/file trees
4974 * and root tree, the number of items that the qgroup reserves is
4975 * different with the free space reservation. So we can not use
4976 * the space reseravtion mechanism in start_transaction().
4977 */
4978int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4979 struct btrfs_block_rsv *rsv,
4980 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004981 u64 *qgroup_reserved,
4982 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004983{
Miao Xied5c12072013-02-28 10:04:33 +00004984 u64 num_bytes;
4985 int ret;
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004986 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00004987
4988 if (root->fs_info->quota_enabled) {
4989 /* One for parent inode, two for dir entries */
4990 num_bytes = 3 * root->leafsize;
4991 ret = btrfs_qgroup_reserve(root, num_bytes);
4992 if (ret)
4993 return ret;
4994 } else {
4995 num_bytes = 0;
4996 }
4997
4998 *qgroup_reserved = num_bytes;
4999
5000 num_bytes = btrfs_calc_trans_metadata_size(root, items);
5001 rsv->space_info = __find_space_info(root->fs_info,
5002 BTRFS_BLOCK_GROUP_METADATA);
5003 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
5004 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04005005
5006 if (ret == -ENOSPC && use_global_rsv)
5007 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
5008
Miao Xied5c12072013-02-28 10:04:33 +00005009 if (ret) {
5010 if (*qgroup_reserved)
5011 btrfs_qgroup_free(root, *qgroup_reserved);
5012 }
5013
5014 return ret;
5015}
5016
5017void btrfs_subvolume_release_metadata(struct btrfs_root *root,
5018 struct btrfs_block_rsv *rsv,
5019 u64 qgroup_reserved)
5020{
5021 btrfs_block_rsv_release(root, rsv, (u64)-1);
5022 if (qgroup_reserved)
5023 btrfs_qgroup_free(root, qgroup_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005024}
5025
Josef Bacik7709cde2011-08-04 10:25:02 -04005026/**
5027 * drop_outstanding_extent - drop an outstanding extent
5028 * @inode: the inode we're dropping the extent for
5029 *
5030 * This is called when we are freeing up an outstanding extent, either called
5031 * after an error or after an extent is written. This will return the number of
5032 * reserved extents that need to be freed. This must be called with
5033 * BTRFS_I(inode)->lock held.
5034 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00005035static unsigned drop_outstanding_extent(struct inode *inode)
5036{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005037 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005038 unsigned dropped_extents = 0;
5039
Josef Bacik9e0baf62011-07-15 15:16:44 +00005040 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
5041 BTRFS_I(inode)->outstanding_extents--;
5042
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005043 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04005044 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5045 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005046 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005047
Josef Bacik9e0baf62011-07-15 15:16:44 +00005048 /*
5049 * If we have more or the same amount of outsanding extents than we have
5050 * reserved then we need to leave the reserved extents count alone.
5051 */
5052 if (BTRFS_I(inode)->outstanding_extents >=
5053 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005054 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005055
5056 dropped_extents = BTRFS_I(inode)->reserved_extents -
5057 BTRFS_I(inode)->outstanding_extents;
5058 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005059 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005060}
5061
Josef Bacik7709cde2011-08-04 10:25:02 -04005062/**
5063 * calc_csum_metadata_size - return the amount of metada space that must be
5064 * reserved/free'd for the given bytes.
5065 * @inode: the inode we're manipulating
5066 * @num_bytes: the number of bytes in question
5067 * @reserve: 1 if we are reserving space, 0 if we are freeing space
5068 *
5069 * This adjusts the number of csum_bytes in the inode and then returns the
5070 * correct amount of metadata that must either be reserved or freed. We
5071 * calculate how many checksums we can fit into one leaf and then divide the
5072 * number of bytes that will need to be checksumed by this value to figure out
5073 * how many checksums will be required. If we are adding bytes then the number
5074 * may go up and we will return the number of additional bytes that must be
5075 * reserved. If it is going down we will return the number of bytes that must
5076 * be freed.
5077 *
5078 * This must be called with BTRFS_I(inode)->lock held.
5079 */
5080static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
5081 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005082{
Josef Bacik7709cde2011-08-04 10:25:02 -04005083 struct btrfs_root *root = BTRFS_I(inode)->root;
5084 u64 csum_size;
5085 int num_csums_per_leaf;
5086 int num_csums;
5087 int old_csums;
5088
5089 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
5090 BTRFS_I(inode)->csum_bytes == 0)
5091 return 0;
5092
5093 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
5094 if (reserve)
5095 BTRFS_I(inode)->csum_bytes += num_bytes;
5096 else
5097 BTRFS_I(inode)->csum_bytes -= num_bytes;
5098 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
5099 num_csums_per_leaf = (int)div64_u64(csum_size,
5100 sizeof(struct btrfs_csum_item) +
5101 sizeof(struct btrfs_disk_key));
5102 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
5103 num_csums = num_csums + num_csums_per_leaf - 1;
5104 num_csums = num_csums / num_csums_per_leaf;
5105
5106 old_csums = old_csums + num_csums_per_leaf - 1;
5107 old_csums = old_csums / num_csums_per_leaf;
5108
5109 /* No change, no need to reserve more */
5110 if (old_csums == num_csums)
5111 return 0;
5112
5113 if (reserve)
5114 return btrfs_calc_trans_metadata_size(root,
5115 num_csums - old_csums);
5116
5117 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005118}
5119
5120int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
5121{
5122 struct btrfs_root *root = BTRFS_I(inode)->root;
5123 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005124 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05005125 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005126 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05005127 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00005128 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07005129 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005130 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00005131 u64 to_free = 0;
5132 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005133
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005134 /* If we are a free space inode we need to not flush since we will be in
5135 * the middle of a transaction commit. We also don't need the delalloc
5136 * mutex since we won't race with anybody. We need this mostly to make
5137 * lockdep shut its filthy mouth.
5138 */
5139 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00005140 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005141 delalloc_lock = false;
5142 }
Josef Bacikc09544e2011-08-30 10:19:10 -04005143
Miao Xie08e007d2012-10-16 11:33:38 +00005144 if (flush != BTRFS_RESERVE_NO_FLUSH &&
5145 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005146 schedule_timeout(1);
5147
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005148 if (delalloc_lock)
5149 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
5150
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005151 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005152
Josef Bacik9e0baf62011-07-15 15:16:44 +00005153 spin_lock(&BTRFS_I(inode)->lock);
5154 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05005155
Josef Bacik9e0baf62011-07-15 15:16:44 +00005156 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05005157 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00005158 nr_extents = BTRFS_I(inode)->outstanding_extents -
5159 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005160
5161 /*
5162 * Add an item to reserve for updating the inode when we complete the
5163 * delalloc io.
5164 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04005165 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5166 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005167 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05005168 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005169 }
5170
5171 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04005172 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05005173 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005174 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05005175
Wang Shilong88e081bf2013-03-01 11:36:01 +00005176 if (root->fs_info->quota_enabled) {
Arne Jansenc5567232011-09-14 15:44:05 +02005177 ret = btrfs_qgroup_reserve(root, num_bytes +
5178 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005179 if (ret)
5180 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00005181 }
Arne Jansenc5567232011-09-14 15:44:05 +02005182
Wang Shilong88e081bf2013-03-01 11:36:01 +00005183 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
5184 if (unlikely(ret)) {
5185 if (root->fs_info->quota_enabled)
Miao Xie4b5829a2012-12-05 10:53:25 +00005186 btrfs_qgroup_free(root, num_bytes +
5187 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005188 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005189 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005190
Josef Bacik660d3f62011-12-09 11:18:51 -05005191 spin_lock(&BTRFS_I(inode)->lock);
5192 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04005193 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5194 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05005195 nr_extents--;
5196 }
5197 BTRFS_I(inode)->reserved_extents += nr_extents;
5198 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005199
5200 if (delalloc_lock)
5201 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05005202
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005203 if (to_reserve)
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05305204 trace_btrfs_space_reservation(root->fs_info, "delalloc",
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005205 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005206 block_rsv_add_bytes(block_rsv, to_reserve, 1);
5207
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005208 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00005209
5210out_fail:
5211 spin_lock(&BTRFS_I(inode)->lock);
5212 dropped = drop_outstanding_extent(inode);
5213 /*
5214 * If the inodes csum_bytes is the same as the original
5215 * csum_bytes then we know we haven't raced with any free()ers
5216 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00005217 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04005218 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00005219 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04005220 } else {
5221 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
5222 u64 bytes;
5223
5224 /*
5225 * This is tricky, but first we need to figure out how much we
5226 * free'd from any free-ers that occured during this
5227 * reservation, so we reset ->csum_bytes to the csum_bytes
5228 * before we dropped our lock, and then call the free for the
5229 * number of bytes that were freed while we were trying our
5230 * reservation.
5231 */
5232 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
5233 BTRFS_I(inode)->csum_bytes = csum_bytes;
5234 to_free = calc_csum_metadata_size(inode, bytes, 0);
5235
5236
5237 /*
5238 * Now we need to see how much we would have freed had we not
5239 * been making this reservation and our ->csum_bytes were not
5240 * artificially inflated.
5241 */
5242 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
5243 bytes = csum_bytes - orig_csum_bytes;
5244 bytes = calc_csum_metadata_size(inode, bytes, 0);
5245
5246 /*
5247 * Now reset ->csum_bytes to what it should be. If bytes is
5248 * more than to_free then we would have free'd more space had we
5249 * not had an artificially high ->csum_bytes, so we need to free
5250 * the remainder. If bytes is the same or less then we don't
5251 * need to do anything, the other free-ers did the correct
5252 * thing.
5253 */
5254 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
5255 if (bytes > to_free)
5256 to_free = bytes - to_free;
5257 else
5258 to_free = 0;
5259 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00005260 spin_unlock(&BTRFS_I(inode)->lock);
5261 if (dropped)
5262 to_free += btrfs_calc_trans_metadata_size(root, dropped);
5263
5264 if (to_free) {
5265 btrfs_block_rsv_release(root, block_rsv, to_free);
5266 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5267 btrfs_ino(inode), to_free, 0);
5268 }
5269 if (delalloc_lock)
5270 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
5271 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005272}
5273
Josef Bacik7709cde2011-08-04 10:25:02 -04005274/**
5275 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5276 * @inode: the inode to release the reservation for
5277 * @num_bytes: the number of bytes we're releasing
5278 *
5279 * This will release the metadata reservation for an inode. This can be called
5280 * once we complete IO for a given set of bytes to release their metadata
5281 * reservations.
5282 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005283void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
5284{
5285 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005286 u64 to_free = 0;
5287 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005288
5289 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04005290 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005291 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005292
Miao Xie09348562013-02-07 10:12:07 +00005293 if (num_bytes)
5294 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04005295 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005296 if (dropped > 0)
5297 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005298
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005299 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5300 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02005301 if (root->fs_info->quota_enabled) {
5302 btrfs_qgroup_free(root, num_bytes +
5303 dropped * root->leafsize);
5304 }
5305
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005306 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
5307 to_free);
5308}
5309
Josef Bacik7709cde2011-08-04 10:25:02 -04005310/**
5311 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5312 * @inode: inode we're writing to
5313 * @num_bytes: the number of bytes we want to allocate
5314 *
5315 * This will do the following things
5316 *
5317 * o reserve space in the data space info for num_bytes
5318 * o reserve space in the metadata space info based on number of outstanding
5319 * extents and how much csums will be needed
5320 * o add to the inodes ->delalloc_bytes
5321 * o add it to the fs_info's delalloc inodes list.
5322 *
5323 * This will return 0 for success and -ENOSPC if there is no space left.
5324 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005325int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5326{
5327 int ret;
5328
5329 ret = btrfs_check_data_free_space(inode, num_bytes);
5330 if (ret)
5331 return ret;
5332
5333 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5334 if (ret) {
5335 btrfs_free_reserved_data_space(inode, num_bytes);
5336 return ret;
5337 }
5338
5339 return 0;
5340}
5341
Josef Bacik7709cde2011-08-04 10:25:02 -04005342/**
5343 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5344 * @inode: inode we're releasing space for
5345 * @num_bytes: the number of bytes we want to free up
5346 *
5347 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5348 * called in the case that we don't need the metadata AND data reservations
5349 * anymore. So if there is an error or we insert an inline extent.
5350 *
5351 * This function will release the metadata space that was not used and will
5352 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5353 * list if there are no delalloc bytes left.
5354 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005355void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5356{
5357 btrfs_delalloc_release_metadata(inode, num_bytes);
5358 btrfs_free_reserved_data_space(inode, num_bytes);
5359}
5360
Liu Boc53d6132012-12-27 09:01:19 +00005361static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005362 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04005363{
Josef Bacik0af3d002010-06-21 14:48:16 -04005364 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04005365 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04005366 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005367 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04005368 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04005369 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04005370
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005371 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00005372 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02005373 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005374 if (alloc)
5375 old_val += num_bytes;
5376 else
5377 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02005378 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00005379 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005380
Chris Masond3977122009-01-05 21:25:51 -05005381 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04005382 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005383 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005384 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005385 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5386 BTRFS_BLOCK_GROUP_RAID1 |
5387 BTRFS_BLOCK_GROUP_RAID10))
5388 factor = 2;
5389 else
5390 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04005391 /*
5392 * If this block group has free space cache written out, we
5393 * need to make sure to load it if we are removing space. This
5394 * is because we need the unpinning stage to actually add the
5395 * space back to the block group, otherwise we will leak space.
5396 */
5397 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00005398 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005399
Chris Masondb945352007-10-15 16:15:53 -04005400 byte_in_group = bytenr - cache->key.objectid;
5401 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005402
Josef Bacik25179202008-10-29 14:49:05 -04005403 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005404 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005405
Josef Bacik73bc1872011-10-03 14:07:49 -04005406 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005407 cache->disk_cache_state < BTRFS_DC_CLEAR)
5408 cache->disk_cache_state = BTRFS_DC_CLEAR;
5409
Josef Bacik0f9dd462008-09-23 13:14:11 -04005410 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04005411 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005412 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005413 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005414 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005415 btrfs_set_block_group_used(&cache->item, old_val);
5416 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005417 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005418 cache->space_info->bytes_used += num_bytes;
5419 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005420 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005421 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005422 } else {
Chris Masondb945352007-10-15 16:15:53 -04005423 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04005424 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005425 cache->pinned += num_bytes;
5426 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005427 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005428 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005429 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005430 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005431
Yan, Zhengf0486c62010-05-16 10:46:25 -04005432 set_extent_dirty(info->pinned_extents,
5433 bytenr, bytenr + num_bytes - 1,
5434 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04005435 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005436 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005437 total -= num_bytes;
5438 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005439 }
5440 return 0;
5441}
Chris Mason6324fbf2008-03-24 15:01:59 -04005442
Chris Masona061fc82008-05-07 11:43:44 -04005443static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5444{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005445 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005446 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005447
Liu Boa1897fd2012-12-27 09:01:23 +00005448 spin_lock(&root->fs_info->block_group_cache_lock);
5449 bytenr = root->fs_info->first_logical_byte;
5450 spin_unlock(&root->fs_info->block_group_cache_lock);
5451
5452 if (bytenr < (u64)-1)
5453 return bytenr;
5454
Josef Bacik0f9dd462008-09-23 13:14:11 -04005455 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5456 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005457 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005458
Yan Zhengd2fb3432008-12-11 16:30:39 -05005459 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005460 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005461
5462 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005463}
5464
Yan, Zhengf0486c62010-05-16 10:46:25 -04005465static int pin_down_extent(struct btrfs_root *root,
5466 struct btrfs_block_group_cache *cache,
5467 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005468{
Yan Zheng11833d62009-09-11 16:11:19 -04005469 spin_lock(&cache->space_info->lock);
5470 spin_lock(&cache->lock);
5471 cache->pinned += num_bytes;
5472 cache->space_info->bytes_pinned += num_bytes;
5473 if (reserved) {
5474 cache->reserved -= num_bytes;
5475 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005476 }
Yan Zheng11833d62009-09-11 16:11:19 -04005477 spin_unlock(&cache->lock);
5478 spin_unlock(&cache->space_info->lock);
5479
Yan, Zhengf0486c62010-05-16 10:46:25 -04005480 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5481 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Josef Bacik0be5dc62013-10-07 15:18:52 -04005482 if (reserved)
5483 trace_btrfs_reserved_extent_free(root, bytenr, num_bytes);
Yan324ae4d2007-11-16 14:57:08 -05005484 return 0;
5485}
Chris Mason9078a3e2007-04-26 16:46:15 -04005486
Yan, Zhengf0486c62010-05-16 10:46:25 -04005487/*
5488 * this function must be called within transaction
5489 */
5490int btrfs_pin_extent(struct btrfs_root *root,
5491 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005492{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005493 struct btrfs_block_group_cache *cache;
5494
5495 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005496 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005497
5498 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5499
5500 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005501 return 0;
5502}
Zheng Yane8569812008-09-26 10:05:48 -04005503
Yan, Zhengf0486c62010-05-16 10:46:25 -04005504/*
Chris Masone688b7252011-10-31 20:52:39 -04005505 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005506 */
Liu Bodcfac412012-12-27 09:01:20 +00005507int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b7252011-10-31 20:52:39 -04005508 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005509{
Chris Masone688b7252011-10-31 20:52:39 -04005510 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04005511 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04005512
5513 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005514 if (!cache)
5515 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04005516
5517 /*
5518 * pull in the free space cache (if any) so that our pin
5519 * removes the free space from the cache. We have load_only set
5520 * to one because the slow code to read in the free extents does check
5521 * the pinned extents.
5522 */
Liu Bof6373bf2012-12-27 09:01:18 +00005523 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04005524
5525 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5526
5527 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04005528 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04005529 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005530 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04005531}
5532
Josef Bacik8c2a1a32013-06-06 13:19:32 -04005533static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
5534{
5535 int ret;
5536 struct btrfs_block_group_cache *block_group;
5537 struct btrfs_caching_control *caching_ctl;
5538
5539 block_group = btrfs_lookup_block_group(root->fs_info, start);
5540 if (!block_group)
5541 return -EINVAL;
5542
5543 cache_block_group(block_group, 0);
5544 caching_ctl = get_caching_control(block_group);
5545
5546 if (!caching_ctl) {
5547 /* Logic error */
5548 BUG_ON(!block_group_cache_done(block_group));
5549 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5550 } else {
5551 mutex_lock(&caching_ctl->mutex);
5552
5553 if (start >= caching_ctl->progress) {
5554 ret = add_excluded_extent(root, start, num_bytes);
5555 } else if (start + num_bytes <= caching_ctl->progress) {
5556 ret = btrfs_remove_free_space(block_group,
5557 start, num_bytes);
5558 } else {
5559 num_bytes = caching_ctl->progress - start;
5560 ret = btrfs_remove_free_space(block_group,
5561 start, num_bytes);
5562 if (ret)
5563 goto out_lock;
5564
5565 num_bytes = (start + num_bytes) -
5566 caching_ctl->progress;
5567 start = caching_ctl->progress;
5568 ret = add_excluded_extent(root, start, num_bytes);
5569 }
5570out_lock:
5571 mutex_unlock(&caching_ctl->mutex);
5572 put_caching_control(caching_ctl);
5573 }
5574 btrfs_put_block_group(block_group);
5575 return ret;
5576}
5577
5578int btrfs_exclude_logged_extents(struct btrfs_root *log,
5579 struct extent_buffer *eb)
5580{
5581 struct btrfs_file_extent_item *item;
5582 struct btrfs_key key;
5583 int found_type;
5584 int i;
5585
5586 if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
5587 return 0;
5588
5589 for (i = 0; i < btrfs_header_nritems(eb); i++) {
5590 btrfs_item_key_to_cpu(eb, &key, i);
5591 if (key.type != BTRFS_EXTENT_DATA_KEY)
5592 continue;
5593 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
5594 found_type = btrfs_file_extent_type(eb, item);
5595 if (found_type == BTRFS_FILE_EXTENT_INLINE)
5596 continue;
5597 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
5598 continue;
5599 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
5600 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
5601 __exclude_logged_extent(log, key.objectid, key.offset);
5602 }
5603
5604 return 0;
5605}
5606
Josef Bacikfb25e912011-07-26 17:00:46 -04005607/**
5608 * btrfs_update_reserved_bytes - update the block_group and space info counters
5609 * @cache: The cache we are manipulating
5610 * @num_bytes: The number of bytes in question
5611 * @reserve: One of the reservation enums
Miao Xiee570fd22014-06-19 10:42:50 +08005612 * @delalloc: The blocks are allocated for the delalloc write
Josef Bacikfb25e912011-07-26 17:00:46 -04005613 *
5614 * This is called by the allocator when it reserves space, or by somebody who is
5615 * freeing space that was never actually used on disk. For example if you
5616 * reserve some space for a new leaf in transaction A and before transaction A
5617 * commits you free that leaf, you call this with reserve set to 0 in order to
5618 * clear the reservation.
5619 *
5620 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5621 * ENOSPC accounting. For data we handle the reservation through clearing the
5622 * delalloc bits in the io_tree. We have to do this since we could end up
5623 * allocating less disk space for the amount of data we have reserved in the
5624 * case of compression.
5625 *
5626 * If this is a reservation and the block group has become read only we cannot
5627 * make the reservation and return -EAGAIN, otherwise this function always
5628 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005629 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005630static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
Miao Xiee570fd22014-06-19 10:42:50 +08005631 u64 num_bytes, int reserve, int delalloc)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005632{
Josef Bacikfb25e912011-07-26 17:00:46 -04005633 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005634 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005635
Josef Bacikfb25e912011-07-26 17:00:46 -04005636 spin_lock(&space_info->lock);
5637 spin_lock(&cache->lock);
5638 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005639 if (cache->ro) {
5640 ret = -EAGAIN;
5641 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005642 cache->reserved += num_bytes;
5643 space_info->bytes_reserved += num_bytes;
5644 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005645 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005646 "space_info", space_info->flags,
5647 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005648 space_info->bytes_may_use -= num_bytes;
5649 }
Miao Xiee570fd22014-06-19 10:42:50 +08005650
5651 if (delalloc)
5652 cache->delalloc_bytes += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005653 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005654 } else {
5655 if (cache->ro)
5656 space_info->bytes_readonly += num_bytes;
5657 cache->reserved -= num_bytes;
5658 space_info->bytes_reserved -= num_bytes;
Miao Xiee570fd22014-06-19 10:42:50 +08005659
5660 if (delalloc)
5661 cache->delalloc_bytes -= num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005662 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005663 spin_unlock(&cache->lock);
5664 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005665 return ret;
5666}
5667
Jeff Mahoney143bede2012-03-01 14:56:26 +01005668void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005669 struct btrfs_root *root)
5670{
5671 struct btrfs_fs_info *fs_info = root->fs_info;
5672 struct btrfs_caching_control *next;
5673 struct btrfs_caching_control *caching_ctl;
5674 struct btrfs_block_group_cache *cache;
5675
Josef Bacik9e351cc2014-03-13 15:42:13 -04005676 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04005677
5678 list_for_each_entry_safe(caching_ctl, next,
5679 &fs_info->caching_block_groups, list) {
5680 cache = caching_ctl->block_group;
5681 if (block_group_cache_done(cache)) {
5682 cache->last_byte_to_unpin = (u64)-1;
5683 list_del_init(&caching_ctl->list);
5684 put_caching_control(caching_ctl);
5685 } else {
5686 cache->last_byte_to_unpin = caching_ctl->progress;
5687 }
5688 }
5689
5690 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5691 fs_info->pinned_extents = &fs_info->freed_extents[1];
5692 else
5693 fs_info->pinned_extents = &fs_info->freed_extents[0];
5694
Josef Bacik9e351cc2014-03-13 15:42:13 -04005695 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005696
5697 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005698}
5699
5700static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5701{
5702 struct btrfs_fs_info *fs_info = root->fs_info;
5703 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005704 struct btrfs_space_info *space_info;
5705 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005706 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005707 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005708
5709 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005710 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005711 if (!cache ||
5712 start >= cache->key.objectid + cache->key.offset) {
5713 if (cache)
5714 btrfs_put_block_group(cache);
5715 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005716 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005717 }
5718
5719 len = cache->key.objectid + cache->key.offset - start;
5720 len = min(len, end + 1 - start);
5721
5722 if (start < cache->last_byte_to_unpin) {
5723 len = min(len, cache->last_byte_to_unpin - start);
5724 btrfs_add_free_space(cache, start, len);
5725 }
Josef Bacik25179202008-10-29 14:49:05 -04005726
Yan, Zhengf0486c62010-05-16 10:46:25 -04005727 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005728 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005729
Josef Bacik7b398f82012-10-22 15:52:28 -04005730 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005731 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005732 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005733 space_info->bytes_pinned -= len;
Liu Bod288db52014-07-02 16:58:01 +08005734 percpu_counter_add(&space_info->total_bytes_pinned, -len);
Josef Bacik7b398f82012-10-22 15:52:28 -04005735 if (cache->ro) {
5736 space_info->bytes_readonly += len;
5737 readonly = true;
5738 }
Josef Bacik25179202008-10-29 14:49:05 -04005739 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005740 if (!readonly && global_rsv->space_info == space_info) {
5741 spin_lock(&global_rsv->lock);
5742 if (!global_rsv->full) {
5743 len = min(len, global_rsv->size -
5744 global_rsv->reserved);
5745 global_rsv->reserved += len;
5746 space_info->bytes_may_use += len;
5747 if (global_rsv->reserved >= global_rsv->size)
5748 global_rsv->full = 1;
5749 }
5750 spin_unlock(&global_rsv->lock);
5751 }
5752 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005753 }
5754
5755 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005756 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005757 return 0;
5758}
5759
5760int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005761 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005762{
Yan Zheng11833d62009-09-11 16:11:19 -04005763 struct btrfs_fs_info *fs_info = root->fs_info;
5764 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005765 u64 start;
5766 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005767 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005768
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005769 if (trans->aborted)
5770 return 0;
5771
Yan Zheng11833d62009-09-11 16:11:19 -04005772 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5773 unpin = &fs_info->freed_extents[1];
5774 else
5775 unpin = &fs_info->freed_extents[0];
5776
Chris Masond3977122009-01-05 21:25:51 -05005777 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005778 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005779 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005780 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005781 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005782
Li Dongyang5378e602011-03-24 10:24:27 +00005783 if (btrfs_test_opt(root, DISCARD))
5784 ret = btrfs_discard_extent(root, start,
5785 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005786
Chris Mason1a5bc162007-10-15 16:15:26 -04005787 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005788 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005789 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005790 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005791
Chris Masone20d96d2007-03-22 12:13:20 -04005792 return 0;
5793}
5794
Josef Bacikb150a4f2013-06-19 15:00:04 -04005795static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
5796 u64 owner, u64 root_objectid)
5797{
5798 struct btrfs_space_info *space_info;
5799 u64 flags;
5800
5801 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
5802 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
5803 flags = BTRFS_BLOCK_GROUP_SYSTEM;
5804 else
5805 flags = BTRFS_BLOCK_GROUP_METADATA;
5806 } else {
5807 flags = BTRFS_BLOCK_GROUP_DATA;
5808 }
5809
5810 space_info = __find_space_info(fs_info, flags);
5811 BUG_ON(!space_info); /* Logic bug */
5812 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
5813}
5814
5815
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005816static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5817 struct btrfs_root *root,
5818 u64 bytenr, u64 num_bytes, u64 parent,
5819 u64 root_objectid, u64 owner_objectid,
5820 u64 owner_offset, int refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07005821 struct btrfs_delayed_extent_op *extent_op,
5822 int no_quota)
Chris Masona28ec192007-03-06 20:08:01 -05005823{
Chris Masone2fa7222007-03-12 16:22:34 -04005824 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005825 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005826 struct btrfs_fs_info *info = root->fs_info;
5827 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005828 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005829 struct btrfs_extent_item *ei;
5830 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005831 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005832 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005833 int extent_slot = 0;
5834 int found_extent = 0;
5835 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005836 u32 item_size;
5837 u64 refs;
Josef Bacikfcebe452014-05-13 17:30:47 -07005838 int last_ref = 0;
5839 enum btrfs_qgroup_operation_type type = BTRFS_QGROUP_OPER_SUB_EXCL;
Josef Bacik3173a182013-03-07 14:22:04 -05005840 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5841 SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05005842
Josef Bacikfcebe452014-05-13 17:30:47 -07005843 if (!info->quota_enabled || !is_fstree(root_objectid))
5844 no_quota = 1;
5845
Chris Mason5caf2a02007-04-02 11:20:42 -04005846 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005847 if (!path)
5848 return -ENOMEM;
5849
Chris Mason3c12ac72008-04-21 12:01:38 -04005850 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005851 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005852
5853 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5854 BUG_ON(!is_data && refs_to_drop != 1);
5855
Josef Bacik3173a182013-03-07 14:22:04 -05005856 if (is_data)
5857 skinny_metadata = 0;
5858
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005859 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5860 bytenr, num_bytes, parent,
5861 root_objectid, owner_objectid,
5862 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005863 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005864 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005865 while (extent_slot >= 0) {
5866 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005867 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005868 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005869 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005870 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5871 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005872 found_extent = 1;
5873 break;
5874 }
Josef Bacik3173a182013-03-07 14:22:04 -05005875 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5876 key.offset == owner_objectid) {
5877 found_extent = 1;
5878 break;
5879 }
Chris Mason952fcca2008-02-18 16:33:44 -05005880 if (path->slots[0] - extent_slot > 5)
5881 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005882 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005883 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005884#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5885 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5886 if (found_extent && item_size < sizeof(*ei))
5887 found_extent = 0;
5888#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005889 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005890 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005891 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005892 NULL, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07005893 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06005894 if (ret) {
5895 btrfs_abort_transaction(trans, extent_root, ret);
5896 goto out;
5897 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005898 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005899 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005900
5901 key.objectid = bytenr;
5902 key.type = BTRFS_EXTENT_ITEM_KEY;
5903 key.offset = num_bytes;
5904
Josef Bacik3173a182013-03-07 14:22:04 -05005905 if (!is_data && skinny_metadata) {
5906 key.type = BTRFS_METADATA_ITEM_KEY;
5907 key.offset = owner_objectid;
5908 }
5909
Zheng Yan31840ae2008-09-23 13:14:14 -04005910 ret = btrfs_search_slot(trans, extent_root,
5911 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05005912 if (ret > 0 && skinny_metadata && path->slots[0]) {
5913 /*
5914 * Couldn't find our skinny metadata item,
5915 * see if we have ye olde extent item.
5916 */
5917 path->slots[0]--;
5918 btrfs_item_key_to_cpu(path->nodes[0], &key,
5919 path->slots[0]);
5920 if (key.objectid == bytenr &&
5921 key.type == BTRFS_EXTENT_ITEM_KEY &&
5922 key.offset == num_bytes)
5923 ret = 0;
5924 }
5925
5926 if (ret > 0 && skinny_metadata) {
5927 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01005928 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05005929 key.type = BTRFS_EXTENT_ITEM_KEY;
5930 key.offset = num_bytes;
5931 btrfs_release_path(path);
5932 ret = btrfs_search_slot(trans, extent_root,
5933 &key, path, -1, 1);
5934 }
5935
Josef Bacikf3465ca2008-11-12 14:19:50 -05005936 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005937 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005938 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005939 if (ret > 0)
5940 btrfs_print_leaf(extent_root,
5941 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005942 }
David Sterba005d6422012-09-18 07:52:32 -06005943 if (ret < 0) {
5944 btrfs_abort_transaction(trans, extent_root, ret);
5945 goto out;
5946 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005947 extent_slot = path->slots[0];
5948 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305949 } else if (WARN_ON(ret == -ENOENT)) {
Chris Mason7bb86312007-12-11 09:25:06 -05005950 btrfs_print_leaf(extent_root, path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005951 btrfs_err(info,
5952 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005953 bytenr, parent, root_objectid, owner_objectid,
5954 owner_offset);
Josef Bacikc4a050b2014-03-14 16:36:53 -04005955 btrfs_abort_transaction(trans, extent_root, ret);
5956 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005957 } else {
David Sterba005d6422012-09-18 07:52:32 -06005958 btrfs_abort_transaction(trans, extent_root, ret);
5959 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005960 }
Chris Mason5f39d392007-10-15 16:14:19 -04005961
5962 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005963 item_size = btrfs_item_size_nr(leaf, extent_slot);
5964#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5965 if (item_size < sizeof(*ei)) {
5966 BUG_ON(found_extent || extent_slot != path->slots[0]);
5967 ret = convert_extent_item_v0(trans, extent_root, path,
5968 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005969 if (ret < 0) {
5970 btrfs_abort_transaction(trans, extent_root, ret);
5971 goto out;
5972 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005973
David Sterbab3b4aa72011-04-21 01:20:15 +02005974 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005975 path->leave_spinning = 1;
5976
5977 key.objectid = bytenr;
5978 key.type = BTRFS_EXTENT_ITEM_KEY;
5979 key.offset = num_bytes;
5980
5981 ret = btrfs_search_slot(trans, extent_root, &key, path,
5982 -1, 1);
5983 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005984 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005985 ret, bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005986 btrfs_print_leaf(extent_root, path->nodes[0]);
5987 }
David Sterba005d6422012-09-18 07:52:32 -06005988 if (ret < 0) {
5989 btrfs_abort_transaction(trans, extent_root, ret);
5990 goto out;
5991 }
5992
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005993 extent_slot = path->slots[0];
5994 leaf = path->nodes[0];
5995 item_size = btrfs_item_size_nr(leaf, extent_slot);
5996 }
5997#endif
5998 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005999 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04006000 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05006001 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
6002 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006003 struct btrfs_tree_block_info *bi;
6004 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
6005 bi = (struct btrfs_tree_block_info *)(ei + 1);
6006 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05006007 }
6008
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006009 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04006010 if (refs < refs_to_drop) {
6011 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
David Sterba351fd352014-05-15 16:48:20 +02006012 "for bytenr %Lu", refs_to_drop, refs, bytenr);
Josef Bacik32b02532013-04-24 16:38:50 -04006013 ret = -EINVAL;
6014 btrfs_abort_transaction(trans, extent_root, ret);
6015 goto out;
6016 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006017 refs -= refs_to_drop;
6018
6019 if (refs > 0) {
Josef Bacikfcebe452014-05-13 17:30:47 -07006020 type = BTRFS_QGROUP_OPER_SUB_SHARED;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006021 if (extent_op)
6022 __run_delayed_extent_op(extent_op, leaf, ei);
6023 /*
6024 * In the case of inline back ref, reference count will
6025 * be updated by remove_extent_backref
6026 */
6027 if (iref) {
6028 BUG_ON(!found_extent);
6029 } else {
6030 btrfs_set_extent_refs(leaf, ei, refs);
6031 btrfs_mark_buffer_dirty(leaf);
6032 }
6033 if (found_extent) {
6034 ret = remove_extent_backref(trans, extent_root, path,
6035 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07006036 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06006037 if (ret) {
6038 btrfs_abort_transaction(trans, extent_root, ret);
6039 goto out;
6040 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006041 }
Josef Bacikb150a4f2013-06-19 15:00:04 -04006042 add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
6043 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006044 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006045 if (found_extent) {
6046 BUG_ON(is_data && refs_to_drop !=
6047 extent_data_ref_count(root, path, iref));
6048 if (iref) {
6049 BUG_ON(path->slots[0] != extent_slot);
6050 } else {
6051 BUG_ON(path->slots[0] != extent_slot + 1);
6052 path->slots[0] = extent_slot;
6053 num_to_del = 2;
6054 }
Chris Mason78fae272007-03-25 11:35:08 -04006055 }
Chris Masonb9473432009-03-13 11:00:37 -04006056
Josef Bacikfcebe452014-05-13 17:30:47 -07006057 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05006058 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
6059 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06006060 if (ret) {
6061 btrfs_abort_transaction(trans, extent_root, ret);
6062 goto out;
6063 }
David Sterbab3b4aa72011-04-21 01:20:15 +02006064 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01006065
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006066 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05006067 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06006068 if (ret) {
6069 btrfs_abort_transaction(trans, extent_root, ret);
6070 goto out;
6071 }
Chris Mason459931e2008-12-10 09:10:46 -05006072 }
6073
Liu Boc53d6132012-12-27 09:01:19 +00006074 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06006075 if (ret) {
6076 btrfs_abort_transaction(trans, extent_root, ret);
6077 goto out;
6078 }
Chris Masona28ec192007-03-06 20:08:01 -05006079 }
Josef Bacikfcebe452014-05-13 17:30:47 -07006080 btrfs_release_path(path);
6081
6082 /* Deal with the quota accounting */
6083 if (!ret && last_ref && !no_quota) {
6084 int mod_seq = 0;
6085
6086 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
6087 type == BTRFS_QGROUP_OPER_SUB_SHARED)
6088 mod_seq = 1;
6089
6090 ret = btrfs_qgroup_record_ref(trans, info, root_objectid,
6091 bytenr, num_bytes, type,
6092 mod_seq);
6093 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006094out:
Chris Mason5caf2a02007-04-02 11:20:42 -04006095 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05006096 return ret;
6097}
6098
6099/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006100 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04006101 * delayed ref for that extent as well. This searches the delayed ref tree for
6102 * a given extent, and if there are no other delayed refs to be processed, it
6103 * removes it from the tree.
6104 */
6105static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
6106 struct btrfs_root *root, u64 bytenr)
6107{
6108 struct btrfs_delayed_ref_head *head;
6109 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006110 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04006111
6112 delayed_refs = &trans->transaction->delayed_refs;
6113 spin_lock(&delayed_refs->lock);
6114 head = btrfs_find_delayed_ref_head(trans, bytenr);
6115 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08006116 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04006117
Josef Bacikd7df2c72014-01-23 09:21:38 -05006118 spin_lock(&head->lock);
6119 if (rb_first(&head->ref_root))
Chris Mason1887be62009-03-13 10:11:24 -04006120 goto out;
6121
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006122 if (head->extent_op) {
6123 if (!head->must_insert_reserved)
6124 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00006125 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006126 head->extent_op = NULL;
6127 }
6128
Chris Mason1887be62009-03-13 10:11:24 -04006129 /*
6130 * waiting for the lock here would deadlock. If someone else has it
6131 * locked they are already in the process of dropping it anyway
6132 */
6133 if (!mutex_trylock(&head->mutex))
6134 goto out;
6135
6136 /*
6137 * at this point we have a head with no other entries. Go
6138 * ahead and process it.
6139 */
6140 head->node.in_tree = 0;
Liu Boc46effa2013-10-14 12:59:45 +08006141 rb_erase(&head->href_node, &delayed_refs->href_root);
Chris Masonc3e69d52009-03-13 10:17:05 -04006142
Josef Bacikd7df2c72014-01-23 09:21:38 -05006143 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04006144
6145 /*
6146 * we don't take a ref on the node because we're removing it from the
6147 * tree, so we just steal the ref the tree was holding.
6148 */
Chris Masonc3e69d52009-03-13 10:17:05 -04006149 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05006150 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04006151 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05006152 head->processing = 0;
6153 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04006154 spin_unlock(&delayed_refs->lock);
6155
Yan, Zhengf0486c62010-05-16 10:46:25 -04006156 BUG_ON(head->extent_op);
6157 if (head->must_insert_reserved)
6158 ret = 1;
6159
6160 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04006161 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006162 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04006163out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05006164 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08006165
6166out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04006167 spin_unlock(&delayed_refs->lock);
6168 return 0;
6169}
6170
Yan, Zhengf0486c62010-05-16 10:46:25 -04006171void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
6172 struct btrfs_root *root,
6173 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02006174 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006175{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006176 struct btrfs_block_group_cache *cache = NULL;
Josef Bacikb150a4f2013-06-19 15:00:04 -04006177 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006178 int ret;
6179
6180 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006181 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6182 buf->start, buf->len,
6183 parent, root->root_key.objectid,
6184 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02006185 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006186 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006187 }
6188
6189 if (!last_ref)
6190 return;
6191
Yan, Zhengf0486c62010-05-16 10:46:25 -04006192 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006193
6194 if (btrfs_header_generation(buf) == trans->transid) {
6195 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
6196 ret = check_ref_cleanup(trans, root, buf->start);
6197 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04006198 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006199 }
6200
6201 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
6202 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04006203 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006204 }
6205
6206 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
6207
6208 btrfs_add_free_space(cache, buf->start, buf->len);
Miao Xiee570fd22014-06-19 10:42:50 +08006209 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0);
Josef Bacik0be5dc62013-10-07 15:18:52 -04006210 trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
Josef Bacikb150a4f2013-06-19 15:00:04 -04006211 pin = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006212 }
6213out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04006214 if (pin)
6215 add_pinned_bytes(root->fs_info, buf->len,
6216 btrfs_header_level(buf),
6217 root->root_key.objectid);
6218
Josef Bacika826d6d2011-03-16 13:42:43 -04006219 /*
6220 * Deleting the buffer, clear the corrupt flag since it doesn't matter
6221 * anymore.
6222 */
6223 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006224 btrfs_put_block_group(cache);
6225}
6226
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006227/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006228int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6229 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Josef Bacikfcebe452014-05-13 17:30:47 -07006230 u64 owner, u64 offset, int no_quota)
Chris Mason925baed2008-06-25 16:01:30 -04006231{
6232 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006233 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04006234
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04006235#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
6236 if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state)))
6237 return 0;
6238#endif
Josef Bacikb150a4f2013-06-19 15:00:04 -04006239 add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
6240
Chris Mason56bec292009-03-13 10:10:06 -04006241 /*
6242 * tree log blocks never actually go into the extent allocation
6243 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04006244 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006245 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
6246 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04006247 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04006248 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04006249 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006250 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006251 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
6252 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006253 parent, root_objectid, (int)owner,
Josef Bacikfcebe452014-05-13 17:30:47 -07006254 BTRFS_DROP_DELAYED_REF, NULL, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006255 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006256 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
6257 num_bytes,
6258 parent, root_objectid, owner,
6259 offset, BTRFS_DROP_DELAYED_REF,
Josef Bacikfcebe452014-05-13 17:30:47 -07006260 NULL, no_quota);
Chris Mason56bec292009-03-13 10:10:06 -04006261 }
Chris Mason925baed2008-06-25 16:01:30 -04006262 return ret;
6263}
6264
David Woodhouse53b381b2013-01-29 18:40:14 -05006265static u64 stripe_align(struct btrfs_root *root,
6266 struct btrfs_block_group_cache *cache,
6267 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05006268{
Qu Wenruofda28322013-02-26 08:10:22 +00006269 u64 ret = ALIGN(val, root->stripesize);
Chris Mason87ee04e2007-11-30 11:30:34 -05006270 return ret;
6271}
6272
Chris Masonfec577f2007-02-26 10:40:21 -05006273/*
Josef Bacik817d52f2009-07-13 21:29:25 -04006274 * when we wait for progress in the block group caching, its because
6275 * our allocation attempt failed at least once. So, we must sleep
6276 * and let some progress happen before we try again.
6277 *
6278 * This function will sleep at least once waiting for new free space to
6279 * show up, and then it will check the block group free space numbers
6280 * for our min num_bytes. Another option is to have it go ahead
6281 * and look in the rbtree for a free extent of a given size, but this
6282 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04006283 *
6284 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
6285 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04006286 */
Josef Bacik36cce922013-08-05 11:15:21 -04006287static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04006288wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
6289 u64 num_bytes)
6290{
Yan Zheng11833d62009-09-11 16:11:19 -04006291 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04006292
Yan Zheng11833d62009-09-11 16:11:19 -04006293 caching_ctl = get_caching_control(cache);
6294 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04006295 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04006296
Yan Zheng11833d62009-09-11 16:11:19 -04006297 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08006298 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04006299
6300 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04006301}
6302
6303static noinline int
6304wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
6305{
6306 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04006307 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006308
6309 caching_ctl = get_caching_control(cache);
6310 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04006311 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006312
6313 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04006314 if (cache->cached == BTRFS_CACHE_ERROR)
6315 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04006316 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04006317 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04006318}
6319
Liu Bo31e50222012-11-21 14:18:10 +00006320int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04006321{
Ilya Dryomov7738a532012-03-27 17:09:17 +03006322 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00006323 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006324 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00006325 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006326 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00006327 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006328 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00006329 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006330 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05006331 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05006332 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05006333 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006334
Chris Masone942f882013-02-20 14:06:05 -05006335 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04006336}
6337
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006338int get_block_group_index(struct btrfs_block_group_cache *cache)
Ilya Dryomov7738a532012-03-27 17:09:17 +03006339{
Liu Bo31e50222012-11-21 14:18:10 +00006340 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03006341}
6342
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006343static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
6344 [BTRFS_RAID_RAID10] = "raid10",
6345 [BTRFS_RAID_RAID1] = "raid1",
6346 [BTRFS_RAID_DUP] = "dup",
6347 [BTRFS_RAID_RAID0] = "raid0",
6348 [BTRFS_RAID_SINGLE] = "single",
6349 [BTRFS_RAID_RAID5] = "raid5",
6350 [BTRFS_RAID_RAID6] = "raid6",
6351};
6352
Jeff Mahoney1b8e5df2013-11-20 16:50:23 -05006353static const char *get_raid_name(enum btrfs_raid_types type)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006354{
6355 if (type >= BTRFS_NR_RAID_TYPES)
6356 return NULL;
6357
6358 return btrfs_raid_type_names[type];
6359}
6360
Josef Bacik817d52f2009-07-13 21:29:25 -04006361enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05006362 LOOP_CACHING_NOWAIT = 0,
6363 LOOP_CACHING_WAIT = 1,
6364 LOOP_ALLOC_CHUNK = 2,
6365 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04006366};
6367
Miao Xiee570fd22014-06-19 10:42:50 +08006368static inline void
6369btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
6370 int delalloc)
6371{
6372 if (delalloc)
6373 down_read(&cache->data_rwsem);
6374}
6375
6376static inline void
6377btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
6378 int delalloc)
6379{
6380 btrfs_get_block_group(cache);
6381 if (delalloc)
6382 down_read(&cache->data_rwsem);
6383}
6384
6385static struct btrfs_block_group_cache *
6386btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
6387 struct btrfs_free_cluster *cluster,
6388 int delalloc)
6389{
6390 struct btrfs_block_group_cache *used_bg;
6391 bool locked = false;
6392again:
6393 spin_lock(&cluster->refill_lock);
6394 if (locked) {
6395 if (used_bg == cluster->block_group)
6396 return used_bg;
6397
6398 up_read(&used_bg->data_rwsem);
6399 btrfs_put_block_group(used_bg);
6400 }
6401
6402 used_bg = cluster->block_group;
6403 if (!used_bg)
6404 return NULL;
6405
6406 if (used_bg == block_group)
6407 return used_bg;
6408
6409 btrfs_get_block_group(used_bg);
6410
6411 if (!delalloc)
6412 return used_bg;
6413
6414 if (down_read_trylock(&used_bg->data_rwsem))
6415 return used_bg;
6416
6417 spin_unlock(&cluster->refill_lock);
6418 down_read(&used_bg->data_rwsem);
6419 locked = true;
6420 goto again;
6421}
6422
6423static inline void
6424btrfs_release_block_group(struct btrfs_block_group_cache *cache,
6425 int delalloc)
6426{
6427 if (delalloc)
6428 up_read(&cache->data_rwsem);
6429 btrfs_put_block_group(cache);
6430}
6431
Josef Bacik817d52f2009-07-13 21:29:25 -04006432/*
Chris Masonfec577f2007-02-26 10:40:21 -05006433 * walks the btree of allocated extents and find a hole of a given size.
6434 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08006435 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04006436 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08006437 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05006438 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08006439 *
6440 * If there is no suitable free space, we will record the max size of
6441 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05006442 */
Josef Bacik00361582013-08-14 14:02:47 -04006443static noinline int find_free_extent(struct btrfs_root *orig_root,
Chris Mason98ed5172008-01-03 10:01:48 -05006444 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05006445 u64 hint_byte, struct btrfs_key *ins,
Miao Xiee570fd22014-06-19 10:42:50 +08006446 u64 flags, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05006447{
Josef Bacik80eb2342008-10-29 14:49:05 -04006448 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05006449 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006450 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04006451 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05006452 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08006453 u64 max_extent_size = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04006454 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04006455 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006456 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00006457 int index = __get_raid_index(flags);
6458 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
Josef Bacikfb25e912011-07-26 17:00:46 -04006459 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik0a243252009-09-11 16:11:20 -04006460 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006461 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04006462 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08006463 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006464
Chris Masondb945352007-10-15 16:15:53 -04006465 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04006466 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04006467 ins->objectid = 0;
6468 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04006469
David Sterbab6919a52013-04-29 13:39:40 +00006470 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05006471
David Sterbab6919a52013-04-29 13:39:40 +00006472 space_info = __find_space_info(root->fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006473 if (!space_info) {
David Sterbab6919a52013-04-29 13:39:40 +00006474 btrfs_err(root->fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006475 return -ENOSPC;
6476 }
Josef Bacik2552d172009-04-03 10:14:19 -04006477
Josef Bacik67377732010-09-16 16:19:09 -04006478 /*
6479 * If the space info is for both data and metadata it means we have a
6480 * small filesystem and we can't use the clustering stuff.
6481 */
6482 if (btrfs_mixed_space_info(space_info))
6483 use_cluster = false;
6484
David Sterbab6919a52013-04-29 13:39:40 +00006485 if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006486 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05006487 if (!btrfs_test_opt(root, SSD))
6488 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04006489 }
6490
David Sterbab6919a52013-04-29 13:39:40 +00006491 if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
Josef Bacik67377732010-09-16 16:19:09 -04006492 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006493 last_ptr = &root->fs_info->data_alloc_cluster;
6494 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006495
Chris Mason239b14b2008-03-24 15:02:07 -04006496 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006497 spin_lock(&last_ptr->lock);
6498 if (last_ptr->block_group)
6499 hint_byte = last_ptr->window_start;
6500 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04006501 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006502
Chris Masona061fc82008-05-07 11:43:44 -04006503 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04006504 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04006505
Josef Bacik817d52f2009-07-13 21:29:25 -04006506 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006507 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006508
Josef Bacik2552d172009-04-03 10:14:19 -04006509 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04006510 block_group = btrfs_lookup_block_group(root->fs_info,
6511 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04006512 /*
6513 * we don't want to use the block group if it doesn't match our
6514 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05006515 *
6516 * However if we are re-searching with an ideal block group
6517 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04006518 */
David Sterbab6919a52013-04-29 13:39:40 +00006519 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05006520 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04006521 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04006522 if (list_empty(&block_group->list) ||
6523 block_group->ro) {
6524 /*
6525 * someone is removing this block group,
6526 * we can't jump into the have_block_group
6527 * target because our list pointers are not
6528 * valid
6529 */
6530 btrfs_put_block_group(block_group);
6531 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05006532 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006533 index = get_block_group_index(block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08006534 btrfs_lock_block_group(block_group, delalloc);
Chris Mason44fb5512009-06-04 15:34:51 -04006535 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05006536 }
Josef Bacik2552d172009-04-03 10:14:19 -04006537 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006538 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006539 }
Chris Mason42e70e72008-11-07 18:17:11 -05006540 }
Josef Bacik2552d172009-04-03 10:14:19 -04006541search:
Miao Xie60d2adb2011-09-09 17:34:35 +08006542 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04006543 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006544 list_for_each_entry(block_group, &space_info->block_groups[index],
6545 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04006546 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04006547 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05006548
Miao Xiee570fd22014-06-19 10:42:50 +08006549 btrfs_grab_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04006550 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05006551
Chris Mason83a50de2010-12-13 15:06:46 -05006552 /*
6553 * this can happen if we end up cycling through all the
6554 * raid types, but we want to make sure we only allocate
6555 * for the proper type.
6556 */
David Sterbab6919a52013-04-29 13:39:40 +00006557 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05006558 u64 extra = BTRFS_BLOCK_GROUP_DUP |
6559 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05006560 BTRFS_BLOCK_GROUP_RAID5 |
6561 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05006562 BTRFS_BLOCK_GROUP_RAID10;
6563
6564 /*
6565 * if they asked for extra copies and this block group
6566 * doesn't provide them, bail. This does allow us to
6567 * fill raid0 from raid1.
6568 */
David Sterbab6919a52013-04-29 13:39:40 +00006569 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05006570 goto loop;
6571 }
6572
Josef Bacik2552d172009-04-03 10:14:19 -04006573have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05006574 cached = block_group_cache_done(block_group);
6575 if (unlikely(!cached)) {
Liu Bof6373bf2012-12-27 09:01:18 +00006576 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04006577 BUG_ON(ret < 0);
6578 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05006579 }
6580
Josef Bacik36cce922013-08-05 11:15:21 -04006581 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
6582 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05006583 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04006584 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006585
Josef Bacik0a243252009-09-11 16:11:20 -04006586 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006587 * Ok we want to try and use the cluster allocator, so
6588 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04006589 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006590 if (last_ptr) {
Miao Xie215a63d2014-01-15 20:00:56 +08006591 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05006592 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006593 /*
6594 * the refill lock keeps out other
6595 * people trying to start a new cluster
6596 */
Miao Xiee570fd22014-06-19 10:42:50 +08006597 used_block_group = btrfs_lock_cluster(block_group,
6598 last_ptr,
6599 delalloc);
6600 if (!used_block_group)
Chris Mason44fb5512009-06-04 15:34:51 -04006601 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04006602
Miao Xiee570fd22014-06-19 10:42:50 +08006603 if (used_block_group != block_group &&
6604 (used_block_group->ro ||
6605 !block_group_bits(used_block_group, flags)))
6606 goto release_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006607
6608 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08006609 last_ptr,
6610 num_bytes,
6611 used_block_group->key.objectid,
6612 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006613 if (offset) {
6614 /* we have a block, we're done */
6615 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006616 trace_btrfs_reserve_extent_cluster(root,
Miao Xie89d43462014-01-15 20:00:57 +08006617 used_block_group,
6618 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08006619 if (used_block_group != block_group) {
Miao Xiee570fd22014-06-19 10:42:50 +08006620 btrfs_release_block_group(block_group,
6621 delalloc);
Miao Xie215a63d2014-01-15 20:00:56 +08006622 block_group = used_block_group;
6623 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006624 goto checks;
6625 }
6626
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006627 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xiee570fd22014-06-19 10:42:50 +08006628release_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006629 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6630 * set up a new clusters, so lets just skip it
6631 * and let the allocator find whatever block
6632 * it can find. If we reach this point, we
6633 * will have tried the cluster allocator
6634 * plenty of times and not have found
6635 * anything, so we are likely way too
6636 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006637 * anything.
6638 *
6639 * However, if the cluster is taken from the
6640 * current block group, release the cluster
6641 * first, so that we stand a better chance of
6642 * succeeding in the unclustered
6643 * allocation. */
6644 if (loop >= LOOP_NO_EMPTY_SIZE &&
Miao Xiee570fd22014-06-19 10:42:50 +08006645 used_block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006646 spin_unlock(&last_ptr->refill_lock);
Miao Xiee570fd22014-06-19 10:42:50 +08006647 btrfs_release_block_group(used_block_group,
6648 delalloc);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006649 goto unclustered_alloc;
6650 }
6651
Chris Masonfa9c0d792009-04-03 09:47:43 -04006652 /*
6653 * this cluster didn't work out, free it and
6654 * start over
6655 */
6656 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6657
Miao Xiee570fd22014-06-19 10:42:50 +08006658 if (used_block_group != block_group)
6659 btrfs_release_block_group(used_block_group,
6660 delalloc);
6661refill_cluster:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006662 if (loop >= LOOP_NO_EMPTY_SIZE) {
6663 spin_unlock(&last_ptr->refill_lock);
6664 goto unclustered_alloc;
6665 }
6666
Chris Mason8de972b2013-01-04 15:39:43 -05006667 aligned_cluster = max_t(unsigned long,
6668 empty_cluster + empty_size,
6669 block_group->full_stripe_len);
6670
Chris Masonfa9c0d792009-04-03 09:47:43 -04006671 /* allocate a cluster in this block group */
Josef Bacik00361582013-08-14 14:02:47 -04006672 ret = btrfs_find_space_cluster(root, block_group,
6673 last_ptr, search_start,
6674 num_bytes,
6675 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006676 if (ret == 0) {
6677 /*
6678 * now pull our allocation out of this
6679 * cluster
6680 */
6681 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08006682 last_ptr,
6683 num_bytes,
6684 search_start,
6685 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006686 if (offset) {
6687 /* we found one, proceed */
6688 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006689 trace_btrfs_reserve_extent_cluster(root,
6690 block_group, search_start,
6691 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006692 goto checks;
6693 }
Josef Bacik0a243252009-09-11 16:11:20 -04006694 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6695 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006696 spin_unlock(&last_ptr->refill_lock);
6697
Josef Bacik0a243252009-09-11 16:11:20 -04006698 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006699 wait_block_group_cache_progress(block_group,
6700 num_bytes + empty_cluster + empty_size);
6701 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006702 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006703
Chris Masonfa9c0d792009-04-03 09:47:43 -04006704 /*
6705 * at this point we either didn't find a cluster
6706 * or we weren't able to allocate a block from our
6707 * cluster. Free the cluster we've been trying
6708 * to use, and go to the next block group
6709 */
Josef Bacik0a243252009-09-11 16:11:20 -04006710 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006711 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04006712 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006713 }
6714
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006715unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006716 spin_lock(&block_group->free_space_ctl->tree_lock);
6717 if (cached &&
6718 block_group->free_space_ctl->free_space <
6719 num_bytes + empty_cluster + empty_size) {
Miao Xiea4820392013-09-09 13:19:42 +08006720 if (block_group->free_space_ctl->free_space >
6721 max_extent_size)
6722 max_extent_size =
6723 block_group->free_space_ctl->free_space;
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006724 spin_unlock(&block_group->free_space_ctl->tree_lock);
6725 goto loop;
6726 }
6727 spin_unlock(&block_group->free_space_ctl->tree_lock);
6728
Josef Bacik6226cb02009-04-03 10:14:18 -04006729 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08006730 num_bytes, empty_size,
6731 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006732 /*
6733 * If we didn't find a chunk, and we haven't failed on this
6734 * block group before, and this block group is in the middle of
6735 * caching and we are ok with waiting, then go ahead and wait
6736 * for progress to be made, and set failed_alloc to true.
6737 *
6738 * If failed_alloc is true then we've already waited on this
6739 * block group once and should move on to the next block group.
6740 */
6741 if (!offset && !failed_alloc && !cached &&
6742 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006743 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006744 num_bytes + empty_size);
6745 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006746 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006747 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08006748 if (!cached)
6749 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006750 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04006751 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006752checks:
Miao Xie215a63d2014-01-15 20:00:56 +08006753 search_start = stripe_align(root, block_group,
David Woodhouse53b381b2013-01-29 18:40:14 -05006754 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04006755
Josef Bacik2552d172009-04-03 10:14:19 -04006756 /* move on to the next group */
6757 if (search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08006758 block_group->key.objectid + block_group->key.offset) {
6759 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04006760 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04006761 }
Josef Bacik80eb2342008-10-29 14:49:05 -04006762
Josef Bacik6226cb02009-04-03 10:14:18 -04006763 if (offset < search_start)
Miao Xie215a63d2014-01-15 20:00:56 +08006764 btrfs_add_free_space(block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04006765 search_start - offset);
6766 BUG_ON(offset > search_start);
6767
Miao Xie215a63d2014-01-15 20:00:56 +08006768 ret = btrfs_update_reserved_bytes(block_group, num_bytes,
Miao Xiee570fd22014-06-19 10:42:50 +08006769 alloc_type, delalloc);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006770 if (ret == -EAGAIN) {
Miao Xie215a63d2014-01-15 20:00:56 +08006771 btrfs_add_free_space(block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006772 goto loop;
6773 }
Yan Zheng11833d62009-09-11 16:11:19 -04006774
Josef Bacik2552d172009-04-03 10:14:19 -04006775 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006776 ins->objectid = search_start;
6777 ins->offset = num_bytes;
6778
Josef Bacik3f7de032011-11-10 08:29:20 -05006779 trace_btrfs_reserve_extent(orig_root, block_group,
6780 search_start, num_bytes);
Miao Xiee570fd22014-06-19 10:42:50 +08006781 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04006782 break;
6783loop:
Josef Bacik0a243252009-09-11 16:11:20 -04006784 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006785 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006786 BUG_ON(index != get_block_group_index(block_group));
Miao Xiee570fd22014-06-19 10:42:50 +08006787 btrfs_release_block_group(block_group, delalloc);
Josef Bacik2552d172009-04-03 10:14:19 -04006788 }
6789 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05006790
Miao Xie60d2adb2011-09-09 17:34:35 +08006791 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6792 goto search;
6793
Yan, Zhengb742bb82010-05-16 10:46:24 -04006794 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6795 goto search;
6796
Josef Bacik285ff5a2012-01-13 15:27:45 -05006797 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05006798 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6799 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04006800 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6801 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6802 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6803 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04006804 */
Josef Bacik723bda22011-05-27 16:11:38 -04006805 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006806 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04006807 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04006808 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04006809 struct btrfs_trans_handle *trans;
Wang Shilongf017f152014-03-13 13:19:47 +08006810 int exist = 0;
Josef Bacik00361582013-08-14 14:02:47 -04006811
Wang Shilongf017f152014-03-13 13:19:47 +08006812 trans = current->journal_info;
6813 if (trans)
6814 exist = 1;
6815 else
6816 trans = btrfs_join_transaction(root);
6817
Josef Bacik00361582013-08-14 14:02:47 -04006818 if (IS_ERR(trans)) {
6819 ret = PTR_ERR(trans);
6820 goto out;
6821 }
6822
David Sterbab6919a52013-04-29 13:39:40 +00006823 ret = do_chunk_alloc(trans, root, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04006824 CHUNK_ALLOC_FORCE);
6825 /*
6826 * Do not bail out on ENOSPC since we
6827 * can do more things.
6828 */
Josef Bacik00361582013-08-14 14:02:47 -04006829 if (ret < 0 && ret != -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04006830 btrfs_abort_transaction(trans,
6831 root, ret);
Josef Bacik00361582013-08-14 14:02:47 -04006832 else
6833 ret = 0;
Wang Shilongf017f152014-03-13 13:19:47 +08006834 if (!exist)
6835 btrfs_end_transaction(trans, root);
Josef Bacik00361582013-08-14 14:02:47 -04006836 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04006837 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04006838 }
6839
6840 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006841 empty_size = 0;
6842 empty_cluster = 0;
6843 }
Chris Mason42e70e72008-11-07 18:17:11 -05006844
Josef Bacik723bda22011-05-27 16:11:38 -04006845 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04006846 } else if (!ins->objectid) {
6847 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04006848 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04006849 ret = 0;
6850 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006851out:
Miao Xiea4820392013-09-09 13:19:42 +08006852 if (ret == -ENOSPC)
6853 ins->offset = max_extent_size;
Chris Mason0f70abe2007-02-28 16:46:22 -05006854 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006855}
Chris Masonec44a352008-04-28 15:29:52 -04006856
Josef Bacik9ed74f22009-09-11 16:12:44 -04006857static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6858 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006859{
6860 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006861 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006862
Josef Bacik9ed74f22009-09-11 16:12:44 -04006863 spin_lock(&info->lock);
Frank Holtonefe120a2013-12-20 11:37:06 -05006864 printk(KERN_INFO "BTRFS: space_info %llu has %llu free, is %sfull\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006865 info->flags,
6866 info->total_bytes - info->bytes_used - info->bytes_pinned -
6867 info->bytes_reserved - info->bytes_readonly,
Chris Masond3977122009-01-05 21:25:51 -05006868 (info->full) ? "" : "not ");
Frank Holtonefe120a2013-12-20 11:37:06 -05006869 printk(KERN_INFO "BTRFS: space_info total=%llu, used=%llu, pinned=%llu, "
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006870 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006871 info->total_bytes, info->bytes_used, info->bytes_pinned,
6872 info->bytes_reserved, info->bytes_may_use,
6873 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006874 spin_unlock(&info->lock);
6875
6876 if (!dump_block_groups)
6877 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006878
Josef Bacik80eb2342008-10-29 14:49:05 -04006879 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006880again:
6881 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006882 spin_lock(&cache->lock);
Frank Holtonefe120a2013-12-20 11:37:06 -05006883 printk(KERN_INFO "BTRFS: "
6884 "block group %llu has %llu bytes, "
6885 "%llu used %llu pinned %llu reserved %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006886 cache->key.objectid, cache->key.offset,
6887 btrfs_block_group_used(&cache->item), cache->pinned,
6888 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006889 btrfs_dump_free_space(cache, bytes);
6890 spin_unlock(&cache->lock);
6891 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006892 if (++index < BTRFS_NR_RAID_TYPES)
6893 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006894 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006895}
Zheng Yane8569812008-09-26 10:05:48 -04006896
Josef Bacik00361582013-08-14 14:02:47 -04006897int btrfs_reserve_extent(struct btrfs_root *root,
Yan Zheng11833d62009-09-11 16:11:19 -04006898 u64 num_bytes, u64 min_alloc_size,
6899 u64 empty_size, u64 hint_byte,
Miao Xiee570fd22014-06-19 10:42:50 +08006900 struct btrfs_key *ins, int is_data, int delalloc)
Chris Masonfec577f2007-02-26 10:40:21 -05006901{
Miao Xie9e622d62012-01-26 15:01:12 -05006902 bool final_tried = false;
David Sterbab6919a52013-04-29 13:39:40 +00006903 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05006904 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006905
David Sterbab6919a52013-04-29 13:39:40 +00006906 flags = btrfs_get_alloc_profile(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04006907again:
Chris Masondb945352007-10-15 16:15:53 -04006908 WARN_ON(num_bytes < root->sectorsize);
Josef Bacik00361582013-08-14 14:02:47 -04006909 ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
Miao Xiee570fd22014-06-19 10:42:50 +08006910 flags, delalloc);
Chris Mason3b951512008-04-17 11:29:12 -04006911
Miao Xie9e622d62012-01-26 15:01:12 -05006912 if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08006913 if (!final_tried && ins->offset) {
6914 num_bytes = min(num_bytes >> 1, ins->offset);
Zach Brown24542bf2012-11-16 00:04:43 +00006915 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05006916 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006917 if (num_bytes == min_alloc_size)
6918 final_tried = true;
6919 goto again;
6920 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6921 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006922
David Sterbab6919a52013-04-29 13:39:40 +00006923 sinfo = __find_space_info(root->fs_info, flags);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006924 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006925 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006926 if (sinfo)
6927 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006928 }
Chris Mason925baed2008-06-25 16:01:30 -04006929 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006930
6931 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006932}
6933
Chris Masone688b7252011-10-31 20:52:39 -04006934static int __btrfs_free_reserved_extent(struct btrfs_root *root,
Miao Xiee570fd22014-06-19 10:42:50 +08006935 u64 start, u64 len,
6936 int pin, int delalloc)
Chris Mason65b51a02008-08-01 15:11:20 -04006937{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006938 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006939 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006940
Josef Bacik0f9dd462008-09-23 13:14:11 -04006941 cache = btrfs_lookup_block_group(root->fs_info, start);
6942 if (!cache) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006943 btrfs_err(root->fs_info, "Unable to find block group for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006944 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006945 return -ENOSPC;
6946 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006947
Li Dongyang5378e602011-03-24 10:24:27 +00006948 if (btrfs_test_opt(root, DISCARD))
6949 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006950
Chris Masone688b7252011-10-31 20:52:39 -04006951 if (pin)
6952 pin_down_extent(root, cache, start, len, 1);
6953 else {
6954 btrfs_add_free_space(cache, start, len);
Miao Xiee570fd22014-06-19 10:42:50 +08006955 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04006956 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006957 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006958
liubo1abe9b82011-03-24 11:18:59 +00006959 trace_btrfs_reserved_extent_free(root, start, len);
6960
Chris Masone6dcd2d2008-07-17 12:53:50 -04006961 return ret;
6962}
6963
Chris Masone688b7252011-10-31 20:52:39 -04006964int btrfs_free_reserved_extent(struct btrfs_root *root,
Miao Xiee570fd22014-06-19 10:42:50 +08006965 u64 start, u64 len, int delalloc)
Chris Masone688b7252011-10-31 20:52:39 -04006966{
Miao Xiee570fd22014-06-19 10:42:50 +08006967 return __btrfs_free_reserved_extent(root, start, len, 0, delalloc);
Chris Masone688b7252011-10-31 20:52:39 -04006968}
6969
6970int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6971 u64 start, u64 len)
6972{
Miao Xiee570fd22014-06-19 10:42:50 +08006973 return __btrfs_free_reserved_extent(root, start, len, 1, 0);
Chris Masone688b7252011-10-31 20:52:39 -04006974}
6975
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006976static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6977 struct btrfs_root *root,
6978 u64 parent, u64 root_objectid,
6979 u64 flags, u64 owner, u64 offset,
6980 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006981{
6982 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006983 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006984 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006985 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006986 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006987 struct extent_buffer *leaf;
6988 int type;
6989 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006990
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006991 if (parent > 0)
6992 type = BTRFS_SHARED_DATA_REF_KEY;
6993 else
6994 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006995
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006996 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006997
6998 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006999 if (!path)
7000 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05007001
Chris Masonb9473432009-03-13 11:00:37 -04007002 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007003 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
7004 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007005 if (ret) {
7006 btrfs_free_path(path);
7007 return ret;
7008 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04007009
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007010 leaf = path->nodes[0];
7011 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05007012 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007013 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
7014 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
7015 btrfs_set_extent_flags(leaf, extent_item,
7016 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05007017
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007018 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
7019 btrfs_set_extent_inline_ref_type(leaf, iref, type);
7020 if (parent > 0) {
7021 struct btrfs_shared_data_ref *ref;
7022 ref = (struct btrfs_shared_data_ref *)(iref + 1);
7023 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
7024 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
7025 } else {
7026 struct btrfs_extent_data_ref *ref;
7027 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
7028 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
7029 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
7030 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
7031 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
7032 }
Chris Mason47e4bb92008-02-01 14:51:59 -05007033
7034 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05007035 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04007036
Josef Bacikfcebe452014-05-13 17:30:47 -07007037 /* Always set parent to 0 here since its exclusive anyway. */
7038 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
7039 ins->objectid, ins->offset,
7040 BTRFS_QGROUP_OPER_ADD_EXCL, 0);
7041 if (ret)
7042 return ret;
7043
Liu Boc53d6132012-12-27 09:01:19 +00007044 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007045 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007046 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007047 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05007048 BUG();
7049 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04007050 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007051 return ret;
7052}
7053
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007054static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
7055 struct btrfs_root *root,
7056 u64 parent, u64 root_objectid,
7057 u64 flags, struct btrfs_disk_key *key,
Josef Bacikfcebe452014-05-13 17:30:47 -07007058 int level, struct btrfs_key *ins,
7059 int no_quota)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007060{
7061 int ret;
7062 struct btrfs_fs_info *fs_info = root->fs_info;
7063 struct btrfs_extent_item *extent_item;
7064 struct btrfs_tree_block_info *block_info;
7065 struct btrfs_extent_inline_ref *iref;
7066 struct btrfs_path *path;
7067 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05007068 u32 size = sizeof(*extent_item) + sizeof(*iref);
Josef Bacikfcebe452014-05-13 17:30:47 -07007069 u64 num_bytes = ins->offset;
Josef Bacik3173a182013-03-07 14:22:04 -05007070 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
7071 SKINNY_METADATA);
7072
7073 if (!skinny_metadata)
7074 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007075
7076 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04007077 if (!path) {
7078 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
7079 root->leafsize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07007080 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04007081 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007082
7083 path->leave_spinning = 1;
7084 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
7085 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007086 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04007087 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
7088 root->leafsize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007089 btrfs_free_path(path);
7090 return ret;
7091 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007092
7093 leaf = path->nodes[0];
7094 extent_item = btrfs_item_ptr(leaf, path->slots[0],
7095 struct btrfs_extent_item);
7096 btrfs_set_extent_refs(leaf, extent_item, 1);
7097 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
7098 btrfs_set_extent_flags(leaf, extent_item,
7099 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007100
Josef Bacik3173a182013-03-07 14:22:04 -05007101 if (skinny_metadata) {
7102 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
Josef Bacikfcebe452014-05-13 17:30:47 -07007103 num_bytes = root->leafsize;
Josef Bacik3173a182013-03-07 14:22:04 -05007104 } else {
7105 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
7106 btrfs_set_tree_block_key(leaf, block_info, key);
7107 btrfs_set_tree_block_level(leaf, block_info, level);
7108 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
7109 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007110
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007111 if (parent > 0) {
7112 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
7113 btrfs_set_extent_inline_ref_type(leaf, iref,
7114 BTRFS_SHARED_BLOCK_REF_KEY);
7115 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
7116 } else {
7117 btrfs_set_extent_inline_ref_type(leaf, iref,
7118 BTRFS_TREE_BLOCK_REF_KEY);
7119 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
7120 }
7121
7122 btrfs_mark_buffer_dirty(leaf);
7123 btrfs_free_path(path);
7124
Josef Bacikfcebe452014-05-13 17:30:47 -07007125 if (!no_quota) {
7126 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
7127 ins->objectid, num_bytes,
7128 BTRFS_QGROUP_OPER_ADD_EXCL, 0);
7129 if (ret)
7130 return ret;
7131 }
7132
Josef Bacik3173a182013-03-07 14:22:04 -05007133 ret = update_block_group(root, ins->objectid, root->leafsize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007134 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007135 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007136 ins->objectid, ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007137 BUG();
7138 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04007139
7140 trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->leafsize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007141 return ret;
7142}
7143
7144int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
7145 struct btrfs_root *root,
7146 u64 root_objectid, u64 owner,
7147 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007148{
7149 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04007150
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007151 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04007152
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007153 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
7154 ins->offset, 0,
7155 root_objectid, owner, offset,
7156 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007157 return ret;
7158}
Chris Masone02119d2008-09-05 16:13:11 -04007159
7160/*
7161 * this is used by the tree logging recovery code. It records that
7162 * an extent has been allocated and makes sure to clear the free
7163 * space cache bits as well
7164 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007165int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
7166 struct btrfs_root *root,
7167 u64 root_objectid, u64 owner, u64 offset,
7168 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04007169{
7170 int ret;
7171 struct btrfs_block_group_cache *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04007172
7173 /*
7174 * Mixed block groups will exclude before processing the log so we only
7175 * need to do the exlude dance if this fs isn't mixed.
7176 */
7177 if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
7178 ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
7179 if (ret)
7180 return ret;
7181 }
Chris Masone02119d2008-09-05 16:13:11 -04007182
Chris Masone02119d2008-09-05 16:13:11 -04007183 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04007184 if (!block_group)
7185 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04007186
Josef Bacikfb25e912011-07-26 17:00:46 -04007187 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
Miao Xiee570fd22014-06-19 10:42:50 +08007188 RESERVE_ALLOC_NO_ACCOUNT, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007189 BUG_ON(ret); /* logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007190 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
7191 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04007192 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04007193 return ret;
7194}
7195
Eric Sandeen48a3b632013-04-25 20:41:01 +00007196static struct extent_buffer *
7197btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
7198 u64 bytenr, u32 blocksize, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04007199{
7200 struct extent_buffer *buf;
7201
7202 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
7203 if (!buf)
7204 return ERR_PTR(-ENOMEM);
7205 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04007206 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04007207 btrfs_tree_lock(buf);
7208 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05007209 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05007210
7211 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04007212 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05007213
Chris Masond0c803c2008-09-11 16:17:57 -04007214 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00007215 /*
7216 * we allow two log transactions at a time, use different
7217 * EXENT bit to differentiate dirty pages.
7218 */
7219 if (root->log_transid % 2 == 0)
7220 set_extent_dirty(&root->dirty_log_pages, buf->start,
7221 buf->start + buf->len - 1, GFP_NOFS);
7222 else
7223 set_extent_new(&root->dirty_log_pages, buf->start,
7224 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04007225 } else {
7226 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
7227 buf->start + buf->len - 1, GFP_NOFS);
7228 }
Chris Mason65b51a02008-08-01 15:11:20 -04007229 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05007230 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04007231 return buf;
7232}
7233
Yan, Zhengf0486c62010-05-16 10:46:25 -04007234static struct btrfs_block_rsv *
7235use_block_rsv(struct btrfs_trans_handle *trans,
7236 struct btrfs_root *root, u32 blocksize)
7237{
7238 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00007239 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007240 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00007241 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007242
7243 block_rsv = get_block_rsv(trans, root);
7244
Miao Xieb586b322013-05-13 13:55:10 +00007245 if (unlikely(block_rsv->size == 0))
7246 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00007247again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007248 ret = block_rsv_use_bytes(block_rsv, blocksize);
7249 if (!ret)
7250 return block_rsv;
7251
Miao Xieb586b322013-05-13 13:55:10 +00007252 if (block_rsv->failfast)
7253 return ERR_PTR(ret);
7254
Miao Xied88033d2013-05-13 13:55:12 +00007255 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
7256 global_updated = true;
7257 update_global_block_rsv(root->fs_info);
7258 goto again;
7259 }
7260
Miao Xieb586b322013-05-13 13:55:10 +00007261 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
7262 static DEFINE_RATELIMIT_STATE(_rs,
7263 DEFAULT_RATELIMIT_INTERVAL * 10,
7264 /*DEFAULT_RATELIMIT_BURST*/ 1);
7265 if (__ratelimit(&_rs))
7266 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05007267 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00007268 }
7269try_reserve:
7270 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
7271 BTRFS_RESERVE_NO_FLUSH);
7272 if (!ret)
7273 return block_rsv;
7274 /*
7275 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00007276 * the global reserve if its space type is the same as the global
7277 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00007278 */
Miao Xie5881cfc2013-05-13 13:55:11 +00007279 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
7280 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00007281 ret = block_rsv_use_bytes(global_rsv, blocksize);
7282 if (!ret)
7283 return global_rsv;
7284 }
7285 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007286}
7287
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007288static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
7289 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007290{
7291 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007292 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007293}
7294
Chris Masonfec577f2007-02-26 10:40:21 -05007295/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007296 * finds a free extent and does all the dirty work required for allocation
7297 * returns the key for the extent through ins, and a tree buffer for
7298 * the first block of the extent through buf.
7299 *
Chris Masonfec577f2007-02-26 10:40:21 -05007300 * returns the tree buffer or NULL.
7301 */
Chris Mason5f39d392007-10-15 16:14:19 -04007302struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007303 struct btrfs_root *root, u32 blocksize,
7304 u64 parent, u64 root_objectid,
7305 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02007306 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05007307{
Chris Masone2fa7222007-03-12 16:22:34 -04007308 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007309 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04007310 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007311 u64 flags = 0;
7312 int ret;
Josef Bacik3173a182013-03-07 14:22:04 -05007313 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
7314 SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007315
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04007316#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
7317 if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state))) {
7318 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
7319 blocksize, level);
7320 if (!IS_ERR(buf))
7321 root->alloc_bytenr += blocksize;
7322 return buf;
7323 }
7324#endif
Yan, Zhengf0486c62010-05-16 10:46:25 -04007325 block_rsv = use_block_rsv(trans, root, blocksize);
7326 if (IS_ERR(block_rsv))
7327 return ERR_CAST(block_rsv);
7328
Josef Bacik00361582013-08-14 14:02:47 -04007329 ret = btrfs_reserve_extent(root, blocksize, blocksize,
Miao Xiee570fd22014-06-19 10:42:50 +08007330 empty_size, hint, &ins, 0, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05007331 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007332 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007333 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05007334 }
Chris Mason55c69072008-01-09 15:55:33 -05007335
Chris Mason4008c042009-02-12 14:09:45 -05007336 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
7337 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007338 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007339
7340 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
7341 if (parent == 0)
7342 parent = ins.objectid;
7343 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7344 } else
7345 BUG_ON(parent > 0);
7346
7347 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
7348 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00007349 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007350 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007351 if (key)
7352 memcpy(&extent_op->key, key, sizeof(extent_op->key));
7353 else
7354 memset(&extent_op->key, 0, sizeof(extent_op->key));
7355 extent_op->flags_to_set = flags;
Josef Bacik3173a182013-03-07 14:22:04 -05007356 if (skinny_metadata)
7357 extent_op->update_key = 0;
7358 else
7359 extent_op->update_key = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007360 extent_op->update_flags = 1;
7361 extent_op->is_data = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04007362 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007363
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007364 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
7365 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007366 ins.offset, parent, root_objectid,
7367 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02007368 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007369 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007370 }
Chris Masonfec577f2007-02-26 10:40:21 -05007371 return buf;
7372}
Chris Masona28ec192007-03-06 20:08:01 -05007373
Yan Zheng2c47e6052009-06-27 21:07:35 -04007374struct walk_control {
7375 u64 refs[BTRFS_MAX_LEVEL];
7376 u64 flags[BTRFS_MAX_LEVEL];
7377 struct btrfs_key update_progress;
7378 int stage;
7379 int level;
7380 int shared_level;
7381 int update_ref;
7382 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007383 int reada_slot;
7384 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007385 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007386};
7387
7388#define DROP_REFERENCE 1
7389#define UPDATE_BACKREF 2
7390
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007391static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
7392 struct btrfs_root *root,
7393 struct walk_control *wc,
7394 struct btrfs_path *path)
7395{
7396 u64 bytenr;
7397 u64 generation;
7398 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007399 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007400 u32 nritems;
7401 u32 blocksize;
7402 struct btrfs_key key;
7403 struct extent_buffer *eb;
7404 int ret;
7405 int slot;
7406 int nread = 0;
7407
7408 if (path->slots[wc->level] < wc->reada_slot) {
7409 wc->reada_count = wc->reada_count * 2 / 3;
7410 wc->reada_count = max(wc->reada_count, 2);
7411 } else {
7412 wc->reada_count = wc->reada_count * 3 / 2;
7413 wc->reada_count = min_t(int, wc->reada_count,
7414 BTRFS_NODEPTRS_PER_BLOCK(root));
7415 }
7416
7417 eb = path->nodes[wc->level];
7418 nritems = btrfs_header_nritems(eb);
7419 blocksize = btrfs_level_size(root, wc->level - 1);
7420
7421 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
7422 if (nread >= wc->reada_count)
7423 break;
7424
7425 cond_resched();
7426 bytenr = btrfs_node_blockptr(eb, slot);
7427 generation = btrfs_node_ptr_generation(eb, slot);
7428
7429 if (slot == path->slots[wc->level])
7430 goto reada;
7431
7432 if (wc->stage == UPDATE_BACKREF &&
7433 generation <= root->root_key.offset)
7434 continue;
7435
Yan, Zheng94fcca92009-10-09 09:25:16 -04007436 /* We don't lock the tree block, it's OK to be racy here */
Josef Bacik3173a182013-03-07 14:22:04 -05007437 ret = btrfs_lookup_extent_info(trans, root, bytenr,
7438 wc->level - 1, 1, &refs,
7439 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007440 /* We don't care about errors in readahead. */
7441 if (ret < 0)
7442 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007443 BUG_ON(refs == 0);
7444
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007445 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007446 if (refs == 1)
7447 goto reada;
7448
Yan, Zheng94fcca92009-10-09 09:25:16 -04007449 if (wc->level == 1 &&
7450 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7451 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007452 if (!wc->update_ref ||
7453 generation <= root->root_key.offset)
7454 continue;
7455 btrfs_node_key_to_cpu(eb, &key, slot);
7456 ret = btrfs_comp_cpu_keys(&key,
7457 &wc->update_progress);
7458 if (ret < 0)
7459 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007460 } else {
7461 if (wc->level == 1 &&
7462 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7463 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007464 }
7465reada:
7466 ret = readahead_tree_block(root, bytenr, blocksize,
7467 generation);
7468 if (ret)
7469 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007470 nread++;
7471 }
7472 wc->reada_slot = slot;
7473}
7474
Mark Fasheh11526512014-07-17 12:39:01 -07007475static int account_leaf_items(struct btrfs_trans_handle *trans,
7476 struct btrfs_root *root,
7477 struct extent_buffer *eb)
7478{
7479 int nr = btrfs_header_nritems(eb);
7480 int i, extent_type, ret;
7481 struct btrfs_key key;
7482 struct btrfs_file_extent_item *fi;
7483 u64 bytenr, num_bytes;
7484
7485 for (i = 0; i < nr; i++) {
7486 btrfs_item_key_to_cpu(eb, &key, i);
7487
7488 if (key.type != BTRFS_EXTENT_DATA_KEY)
7489 continue;
7490
7491 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
7492 /* filter out non qgroup-accountable extents */
7493 extent_type = btrfs_file_extent_type(eb, fi);
7494
7495 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
7496 continue;
7497
7498 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
7499 if (!bytenr)
7500 continue;
7501
7502 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
7503
7504 ret = btrfs_qgroup_record_ref(trans, root->fs_info,
7505 root->objectid,
7506 bytenr, num_bytes,
7507 BTRFS_QGROUP_OPER_SUB_SUBTREE, 0);
7508 if (ret)
7509 return ret;
7510 }
7511 return 0;
7512}
7513
7514/*
7515 * Walk up the tree from the bottom, freeing leaves and any interior
7516 * nodes which have had all slots visited. If a node (leaf or
7517 * interior) is freed, the node above it will have it's slot
7518 * incremented. The root node will never be freed.
7519 *
7520 * At the end of this function, we should have a path which has all
7521 * slots incremented to the next position for a search. If we need to
7522 * read a new node it will be NULL and the node above it will have the
7523 * correct slot selected for a later read.
7524 *
7525 * If we increment the root nodes slot counter past the number of
7526 * elements, 1 is returned to signal completion of the search.
7527 */
7528static int adjust_slots_upwards(struct btrfs_root *root,
7529 struct btrfs_path *path, int root_level)
7530{
7531 int level = 0;
7532 int nr, slot;
7533 struct extent_buffer *eb;
7534
7535 if (root_level == 0)
7536 return 1;
7537
7538 while (level <= root_level) {
7539 eb = path->nodes[level];
7540 nr = btrfs_header_nritems(eb);
7541 path->slots[level]++;
7542 slot = path->slots[level];
7543 if (slot >= nr || level == 0) {
7544 /*
7545 * Don't free the root - we will detect this
7546 * condition after our loop and return a
7547 * positive value for caller to stop walking the tree.
7548 */
7549 if (level != root_level) {
7550 btrfs_tree_unlock_rw(eb, path->locks[level]);
7551 path->locks[level] = 0;
7552
7553 free_extent_buffer(eb);
7554 path->nodes[level] = NULL;
7555 path->slots[level] = 0;
7556 }
7557 } else {
7558 /*
7559 * We have a valid slot to walk back down
7560 * from. Stop here so caller can process these
7561 * new nodes.
7562 */
7563 break;
7564 }
7565
7566 level++;
7567 }
7568
7569 eb = path->nodes[root_level];
7570 if (path->slots[root_level] >= btrfs_header_nritems(eb))
7571 return 1;
7572
7573 return 0;
7574}
7575
7576/*
7577 * root_eb is the subtree root and is locked before this function is called.
7578 */
7579static int account_shared_subtree(struct btrfs_trans_handle *trans,
7580 struct btrfs_root *root,
7581 struct extent_buffer *root_eb,
7582 u64 root_gen,
7583 int root_level)
7584{
7585 int ret = 0;
7586 int level;
7587 struct extent_buffer *eb = root_eb;
7588 struct btrfs_path *path = NULL;
7589
7590 BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL);
7591 BUG_ON(root_eb == NULL);
7592
7593 if (!root->fs_info->quota_enabled)
7594 return 0;
7595
7596 if (!extent_buffer_uptodate(root_eb)) {
7597 ret = btrfs_read_buffer(root_eb, root_gen);
7598 if (ret)
7599 goto out;
7600 }
7601
7602 if (root_level == 0) {
7603 ret = account_leaf_items(trans, root, root_eb);
7604 goto out;
7605 }
7606
7607 path = btrfs_alloc_path();
7608 if (!path)
7609 return -ENOMEM;
7610
7611 /*
7612 * Walk down the tree. Missing extent blocks are filled in as
7613 * we go. Metadata is accounted every time we read a new
7614 * extent block.
7615 *
7616 * When we reach a leaf, we account for file extent items in it,
7617 * walk back up the tree (adjusting slot pointers as we go)
7618 * and restart the search process.
7619 */
7620 extent_buffer_get(root_eb); /* For path */
7621 path->nodes[root_level] = root_eb;
7622 path->slots[root_level] = 0;
7623 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
7624walk_down:
7625 level = root_level;
7626 while (level >= 0) {
7627 if (path->nodes[level] == NULL) {
7628 int child_bsize = root->nodesize;
7629 int parent_slot;
7630 u64 child_gen;
7631 u64 child_bytenr;
7632
7633 /* We need to get child blockptr/gen from
7634 * parent before we can read it. */
7635 eb = path->nodes[level + 1];
7636 parent_slot = path->slots[level + 1];
7637 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
7638 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
7639
7640 eb = read_tree_block(root, child_bytenr, child_bsize,
7641 child_gen);
7642 if (!eb || !extent_buffer_uptodate(eb)) {
7643 ret = -EIO;
7644 goto out;
7645 }
7646
7647 path->nodes[level] = eb;
7648 path->slots[level] = 0;
7649
7650 btrfs_tree_read_lock(eb);
7651 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
7652 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
7653
7654 ret = btrfs_qgroup_record_ref(trans, root->fs_info,
7655 root->objectid,
7656 child_bytenr,
7657 child_bsize,
7658 BTRFS_QGROUP_OPER_SUB_SUBTREE,
7659 0);
7660 if (ret)
7661 goto out;
7662
7663 }
7664
7665 if (level == 0) {
7666 ret = account_leaf_items(trans, root, path->nodes[level]);
7667 if (ret)
7668 goto out;
7669
7670 /* Nonzero return here means we completed our search */
7671 ret = adjust_slots_upwards(root, path, root_level);
7672 if (ret)
7673 break;
7674
7675 /* Restart search with new slots */
7676 goto walk_down;
7677 }
7678
7679 level--;
7680 }
7681
7682 ret = 0;
7683out:
7684 btrfs_free_path(path);
7685
7686 return ret;
7687}
7688
Chris Mason9aca1d52007-03-13 11:09:37 -04007689/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007690 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007691 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04007692 * when wc->stage == UPDATE_BACKREF, this function updates
7693 * back refs for pointers in the block.
7694 *
7695 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04007696 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007697static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
7698 struct btrfs_root *root,
7699 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007700 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04007701{
7702 int level = wc->level;
7703 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04007704 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7705 int ret;
7706
7707 if (wc->stage == UPDATE_BACKREF &&
7708 btrfs_header_owner(eb) != root->root_key.objectid)
7709 return 1;
7710
7711 /*
7712 * when reference count of tree block is 1, it won't increase
7713 * again. once full backref flag is set, we never clear it.
7714 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04007715 if (lookup_info &&
7716 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
7717 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007718 BUG_ON(!path->locks[level]);
7719 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007720 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007721 &wc->refs[level],
7722 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007723 BUG_ON(ret == -ENOMEM);
7724 if (ret)
7725 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007726 BUG_ON(wc->refs[level] == 0);
7727 }
7728
Yan Zheng2c47e6052009-06-27 21:07:35 -04007729 if (wc->stage == DROP_REFERENCE) {
7730 if (wc->refs[level] > 1)
7731 return 1;
7732
7733 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04007734 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007735 path->locks[level] = 0;
7736 }
7737 return 0;
7738 }
7739
7740 /* wc->stage == UPDATE_BACKREF */
7741 if (!(wc->flags[level] & flag)) {
7742 BUG_ON(!path->locks[level]);
Josef Bacike339a6b2014-07-02 10:54:25 -07007743 ret = btrfs_inc_ref(trans, root, eb, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007744 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07007745 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007746 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007747 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04007748 eb->len, flag,
7749 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007750 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007751 wc->flags[level] |= flag;
7752 }
7753
7754 /*
7755 * the block is shared by multiple trees, so it's not good to
7756 * keep the tree lock
7757 */
7758 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04007759 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007760 path->locks[level] = 0;
7761 }
7762 return 0;
7763}
7764
7765/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007766 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007767 *
7768 * when wc->stage == DROP_REFERENCE, this function checks
7769 * reference count of the block pointed to. if the block
7770 * is shared and we need update back refs for the subtree
7771 * rooted at the block, this function changes wc->stage to
7772 * UPDATE_BACKREF. if the block is shared and there is no
7773 * need to update back, this function drops the reference
7774 * to the block.
7775 *
7776 * NOTE: return value 1 means we should stop walking down.
7777 */
7778static noinline int do_walk_down(struct btrfs_trans_handle *trans,
7779 struct btrfs_root *root,
7780 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007781 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007782{
7783 u64 bytenr;
7784 u64 generation;
7785 u64 parent;
7786 u32 blocksize;
7787 struct btrfs_key key;
7788 struct extent_buffer *next;
7789 int level = wc->level;
7790 int reada = 0;
7791 int ret = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07007792 bool need_account = false;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007793
7794 generation = btrfs_node_ptr_generation(path->nodes[level],
7795 path->slots[level]);
7796 /*
7797 * if the lower level block was created before the snapshot
7798 * was created, we know there is no need to update back refs
7799 * for the subtree
7800 */
7801 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04007802 generation <= root->root_key.offset) {
7803 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007804 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007805 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007806
7807 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
7808 blocksize = btrfs_level_size(root, level - 1);
7809
7810 next = btrfs_find_tree_block(root, bytenr, blocksize);
7811 if (!next) {
7812 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00007813 if (!next)
7814 return -ENOMEM;
Josef Bacikb2aaaa32013-07-05 17:05:38 -04007815 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
7816 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007817 reada = 1;
7818 }
7819 btrfs_tree_lock(next);
7820 btrfs_set_lock_blocking(next);
7821
Josef Bacik3173a182013-03-07 14:22:04 -05007822 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007823 &wc->refs[level - 1],
7824 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007825 if (ret < 0) {
7826 btrfs_tree_unlock(next);
7827 return ret;
7828 }
7829
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007830 if (unlikely(wc->refs[level - 1] == 0)) {
7831 btrfs_err(root->fs_info, "Missing references.");
7832 BUG();
7833 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007834 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007835
Yan, Zheng94fcca92009-10-09 09:25:16 -04007836 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007837 if (wc->refs[level - 1] > 1) {
Mark Fasheh11526512014-07-17 12:39:01 -07007838 need_account = true;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007839 if (level == 1 &&
7840 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7841 goto skip;
7842
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007843 if (!wc->update_ref ||
7844 generation <= root->root_key.offset)
7845 goto skip;
7846
7847 btrfs_node_key_to_cpu(path->nodes[level], &key,
7848 path->slots[level]);
7849 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
7850 if (ret < 0)
7851 goto skip;
7852
7853 wc->stage = UPDATE_BACKREF;
7854 wc->shared_level = level - 1;
7855 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007856 } else {
7857 if (level == 1 &&
7858 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7859 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007860 }
7861
Chris Masonb9fab912012-05-06 07:23:47 -04007862 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007863 btrfs_tree_unlock(next);
7864 free_extent_buffer(next);
7865 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007866 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007867 }
7868
7869 if (!next) {
7870 if (reada && level == 1)
7871 reada_walk_down(trans, root, wc, path);
7872 next = read_tree_block(root, bytenr, blocksize, generation);
Josef Bacik416bc652013-04-23 14:17:42 -04007873 if (!next || !extent_buffer_uptodate(next)) {
7874 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00007875 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04007876 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007877 btrfs_tree_lock(next);
7878 btrfs_set_lock_blocking(next);
7879 }
7880
7881 level--;
7882 BUG_ON(level != btrfs_header_level(next));
7883 path->nodes[level] = next;
7884 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007885 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007886 wc->level = level;
7887 if (wc->level == 1)
7888 wc->reada_slot = 0;
7889 return 0;
7890skip:
7891 wc->refs[level - 1] = 0;
7892 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007893 if (wc->stage == DROP_REFERENCE) {
7894 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7895 parent = path->nodes[level]->start;
7896 } else {
7897 BUG_ON(root->root_key.objectid !=
7898 btrfs_header_owner(path->nodes[level]));
7899 parent = 0;
7900 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007901
Mark Fasheh11526512014-07-17 12:39:01 -07007902 if (need_account) {
7903 ret = account_shared_subtree(trans, root, next,
7904 generation, level - 1);
7905 if (ret) {
7906 printk_ratelimited(KERN_ERR "BTRFS: %s Error "
7907 "%d accounting shared subtree. Quota "
7908 "is out of sync, rescan required.\n",
7909 root->fs_info->sb->s_id, ret);
7910 }
7911 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007912 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007913 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007914 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007915 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007916 btrfs_tree_unlock(next);
7917 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04007918 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007919 return 1;
7920}
7921
7922/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007923 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007924 *
7925 * when wc->stage == DROP_REFERENCE, this function drops
7926 * reference count on the block.
7927 *
7928 * when wc->stage == UPDATE_BACKREF, this function changes
7929 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7930 * to UPDATE_BACKREF previously while processing the block.
7931 *
7932 * NOTE: return value 1 means we should stop walking up.
7933 */
7934static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7935 struct btrfs_root *root,
7936 struct btrfs_path *path,
7937 struct walk_control *wc)
7938{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007939 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007940 int level = wc->level;
7941 struct extent_buffer *eb = path->nodes[level];
7942 u64 parent = 0;
7943
7944 if (wc->stage == UPDATE_BACKREF) {
7945 BUG_ON(wc->shared_level < level);
7946 if (level < wc->shared_level)
7947 goto out;
7948
Yan Zheng2c47e6052009-06-27 21:07:35 -04007949 ret = find_next_key(path, level + 1, &wc->update_progress);
7950 if (ret > 0)
7951 wc->update_ref = 0;
7952
7953 wc->stage = DROP_REFERENCE;
7954 wc->shared_level = -1;
7955 path->slots[level] = 0;
7956
7957 /*
7958 * check reference count again if the block isn't locked.
7959 * we should start walking down the tree again if reference
7960 * count is one.
7961 */
7962 if (!path->locks[level]) {
7963 BUG_ON(level == 0);
7964 btrfs_tree_lock(eb);
7965 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007966 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007967
7968 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007969 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007970 &wc->refs[level],
7971 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007972 if (ret < 0) {
7973 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007974 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007975 return ret;
7976 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007977 BUG_ON(wc->refs[level] == 0);
7978 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04007979 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007980 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007981 return 1;
7982 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007983 }
7984 }
7985
7986 /* wc->stage == DROP_REFERENCE */
7987 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7988
7989 if (wc->refs[level] == 1) {
7990 if (level == 0) {
7991 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Josef Bacike339a6b2014-07-02 10:54:25 -07007992 ret = btrfs_dec_ref(trans, root, eb, 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007993 else
Josef Bacike339a6b2014-07-02 10:54:25 -07007994 ret = btrfs_dec_ref(trans, root, eb, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007995 BUG_ON(ret); /* -ENOMEM */
Mark Fasheh11526512014-07-17 12:39:01 -07007996 ret = account_leaf_items(trans, root, eb);
7997 if (ret) {
7998 printk_ratelimited(KERN_ERR "BTRFS: %s Error "
7999 "%d accounting leaf items. Quota "
8000 "is out of sync, rescan required.\n",
8001 root->fs_info->sb->s_id, ret);
8002 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008003 }
8004 /* make block locked assertion in clean_tree_block happy */
8005 if (!path->locks[level] &&
8006 btrfs_header_generation(eb) == trans->transid) {
8007 btrfs_tree_lock(eb);
8008 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04008009 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008010 }
8011 clean_tree_block(trans, root, eb);
8012 }
8013
8014 if (eb == root->node) {
8015 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8016 parent = eb->start;
8017 else
8018 BUG_ON(root->root_key.objectid !=
8019 btrfs_header_owner(eb));
8020 } else {
8021 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8022 parent = path->nodes[level + 1]->start;
8023 else
8024 BUG_ON(root->root_key.objectid !=
8025 btrfs_header_owner(path->nodes[level + 1]));
8026 }
8027
Jan Schmidt5581a512012-05-16 17:04:52 +02008028 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008029out:
8030 wc->refs[level] = 0;
8031 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008032 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008033}
8034
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008035static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
8036 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008037 struct btrfs_path *path,
8038 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008039{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008040 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04008041 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008042 int ret;
8043
Yan Zheng2c47e6052009-06-27 21:07:35 -04008044 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04008045 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008046 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04008047 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008048
Yan Zheng2c47e6052009-06-27 21:07:35 -04008049 if (level == 0)
8050 break;
8051
Yan, Zheng7a7965f2010-02-01 02:41:17 +00008052 if (path->slots[level] >=
8053 btrfs_header_nritems(path->nodes[level]))
8054 break;
8055
Yan, Zheng94fcca92009-10-09 09:25:16 -04008056 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008057 if (ret > 0) {
8058 path->slots[level]++;
8059 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00008060 } else if (ret < 0)
8061 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008062 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008063 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04008064 return 0;
8065}
8066
Chris Masond3977122009-01-05 21:25:51 -05008067static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05008068 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04008069 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04008070 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05008071{
Yan Zheng2c47e6052009-06-27 21:07:35 -04008072 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05008073 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04008074
Yan Zheng2c47e6052009-06-27 21:07:35 -04008075 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
8076 while (level < max_level && path->nodes[level]) {
8077 wc->level = level;
8078 if (path->slots[level] + 1 <
8079 btrfs_header_nritems(path->nodes[level])) {
8080 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05008081 return 0;
8082 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008083 ret = walk_up_proc(trans, root, path, wc);
8084 if (ret > 0)
8085 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05008086
Yan Zheng2c47e6052009-06-27 21:07:35 -04008087 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04008088 btrfs_tree_unlock_rw(path->nodes[level],
8089 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008090 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008091 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008092 free_extent_buffer(path->nodes[level]);
8093 path->nodes[level] = NULL;
8094 level++;
Chris Mason20524f02007-03-10 06:35:47 -05008095 }
8096 }
8097 return 1;
8098}
8099
Chris Mason9aca1d52007-03-13 11:09:37 -04008100/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04008101 * drop a subvolume tree.
8102 *
8103 * this function traverses the tree freeing any blocks that only
8104 * referenced by the tree.
8105 *
8106 * when a shared tree block is found. this function decreases its
8107 * reference count by one. if update_ref is true, this function
8108 * also make sure backrefs for the shared block and all lower level
8109 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00008110 *
8111 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04008112 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04008113int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008114 struct btrfs_block_rsv *block_rsv, int update_ref,
8115 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05008116{
Chris Mason5caf2a02007-04-02 11:20:42 -04008117 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008118 struct btrfs_trans_handle *trans;
8119 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04008120 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008121 struct walk_control *wc;
8122 struct btrfs_key key;
8123 int err = 0;
8124 int ret;
8125 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04008126 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05008127
Mark Fasheh11526512014-07-17 12:39:01 -07008128 btrfs_debug(root->fs_info, "Drop subvolume %llu", root->objectid);
8129
Chris Mason5caf2a02007-04-02 11:20:42 -04008130 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00008131 if (!path) {
8132 err = -ENOMEM;
8133 goto out;
8134 }
Chris Mason20524f02007-03-10 06:35:47 -05008135
Yan Zheng2c47e6052009-06-27 21:07:35 -04008136 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07008137 if (!wc) {
8138 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00008139 err = -ENOMEM;
8140 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07008141 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008142
Yan, Zhenga22285a2010-05-16 10:48:46 -04008143 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008144 if (IS_ERR(trans)) {
8145 err = PTR_ERR(trans);
8146 goto out_free;
8147 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00008148
Yan, Zheng3fd0a552010-05-16 10:49:59 -04008149 if (block_rsv)
8150 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008151
Chris Mason9f3a7422007-08-07 15:52:19 -04008152 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008153 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008154 path->nodes[level] = btrfs_lock_root_node(root);
8155 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04008156 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008157 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008158 memset(&wc->update_progress, 0,
8159 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04008160 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04008161 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008162 memcpy(&wc->update_progress, &key,
8163 sizeof(wc->update_progress));
8164
Chris Mason6702ed42007-08-07 16:15:09 -04008165 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008166 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04008167 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008168 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
8169 path->lowest_level = 0;
8170 if (ret < 0) {
8171 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008172 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04008173 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008174 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008175
Chris Mason7d9eb122008-07-08 14:19:17 -04008176 /*
8177 * unlock our path, this is safe because only this
8178 * function is allowed to delete this snapshot
8179 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008180 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04008181
Yan Zheng2c47e6052009-06-27 21:07:35 -04008182 level = btrfs_header_level(root->node);
8183 while (1) {
8184 btrfs_tree_lock(path->nodes[level]);
8185 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04008186 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008187
8188 ret = btrfs_lookup_extent_info(trans, root,
8189 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05008190 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04008191 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008192 if (ret < 0) {
8193 err = ret;
8194 goto out_end_trans;
8195 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008196 BUG_ON(wc->refs[level] == 0);
8197
8198 if (level == root_item->drop_level)
8199 break;
8200
8201 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04008202 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008203 WARN_ON(wc->refs[level] != 1);
8204 level--;
8205 }
8206 }
8207
8208 wc->level = level;
8209 wc->shared_level = -1;
8210 wc->stage = DROP_REFERENCE;
8211 wc->update_ref = update_ref;
8212 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008213 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008214 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04008215
8216 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00008217
Yan Zheng2c47e6052009-06-27 21:07:35 -04008218 ret = walk_down_tree(trans, root, path, wc);
8219 if (ret < 0) {
8220 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04008221 break;
8222 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008223
8224 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
8225 if (ret < 0) {
8226 err = ret;
8227 break;
8228 }
8229
8230 if (ret > 0) {
8231 BUG_ON(wc->stage != DROP_REFERENCE);
8232 break;
8233 }
8234
8235 if (wc->stage == DROP_REFERENCE) {
8236 level = wc->level;
8237 btrfs_node_key(path->nodes[level],
8238 &root_item->drop_progress,
8239 path->slots[level]);
8240 root_item->drop_level = level;
8241 }
8242
8243 BUG_ON(wc->level == 0);
Josef Bacik3c8f2422013-07-15 11:57:06 -04008244 if (btrfs_should_end_transaction(trans, tree_root) ||
8245 (!for_reloc && btrfs_need_cleaner_sleep(root))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008246 ret = btrfs_update_root(trans, tree_root,
8247 &root->root_key,
8248 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008249 if (ret) {
8250 btrfs_abort_transaction(trans, tree_root, ret);
8251 err = ret;
8252 goto out_end_trans;
8253 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008254
Mark Fasheh11526512014-07-17 12:39:01 -07008255 /*
8256 * Qgroup update accounting is run from
8257 * delayed ref handling. This usually works
8258 * out because delayed refs are normally the
8259 * only way qgroup updates are added. However,
8260 * we may have added updates during our tree
8261 * walk so run qgroups here to make sure we
8262 * don't lose any updates.
8263 */
8264 ret = btrfs_delayed_qgroup_accounting(trans,
8265 root->fs_info);
8266 if (ret)
8267 printk_ratelimited(KERN_ERR "BTRFS: Failure %d "
8268 "running qgroup updates "
8269 "during snapshot delete. "
8270 "Quota is out of sync, "
8271 "rescan required.\n", ret);
8272
Yan, Zheng3fd0a552010-05-16 10:49:59 -04008273 btrfs_end_transaction_throttle(trans, tree_root);
Josef Bacik3c8f2422013-07-15 11:57:06 -04008274 if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05008275 pr_debug("BTRFS: drop snapshot early exit\n");
Josef Bacik3c8f2422013-07-15 11:57:06 -04008276 err = -EAGAIN;
8277 goto out_free;
8278 }
8279
Yan, Zhenga22285a2010-05-16 10:48:46 -04008280 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008281 if (IS_ERR(trans)) {
8282 err = PTR_ERR(trans);
8283 goto out_free;
8284 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04008285 if (block_rsv)
8286 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04008287 }
Chris Mason20524f02007-03-10 06:35:47 -05008288 }
David Sterbab3b4aa72011-04-21 01:20:15 +02008289 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008290 if (err)
8291 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008292
8293 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008294 if (ret) {
8295 btrfs_abort_transaction(trans, tree_root, ret);
8296 goto out_end_trans;
8297 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008298
Yan, Zheng76dda932009-09-21 16:00:26 -04008299 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00008300 ret = btrfs_find_root(tree_root, &root->root_key, path,
8301 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008302 if (ret < 0) {
8303 btrfs_abort_transaction(trans, tree_root, ret);
8304 err = ret;
8305 goto out_end_trans;
8306 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05008307 /* if we fail to delete the orphan item this time
8308 * around, it'll get picked up the next time.
8309 *
8310 * The most common failure here is just -ENOENT.
8311 */
8312 btrfs_del_orphan_item(trans, tree_root,
8313 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04008314 }
8315 }
8316
Miao Xie27cdeb72014-04-02 19:51:05 +08008317 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Miao Xiecb517ea2013-05-15 07:48:19 +00008318 btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04008319 } else {
8320 free_extent_buffer(root->node);
8321 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00008322 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04008323 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04008324 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008325out_end_trans:
Mark Fasheh11526512014-07-17 12:39:01 -07008326 ret = btrfs_delayed_qgroup_accounting(trans, tree_root->fs_info);
8327 if (ret)
8328 printk_ratelimited(KERN_ERR "BTRFS: Failure %d "
8329 "running qgroup updates "
8330 "during snapshot delete. "
8331 "Quota is out of sync, "
8332 "rescan required.\n", ret);
8333
Yan, Zheng3fd0a552010-05-16 10:49:59 -04008334 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008335out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04008336 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04008337 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00008338out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04008339 /*
8340 * So if we need to stop dropping the snapshot for whatever reason we
8341 * need to make sure to add it back to the dead root list so that we
8342 * keep trying to do the work later. This also cleans up roots if we
8343 * don't have it in the radix (like when we recover after a power fail
8344 * or unmount) so we don't leak memory.
8345 */
Josef Bacikb37b39c2013-07-23 16:57:15 -04008346 if (!for_reloc && root_dropped == false)
Josef Bacikd29a9f62013-07-17 19:30:20 -04008347 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08008348 if (err && err != -EAGAIN)
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00008349 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04008350 return err;
Chris Mason20524f02007-03-10 06:35:47 -05008351}
Chris Mason9078a3e2007-04-26 16:46:15 -04008352
Yan Zheng2c47e6052009-06-27 21:07:35 -04008353/*
8354 * drop subtree rooted at tree block 'node'.
8355 *
8356 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008357 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04008358 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04008359int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
8360 struct btrfs_root *root,
8361 struct extent_buffer *node,
8362 struct extent_buffer *parent)
8363{
8364 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008365 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008366 int level;
8367 int parent_level;
8368 int ret = 0;
8369 int wret;
8370
Yan Zheng2c47e6052009-06-27 21:07:35 -04008371 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
8372
Yan Zhengf82d02d2008-10-29 14:49:05 -04008373 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00008374 if (!path)
8375 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008376
Yan Zheng2c47e6052009-06-27 21:07:35 -04008377 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00008378 if (!wc) {
8379 btrfs_free_path(path);
8380 return -ENOMEM;
8381 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04008382
Chris Masonb9447ef2009-03-09 11:45:38 -04008383 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008384 parent_level = btrfs_header_level(parent);
8385 extent_buffer_get(parent);
8386 path->nodes[parent_level] = parent;
8387 path->slots[parent_level] = btrfs_header_nritems(parent);
8388
Chris Masonb9447ef2009-03-09 11:45:38 -04008389 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008390 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008391 path->nodes[level] = node;
8392 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04008393 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008394
8395 wc->refs[parent_level] = 1;
8396 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8397 wc->level = level;
8398 wc->shared_level = -1;
8399 wc->stage = DROP_REFERENCE;
8400 wc->update_ref = 0;
8401 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02008402 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04008403 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008404
8405 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04008406 wret = walk_down_tree(trans, root, path, wc);
8407 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04008408 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04008409 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04008410 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04008411
Yan Zheng2c47e6052009-06-27 21:07:35 -04008412 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008413 if (wret < 0)
8414 ret = wret;
8415 if (wret != 0)
8416 break;
8417 }
8418
Yan Zheng2c47e6052009-06-27 21:07:35 -04008419 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04008420 btrfs_free_path(path);
8421 return ret;
8422}
8423
Chris Masonec44a352008-04-28 15:29:52 -04008424static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8425{
8426 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008427 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04008428
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008429 /*
8430 * if restripe for this chunk_type is on pick target profile and
8431 * return, otherwise do the usual balance
8432 */
8433 stripped = get_restripe_target(root->fs_info, flags);
8434 if (stripped)
8435 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02008436
Miao Xie95669972014-07-24 11:37:14 +08008437 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05008438
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008439 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008440 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008441 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8442
Chris Masonec44a352008-04-28 15:29:52 -04008443 if (num_devices == 1) {
8444 stripped |= BTRFS_BLOCK_GROUP_DUP;
8445 stripped = flags & ~stripped;
8446
8447 /* turn raid0 into single device chunks */
8448 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8449 return stripped;
8450
8451 /* turn mirroring into duplication */
8452 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8453 BTRFS_BLOCK_GROUP_RAID10))
8454 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04008455 } else {
8456 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04008457 if (flags & stripped)
8458 return flags;
8459
8460 stripped |= BTRFS_BLOCK_GROUP_DUP;
8461 stripped = flags & ~stripped;
8462
8463 /* switch duplicated blocks with raid1 */
8464 if (flags & BTRFS_BLOCK_GROUP_DUP)
8465 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8466
Ilya Dryomove3176ca2012-03-27 17:09:16 +03008467 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04008468 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03008469
Chris Masonec44a352008-04-28 15:29:52 -04008470 return flags;
8471}
8472
Miao Xie199c36e2011-07-15 10:34:36 +00008473static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04008474{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008475 struct btrfs_space_info *sinfo = cache->space_info;
8476 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00008477 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008478 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04008479
Chris Masonc286ac42008-07-22 23:06:41 -04008480
Miao Xie199c36e2011-07-15 10:34:36 +00008481 /*
8482 * We need some metadata space and system metadata space for
8483 * allocating chunks in some corner cases until we force to set
8484 * it to be readonly.
8485 */
8486 if ((sinfo->flags &
8487 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
8488 !force)
8489 min_allocable_bytes = 1 * 1024 * 1024;
8490 else
8491 min_allocable_bytes = 0;
8492
Yan, Zhengf0486c62010-05-16 10:46:25 -04008493 spin_lock(&sinfo->lock);
8494 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00008495
8496 if (cache->ro) {
8497 ret = 0;
8498 goto out;
8499 }
8500
Yan, Zhengf0486c62010-05-16 10:46:25 -04008501 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8502 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04008503
Yan, Zhengf0486c62010-05-16 10:46:25 -04008504 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04008505 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
8506 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04008507 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008508 cache->ro = 1;
8509 ret = 0;
8510 }
WuBo61cfea92011-07-26 03:30:11 +00008511out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04008512 spin_unlock(&cache->lock);
8513 spin_unlock(&sinfo->lock);
8514 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04008515}
8516
Yan, Zhengf0486c62010-05-16 10:46:25 -04008517int btrfs_set_block_group_ro(struct btrfs_root *root,
8518 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008519
8520{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008521 struct btrfs_trans_handle *trans;
8522 u64 alloc_flags;
8523 int ret;
8524
8525 BUG_ON(cache->ro);
8526
Josef Bacik7a7eaa42011-04-13 12:54:33 -04008527 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008528 if (IS_ERR(trans))
8529 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008530
8531 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008532 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04008533 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008534 CHUNK_ALLOC_FORCE);
8535 if (ret < 0)
8536 goto out;
8537 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008538
Miao Xie199c36e2011-07-15 10:34:36 +00008539 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008540 if (!ret)
8541 goto out;
8542 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04008543 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04008544 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008545 if (ret < 0)
8546 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00008547 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008548out:
8549 btrfs_end_transaction(trans, root);
8550 return ret;
8551}
8552
Chris Masonc87f08c2011-02-16 13:57:04 -05008553int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
8554 struct btrfs_root *root, u64 type)
8555{
8556 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04008557 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04008558 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05008559}
8560
Miao Xie6d07bce2011-01-05 10:07:31 +00008561/*
8562 * helper to account the unused space of all the readonly block group in the
8563 * list. takes mirrors into account.
8564 */
8565static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8566{
8567 struct btrfs_block_group_cache *block_group;
8568 u64 free_bytes = 0;
8569 int factor;
8570
8571 list_for_each_entry(block_group, groups_list, list) {
8572 spin_lock(&block_group->lock);
8573
8574 if (!block_group->ro) {
8575 spin_unlock(&block_group->lock);
8576 continue;
8577 }
8578
8579 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8580 BTRFS_BLOCK_GROUP_RAID10 |
8581 BTRFS_BLOCK_GROUP_DUP))
8582 factor = 2;
8583 else
8584 factor = 1;
8585
8586 free_bytes += (block_group->key.offset -
8587 btrfs_block_group_used(&block_group->item)) *
8588 factor;
8589
8590 spin_unlock(&block_group->lock);
8591 }
8592
8593 return free_bytes;
8594}
8595
8596/*
8597 * helper to account the unused space of all the readonly block group in the
8598 * space_info. takes mirrors into account.
8599 */
8600u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8601{
8602 int i;
8603 u64 free_bytes = 0;
8604
8605 spin_lock(&sinfo->lock);
8606
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308607 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
Miao Xie6d07bce2011-01-05 10:07:31 +00008608 if (!list_empty(&sinfo->block_groups[i]))
8609 free_bytes += __btrfs_get_ro_block_group_free_space(
8610 &sinfo->block_groups[i]);
8611
8612 spin_unlock(&sinfo->lock);
8613
8614 return free_bytes;
8615}
8616
Jeff Mahoney143bede2012-03-01 14:56:26 +01008617void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04008618 struct btrfs_block_group_cache *cache)
8619{
8620 struct btrfs_space_info *sinfo = cache->space_info;
8621 u64 num_bytes;
8622
8623 BUG_ON(!cache->ro);
8624
8625 spin_lock(&sinfo->lock);
8626 spin_lock(&cache->lock);
8627 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8628 cache->bytes_super - btrfs_block_group_used(&cache->item);
8629 sinfo->bytes_readonly -= num_bytes;
8630 cache->ro = 0;
8631 spin_unlock(&cache->lock);
8632 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008633}
8634
Josef Bacikba1bf482009-09-11 16:11:19 -04008635/*
8636 * checks to see if its even possible to relocate this block group.
8637 *
8638 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8639 * ok to go ahead and try.
8640 */
8641int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04008642{
Zheng Yan1a40e232008-09-26 10:09:34 -04008643 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04008644 struct btrfs_space_info *space_info;
8645 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8646 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04008647 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00008648 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04008649 u64 dev_min = 1;
8650 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008651 u64 target;
liubocdcb7252011-08-03 10:15:25 +00008652 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04008653 int full = 0;
8654 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008655
Josef Bacikba1bf482009-09-11 16:11:19 -04008656 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008657
Josef Bacikba1bf482009-09-11 16:11:19 -04008658 /* odd, couldn't find the block group, leave it alone */
8659 if (!block_group)
8660 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008661
liubocdcb7252011-08-03 10:15:25 +00008662 min_free = btrfs_block_group_used(&block_group->item);
8663
Josef Bacikba1bf482009-09-11 16:11:19 -04008664 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00008665 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04008666 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008667
Josef Bacikba1bf482009-09-11 16:11:19 -04008668 space_info = block_group->space_info;
8669 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008670
Josef Bacikba1bf482009-09-11 16:11:19 -04008671 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008672
Josef Bacikba1bf482009-09-11 16:11:19 -04008673 /*
8674 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008675 * relocate it unless we're able to allocate a new chunk below.
8676 *
8677 * Otherwise, we need to make sure we have room in the space to handle
8678 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008679 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008680 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00008681 (space_info->bytes_used + space_info->bytes_reserved +
8682 space_info->bytes_pinned + space_info->bytes_readonly +
8683 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008684 spin_unlock(&space_info->lock);
8685 goto out;
8686 }
8687 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008688
Josef Bacikba1bf482009-09-11 16:11:19 -04008689 /*
8690 * ok we don't have enough space, but maybe we have free space on our
8691 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008692 * alloc devices and guess if we have enough space. if this block
8693 * group is going to be restriped, run checks against the target
8694 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04008695 */
8696 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05008697
liubocdcb7252011-08-03 10:15:25 +00008698 /*
8699 * index:
8700 * 0: raid10
8701 * 1: raid1
8702 * 2: dup
8703 * 3: raid0
8704 * 4: single
8705 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008706 target = get_restripe_target(root->fs_info, block_group->flags);
8707 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00008708 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008709 } else {
8710 /*
8711 * this is just a balance, so if we were marked as full
8712 * we know there is no space for a new chunk
8713 */
8714 if (full)
8715 goto out;
8716
8717 index = get_block_group_index(block_group);
8718 }
8719
Miao Xiee6ec7162013-01-17 05:38:51 +00008720 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00008721 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04008722 /* Divide by 2 */
8723 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008724 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00008725 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00008726 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04008727 /* Multiply by 2 */
8728 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008729 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00008730 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04008731 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00008732 }
8733
Josef Bacik6df9a952013-06-27 13:22:46 -04008734 /* We need to do this so that we can look at pending chunks */
8735 trans = btrfs_join_transaction(root);
8736 if (IS_ERR(trans)) {
8737 ret = PTR_ERR(trans);
8738 goto out;
8739 }
8740
Josef Bacikba1bf482009-09-11 16:11:19 -04008741 mutex_lock(&root->fs_info->chunk_mutex);
8742 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00008743 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008744
Josef Bacikba1bf482009-09-11 16:11:19 -04008745 /*
8746 * check to make sure we can actually find a chunk with enough
8747 * space to fit our block group in.
8748 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01008749 if (device->total_bytes > device->bytes_used + min_free &&
8750 !device->is_tgtdev_for_dev_replace) {
Josef Bacik6df9a952013-06-27 13:22:46 -04008751 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008752 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008753 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00008754 dev_nr++;
8755
8756 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05008757 break;
liubocdcb7252011-08-03 10:15:25 +00008758
Josef Bacikba1bf482009-09-11 16:11:19 -04008759 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008760 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008761 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008762 mutex_unlock(&root->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04008763 btrfs_end_transaction(trans, root);
Chris Masonedbd8d42007-12-21 16:27:24 -05008764out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008765 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008766 return ret;
8767}
8768
Christoph Hellwigb2950862008-12-02 09:54:17 -05008769static int find_first_block_group(struct btrfs_root *root,
8770 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008771{
Chris Mason925baed2008-06-25 16:01:30 -04008772 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008773 struct btrfs_key found_key;
8774 struct extent_buffer *leaf;
8775 int slot;
8776
8777 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8778 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008779 goto out;
8780
Chris Masond3977122009-01-05 21:25:51 -05008781 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008782 slot = path->slots[0];
8783 leaf = path->nodes[0];
8784 if (slot >= btrfs_header_nritems(leaf)) {
8785 ret = btrfs_next_leaf(root, path);
8786 if (ret == 0)
8787 continue;
8788 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008789 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008790 break;
8791 }
8792 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8793
8794 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008795 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8796 ret = 0;
8797 goto out;
8798 }
Chris Mason0b86a832008-03-24 15:01:56 -04008799 path->slots[0]++;
8800 }
Chris Mason925baed2008-06-25 16:01:30 -04008801out:
Chris Mason0b86a832008-03-24 15:01:56 -04008802 return ret;
8803}
8804
Josef Bacik0af3d002010-06-21 14:48:16 -04008805void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8806{
8807 struct btrfs_block_group_cache *block_group;
8808 u64 last = 0;
8809
8810 while (1) {
8811 struct inode *inode;
8812
8813 block_group = btrfs_lookup_first_block_group(info, last);
8814 while (block_group) {
8815 spin_lock(&block_group->lock);
8816 if (block_group->iref)
8817 break;
8818 spin_unlock(&block_group->lock);
8819 block_group = next_block_group(info->tree_root,
8820 block_group);
8821 }
8822 if (!block_group) {
8823 if (last == 0)
8824 break;
8825 last = 0;
8826 continue;
8827 }
8828
8829 inode = block_group->inode;
8830 block_group->iref = 0;
8831 block_group->inode = NULL;
8832 spin_unlock(&block_group->lock);
8833 iput(inode);
8834 last = block_group->key.objectid + block_group->key.offset;
8835 btrfs_put_block_group(block_group);
8836 }
8837}
8838
Zheng Yan1a40e232008-09-26 10:09:34 -04008839int btrfs_free_block_groups(struct btrfs_fs_info *info)
8840{
8841 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008842 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008843 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008844 struct rb_node *n;
8845
Josef Bacik9e351cc2014-03-13 15:42:13 -04008846 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04008847 while (!list_empty(&info->caching_block_groups)) {
8848 caching_ctl = list_entry(info->caching_block_groups.next,
8849 struct btrfs_caching_control, list);
8850 list_del(&caching_ctl->list);
8851 put_caching_control(caching_ctl);
8852 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04008853 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04008854
Zheng Yan1a40e232008-09-26 10:09:34 -04008855 spin_lock(&info->block_group_cache_lock);
8856 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8857 block_group = rb_entry(n, struct btrfs_block_group_cache,
8858 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008859 rb_erase(&block_group->cache_node,
8860 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008861 spin_unlock(&info->block_group_cache_lock);
8862
Josef Bacik80eb2342008-10-29 14:49:05 -04008863 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008864 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008865 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008866
Josef Bacik817d52f2009-07-13 21:29:25 -04008867 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008868 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008869
Josef Bacik3c148742011-02-02 15:53:47 +00008870 /*
8871 * We haven't cached this block group, which means we could
8872 * possibly have excluded extents on this block group.
8873 */
Josef Bacik36cce922013-08-05 11:15:21 -04008874 if (block_group->cached == BTRFS_CACHE_NO ||
8875 block_group->cached == BTRFS_CACHE_ERROR)
Josef Bacik3c148742011-02-02 15:53:47 +00008876 free_excluded_extents(info->extent_root, block_group);
8877
Josef Bacik817d52f2009-07-13 21:29:25 -04008878 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008879 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008880
8881 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008882 }
8883 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008884
8885 /* now that all the block groups are freed, go through and
8886 * free all the space_info structs. This is only called during
8887 * the final stages of unmount, and so we know nobody is
8888 * using them. We call synchronize_rcu() once before we start,
8889 * just to be on the safe side.
8890 */
8891 synchronize_rcu();
8892
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008893 release_global_block_rsv(info);
8894
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308895 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008896 int i;
8897
Chris Mason4184ea72009-03-10 12:39:20 -04008898 space_info = list_entry(info->space_info.next,
8899 struct btrfs_space_info,
8900 list);
David Sterbab069e0c2013-02-08 21:28:17 +00008901 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308902 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00008903 space_info->bytes_reserved > 0 ||
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308904 space_info->bytes_may_use > 0)) {
David Sterbab069e0c2013-02-08 21:28:17 +00008905 dump_space_info(space_info, 0, 0);
8906 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008907 }
Chris Mason4184ea72009-03-10 12:39:20 -04008908 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008909 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
8910 struct kobject *kobj;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04008911 kobj = space_info->block_group_kobjs[i];
8912 space_info->block_group_kobjs[i] = NULL;
8913 if (kobj) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008914 kobject_del(kobj);
8915 kobject_put(kobj);
8916 }
8917 }
8918 kobject_del(&space_info->kobj);
8919 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04008920 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008921 return 0;
8922}
8923
Yan, Zhengb742bb82010-05-16 10:46:24 -04008924static void __link_block_group(struct btrfs_space_info *space_info,
8925 struct btrfs_block_group_cache *cache)
8926{
8927 int index = get_block_group_index(cache);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04008928 bool first = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04008929
8930 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04008931 if (list_empty(&space_info->block_groups[index]))
8932 first = true;
8933 list_add_tail(&cache->list, &space_info->block_groups[index]);
8934 up_write(&space_info->groups_sem);
8935
8936 if (first) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -04008937 struct raid_kobject *rkobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008938 int ret;
8939
Jeff Mahoneyc1895442014-05-27 12:59:57 -04008940 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
8941 if (!rkobj)
8942 goto out_err;
8943 rkobj->raid_type = index;
8944 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
8945 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
8946 "%s", get_raid_name(index));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008947 if (ret) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -04008948 kobject_put(&rkobj->kobj);
8949 goto out_err;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008950 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -04008951 space_info->block_group_kobjs[index] = &rkobj->kobj;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008952 }
Jeff Mahoneyc1895442014-05-27 12:59:57 -04008953
8954 return;
8955out_err:
8956 pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n");
Yan, Zhengb742bb82010-05-16 10:46:24 -04008957}
8958
Miao Xie920e4a52014-01-15 20:00:55 +08008959static struct btrfs_block_group_cache *
8960btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
8961{
8962 struct btrfs_block_group_cache *cache;
8963
8964 cache = kzalloc(sizeof(*cache), GFP_NOFS);
8965 if (!cache)
8966 return NULL;
8967
8968 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8969 GFP_NOFS);
8970 if (!cache->free_space_ctl) {
8971 kfree(cache);
8972 return NULL;
8973 }
8974
8975 cache->key.objectid = start;
8976 cache->key.offset = size;
8977 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
8978
8979 cache->sectorsize = root->sectorsize;
8980 cache->fs_info = root->fs_info;
8981 cache->full_stripe_len = btrfs_full_stripe_len(root,
8982 &root->fs_info->mapping_tree,
8983 start);
8984 atomic_set(&cache->count, 1);
8985 spin_lock_init(&cache->lock);
Miao Xiee570fd22014-06-19 10:42:50 +08008986 init_rwsem(&cache->data_rwsem);
Miao Xie920e4a52014-01-15 20:00:55 +08008987 INIT_LIST_HEAD(&cache->list);
8988 INIT_LIST_HEAD(&cache->cluster_list);
8989 INIT_LIST_HEAD(&cache->new_bg_list);
8990 btrfs_init_free_space_ctl(cache);
8991
8992 return cache;
8993}
8994
Chris Mason9078a3e2007-04-26 16:46:15 -04008995int btrfs_read_block_groups(struct btrfs_root *root)
8996{
8997 struct btrfs_path *path;
8998 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008999 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04009000 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04009001 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04009002 struct btrfs_key key;
9003 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04009004 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04009005 int need_clear = 0;
9006 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04009007
Chris Masonbe744172007-05-06 10:15:01 -04009008 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04009009 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04009010 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04009011 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04009012 path = btrfs_alloc_path();
9013 if (!path)
9014 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04009015 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04009016
David Sterba6c417612011-04-13 15:41:04 +02009017 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04009018 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02009019 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04009020 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04009021 if (btrfs_test_opt(root, CLEAR_CACHE))
9022 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04009023
Chris Masond3977122009-01-05 21:25:51 -05009024 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04009025 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04009026 if (ret > 0)
9027 break;
Chris Mason0b86a832008-03-24 15:01:56 -04009028 if (ret != 0)
9029 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +08009030
Chris Mason5f39d392007-10-15 16:14:19 -04009031 leaf = path->nodes[0];
9032 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +08009033
9034 cache = btrfs_create_block_group_cache(root, found_key.objectid,
9035 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04009036 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04009037 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04009038 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04009039 }
Josef Bacik96303082009-07-13 21:29:25 -04009040
Liu Bocf7c1ef2012-07-06 03:31:34 -06009041 if (need_clear) {
9042 /*
9043 * When we mount with old space cache, we need to
9044 * set BTRFS_DC_CLEAR and set dirty flag.
9045 *
9046 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
9047 * truncate the old free space cache inode and
9048 * setup a new one.
9049 * b) Setting 'dirty flag' makes sure that we flush
9050 * the new space cache info onto disk.
9051 */
Josef Bacik0af3d002010-06-21 14:48:16 -04009052 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06009053 if (btrfs_test_opt(root, SPACE_CACHE))
9054 cache->dirty = 1;
9055 }
Josef Bacik0af3d002010-06-21 14:48:16 -04009056
Chris Mason5f39d392007-10-15 16:14:19 -04009057 read_extent_buffer(leaf, &cache->item,
9058 btrfs_item_ptr_offset(leaf, path->slots[0]),
9059 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +08009060 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason0b86a832008-03-24 15:01:56 -04009061
Chris Mason9078a3e2007-04-26 16:46:15 -04009062 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02009063 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +08009064
Josef Bacik817d52f2009-07-13 21:29:25 -04009065 /*
Josef Bacik3c148742011-02-02 15:53:47 +00009066 * We need to exclude the super stripes now so that the space
9067 * info has super bytes accounted for, otherwise we'll think
9068 * we have more space than we actually do.
9069 */
Josef Bacik835d9742013-03-19 12:13:25 -04009070 ret = exclude_super_stripes(root, cache);
9071 if (ret) {
9072 /*
9073 * We may have excluded something, so call this just in
9074 * case.
9075 */
9076 free_excluded_extents(root, cache);
Miao Xie920e4a52014-01-15 20:00:55 +08009077 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04009078 goto error;
9079 }
Josef Bacik3c148742011-02-02 15:53:47 +00009080
9081 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04009082 * check for two cases, either we are full, and therefore
9083 * don't need to bother with the caching work since we won't
9084 * find any space, or we are empty, and we can just add all
9085 * the space in and be done with it. This saves us _alot_ of
9086 * time, particularly in the full case.
9087 */
9088 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04009089 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04009090 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04009091 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04009092 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04009093 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04009094 cache->cached = BTRFS_CACHE_FINISHED;
9095 add_new_free_space(cache, root->fs_info,
9096 found_key.objectid,
9097 found_key.objectid +
9098 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04009099 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04009100 }
Chris Mason96b51792007-10-15 16:15:19 -04009101
Josef Bacik8c579fe2013-04-02 12:40:42 -04009102 ret = btrfs_add_block_group_cache(root->fs_info, cache);
9103 if (ret) {
9104 btrfs_remove_free_space_cache(cache);
9105 btrfs_put_block_group(cache);
9106 goto error;
9107 }
9108
Chris Mason6324fbf2008-03-24 15:01:59 -04009109 ret = update_space_info(info, cache->flags, found_key.offset,
9110 btrfs_block_group_used(&cache->item),
9111 &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04009112 if (ret) {
9113 btrfs_remove_free_space_cache(cache);
9114 spin_lock(&info->block_group_cache_lock);
9115 rb_erase(&cache->cache_node,
9116 &info->block_group_cache_tree);
9117 spin_unlock(&info->block_group_cache_lock);
9118 btrfs_put_block_group(cache);
9119 goto error;
9120 }
9121
Chris Mason6324fbf2008-03-24 15:01:59 -04009122 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04009123 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009124 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04009125 spin_unlock(&cache->space_info->lock);
9126
Yan, Zhengb742bb82010-05-16 10:46:24 -04009127 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04009128
Chris Mason75ccf472008-09-30 19:24:06 -04009129 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05009130 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00009131 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04009132 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04009133
9134 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
9135 if (!(get_alloc_profile(root, space_info->flags) &
9136 (BTRFS_BLOCK_GROUP_RAID10 |
9137 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05009138 BTRFS_BLOCK_GROUP_RAID5 |
9139 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -04009140 BTRFS_BLOCK_GROUP_DUP)))
9141 continue;
9142 /*
9143 * avoid allocating from un-mirrored block group if there are
9144 * mirrored block groups.
9145 */
chandan1095cc02013-07-16 12:28:56 +05309146 list_for_each_entry(cache,
9147 &space_info->block_groups[BTRFS_RAID_RAID0],
9148 list)
Miao Xie199c36e2011-07-15 10:34:36 +00009149 set_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +05309150 list_for_each_entry(cache,
9151 &space_info->block_groups[BTRFS_RAID_SINGLE],
9152 list)
Miao Xie199c36e2011-07-15 10:34:36 +00009153 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04009154 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04009155
9156 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04009157 ret = 0;
9158error:
Chris Mason9078a3e2007-04-26 16:46:15 -04009159 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04009160 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04009161}
Chris Mason6324fbf2008-03-24 15:01:59 -04009162
Josef Bacikea658ba2012-09-11 16:57:25 -04009163void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
9164 struct btrfs_root *root)
9165{
9166 struct btrfs_block_group_cache *block_group, *tmp;
9167 struct btrfs_root *extent_root = root->fs_info->extent_root;
9168 struct btrfs_block_group_item item;
9169 struct btrfs_key key;
9170 int ret = 0;
9171
9172 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
9173 new_bg_list) {
9174 list_del_init(&block_group->new_bg_list);
9175
9176 if (ret)
9177 continue;
9178
9179 spin_lock(&block_group->lock);
9180 memcpy(&item, &block_group->item, sizeof(item));
9181 memcpy(&key, &block_group->key, sizeof(key));
9182 spin_unlock(&block_group->lock);
9183
9184 ret = btrfs_insert_item(trans, extent_root, &key, &item,
9185 sizeof(item));
9186 if (ret)
9187 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacik6df9a952013-06-27 13:22:46 -04009188 ret = btrfs_finish_chunk_alloc(trans, extent_root,
9189 key.objectid, key.offset);
9190 if (ret)
9191 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacikea658ba2012-09-11 16:57:25 -04009192 }
9193}
9194
Chris Mason6324fbf2008-03-24 15:01:59 -04009195int btrfs_make_block_group(struct btrfs_trans_handle *trans,
9196 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04009197 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04009198 u64 size)
9199{
9200 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04009201 struct btrfs_root *extent_root;
9202 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04009203
9204 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04009205
Miao Xie995946d2014-04-02 19:51:06 +08009206 btrfs_set_log_full_commit(root->fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -04009207
Miao Xie920e4a52014-01-15 20:00:55 +08009208 cache = btrfs_create_block_group_cache(root, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -04009209 if (!cache)
9210 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08009211
Chris Mason6324fbf2008-03-24 15:01:59 -04009212 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04009213 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
Chris Mason6324fbf2008-03-24 15:01:59 -04009214 btrfs_set_block_group_flags(&cache->item, type);
9215
Miao Xie920e4a52014-01-15 20:00:55 +08009216 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -04009217 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04009218 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik835d9742013-03-19 12:13:25 -04009219 ret = exclude_super_stripes(root, cache);
9220 if (ret) {
9221 /*
9222 * We may have excluded something, so call this just in
9223 * case.
9224 */
9225 free_excluded_extents(root, cache);
Miao Xie920e4a52014-01-15 20:00:55 +08009226 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04009227 return ret;
9228 }
Josef Bacik96303082009-07-13 21:29:25 -04009229
Josef Bacik817d52f2009-07-13 21:29:25 -04009230 add_new_free_space(cache, root->fs_info, chunk_offset,
9231 chunk_offset + size);
9232
Yan Zheng11833d62009-09-11 16:11:19 -04009233 free_excluded_extents(root, cache);
9234
Josef Bacik8c579fe2013-04-02 12:40:42 -04009235 ret = btrfs_add_block_group_cache(root->fs_info, cache);
9236 if (ret) {
9237 btrfs_remove_free_space_cache(cache);
9238 btrfs_put_block_group(cache);
9239 return ret;
9240 }
9241
Chris Mason6324fbf2008-03-24 15:01:59 -04009242 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
9243 &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04009244 if (ret) {
9245 btrfs_remove_free_space_cache(cache);
9246 spin_lock(&root->fs_info->block_group_cache_lock);
9247 rb_erase(&cache->cache_node,
9248 &root->fs_info->block_group_cache_tree);
9249 spin_unlock(&root->fs_info->block_group_cache_lock);
9250 btrfs_put_block_group(cache);
9251 return ret;
9252 }
Li Zefanc7c144d2011-12-07 10:39:22 +08009253 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04009254
9255 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04009256 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04009257 spin_unlock(&cache->space_info->lock);
9258
Yan, Zhengb742bb82010-05-16 10:46:24 -04009259 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04009260
Josef Bacikea658ba2012-09-11 16:57:25 -04009261 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04009262
Chris Masond18a2c42008-04-04 15:40:00 -04009263 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04009264
Chris Mason6324fbf2008-03-24 15:01:59 -04009265 return 0;
9266}
Zheng Yan1a40e232008-09-26 10:09:34 -04009267
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02009268static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
9269{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03009270 u64 extra_flags = chunk_to_extended(flags) &
9271 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02009272
Miao Xiede98ced2013-01-29 10:13:12 +00009273 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02009274 if (flags & BTRFS_BLOCK_GROUP_DATA)
9275 fs_info->avail_data_alloc_bits &= ~extra_flags;
9276 if (flags & BTRFS_BLOCK_GROUP_METADATA)
9277 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
9278 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
9279 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00009280 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02009281}
9282
Zheng Yan1a40e232008-09-26 10:09:34 -04009283int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
9284 struct btrfs_root *root, u64 group_start)
9285{
9286 struct btrfs_path *path;
9287 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04009288 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04009289 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04009290 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04009291 struct inode *inode;
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009292 struct kobject *kobj = NULL;
Zheng Yan1a40e232008-09-26 10:09:34 -04009293 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02009294 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04009295 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04009296
Zheng Yan1a40e232008-09-26 10:09:34 -04009297 root = root->fs_info->extent_root;
9298
9299 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
9300 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05009301 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04009302
liubo9f7c43c2011-03-07 02:13:33 +00009303 /*
9304 * Free the reserved super bytes from this block group before
9305 * remove it.
9306 */
9307 free_excluded_extents(root, block_group);
9308
Zheng Yan1a40e232008-09-26 10:09:34 -04009309 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02009310 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04009311 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
9312 BTRFS_BLOCK_GROUP_RAID1 |
9313 BTRFS_BLOCK_GROUP_RAID10))
9314 factor = 2;
9315 else
9316 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04009317
Chris Mason44fb5512009-06-04 15:34:51 -04009318 /* make sure this block group isn't part of an allocation cluster */
9319 cluster = &root->fs_info->data_alloc_cluster;
9320 spin_lock(&cluster->refill_lock);
9321 btrfs_return_cluster_to_free_space(block_group, cluster);
9322 spin_unlock(&cluster->refill_lock);
9323
9324 /*
9325 * make sure this block group isn't part of a metadata
9326 * allocation cluster
9327 */
9328 cluster = &root->fs_info->meta_alloc_cluster;
9329 spin_lock(&cluster->refill_lock);
9330 btrfs_return_cluster_to_free_space(block_group, cluster);
9331 spin_unlock(&cluster->refill_lock);
9332
Zheng Yan1a40e232008-09-26 10:09:34 -04009333 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009334 if (!path) {
9335 ret = -ENOMEM;
9336 goto out;
9337 }
Zheng Yan1a40e232008-09-26 10:09:34 -04009338
Ilya Dryomov10b2f342011-10-02 13:56:53 +03009339 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04009340 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00009341 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009342 if (ret) {
9343 btrfs_add_delayed_iput(inode);
9344 goto out;
9345 }
Josef Bacik0af3d002010-06-21 14:48:16 -04009346 clear_nlink(inode);
9347 /* One for the block groups ref */
9348 spin_lock(&block_group->lock);
9349 if (block_group->iref) {
9350 block_group->iref = 0;
9351 block_group->inode = NULL;
9352 spin_unlock(&block_group->lock);
9353 iput(inode);
9354 } else {
9355 spin_unlock(&block_group->lock);
9356 }
9357 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04009358 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04009359 }
9360
9361 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
9362 key.offset = block_group->key.objectid;
9363 key.type = 0;
9364
9365 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
9366 if (ret < 0)
9367 goto out;
9368 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02009369 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04009370 if (ret == 0) {
9371 ret = btrfs_del_item(trans, tree_root, path);
9372 if (ret)
9373 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02009374 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04009375 }
9376
Yan Zheng3dfdb932009-01-21 10:49:16 -05009377 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04009378 rb_erase(&block_group->cache_node,
9379 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00009380
9381 if (root->fs_info->first_logical_byte == block_group->key.objectid)
9382 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05009383 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04009384
Josef Bacik80eb2342008-10-29 14:49:05 -04009385 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04009386 /*
9387 * we must use list_del_init so people can check to see if they
9388 * are still on the list after taking the semaphore
9389 */
9390 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009391 if (list_empty(&block_group->space_info->block_groups[index])) {
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009392 kobj = block_group->space_info->block_group_kobjs[index];
9393 block_group->space_info->block_group_kobjs[index] = NULL;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02009394 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04009395 }
Josef Bacik80eb2342008-10-29 14:49:05 -04009396 up_write(&block_group->space_info->groups_sem);
Jeff Mahoneyc1895442014-05-27 12:59:57 -04009397 if (kobj) {
9398 kobject_del(kobj);
9399 kobject_put(kobj);
9400 }
Zheng Yan1a40e232008-09-26 10:09:34 -04009401
Josef Bacik817d52f2009-07-13 21:29:25 -04009402 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04009403 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04009404
9405 btrfs_remove_free_space_cache(block_group);
9406
Yan Zhengc146afa2008-11-12 14:34:12 -05009407 spin_lock(&block_group->space_info->lock);
9408 block_group->space_info->total_bytes -= block_group->key.offset;
9409 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04009410 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05009411 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04009412
Josef Bacik0af3d002010-06-21 14:48:16 -04009413 memcpy(&key, &block_group->key, sizeof(key));
9414
Chris Mason283bb192009-07-24 16:30:55 -04009415 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05009416
Chris Masonfa9c0d792009-04-03 09:47:43 -04009417 btrfs_put_block_group(block_group);
9418 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04009419
9420 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
9421 if (ret > 0)
9422 ret = -EIO;
9423 if (ret < 0)
9424 goto out;
9425
9426 ret = btrfs_del_item(trans, root, path);
9427out:
9428 btrfs_free_path(path);
9429 return ret;
9430}
liuboacce9522011-01-06 19:30:25 +08009431
liuboc59021f2011-03-07 02:13:14 +00009432int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
9433{
9434 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00009435 struct btrfs_super_block *disk_super;
9436 u64 features;
9437 u64 flags;
9438 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00009439 int ret;
9440
David Sterba6c417612011-04-13 15:41:04 +02009441 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00009442 if (!btrfs_super_root(disk_super))
9443 return 1;
liuboc59021f2011-03-07 02:13:14 +00009444
liubo1aba86d2011-04-08 08:44:37 +00009445 features = btrfs_super_incompat_flags(disk_super);
9446 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
9447 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00009448
liubo1aba86d2011-04-08 08:44:37 +00009449 flags = BTRFS_BLOCK_GROUP_SYSTEM;
9450 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00009451 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00009452 goto out;
liuboc59021f2011-03-07 02:13:14 +00009453
liubo1aba86d2011-04-08 08:44:37 +00009454 if (mixed) {
9455 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
9456 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9457 } else {
9458 flags = BTRFS_BLOCK_GROUP_METADATA;
9459 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9460 if (ret)
9461 goto out;
9462
9463 flags = BTRFS_BLOCK_GROUP_DATA;
9464 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9465 }
9466out:
liuboc59021f2011-03-07 02:13:14 +00009467 return ret;
9468}
9469
liuboacce9522011-01-06 19:30:25 +08009470int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
9471{
9472 return unpin_extent_range(root, start, end);
9473}
9474
9475int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00009476 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08009477{
Li Dongyang5378e602011-03-24 10:24:27 +00009478 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08009479}
Li Dongyangf7039b12011-03-24 10:24:28 +00009480
9481int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
9482{
9483 struct btrfs_fs_info *fs_info = root->fs_info;
9484 struct btrfs_block_group_cache *cache = NULL;
9485 u64 group_trimmed;
9486 u64 start;
9487 u64 end;
9488 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08009489 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00009490 int ret = 0;
9491
Liu Bo2cac13e2012-02-09 18:17:41 +08009492 /*
9493 * try to trim all FS space, our block group may start from non-zero.
9494 */
9495 if (range->len == total_bytes)
9496 cache = btrfs_lookup_first_block_group(fs_info, range->start);
9497 else
9498 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00009499
9500 while (cache) {
9501 if (cache->key.objectid >= (range->start + range->len)) {
9502 btrfs_put_block_group(cache);
9503 break;
9504 }
9505
9506 start = max(range->start, cache->key.objectid);
9507 end = min(range->start + range->len,
9508 cache->key.objectid + cache->key.offset);
9509
9510 if (end - start >= range->minlen) {
9511 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00009512 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04009513 if (ret) {
9514 btrfs_put_block_group(cache);
9515 break;
9516 }
9517 ret = wait_block_group_cache_done(cache);
9518 if (ret) {
9519 btrfs_put_block_group(cache);
9520 break;
9521 }
Li Dongyangf7039b12011-03-24 10:24:28 +00009522 }
9523 ret = btrfs_trim_block_group(cache,
9524 &group_trimmed,
9525 start,
9526 end,
9527 range->minlen);
9528
9529 trimmed += group_trimmed;
9530 if (ret) {
9531 btrfs_put_block_group(cache);
9532 break;
9533 }
9534 }
9535
9536 cache = next_block_group(fs_info->tree_root, cache);
9537 }
9538
9539 range->len = trimmed;
9540 return ret;
9541}
Miao Xie8257b2d2014-03-06 13:38:19 +08009542
9543/*
9544 * btrfs_{start,end}_write() is similar to mnt_{want, drop}_write(),
9545 * they are used to prevent the some tasks writing data into the page cache
9546 * by nocow before the subvolume is snapshoted, but flush the data into
9547 * the disk after the snapshot creation.
9548 */
9549void btrfs_end_nocow_write(struct btrfs_root *root)
9550{
9551 percpu_counter_dec(&root->subv_writers->counter);
9552 /*
9553 * Make sure counter is updated before we wake up
9554 * waiters.
9555 */
9556 smp_mb();
9557 if (waitqueue_active(&root->subv_writers->wait))
9558 wake_up(&root->subv_writers->wait);
9559}
9560
9561int btrfs_start_nocow_write(struct btrfs_root *root)
9562{
9563 if (unlikely(atomic_read(&root->will_be_snapshoted)))
9564 return 0;
9565
9566 percpu_counter_inc(&root->subv_writers->counter);
9567 /*
9568 * Make sure counter is updated before we check for snapshot creation.
9569 */
9570 smp_mb();
9571 if (unlikely(atomic_read(&root->will_be_snapshoted))) {
9572 btrfs_end_nocow_write(root);
9573 return 0;
9574 }
9575 return 1;
9576}